~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-security

« back to all changes in this revision

Viewing changes to cipher/dsa.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
 
/* dsa.c  -  DSA signature algorithm
2
 
 *      Copyright (C) 1998, 1999, 2000 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 <stdlib.h>
24
 
#include <string.h>
25
 
#include <assert.h>
26
 
#include "util.h"
27
 
#include "mpi.h"
28
 
#include "cipher.h"
29
 
#include "dsa.h"
30
 
 
31
 
typedef struct {
32
 
    MPI p;          /* prime */
33
 
    MPI q;          /* group order */
34
 
    MPI g;          /* group generator */
35
 
    MPI y;          /* g^x mod p */
36
 
} DSA_public_key;
37
 
 
38
 
 
39
 
typedef struct {
40
 
    MPI p;          /* prime */
41
 
    MPI q;          /* group order */
42
 
    MPI g;          /* group generator */
43
 
    MPI y;          /* g^x mod p */
44
 
    MPI x;          /* secret exponent */
45
 
} DSA_secret_key;
46
 
 
47
 
 
48
 
static MPI gen_k( MPI q );
49
 
static void test_keys( DSA_secret_key *sk, unsigned qbits );
50
 
static int  check_secret_key( DSA_secret_key *sk );
51
 
static void generate( DSA_secret_key *sk, unsigned nbits, MPI **ret_factors );
52
 
static void sign(MPI r, MPI s, MPI input, DSA_secret_key *skey);
53
 
static int  verify(MPI r, MPI s, MPI input, DSA_public_key *pkey);
54
 
 
55
 
 
56
 
static void (*progress_cb) ( void *, int );
57
 
static void *progress_cb_data;
58
 
 
59
 
void
60
 
register_pk_dsa_progress ( void (*cb)( void *, int), void *cb_data )
61
 
{
62
 
    progress_cb = cb;
63
 
    progress_cb_data = cb_data;
64
 
}
65
 
 
66
 
 
67
 
static void
68
 
progress( int c )
69
 
{
70
 
    if ( progress_cb )
71
 
        progress_cb ( progress_cb_data, c );
72
 
    else
73
 
        fputc( c, stderr );
74
 
}
75
 
 
76
 
 
77
 
 
78
 
/****************
79
 
 * Generate a random secret exponent k less than q
80
 
 */
81
 
static MPI
82
 
gen_k( MPI q )
83
 
{
84
 
    MPI k = mpi_alloc_secure( mpi_get_nlimbs(q) );
85
 
    unsigned int nbits = mpi_get_nbits(q);
86
 
    unsigned int nbytes = (nbits+7)/8;
87
 
    char *rndbuf = NULL;
88
 
 
89
 
    if( DBG_CIPHER )
90
 
        log_debug("choosing a random k ");
91
 
    for(;;) {
92
 
        if( DBG_CIPHER )
93
 
            progress('.');
94
 
 
95
 
        if( !rndbuf || nbits < 32 ) {
96
 
            m_free(rndbuf);
97
 
            rndbuf = get_random_bits( nbits, 1, 1 );
98
 
        }
99
 
        else { /* change only some of the higher bits */
100
 
            /* we could imporove this by directly requesting more memory
101
 
             * at the first call to get_random_bits() and use this the here
102
 
             * maybe it is easier to do this directly in random.c */
103
 
            char *pp = get_random_bits( 32, 1, 1 );
104
 
            memcpy( rndbuf,pp, 4 );
105
 
            m_free(pp);
106
 
        }
107
 
        mpi_set_buffer( k, rndbuf, nbytes, 0 );
108
 
        if( mpi_test_bit( k, nbits-1 ) )
109
 
            mpi_set_highbit( k, nbits-1 );
110
 
        else {
111
 
            mpi_set_highbit( k, nbits-1 );
112
 
            mpi_clear_bit( k, nbits-1 );
113
 
        }
114
 
 
115
 
        if( !(mpi_cmp( k, q ) < 0) ) {  /* check: k < q */
116
 
            if( DBG_CIPHER )
117
 
                progress('+');
118
 
            continue; /* no  */
119
 
        }
120
 
        if( !(mpi_cmp_ui( k, 0 ) > 0) ) { /* check: k > 0 */
121
 
            if( DBG_CIPHER )
122
 
                progress('-');
123
 
            continue; /* no */
124
 
        }
125
 
        break;  /* okay */
126
 
    }
127
 
    m_free(rndbuf);
128
 
    if( DBG_CIPHER )
129
 
        progress('\n');
130
 
 
131
 
    return k;
132
 
}
133
 
 
134
 
 
135
 
