~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to mpi/mpicoder.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* mpicoder.c  -  Coder for the external representation of MPIs
2
 
 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3
 
 *
4
 
 * This file is part of GnuPG.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#include <config.h>
22
 
#include <stdio.h>
23
 
#include <string.h>
24
 
#include <stdlib.h>
25
 
#include <assert.h>
26
 
 
27
 
#include "mpi.h"
28
 
#include "mpi-internal.h"
29
 
#include "iobuf.h"
30
 
#include "memory.h"
31
 
#include "util.h"
32
 
 
33
 
#ifdef M_DEBUG
34
 
#undef mpi_read
35
 
#endif
36
 
 
37
 
#define MAX_EXTERN_MPI_BITS 16384
38
 
 
39
 
/****************
40
 
 * write an mpi to out.
41
 
 */
42
 
int
43
 
mpi_write( IOBUF out, MPI a )
44
 
{
45
 
    int rc;
46
 
    unsigned nbits = mpi_get_nbits(a);
47
 
    byte *p, *buf;
48
 
    unsigned n;
49
 
 
50
 
    if( nbits > MAX_EXTERN_MPI_BITS )
51
 
        log_bug("mpi_encode: mpi too large (%u bits)\n", nbits);
52
 
 
53
 
    iobuf_put(out, (nbits >>8) );
54
 
    iobuf_put(out, (nbits) );
55
 
 
56
 
    p = buf = mpi_get_buffer( a, &n, NULL );
57
 
    rc = iobuf_write( out, p, n );
58
 
    m_free(buf);
59
 
    return rc;
60
 
}
61
 
 
62
 
 
63
 
/****************
64
 
 * Read an external representation of an mpi and return the MPI
65
 
 * The external format is a 16 bit unsigned value stored in network byte order,
66
 
 * giving the number of bits for the following integer. The integer is stored
67
 
 * with MSB first (left padded with zeroes to align on a byte boundary).
68
 
 */
69
 
MPI
70
 
#ifdef M_DEBUG
71
 
mpi_debug_read(IOBUF inp, unsigned *ret_nread, int secure, const char *info)
72
 
#else
73
 
mpi_read(IOBUF inp, unsigned *ret_nread, int secure)
74
 
#endif
75
 
{
76
 
    int c, i, j;
77
 
    unsigned nbits, nbytes, nlimbs, nread=0;
78
 
    mpi_limb_t a;
79
 
    MPI val = MPI_NULL;
80
 
 
81
 
    if( (c = iobuf_get(inp)) == -1 )
82
 
        goto leave;
83
 
    nread++;
84
 
    nbits = c << 8;
85
 
    if( (c = iobuf_get(inp)) == -1 )
86
 
        goto leave;
87
 
    nread++;
88
 
    nbits |= c;
89
 
    if( nbits > MAX_EXTERN_MPI_BITS ) {
90
 
        log_error("mpi too large (%u bits)\n", nbits);
91
 
        goto leave;
92
 
    }
93
 
 
94
 
    nbytes = (nbits+7) / 8;
95
 
    nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
96
 
#ifdef M_DEBUG
97
 
    val = secure? mpi_debug_alloc_secure( nlimbs, info )
98
 
                : mpi_debug_alloc( nlimbs, info );
99
 
#else
100
 
    val = secure? mpi_alloc_secure( nlimbs )
101
 
                : mpi_alloc( nlimbs );
102
 
#endif
103
 
    i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
104
 
    i %= BYTES_PER_MPI_LIMB;
105
 
    val->nbits = nbits;
106
 
    j= val->nlimbs = nlimbs;
107
 
    val->sign = 0;
108
 
    for( ; j > 0; j-- ) {
109
 
        a = 0;
110
 
        for(; i < BYTES_PER_MPI_LIMB; i++ ) {
111
 
            a <<= 8;
112
 
            a |= iobuf_get(inp) & 0xff; nread++;
113
 
        }
114
 
        i = 0;
115
 
        val->d[j-1] = a;
116
 
    }
117
 
 
118
 
  leave:
119
 
    if( nread > *ret_nread )
120
 
        log_bug("mpi crosses packet border\n");
121
 
    else
122
 
        *ret_nread = nread;
123
 
    return val;
124
 
}
125
 
 
126
 
 
127
 
