-
+ 6A4860EA723098A2E2BA703D8748F6D26C22D1AE72647A5CCCA67AB93976390B0D992BA870796BE3DA3B736B1A11B36F700A93214AE5926EC2652D1EDEE390A3
mpi/mpicoder.c
(0 . 0)(1 . 472)
6571 /* mpicoder.c - Coder for the external representation of MPIs
6572 * Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
6573 *
6574 * This file is part of GnuPG.
6575 *
6576 * GnuPG is free software; you can redistribute it and/or modify
6577 * it under the terms of the GNU General Public License as published by
6578 * the Free Software Foundation; either version 3 of the License, or
6579 * (at your option) any later version.
6580 *
6581 * GnuPG is distributed in the hope that it will be useful,
6582 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6583 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6584 * GNU General Public License for more details.
6585 *
6586 * You should have received a copy of the GNU General Public License
6587 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6588 */
6589
6590 #include <config.h>
6591 #include <stdio.h>
6592 #include <string.h>
6593 #include <stdlib.h>
6594 #include <assert.h>
6595
6596 #include "mpi.h"
6597 #include "mpi-internal.h"
6598 #include "iobuf.h"
6599 #include "memory.h"
6600 #include "util.h"
6601
6602 #ifdef M_DEBUG
6603 #undef mpi_read
6604 #endif
6605
6606 #define MAX_EXTERN_MPI_BITS 16384
6607
6608 /****************
6609 * write an mpi to out.
6610 */
6611 int
6612 mpi_write( IOBUF out, MPI a )
6613 {
6614 int rc;
6615 unsigned nbits = mpi_get_nbits(a);
6616 byte *p, *buf;
6617 unsigned n;
6618
6619 if( nbits > MAX_EXTERN_MPI_BITS )
6620 log_bug("mpi_encode: mpi too large (%u bits)\n", nbits);
6621
6622 iobuf_put(out, (nbits >>8) );
6623 iobuf_put(out, (nbits) );
6624
6625 p = buf = mpi_get_buffer( a, &n, NULL );
6626 rc = iobuf_write( out, p, n );
6627 xfree(buf);
6628 return rc;
6629 }
6630
6631
6632 /****************
6633 * Read an external representation of an mpi and return the MPI
6634 * The external format is a 16 bit unsigned value stored in network byte order,
6635 * giving the number of bits for the following integer. The integer is stored
6636 * with MSB first (left padded with zeroes to align on a byte boundary).
6637 */
6638 MPI
6639 #ifdef M_DEBUG
6640 mpi_debug_read(IOBUF inp, unsigned *ret_nread, int secure, const char *info)
6641 #else
6642 mpi_read(IOBUF inp, unsigned *ret_nread, int secure)
6643 #endif
6644 {
6645 int c, i, j;
6646 unsigned int nmax = *ret_nread;
6647 unsigned nbits, nbytes, nlimbs, nread=0;
6648 mpi_limb_t a;
6649 MPI val = NULL;
6650
6651 if (nread == nmax)
6652 goto overflow;
6653 if( (c = iobuf_get(inp)) == -1 )
6654 goto leave;
6655 nread++;
6656 nbits = c << 8;
6657
6658 if (nread == nmax)
6659 goto overflow;
6660 if( (c = iobuf_get(inp)) == -1 )
6661 goto leave;
6662 nread++;
6663 nbits |= c;
6664
6665 if( nbits > MAX_EXTERN_MPI_BITS ) {
6666 log_error("mpi too large for this implementation (%u bits)\n", nbits);
6667 goto leave;
6668 }
6669
6670 nbytes = (nbits+7) / 8;
6671 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
6672 #ifdef M_DEBUG
6673 val = secure? mpi_debug_alloc_secure( nlimbs, info )
6674 : mpi_debug_alloc( nlimbs, info );
6675 #else
6676 val = secure? mpi_alloc_secure( nlimbs )
6677 : mpi_alloc( nlimbs );
6678 #endif
6679 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
6680 i %= BYTES_PER_MPI_LIMB;
6681 val->nbits = nbits;
6682 j= val->nlimbs = nlimbs;
6683 val->sign = 0;
6684 for( ; j > 0; j-- ) {
6685 a = 0;
6686 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
6687 if (nread == nmax) {
6688 #ifdef M_DEBUG
6689 mpi_debug_free (val);
6690 #else
6691 mpi_free (val);
6692 #endif
6693 val = NULL;
6694 goto overflow;
6695 }
6696 a <<= 8;
6697 a |= iobuf_get(inp) & 0xff; nread++;
6698 }
6699 i = 0;
6700 val->d[j-1] = a;
6701 }
6702
6703 leave:
6704 *ret_nread = nread;
6705 return val;
6706 overflow:
6707 log_error ("mpi larger than indicated length (%u bytes)\n", nmax);
6708 *ret_nread = nread;
6709 return val;
6710 }
6711
6712
6713 MPI
6714 mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
6715 {
6716 int i, j;
6717 unsigned nbits, nbytes, nlimbs, nread=0;
6718 mpi_limb_t a;
6719 MPI val = NULL;
6720
6721 if( *ret_nread < 2 )
6722 goto leave;
6723 nbits = buffer[0] << 8 | buffer[1];
6724 if( nbits > MAX_EXTERN_MPI_BITS ) {
6725 log_info ("mpi too large (%u bits)\n", nbits);
6726 goto leave;
6727 }
6728 buffer += 2;
6729 nread = 2;
6730
6731 nbytes = (nbits+7) / 8;
6732 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
6733 val = secure? mpi_alloc_secure( nlimbs )
6734 : mpi_alloc( nlimbs );
6735 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
6736 i %= BYTES_PER_MPI_LIMB;
6737 val->nbits = nbits;
6738 j= val->nlimbs = nlimbs;
6739 val->sign = 0;
6740 for( ; j > 0; j-- ) {
6741 a = 0;
6742 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
6743 if( ++nread > *ret_nread ) {
6744 /* This (as well as the above error condition) may
6745 happen if we use this function to parse a decrypted
6746 MPI which didn't turn out to be a real MPI - possible
6747 because the supplied key was wrong but the OpenPGP
6748 checksum didn't caught it. */
6749 log_info ("mpi larger than buffer\n");
6750 mpi_free (val);
6751 val = NULL;
6752 goto leave;
6753 }
6754 a <<= 8;
6755 a |= *buffer++;
6756 }
6757 i = 0;
6758 val->d[j-1] = a;
6759 }
6760
6761 leave:
6762 *ret_nread = nread;
6763 return val;
6764 }
6765
6766
6767 /****************
6768 * Make an mpi from a character string.
6769 */
6770 int
6771 mpi_fromstr(MPI val, const char *str)
6772 {
6773 int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
6774 unsigned nbits, nbytes, nlimbs;
6775 mpi_limb_t a;
6776
6777 if( *str == '-' ) {
6778 sign = 1;
6779 str++;
6780 }
6781 if( *str == '0' && str[1] == 'x' )
6782 hexmode = 1;
6783 else
6784 return 1; /* other bases are not yet supported */
6785 str += 2;
6786
6787 nbits = strlen(str)*4;
6788 if( nbits % 8 )
6789 prepend_zero = 1;
6790 nbytes = (nbits+7) / 8;
6791 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
6792 if( val->alloced < nlimbs )
6793 mpi_resize(val, nlimbs );
6794 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
6795 i %= BYTES_PER_MPI_LIMB;
6796 j= val->nlimbs = nlimbs;
6797 val->sign = sign;
6798 for( ; j > 0; j-- ) {
6799 a = 0;
6800 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
6801 if( prepend_zero ) {
6802 c1 = '0';
6803 prepend_zero = 0;
6804 }
6805 else
6806 c1 = *str++;
6807 assert(c1);
6808 c2 = *str++;
6809 assert(c2);
6810 if( c1 >= '0' && c1 <= '9' )
6811 c = c1 - '0';
6812 else if( c1 >= 'a' && c1 <= 'f' )
6813 c = c1 - 'a' + 10;
6814 else if( c1 >= 'A' && c1 <= 'F' )
6815 c = c1 - 'A' + 10;
6816 else {
6817 mpi_clear(val);
6818 return 1;
6819 }
6820 c <<= 4;
6821 if( c2 >= '0' && c2 <= '9' )
6822 c |= c2 - '0';
6823 else if( c2 >= 'a' && c2 <= 'f' )
6824 c |= c2 - 'a' + 10;
6825 else if( c2 >= 'A' && c2 <= 'F' )
6826 c |= c2 - 'A' + 10;
6827 else {
6828 mpi_clear(val);
6829 return 1;
6830 }
6831 a <<= 8;
6832 a |= c;
6833 }
6834 i = 0;
6835 val->d[j-1] = a;
6836 }
6837
6838 return 0;
6839 }
6840
6841
6842 /****************
6843 * print an MPI to the given stream and return the number of characters
6844 * printed.
6845 */
6846 int
6847 mpi_print( FILE *fp, MPI a, int mode )
6848 {
6849 int i, n=0;
6850
6851 if( a == NULL )
6852 return fprintf(fp, "[MPI_NULL]");
6853 if( !mode ) {
6854 unsigned int n1;
6855
6856 n1 = mpi_get_nbits(a);
6857 n += fprintf(fp, "[%u bits]", n1);
6858 }
6859 else {
6860 if( a->sign )
6861 putc('-', fp);
6862 #if BYTES_PER_MPI_LIMB == 2
6863 #define X "4"
6864 #elif BYTES_PER_MPI_LIMB == 4
6865 #define X "8"
6866 #elif BYTES_PER_MPI_LIMB == 8
6867 #define X "16"
6868 #else
6869 #error please define the format here
6870 #endif
6871 for(i=a->nlimbs; i > 0 ; i-- ) {
6872 n += fprintf(fp, i!=a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
6873 #undef X
6874 }
6875 if( !a->nlimbs )
6876 putc('0', fp );
6877 }
6878 return n;
6879 }
6880
6881
6882 void
6883 g10_log_mpidump( const char *text, MPI a )
6884 {
6885 FILE *fp = log_stream();
6886
6887 g10_log_print_prefix(text);
6888 mpi_print(fp, a, 1 );
6889 fputc('\n', fp);
6890 }
6891
6892 /****************
6893 * Special function to get the low 8 bytes from an mpi.
6894 * This can be used as a keyid; KEYID is an 2 element array.
6895 * Return the low 4 bytes.
6896 */
6897 u32
6898 mpi_get_keyid( MPI a, u32 *keyid )
6899 {
6900 #if BYTES_PER_MPI_LIMB == 4
6901 if( keyid ) {
6902 keyid[0] = a->nlimbs >= 2? a->d[1] : 0;
6903 keyid[1] = a->nlimbs >= 1? a->d[0] : 0;
6904 }
6905 return a->nlimbs >= 1? a->d[0] : 0;
6906 #elif BYTES_PER_MPI_LIMB == 8
6907 if( keyid ) {
6908 keyid[0] = a->nlimbs? (u32)(a->d[0] >> 32) : 0;
6909 keyid[1] = a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
6910 }
6911 return a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
6912 #else
6913 #error Make this function work with other LIMB sizes
6914 #endif
6915 }
6916
6917
6918 /****************
6919 * Return an xmalloced buffer with the MPI (msb first).
6920 * NBYTES receives the length of this buffer. Caller must free the
6921 * return string (This function does return a 0 byte buffer with NBYTES
6922 * set to zero if the value of A is zero. If sign is not NULL, it will
6923 * be set to the sign of the A.
6924 */
6925 static byte *
6926 do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
6927 {
6928 byte *p, *buffer;
6929 mpi_limb_t alimb;
6930 int i;
6931 unsigned int n;
6932
6933 if( sign )
6934 *sign = a->sign;
6935 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
6936 if (!n)
6937 n++; /* avoid zero length allocation */
6938 p = buffer = force_secure || mpi_is_secure(a) ? xmalloc_secure(n)
6939 : xmalloc(n);
6940
6941 for(i=a->nlimbs-1; i >= 0; i-- ) {
6942 alimb = a->d[i];
6943 #if BYTES_PER_MPI_LIMB == 4
6944 *p++ = alimb >> 24;
6945 *p++ = alimb >> 16;
6946 *p++ = alimb >> 8;
6947 *p++ = alimb ;
6948 #elif BYTES_PER_MPI_LIMB == 8
6949 *p++ = alimb >> 56;
6950 *p++ = alimb >> 48;
6951 *p++ = alimb >> 40;
6952 *p++ = alimb >> 32;
6953 *p++ = alimb >> 24;
6954 *p++ = alimb >> 16;
6955 *p++ = alimb >> 8;
6956 *p++ = alimb ;
6957 #else
6958 #error please implement for this limb size.
6959 #endif
6960 }
6961
6962 /* this is sub-optimal but we need to do the shift operation
6963 * because the caller has to free the returned buffer */
6964 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
6965 ;
6966 if( p != buffer )
6967 memmove(buffer,p, *nbytes);
6968
6969 return buffer;
6970 }
6971
6972
6973 byte *
6974 mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
6975 {
6976 return do_get_buffer( a, nbytes, sign, 0 );
6977 }
6978
6979 byte *
6980 mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign )
6981 {
6982 return do_get_buffer( a, nbytes, sign, 1 );
6983 }
6984
6985 /****************
6986 * Use BUFFER to update MPI.
6987 */
6988 void
6989 mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign )
6990 {
6991 const byte *p;
6992 mpi_limb_t alimb;
6993 int nlimbs;
6994 int i;
6995
6996 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
6997 RESIZE_IF_NEEDED(a, nlimbs);
6998 a->sign = sign;
6999
7000 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
7001 #if BYTES_PER_MPI_LIMB == 4
7002 alimb = (mpi_limb_t)*p-- ;
7003 alimb |= (mpi_limb_t)*p-- << 8 ;
7004 alimb |= (mpi_limb_t)*p-- << 16 ;
7005 alimb |= (mpi_limb_t)*p-- << 24 ;
7006 #elif BYTES_PER_MPI_LIMB == 8
7007 alimb = (mpi_limb_t)*p-- ;
7008 alimb |= (mpi_limb_t)*p-- << 8 ;
7009 alimb |= (mpi_limb_t)*p-- << 16 ;
7010 alimb |= (mpi_limb_t)*p-- << 24 ;
7011 alimb |= (mpi_limb_t)*p-- << 32 ;
7012 alimb |= (mpi_limb_t)*p-- << 40 ;
7013 alimb |= (mpi_limb_t)*p-- << 48 ;
7014 alimb |= (mpi_limb_t)*p-- << 56 ;
7015 #else
7016 #error please implement for this limb size.
7017 #endif
7018 a->d[i++] = alimb;
7019 }
7020 if( p >= buffer ) {
7021 #if BYTES_PER_MPI_LIMB == 4
7022 alimb = *p-- ;
7023 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
7024 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
7025 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
7026 #elif BYTES_PER_MPI_LIMB == 8
7027 alimb = (mpi_limb_t)*p-- ;
7028 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
7029 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
7030 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
7031 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
7032 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
7033 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
7034 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
7035 #else
7036 #error please implement for this limb size.
7037 #endif
7038 a->d[i++] = alimb;
7039 }
7040 a->nlimbs = i;
7041 assert( i == nlimbs );
7042 }