static void
136
 
test_keys( DSA_secret_key *sk, unsigned qbits )
137
 
{
138
 
    DSA_public_key pk;
139
 
    MPI test = mpi_alloc( qbits / BITS_PER_MPI_LIMB );
140
 
    MPI out1_a = mpi_alloc( qbits / BITS_PER_MPI_LIMB );
141
 
    MPI out1_b = mpi_alloc( qbits / BITS_PER_MPI_LIMB );
142
 
 
143
 
    pk.p = sk->p;
144
 
    pk.q = sk->q;
145
 
    pk.g = sk->g;
146
 
    pk.y = sk->y;
147
 
    /*mpi_set_bytes( test, qbits, get_random_byte, 0 );*/
148
 
    {   char *p = get_random_bits( qbits, 0, 0 );
149
 
        mpi_set_buffer( test, p, (qbits+7)/8, 0 );
150
 
        m_free(p);
151
 
    }
152
 
 
153
 
    sign( out1_a, out1_b, test, sk );
154
 
    if( !verify( out1_a, out1_b, test, &pk ) )
155
 
        log_fatal("DSA:: sign, verify failed\n");
156
 
 
157
 
    mpi_free( test );
158
 
    mpi_free( out1_a );
159
 
    mpi_free( out1_b );
160
 
}
161
 
 
162
 
 
163
 
 
164
 
/****************
165
 
 * Generate a DSA key pair with a key of size NBITS
166
 
 * Returns: 2 structures filled with all needed values
167
 
 *          and an array with the n-1 factors of (p-1)
168
 
 */
169
 
static void
170
 
generate( DSA_secret_key *sk, unsigned nbits, MPI **ret_factors )
171
 
{
172
 
    MPI p;    /* the prime */
173
 
    MPI q;    /* the 160 bit prime factor */
174
 
    MPI g;    /* the generator */
175
 
    MPI y;    /* g^x mod p */
176
 
    MPI x;    /* the secret exponent */
177
 
    MPI h, e;  /* helper */
178
 
    unsigned qbits;
179
 
    byte *rndbuf;
180
 
 
181
 
    assert( nbits >= 512 && nbits <= 1024 );
182
 
 
183
 
    qbits = 160;
184
 
    p = generate_elg_prime( 1, nbits, qbits, NULL, ret_factors );
185
 
    /* get q out of factors */
186
 
    q = mpi_copy((*ret_factors)[0]);
187
 
    if( mpi_get_nbits(q) != qbits )
188
 
        BUG();
189
 
 
190
 
    /* find a generator g (h and e are helpers)*/
191
 
    /* e = (p-1)/q */
192
 
    e = mpi_alloc( mpi_get_nlimbs(p) );
193
 
    mpi_sub_ui( e, p, 1 );
194
 
    mpi_fdiv_q( e, e, q );
195
 
    g = mpi_alloc( mpi_get_nlimbs(p) );
196
 
    h = mpi_alloc_set_ui( 1 ); /* we start with 2 */
197
 
    do {
198
 
        mpi_add_ui( h, h, 1 );
199
 
        /* g = h^e mod p */
200
 
        mpi_powm( g, h, e, p );
201
 
    } while( !mpi_cmp_ui( g, 1 ) );  /* continue until g != 1 */
202
 
 
203
 
    /* select a random number which has these properties:
204
 
     *   0 < x < q-1
205
 
     * This must be a very good random number because this
206
 
     * is the secret part. */
207
 
    if( DBG_CIPHER )
208
 
        log_debug("choosing a random x ");
209
 
    assert( qbits >= 160 );
210
 
    x = mpi_alloc_secure( mpi_get_nlimbs(q) );
211
 
    mpi_sub_ui( h, q, 1 );  /* put q-1 into h */
212
 
    rndbuf = NULL;
213
 
    do {
214
 
        if( DBG_CIPHER )
215
 
            progress('.');
216
 
        if( !rndbuf )
217
 
            rndbuf = get_random_bits( qbits, 2, 1 );
218
 
        else { /* change only some of the higher bits (= 2 bytes)*/
219
 
            char *r = get_random_bits( 16, 2, 1 );
220
 
            memcpy(rndbuf, r, 16/8 );
221
 
            m_free(r);
222
 
        }
223
 
        mpi_set_buffer( x, rndbuf, (qbits+7)/8, 0 );
224
 
        mpi_clear_highbit( x, qbits+1 );
225
 
    } while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, h )<0 ) );
