diff options
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_gen.c')
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_gen.c | 103 |
1 files changed, 100 insertions, 3 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c index b1ee5d8dce..95e636d3f0 100644 --- a/src/lib/libcrypto/rsa/rsa_gen.c +++ b/src/lib/libcrypto/rsa/rsa_gen.c | |||
@@ -74,11 +74,108 @@ RSA *RSA_generate_key(int bits, unsigned long e_value, | |||
74 | if (ctx == NULL) goto err; | 74 | if (ctx == NULL) goto err; |
75 | ctx2=BN_CTX_new(); | 75 | ctx2=BN_CTX_new(); |
76 | 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; | ||
77 | 83 | ||
78 | /* Body of this routine removed for OpenBSD - will return | 84 | bitsp=(bits+1)/2; |
79 | * when the RSA patent expires | 85 | bitsq=bits-bitsp; |
80 | */ | 86 | rsa=RSA_new(); |
87 | if (rsa == NULL) goto err; | ||
81 | 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 & (1<<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; | ||
177 | |||
178 | ok=1; | ||
82 | err: | 179 | err: |
83 | if (ok == -1) | 180 | if (ok == -1) |
84 | { | 181 | { |