-
+ 6A4860EA723098A2E2BA703D8748F6D26C22D1AE72647A5CCCA67AB93976390B0D992BA870796BE3DA3B736B1A11B36F700A93214AE5926EC2652D1EDEE390A3
mpi/mpicoder.c
(0 . 0)(1 . 472)
8097 /* mpicoder.c - Coder for the external representation of MPIs
8098 * Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
8099 *
8100 * This file is part of GnuPG.
8101 *
8102 * GnuPG is free software; you can redistribute it and/or modify
8103 * it under the terms of the GNU General Public License as published by
8104 * the Free Software Foundation; either version 3 of the License, or
8105 * (at your option) any later version.
8106 *
8107 * GnuPG is distributed in the hope that it will be useful,
8108 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8109 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8110 * GNU General Public License for more details.
8111 *
8112 * You should have received a copy of the GNU General Public License
8113 * along with this program; if not, see <http://www.gnu.org/licenses/>.
8114 */
8115
8116 #include <config.h>
8117 #include <stdio.h>
8118 #include <string.h>
8119 #include <stdlib.h>
8120 #include <assert.h>
8121
8122 #include "mpi.h"
8123 #include "mpi-internal.h"
8124 #include "iobuf.h"
8125 #include "memory.h"
8126 #include "util.h"
8127
8128 #ifdef M_DEBUG
8129 #undef mpi_read
8130 #endif
8131
8132 #define MAX_EXTERN_MPI_BITS 16384
8133
8134 /****************
8135 * write an mpi to out.
8136 */
8137 int
8138 mpi_write( IOBUF out, MPI a )
8139 {
8140 int rc;
8141 unsigned nbits = mpi_get_nbits(a);
8142 byte *p, *buf;
8143 unsigned n;
8144
8145 if( nbits > MAX_EXTERN_MPI_BITS )
8146 log_bug("mpi_encode: mpi too large (%u bits)\n", nbits);
8147
8148 iobuf_put(out, (nbits >>8) );
8149 iobuf_put(out, (nbits) );
8150
8151 p = buf = mpi_get_buffer( a, &n, NULL );
8152 rc = iobuf_write( out, p, n );
8153 xfree(buf);
8154 return rc;
8155 }
8156
8157
8158 /****************
8159 * Read an external representation of an mpi and return the MPI
8160 * The external format is a 16 bit unsigned value stored in network byte order,
8161 * giving the number of bits for the following integer. The integer is stored
8162 * with MSB first (left padded with zeroes to align on a byte boundary).
8163 */
8164 MPI
8165 #ifdef M_DEBUG
8166 mpi_debug_read(IOBUF inp, unsigned *ret_nread, int secure, const char *info)
8167 #else
8168 mpi_read(IOBUF inp, unsigned *ret_nread, int secure)
8169 #endif
8170 {
8171 int c, i, j;
8172 unsigned int nmax = *ret_nread;
8173 unsigned nbits, nbytes, nlimbs, nread=0;
8174 mpi_limb_t a;
8175 MPI val = NULL;
8176
8177 if (nread == nmax)
8178 goto overflow;
8179 if( (c = iobuf_get(inp)) == -1 )
8180 goto leave;
8181 nread++;
8182 nbits = c << 8;
8183
8184 if (nread == nmax)
8185 goto overflow;
8186 if( (c = iobuf_get(inp)) == -1 )
8187 goto leave;
8188 nread++;
8189 nbits |= c;
8190
8191 if( nbits > MAX_EXTERN_MPI_BITS ) {
8192 log_error("mpi too large for this implementation (%u bits)\n", nbits);
8193 goto leave;
8194 }
8195
8196 nbytes = (nbits+7) / 8;
8197 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
8198 #ifdef M_DEBUG
8199 val = secure? mpi_debug_alloc_secure( nlimbs, info )
8200 : mpi_debug_alloc( nlimbs, info );
8201 #else
8202 val = secure? mpi_alloc_secure( nlimbs )
8203 : mpi_alloc( nlimbs );
8204 #endif
8205 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
8206 i %= BYTES_PER_MPI_LIMB;
8207 val->nbits = nbits;
8208 j= val->nlimbs = nlimbs;
8209 val->sign = 0;
8210 for( ; j > 0; j-- ) {
8211 a = 0;
8212 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
8213 if (nread == nmax) {
8214 #ifdef M_DEBUG
8215 mpi_debug_free (val);
8216 #else
8217 mpi_free (val);
8218 #endif
8219 val = NULL;
8220 goto overflow;
8221 }
8222 a <<= 8;
8223 a |= iobuf_get(inp) & 0xff; nread++;
8224 }
8225 i = 0;
8226 val->d[j-1] = a;
8227 }
8228
8229 leave:
8230 *ret_nread = nread;
8231 return val;
8232 overflow:
8233 log_error ("mpi larger than indicated length (%u bytes)\n", nmax);
8234 *ret_nread = nread;
8235 return val;
8236 }
8237
8238
8239 MPI
8240 mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
8241 {
8242 int i, j;
8243 unsigned nbits, nbytes, nlimbs, nread=0;
8244 mpi_limb_t a;
8245 MPI val = NULL;
8246
8247 if( *ret_nread < 2 )
8248 goto leave;
8249 nbits = buffer[0] << 8 | buffer[1];
8250 if( nbits > MAX_EXTERN_MPI_BITS ) {
8251 log_info ("mpi too large (%u bits)\n", nbits);
8252 goto leave;
8253 }
8254 buffer += 2;
8255 nread = 2;
8256
8257 nbytes = (nbits+7) / 8;
8258 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
8259 val = secure? mpi_alloc_secure( nlimbs )
8260 : mpi_alloc( nlimbs );
8261 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
8262 i %= BYTES_PER_MPI_LIMB;
8263 val->nbits = nbits;
8264 j= val->nlimbs = nlimbs;
8265 val->sign = 0;
8266 for( ; j > 0; j-- ) {
8267 a = 0;
8268 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
8269 if( ++nread > *ret_nread ) {
8270 /* This (as well as the above error condition) may
8271 happen if we use this function to parse a decrypted
8272 MPI which didn't turn out to be a real MPI - possible
8273 because the supplied key was wrong but the OpenPGP
8274 checksum didn't caught it. */
8275 log_info ("mpi larger than buffer\n");
8276 mpi_free (val);
8277 val = NULL;
8278 goto leave;
8279 }
8280 a <<= 8;
8281 a |= *buffer++;
8282 }
8283 i = 0;
8284 val->d[j-1] = a;
8285 }
8286
8287 leave:
8288 *ret_nread = nread;
8289 return val;
8290 }
8291
8292
8293 /****************
8294 * Make an mpi from a character string.
8295 */
8296 int
8297 mpi_fromstr(MPI val, const char *str)
8298 {
8299 int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
8300 unsigned nbits, nbytes, nlimbs;
8301 mpi_limb_t a;
8302
8303 if( *str == '-' ) {
8304 sign = 1;
8305 str++;
8306 }
8307 if( *str == '0' && str[1] == 'x' )
8308 hexmode = 1;
8309 else
8310 return 1; /* other bases are not yet supported */
8311 str += 2;
8312
8313 nbits = strlen(str)*4;
8314 if( nbits % 8 )
8315 prepend_zero = 1;
8316 nbytes = (nbits+7) / 8;
8317 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
8318 if( val->alloced < nlimbs )
8319 mpi_resize(val, nlimbs );
8320 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
8321 i %= BYTES_PER_MPI_LIMB;
8322 j= val->nlimbs = nlimbs;
8323 val->sign = sign;
8324 for( ; j > 0; j-- ) {
8325 a = 0;
8326 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
8327 if( prepend_zero ) {
8328 c1 = '0';
8329 prepend_zero = 0;
8330 }
8331 else
8332 c1 = *str++;
8333 assert(c1);
8334 c2 = *str++;
8335 assert(c2);
8336 if( c1 >= '0' && c1 <= '9' )
8337 c = c1 - '0';
8338 else if( c1 >= 'a' && c1 <= 'f' )
8339 c = c1 - 'a' + 10;
8340 else if( c1 >= 'A' && c1 <= 'F' )
8341 c = c1 - 'A' + 10;
8342 else {
8343 mpi_clear(val);
8344 return 1;
8345 }
8346 c <<= 4;
8347 if( c2 >= '0' && c2 <= '9' )
8348 c |= c2 - '0';
8349 else if( c2 >= 'a' && c2 <= 'f' )
8350 c |= c2 - 'a' + 10;
8351 else if( c2 >= 'A' && c2 <= 'F' )
8352 c |= c2 - 'A' + 10;
8353 else {
8354 mpi_clear(val);
8355 return 1;
8356 }
8357 a <<= 8;
8358 a |= c;
8359 }
8360 i = 0;
8361 val->d[j-1] = a;
8362 }
8363
8364 return 0;
8365 }
8366
8367
8368 /****************
8369 * print an MPI to the given stream and return the number of characters
8370 * printed.
8371 */
8372 int
8373 mpi_print( FILE *fp, MPI a, int mode )
8374 {
8375 int i, n=0;
8376
8377 if( a == NULL )
8378 return fprintf(fp, "[MPI_NULL]");
8379 if( !mode ) {
8380 unsigned int n1;
8381
8382 n1 = mpi_get_nbits(a);
8383 n += fprintf(fp, "[%u bits]", n1);
8384 }
8385 else {
8386 if( a->sign )
8387 putc('-', fp);
8388 #if BYTES_PER_MPI_LIMB == 2
8389 #define X "4"
8390 #elif BYTES_PER_MPI_LIMB == 4
8391 #define X "8"
8392 #elif BYTES_PER_MPI_LIMB == 8
8393 #define X "16"
8394 #else
8395 #error please define the format here
8396 #endif
8397 for(i=a->nlimbs; i > 0 ; i-- ) {
8398 n += fprintf(fp, i!=a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
8399 #undef X
8400 }
8401 if( !a->nlimbs )
8402 putc('0', fp );
8403 }
8404 return n;
8405 }
8406
8407
8408 void
8409 g10_log_mpidump( const char *text, MPI a )
8410 {
8411 FILE *fp = log_stream();
8412
8413 g10_log_print_prefix(text);
8414 mpi_print(fp, a, 1 );
8415 fputc('\n', fp);
8416 }
8417
8418 /****************
8419 * Special function to get the low 8 bytes from an mpi.
8420 * This can be used as a keyid; KEYID is an 2 element array.
8421 * Return the low 4 bytes.
8422 */
8423 u32
8424 mpi_get_keyid( MPI a, u32 *keyid )
8425 {
8426 #if BYTES_PER_MPI_LIMB == 4
8427 if( keyid ) {
8428 keyid[0] = a->nlimbs >= 2? a->d[1] : 0;
8429 keyid[1] = a->nlimbs >= 1? a->d[0] : 0;
8430 }
8431 return a->nlimbs >= 1? a->d[0] : 0;
8432 #elif BYTES_PER_MPI_LIMB == 8
8433 if( keyid ) {
8434 keyid[0] = a->nlimbs? (u32)(a->d[0] >> 32) : 0;
8435 keyid[1] = a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
8436 }
8437 return a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
8438 #else
8439 #error Make this function work with other LIMB sizes
8440 #endif
8441 }
8442
8443
8444 /****************
8445 * Return an xmalloced buffer with the MPI (msb first).
8446 * NBYTES receives the length of this buffer. Caller must free the
8447 * return string (This function does return a 0 byte buffer with NBYTES
8448 * set to zero if the value of A is zero. If sign is not NULL, it will
8449 * be set to the sign of the A.
8450 */
8451 static byte *
8452 do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
8453 {
8454 byte *p, *buffer;
8455 mpi_limb_t alimb;
8456 int i;
8457 unsigned int n;
8458
8459 if( sign )
8460 *sign = a->sign;
8461 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
8462 if (!n)
8463 n++; /* avoid zero length allocation */
8464 p = buffer = force_secure || mpi_is_secure(a) ? xmalloc_secure(n)
8465 : xmalloc(n);
8466
8467 for(i=a->nlimbs-1; i >= 0; i-- ) {
8468 alimb = a->d[i];
8469 #if BYTES_PER_MPI_LIMB == 4
8470 *p++ = alimb >> 24;
8471 *p++ = alimb >> 16;
8472 *p++ = alimb >> 8;
8473 *p++ = alimb ;
8474 #elif BYTES_PER_MPI_LIMB == 8
8475 *p++ = alimb >> 56;
8476 *p++ = alimb >> 48;
8477 *p++ = alimb >> 40;
8478 *p++ = alimb >> 32;
8479 *p++ = alimb >> 24;
8480 *p++ = alimb >> 16;
8481 *p++ = alimb >> 8;
8482 *p++ = alimb ;
8483 #else
8484 #error please implement for this limb size.
8485 #endif
8486 }
8487
8488 /* this is sub-optimal but we need to do the shift operation
8489 * because the caller has to free the returned buffer */
8490 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
8491 ;
8492 if( p != buffer )
8493 memmove(buffer,p, *nbytes);
8494
8495 return buffer;
8496 }
8497
8498
8499 byte *
8500 mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
8501 {
8502 return do_get_buffer( a, nbytes, sign, 0 );
8503 }
8504
8505 byte *
8506 mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign )
8507 {
8508 return do_get_buffer( a, nbytes, sign, 1 );
8509 }
8510
8511 /****************
8512 * Use BUFFER to update MPI.
8513 */
8514 void
8515 mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign )
8516 {
8517 const byte *p;
8518 mpi_limb_t alimb;
8519 int nlimbs;
8520 int i;
8521
8522 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
8523 RESIZE_IF_NEEDED(a, nlimbs);
8524 a->sign = sign;
8525
8526 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
8527 #if BYTES_PER_MPI_LIMB == 4
8528 alimb = (mpi_limb_t)*p-- ;
8529 alimb |= (mpi_limb_t)*p-- << 8 ;
8530 alimb |= (mpi_limb_t)*p-- << 16 ;
8531 alimb |= (mpi_limb_t)*p-- << 24 ;
8532 #elif BYTES_PER_MPI_LIMB == 8
8533 alimb = (mpi_limb_t)*p-- ;
8534 alimb |= (mpi_limb_t)*p-- << 8 ;
8535 alimb |= (mpi_limb_t)*p-- << 16 ;
8536 alimb |= (mpi_limb_t)*p-- << 24 ;
8537 alimb |= (mpi_limb_t)*p-- << 32 ;
8538 alimb |= (mpi_limb_t)*p-- << 40 ;
8539 alimb |= (mpi_limb_t)*p-- << 48 ;
8540 alimb |= (mpi_limb_t)*p-- << 56 ;
8541 #else
8542 #error please implement for this limb size.
8543 #endif
8544 a->d[i++] = alimb;
8545 }
8546 if( p >= buffer ) {
8547 #if BYTES_PER_MPI_LIMB == 4
8548 alimb = *p-- ;
8549 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
8550 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
8551 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
8552 #elif BYTES_PER_MPI_LIMB == 8
8553 alimb = (mpi_limb_t)*p-- ;
8554 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
8555 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
8556 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
8557 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
8558 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
8559 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
8560 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
8561 #else
8562 #error please implement for this limb size.
8563 #endif
8564 a->d[i++] = alimb;
8565 }
8566 a->nlimbs = i;
8567 assert( i == nlimbs );
8568 }