226
 
    m_free(rndbuf);
227
 
    mpi_free( e );
228
 
    mpi_free( h );
229
 
 
230
 
    /* y = g^x mod p */
231
 
    y = mpi_alloc( mpi_get_nlimbs(p) );
232
 
    mpi_powm( y, g, x, p );
233
 
 
234
 
    if( DBG_CIPHER ) {
235
 
        progress('\n');
236
 
        log_mpidump("dsa  p= ", p );
237
 
        log_mpidump("dsa  q= ", q );
238
 
        log_mpidump("dsa  g= ", g );
239
 
        log_mpidump("dsa  y= ", y );
240
 
        log_mpidump("dsa  x= ", x );
241
 
    }
242
 
 
243
 
    /* copy the stuff to the key structures */
244
 
    sk->p = p;
245
 
    sk->q = q;
246
 
    sk->g = g;
247
 
    sk->y = y;
248
 
    sk->x = x;
249
 
 
250
 
    /* now we can test our keys (this should never fail!) */
251
 
    test_keys( sk, qbits );
252
 
}
253
 
 
254
 
 
255
 
 
256
 
/****************
257
 
 * Test whether the secret key is valid.
258
 
 * Returns: if this is a valid key.
259
 
 */
260
 
static int
261
 
check_secret_key( DSA_secret_key *sk )
262
 
{
263
 
    int rc;
264
 
    MPI y = mpi_alloc( mpi_get_nlimbs(sk->y) );
265
 
 
266
 
    mpi_powm( y, sk->g, sk->x, sk->p );
267
 
    rc = !mpi_cmp( y, sk->y );
268
 
    mpi_free( y );
269
 
    return rc;
270
 
}
271
 
 
272
 
 
273
 
 
274
 
/****************
275
 
 * Make a DSA signature from HASH and put it into r and s.
276
 
 *
277
 
 * Without generating the k this function runs in 
278
 
 * about 26ms on a 300 Mhz Mobile Pentium
279
 
 */
280
 
 
281
 
static void
282
 
sign(MPI r, MPI s, MPI hash, DSA_secret_key *skey )
283
 
{
284
 
    MPI k;
285
 
    MPI kinv;
286
 
    MPI tmp;
287
 
 
288
 
    /* select a random k with 0 < k < q */
289
 
    k = gen_k( skey->q );
290
 
 
291
 
    /* r = (a^k mod p) mod q */
292
 
    mpi_powm( r, skey->g, k, skey->p );
293
 
    mpi_fdiv_r( r, r, skey->q );
294
 
 
295
 
    /* kinv = k^(-1) mod q */
296
 
    kinv = mpi_alloc( mpi_get_nlimbs(k) );
297
 
    mpi_invm(kinv, k, skey->q );
298
 
 
299
 
    /* s = (kinv * ( hash + x * r)) mod q */
300
 
    tmp = mpi_alloc( mpi_get_nlimbs(skey->p) );
301
 
    mpi_mul( tmp, skey->x, r );
302
 
    mpi_add( tmp, tmp, hash );
303
 
    mpi_mulm( s , kinv, tmp, skey->q );
304
 
 
305
 
    mpi_free(k);
306
 
    mpi_free(kinv);
307
 
    mpi_free(tmp);
308
 
}
309
 
 
310
 
 
311
 
