rsa_x931g.c revision 193645
1193645Ssimon/* crypto/rsa/rsa_gen.c */
2193645Ssimon/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3193645Ssimon * All rights reserved.
4193645Ssimon *
5193645Ssimon * This package is an SSL implementation written
6193645Ssimon * by Eric Young (eay@cryptsoft.com).
7193645Ssimon * The implementation was written so as to conform with Netscapes SSL.
8193645Ssimon *
9193645Ssimon * This library is free for commercial and non-commercial use as long as
10193645Ssimon * the following conditions are aheared to.  The following conditions
11193645Ssimon * apply to all code found in this distribution, be it the RC4, RSA,
12193645Ssimon * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13193645Ssimon * included with this distribution is covered by the same copyright terms
14193645Ssimon * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15193645Ssimon *
16193645Ssimon * Copyright remains Eric Young's, and as such any Copyright notices in
17193645Ssimon * the code are not to be removed.
18193645Ssimon * If this package is used in a product, Eric Young should be given attribution
19193645Ssimon * as the author of the parts of the library used.
20193645Ssimon * This can be in the form of a textual message at program startup or
21193645Ssimon * in documentation (online or textual) provided with the package.
22193645Ssimon *
23193645Ssimon * Redistribution and use in source and binary forms, with or without
24193645Ssimon * modification, are permitted provided that the following conditions
25193645Ssimon * are met:
26193645Ssimon * 1. Redistributions of source code must retain the copyright
27193645Ssimon *    notice, this list of conditions and the following disclaimer.
28193645Ssimon * 2. Redistributions in binary form must reproduce the above copyright
29193645Ssimon *    notice, this list of conditions and the following disclaimer in the
30193645Ssimon *    documentation and/or other materials provided with the distribution.
31193645Ssimon * 3. All advertising materials mentioning features or use of this software
32193645Ssimon *    must display the following acknowledgement:
33193645Ssimon *    "This product includes cryptographic software written by
34193645Ssimon *     Eric Young (eay@cryptsoft.com)"
35193645Ssimon *    The word 'cryptographic' can be left out if the rouines from the library
36193645Ssimon *    being used are not cryptographic related :-).
37193645Ssimon * 4. If you include any Windows specific code (or a derivative thereof) from
38193645Ssimon *    the apps directory (application code) you must include an acknowledgement:
39193645Ssimon *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40193645Ssimon *
41193645Ssimon * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42193645Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43193645Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44193645Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45193645Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46193645Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47193645Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48193645Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49193645Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50193645Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51193645Ssimon * SUCH DAMAGE.
52193645Ssimon *
53193645Ssimon * The licence and distribution terms for any publically available version or
54193645Ssimon * derivative of this code cannot be changed.  i.e. this code cannot simply be
55193645Ssimon * copied and put under another distribution licence
56193645Ssimon * [including the GNU Public Licence.]
57193645Ssimon */
58193645Ssimon
59193645Ssimon#include <stdio.h>
60193645Ssimon#include <string.h>
61193645Ssimon#include <time.h>
62193645Ssimon#include <openssl/err.h>
63193645Ssimon#include <openssl/bn.h>
64193645Ssimon#include <openssl/rsa.h>
65193645Ssimon
66193645Ssimon#ifndef OPENSSL_FIPS
67193645Ssimon
68193645Ssimon/* X9.31 RSA key derivation and generation */
69193645Ssimon
70193645Ssimonint RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2,
71193645Ssimon			const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp,
72193645Ssimon			const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq,
73193645Ssimon			const BIGNUM *e, BN_GENCB *cb)
74193645Ssimon	{
75193645Ssimon	BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL;
76193645Ssimon	BN_CTX *ctx=NULL,*ctx2=NULL;
77193645Ssimon
78193645Ssimon	if (!rsa)
79193645Ssimon		goto err;
80193645Ssimon
81193645Ssimon	ctx = BN_CTX_new();
82193645Ssimon	if (!ctx)
83193645Ssimon		goto err;
84193645Ssimon	BN_CTX_start(ctx);
85193645Ssimon
86193645Ssimon	r0 = BN_CTX_get(ctx);
87193645Ssimon	r1 = BN_CTX_get(ctx);
88193645Ssimon	r2 = BN_CTX_get(ctx);
89193645Ssimon	r3 = BN_CTX_get(ctx);
90193645Ssimon
91193645Ssimon	if (r3 == NULL)
92193645Ssimon		goto err;
93193645Ssimon	if (!rsa->e)
94193645Ssimon		{
95193645Ssimon		rsa->e = BN_dup(e);
96193645Ssimon		if (!rsa->e)
97193645Ssimon			goto err;
98193645Ssimon		}
99193645Ssimon	else
100193645Ssimon		e = rsa->e;
101193645Ssimon
102193645Ssimon	/* If not all parameters present only calculate what we can.
103193645Ssimon	 * This allows test programs to output selective parameters.
104193645Ssimon	 */
105193645Ssimon
106193645Ssimon	if (Xp && !rsa->p)
107193645Ssimon		{
108193645Ssimon		rsa->p = BN_new();
109193645Ssimon		if (!rsa->p)
110193645Ssimon			goto err;
111193645Ssimon
112193645Ssimon		if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,
113193645Ssimon					Xp, Xp1, Xp2, e, ctx, cb))
114193645Ssimon			goto err;
115193645Ssimon		}
116193645Ssimon
117193645Ssimon	if (Xq && !rsa->q)
118193645Ssimon		{
119193645Ssimon		rsa->q = BN_new();
120193645Ssimon		if (!rsa->q)
121193645Ssimon			goto err;
122193645Ssimon		if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,
123193645Ssimon					Xq, Xq1, Xq2, e, ctx, cb))
124193645Ssimon			goto err;
125193645Ssimon		}
126193645Ssimon
127193645Ssimon	if (!rsa->p || !rsa->q)
128193645Ssimon		{
129193645Ssimon		BN_CTX_end(ctx);
130193645Ssimon		BN_CTX_free(ctx);
131193645Ssimon		return 2;
132193645Ssimon		}
133193645Ssimon
134193645Ssimon	/* Since both primes are set we can now calculate all remaining
135193645Ssimon	 * components.
136193645Ssimon	 */
137193645Ssimon
138193645Ssimon	/* calculate n */
139193645Ssimon	rsa->n=BN_new();
140193645Ssimon	if (rsa->n == NULL)
141193645Ssimon		goto err;
142193645Ssimon	if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx))
143193645Ssimon		goto err;
144193645Ssimon
145193645Ssimon	/* calculate d */
146193645Ssimon	if (!BN_sub(r1,rsa->p,BN_value_one()))
147193645Ssimon		goto err;	/* p-1 */
148193645Ssimon	if (!BN_sub(r2,rsa->q,BN_value_one()))
149193645Ssimon		goto err;	/* q-1 */
150193645Ssimon	if (!BN_mul(r0,r1,r2,ctx))
151193645Ssimon		goto err;	/* (p-1)(q-1) */
152193645Ssimon
153193645Ssimon	if (!BN_gcd(r3, r1, r2, ctx))
154193645Ssimon		goto err;
155193645Ssimon
156193645Ssimon	if (!BN_div(r0, NULL, r0, r3, ctx))
157193645Ssimon		goto err;	/* LCM((p-1)(q-1)) */
158193645Ssimon
159193645Ssimon	ctx2 = BN_CTX_new();
160193645Ssimon	if (!ctx2)
161193645Ssimon		goto err;
162193645Ssimon
163193645Ssimon	rsa->d=BN_mod_inverse(NULL,rsa->e,r0,ctx2);	/* d */
164193645Ssimon	if (rsa->d == NULL)
165193645Ssimon		goto err;
166193645Ssimon
167193645Ssimon	/* calculate d mod (p-1) */
168193645Ssimon	rsa->dmp1=BN_new();
169193645Ssimon	if (rsa->dmp1 == NULL)
170193645Ssimon		goto err;
171193645Ssimon	if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx))
172193645Ssimon		goto err;
173193645Ssimon
174193645Ssimon	/* calculate d mod (q-1) */
175193645Ssimon	rsa->dmq1=BN_new();
176193645Ssimon	if (rsa->dmq1 == NULL)
177193645Ssimon		goto err;
178193645Ssimon	if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx))
179193645Ssimon		goto err;
180193645Ssimon
181193645Ssimon	/* calculate inverse of q mod p */
182193645Ssimon	rsa->iqmp=BN_mod_inverse(NULL,rsa->q,rsa->p,ctx2);
183193645Ssimon
184193645Ssimon	err:
185193645Ssimon	if (ctx)
186193645Ssimon		{
187193645Ssimon		BN_CTX_end(ctx);
188193645Ssimon		BN_CTX_free(ctx);
189193645Ssimon		}
190193645Ssimon	if (ctx2)
191193645Ssimon		BN_CTX_free(ctx2);
192193645Ssimon	/* If this is set all calls successful */
193193645Ssimon	if (rsa && rsa->iqmp != NULL)
194193645Ssimon		return 1;
195193645Ssimon
196193645Ssimon	return 0;
197193645Ssimon
198193645Ssimon	}
199193645Ssimon
200193645Ssimonint RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, BN_GENCB *cb)
201193645Ssimon	{
202193645Ssimon	int ok = 0;
203193645Ssimon	BIGNUM *Xp = NULL, *Xq = NULL;
204193645Ssimon	BN_CTX *ctx = NULL;
205193645Ssimon
206193645Ssimon	ctx = BN_CTX_new();
207193645Ssimon	if (!ctx)
208193645Ssimon		goto error;
209193645Ssimon
210193645Ssimon	BN_CTX_start(ctx);
211193645Ssimon	Xp = BN_CTX_get(ctx);
212193645Ssimon	Xq = BN_CTX_get(ctx);
213193645Ssimon	if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))
214193645Ssimon		goto error;
215193645Ssimon
216193645Ssimon	rsa->p = BN_new();
217193645Ssimon	rsa->q = BN_new();
218193645Ssimon	if (!rsa->p || !rsa->q)
219193645Ssimon		goto error;
220193645Ssimon
221193645Ssimon	/* Generate two primes from Xp, Xq */
222193645Ssimon
223193645Ssimon	if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,
224193645Ssimon					e, ctx, cb))
225193645Ssimon		goto error;
226193645Ssimon
227193645Ssimon	if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,
228193645Ssimon					e, ctx, cb))
229193645Ssimon		goto error;
230193645Ssimon
231193645Ssimon	/* Since rsa->p and rsa->q are valid this call will just derive
232193645Ssimon	 * remaining RSA components.
233193645Ssimon	 */
234193645Ssimon
235193645Ssimon	if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,
236193645Ssimon				NULL, NULL, NULL, NULL, NULL, NULL, e, cb))
237193645Ssimon		goto error;
238193645Ssimon
239193645Ssimon	ok = 1;
240193645Ssimon
241193645Ssimon	error:
242193645Ssimon	if (ctx)
243193645Ssimon		{
244193645Ssimon		BN_CTX_end(ctx);
245193645Ssimon		BN_CTX_free(ctx);
246193645Ssimon		}
247193645Ssimon
248193645Ssimon	if (ok)
249193645Ssimon		return 1;
250193645Ssimon
251193645Ssimon	return 0;
252193645Ssimon
253193645Ssimon	}
254193645Ssimon
255193645Ssimon#endif
256