dsa_gen.c 8.4 KB
Newer Older
1
/* crypto/dsa/dsa_gen.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 * 
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 * 
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from 
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#undef GENUINE_DSA

#ifdef GENUINE_DSA
#define HASH    SHA
#else
#define HASH    SHA1
#endif 

#include <stdio.h>
#include <time.h>
#include "cryptlib.h"
#include "sha.h"
#include "bn.h"
#include "dsa.h"
#include "rand.h"

U
Ulf Möller 已提交
75 76 77
DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
	     int *counter_ret, unsigned long *h_ret, void (*callback)(),
	     char *cb_arg)
78 79 80 81 82 83 84
	{
	int ok=0;
	unsigned char seed[SHA_DIGEST_LENGTH];
	unsigned char md[SHA_DIGEST_LENGTH];
	unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH];
	BIGNUM *r0,*W,*X,*c,*test;
	BIGNUM *g=NULL,*q=NULL,*p=NULL;
85
	BN_MONT_CTX *mont=NULL;
86 87 88 89 90 91 92 93 94 95 96 97
	int k,n=0,i,b,m=0;
	int counter=0;
	BN_CTX *ctx=NULL,*ctx2=NULL;
	unsigned int h=2;
	DSA *ret=NULL;

	if (bits < 512) bits=512;
	bits=(bits+63)/64*64;

	if ((seed_in != NULL) && (seed_len == 20))
		memcpy(seed,seed_in,seed_len);

98 99 100 101 102 103 104 105 106 107 108 109 110 111
	if ((ctx=BN_CTX_new()) == NULL) goto err;
	if ((ctx2=BN_CTX_new()) == NULL) goto err;
	if ((ret=DSA_new()) == NULL) goto err;

	if ((mont=BN_MONT_CTX_new()) == NULL) goto err;

	r0= &(ctx2->bn[0]);
	g= &(ctx2->bn[1]);
	W= &(ctx2->bn[2]);
	q= &(ctx2->bn[3]);
	X= &(ctx2->bn[4]);
	c= &(ctx2->bn[5]);
	p= &(ctx2->bn[6]);
	test= &(ctx2->bn[7]);
112 113 114 115 116 117 118 119

	BN_lshift(test,BN_value_one(),bits-1);

	for (;;)
		{
		for (;;)
			{
			/* step 1 */
120
			if (callback != NULL) callback(0,m++,cb_arg);
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

			if (!seed_len)
				RAND_bytes(seed,SHA_DIGEST_LENGTH);
			else
				seed_len=0;

			memcpy(buf,seed,SHA_DIGEST_LENGTH);
			memcpy(buf2,seed,SHA_DIGEST_LENGTH);
			for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)
				{
				buf[i]++;
				if (buf[i] != 0) break;
				}

			/* step 2 */
			HASH(seed,SHA_DIGEST_LENGTH,md);
			HASH(buf,SHA_DIGEST_LENGTH,buf2);
			for (i=0; i<SHA_DIGEST_LENGTH; i++)
				md[i]^=buf2[i];

			/* step 3 */
			md[0]|=0x80;
			md[SHA_DIGEST_LENGTH-1]|=0x01;
			if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) abort();

			/* step 4 */
147
			if (DSA_is_prime(q,callback,cb_arg) > 0) break;
148 149 150 151
			/* do a callback call */
			/* step 5 */
			}

152 153
		if (callback != NULL) callback(2,0,cb_arg);
		if (callback != NULL) callback(3,0,cb_arg);
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

		/* step 6 */
		counter=0;

		n=(bits-1)/160;
		b=(bits-1)-n*160;

		for (;;)
			{
			/* step 7 */
			BN_zero(W);
			for (k=0; k<=n; k++)
				{
				for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)
					{
					buf[i]++;
					if (buf[i] != 0) break;
					}

				HASH(buf,SHA_DIGEST_LENGTH,md);

				/* step 8 */
				if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) abort();
				BN_lshift(r0,r0,160*k);
				BN_add(W,W,r0);
				}

			/* more of step 8 */
			BN_mask_bits(W,bits-1);
			BN_copy(X,W); /* this should be ok */
			BN_add(X,X,test); /* this should be ok */

			/* step 9 */
			BN_lshift1(r0,q);
			BN_mod(c,X,r0,ctx);
			BN_sub(r0,c,BN_value_one());
			BN_sub(p,X,r0);

			/* step 10 */
			if (BN_cmp(p,test) >= 0)
				{
				/* step 11 */
196
				if (DSA_is_prime(p,callback,cb_arg) > 0)
197 198 199 200 201 202 203 204 205
					goto end;
				}

			/* step 13 */
			counter++;

			/* step 14 */
			if (counter >= 4096) break;