/****************
312
 
 * Returns true if the signature composed from R and S is valid.
313
 
 *
314
 
 * Without the checks this function runs in 
315
 
 * about 31ms on a 300 Mhz Mobile Pentium
316
 
 */
317
 
static int
318
 
verify(MPI r, MPI s, MPI hash, DSA_public_key *pkey )
319
 
{
320
 
    int rc;
321
 
    MPI w, u1, u2, v;
322
 
    MPI base[3];
323
 
    MPI exp[3];
324
 
 
325
 
 
326
 
    if( !(mpi_cmp_ui( r, 0 ) > 0 && mpi_cmp( r, pkey->q ) < 0) )
327
 
        return 0; /* assertion  0 < r < q  failed */
328
 
    if( !(mpi_cmp_ui( s, 0 ) > 0 && mpi_cmp( s, pkey->q ) < 0) )
329
 
        return 0; /* assertion  0 < s < q  failed */
330
 
 
331
 
    w  = mpi_alloc( mpi_get_nlimbs(pkey->q) );
332
 
    u1 = mpi_alloc( mpi_get_nlimbs(pkey->q) );
333
 
    u2 = mpi_alloc( mpi_get_nlimbs(pkey->q) );
334
 
    v  = mpi_alloc( mpi_get_nlimbs(pkey->p) );
335
 
 
336
 
    /* w = s^(-1) mod q */
337
 
    mpi_invm( w, s, pkey->q );
338
 
 
339
 
    /* u1 = (hash * w) mod q */
340
 
    mpi_mulm( u1, hash, w, pkey->q );
341
 
 
342
 
    /* u2 = r * w mod q  */
343
 
    mpi_mulm( u2, r, w, pkey->q );
344
 
 
345
 
    /* v =  g^u1 * y^u2 mod p mod q */
346
 
    base[0] = pkey->g; exp[0] = u1;
347
 
    base[1] = pkey->y; exp[1] = u2;
348
 
    base[2] = NULL;    exp[2] = NULL;
349
 
    mpi_mulpowm( v, base, exp, pkey->p );
350
 
    mpi_fdiv_r( v, v, pkey->q );
351
 
 
352
 
    rc = !mpi_cmp( v, r );
353
 
 
354
 
    mpi_free(w);
355
 
    mpi_free(u1);
356
 
    mpi_free(u2);
357
 
    mpi_free(v);
358
 
    return rc;
359
 
}
360
 
 
361
 
 
362
 
/*********************************************
363
 
 **************  interface  ******************
364
 
 *********************************************/
365
 
 
366
 
int
367
 
dsa_generate( int algo, unsigned nbits, MPI *skey, MPI **retfactors )
368
 
{
369
 
    DSA_secret_key sk;
370
 
 
371
 
    if( algo != PUBKEY_ALGO_DSA )
372
 
        return G10ERR_PUBKEY_ALGO;
373
 
 
374
 
    generate( &sk, nbits, retfactors );
375
 
    skey[0] = sk.p;
376
 
    skey[1] = sk.q;
377
 
    skey[2] = sk.g;
378
 
    skey[3] = sk.y;
379
 
    skey[4] = sk.x;
380
 
    return 0;
381
 
}
382
 
 
383
 
 
384
 
int
385
 
dsa_check_secret_key( int algo, MPI *skey )
386
 
