summaryrefslogtreecommitdiffstats
path: root/cipher/dsa.c
blob: d1c15c41c8be20cfb3d94755457f3cfe73c783f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* dsa.c  -  DSA signature scheme
 *	Copyright (C) 1998 Free Software Foundation, Inc.
 *
 * This file is part of GNUPG.
 *
 * GNUPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "util.h"
#include "mpi.h"
#include "cipher.h"
#include "dsa.h"

/****************
 * Generate a random secret exponent k less than q
 */
static MPI
gen_k( MPI q )
{
    MPI k = mpi_alloc_secure( mpi_get_nlimbs(q) );
    unsigned nbits = mpi_get_nbits(q);

    if( DBG_CIPHER )
	log_debug("choosing a random k ");
    for(;;) {
	if( DBG_CIPHER )
	    fputc('.', stderr);
	mpi_set_bytes( k, nbits , get_random_byte, 1 );
	if( !(mpi_cmp( k, q ) < 0) )  /* check: k < q */
	    continue; /* no  */
	if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */
	    continue; /* no */
	break;	/* okay */
    }
    if( DBG_CIPHER )
	fputc('\n', stderr);

    return k;
}

void
dsa_free_public_key( DSA_public_key *pk )
{
    mpi_free( pk->p ); pk->p = NULL;
    mpi_free( pk->q ); pk->q = NULL;
    mpi_free( pk->g ); pk->g = NULL;
    mpi_free( pk->y ); pk->y = NULL;
}

void
dsa_free_secret_key( DSA_secret_key *sk )
{
    mpi_free( sk->p ); sk->p = NULL;
    mpi_free( sk->q ); sk->q = NULL;
    mpi_free( sk->g ); sk->g = NULL;
    mpi_free( sk->y ); sk->y = NULL;
    mpi_free( sk->x ); sk->x = NULL;
}


static void
test_keys( DSA_public_key *pk, DSA_secret_key *sk, unsigned qbits )
{
    MPI test = mpi_alloc( qbits / BITS_PER_MPI_LIMB );
    MPI out1_a = mpi_alloc( qbits / BITS_PER_MPI_LIMB );
    MPI out1_b = mpi_alloc( qbits / BITS_PER_MPI_LIMB );

    mpi_set_bytes( test, qbits, get_random_byte, 0 );

    dsa_sign( out1_a, out1_b, test, sk );
    if( !dsa_verify( out1_a, out1_b, test, pk ) )
	log_fatal("DSA:: sign, verify failed\n");

    mpi_free( test );
    mpi_free( out1_a );
    mpi_free( out1_b );
}



/****************
 * Generate a DSA key pair with a key of size NBITS
 * Returns: 2 structures filled with all needed values
 *	    and an array with the n-1 factors of (p-1)
 */
void
dsa_generate( DSA_public_key *pk, DSA_secret_key *sk,
	      unsigned nbits, MPI **ret_factors )
{
    MPI p;    /* the prime */
    MPI q;    /* the 160 bit prime factor */
    MPI g;    /* the generator */
    MPI y;    /* g^x mod p */
    MPI x;    /* the secret exponent */
    MPI h, e;  /* helper */
    unsigned qbits;
    byte *rndbuf;

    assert( nbits >= 512 && nbits <= 1024 );

    qbits = 160;
    p = generate_elg_prime( 1, nbits, qbits, NULL, ret_factors );
    /* get q out of factors */
    q = mpi_copy((*ret_factors)[0]);
    if( mpi_get_nbits(q) != qbits )
	BUG();

    /* find a generator g (h and e are helpers)*/
    /* e = (p-1)/q */
    e = mpi_alloc( mpi_get_nlimbs(p) );
    mpi_sub_ui( e, p, 1 );
    mpi_fdiv_q( e, e, q );
    g = mpi_alloc( mpi_get_nlimbs(p) );
    h = mpi_alloc_set_ui( 1 ); /* we start with 2 */
    do {
	mpi_add_ui( h, h, 1 );
	/* g = h^e mod p */
	mpi_powm( g, h, e, p );
    } while( !mpi_cmp_ui( g, 1 ) );  /* continue until g != 1 */

    /* select a random number which has these properties:
     *	 0 < x < q-1
     * This must be a very good random number because this
     * is the secret part. */
    if( DBG_CIPHER )
	log_debug("choosing a random x ");
    assert( qbits >= 16 );
    x = mpi_alloc_secure( mpi_get_nlimbs(q) );
    mpi_sub_ui( h, q, 1 );  /* put q-1 into h */
    rndbuf = NULL;
    do {
	if( DBG_CIPHER )
	    fputc('.', stderr);
	if( !rndbuf )
	    rndbuf = get_random_bits( qbits, 2, 1 );
	else { /* change only some of the higher bits (= 2 bytes)*/
	    char *r = get_random_bits( 16, 2, 1 );
	    memcpy(rndbuf, r, 16/8 );
	    m_free(r);
	}
	mpi_set_buffer( x, rndbuf, (qbits+7)/8, 0 );
	mpi_clear_highbit( x, qbits+1 );
    } while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, h )<0 ) );
    m_free(rndbuf);
    mpi_free( e );
    mpi_free( h );

    /* y = g^x mod p */
    y = mpi_alloc( mpi_get_nlimbs(p) );
    mpi_powm( y, g, x, p );

    if( DBG_CIPHER ) {
	fputc('\n', stderr);
	log_mpidump("dsa  p= ", p );
	log_mpidump("dsa  q= ", q );
	log_mpidump("dsa  g= ", g );
	log_mpidump("dsa  y= ", y );
	log_mpidump("dsa  x= ", x );
    }

    /* copy the stuff to the key structures */
    pk->p = mpi_copy(p);
    pk->q = mpi_copy(q);
    pk->g = mpi_copy(g);
    pk->y = mpi_copy(y);
    sk->p = p;
    sk->q = q;
    sk->g = g;
    sk->y = y;
    sk->x = x;

    /* now we can test our keys (this should never fail!) */
    test_keys( pk, sk, qbits );
}



