summaryrefslogtreecommitdiffstats
path: root/lib/nettle/int/dsa-keygen-fips186.c
blob: 69f7f8cebaf67a6c05d4e6066f7ab4fbab04f284 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* dsa-keygen.c
 *
 * Generation of DSA keypairs
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2013, 2014 Red Hat
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include <nettle/dsa.h>
#include <dsa-fips.h>

#include <nettle/bignum.h>

unsigned _dsa_check_qp_sizes(unsigned q_bits, unsigned p_bits, unsigned generate)
{
	switch (q_bits) {
	case 160:
		FIPS_RULE(generate != 0, 0, "DSA 160-bit generation\n");

		if (p_bits != 1024)
			return 0;
		break;
	case 224:
		if (p_bits != 2048)
			return 0;
		break;
	case 256:
		if (p_bits != 2048 && p_bits != 3072)
			return 0;
		break;
	default:
		return 0;
	}
	return 1;
}

/* This generates p,q params using the A.1.2.1 algorithm in FIPS 186-4.
 * 
 * The hash function used is SHA384.
 */
int
_dsa_generate_dss_pq(struct dsa_params *params,
		     struct dss_params_validation_seeds *cert,
		     unsigned seed_length, void *seed,
		     void *progress_ctx, nettle_progress_func * progress,
		     unsigned p_bits /* = L */ , unsigned q_bits /* = N */ )
{
	mpz_t r, p0, t, z, s, tmp, dp0;
	int ret;
	unsigned iterations, old_counter, i;
	uint8_t *storage = NULL;
	unsigned storage_length = 0;

	ret = _dsa_check_qp_sizes(q_bits, p_bits, 1);
	if (ret == 0) {
		return 0;
	}

	if (seed_length < q_bits / 8) {
		_gnutls_debug_log("Seed length must be larger than %d bytes (it is %d)\n", q_bits/8, seed_length);
		return 0;
	}

	mpz_init(p0);
	mpz_init(dp0);
	mpz_init(r);
	mpz_init(t);
	mpz_init(z);
	mpz_init(s);
	mpz_init(tmp);

	/* firstseed < 2^(N-1) */
	mpz_set_ui(r, 1);
	mpz_mul_2exp(r, r, q_bits - 1);

	nettle_mpz_set_str_256_u(s, seed_length, seed);
	if (mpz_cmp(s, r) < 0) {
		goto fail;
	}

	cert->qseed_length = sizeof(cert->qseed);
	cert->pseed_length = sizeof(cert->pseed);

	ret = st_provable_prime(params->q,
				&cert->qseed_length, cert->qseed,
				&cert->qgen_counter,
				q_bits,
				seed_length, seed, progress_ctx, progress);
	if (ret == 0) {
		goto fail;
	}

	if (progress)
		progress(progress_ctx, 'q');

	ret = st_provable_prime(p0,
				&cert->pseed_length, cert->pseed,
				&cert->pgen_counter,
				1 + div_ceil(p_bits, 2),
				cert->qseed_length, cert->qseed,
				progress_ctx, progress);
	if (ret == 0) {
		goto fail;
	}

	iterations = div_ceil(p_bits, DIGEST_SIZE*8);
	old_counter = cert->pgen_counter;

	if (iterations > 0) {
		storage_length = iterations * DIGEST_SIZE;
		storage = malloc(storage_length);
		if (storage == NULL) {
			goto fail;
		}

		nettle_mpz_set_str_256_u(s, cert->pseed_length, cert->pseed);
		for (i = 0; i < iterations; i++) {
			cert->pseed_length = nettle_mpz_sizeinbase_256_u(s);
			nettle_mpz_get_str_256(cert->pseed_length, cert->pseed, s);

			hash(&storage[(iterations - i - 1) * DIGEST_SIZE],
			     cert->pseed_length, cert->pseed);
			mpz_add_ui(s, s, 1);
		}

		/* x = 2^(p_bits-1) + (x mod 2^(p_bits-1)) */
		nettle_mpz_set_str_256_u(tmp, storage_length, storage);
	}

	mpz_set_ui(r, 1);
	mpz_mul_2exp(r, r, p_bits - 1);

	mpz_fdiv_r_2exp(tmp, tmp, p_bits - 1);
	mpz_add(tmp, tmp, r);

	/* Generate candidate prime p in [2^(bits-1), 2^bits] */

	/* t = u[x/2c0] */
	mpz_mul_2exp(dp0, p0, 1);	/* dp0 = 2*p0 */
	mpz_mul(dp0, dp0, params->q);	/* dp0 = 2*p0*q */

	mpz_cdiv_q(t, tmp, dp0);

 retry:
	/* c = 2p0*q*t + 1 */
	mpz_mul(params->p, dp0, t);
	mpz_add_ui(params->p, params->p, 1);

	if (mpz_sizeinbase(params->p, 2) > p_bits) {
		/* t = 2^(bits-1)/2qp0 */
		mpz_set_ui(tmp, 1);
		mpz_mul_2exp(tmp, tmp, p_bits - 1);
		mpz_cdiv_q(t, tmp, dp0);

		/* p = t* 2tq p0 + 1 */
		mpz_mul(params->p, dp0, t);
		mpz_add_ui(params->p, params->p, 1);
	}

	cert->pgen_counter++;

	mpz_set_ui(r, 0);

	if (iterations > 0) {
		for (i = 0; i < iterations; i++) {
			cert->pseed_length = nettle_mpz_sizeinbase_256_u(s);
			nettle_mpz_get_str_256(cert->pseed_length, cert->pseed, s);

			hash(&storage[(iterations - i - 1) * DIGEST_SIZE],
			     cert->pseed_length, cert->pseed);
			mpz_add_ui(s, s, 1);
		}

		/* r = a */
		nettle_mpz_set_str_256_u(r, storage_length, storage);
	}

	cert->pseed_length = nettle_mpz_sizeinbase_256_u(s);
	nettle_mpz_get_str_256(cert->pseed_length, cert->pseed, s);

	/* a = 2 + (a mod (p-3)) */
	mpz_sub_ui(tmp, params->p, 3);	/* c is too large to worry about negatives */
	mpz_mod(r, r, tmp);
	mpz_add_ui(r, r, 2);

	/* z = a^(2tq) mod p */
	mpz_mul_2exp(tmp, t, 1);	/* tmp = 2t */
	mpz_mul(tmp, tmp, params->q);	/* tmp = 2tq */
	mpz_powm(z, r, tmp, params->p);

	mpz_sub_ui(tmp, z, 1);

	mpz_gcd(tmp, tmp, params->p);
	if (mpz_cmp_ui(tmp, 1) == 0) {
		mpz_powm(tmp, z, p0, params->p);
		if (mpz_cmp_ui(tmp, 1) == 0) {
			goto success;
		}
	}

	if (progress)
		progress(progress_ctx, 'x');

	if (cert->pgen_counter >= (4 * p_bits + old_counter))
		return 0;

	mpz_add_ui(t, t, 1);
	goto retry;

 success:
	if (progress)
		progress(progress_ctx, 'p');

	ret = 1;
	goto finish;

 fail:
	ret = 0;

 finish:
	mpz_clear(dp0);
	mpz_clear(p0);
	mpz_clear(tmp);
	mpz_clear(t);
	mpz_clear(z);
	mpz_clear(s);
	mpz_clear(r);
	free(storage);
	return ret;
}

