summaryrefslogtreecommitdiffstats
path: root/g10/build-packet.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2015-03-16 20:14:58 +0100
committerWerner Koch <wk@gnupg.org>2015-03-16 20:14:58 +0100
commitab17f7b6c392782718f57eaea94fc18a0ff49389 (patch)
treeee69e586534149e4d40a2f8dd0148a35069ee342 /g10/build-packet.c
parentgpg: Allow printing of MPI values in --list-mode. (diff)
downloadgnupg2-ab17f7b6c392782718f57eaea94fc18a0ff49389.tar.xz
gnupg2-ab17f7b6c392782718f57eaea94fc18a0ff49389.zip
gpg: Create all MPIs with RFC-4880 correct length headers.
* g10/build-packet.c (gpg_mpi_write): Strip leading zeroes. -- This used not to work with opaque MPI as returned by Libgcrypt from ECC operations. This patch fixes this. Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'g10/build-packet.c')
-rw-r--r--g10/build-packet.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/g10/build-packet.c b/g10/build-packet.c
index e984e3e1b..269c63ca9 100644
--- a/g10/build-packet.c
+++ b/g10/build-packet.c
@@ -164,10 +164,28 @@ gpg_mpi_write (iobuf_t out, gcry_mpi_t a)
if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
{
unsigned int nbits;
- const void *p;
+ const unsigned char *p;
unsigned char lenhdr[2];
+ /* gcry_log_debugmpi ("a", a); */
p = gcry_mpi_get_opaque (a, &nbits);
+ if (p)
+ {
+ /* Strip leading zero bits. */
+ for (; nbits >= 8 && !*p; p++, nbits -= 8)
+ ;
+ if (nbits >= 8 && !(*p & 0x80))
+ if (--nbits >= 7 && !(*p & 0x40))
+ if (--nbits >= 6 && !(*p & 0x20))
+ if (--nbits >= 5 && !(*p & 0x10))
+ if (--nbits >= 4 && !(*p & 0x08))
+ if (--nbits >= 3 && !(*p & 0x04))
+ if (--nbits >= 2 && !(*p & 0x02))
+ if (--nbits >= 1 && !(*p & 0x01))
+ --nbits;
+ }
+ /* gcry_log_debug (" [%u bit]\n", nbits); */
+ /* gcry_log_debughex (" ", p, (nbits+7)/8); */
lenhdr[0] = nbits >> 8;
lenhdr[1] = nbits;
rc = iobuf_write (out, lenhdr, 2);