-
+ E415FDE563A94473CFE9E85E8862CF30F6C8B820F383F6A964AEF675E576648BC9F66F55BA7EF01E27969FE57D14293B96DAAC1525197BE392ABB5C9B1AD4040
smg_comms/mpi/mpicoder.c
(0 . 0)(1 . 347)
5419 /* mpicoder.c - Coder for the external representation of MPIs
5420 * Modified by No Such Labs. (C) 2015. See README.
5421 *
5422 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
5423 * SHA256(gnupg-1.4.10.tar.gz):
5424 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
5425 * (C) 1994-2005 Free Software Foundation, Inc.
5426 *
5427 * This program is free software: you can redistribute it and/or modify
5428 * it under the terms of the GNU General Public License as published by
5429 * the Free Software Foundation, either version 3 of the License, or
5430 * (at your option) any later version.
5431 *
5432 * This program is distributed in the hope that it will be useful,
5433 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5434 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5435 * GNU General Public License for more details.
5436 *
5437 * You should have received a copy of the GNU General Public License
5438 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5439 */
5440
5441 #include <stdio.h>
5442 #include <string.h>
5443 #include <stdlib.h>
5444 #include <assert.h>
5445
5446 #include "knobs.h"
5447 #include "mpi.h"
5448 #include "mpi-internal.h"
5449 #include "memory.h"
5450 #include "util.h"
5451
5452 #ifdef M_DEBUG
5453 #undef mpi_read
5454 #endif
5455
5456 #define MAX_EXTERN_MPI_BITS 16384
5457
5458
5459 MPI
5460 mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
5461 {
5462 int i, j;
5463 unsigned nbits, nbytes, nlimbs, nread=0;
5464 mpi_limb_t a;
5465 MPI val = NULL;
5466
5467 if( *ret_nread < 2 )
5468 goto leave;
5469 nbits = buffer[0] << 8 | buffer[1];
5470 if( nbits > MAX_EXTERN_MPI_BITS ) {
5471 log_info ("mpi too large (%u bits)\n", nbits);
5472 goto leave;
5473 }
5474 buffer += 2;
5475 nread = 2;
5476
5477 nbytes = (nbits+7) / 8;
5478 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
5479 val = secure? mpi_alloc_secure( nlimbs )
5480 : mpi_alloc( nlimbs );
5481 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
5482 i %= BYTES_PER_MPI_LIMB;
5483 val->nbits = nbits;
5484 j= val->nlimbs = nlimbs;
5485 val->sign = 0;
5486 for( ; j > 0; j-- ) {
5487 a = 0;
5488 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
5489 if( ++nread > *ret_nread ) {
5490 /* This (as well as the above error condition) may
5491 happen if we use this function to parse a decrypted
5492 MPI which didn't turn out to be a real MPI - possible
5493 because the supplied key was wrong but the OpenPGP
5494 checksum didn't caught it. */
5495 log_info ("mpi larger than buffer\n");
5496 mpi_free (val);
5497 val = NULL;
5498 goto leave;
5499 }
5500 a <<= 8;
5501 a |= *buffer++;
5502 }
5503 i = 0;
5504 val->d[j-1] = a;
5505 }
5506
5507 leave:
5508 *ret_nread = nread;
5509 return val;
5510 }
5511
5512
5513 /****************
5514 * Make an mpi from a character string.
5515 */
5516 int
5517 mpi_fromstr(MPI val, const char *str)
5518 {
5519 int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
5520 unsigned nbits, nbytes, nlimbs;
5521 mpi_limb_t a;
5522
5523 if( *str == '-' ) {
5524 sign = 1;
5525 str++;
5526 }
5527 if( *str == '0' && str[1] == 'x' )
5528 hexmode = 1;
5529 else
5530 return 1; /* other bases are not yet supported */
5531 str += 2;
5532
5533 nbits = strlen(str)*4;
5534 if( nbits % 8 )
5535 prepend_zero = 1;
5536 nbytes = (nbits+7) / 8;
5537 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
5538 if( val->alloced < nlimbs )
5539 mpi_resize(val, nlimbs );
5540 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
5541 i %= BYTES_PER_MPI_LIMB;
5542 j= val->nlimbs = nlimbs;
5543 val->sign = sign;
5544 for( ; j > 0; j-- ) {
5545 a = 0;
5546 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
5547 if( prepend_zero ) {
5548 c1 = '0';
5549 prepend_zero = 0;
5550 }
5551 else
5552 c1 = *str++;
5553 assert(c1);
5554 c2 = *str++;
5555 assert(c2);
5556 if( c1 >= '0' && c1 <= '9' )
5557 c = c1 - '0';
5558 else if( c1 >= 'a' && c1 <= 'f' )
5559 c = c1 - 'a' + 10;
5560 else if( c1 >= 'A' && c1 <= 'F' )
5561 c = c1 - 'A' + 10;
5562 else {
5563 mpi_clear(val);
5564 return 1;
5565 }
5566 c <<= 4;
5567 if( c2 >= '0' && c2 <= '9' )
5568 c |= c2 - '0';
5569 else if( c2 >= 'a' && c2 <= 'f' )
5570 c |= c2 - 'a' + 10;
5571 else if( c2 >= 'A' && c2 <= 'F' )
5572 c |= c2 - 'A' + 10;
5573 else {
5574 mpi_clear(val);
5575 return 1;
5576 }
5577 a <<= 8;
5578 a |= c;
5579 }
5580 i = 0;
5581 val->d[j-1] = a;
5582 }
5583
5584 return 0;
5585 }
5586
5587
5588 /****************
5589 * print an MPI to the given stream and return the number of characters
5590 * printed.
5591 */
5592 int
5593 mpi_print( FILE *fp, MPI a, int mode )
5594 {
5595 int i, n=0;
5596
5597 if( a == NULL )
5598 return fprintf(fp, "[MPI_NULL]");
5599 if( !mode ) {
5600 unsigned int n1;
5601
5602 n1 = mpi_get_nbits(a);
5603 n += fprintf(fp, "[%u bits]", n1);
5604 }
5605 else {
5606 if( a->sign )
5607 putc('-', fp);
5608 #if BYTES_PER_MPI_LIMB == 2
5609 #define X "4"
5610 #elif BYTES_PER_MPI_LIMB == 4
5611 #define X "8"
5612 #elif BYTES_PER_MPI_LIMB == 8
5613 #define X "16"
5614 #else
5615 #error please define the format here
5616 #endif
5617 for(i=a->nlimbs; i > 0 ; i-- ) {
5618 n += fprintf(fp, i!=a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
5619 #undef X
5620 }
5621 if( !a->nlimbs )
5622 putc('0', fp );
5623 }
5624 return n;
5625 }
5626
5627
5628 /*
5629 void
5630 g10_log_mpidump( const char *text, MPI a )
5631 {
5632 FILE *fp = log_stream();
5633
5634 g10_log_print_prefix(text);
5635 mpi_print(fp, a, 1 );
5636 fputc('\n', fp);
5637 }
5638 */
5639
5640
5641 /****************
5642 * Return an xmalloced buffer with the MPI (msb first).
5643 * NBYTES receives the length of this buffer. Caller must free the
5644 * return string (This function does return a 0 byte buffer with NBYTES
5645 * set to zero if the value of A is zero. If sign is not NULL, it will
5646 * be set to the sign of the A.
5647 */
5648 static byte *
5649 do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
5650 {
5651 byte *p, *buffer;
5652 mpi_limb_t alimb;
5653 int i;
5654 unsigned int n;
5655
5656 if( sign )
5657 *sign = a->sign;
5658 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
5659 if (!n)
5660 n++; /* avoid zero length allocation */
5661 p = buffer = force_secure || mpi_is_secure(a) ? xmalloc_secure(n)
5662 : xmalloc(n);
5663
5664 for(i=a->nlimbs-1; i >= 0; i-- ) {
5665 alimb = a->d[i];
5666 #if BYTES_PER_MPI_LIMB == 4
5667 *p++ = alimb >> 24;
5668 *p++ = alimb >> 16;
5669 *p++ = alimb >> 8;
5670 *p++ = alimb ;
5671 #elif BYTES_PER_MPI_LIMB == 8
5672 *p++ = alimb >> 56;
5673 *p++ = alimb >> 48;
5674 *p++ = alimb >> 40;
5675 *p++ = alimb >> 32;
5676 *p++ = alimb >> 24;
5677 *p++ = alimb >> 16;
5678 *p++ = alimb >> 8;
5679 *p++ = alimb ;
5680 #else
5681 #error please implement for this limb size.
5682 #endif
5683 }
5684
5685 /* this is sub-optimal but we need to do the shift operation
5686 * because the caller has to free the returned buffer */
5687 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
5688 ;
5689 if( p != buffer )
5690 memmove(buffer,p, *nbytes);
5691
5692 return buffer;
5693 }
5694
5695
5696 byte *
5697 mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
5698 {
5699 return do_get_buffer( a, nbytes, sign, 0 );
5700 }
5701
5702 byte *
5703 mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign )
5704 {
5705 return do_get_buffer( a, nbytes, sign, 1 );
5706 }
5707
5708 /****************
5709 * Use BUFFER to update MPI.
5710 */
5711 void
5712 mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign )
5713 {
5714 const byte *p;
5715 mpi_limb_t alimb;
5716 int nlimbs;
5717 int i;
5718
5719 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
5720 RESIZE_IF_NEEDED(a, nlimbs);
5721 a->sign = sign;
5722
5723 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
5724 #if BYTES_PER_MPI_LIMB == 4
5725 alimb = (mpi_limb_t)*p-- ;
5726 alimb |= (mpi_limb_t)*p-- << 8 ;
5727 alimb |= (mpi_limb_t)*p-- << 16 ;
5728 alimb |= (mpi_limb_t)*p-- << 24 ;
5729 #elif BYTES_PER_MPI_LIMB == 8
5730 alimb = (mpi_limb_t)*p-- ;
5731 alimb |= (mpi_limb_t)*p-- << 8 ;
5732 alimb |= (mpi_limb_t)*p-- << 16 ;
5733 alimb |= (mpi_limb_t)*p-- << 24 ;
5734 alimb |= (mpi_limb_t)*p-- << 32 ;
5735 alimb |= (mpi_limb_t)*p-- << 40 ;
5736 alimb |= (mpi_limb_t)*p-- << 48 ;
5737 alimb |= (mpi_limb_t)*p-- << 56 ;
5738 #else
5739 #error please implement for this limb size.
5740 #endif
5741 a->d[i++] = alimb;
5742 }
5743 if( p >= buffer ) {
5744 #if BYTES_PER_MPI_LIMB == 4
5745 alimb = *p-- ;
5746 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
5747 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
5748 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
5749 #elif BYTES_PER_MPI_LIMB == 8
5750 alimb = (mpi_limb_t)*p-- ;
5751 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
5752 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
5753 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
5754 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
5755 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
5756 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
5757 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
5758 #else
5759 #error please implement for this limb size.
5760 #endif
5761 a->d[i++] = alimb;
5762 }
5763 a->nlimbs = i;
5764 assert( i == nlimbs );
5765 }