int
_dsa_generate_dss_g(struct dsa_params *params,
		    unsigned domain_seed_size, const uint8_t* domain_seed,
		    void *progress_ctx, nettle_progress_func * progress,
		    unsigned index)
{
	mpz_t e, w;
	uint16_t count;
	uint8_t *dseed = NULL;
	unsigned dseed_size;
	unsigned pos;
	uint8_t digest[DIGEST_SIZE];
	int ret;

	if (index > 255 || domain_seed_size == 0)
		return 0;

	dseed_size = domain_seed_size + 4 + 1 + 2;
	dseed = malloc(dseed_size);
	if (dseed == NULL)
		return 0;

	mpz_init(e);
	mpz_init(w);

	memcpy(dseed, domain_seed, domain_seed_size);
	pos = domain_seed_size;

	memcpy(dseed + pos, "\x67\x67\x65\x6e", 4);
	pos += 4;

	*(dseed + pos) = (uint8_t) index;
	pos += 1;

	mpz_sub_ui(e, params->p, 1);
	mpz_fdiv_q(e, e, params->q);

	for (count = 1; count < 65535; count++) {
		*(dseed + pos) = (count >> 8) & 0xff;
		*(dseed + pos + 1) = count & 0xff;

		hash(digest, dseed_size, dseed);

		nettle_mpz_set_str_256_u(w, DIGEST_SIZE, digest);

		mpz_powm(params->g, w, e, params->p);

		if (mpz_cmp_ui(params->g, 2) >= 0) {
			/* found */
			goto success;
		}
		if (progress)
			progress(progress_ctx, 'x');
	}

	/* if we're here we failed */
	if (progress)
		progress(progress_ctx, 'X');
	ret = 0;
	goto finish;

 success:
	if (progress)
		progress(progress_ctx, 'g');

	ret = 1;

 finish:
	free(dseed);
	mpz_clear(e);
	mpz_clear(w);
	return ret;

}

/* Generates the public and private DSA (or DH) keys
 */