MPI
128
 
mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
129
 
{
130
 
    int i, j;
131
 
    unsigned nbits, nbytes, nlimbs, nread=0;
132
 
    mpi_limb_t a;
133
 
    MPI val = MPI_NULL;
134
 
 
135
 
    if( *ret_nread < 2 )
136
 
        goto leave;
137
 
    nbits = buffer[0] << 8 | buffer[1];
138
 
    if( nbits > MAX_EXTERN_MPI_BITS ) {
139
 
        log_info ("mpi too large (%u bits)\n", nbits);
140
 
        goto leave;
141
 
    }
142
 
    buffer += 2;
143
 
    nread = 2;
144
 
 
145
 
    nbytes = (nbits+7) / 8;
146
 
    nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
147
 
    val = secure? mpi_alloc_secure( nlimbs )
148
 
                : mpi_alloc( nlimbs );
149
 
    i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
150
 
    i %= BYTES_PER_MPI_LIMB;
151
 
    val->nbits = nbits;
152
 
    j= val->nlimbs = nlimbs;
153
 
    val->sign = 0;
154
 
    for( ; j > 0; j-- ) {
155
 
        a = 0;
156
 
        for(; i < BYTES_PER_MPI_LIMB; i++ ) {
157
 
          if( ++nread > *ret_nread ) {
158
 
              /* This (as well as the above error condition) may
159
 
                 happen if we use this function to parse a decrypted
160
 
                 MPI which didn't turn out to be a real MPI - possible
161
 
                 because the supplied key was wrong but the OpenPGP
162
 
                 checksum didn't caught it. */
163
 
                log_info ("mpi larger than buffer\n");
164
 
                mpi_free (val);
165
 
                val = MPI_NULL;
166
 
                goto leave;
167
 
          }
168
 
          a <<= 8;
169
 
          a |= *buffer++;
170
 
        }
171
 
        i = 0;
172
 
        val->d[j-1] = a;
173
 
    }
174
 
 
175
 
  leave:
176
 
    *ret_nread = nread;
177
 
    return val;
178
 
}
179
 
 
180
 
 
181
 
/****************
182
 
 * Make an mpi from a character string.
183
 
 */
184
 
int
185
 
mpi_fromstr(MPI val, const char *str)
186
 
{
187
 
    int hexmode=0, sign=0, prepend_zero=0, i, j, c, c1, c2;
188
 
    unsigned nbits, nbytes, nlimbs;
189
 
    mpi_limb_t a;
190
 
 
191
 
    if( *str == '-' ) {
192
 
        sign = 1;
193
 
        str++;
194
 
    }
195
 
    if( *str == '0' && str[1] == 'x' )
196
 
        hexmode = 1;
197
 
    else
198
 
        return 1; /* other bases are not yet supported */
199
 
    str += 2;
200
 
 
201
 
    nbits = strlen(str)*4;
202
 
    if( nbits % 8 )
203
 
        prepend_zero = 1;
204
 
    nbytes = (nbits+7) / 8;
205
 
    nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
206
 
    if( val->alloced < nlimbs )
207
 
        mpi_resize(val, nlimbs );
208
 
    i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
209
 
    i %= BYTES_PER_MPI_LIMB;
210
 
    j= val->nlimbs = nlimbs;
211
 
    val->sign = sign;
212
 
    for( ; j > 0; j-- ) {
213
 
        a = 0;
214
 
        for(; i < BYTES_PER_MPI_LIMB; i++ ) {
215
 
            if( prepend_zero ) {
216
 
                c1 = '0';
217
 
                prepend_zero = 0;
218
 
            }
219
 
            else
220
 
                c1 = *str++;
221
 
            assert(c1);
222
 
            c2 = *str++;
223
 
            assert(c2);
224
 
            if( c1 >= '0' && c1 <= '9' )
225
 
                c = c1 - '0';
226
 
            else if( c1 >= 'a' && c1 <= 'f' )
227
 
                c = c1 - 'a' + 10;
228
 
            else if( c1 >= 'A' && c1 <= 'F' )
229
 
                c = c1 - 'A' + 10;
230
 
            else {
231
 
                mpi_clear(val);
232
 
                return 1;
233
 
            }
234
 
            c <<= 4;
235
 
            if( c2 >= '0' && c2 <= '9' )
236
 
                c |= c2 - '0';
237
 
            else if( c2 >= 'a' && c2 <= 'f' )
238
 
                c |= c2 - 'a' + 10;
239
 
            else if( c2 >= 'A' && c2 <= 'F' )
240
 
                c |= c2 - 'A' + 10;
241
 
            else {
242
 
                mpi_clear(val);
243
 
                return 1;
244
 
            }
245
 
            a <<= 8;
246
 
            a |= c;
247
 
        }
248
 
        i = 0;
249
 
        val->d[j-1] = a;
250
 
    }
251
 
 
252
 
    return 0;
253
 
}
254
 
 
255
 
 
256
 
