/src/openssl30/crypto/rsa/rsa_oaep.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2024 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 | | /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */ |
11 | | |
12 | | /* |
13 | | * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL: |
14 | | * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security |
15 | | * proof for the original OAEP scheme, which EME-OAEP is based on. A new |
16 | | * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern, |
17 | | * "RSA-OEAP is Still Alive!", Dec. 2000, <URL: |
18 | | * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements |
19 | | * for the underlying permutation: "partial-one-wayness" instead of |
20 | | * one-wayness. For the RSA function, this is an equivalent notion. |
21 | | */ |
22 | | |
23 | | /* |
24 | | * RSA low level APIs are deprecated for public use, but still ok for |
25 | | * internal use. |
26 | | */ |
27 | | #include "internal/deprecated.h" |
28 | | |
29 | | #include "internal/constant_time.h" |
30 | | |
31 | | #include <stdio.h> |
32 | | #include "internal/cryptlib.h" |
33 | | #include <openssl/bn.h> |
34 | | #include <openssl/evp.h> |
35 | | #include <openssl/rand.h> |
36 | | #include <openssl/sha.h> |
37 | | #include "rsa_local.h" |
38 | | |
39 | | int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, |
40 | | const unsigned char *from, int flen, |
41 | | const unsigned char *param, int plen) |
42 | 0 | { |
43 | 0 | return ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(NULL, to, tlen, from, flen, |
44 | 0 | param, plen, NULL, NULL); |
45 | 0 | } |
46 | | |
47 | | /* |
48 | | * Perform the padding as per NIST 800-56B 7.2.2.3 |
49 | | * from (K) is the key material. |
50 | | * param (A) is the additional input. |
51 | | * Step numbers are included here but not in the constant time inverse below |
52 | | * to avoid complicating an already difficult enough function. |
53 | | */ |
54 | | int ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(OSSL_LIB_CTX *libctx, |
55 | | unsigned char *to, int tlen, |
56 | | const unsigned char *from, int flen, |
57 | | const unsigned char *param, |
58 | | int plen, const EVP_MD *md, |
59 | | const EVP_MD *mgf1md) |
60 | 0 | { |
61 | 0 | int rv = 0; |
62 | 0 | int i, emlen = tlen - 1; |
63 | 0 | unsigned char *db, *seed; |
64 | 0 | unsigned char *dbmask = NULL; |
65 | 0 | unsigned char seedmask[EVP_MAX_MD_SIZE]; |
66 | 0 | int mdlen, dbmask_len = 0; |
67 | |
|
68 | 0 | if (md == NULL) { |
69 | 0 | #ifndef FIPS_MODULE |
70 | 0 | md = EVP_sha1(); |
71 | | #else |
72 | | ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER); |
73 | | return 0; |
74 | | #endif |
75 | 0 | } |
76 | 0 | if (mgf1md == NULL) |
77 | 0 | mgf1md = md; |
78 | |
|
79 | 0 | mdlen = EVP_MD_get_size(md); |
80 | 0 | if (mdlen <= 0) { |
81 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_LENGTH); |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | | /* step 2b: check KLen > nLen - 2 HLen - 2 */ |
86 | 0 | if (flen > emlen - 2 * mdlen - 1) { |
87 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
88 | 0 | return 0; |
89 | 0 | } |
90 | | |
91 | 0 | if (emlen < 2 * mdlen + 1) { |
92 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | /* step 3i: EM = 00000000 || maskedMGF || maskedDB */ |
97 | 0 | to[0] = 0; |
98 | 0 | seed = to + 1; |
99 | 0 | db = to + mdlen + 1; |
100 | | |
101 | | /* step 3a: hash the additional input */ |
102 | 0 | if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL)) |
103 | 0 | goto err; |
104 | | /* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */ |
105 | 0 | memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1); |
106 | | /* step 3c: DB = HA || PS || 00000001 || K */ |
107 | 0 | db[emlen - flen - mdlen - 1] = 0x01; |
108 | 0 | memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen); |
109 | | /* step 3d: generate random byte string */ |
110 | 0 | if (RAND_bytes_ex(libctx, seed, mdlen, 0) <= 0) |
111 | 0 | goto err; |
112 | | |
113 | 0 | dbmask_len = emlen - mdlen; |
114 | 0 | dbmask = OPENSSL_malloc(dbmask_len); |
115 | 0 | if (dbmask == NULL) { |
116 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); |
117 | 0 | goto err; |
118 | 0 | } |
119 | | |
120 | | /* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */ |
121 | 0 | if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0) |
122 | 0 | goto err; |
123 | | /* step 3f: maskedDB = DB XOR dbMask */ |
124 | 0 | for (i = 0; i < dbmask_len; i++) |
125 | 0 | db[i] ^= dbmask[i]; |
126 | | |
127 | | /* step 3g: mgfSeed = MGF(maskedDB, HLen) */ |
128 | 0 | if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0) |
129 | 0 | goto err; |
130 | | /* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */ |
131 | 0 | for (i = 0; i < mdlen; i++) |
132 | 0 | seed[i] ^= seedmask[i]; |
133 | 0 | rv = 1; |
134 | |
|
135 | 0 | err: |
136 | 0 | OPENSSL_cleanse(seedmask, sizeof(seedmask)); |
137 | 0 | OPENSSL_clear_free(dbmask, dbmask_len); |
138 | 0 | return rv; |
139 | 0 | } |
140 | | |
141 | | int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, |
142 | | const unsigned char *from, int flen, |
143 | | const unsigned char *param, int plen, |
144 | | const EVP_MD *md, const EVP_MD *mgf1md) |
145 | 0 | { |
146 | 0 | return ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(NULL, to, tlen, from, flen, |
147 | 0 | param, plen, md, mgf1md); |
148 | 0 | } |
149 | | |
150 | | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, |
151 | | const unsigned char *from, int flen, int num, |
152 | | const unsigned char *param, int plen) |
153 | 0 | { |
154 | 0 | return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num, |
155 | 0 | param, plen, NULL, NULL); |
156 | 0 | } |
157 | | |
158 | | int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, |
159 | | const unsigned char *from, int flen, |
160 | | int num, const unsigned char *param, |
161 | | int plen, const EVP_MD *md, |
162 | | const EVP_MD *mgf1md) |
163 | 0 | { |
164 | 0 | int i, dblen = 0, mlen = -1, one_index = 0, msg_index; |
165 | 0 | unsigned int good = 0, found_one_byte, mask; |
166 | 0 | const unsigned char *maskedseed, *maskeddb; |
167 | | /* |
168 | | * |em| is the encoded message, zero-padded to exactly |num| bytes: em = |
169 | | * Y || maskedSeed || maskedDB |
170 | | */ |
171 | 0 | unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE], |
172 | 0 | phash[EVP_MAX_MD_SIZE]; |
173 | 0 | int mdlen; |
174 | |
|
175 | 0 | if (md == NULL) { |
176 | 0 | #ifndef FIPS_MODULE |
177 | 0 | md = EVP_sha1(); |
178 | | #else |
179 | | ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER); |
180 | | return -1; |
181 | | #endif |
182 | 0 | } |
183 | |
|
184 | 0 | if (mgf1md == NULL) |
185 | 0 | mgf1md = md; |
186 | |
|
187 | 0 | mdlen = EVP_MD_get_size(md); |
188 | |
|
189 | 0 | if (tlen <= 0 || flen <= 0 || mdlen <= 0) |
190 | 0 | return -1; |
191 | | /* |
192 | | * |num| is the length of the modulus; |flen| is the length of the |
193 | | * encoded message. Therefore, for any |from| that was obtained by |
194 | | * decrypting a ciphertext, we must have |flen| <= |num|. Similarly, |
195 | | * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of |
196 | | * the ciphertext, see PKCS #1 v2.2, section 7.1.2. |
197 | | * This does not leak any side-channel information. |
198 | | */ |
199 | 0 | if (num < flen || num < 2 * mdlen + 2) { |
200 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_OAEP_DECODING_ERROR); |
201 | 0 | return -1; |
202 | 0 | } |
203 | | |
204 | 0 | dblen = num - mdlen - 1; |
205 | 0 | db = OPENSSL_malloc(dblen); |
206 | 0 | if (db == NULL) { |
207 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); |
208 | 0 | goto cleanup; |
209 | 0 | } |
210 | | |
211 | 0 | em = OPENSSL_malloc(num); |
212 | 0 | if (em == NULL) { |
213 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); |
214 | 0 | goto cleanup; |
215 | 0 | } |
216 | | |
217 | | /* |
218 | | * Caller is encouraged to pass zero-padded message created with |
219 | | * BN_bn2binpad. Trouble is that since we can't read out of |from|'s |
220 | | * bounds, it's impossible to have an invariant memory access pattern |
221 | | * in case |from| was not zero-padded in advance. |
222 | | */ |
223 | 0 | for (from += flen, em += num, i = 0; i < num; i++) { |
224 | 0 | mask = ~constant_time_is_zero(flen); |
225 | 0 | flen -= 1 & mask; |
226 | 0 | from -= 1 & mask; |
227 | 0 | *--em = *from & mask; |
228 | 0 | } |
229 | | |
230 | | /* |
231 | | * The first byte must be zero, however we must not leak if this is |
232 | | * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA |
233 | | * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001). |
234 | | */ |
235 | 0 | good = constant_time_is_zero(em[0]); |
236 | |
|
237 | 0 | maskedseed = em + 1; |
238 | 0 | maskeddb = em + 1 + mdlen; |
239 | |
|
240 | 0 | if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) |
241 | 0 | goto cleanup; |
242 | 0 | for (i = 0; i < mdlen; i++) |
243 | 0 | seed[i] ^= maskedseed[i]; |
244 | |
|
245 | 0 | if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) |
246 | 0 | goto cleanup; |
247 | 0 | for (i = 0; i < dblen; i++) |
248 | 0 | db[i] ^= maskeddb[i]; |
249 | |
|
250 | 0 | if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL)) |
251 | 0 | goto cleanup; |
252 | | |
253 | 0 | good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen)); |
254 | |
|
255 | 0 | found_one_byte = 0; |
256 | 0 | for (i = mdlen; i < dblen; i++) { |
257 | | /* |
258 | | * Padding consists of a number of 0-bytes, followed by a 1. |
259 | | */ |
260 | 0 | unsigned int equals1 = constant_time_eq(db[i], 1); |
261 | 0 | unsigned int equals0 = constant_time_is_zero(db[i]); |
262 | 0 | one_index = constant_time_select_int(~found_one_byte & equals1, |
263 | 0 | i, one_index); |
264 | 0 | found_one_byte |= equals1; |
265 | 0 | good &= (found_one_byte | equals0); |
266 | 0 | } |
267 | |
|
268 | 0 | good &= found_one_byte; |
269 | | |
270 | | /* |
271 | | * At this point |good| is zero unless the plaintext was valid, |
272 | | * so plaintext-awareness ensures timing side-channels are no longer a |
273 | | * concern. |
274 | | */ |
275 | 0 | msg_index = one_index + 1; |
276 | 0 | mlen = dblen - msg_index; |
277 | | |
278 | | /* |
279 | | * For good measure, do this check in constant time as well. |
280 | | */ |
281 | 0 | good &= constant_time_ge(tlen, mlen); |
282 | | |
283 | | /* |
284 | | * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left. |
285 | | * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|. |
286 | | * Otherwise leave |to| unchanged. |
287 | | * Copy the memory back in a way that does not reveal the size of |
288 | | * the data being copied via a timing side channel. This requires copying |
289 | | * parts of the buffer multiple times based on the bits set in the real |
290 | | * length. Clear bits do a non-copy with identical access pattern. |
291 | | * The loop below has overall complexity of O(N*log(N)). |
292 | | */ |
293 | 0 | tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen), |
294 | 0 | dblen - mdlen - 1, tlen); |
295 | 0 | for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) { |
296 | 0 | mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0); |
297 | 0 | for (i = mdlen + 1; i < dblen - msg_index; i++) |
298 | 0 | db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]); |
299 | 0 | } |
300 | 0 | for (i = 0; i < tlen; i++) { |
301 | 0 | mask = good & constant_time_lt(i, mlen); |
302 | 0 | to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]); |
303 | 0 | } |
304 | |
|
305 | 0 | #ifndef FIPS_MODULE |
306 | | /* |
307 | | * To avoid chosen ciphertext attacks, the error message should not |
308 | | * reveal which kind of decoding error happened. |
309 | | * |
310 | | * This trick doesn't work in the FIPS provider because libcrypto manages |
311 | | * the error stack. Instead we opt not to put an error on the stack at all |
312 | | * in case of padding failure in the FIPS provider. |
313 | | */ |
314 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_OAEP_DECODING_ERROR); |
315 | 0 | err_clear_last_constant_time(1 & good); |
316 | 0 | #endif |
317 | 0 | cleanup: |
318 | 0 | OPENSSL_cleanse(seed, sizeof(seed)); |
319 | 0 | OPENSSL_clear_free(db, dblen); |
320 | 0 | OPENSSL_clear_free(em, num); |
321 | |
|
322 | 0 | return constant_time_select_int(good, mlen, -1); |
323 | 0 | } |
324 | | |
325 | | /* |
326 | | * Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B. |
327 | | * The variables are named differently to NIST: |
328 | | * mask (T) and len (maskLen)are the returned mask. |
329 | | * seed (mgfSeed). |
330 | | * The range checking steps inm the process are performed outside. |
331 | | */ |
332 | | int PKCS1_MGF1(unsigned char *mask, long len, |
333 | | const unsigned char *seed, long seedlen, const EVP_MD *dgst) |
334 | 616 | { |
335 | 616 | long i, outlen = 0; |
336 | 616 | unsigned char cnt[4]; |
337 | 616 | EVP_MD_CTX *c = EVP_MD_CTX_new(); |
338 | 616 | unsigned char md[EVP_MAX_MD_SIZE]; |
339 | 616 | int mdlen; |
340 | 616 | int rv = -1; |
341 | | |
342 | 616 | if (c == NULL) |
343 | 0 | goto err; |
344 | 616 | mdlen = EVP_MD_get_size(dgst); |
345 | 616 | if (mdlen < 0) |
346 | 0 | goto err; |
347 | | /* step 4 */ |
348 | 4.53k | for (i = 0; outlen < len; i++) { |
349 | | /* step 4a: D = I2BS(counter, 4) */ |
350 | 3.91k | cnt[0] = (unsigned char)((i >> 24) & 255); |
351 | 3.91k | cnt[1] = (unsigned char)((i >> 16) & 255); |
352 | 3.91k | cnt[2] = (unsigned char)((i >> 8)) & 255; |
353 | 3.91k | cnt[3] = (unsigned char)(i & 255); |
354 | | /* step 4b: T =T || hash(mgfSeed || D) */ |
355 | 3.91k | if (!EVP_DigestInit_ex(c, dgst, NULL) |
356 | 3.91k | || !EVP_DigestUpdate(c, seed, seedlen) |
357 | 3.91k | || !EVP_DigestUpdate(c, cnt, 4)) |
358 | 0 | goto err; |
359 | 3.91k | if (outlen + mdlen <= len) { |
360 | 3.32k | if (!EVP_DigestFinal_ex(c, mask + outlen, NULL)) |
361 | 0 | goto err; |
362 | 3.32k | outlen += mdlen; |
363 | 3.32k | } else { |
364 | 592 | if (!EVP_DigestFinal_ex(c, md, NULL)) |
365 | 0 | goto err; |
366 | 592 | memcpy(mask + outlen, md, len - outlen); |
367 | 592 | outlen = len; |
368 | 592 | } |
369 | 3.91k | } |
370 | 616 | rv = 0; |
371 | 616 | err: |
372 | 616 | OPENSSL_cleanse(md, sizeof(md)); |
373 | 616 | EVP_MD_CTX_free(c); |
374 | 616 | return rv; |
375 | 616 | } |