/src/openssl/crypto/rsa/rsa_ossl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* |
11 | | * RSA low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include "internal/cryptlib.h" |
17 | | #include "crypto/bn.h" |
18 | | #include "crypto/sparse_array.h" |
19 | | #include "rsa_local.h" |
20 | | #include "internal/constant_time.h" |
21 | | #if defined(OPENSSL_SYS_TANDEM) |
22 | | #include "internal/tsan_assist.h" |
23 | | #include "internal/threads_common.h" |
24 | | #endif |
25 | | #include <openssl/evp.h> |
26 | | #include <openssl/sha.h> |
27 | | #include <openssl/hmac.h> |
28 | | |
29 | | DEFINE_SPARSE_ARRAY_OF(BN_BLINDING); |
30 | | |
31 | | static int rsa_ossl_public_encrypt(int flen, const unsigned char *from, |
32 | | unsigned char *to, RSA *rsa, int padding); |
33 | | static int rsa_ossl_private_encrypt(int flen, const unsigned char *from, |
34 | | unsigned char *to, RSA *rsa, int padding); |
35 | | static int rsa_ossl_public_decrypt(int flen, const unsigned char *from, |
36 | | unsigned char *to, RSA *rsa, int padding); |
37 | | static int rsa_ossl_private_decrypt(int flen, const unsigned char *from, |
38 | | unsigned char *to, RSA *rsa, int padding); |
39 | | static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, |
40 | | BN_CTX *ctx); |
41 | | static int rsa_ossl_init(RSA *rsa); |
42 | | static int rsa_ossl_finish(RSA *rsa); |
43 | | #ifdef S390X_MOD_EXP |
44 | | static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, |
45 | | BN_CTX *ctx); |
46 | | static const RSA_METHOD rsa_pkcs1_ossl_meth = { |
47 | | "OpenSSL PKCS#1 RSA", |
48 | | rsa_ossl_public_encrypt, |
49 | | rsa_ossl_public_decrypt, /* signature verification */ |
50 | | rsa_ossl_private_encrypt, /* signing */ |
51 | | rsa_ossl_private_decrypt, |
52 | | rsa_ossl_s390x_mod_exp, |
53 | | s390x_mod_exp, |
54 | | rsa_ossl_init, |
55 | | rsa_ossl_finish, |
56 | | RSA_FLAG_FIPS_METHOD, /* flags */ |
57 | | NULL, |
58 | | 0, /* rsa_sign */ |
59 | | 0, /* rsa_verify */ |
60 | | NULL, /* rsa_keygen */ |
61 | | NULL /* rsa_multi_prime_keygen */ |
62 | | }; |
63 | | #else |
64 | | static const RSA_METHOD rsa_pkcs1_ossl_meth = { |
65 | | "OpenSSL PKCS#1 RSA", |
66 | | rsa_ossl_public_encrypt, |
67 | | rsa_ossl_public_decrypt, /* signature verification */ |
68 | | rsa_ossl_private_encrypt, /* signing */ |
69 | | rsa_ossl_private_decrypt, |
70 | | rsa_ossl_mod_exp, |
71 | | BN_mod_exp_mont, /* XXX probably we should not use Montgomery |
72 | | * if e == 3 */ |
73 | | rsa_ossl_init, |
74 | | rsa_ossl_finish, |
75 | | RSA_FLAG_FIPS_METHOD, /* flags */ |
76 | | NULL, |
77 | | 0, /* rsa_sign */ |
78 | | 0, /* rsa_verify */ |
79 | | NULL, /* rsa_keygen */ |
80 | | NULL /* rsa_multi_prime_keygen */ |
81 | | }; |
82 | | #endif |
83 | | |
84 | | static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth; |
85 | | |
86 | | void RSA_set_default_method(const RSA_METHOD *meth) |
87 | 0 | { |
88 | 0 | default_RSA_meth = meth; |
89 | 0 | } |
90 | | |
91 | | const RSA_METHOD *RSA_get_default_method(void) |
92 | 52.9k | { |
93 | 52.9k | return default_RSA_meth; |
94 | 52.9k | } |
95 | | |
96 | | const RSA_METHOD *RSA_PKCS1_OpenSSL(void) |
97 | 0 | { |
98 | 0 | return &rsa_pkcs1_ossl_meth; |
99 | 0 | } |
100 | | |
101 | | const RSA_METHOD *RSA_null_method(void) |
102 | 0 | { |
103 | 0 | return NULL; |
104 | 0 | } |
105 | | |
106 | | static int rsa_ossl_public_encrypt(int flen, const unsigned char *from, |
107 | | unsigned char *to, RSA *rsa, int padding) |
108 | 0 | { |
109 | 0 | BIGNUM *f, *ret; |
110 | 0 | int i, num = 0, r = -1; |
111 | 0 | unsigned char *buf = NULL; |
112 | 0 | BN_CTX *ctx = NULL; |
113 | |
|
114 | 0 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) { |
115 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE); |
116 | 0 | return -1; |
117 | 0 | } |
118 | | |
119 | 0 | if (BN_ucmp(rsa->n, rsa->e) <= 0) { |
120 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
121 | 0 | return -1; |
122 | 0 | } |
123 | | |
124 | | /* for large moduli, enforce exponent limit */ |
125 | 0 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) { |
126 | 0 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) { |
127 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
128 | 0 | return -1; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | 0 | if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL) |
133 | 0 | goto err; |
134 | 0 | BN_CTX_start(ctx); |
135 | 0 | f = BN_CTX_get(ctx); |
136 | 0 | ret = BN_CTX_get(ctx); |
137 | 0 | num = BN_num_bytes(rsa->n); |
138 | 0 | buf = OPENSSL_malloc(num); |
139 | 0 | if (ret == NULL || buf == NULL) |
140 | 0 | goto err; |
141 | | |
142 | 0 | switch (padding) { |
143 | 0 | case RSA_PKCS1_PADDING: |
144 | 0 | i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num, |
145 | 0 | from, flen); |
146 | 0 | break; |
147 | 0 | case RSA_PKCS1_OAEP_PADDING: |
148 | 0 | i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num, |
149 | 0 | from, flen, NULL, 0, |
150 | 0 | NULL, NULL); |
151 | 0 | break; |
152 | 0 | case RSA_NO_PADDING: |
153 | 0 | i = RSA_padding_add_none(buf, num, from, flen); |
154 | 0 | break; |
155 | 0 | default: |
156 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
157 | 0 | goto err; |
158 | 0 | } |
159 | 0 | if (i <= 0) |
160 | 0 | goto err; |
161 | | |
162 | 0 | if (BN_bin2bn(buf, num, f) == NULL) |
163 | 0 | goto err; |
164 | | |
165 | | #ifdef FIPS_MODULE |
166 | | /* |
167 | | * See SP800-56Br2, section 7.1.1.1 |
168 | | * RSAEP: 1 < f < (n – 1). |
169 | | * (where f is the plaintext). |
170 | | * |
171 | | * This bound is somewhat overkill here. RSASVE.GENERATE (7.2.1.2) |
172 | | * regenerates z until 1 < z < n-1, so on that path the plaintext is in |
173 | | * range unconditionally. On the OAEP path the leading 0x00 octet of the |
174 | | * encoding forces m < n-1 unconditionally, while m = 0 or 1 is only |
175 | | * cryptographically negligible, not impossible. The check is kept to |
176 | | * mirror the RSADP bound in rsa_ossl_private_decrypt() and to keep RSAEP |
177 | | * faithful to 7.1.1 of the SP; nothing in the SP relies on it here. |
178 | | */ |
179 | | if (padding == RSA_NO_PADDING) { |
180 | | BIGNUM *nminus1 = BN_CTX_get(ctx); |
181 | | |
182 | | if (BN_ucmp(f, BN_value_one()) <= 0) { |
183 | | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL); |
184 | | goto err; |
185 | | } |
186 | | if (nminus1 == NULL |
187 | | || BN_copy(nminus1, rsa->n) == NULL |
188 | | || !BN_sub_word(nminus1, 1)) |
189 | | goto err; |
190 | | if (BN_ucmp(f, nminus1) >= 0) { |
191 | | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
192 | | goto err; |
193 | | } |
194 | | } else |
195 | | #endif |
196 | 0 | { |
197 | 0 | if (BN_ucmp(f, rsa->n) >= 0) { |
198 | | /* usually the padding functions would catch this */ |
199 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
200 | 0 | goto err; |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | 0 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
205 | 0 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock, |
206 | 0 | rsa->n, ctx)) |
207 | 0 | goto err; |
208 | | |
209 | 0 | if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx, |
210 | 0 | rsa->_method_mod_n)) |
211 | 0 | goto err; |
212 | | |
213 | | /* |
214 | | * BN_bn2binpad puts in leading 0 bytes if the number is less than |
215 | | * the length of the modulus. |
216 | | */ |
217 | 0 | r = BN_bn2binpad(ret, to, num); |
218 | 0 | err: |
219 | 0 | BN_CTX_end(ctx); |
220 | 0 | BN_CTX_free(ctx); |
221 | 0 | OPENSSL_clear_free(buf, num); |
222 | 0 | return r; |
223 | 0 | } |
224 | | |
225 | | #if defined(OPENSSL_SYS_TANDEM) |
226 | | static TSAN_QUALIFIER uint64_t tsan_thread_id = 1; |
227 | | #endif |
228 | | |
229 | | static uintptr_t get_unique_thread_id(void) |
230 | 0 | { |
231 | | #if defined(OPENSSL_SYS_TANDEM) |
232 | | uintptr_t thread_id = (uintptr_t)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY, |
233 | | NULL); |
234 | | |
235 | | if (thread_id == 0) { |
236 | | thread_id = tsan_counter(&tsan_thread_id); |
237 | | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY, NULL, (void *)thread_id); |
238 | | } |
239 | | return thread_id; |
240 | | #else |
241 | 0 | return (uintptr_t)CRYPTO_THREAD_get_current_id(); |
242 | 0 | #endif |
243 | 0 | } |
244 | | |
245 | | static void free_bn_blinding(ossl_uintmax_t idx, BN_BLINDING *b, void *arg) |
246 | 0 | { |
247 | 0 | BN_BLINDING_free(b); |
248 | 0 | } |
249 | | |
250 | | void ossl_rsa_free_blinding(RSA *rsa) |
251 | 52.9k | { |
252 | 52.9k | SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa; |
253 | | |
254 | 52.9k | ossl_sa_BN_BLINDING_doall_arg(blindings, free_bn_blinding, NULL); |
255 | 52.9k | ossl_sa_BN_BLINDING_free(blindings); |
256 | 52.9k | } |
257 | | |
258 | | void *ossl_rsa_alloc_blinding(void) |
259 | 52.9k | { |
260 | 52.9k | return ossl_sa_BN_BLINDING_new(); |
261 | 52.9k | } |
262 | | |
263 | | static BN_BLINDING *ossl_rsa_get_thread_bn_blinding(RSA *rsa) |
264 | 0 | { |
265 | 0 | SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa; |
266 | 0 | uintptr_t tid = get_unique_thread_id(); |
267 | |
|
268 | 0 | return ossl_sa_BN_BLINDING_get(blindings, tid); |
269 | 0 | } |
270 | | |
271 | | static int ossl_rsa_set_thread_bn_blinding(RSA *rsa, BN_BLINDING *b) |
272 | 0 | { |
273 | 0 | SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa; |
274 | 0 | uintptr_t tid = get_unique_thread_id(); |
275 | |
|
276 | 0 | return ossl_sa_BN_BLINDING_set(blindings, tid, b); |
277 | 0 | } |
278 | | |
279 | | static BN_BLINDING *rsa_get_blinding(RSA *rsa, BN_CTX *ctx) |
280 | 0 | { |
281 | 0 | BN_BLINDING *ret; |
282 | |
|
283 | 0 | if (!CRYPTO_THREAD_read_lock(rsa->lock)) |
284 | 0 | return NULL; |
285 | | |
286 | 0 | ret = ossl_rsa_get_thread_bn_blinding(rsa); |
287 | 0 | CRYPTO_THREAD_unlock(rsa->lock); |
288 | |
|
289 | 0 | if (ret == NULL) { |
290 | 0 | ret = RSA_setup_blinding(rsa, ctx); |
291 | 0 | if (!CRYPTO_THREAD_write_lock(rsa->lock)) { |
292 | 0 | BN_BLINDING_free(ret); |
293 | 0 | ret = NULL; |
294 | 0 | } else { |
295 | 0 | if (!ossl_rsa_set_thread_bn_blinding(rsa, ret)) { |
296 | 0 | BN_BLINDING_free(ret); |
297 | 0 | ret = NULL; |
298 | 0 | } |
299 | 0 | CRYPTO_THREAD_unlock(rsa->lock); |
300 | 0 | } |
301 | 0 | } |
302 | |
|
303 | 0 | return ret; |
304 | 0 | } |
305 | | |
306 | | static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx) |
307 | 0 | { |
308 | | /* |
309 | | * Local blinding: store the unblinding factor in BN_BLINDING. |
310 | | */ |
311 | 0 | return BN_BLINDING_convert_ex(f, NULL, b, ctx); |
312 | 0 | } |
313 | | |
314 | | static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx) |
315 | 0 | { |
316 | | /* |
317 | | * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex |
318 | | * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING |
319 | | * is shared between threads, unblind must be non-null: |
320 | | * BN_BLINDING_invert_ex will then use the local unblinding factor, and |
321 | | * will only read the modulus from BN_BLINDING. In both cases it's safe |
322 | | * to access the blinding without a lock. |
323 | | */ |
324 | 0 | BN_set_flags(f, BN_FLG_CONSTTIME); |
325 | 0 | return BN_BLINDING_invert_ex(f, NULL, b, ctx); |
326 | 0 | } |
327 | | |
328 | | /* signing */ |
329 | | static int rsa_ossl_private_encrypt(int flen, const unsigned char *from, |
330 | | unsigned char *to, RSA *rsa, int padding) |
331 | 0 | { |
332 | 0 | BIGNUM *f, *ret, *res; |
333 | 0 | int i, num = 0, r = -1; |
334 | 0 | unsigned char *buf = NULL; |
335 | 0 | BN_CTX *ctx = NULL; |
336 | 0 | BN_BLINDING *blinding = NULL; |
337 | |
|
338 | 0 | if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL) |
339 | 0 | goto err; |
340 | 0 | BN_CTX_start(ctx); |
341 | 0 | f = BN_CTX_get(ctx); |
342 | 0 | ret = BN_CTX_get(ctx); |
343 | 0 | num = BN_num_bytes(rsa->n); |
344 | 0 | buf = OPENSSL_malloc(num); |
345 | 0 | if (ret == NULL || buf == NULL) |
346 | 0 | goto err; |
347 | | |
348 | 0 | switch (padding) { |
349 | 0 | case RSA_PKCS1_PADDING: |
350 | 0 | i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen); |
351 | 0 | break; |
352 | 0 | case RSA_X931_PADDING: |
353 | 0 | i = RSA_padding_add_X931(buf, num, from, flen); |
354 | 0 | break; |
355 | 0 | case RSA_NO_PADDING: |
356 | 0 | i = RSA_padding_add_none(buf, num, from, flen); |
357 | 0 | break; |
358 | 0 | default: |
359 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
360 | 0 | goto err; |
361 | 0 | } |
362 | 0 | if (i <= 0) |
363 | 0 | goto err; |
364 | | |
365 | 0 | if (BN_bin2bn(buf, num, f) == NULL) |
366 | 0 | goto err; |
367 | | |
368 | 0 | if (BN_ucmp(f, rsa->n) >= 0) { |
369 | | /* usually the padding functions would catch this */ |
370 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
371 | 0 | goto err; |
372 | 0 | } |
373 | | |
374 | 0 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
375 | 0 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock, |
376 | 0 | rsa->n, ctx)) |
377 | 0 | goto err; |
378 | | |
379 | 0 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) { |
380 | 0 | blinding = rsa_get_blinding(rsa, ctx); |
381 | 0 | if (blinding == NULL) { |
382 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
383 | 0 | goto err; |
384 | 0 | } |
385 | | |
386 | 0 | if (!rsa_blinding_convert(blinding, f, ctx)) |
387 | 0 | goto err; |
388 | 0 | } |
389 | | |
390 | 0 | if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) { |
391 | 0 | if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) |
392 | 0 | goto err; |
393 | 0 | } else { |
394 | 0 | BIGNUM *d = BN_new(); |
395 | 0 | if (d == NULL) { |
396 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB); |
397 | 0 | goto err; |
398 | 0 | } |
399 | 0 | if (rsa->d == NULL) { |
400 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY); |
401 | 0 | BN_free(d); |
402 | 0 | goto err; |
403 | 0 | } |
404 | 0 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
405 | |
|
406 | 0 | if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx, |
407 | 0 | rsa->_method_mod_n)) { |
408 | 0 | BN_free(d); |
409 | 0 | goto err; |
410 | 0 | } |
411 | | /* We MUST free d before any further use of rsa->d */ |
412 | 0 | BN_free(d); |
413 | 0 | } |
414 | | |
415 | 0 | if (blinding) |
416 | 0 | if (!rsa_blinding_invert(blinding, ret, ctx)) |
417 | 0 | goto err; |
418 | | |
419 | 0 | if (padding == RSA_X931_PADDING) { |
420 | 0 | if (!BN_sub(f, rsa->n, ret)) |
421 | 0 | goto err; |
422 | 0 | if (BN_cmp(ret, f) > 0) |
423 | 0 | res = f; |
424 | 0 | else |
425 | 0 | res = ret; |
426 | 0 | } else { |
427 | 0 | res = ret; |
428 | 0 | } |
429 | | |
430 | | /* |
431 | | * BN_bn2binpad puts in leading 0 bytes if the number is less than |
432 | | * the length of the modulus. |
433 | | */ |
434 | 0 | r = BN_bn2binpad(res, to, num); |
435 | 0 | err: |
436 | 0 | BN_CTX_end(ctx); |
437 | 0 | BN_CTX_free(ctx); |
438 | 0 | OPENSSL_clear_free(buf, num); |
439 | 0 | return r; |
440 | 0 | } |
441 | | |
442 | | static int derive_kdk(int flen, const unsigned char *from, RSA *rsa, |
443 | | unsigned char *buf, int num, unsigned char *kdk) |
444 | 0 | { |
445 | 0 | int ret = 0; |
446 | 0 | HMAC_CTX *hmac = NULL; |
447 | 0 | EVP_MD *md = NULL; |
448 | 0 | unsigned int md_len = SHA256_DIGEST_LENGTH; |
449 | 0 | unsigned char d_hash[SHA256_DIGEST_LENGTH] = { 0 }; |
450 | | /* |
451 | | * because we use d as a handle to rsa->d we need to keep it local and |
452 | | * free before any further use of rsa->d |
453 | | */ |
454 | 0 | BIGNUM *d = BN_new(); |
455 | |
|
456 | 0 | if (d == NULL) { |
457 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB); |
458 | 0 | goto err; |
459 | 0 | } |
460 | 0 | if (rsa->d == NULL) { |
461 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY); |
462 | 0 | BN_free(d); |
463 | 0 | goto err; |
464 | 0 | } |
465 | 0 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
466 | 0 | if (BN_bn2binpad(d, buf, num) < 0) { |
467 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
468 | 0 | BN_free(d); |
469 | 0 | goto err; |
470 | 0 | } |
471 | 0 | BN_free(d); |
472 | | |
473 | | /* |
474 | | * we use hardcoded hash so that migrating between versions that use |
475 | | * different hash doesn't provide a Bleichenbacher oracle: |
476 | | * if the attacker can see that different versions return different |
477 | | * messages for the same ciphertext, they'll know that the message is |
478 | | * synthetically generated, which means that the padding check failed |
479 | | */ |
480 | 0 | md = EVP_MD_fetch(rsa->libctx, "sha256", NULL); |
481 | 0 | if (md == NULL) { |
482 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED); |
483 | 0 | goto err; |
484 | 0 | } |
485 | | |
486 | 0 | if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) { |
487 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
488 | 0 | goto err; |
489 | 0 | } |
490 | | |
491 | 0 | hmac = HMAC_CTX_new(); |
492 | 0 | if (hmac == NULL) { |
493 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB); |
494 | 0 | goto err; |
495 | 0 | } |
496 | | |
497 | 0 | if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) { |
498 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
499 | 0 | goto err; |
500 | 0 | } |
501 | | |
502 | 0 | if (flen < num) { |
503 | 0 | memset(buf, 0, num - flen); |
504 | 0 | if (HMAC_Update(hmac, buf, num - flen) <= 0) { |
505 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
506 | 0 | goto err; |
507 | 0 | } |
508 | 0 | } |
509 | 0 | if (HMAC_Update(hmac, from, flen) <= 0) { |
510 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
511 | 0 | goto err; |
512 | 0 | } |
513 | | |
514 | 0 | md_len = SHA256_DIGEST_LENGTH; |
515 | 0 | if (HMAC_Final(hmac, kdk, &md_len) <= 0) { |
516 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
517 | 0 | goto err; |
518 | 0 | } |
519 | 0 | ret = 1; |
520 | |
|
521 | 0 | err: |
522 | 0 | HMAC_CTX_free(hmac); |
523 | 0 | EVP_MD_free(md); |
524 | 0 | return ret; |
525 | 0 | } |
526 | | |
527 | | static int rsa_ossl_private_decrypt(int flen, const unsigned char *from, |
528 | | unsigned char *to, RSA *rsa, int padding) |
529 | 0 | { |
530 | 0 | BIGNUM *f, *ret; |
531 | 0 | int j, num = 0, r = -1; |
532 | 0 | unsigned char *buf = NULL; |
533 | 0 | unsigned char kdk[SHA256_DIGEST_LENGTH] = { 0 }; |
534 | 0 | BN_CTX *ctx = NULL; |
535 | 0 | BN_BLINDING *blinding = NULL; |
536 | | |
537 | | /* |
538 | | * we need the value of the private exponent to perform implicit rejection |
539 | | */ |
540 | 0 | if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING)) |
541 | 0 | padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING; |
542 | |
|
543 | 0 | if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL) |
544 | 0 | goto err; |
545 | 0 | BN_CTX_start(ctx); |
546 | 0 | f = BN_CTX_get(ctx); |
547 | 0 | ret = BN_CTX_get(ctx); |
548 | 0 | if (ret == NULL) { |
549 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB); |
550 | 0 | goto err; |
551 | 0 | } |
552 | 0 | num = BN_num_bytes(rsa->n); |
553 | 0 | buf = OPENSSL_malloc(num); |
554 | 0 | if (buf == NULL) |
555 | 0 | goto err; |
556 | | |
557 | | /* |
558 | | * This check was for equality but PGP does evil things and chops off the |
559 | | * top '0' bytes |
560 | | */ |
561 | 0 | if (flen > num) { |
562 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN); |
563 | 0 | goto err; |
564 | 0 | } |
565 | | |
566 | 0 | if (flen < 1) { |
567 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL); |
568 | 0 | goto err; |
569 | 0 | } |
570 | | |
571 | | /* make data into a big number */ |
572 | 0 | if (BN_bin2bn(from, (int)flen, f) == NULL) |
573 | 0 | goto err; |
574 | | |
575 | | #ifdef FIPS_MODULE |
576 | | /* |
577 | | * See SP800-56Br2, section 7.1.2.1 |
578 | | * RSADP: 1 < f < (n – 1) |
579 | | * (where f is the ciphertext). |
580 | | * |
581 | | * Kept under FIPS_MODULE because SP 800-56B KTS-OAEP (section 9.2) also |
582 | | * decrypts through RSADP and needs this bound in a FIPS build, and there |
583 | | * is no KTS-OAEP-specific path to attach it to. The non-FIPS RSASVE path |
584 | | * applies the same 1 < c < n-1 in rsasve_recover() |
585 | | * (providers/implementations/kem/rsa_kem.c); keep the two in step. |
586 | | */ |
587 | | if (padding == RSA_NO_PADDING) { |
588 | | BIGNUM *nminus1 = BN_CTX_get(ctx); |
589 | | |
590 | | if (BN_ucmp(f, BN_value_one()) <= 0) { |
591 | | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL); |
592 | | goto err; |
593 | | } |
594 | | if (nminus1 == NULL |
595 | | || BN_copy(nminus1, rsa->n) == NULL |
596 | | || !BN_sub_word(nminus1, 1)) |
597 | | goto err; |
598 | | if (BN_ucmp(f, nminus1) >= 0) { |
599 | | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
600 | | goto err; |
601 | | } |
602 | | } else |
603 | | #endif |
604 | 0 | { |
605 | 0 | if (BN_ucmp(f, rsa->n) >= 0) { |
606 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
607 | 0 | goto err; |
608 | 0 | } |
609 | 0 | } |
610 | 0 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
611 | 0 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock, |
612 | 0 | rsa->n, ctx)) |
613 | 0 | goto err; |
614 | | |
615 | 0 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) { |
616 | 0 | blinding = rsa_get_blinding(rsa, ctx); |
617 | 0 | if (blinding == NULL) { |
618 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
619 | 0 | goto err; |
620 | 0 | } |
621 | | |
622 | 0 | if (!rsa_blinding_convert(blinding, f, ctx)) |
623 | 0 | goto err; |
624 | 0 | } |
625 | | |
626 | | /* do the decrypt */ |
627 | 0 | if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) { |
628 | 0 | if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) |
629 | 0 | goto err; |
630 | 0 | } else { |
631 | 0 | BIGNUM *d = BN_new(); |
632 | 0 | if (d == NULL) { |
633 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB); |
634 | 0 | goto err; |
635 | 0 | } |
636 | 0 | if (rsa->d == NULL) { |
637 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY); |
638 | 0 | BN_free(d); |
639 | 0 | goto err; |
640 | 0 | } |
641 | 0 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
642 | 0 | if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx, |
643 | 0 | rsa->_method_mod_n)) { |
644 | 0 | BN_free(d); |
645 | 0 | goto err; |
646 | 0 | } |
647 | | /* We MUST free d before any further use of rsa->d */ |
648 | 0 | BN_free(d); |
649 | 0 | } |
650 | | |
651 | 0 | if (blinding) |
652 | 0 | if (!rsa_blinding_invert(blinding, ret, ctx)) |
653 | 0 | goto err; |
654 | | |
655 | | /* |
656 | | * derive the Key Derivation Key from private exponent and public |
657 | | * ciphertext |
658 | | */ |
659 | 0 | if (padding == RSA_PKCS1_PADDING) { |
660 | 0 | if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0) |
661 | 0 | goto err; |
662 | 0 | } |
663 | | |
664 | 0 | j = BN_bn2binpad(ret, buf, num); |
665 | 0 | if (j < 0) |
666 | 0 | goto err; |
667 | | |
668 | 0 | switch (padding) { |
669 | 0 | case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING: |
670 | 0 | r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num); |
671 | 0 | break; |
672 | 0 | case RSA_PKCS1_PADDING: |
673 | 0 | r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk); |
674 | 0 | break; |
675 | 0 | case RSA_PKCS1_OAEP_PADDING: |
676 | 0 | r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0); |
677 | 0 | break; |
678 | 0 | case RSA_NO_PADDING: |
679 | 0 | memcpy(to, buf, (r = j)); |
680 | 0 | break; |
681 | 0 | default: |
682 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
683 | 0 | goto err; |
684 | 0 | } |
685 | 0 | #ifndef FIPS_MODULE |
686 | | /* |
687 | | * This trick doesn't work in the FIPS provider because libcrypto manages |
688 | | * the error stack. Instead we opt not to put an error on the stack at all |
689 | | * in case of padding failure in the FIPS provider. |
690 | | */ |
691 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED); |
692 | 0 | err_clear_last_constant_time(1 & ~constant_time_msb(r)); |
693 | 0 | #endif |
694 | |
|
695 | 0 | err: |
696 | 0 | BN_CTX_end(ctx); |
697 | 0 | BN_CTX_free(ctx); |
698 | 0 | OPENSSL_clear_free(buf, num); |
699 | 0 | return r; |
700 | 0 | } |
701 | | |
702 | | /* signature verification */ |
703 | | static int rsa_ossl_public_decrypt(int flen, const unsigned char *from, |
704 | | unsigned char *to, RSA *rsa, int padding) |
705 | 0 | { |
706 | 0 | BIGNUM *f, *ret; |
707 | 0 | int i, num = 0, r = -1; |
708 | 0 | unsigned char *buf = NULL; |
709 | 0 | BN_CTX *ctx = NULL; |
710 | |
|
711 | 0 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) { |
712 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE); |
713 | 0 | return -1; |
714 | 0 | } |
715 | | |
716 | 0 | if (BN_ucmp(rsa->n, rsa->e) <= 0) { |
717 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
718 | 0 | return -1; |
719 | 0 | } |
720 | | |
721 | | /* for large moduli, enforce exponent limit */ |
722 | 0 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) { |
723 | 0 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) { |
724 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
725 | 0 | return -1; |
726 | 0 | } |
727 | 0 | } |
728 | | |
729 | 0 | if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL) |
730 | 0 | goto err; |
731 | 0 | BN_CTX_start(ctx); |
732 | 0 | f = BN_CTX_get(ctx); |
733 | 0 | ret = BN_CTX_get(ctx); |
734 | 0 | if (ret == NULL) { |
735 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB); |
736 | 0 | goto err; |
737 | 0 | } |
738 | 0 | num = BN_num_bytes(rsa->n); |
739 | 0 | buf = OPENSSL_malloc(num); |
740 | 0 | if (buf == NULL) |
741 | 0 | goto err; |
742 | | |
743 | | /* |
744 | | * This check was for equality but PGP does evil things and chops off the |
745 | | * top '0' bytes |
746 | | */ |
747 | 0 | if (flen > num) { |
748 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN); |
749 | 0 | goto err; |
750 | 0 | } |
751 | | |
752 | 0 | if (BN_bin2bn(from, flen, f) == NULL) |
753 | 0 | goto err; |
754 | | |
755 | 0 | if (BN_ucmp(f, rsa->n) >= 0) { |
756 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
757 | 0 | goto err; |
758 | 0 | } |
759 | | |
760 | 0 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
761 | 0 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock, |
762 | 0 | rsa->n, ctx)) |
763 | 0 | goto err; |
764 | | |
765 | 0 | if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx, |
766 | 0 | rsa->_method_mod_n)) |
767 | 0 | goto err; |
768 | | |
769 | | /* For X9.31: Assuming e is odd it does a 12 mod 16 test */ |
770 | 0 | if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12)) |
771 | 0 | if (!BN_sub(ret, rsa->n, ret)) |
772 | 0 | goto err; |
773 | | |
774 | 0 | i = BN_bn2binpad(ret, buf, num); |
775 | 0 | if (i < 0) |
776 | 0 | goto err; |
777 | | |
778 | 0 | switch (padding) { |
779 | 0 | case RSA_PKCS1_PADDING: |
780 | 0 | r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num); |
781 | 0 | break; |
782 | 0 | case RSA_X931_PADDING: |
783 | 0 | r = RSA_padding_check_X931(to, num, buf, i, num); |
784 | 0 | break; |
785 | 0 | case RSA_NO_PADDING: |
786 | 0 | memcpy(to, buf, (r = i)); |
787 | 0 | break; |
788 | 0 | default: |
789 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
790 | 0 | goto err; |
791 | 0 | } |
792 | 0 | if (r < 0) |
793 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED); |
794 | |
|
795 | 0 | err: |
796 | 0 | BN_CTX_end(ctx); |
797 | 0 | BN_CTX_free(ctx); |
798 | 0 | OPENSSL_clear_free(buf, num); |
799 | 0 | return r; |
800 | 0 | } |
801 | | |
802 | | static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) |
803 | 0 | { |
804 | 0 | BIGNUM *r1, *m1, *vrfy; |
805 | 0 | int ret = 0, smooth = 0; |
806 | 0 | #ifndef FIPS_MODULE |
807 | 0 | BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2]; |
808 | 0 | int i, ex_primes = 0; |
809 | 0 | RSA_PRIME_INFO *pinfo; |
810 | 0 | #endif |
811 | |
|
812 | 0 | BN_CTX_start(ctx); |
813 | |
|
814 | 0 | r1 = BN_CTX_get(ctx); |
815 | 0 | #ifndef FIPS_MODULE |
816 | 0 | r2 = BN_CTX_get(ctx); |
817 | 0 | #endif |
818 | 0 | m1 = BN_CTX_get(ctx); |
819 | 0 | vrfy = BN_CTX_get(ctx); |
820 | 0 | if (vrfy == NULL) |
821 | 0 | goto err; |
822 | | |
823 | 0 | #ifndef FIPS_MODULE |
824 | 0 | if (rsa->version == RSA_ASN1_VERSION_MULTI |
825 | 0 | && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0 |
826 | 0 | || ex_primes > RSA_MAX_PRIME_NUM - 2)) |
827 | 0 | goto err; |
828 | 0 | #endif |
829 | | |
830 | 0 | if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) { |
831 | 0 | BIGNUM *factor = BN_new(); |
832 | |
|
833 | 0 | if (factor == NULL) |
834 | 0 | goto err; |
835 | | |
836 | | /* |
837 | | * Make sure BN_mod_inverse in Montgomery initialization uses the |
838 | | * BN_FLG_CONSTTIME flag |
839 | | */ |
840 | 0 | if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME), |
841 | 0 | BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock, |
842 | 0 | factor, ctx)) |
843 | 0 | || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME), |
844 | 0 | BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock, |
845 | 0 | factor, ctx))) { |
846 | 0 | BN_free(factor); |
847 | 0 | goto err; |
848 | 0 | } |
849 | 0 | #ifndef FIPS_MODULE |
850 | 0 | for (i = 0; i < ex_primes; i++) { |
851 | 0 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); |
852 | 0 | BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME); |
853 | 0 | if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) { |
854 | 0 | BN_free(factor); |
855 | 0 | goto err; |
856 | 0 | } |
857 | 0 | } |
858 | 0 | #endif |
859 | | /* |
860 | | * We MUST free |factor| before any further use of the prime factors |
861 | | */ |
862 | 0 | BN_free(factor); |
863 | |
|
864 | 0 | smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont) |
865 | 0 | #ifndef FIPS_MODULE |
866 | 0 | && (ex_primes == 0) |
867 | 0 | #endif |
868 | 0 | && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p)); |
869 | 0 | } |
870 | | |
871 | 0 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
872 | 0 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock, |
873 | 0 | rsa->n, ctx)) |
874 | 0 | goto err; |
875 | | |
876 | 0 | if (smooth) { |
877 | | /* |
878 | | * Conversion from Montgomery domain, a.k.a. Montgomery reduction, |
879 | | * accepts values in [0-m*2^w) range. w is m's bit width rounded up |
880 | | * to limb width. So that at the very least if |I| is fully reduced, |
881 | | * i.e. less than p*q, we can count on from-to round to perform |
882 | | * below modulo operations on |I|. Unlike BN_mod it's constant time. |
883 | | */ |
884 | 0 | if (/* m1 = I moq q */ |
885 | 0 | !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx) |
886 | 0 | || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx) |
887 | | /* r1 = I mod p */ |
888 | 0 | || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx) |
889 | 0 | || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx) |
890 | | /* |
891 | | * Use parallel exponentiations optimization if possible, |
892 | | * otherwise fallback to two sequential exponentiations: |
893 | | * m1 = m1^dmq1 mod q |
894 | | * r1 = r1^dmp1 mod p |
895 | | */ |
896 | 0 | || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q, |
897 | 0 | rsa->_method_mod_q, |
898 | 0 | r1, r1, rsa->dmp1, rsa->p, |
899 | 0 | rsa->_method_mod_p, |
900 | 0 | ctx) |
901 | | /* r1 = (r1 - m1) mod p */ |
902 | | /* |
903 | | * bn_mod_sub_fixed_top is not regular modular subtraction, |
904 | | * it can tolerate subtrahend to be larger than modulus, but |
905 | | * not bit-wise wider. This makes up for uncommon q>p case, |
906 | | * when |m1| can be larger than |rsa->p|. |
907 | | */ |
908 | 0 | || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p) |
909 | | |
910 | | /* r1 = r1 * iqmp mod p */ |
911 | 0 | || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx) |
912 | 0 | || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p, |
913 | 0 | ctx) |
914 | | /* r0 = r1 * q + m1 */ |
915 | 0 | || !bn_mul_fixed_top(r0, r1, rsa->q, ctx) |
916 | 0 | || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n)) |
917 | 0 | goto err; |
918 | | |
919 | 0 | goto tail; |
920 | 0 | } |
921 | | |
922 | | /* compute I mod q */ |
923 | 0 | { |
924 | 0 | BIGNUM *c = BN_new(); |
925 | 0 | if (c == NULL) |
926 | 0 | goto err; |
927 | 0 | BN_with_flags(c, I, BN_FLG_CONSTTIME); |
928 | |
|
929 | 0 | if (!BN_mod(r1, c, rsa->q, ctx)) { |
930 | 0 | BN_free(c); |
931 | 0 | goto err; |
932 | 0 | } |
933 | | |
934 | 0 | { |
935 | 0 | BIGNUM *dmq1 = BN_new(); |
936 | 0 | if (dmq1 == NULL) { |
937 | 0 | BN_free(c); |
938 | 0 | goto err; |
939 | 0 | } |
940 | 0 | BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME); |
941 | | |
942 | | /* compute r1^dmq1 mod q */ |
943 | 0 | if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, |
944 | 0 | rsa->_method_mod_q)) { |
945 | 0 | BN_free(c); |
946 | 0 | BN_free(dmq1); |
947 | 0 | goto err; |
948 | 0 | } |
949 | | /* We MUST free dmq1 before any further use of rsa->dmq1 */ |
950 | 0 | BN_free(dmq1); |
951 | 0 | } |
952 | | |
953 | | /* compute I mod p */ |
954 | 0 | if (!BN_mod(r1, c, rsa->p, ctx)) { |
955 | 0 | BN_free(c); |
956 | 0 | goto err; |
957 | 0 | } |
958 | | /* We MUST free c before any further use of I */ |
959 | 0 | BN_free(c); |
960 | 0 | } |
961 | | |
962 | 0 | { |
963 | 0 | BIGNUM *dmp1 = BN_new(); |
964 | 0 | if (dmp1 == NULL) |
965 | 0 | goto err; |
966 | 0 | BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME); |
967 | | |
968 | | /* compute r1^dmp1 mod p */ |
969 | 0 | if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, |
970 | 0 | rsa->_method_mod_p)) { |
971 | 0 | BN_free(dmp1); |
972 | 0 | goto err; |
973 | 0 | } |
974 | | /* We MUST free dmp1 before any further use of rsa->dmp1 */ |
975 | 0 | BN_free(dmp1); |
976 | 0 | } |
977 | | |
978 | 0 | #ifndef FIPS_MODULE |
979 | 0 | if (ex_primes > 0) { |
980 | 0 | BIGNUM *di = BN_new(), *cc = BN_new(); |
981 | |
|
982 | 0 | if (cc == NULL || di == NULL) { |
983 | 0 | BN_free(cc); |
984 | 0 | BN_free(di); |
985 | 0 | goto err; |
986 | 0 | } |
987 | | |
988 | 0 | for (i = 0; i < ex_primes; i++) { |
989 | | /* prepare m_i */ |
990 | 0 | if ((m[i] = BN_CTX_get(ctx)) == NULL) { |
991 | 0 | BN_free(cc); |
992 | 0 | BN_free(di); |
993 | 0 | goto err; |
994 | 0 | } |
995 | | |
996 | 0 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); |
997 | | |
998 | | /* prepare c and d_i */ |
999 | 0 | BN_with_flags(cc, I, BN_FLG_CONSTTIME); |
1000 | 0 | BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME); |
1001 | |
|
1002 | 0 | if (!BN_mod(r1, cc, pinfo->r, ctx)) { |
1003 | 0 | BN_free(cc); |
1004 | 0 | BN_free(di); |
1005 | 0 | goto err; |
1006 | 0 | } |
1007 | | /* compute r1 ^ d_i mod r_i */ |
1008 | 0 | if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) { |
1009 | 0 | BN_free(cc); |
1010 | 0 | BN_free(di); |
1011 | 0 | goto err; |
1012 | 0 | } |
1013 | 0 | } |
1014 | | |
1015 | 0 | BN_free(cc); |
1016 | 0 | BN_free(di); |
1017 | 0 | } |
1018 | 0 | #endif |
1019 | | |
1020 | 0 | if (!BN_sub(r0, r0, m1)) |
1021 | 0 | goto err; |
1022 | | /* |
1023 | | * This will help stop the size of r0 increasing, which does affect the |
1024 | | * multiply if it optimised for a power of 2 size |
1025 | | */ |
1026 | 0 | if (BN_is_negative(r0)) |
1027 | 0 | if (!BN_add(r0, r0, rsa->p)) |
1028 | 0 | goto err; |
1029 | | |
1030 | 0 | if (!BN_mul(r1, r0, rsa->iqmp, ctx)) |
1031 | 0 | goto err; |
1032 | | |
1033 | 0 | { |
1034 | 0 | BIGNUM *pr1 = BN_new(); |
1035 | 0 | if (pr1 == NULL) |
1036 | 0 | goto err; |
1037 | 0 | BN_with_flags(pr1, r1, BN_FLG_CONSTTIME); |
1038 | |
|
1039 | 0 | if (!BN_mod(r0, pr1, rsa->p, ctx)) { |
1040 | 0 | BN_free(pr1); |
1041 | 0 | goto err; |
1042 | 0 | } |
1043 | | /* We MUST free pr1 before any further use of r1 */ |
1044 | 0 | BN_free(pr1); |
1045 | 0 | } |
1046 | | |
1047 | | /* |
1048 | | * If p < q it is occasionally possible for the correction of adding 'p' |
1049 | | * if r0 is negative above to leave the result still negative. This can |
1050 | | * break the private key operations: the following second correction |
1051 | | * should *always* correct this rare occurrence. This will *never* happen |
1052 | | * with OpenSSL generated keys because they ensure p > q [steve] |
1053 | | */ |
1054 | 0 | if (BN_is_negative(r0)) |
1055 | 0 | if (!BN_add(r0, r0, rsa->p)) |
1056 | 0 | goto err; |
1057 | 0 | if (!BN_mul(r1, r0, rsa->q, ctx)) |
1058 | 0 | goto err; |
1059 | 0 | if (!BN_add(r0, r1, m1)) |
1060 | 0 | goto err; |
1061 | | |
1062 | 0 | #ifndef FIPS_MODULE |
1063 | | /* add m_i to m in multi-prime case */ |
1064 | 0 | if (ex_primes > 0) { |
1065 | 0 | BIGNUM *pr2 = BN_new(); |
1066 | |
|
1067 | 0 | if (pr2 == NULL) |
1068 | 0 | goto err; |
1069 | | |
1070 | 0 | for (i = 0; i < ex_primes; i++) { |
1071 | 0 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); |
1072 | 0 | if (!BN_sub(r1, m[i], r0)) { |
1073 | 0 | BN_free(pr2); |
1074 | 0 | goto err; |
1075 | 0 | } |
1076 | | |
1077 | 0 | if (!BN_mul(r2, r1, pinfo->t, ctx)) { |
1078 | 0 | BN_free(pr2); |
1079 | 0 | goto err; |
1080 | 0 | } |
1081 | | |
1082 | 0 | BN_with_flags(pr2, r2, BN_FLG_CONSTTIME); |
1083 | |
|
1084 | 0 | if (!BN_mod(r1, pr2, pinfo->r, ctx)) { |
1085 | 0 | BN_free(pr2); |
1086 | 0 | goto err; |
1087 | 0 | } |
1088 | | |
1089 | 0 | if (BN_is_negative(r1)) |
1090 | 0 | if (!BN_add(r1, r1, pinfo->r)) { |
1091 | 0 | BN_free(pr2); |
1092 | 0 | goto err; |
1093 | 0 | } |
1094 | 0 | if (!BN_mul(r1, r1, pinfo->pp, ctx)) { |
1095 | 0 | BN_free(pr2); |
1096 | 0 | goto err; |
1097 | 0 | } |
1098 | 0 | if (!BN_add(r0, r0, r1)) { |
1099 | 0 | BN_free(pr2); |
1100 | 0 | goto err; |
1101 | 0 | } |
1102 | 0 | } |
1103 | 0 | BN_free(pr2); |
1104 | 0 | } |
1105 | 0 | #endif |
1106 | | |
1107 | 0 | tail: |
1108 | 0 | if (rsa->e && rsa->n) { |
1109 | 0 | if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) { |
1110 | 0 | if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx, |
1111 | 0 | rsa->_method_mod_n)) |
1112 | 0 | goto err; |
1113 | 0 | } else { |
1114 | 0 | bn_correct_top(r0); |
1115 | 0 | if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx, |
1116 | 0 | rsa->_method_mod_n)) |
1117 | 0 | goto err; |
1118 | 0 | } |
1119 | | /* |
1120 | | * If 'I' was greater than (or equal to) rsa->n, the operation will |
1121 | | * be equivalent to using 'I mod n'. However, the result of the |
1122 | | * verify will *always* be less than 'n' so we don't check for |
1123 | | * absolute equality, just congruency. |
1124 | | */ |
1125 | 0 | if (!BN_sub(vrfy, vrfy, I)) |
1126 | 0 | goto err; |
1127 | 0 | if (BN_is_zero(vrfy)) { |
1128 | 0 | bn_correct_top(r0); |
1129 | 0 | ret = 1; |
1130 | 0 | goto err; /* not actually error */ |
1131 | 0 | } |
1132 | 0 | if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) |
1133 | 0 | goto err; |
1134 | 0 | if (BN_is_negative(vrfy)) |
1135 | 0 | if (!BN_add(vrfy, vrfy, rsa->n)) |
1136 | 0 | goto err; |
1137 | 0 | if (!BN_is_zero(vrfy)) { |
1138 | | /* |
1139 | | * 'I' and 'vrfy' aren't congruent mod n. Don't leak |
1140 | | * miscalculated CRT output, just do a raw (slower) mod_exp and |
1141 | | * return that instead. |
1142 | | */ |
1143 | |
|
1144 | 0 | BIGNUM *d = BN_new(); |
1145 | 0 | if (d == NULL) |
1146 | 0 | goto err; |
1147 | 0 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
1148 | |
|
1149 | 0 | if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, |
1150 | 0 | rsa->_method_mod_n)) { |
1151 | 0 | BN_free(d); |
1152 | 0 | goto err; |
1153 | 0 | } |
1154 | | /* We MUST free d before any further use of rsa->d */ |
1155 | 0 | BN_free(d); |
1156 | 0 | } |
1157 | 0 | } |
1158 | | /* |
1159 | | * It's unfortunate that we have to bn_correct_top(r0). What hopefully |
1160 | | * saves the day is that correction is highly unlike, and private key |
1161 | | * operations are customarily performed on blinded message. Which means |
1162 | | * that attacker won't observe correlation with chosen plaintext. |
1163 | | * Secondly, remaining code would still handle it in same computational |
1164 | | * time and even conceal memory access pattern around corrected top. |
1165 | | */ |
1166 | 0 | bn_correct_top(r0); |
1167 | 0 | ret = 1; |
1168 | 0 | err: |
1169 | 0 | BN_CTX_end(ctx); |
1170 | 0 | return ret; |
1171 | 0 | } |
1172 | | |
1173 | | static int rsa_ossl_init(RSA *rsa) |
1174 | 52.9k | { |
1175 | 52.9k | rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE; |
1176 | 52.9k | return 1; |
1177 | 52.9k | } |
1178 | | |
1179 | | static int rsa_ossl_finish(RSA *rsa) |
1180 | 52.9k | { |
1181 | 52.9k | #ifndef FIPS_MODULE |
1182 | 52.9k | int i; |
1183 | 52.9k | RSA_PRIME_INFO *pinfo; |
1184 | | |
1185 | 52.9k | for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) { |
1186 | 0 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); |
1187 | 0 | BN_MONT_CTX_free(pinfo->m); |
1188 | 0 | } |
1189 | 52.9k | #endif |
1190 | | |
1191 | 52.9k | BN_MONT_CTX_free(rsa->_method_mod_n); |
1192 | 52.9k | BN_MONT_CTX_free(rsa->_method_mod_p); |
1193 | 52.9k | BN_MONT_CTX_free(rsa->_method_mod_q); |
1194 | 52.9k | return 1; |
1195 | 52.9k | } |
1196 | | |
1197 | | #ifdef S390X_MOD_EXP |
1198 | | static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, |
1199 | | BN_CTX *ctx) |
1200 | | { |
1201 | | int rc; |
1202 | | |
1203 | | if (rsa->version != RSA_ASN1_VERSION_MULTI) { |
1204 | | rc = s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp); |
1205 | | if (rc >= 0) |
1206 | | return rc; |
1207 | | } |
1208 | | return rsa_ossl_mod_exp(r0, i, rsa, ctx); |
1209 | | } |
1210 | | |
1211 | | #endif |