/****************
257
 
 * print an MPI to the given stream and return the number of characters
258
 
 * printed.
259
 
 */
260
 
int
261
 
mpi_print( FILE *fp, MPI a, int mode )
262
 
{
263
 
    int i, n=0;
264
 
 
265
 
    if( a == MPI_NULL )
266
 
        return fprintf(fp, "[MPI_NULL]");
267
 
    if( !mode ) {
268
 
        unsigned int n1;
269
 
 
270
 
        n1 = mpi_get_nbits(a);
271
 
        n += fprintf(fp, "[%u bits]", n1);
272
 
    }
273
 
    else {
274
 
        if( a->sign )
275
 
            putc('-', fp);
276
 
#if BYTES_PER_MPI_LIMB == 2
277
 
#define X "4"
278
 
#elif BYTES_PER_MPI_LIMB == 4
279
 
#define X "8"
280
 
#elif BYTES_PER_MPI_LIMB == 8
281
 
#define X "16"
282
 
#else
283
 
#error please define the format here
284
 
#endif
285
 
        for(i=a->nlimbs; i > 0 ; i-- ) {
286
 
            n += fprintf(fp, i!=a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
287
 
#undef X
288
 
        }
289
 
        if( !a->nlimbs )
290
 
            putc('0', fp );
291
 
    }
292
 
    return n;
293
 
}
294
 
 
295
 
 
296
 
void
297
 
g10_log_mpidump( const char *text, MPI a )
298
 
{
299
 
    FILE *fp = log_stream();
300
 
 
301
 
    g10_log_print_prefix(text);
302
 
    mpi_print(fp, a, 1 );
303
 
    fputc('\n', fp);
304
 
}
305
 
 
306
 
/****************
307
 
 * Special function to get the low 8 bytes from an mpi.
308
 
 * This can be used as a keyid; KEYID is an 2 element array.
309
 
 * Return the low 4 bytes.
310
 
 */
311
 
u32
312
 
mpi_get_keyid( MPI a, u32 *keyid )
313
 
{
314
 
#if BYTES_PER_MPI_LIMB == 4
315
 
    if( keyid ) {
316
 
        keyid[0] = a->nlimbs >= 2? a->d[1] : 0;
317
 
        keyid[1] = a->nlimbs >= 1? a->d[0] : 0;
318
 
    }
319
 
    return a->nlimbs >= 1? a->d[0] : 0;
320
 
#elif BYTES_PER_MPI_LIMB == 8
321
 
    if( keyid ) {
322
 
        keyid[0] = a->nlimbs? (u32)(a->d[0] >> 32) : 0;
323
 
        keyid[1] = a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
324
 
    }
325
 
    return a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
326
 
#else
327
 
#error Make this function work with other LIMB sizes
328
 
#endif
329
 
}
330
 
 
331
 
 
332
 
/****************
333
 
 * Return an m_alloced buffer with the MPI (msb first).
334
 
 * NBYTES receives the length of this buffer. Caller must free the
335
 
 * return string (This function does return a 0 byte buffer with NBYTES
336
 
 * set to zero if the value of A is zero. If sign is not NULL, it will
337
 
 * be set to the sign of the A.
338
 
 */
339
 
static byte *
340
 
do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
341
 
{
342
 
    byte *p, *buffer;
343
 
    mpi_limb_t alimb;
344
 
    int i;
345
 
    unsigned int n;
346
 
 
347
 
    if( sign )
348
 
        *sign = a->sign;
349
 
    *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
350
 
    if (!n)
351
 
      n++; /* avoid zero length allocation */
352
 
    p = buffer = force_secure || mpi_is_secure(a) ? m_alloc_secure(n)
353
 
                                                  : m_alloc(n);
354
 
 
355
 
    for(i=a->nlimbs-1; i >= 0; i-- ) {
356
 
        alimb = a->d[i];
357
 
#if BYTES_PER_MPI_LIMB == 4
358
 
        *p++ = alimb >> 24;
359
 
        *p++ = alimb >> 16;
360
 
        *p++ = alimb >>  8;
361
 
        *p++ = alimb      ;
362
 
#elif BYTES_PER_MPI_LIMB == 8
363
 
        *p++ = alimb >> 56;
364
 
        *p++ = alimb >> 48;
365
 
        *p++ = alimb >> 40;
366
 
        *p++ = alimb >> 32;
367
 
        *p++ = alimb >> 24;
368
 
        *p++ = alimb >> 16;
369
 
        *p++ = alimb >>  8;
370
 
        *p++ = alimb      ;
371
 
#else
372
 
#error please implement for this limb size.
373
 
#endif
374
 
    }
375
 
 
376
 
    /* this is sub-optimal but we need to do the shift operation
377
 
     * because the caller has to free the returned buffer */
378
 
    for(p=buffer; !*p && *nbytes; p++, --*nbytes )
379
 
      ;
380
 
    if( p != buffer )
381
 
      memmove(buffer,p, *nbytes);
382
 
 
383
 
    return buffer;
384
 
}
385
 
 
386
 
 
387
 
byte *
388
 
mpi_get_buffer( MPI a, unsigned *nbytes, int *sign )
389
 
{
390
 
    return do_get_buffer( a, nbytes, sign, 0 );
391
 
}
392
 
 
393
 
byte *
394
 
mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign )
395
 
