-
+ E415FDE563A94473CFE9E85E8862CF30F6C8B820F383F6A964AEF675E576648BC9F66F55BA7EF01E27969FE57D14293B96DAAC1525197BE392ABB5C9B1AD4040
eucrypt/mpi/mpicoder.c
(0 . 0)(1 . 347)
4663 /* mpicoder.c - Coder for the external representation of MPIs
4664 * Modified by No Such Labs. (C) 2015. See README.
4665 *
4666 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
4667 * SHA256(gnupg-1.4.10.tar.gz):
4668 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
4669 * (C) 1994-2005 Free Software Foundation, Inc.
4670 *
4671 * This program is free software: you can redistribute it and/or modify
4672 * it under the terms of the GNU General Public License as published by
4673 * the Free Software Foundation, either version 3 of the License, or
4674 * (at your option) any later version.
4675 *
4676 * This program is distributed in the hope that it will be useful,
4677 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4678 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4679 * GNU General Public License for more details.
4680 *
4681 * You should have received a copy of the GNU General Public License
4682 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4683 */
4684
4685 #include <stdio.h>
4686 #include <string.h>
4687 #include <stdlib.h>
4688 #include <assert.h>
4689
4690 #include "knobs.h"
4691 #include "mpi.h"
4692 #include "mpi-internal.h"
4693 #include "memory.h"
4694 #include "util.h"
4695
4696 #ifdef M_DEBUG
4697 #undef mpi_read
4698 #endif
4699
4700 #define MAX_EXTERN_MPI_BITS 16384
4701
4702
4703 MPI
4704 mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
4705 {
4706 int i, j;
4707 unsigned nbits, nbytes, nlimbs, nread=0;
4708 mpi_limb_t a;
4709 MPI val = NULL;
4710
4711 if( *ret_nread < 2 )
4712 goto leave;
4713 nbits = buffer[0] << 8 | buffer[1];
4714 if( nbits > MAX_EXTERN_MPI_BITS ) {
4715 log_info ("mpi too large (%u bits)\n", nbits);
4716 goto leave;
4717 }
4718 buffer += 2;
4719 nread = 2;
4720
4721 nbytes = (nbits+7) / 8;
4722 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
4723 val = secure? mpi_alloc_secure( nlimbs )
4724 : mpi_alloc( nlimbs );
4725 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
4726 i %= BYTES_PER_MPI_LIMB;
4727 val->nbits = nbits;
4728 j= val->nlimbs = nlimbs;
4729 val->sign = 0;
4730 for( ; j > 0; j-- ) {
4731 a = 0;
4732 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
4733 if( ++nread > *ret_nread ) {
4734 /* This (as well as the above error condition) may
4735 happen if we use this function to parse a decrypted
4736 MPI which didn't turn out to be a real MPI - possible
4737 because the supplied key was wrong but the OpenPGP
4738 checksum didn't caught it. */
4739 log_info ("mpi larger than buffer\n");
4740 mpi_free (val);
4741 val = NULL;
4742 goto leave;
4743 }
4744 a <<= 8;
4745 a |= *buffer++;
4746 }
4747 i = 0;
4748 val->d[j-1] = a;
4749 }
4750
4751 leave:
4752 *ret_nread = nread;
4753 return val;
4754 }
4755
4756
4757 /****************
4758 * Make an mpi from a character string.
4759 */
4760 int
4761 mpi_fromstr(MPI val, const char *str)
4762 {
4763 int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
4764 unsigned nbits, nbytes, nlimbs;
4765 mpi_limb_t a;
4766
4767 if( *str == '-' ) {
4768 sign = 1;
4769 str++;
4770 }
4771 if( *str == '0' && str[1] == 'x' )
4772 hexmode = 1;
4773 else
4774 return 1; /* other bases are not yet supported */
4775 str += 2;
4776
4777 nbits = strlen(str)*4;
4778 if( nbits % 8 )
4779 prepend_zero = 1;
4780 nbytes = (nbits+7) / 8;
4781 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
4782 if( val->alloced < nlimbs )
4783 mpi_resize(val, nlimbs );
4784 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
4785 i %= BYTES_PER_MPI_LIMB;
4786 j= val->nlimbs = nlimbs;
4787 val->sign = sign;
4788 for( ; j > 0; j-- ) {
4789 a = 0;
4790 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
4791 if( prepend_zero ) {
4792 c1 = '0';
4793 prepend_zero = 0;
4794 }
4795 else
4796 c1 = *str++;
4797 assert(c1);
4798 c2 = *str++;
4799 assert(c2);
4800 if( c1 >= '0' && c1 <= '9' )
4801 c = c1 - '0';
4802 else if( c1 >= 'a' && c1 <= 'f' )
4803 c = c1 - 'a' + 10;
4804 else if( c1 >= 'A' && c1 <= 'F' )
4805 c = c1 - 'A' + 10;
4806 else {
4807 mpi_clear(val);
4808 return 1;
4809 }
4810 c <<= 4;
4811 if( c2 >= '0' && c2 <= '9' )
4812 c |= c2 - '0';
4813 else if( c2 >= 'a' && c2 <= 'f' )
4814 c |= c2 - 'a' + 10;
4815 else if( c2 >= 'A' && c2 <= 'F' )
4816 c |= c2 - 'A' + 10;
4817 else {
4818 mpi_clear(val);
4819 return 1;
4820 }
4821 a <<= 8;
4822 a |= c;
4823 }
4824 i = 0;
4825 val->d[j-1] = a;
4826 }
4827
4828 return 0;
4829 }
4830
4831
4832 /****************
4833 * print an MPI to the given stream and return the number of characters
4834 * printed.
4835 */
4836 int
4837 mpi_print( FILE *fp, MPI a, int mode )
4838 {
4839 int i, n=0;
4840
4841 if( a == NULL )
4842 return fprintf(fp, "[MPI_NULL]");
4843 if( !mode ) {
4844 unsigned int n1;
4845
4846 n1 = mpi_get_nbits(a);
4847 n += fprintf(fp, "[%u bits]", n1);
4848 }
4849 else {
4850 if( a->sign )
4851 putc('-', fp);
4852 #if BYTES_PER_MPI_LIMB == 2
4853 #define X "4"
4854 #elif BYTES_PER_MPI_LIMB == 4
4855 #define X "8"
4856 #elif BYTES_PER_MPI_LIMB == 8
4857 #define X "16"
4858 #else
4859 #error please define the format here
4860 #endif
4861 for(i=a->nlimbs; i > 0 ; i-- ) {
4862 n += fprintf(fp, i!=a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
4863 #undef X
4864 }
4865 if( !a->nlimbs )
4866 putc('0', fp );
4867 }
4868 return n;
4869 }
4870
4871
4872 /*
4873 void
4874 g10_log_mpidump( const char *text, MPI a )
4875 {
4876 FILE *fp = log_stream();
4877
4878 g10_log_print_prefix(text);
4879 mpi_print(fp, a, 1 );
4880 fputc('\n', fp);
4881 }
4882 */
4883
4884
4885 /****************
4886 * Return an xmalloced buffer with the MPI (msb first).
4887 * NBYTES receives the length of this buffer. Caller must free the
4888 * return string (This function does return a 0 byte buffer with NBYTES
4889 * set to zero if the value of A is zero. If sign is not NULL, it will
4890 * be set to the sign of the A.
4891 */
4892 static byte *
4893 do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
4894 {
4895 byte *p, *buffer;
4896 mpi_limb_t alimb;
4897 int i;
4898 unsigned int n;
4899
4900 if( sign )
4901 *sign = a->sign;
4902 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
4903 if (!n)
4904 n++; /* avoid zero length allocation */
4905 p = buffer = force_secure || mpi_is_secure(a) ? xmalloc_secure(n)
4906 : xmalloc(n);
4907
4908 for(i=a->nlimbs-1; i >= 0; i-- ) {
4909 alimb = a->d[i];
4910 #if BYTES_PER_MPI_LIMB == 4
4911 *p++ = alimb >> 24;
4912 *p++ = alimb >> 16;
4913 *p++ = alimb >> 8;
4914 *p++ = alimb ;
4915 #elif BYTES_PER_MPI_LIMB == 8
4916 *p++ = alimb >> 56;
4917 *p++ = alimb >> 48;
4918 *p++ = alimb >> 40;
4919 *p++ = alimb >> 32;
4920 *p++ = alimb >> 24;
4921 *p++ = alimb >> 16;
4922 *p++ = alimb >> 8;
4923 *p++ = alimb ;
4924 #else
4925 #error please implement for this limb size.
4926 #endif
4927 }
4928
4929 /* this is sub-optimal but we need to do the shift operation
4930 * because the caller has to free the returned buffer */
4931 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
4932 ;
4933 if( p != buffer )
4934 memmove(buffer,p, *nbytes);
4935
4936 return buffer;
4937 }
4938
4939
4940 byte *
4941 mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
4942 {
4943 return do_get_buffer( a, nbytes, sign, 0 );
4944 }
4945
4946 byte *
4947 mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign )
4948 {
4949 return do_get_buffer( a, nbytes, sign, 1 );
4950 }
4951
4952 /****************
4953 * Use BUFFER to update MPI.
4954 */
4955 void
4956 mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign )
4957 {
4958 const byte *p;
4959 mpi_limb_t alimb;
4960 int nlimbs;
4961 int i;
4962
4963 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
4964 RESIZE_IF_NEEDED(a, nlimbs);
4965 a->sign = sign;
4966
4967 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
4968 #if BYTES_PER_MPI_LIMB == 4
4969 alimb = (mpi_limb_t)*p-- ;
4970 alimb |= (mpi_limb_t)*p-- << 8 ;
4971 alimb |= (mpi_limb_t)*p-- << 16 ;
4972 alimb |= (mpi_limb_t)*p-- << 24 ;
4973 #elif BYTES_PER_MPI_LIMB == 8
4974 alimb = (mpi_limb_t)*p-- ;
4975 alimb |= (mpi_limb_t)*p-- << 8 ;
4976 alimb |= (mpi_limb_t)*p-- << 16 ;
4977 alimb |= (mpi_limb_t)*p-- << 24 ;
4978 alimb |= (mpi_limb_t)*p-- << 32 ;
4979 alimb |= (mpi_limb_t)*p-- << 40 ;
4980 alimb |= (mpi_limb_t)*p-- << 48 ;
4981 alimb |= (mpi_limb_t)*p-- << 56 ;
4982 #else
4983 #error please implement for this limb size.
4984 #endif
4985 a->d[i++] = alimb;
4986 }
4987 if( p >= buffer ) {
4988 #if BYTES_PER_MPI_LIMB == 4
4989 alimb = *p-- ;
4990 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
4991 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
4992 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
4993 #elif BYTES_PER_MPI_LIMB == 8
4994 alimb = (mpi_limb_t)*p-- ;
4995 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
4996 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
4997 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
4998 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
4999 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
5000 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
5001 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
5002 #else
5003 #error please implement for this limb size.
5004 #endif
5005 a->d[i++] = alimb;
5006 }
5007 a->nlimbs = i;
5008 assert( i == nlimbs );
5009 }