diff options
author | markus <> | 2002-09-05 12:51:50 +0000 |
---|---|---|
committer | markus <> | 2002-09-05 12:51:50 +0000 |
commit | 15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch) | |
tree | bf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/rsa/rsa_gen.c | |
parent | 027351f729b9e837200dae6e1520cda6577ab930 (diff) | |
download | openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2 openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip |
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_gen.c')
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_gen.c | 120 |
1 files changed, 108 insertions, 12 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c index 4cbd373829..00c25adbc5 100644 --- a/src/lib/libcrypto/rsa/rsa_gen.c +++ b/src/lib/libcrypto/rsa/rsa_gen.c | |||
@@ -59,34 +59,130 @@ | |||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include <time.h> | 60 | #include <time.h> |
61 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
62 | #include "bn.h" | 62 | #include <openssl/bn.h> |
63 | #include "rsa.h" | 63 | #include <openssl/rsa.h> |
64 | 64 | ||
65 | RSA *RSA_generate_key(bits, e_value, callback,cb_arg) | 65 | RSA *RSA_generate_key(int bits, unsigned long e_value, |
66 | int bits; | 66 | void (*callback)(int,int,void *), void *cb_arg) |
67 | unsigned long e_value; | ||
68 | void (*callback)(P_I_I_P); | ||
69 | char *cb_arg; | ||
70 | { | 67 | { |
71 | RSA *rsa=NULL; | 68 | RSA *rsa=NULL; |
72 | BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp; | 69 | BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp; |
73 | int bitsp,bitsq,ok= -1,n=0; | 70 | int bitsp,bitsq,ok= -1,n=0,i; |
74 | BN_CTX *ctx=NULL,*ctx2=NULL; | 71 | BN_CTX *ctx=NULL,*ctx2=NULL; |
75 | 72 | ||
76 | ctx=BN_CTX_new(); | 73 | ctx=BN_CTX_new(); |
77 | if (ctx == NULL) goto err; | 74 | if (ctx == NULL) goto err; |
78 | ctx2=BN_CTX_new(); | 75 | ctx2=BN_CTX_new(); |
79 | if (ctx2 == NULL) goto err; | 76 | if (ctx2 == NULL) goto err; |
77 | BN_CTX_start(ctx); | ||
78 | r0 = BN_CTX_get(ctx); | ||
79 | r1 = BN_CTX_get(ctx); | ||
80 | r2 = BN_CTX_get(ctx); | ||
81 | r3 = BN_CTX_get(ctx); | ||
82 | if (r3 == NULL) goto err; | ||
83 | |||
84 | bitsp=(bits+1)/2; | ||
85 | bitsq=bits-bitsp; | ||
86 | rsa=RSA_new(); | ||
87 | if (rsa == NULL) goto err; | ||
88 | |||
89 | /* set e */ | ||
90 | rsa->e=BN_new(); | ||
91 | if (rsa->e == NULL) goto err; | ||
92 | |||
93 | #if 1 | ||
94 | /* The problem is when building with 8, 16, or 32 BN_ULONG, | ||
95 | * unsigned long can be larger */ | ||
96 | for (i=0; i<sizeof(unsigned long)*8; i++) | ||
97 | { | ||
98 | if (e_value & (1UL<<i)) | ||
99 | BN_set_bit(rsa->e,i); | ||
100 | } | ||
101 | #else | ||
102 | if (!BN_set_word(rsa->e,e_value)) goto err; | ||
103 | #endif | ||
104 | |||
105 | /* generate p and q */ | ||
106 | for (;;) | ||
107 | { | ||
108 | rsa->p=BN_generate_prime(NULL,bitsp,0,NULL,NULL,callback,cb_arg); | ||
109 | if (rsa->p == NULL) goto err; | ||
110 | if (!BN_sub(r2,rsa->p,BN_value_one())) goto err; | ||
111 | if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; | ||
112 | if (BN_is_one(r1)) break; | ||
113 | if (callback != NULL) callback(2,n++,cb_arg); | ||
114 | BN_free(rsa->p); | ||
115 | } | ||
116 | if (callback != NULL) callback(3,0,cb_arg); | ||
117 | for (;;) | ||
118 | { | ||
119 | rsa->q=BN_generate_prime(NULL,bitsq,0,NULL,NULL,callback,cb_arg); | ||
120 | if (rsa->q == NULL) goto err; | ||
121 | if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; | ||
122 | if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; | ||
123 | if (BN_is_one(r1) && (BN_cmp(rsa->p,rsa->q) != 0)) | ||
124 | break; | ||
125 | if (callback != NULL) callback(2,n++,cb_arg); | ||
126 | BN_free(rsa->q); | ||
127 | } | ||
128 | if (callback != NULL) callback(3,1,cb_arg); | ||
129 | if (BN_cmp(rsa->p,rsa->q) < 0) | ||
130 | { | ||
131 | tmp=rsa->p; | ||
132 | rsa->p=rsa->q; | ||
133 | rsa->q=tmp; | ||
134 | } | ||
135 | |||
136 | /* calculate n */ | ||
137 | rsa->n=BN_new(); | ||
138 | if (rsa->n == NULL) goto err; | ||
139 | if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err; | ||
140 | |||
141 | /* calculate d */ | ||
142 | if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */ | ||
143 | if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */ | ||
144 | if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */ | ||
145 | |||
146 | /* should not be needed, since gcd(p-1,e) == 1 and gcd(q-1,e) == 1 */ | ||
147 | /* for (;;) | ||
148 | { | ||
149 | if (!BN_gcd(r3,r0,rsa->e,ctx)) goto err; | ||
150 | if (BN_is_one(r3)) break; | ||
151 | |||
152 | if (1) | ||
153 | { | ||
154 | if (!BN_add_word(rsa->e,2L)) goto err; | ||
155 | continue; | ||
156 | } | ||
157 | RSAerr(RSA_F_RSA_GENERATE_KEY,RSA_R_BAD_E_VALUE); | ||
158 | goto err; | ||
159 | } | ||
160 | */ | ||
161 | rsa->d=BN_mod_inverse(NULL,rsa->e,r0,ctx2); /* d */ | ||
162 | if (rsa->d == NULL) goto err; | ||
163 | |||
164 | /* calculate d mod (p-1) */ | ||
165 | rsa->dmp1=BN_new(); | ||
166 | if (rsa->dmp1 == NULL) goto err; | ||
167 | if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx)) goto err; | ||
168 | |||
169 | /* calculate d mod (q-1) */ | ||
170 | rsa->dmq1=BN_new(); | ||
171 | if (rsa->dmq1 == NULL) goto err; | ||
172 | if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx)) goto err; | ||
173 | |||
174 | /* calculate inverse of q mod p */ | ||
175 | rsa->iqmp=BN_mod_inverse(NULL,rsa->q,rsa->p,ctx2); | ||
176 | if (rsa->iqmp == NULL) goto err; | ||
80 | 177 | ||
81 | /* Body of this routine removed for OpenBSD - will return | 178 | ok=1; |
82 | * when the RSA patent expires | ||
83 | */ | ||
84 | err: | 179 | err: |
85 | if (ok == -1) | 180 | if (ok == -1) |
86 | { | 181 | { |
87 | RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN); | 182 | RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN); |
88 | ok=0; | 183 | ok=0; |
89 | } | 184 | } |
185 | BN_CTX_end(ctx); | ||
90 | BN_CTX_free(ctx); | 186 | BN_CTX_free(ctx); |
91 | BN_CTX_free(ctx2); | 187 | BN_CTX_free(ctx2); |
92 | 188 | ||