void
_dsa_generate_dss_xy(struct dsa_params *params,
		     mpz_t y, mpz_t x,
		     void *random_ctx, nettle_random_func * random)
{
	mpz_t r;

	mpz_init(r);
	mpz_set(r, params->q);
	mpz_sub_ui(r, r, 2);
	nettle_mpz_random(x, random_ctx, random, r);
	mpz_add_ui(x, x, 1);

	mpz_powm(y, params->g, x, params->p);

	mpz_clear(r);
}

/* This generates p, q, g params using the algorithms from FIPS 186-4.
 * For p, q, the Shawe-Taylor algorithm is used.
 * For g, the verifiable canonical generation of the generator is used.
 * 
 * The hash function used is SHA384.
 * 
 * pub: The output public key
 * key: The output private key
 * cert: A certificate that can be used to verify the generated parameters
 * index: 1 for digital signatures (DSA), 2 for key establishment (DH)
 * p_bits: The requested size of p
 * q_bits: The requested size of q
 * 
 */
int
dsa_generate_dss_pqg(struct dsa_params *params,
			 struct dss_params_validation_seeds *cert,
			 unsigned index,
			 void *random_ctx, nettle_random_func * random,
			 void *progress_ctx, nettle_progress_func * progress,
			 unsigned p_bits /* = L */ , unsigned q_bits /* = N */ )
{
	int ret;
	uint8_t domain_seed[MAX_PVP_SEED_SIZE*3];
	unsigned domain_seed_size = 0;

	ret = _dsa_check_qp_sizes(q_bits, p_bits, 1);
	if (ret == 0)
		return 0;

	cert->seed_length = 2 * (q_bits / 8) + 1;

	if (cert->seed_length > sizeof(cert->seed))
		return 0;

	random(random_ctx, cert->seed_length, cert->seed);

	ret = _dsa_generate_dss_pq(params, cert, cert->seed_length, cert->seed,
				   progress_ctx, progress, p_bits, q_bits);
	if (ret == 0)
		return 0;

	domain_seed_size = cert->seed_length + cert->qseed_length + cert->pseed_length;
	memcpy(domain_seed, cert->seed, cert->seed_length);
	memcpy(&domain_seed[cert->seed_length], cert->pseed, cert->pseed_length);
	memcpy(&domain_seed[cert->seed_length+cert->pseed_length], cert->qseed, cert->qseed_length);
	ret = _dsa_generate_dss_g(params, domain_seed_size, domain_seed,
				  progress_ctx, progress, index);
	if (ret == 0)
		return 0;

	return 1;
}

int
_dsa_generate_dss_pqg(struct dsa_params *params,
			 struct dss_params_validation_seeds *cert,
			 unsigned index,
			 unsigned seed_size, void *seed,
			 void *progress_ctx, nettle_progress_func * progress,
			 unsigned p_bits /* = L */ , unsigned q_bits /* = N */ )
{
	int ret;
	uint8_t domain_seed[MAX_PVP_SEED_SIZE*3];
	unsigned domain_seed_size = 0;

	ret = _dsa_check_qp_sizes(q_bits, p_bits, 1);
	if (ret == 0)
		return 0;

	if (_gnutls_fips_mode_enabled() != 0) {
		cert->seed_length = 2 * (q_bits / 8) + 1;

		FIPS_RULE(cert->seed_length != seed_size, 0, "unsupported DSA seed length (is %d, should be %d)\n", seed_size, cert->seed_length);
	} else {
		cert->seed_length = seed_size;
	}

	if (cert->seed_length > sizeof(cert->seed))
		return 0;


	memcpy(cert->seed, seed, cert->seed_length);

	ret = _dsa_generate_dss_pq(params, cert, cert->seed_length, cert->seed,
				   progress_ctx, progress, p_bits, q_bits);
	if (ret == 0)
		return 0;

	domain_seed_size = cert->seed_length + cert->qseed_length + cert->pseed_length;
	memcpy(domain_seed, cert->seed, cert->seed_length);
	memcpy(&domain_seed[cert->seed_length], cert->pseed, cert->pseed_length);
	memcpy(&domain_seed[cert->seed_length+cert->pseed_length], cert->qseed, cert->qseed_length);
	ret = _dsa_generate_dss_g(params, domain_seed_size, domain_seed,
				  progress_ctx, progress, index);
	if (ret == 0)
		return 0;

	return 1;
}

int
dsa_generate_dss_keypair(struct dsa_params *params,
			 mpz_t y,
			 mpz_t x,
			 void *random_ctx, nettle_random_func * random,
			 void *progress_ctx, nettle_progress_func * progress)
{
	_dsa_generate_dss_xy(params, y, x, random_ctx, random);

	if (progress)
		progress(progress_ctx, '\n');

	return 1;

}