1 /* mpicoder.c - Coder for the external representation of MPIs
2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "mpi-internal.h"
23 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
24 #define MAX_EXTERN_MPI_BITS 16384
27 static uint8_t asn[15] = /* Object ID is 1.3.14.3.2.26 */
28 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
29 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
34 do_encode_md(const void *sha_buffer, unsigned nbits)
36 int nframe = (nbits+7) / 8;
37 uint8_t *frame, *fr_pt;
39 size_t asnlen = DIM(asn);
42 if(SHA1_DIGEST_LENGTH + asnlen + 4 > nframe )
43 printk("MPI: can't encode a %d bit MD into a %d bits frame\n",
44 (int)(SHA1_DIGEST_LENGTH*8), (int)nbits);
46 /* We encode the MD in this way:
48 * 0 A PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
50 * PAD consists of FF bytes.
52 frame = kmalloc(nframe, GFP_KERNEL);
57 frame[n++] = 1; /* block type */
58 i = nframe - SHA1_DIGEST_LENGTH - asnlen -3 ;
61 printk("MPI: message digest encoding failed\n");
66 memset( frame+n, 0xff, i ); n += i;
68 memcpy( frame+n, &asn, asnlen ); n += asnlen;
69 memcpy( frame+n, sha_buffer, SHA1_DIGEST_LENGTH ); n += SHA1_DIGEST_LENGTH;
75 printk("MPI: message digest encoding failed, frame length is wrong\n");
80 a = mpi_alloc( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
81 mpi_set_buffer( a, frame, nframe, 0 );
89 mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
91 const uint8_t *buffer = xbuffer;
93 unsigned nbits, nbytes, nlimbs, nread=0;
99 nbits = buffer[0] << 8 | buffer[1];
101 if( nbits > MAX_EXTERN_MPI_BITS ) {
102 printk("MPI: mpi too large (%u bits)\n", nbits);
108 nbytes = (nbits+7) / 8;
109 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
110 val = mpi_alloc( nlimbs );
113 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
114 i %= BYTES_PER_MPI_LIMB;
116 j= val->nlimbs = nlimbs;
118 for( ; j > 0; j-- ) {
120 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
121 if( ++nread > *ret_nread ) {
122 printk("MPI: mpi larger than buffer nread=%d ret_nread=%d\n", nread, *ret_nread);
139 * Make an mpi from a character string.
142 mpi_fromstr(MPI val, const char *str)
144 int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
145 unsigned nbits, nbytes, nlimbs;
152 if( *str == '0' && str[1] == 'x' )
155 return -EINVAL; /* other bases are not yet supported */
158 nbits = strlen(str)*4;
161 nbytes = (nbits+7) / 8;
162 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
163 if( val->alloced < nlimbs )
164 if (!mpi_resize(val, nlimbs ))
166 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
167 i %= BYTES_PER_MPI_LIMB;
168 j= val->nlimbs = nlimbs;
170 for( ; j > 0; j-- ) {
172 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
182 if( c1 >= '0' && c1 <= '9' )
184 else if( c1 >= 'a' && c1 <= 'f' )
186 else if( c1 >= 'A' && c1 <= 'F' )
193 if( c2 >= '0' && c2 <= '9' )
195 else if( c2 >= 'a' && c2 <= 'f' )
197 else if( c2 >= 'A' && c2 <= 'F' )
216 * Special function to get the low 8 bytes from an mpi.
217 * This can be used as a keyid; KEYID is an 2 element array.
218 * Return the low 4 bytes.
221 mpi_get_keyid( const MPI a, u32 *keyid )
223 #if BYTES_PER_MPI_LIMB == 4
225 keyid[0] = a->nlimbs >= 2? a->d[1] : 0;
226 keyid[1] = a->nlimbs >= 1? a->d[0] : 0;
228 return a->nlimbs >= 1? a->d[0] : 0;
229 #elif BYTES_PER_MPI_LIMB == 8
231 keyid[0] = a->nlimbs? (u32)(a->d[0] >> 32) : 0;
232 keyid[1] = a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
234 return a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
236 #error Make this function work with other LIMB sizes
242 * Return an allocated buffer with the MPI (msb first).
243 * NBYTES receives the length of this buffer. Caller must free the
244 * return string (This function does return a 0 byte buffer with NBYTES
245 * set to zero if the value of A is zero. If sign is not NULL, it will
246 * be set to the sign of the A.
249 mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
258 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
260 n++; /* avoid zero length allocation */
261 p = buffer = kmalloc(n, GFP_KERNEL);
263 for(i=a->nlimbs-1; i >= 0; i-- ) {
265 #if BYTES_PER_MPI_LIMB == 4
270 #elif BYTES_PER_MPI_LIMB == 8
280 #error please implement for this limb size.
284 /* this is sub-optimal but we need to do the shift operation
285 * because the caller has to free the returned buffer */
286 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
289 memmove(buffer,p, *nbytes);
296 * Use BUFFER to update MPI.
299 mpi_set_buffer( MPI a, const void *xbuffer, unsigned nbytes, int sign )
301 const uint8_t *buffer = xbuffer, *p;
306 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
307 if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
311 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
312 #if BYTES_PER_MPI_LIMB == 4
313 alimb = (mpi_limb_t)*p-- ;
314 alimb |= (mpi_limb_t)*p-- << 8 ;
315 alimb |= (mpi_limb_t)*p-- << 16 ;
316 alimb |= (mpi_limb_t)*p-- << 24 ;
317 #elif BYTES_PER_MPI_LIMB == 8
318 alimb = (mpi_limb_t)*p-- ;
319 alimb |= (mpi_limb_t)*p-- << 8 ;
320 alimb |= (mpi_limb_t)*p-- << 16 ;
321 alimb |= (mpi_limb_t)*p-- << 24 ;
322 alimb |= (mpi_limb_t)*p-- << 32 ;
323 alimb |= (mpi_limb_t)*p-- << 40 ;
324 alimb |= (mpi_limb_t)*p-- << 48 ;
325 alimb |= (mpi_limb_t)*p-- << 56 ;
327 #error please implement for this limb size.
332 #if BYTES_PER_MPI_LIMB == 4
334 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
335 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
336 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
337 #elif BYTES_PER_MPI_LIMB == 8
338 alimb = (mpi_limb_t)*p-- ;
339 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 8 ;
340 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
341 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
342 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
343 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
344 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
345 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
347 #error please implement for this limb size.
354 printk("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i, nlimbs);