/****************
 * Test whether the secret key is valid.
 * Returns: if this is a valid key.
 */
int
dsa_check_secret_key( DSA_secret_key *sk )
{
    int rc;
    MPI y = mpi_alloc( mpi_get_nlimbs(sk->y) );

    mpi_powm( y, sk->g, sk->x, sk->p );
    rc = !mpi_cmp( y, sk->y );
    mpi_free( y );
    return rc;
}



/****************
 * Make a DSA signature from HASH and put it into r and s.
 */

void
dsa_sign(MPI r, MPI s, MPI hash, DSA_secret_key *skey )
{
    MPI k;
    MPI kinv;
    MPI tmp;

    /* select a random k with 0 < k < q */
    k = gen_k( skey->q );

    /* r = (a^k mod p) mod q */
    mpi_powm( r, skey->g, k, skey->p );
    mpi_fdiv_r( r, r, skey->q );

    /* kinv = k^(-1) mod q */
    kinv = mpi_alloc( mpi_get_nlimbs(k) );
    mpi_invm(kinv, k, skey->q );

    /* s = (kinv * ( hash + x * r)) mod q */
    tmp = mpi_alloc( mpi_get_nlimbs(skey->p) );
    mpi_mul( tmp, skey->x, r );
    mpi_add( tmp, tmp, hash );
    mpi_mulm( s , kinv, tmp, skey->q );

    mpi_free(k);
    mpi_free(kinv);
    mpi_free(tmp);
}


/****************
 * Returns true if the signature composed from R and S is valid.
 */
int
dsa_verify(MPI r, MPI s, MPI hash, DSA_public_key *pkey )
{
    int rc;
    MPI w, u1, u2, v;
    MPI base[3];
    MPI exp[3];

    if( !(mpi_cmp_ui( r, 0 ) > 0 && mpi_cmp( r, pkey->q ) < 0) )
	return 0; /* assertion	0 < r < q  failed */
    if( !(mpi_cmp_ui( s, 0 ) > 0 && mpi_cmp( s, pkey->q ) < 0) )
	return 0; /* assertion	0 < s < q  failed */

    w  = mpi_alloc( mpi_get_nlimbs(pkey->q) );
    u1 = mpi_alloc( mpi_get_nlimbs(pkey->q) );
    u2 = mpi_alloc( mpi_get_nlimbs(pkey->q) );
    v  = mpi_alloc( mpi_get_nlimbs(pkey->p) );

    /* w = s^(-1) mod q */
    mpi_invm( w, s, pkey->q );

    /* u1 = (hash * w) mod q */
    mpi_mulm( u1, hash, w, pkey->q );

    /* u2 = r * w mod q  */
    mpi_mulm( u2, r, w, pkey->q );

    /* v =  g^u1 * y^u2 mod p mod q */
    base[0] = pkey->g; exp[0] = u1;
    base[1] = pkey->y; exp[1] = u2;
    base[2] = NULL;    exp[2] = NULL;
    mpi_mulpowm( v, base, exp, pkey->p );
    mpi_fdiv_r( v, v, pkey->q );

    rc = !mpi_cmp( v, r );

    mpi_free(w);
    mpi_free(u1);
    mpi_free(u2);
    mpi_free(v);
    return rc;
}