raw
mpi-genesis             1 /* mpicoder.c  -  Coder for the external representation of MPIs
mpi_second_cut 2 * Modified by No Such Labs. (C) 2015. See README.
mpi-genesis 3 *
mpi_second_cut 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
mpi_second_cut 5 * SHA256(gnupg-1.4.10.tar.gz):
mpi_second_cut 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
mpi_second_cut 7 * (C) 1994-2005 Free Software Foundation, Inc.
mpi-genesis 8 *
mpi_second_cut 9 * This program is free software: you can redistribute it and/or modify
mpi-genesis 10 * it under the terms of the GNU General Public License as published by
mpi_second_cut 11 * the Free Software Foundation, either version 3 of the License, or
mpi-genesis 12 * (at your option) any later version.
mpi-genesis 13 *
mpi_second_cut 14 * This program is distributed in the hope that it will be useful,
mpi-genesis 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 17 * GNU General Public License for more details.
mpi-genesis 18 *
mpi-genesis 19 * You should have received a copy of the GNU General Public License
mpi_second_cut 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
mpi-genesis 21 */
mpi-genesis 22
mpi-genesis 23 #include <stdio.h>
mpi-genesis 24 #include <string.h>
mpi-genesis 25 #include <stdlib.h>
mpi-genesis 26 #include <assert.h>
mpi-genesis 27
mpi_second_cut 28 #include "knobs.h"
mpi-genesis 29 #include "mpi.h"
mpi-genesis 30 #include "mpi-internal.h"
mpi-genesis 31 #include "memory.h"
mpi-genesis 32 #include "util.h"
mpi-genesis 33
mpi-genesis 34 #ifdef M_DEBUG
mpi-genesis 35 #undef mpi_read
mpi-genesis 36 #endif
mpi-genesis 37
mpi-genesis 38 #define MAX_EXTERN_MPI_BITS 16384
mpi-genesis 39
mpi-genesis 40
mpi-genesis 41 MPI
mpi-genesis 42 mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
mpi-genesis 43 {
mpi-genesis 44 int i, j;
mpi-genesis 45 unsigned nbits, nbytes, nlimbs, nread=0;
mpi-genesis 46 mpi_limb_t a;
mpi-genesis 47 MPI val = NULL;
mpi-genesis 48
mpi-genesis 49 if( *ret_nread < 2 )
mpi-genesis 50 goto leave;
mpi-genesis 51 nbits = buffer[0] << 8 | buffer[1];
mpi-genesis 52 if( nbits > MAX_EXTERN_MPI_BITS ) {
mpi-genesis 53 log_info ("mpi too large (%u bits)\n", nbits);
mpi-genesis 54 goto leave;
mpi-genesis 55 }
mpi-genesis 56 buffer += 2;
mpi-genesis 57 nread = 2;
mpi-genesis 58
mpi-genesis 59 nbytes = (nbits+7) / 8;
mpi-genesis 60 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
mpi-genesis 61 val = secure? mpi_alloc_secure( nlimbs )
mpi-genesis 62 : mpi_alloc( nlimbs );
mpi-genesis 63 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
mpi-genesis 64 i %= BYTES_PER_MPI_LIMB;
mpi-genesis 65 val->nbits = nbits;
mpi-genesis 66 j= val->nlimbs = nlimbs;
mpi-genesis 67 val->sign = 0;
mpi-genesis 68 for( ; j > 0; j-- ) {
mpi-genesis 69 a = 0;
mpi-genesis 70 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
mpi-genesis 71 if( ++nread > *ret_nread ) {
mpi-genesis 72 /* This (as well as the above error condition) may
mpi-genesis 73 happen if we use this function to parse a decrypted
mpi-genesis 74 MPI which didn't turn out to be a real MPI - possible
mpi-genesis 75 because the supplied key was wrong but the OpenPGP
mpi-genesis 76 checksum didn't caught it. */
mpi-genesis 77 log_info ("mpi larger than buffer\n");
mpi-genesis 78 mpi_free (val);
mpi-genesis 79 val = NULL;
mpi-genesis 80 goto leave;
mpi-genesis 81 }
mpi-genesis 82 a <<= 8;
mpi-genesis 83 a |= *buffer++;
mpi-genesis 84 }
mpi-genesis 85 i = 0;
mpi-genesis 86 val->d[j-1] = a;
mpi-genesis 87 }
mpi-genesis 88
mpi-genesis 89 leave:
mpi-genesis 90 *ret_nread = nread;
mpi-genesis 91 return val;
mpi-genesis 92 }
mpi-genesis 93
mpi-genesis 94
mpi-genesis 95 /****************
mpi-genesis 96 * Make an mpi from a character string.
mpi-genesis 97 */
mpi-genesis 98 int
mpi-genesis 99 mpi_fromstr(MPI val, const char *str)
mpi-genesis 100 {
mpi-genesis 101 int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
mpi-genesis 102 unsigned nbits, nbytes, nlimbs;
mpi-genesis 103 mpi_limb_t a;
mpi-genesis 104
mpi-genesis 105 if( *str == '-' ) {
mpi-genesis 106 sign = 1;
mpi-genesis 107 str++;
mpi-genesis 108 }
mpi-genesis 109 if( *str == '0' && str[1] == 'x' )
mpi-genesis 110 hexmode = 1;
mpi-genesis 111 else
mpi-genesis 112 return 1; /* other bases are not yet supported */
mpi-genesis 113 str += 2;
mpi-genesis 114
mpi-genesis 115 nbits = strlen(str)*4;
mpi-genesis 116 if( nbits % 8 )
mpi-genesis 117 prepend_zero = 1;
mpi-genesis 118 nbytes = (nbits+7) / 8;
mpi-genesis 119 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
mpi-genesis 120 if( val->alloced < nlimbs )
mpi-genesis 121 mpi_resize(val, nlimbs );
mpi-genesis 122 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
mpi-genesis 123 i %= BYTES_PER_MPI_LIMB;
mpi-genesis 124 j= val->nlimbs = nlimbs;
mpi-genesis 125 val->sign = sign;
mpi-genesis 126 for( ; j > 0; j-- ) {
mpi-genesis 127 a = 0;
mpi-genesis 128 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
mpi-genesis 129 if( prepend_zero ) {
mpi-genesis 130 c1 = '0';
mpi-genesis 131 prepend_zero = 0;
mpi-genesis 132 }
mpi-genesis 133 else
mpi-genesis 134 c1 = *str++;
mpi-genesis 135 assert(c1);
mpi-genesis 136 c2 = *str++;
mpi-genesis 137 assert(c2);
mpi-genesis 138 if( c1 >= '0' && c1 <= '9' )
mpi-genesis 139 c = c1 - '0';
mpi-genesis 140 else if( c1 >= 'a' && c1 <= 'f' )
mpi-genesis 141 c = c1 - 'a' + 10;
mpi-genesis 142 else if( c1 >= 'A' && c1 <= 'F' )
mpi-genesis 143 c = c1 - 'A' + 10;
mpi-genesis 144 else {
mpi-genesis 145 mpi_clear(val);
mpi-genesis 146 return 1;
mpi-genesis 147 }
mpi-genesis 148 c <<= 4;
mpi-genesis 149 if( c2 >= '0' && c2 <= '9' )
mpi-genesis 150 c |= c2 - '0';
mpi-genesis 151 else if( c2 >= 'a' && c2 <= 'f' )
mpi-genesis 152 c |= c2 - 'a' + 10;
mpi-genesis 153 else if( c2 >= 'A' && c2 <= 'F' )
mpi-genesis 154 c |= c2 - 'A' + 10;
mpi-genesis 155 else {
mpi-genesis 156 mpi_clear(val);
mpi-genesis 157 return 1;
mpi-genesis 158 }
mpi-genesis 159 a <<= 8;
mpi-genesis 160 a |= c;
mpi-genesis 161 }
mpi-genesis 162 i = 0;
mpi-genesis 163 val->d[j-1] = a;
mpi-genesis 164 }
mpi-genesis 165
mpi-genesis 166 return 0;
mpi-genesis 167 }
mpi-genesis 168
mpi-genesis 169
mpi-genesis 170 /****************
mpi-genesis 171 * print an MPI to the given stream and return the number of characters
mpi-genesis 172 * printed.
mpi-genesis 173 */
mpi-genesis 174 int
mpi-genesis 175 mpi_print( FILE *fp, MPI a, int mode )
mpi-genesis 176 {
mpi-genesis 177 int i, n=0;
mpi-genesis 178
mpi-genesis 179 if( a == NULL )
mpi-genesis 180 return fprintf(fp, "[MPI_NULL]");
mpi-genesis 181 if( !mode ) {
mpi-genesis 182 unsigned int n1;
mpi-genesis 183
mpi-genesis 184 n1 = mpi_get_nbits(a);
mpi-genesis 185 n += fprintf(fp, "[%u bits]", n1);
mpi-genesis 186 }
mpi-genesis 187 else {
mpi-genesis 188 if( a->sign )
mpi-genesis 189 putc('-', fp);
mpi-genesis 190 #if BYTES_PER_MPI_LIMB == 2
mpi-genesis 191 #define X "4"
mpi-genesis 192 #elif BYTES_PER_MPI_LIMB == 4
mpi-genesis 193 #define X "8"
mpi-genesis 194 #elif BYTES_PER_MPI_LIMB == 8
mpi-genesis 195 #define X "16"
mpi-genesis 196 #else
mpi-genesis 197 #error please define the format here
mpi-genesis 198 #endif
mpi-genesis 199 for(i=a->nlimbs; i > 0 ; i-- ) {
mpi-genesis 200 n += fprintf(fp, i!=a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
mpi-genesis 201 #undef X
mpi-genesis 202 }
mpi-genesis 203 if( !a->nlimbs )
mpi-genesis 204 putc('0', fp );
mpi-genesis 205 }
mpi-genesis 206 return n;
mpi-genesis 207 }
mpi-genesis 208
mpi-genesis 209
mpi_second_cut 210 /*
mpi-genesis 211 void
mpi-genesis 212 g10_log_mpidump( const char *text, MPI a )
mpi-genesis 213 {
mpi-genesis 214 FILE *fp = log_stream();
mpi-genesis 215
mpi-genesis 216 g10_log_print_prefix(text);
mpi-genesis 217 mpi_print(fp, a, 1 );
mpi-genesis 218 fputc('\n', fp);
mpi-genesis 219 }
mpi_second_cut 220 */
mpi-genesis 221
mpi-genesis 222
mpi-genesis 223 /****************
mpi-genesis 224 * Return an xmalloced buffer with the MPI (msb first).
mpi-genesis 225 * NBYTES receives the length of this buffer. Caller must free the
mpi-genesis 226 * return string (This function does return a 0 byte buffer with NBYTES
mpi-genesis 227 * set to zero if the value of A is zero. If sign is not NULL, it will
mpi-genesis 228 * be set to the sign of the A.
mpi-genesis 229 */
mpi-genesis 230 static byte *
mpi-genesis 231 do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
mpi-genesis 232 {
mpi-genesis 233 byte *p, *buffer;
mpi-genesis 234 mpi_limb_t alimb;
mpi-genesis 235 int i;
mpi-genesis 236 unsigned int n;
mpi-genesis 237
mpi-genesis 238 if( sign )
mpi-genesis 239 *sign = a->sign;
mpi-genesis 240 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
mpi-genesis 241 if (!n)
mpi-genesis 242 n++; /* avoid zero length allocation */
mpi-genesis 243 p = buffer = force_secure || mpi_is_secure(a) ? xmalloc_secure(n)
mpi-genesis 244 : xmalloc(n);
mpi-genesis 245
mpi-genesis 246 for(i=a->nlimbs-1; i >= 0; i-- ) {
mpi-genesis 247 alimb = a->d[i];
mpi-genesis 248 #if BYTES_PER_MPI_LIMB == 4
mpi-genesis 249 *p++ = alimb >> 24;
mpi-genesis 250 *p++ = alimb >> 16;
mpi-genesis 251 *p++ = alimb >> 8;
mpi-genesis 252 *p++ = alimb ;
mpi-genesis 253 #elif BYTES_PER_MPI_LIMB == 8
mpi-genesis 254 *p++ = alimb >> 56;
mpi-genesis 255 *p++ = alimb >> 48;
mpi-genesis 256 *p++ = alimb >> 40;
mpi-genesis 257 *p++ = alimb >> 32;
mpi-genesis 258 *p++ = alimb >> 24;
mpi-genesis 259 *p++ = alimb >> 16;
mpi-genesis 260 *p++ = alimb >> 8;
mpi-genesis 261 *p++ = alimb ;
mpi-genesis 262 #else
mpi-genesis 263 #error please implement for this limb size.
mpi-genesis 264 #endif
mpi-genesis 265 }
mpi-genesis 266
mpi-genesis 267 /* this is sub-optimal but we need to do the shift operation
mpi-genesis 268 * because the caller has to free the returned buffer */
mpi-genesis 269 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
mpi-genesis 270 ;
mpi-genesis 271 if( p != buffer )
mpi-genesis 272 memmove(buffer,p, *nbytes);
mpi-genesis 273
mpi-genesis 274 return buffer;
mpi-genesis 275 }
mpi-genesis 276
mpi-genesis 277
mpi-genesis 278 byte *
mpi-genesis 279 mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
mpi-genesis 280 {
mpi-genesis 281 return do_get_buffer( a, nbytes, sign, 0 );
mpi-genesis 282 }
mpi-genesis 283
mpi-genesis 284 byte *
mpi-genesis 285 mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign )
mpi-genesis 286 {
mpi-genesis 287 return do_get_buffer( a, nbytes, sign, 1 );
mpi-genesis 288 }
mpi-genesis 289
mpi-genesis 290 /****************
mpi-genesis 291 * Use BUFFER to update MPI.
mpi-genesis 292 */
mpi-genesis 293 void
mpi-genesis 294 mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign )
mpi-genesis 295 {
mpi-genesis 296 const byte *p;
mpi-genesis 297 mpi_limb_t alimb;
mpi-genesis 298 int nlimbs;
mpi-genesis 299 int i;
mpi-genesis 300
mpi-genesis 301 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
mpi-genesis 302 RESIZE_IF_NEEDED(a, nlimbs);
mpi-genesis 303 a->sign = sign;
mpi-genesis 304
mpi-genesis 305 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
mpi-genesis 306 #if BYTES_PER_MPI_LIMB == 4
mpi-genesis 307 alimb = (mpi_limb_t)*p-- ;
mpi-genesis 308 alimb |= (mpi_limb_t)*p-- << 8 ;
mpi-genesis 309 alimb |= (mpi_limb_t)*p-- << 16 ;
mpi-genesis 310 alimb |= (mpi_limb_t)*p-- << 24 ;
mpi-genesis 311 #elif BYTES_PER_MPI_LIMB == 8
mpi-genesis 312 alimb = (mpi_limb_t)*p-- ;
mpi-genesis 313 alimb |= (mpi_limb_t)*p-- << 8 ;
mpi-genesis 314 alimb |= (mpi_limb_t)*p-- << 16 ;
mpi-genesis 315 alimb |= (mpi_limb_t)*p-- << 24 ;
mpi-genesis 316 alimb |= (mpi_limb_t)*p-- << 32 ;
mpi-genesis 317 alimb |= (mpi_limb_t)*p-- << 40 ;
mpi-genesis 318 alimb |= (mpi_limb_t)*p-- << 48 ;
mpi-genesis 319 alimb |= (mpi_limb_t)*p-- << 56 ;
mpi-genesis 320 #else
mpi-genesis 321 #error please implement for this limb size.
mpi-genesis 322 #endif
mpi-genesis 323 a->d[i++] = alimb;
mpi-genesis 324 }
mpi-genesis 325 if( p >= buffer ) {
mpi-genesis 326 #if BYTES_PER_MPI_LIMB == 4
mpi-genesis 327 alimb = *p-- ;
mpi-genesis 328 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
mpi-genesis 329 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
mpi-genesis 330 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
mpi-genesis 331 #elif BYTES_PER_MPI_LIMB == 8
mpi-genesis 332 alimb = (mpi_limb_t)*p-- ;
mpi-genesis 333 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
mpi-genesis 334 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
mpi-genesis 335 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
mpi-genesis 336 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
mpi-genesis 337 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
mpi-genesis 338 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
mpi-genesis 339 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
mpi-genesis 340 #else
mpi-genesis 341 #error please implement for this limb size.
mpi-genesis 342 #endif
mpi-genesis 343 a->d[i++] = alimb;
mpi-genesis 344 }
mpi-genesis 345 a->nlimbs = i;
mpi-genesis 346 assert( i == nlimbs );
mpi-genesis 347 }