{
387
 
    DSA_secret_key sk;
388
 
 
389
 
    if( algo != PUBKEY_ALGO_DSA )
390
 
        return G10ERR_PUBKEY_ALGO;
391
 
    if( !skey[0] || !skey[1] || !skey[2] || !skey[3] || !skey[4] )
392
 
        return G10ERR_BAD_MPI;
393
 
 
394
 
    sk.p = skey[0];
395
 
    sk.q = skey[1];
396
 
    sk.g = skey[2];
397
 
    sk.y = skey[3];
398
 
    sk.x = skey[4];
399
 
    if( !check_secret_key( &sk ) )
400
 
        return G10ERR_BAD_SECKEY;
401
 
 
402
 
    return 0;
403
 
}
404
 
 
405
 
 
406
 
 
407
 
int
408
 
dsa_sign( int algo, MPI *resarr, MPI data, MPI *skey )
409
 
{
410
 
    DSA_secret_key sk;
411
 
 
412
 
    if( algo != PUBKEY_ALGO_DSA )
413
 
        return G10ERR_PUBKEY_ALGO;
414
 
    if( !data || !skey[0] || !skey[1] || !skey[2] || !skey[3] || !skey[4] )
415
 
        return G10ERR_BAD_MPI;
416
 
 
417
 
    sk.p = skey[0];
418
 
    sk.q = skey[1];
419
 
    sk.g = skey[2];
420
 
    sk.y = skey[3];
421
 
    sk.x = skey[4];
422
 
    resarr[0] = mpi_alloc( mpi_get_nlimbs( sk.p ) );
423
 
    resarr[1] = mpi_alloc( mpi_get_nlimbs( sk.p ) );
424
 
    sign( resarr[0], resarr[1], data, &sk );
425
 
    return 0;
426
 
}
427
 
 
428
 
int
429
 
dsa_verify( int algo, MPI hash, MPI *data, MPI *pkey,
430
 
                    int (*cmp)(void *, MPI), void *opaquev )
431
 
{
432
 
    DSA_public_key pk;
433
 
 
434
 
    if( algo != PUBKEY_ALGO_DSA )
435
 
        return G10ERR_PUBKEY_ALGO;
436
 
    if( !data[0] || !data[1] || !hash
437
 
        || !pkey[0] || !pkey[1] || !pkey[2] || !pkey[3] )
438
 
        return G10ERR_BAD_MPI;
439
 
 
440
 
    pk.p = pkey[0];
441
 
    pk.q = pkey[1];
442
 
    pk.g = pkey[2];
443
 
    pk.y = pkey[3];
444
 
    if( !verify( data[0], data[1], hash, &pk ) )
445
 
        return G10ERR_BAD_SIGN;
446
 
    return 0;
447
 
}
448
 
 
449
 
 
450
 
 
451
 
unsigned
452
 
dsa_get_nbits( int algo, MPI *pkey )
453
 
{
454
 
    if( algo != PUBKEY_ALGO_DSA )
455
 
        return 0;
456
 
    return mpi_get_nbits( pkey[0] );
457
 
}
458
 
 
459
 
 
460
 
/****************
461
 
 * Return some information about the algorithm.  We need algo here to
462
 
 * distinguish different flavors of the algorithm.
463
 
 * Returns: A pointer to string describing the algorithm or NULL if
464
 
 *          the ALGO is invalid.
465
 
 * Usage: Bit 0 set : allows signing
466
 
 *            1 set : allows encryption
467
 
 */
468
 
const char *
469
 
dsa_get_info( int algo, int *npkey, int *nskey, int *nenc, int *nsig,
470
 
                                                         int *use )
471
 
{
472
 
    *npkey = 4;
473
 
    *nskey = 5;
474
 
    *nenc = 0;
475
 
    *nsig = 2;
476
 
 
477
 
    switch( algo ) {
478
 
      case PUBKEY_ALGO_DSA:   *use = PUBKEY_USAGE_SIG; return "DSA";
479
 
      default: *use = 0; return NULL;
480
 
    }
481
 
}
482
 
 
483