{
396
 
    return do_get_buffer( a, nbytes, sign, 1 );
397
 
}
398
 
 
399
 
/****************
400
 
 * Use BUFFER to update MPI.
401
 
 */
402
 
void
403
 
mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign )
404
 
{
405
 
    const byte *p;
406
 
    mpi_limb_t alimb;
407
 
    int nlimbs;
408
 
    int i;
409
 
 
410
 
    nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
411
 
    RESIZE_IF_NEEDED(a, nlimbs);
412
 
    a->sign = sign;
413
 
 
414
 
    for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
415
 
#if BYTES_PER_MPI_LIMB == 4
416
 
        alimb  = (mpi_limb_t)*p-- ;
417
 
        alimb |= (mpi_limb_t)*p-- <<  8 ;
418
 
        alimb |= (mpi_limb_t)*p-- << 16 ;
419
 
        alimb |= (mpi_limb_t)*p-- << 24 ;
420
 
#elif BYTES_PER_MPI_LIMB == 8
421
 
        alimb  = (mpi_limb_t)*p--       ;
422
 
        alimb |= (mpi_limb_t)*p-- <<  8 ;
423
 
        alimb |= (mpi_limb_t)*p-- << 16 ;
424
 
        alimb |= (mpi_limb_t)*p-- << 24 ;
425
 
        alimb |= (mpi_limb_t)*p-- << 32 ;
426
 
        alimb |= (mpi_limb_t)*p-- << 40 ;
427
 
        alimb |= (mpi_limb_t)*p-- << 48 ;
428
 
        alimb |= (mpi_limb_t)*p-- << 56 ;
429
 
#else
430
 
#error please implement for this limb size.
431
 
#endif
432
 
        a->d[i++] = alimb;
433
 
    }
434
 
    if( p >= buffer ) {
435
 
#if BYTES_PER_MPI_LIMB == 4
436
 
        alimb  = *p--       ;
437
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- <<  8 ;
438
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
439
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
440
 
#elif BYTES_PER_MPI_LIMB == 8
441
 
        alimb  = (mpi_limb_t)*p-- ;
442
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- <<  8 ;
443
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
444
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
445
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
446
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
447
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
448
 
        if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
449
 
#else
450
 
#error please implement for this limb size.
451
 
#endif
452
 
        a->d[i++] = alimb;
453
 
    }
454
 
    a->nlimbs = i;
455
 
    assert( i == nlimbs );
456
 
}