206
			if (callback != NULL) callback(0,counter,cb_arg);
207 208 209
			}
		}
end:
210
	if (callback != NULL) callback(2,1,cb_arg);
211 212 213 214 215 216 217

	/* We now need to gernerate g */
	/* Set r0=(p-1)/q */
        BN_sub(test,p,BN_value_one());
        BN_div(r0,NULL,test,q,ctx);

	BN_set_word(test,h);
218 219
	BN_MONT_CTX_set(mont,p,ctx);

220 221 222
	for (;;)
		{
		/* g=test^r0%p */
223
		BN_mod_exp_mont(g,test,r0,p,ctx,mont);
224 225 226 227 228
		if (!BN_is_one(g)) break;
		BN_add(test,test,BN_value_one());
		h++;
		}

229
	if (callback != NULL) callback(3,1,cb_arg);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	ok=1;
err:
	if (!ok)
		{
		if (ret != NULL) DSA_free(ret);
		}
	else
		{
		ret->p=BN_dup(p);
		ret->q=BN_dup(q);
		ret->g=BN_dup(g);
		if ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20);
		if (counter_ret != NULL) *counter_ret=counter;
		if (h_ret != NULL) *h_ret=h;
		}
246 247 248
	if (ctx != NULL) BN_CTX_free(ctx);
	if (ctx != NULL) BN_CTX_free(ctx2);
	if (mont != NULL) BN_MONT_CTX_free(mont);
249 250 251
	return(ok?ret:NULL);
	}

U
Ulf Möller 已提交
252
int DSA_is_prime(BIGNUM *w, void (*callback)(), char *cb_arg)
253 254 255
	{
	int ok= -1,j,i,n;
	BN_CTX *ctx=NULL,*ctx2=NULL;
256
	BIGNUM *w_1,*b,*m,*z,*tmp,*mont_1;
257
	int a;
258
	BN_MONT_CTX *mont=NULL;
259 260 261

	if (!BN_is_bit_set(w,0)) return(0);

262 263 264
	if ((ctx=BN_CTX_new()) == NULL) goto err;
	if ((ctx2=BN_CTX_new()) == NULL) goto err;
	if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
265

266 267 268 269 270 271
	m=   &(ctx2->bn[2]);
	b=   &(ctx2->bn[3]);
	z=   &(ctx2->bn[4]);
	w_1= &(ctx2->bn[5]);
	tmp= &(ctx2->bn[6]);
	mont_1= &(ctx2->bn[7]);
272 273 274 275 276 277 278 279 280 281

	/* step 1 */
	n=50;

	/* step 2 */
	if (!BN_sub(w_1,w,BN_value_one())) goto err;
	for (a=1; !BN_is_bit_set(w_1,a); a++)
		;
	if (!BN_rshift(m,w_1,a)) goto err;

282 283 284
	BN_MONT_CTX_set(mont,w,ctx);
	BN_to_montgomery(mont_1,BN_value_one(),mont,ctx);
	BN_to_montgomery(w_1,w_1,mont,ctx);
285 286 287 288
	for (i=1; i < n; i++)
		{
		/* step 3 */
		BN_rand(b,BN_num_bits(w)-2/*-1*/,0,0);
289
		/* BN_set_word(b,0x10001L); */
290 291 292

		/* step 4 */
		j=0;
293 294 295
		if (!BN_mod_exp_mont(z,b,m,w,ctx,mont)) goto err;

		if (!BN_to_montgomery(z,z,mont,ctx)) goto err;
296 297 298 299

		/* step 5 */
		for (;;)
			{
300 301
			if (((j == 0) && (BN_cmp(z,mont_1) == 0)) ||
				(BN_cmp(z,w_1) == 0))
302 303 304
				break;

			/* step 6 */
305
			if ((j > 0) && (BN_cmp(z,mont_1) == 0))
306 307 308 309 310 311 312 313 314 315 316 317
				{
				ok=0;
				goto err;
				}

			j++;
			if (j >= a)
				{
				ok=0;
				goto err;
				}

318
			if (!BN_mod_mul_montgomery(z,z,z,mont,ctx)) goto err;
319
			if (callback != NULL) callback(1,j,cb_arg);
320 321 322 323 324 325 326 327 328 329 330 331
			}
		}

	ok=1;
err:
	if (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB);
	BN_CTX_free(ctx);
	BN_CTX_free(ctx2);
	
	return(ok);
	}