]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
d7f5e5ae | 2 | * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved. |
2039c421 | 3 | * |
2a7b6f39 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2039c421 RS |
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 | |
0f113f3e | 8 | */ |
a4949896 | 9 | |
9347ba48 BM |
10 | /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */ |
11 | ||
0f113f3e MC |
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. | |
9347ba48 BM |
21 | */ |
22 | ||
c5f87134 P |
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 | ||
706457b7 | 29 | #include "internal/constant_time.h" |
a4949896 | 30 | |
474e469b | 31 | #include <stdio.h> |
b39fc560 | 32 | #include "internal/cryptlib.h" |
474e469b | 33 | #include <openssl/bn.h> |
474e469b RS |
34 | #include <openssl/evp.h> |
35 | #include <openssl/rand.h> | |
36 | #include <openssl/sha.h> | |
706457b7 | 37 | #include "rsa_local.h" |
a4949896 | 38 | |
6b691a5c | 39 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, |
0f113f3e MC |
40 | const unsigned char *from, int flen, |
41 | const unsigned char *param, int plen) | |
42 | { | |
0f2deef5 MC |
43 | return rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(NULL, to, tlen, from, |
44 | flen, param, plen, NULL, | |
45 | NULL); | |
0f113f3e | 46 | } |
271fef0e | 47 | |
ad7e17dd P |
48 | /* |
49 | * Perform ihe padding as per NIST 800-56B 7.2.2.3 | |
50 | * from (K) is the key material. | |
51 | * param (A) is the additional input. | |
52 | * Step numbers are included here but not in the constant time inverse below | |
53 | * to avoid complicating an already difficult enough function. | |
54 | */ | |
0f2deef5 MC |
55 | int rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(OPENSSL_CTX *libctx, |
56 | unsigned char *to, int tlen, | |
57 | const unsigned char *from, | |
58 | int flen, | |
59 | const unsigned char *param, | |
60 | int plen, const EVP_MD *md, | |
61 | const EVP_MD *mgf1md) | |
0f113f3e | 62 | { |
82eba370 | 63 | int rv = 0; |
0f113f3e MC |
64 | int i, emlen = tlen - 1; |
65 | unsigned char *db, *seed; | |
82eba370 SL |
66 | unsigned char *dbmask = NULL; |
67 | unsigned char seedmask[EVP_MAX_MD_SIZE]; | |
68 | int mdlen, dbmask_len = 0; | |
0f113f3e | 69 | |
afb638f1 | 70 | #ifndef FIPS_MODE |
0f113f3e MC |
71 | if (md == NULL) |
72 | md = EVP_sha1(); | |
afb638f1 | 73 | #else |
0f2deef5 | 74 | RSAerr(0, ERR_R_PASSED_NULL_PARAMETER); |
afb638f1 MC |
75 | return 0; |
76 | #endif | |
0f113f3e MC |
77 | if (mgf1md == NULL) |
78 | mgf1md = md; | |
79 | ||
80 | mdlen = EVP_MD_size(md); | |
81 | ||
ad7e17dd | 82 | /* step 2b: check KLen > nLen - 2 HLen - 2 */ |
0f113f3e | 83 | if (flen > emlen - 2 * mdlen - 1) { |
0f2deef5 | 84 | RSAerr(0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
0f113f3e MC |
85 | return 0; |
86 | } | |
87 | ||
88 | if (emlen < 2 * mdlen + 1) { | |
0f2deef5 | 89 | RSAerr(0, RSA_R_KEY_SIZE_TOO_SMALL); |
0f113f3e MC |
90 | return 0; |
91 | } | |
92 | ||
ad7e17dd | 93 | /* step 3i: EM = 00000000 || maskedMGF || maskedDB */ |
0f113f3e MC |
94 | to[0] = 0; |
95 | seed = to + 1; | |
96 | db = to + mdlen + 1; | |
97 | ||
ad7e17dd | 98 | /* step 3a: hash the additional input */ |
0f113f3e | 99 | if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL)) |
82eba370 | 100 | goto err; |
ad7e17dd | 101 | /* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */ |
0f113f3e | 102 | memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1); |
ad7e17dd | 103 | /* step 3c: DB = HA || PS || 00000001 || K */ |
0f113f3e MC |
104 | db[emlen - flen - mdlen - 1] = 0x01; |
105 | memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen); | |
ad7e17dd | 106 | /* step 3d: generate random byte string */ |
0f2deef5 | 107 | if (RAND_bytes_ex(libctx, seed, mdlen) <= 0) |
82eba370 | 108 | goto err; |
0f113f3e | 109 | |
82eba370 SL |
110 | dbmask_len = emlen - mdlen; |
111 | dbmask = OPENSSL_malloc(dbmask_len); | |
0f113f3e | 112 | if (dbmask == NULL) { |
0f2deef5 | 113 | RSAerr(0, ERR_R_MALLOC_FAILURE); |
82eba370 | 114 | goto err; |
0f113f3e MC |
115 | } |
116 | ||
ad7e17dd | 117 | /* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */ |
82eba370 | 118 | if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0) |
c6d215e0 | 119 | goto err; |
ad7e17dd | 120 | /* step 3f: maskedDB = DB XOR dbMask */ |
82eba370 | 121 | for (i = 0; i < dbmask_len; i++) |
0f113f3e MC |
122 | db[i] ^= dbmask[i]; |
123 | ||
ad7e17dd | 124 | /* step 3g: mgfSeed = MGF(maskedDB, HLen) */ |
82eba370 | 125 | if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0) |
c6d215e0 | 126 | goto err; |
ad7e17dd | 127 | /* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */ |
0f113f3e MC |
128 | for (i = 0; i < mdlen; i++) |
129 | seed[i] ^= seedmask[i]; | |
82eba370 | 130 | rv = 1; |
c6d215e0 BE |
131 | |
132 | err: | |
82eba370 SL |
133 | OPENSSL_cleanse(seedmask, sizeof(seedmask)); |
134 | OPENSSL_clear_free(dbmask, dbmask_len); | |
135 | return rv; | |
0f113f3e | 136 | } |
a4949896 | 137 | |
0f2deef5 MC |
138 | int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, |
139 | const unsigned char *from, int flen, | |
140 | const unsigned char *param, int plen, | |
141 | const EVP_MD *md, const EVP_MD *mgf1md) | |
142 | { | |
143 | return rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(NULL, to, tlen, from, | |
144 | flen, param, plen, md, | |
145 | mgf1md); | |
146 | } | |
147 | ||
6b691a5c | 148 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, |
0f113f3e MC |
149 | const unsigned char *from, int flen, int num, |
150 | const unsigned char *param, int plen) | |
151 | { | |
152 | return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num, | |
153 | param, plen, NULL, NULL); | |
154 | } | |
271fef0e DSH |
155 | |
156 | int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, | |
0f113f3e MC |
157 | const unsigned char *from, int flen, |
158 | int num, const unsigned char *param, | |
159 | int plen, const EVP_MD *md, | |
160 | const EVP_MD *mgf1md) | |
161 | { | |
e670db01 | 162 | int i, dblen = 0, mlen = -1, one_index = 0, msg_index; |
75f5e944 | 163 | unsigned int good = 0, found_one_byte, mask; |
0f113f3e MC |
164 | const unsigned char *maskedseed, *maskeddb; |
165 | /* | |
166 | * |em| is the encoded message, zero-padded to exactly |num| bytes: em = | |
167 | * Y || maskedSeed || maskedDB | |
168 | */ | |
169 | unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE], | |
170 | phash[EVP_MAX_MD_SIZE]; | |
171 | int mdlen; | |
172 | ||
afb638f1 MC |
173 | if (md == NULL) { |
174 | #ifndef FIPS_MODE | |
0f113f3e | 175 | md = EVP_sha1(); |
afb638f1 MC |
176 | #else |
177 | RSAerr(0, ERR_R_PASSED_NULL_PARAMETER); | |
178 | return -1; | |
179 | #endif | |
180 | } | |
181 | ||
0f113f3e MC |
182 | if (mgf1md == NULL) |
183 | mgf1md = md; | |
184 | ||
185 | mdlen = EVP_MD_size(md); | |
186 | ||
187 | if (tlen <= 0 || flen <= 0) | |
188 | return -1; | |
189 | /* | |
190 | * |num| is the length of the modulus; |flen| is the length of the | |
191 | * encoded message. Therefore, for any |from| that was obtained by | |
192 | * decrypting a ciphertext, we must have |flen| <= |num|. Similarly, | |
d7f5e5ae | 193 | * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of |
0f113f3e MC |
194 | * the ciphertext, see PKCS #1 v2.2, section 7.1.2. |
195 | * This does not leak any side-channel information. | |
196 | */ | |
75f5e944 AP |
197 | if (num < flen || num < 2 * mdlen + 2) { |
198 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | |
199 | RSA_R_OAEP_DECODING_ERROR); | |
200 | return -1; | |
201 | } | |
0f113f3e MC |
202 | |
203 | dblen = num - mdlen - 1; | |
204 | db = OPENSSL_malloc(dblen); | |
582ad5d4 | 205 | if (db == NULL) { |
0f113f3e MC |
206 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE); |
207 | goto cleanup; | |
208 | } | |
209 | ||
75f5e944 AP |
210 | em = OPENSSL_malloc(num); |
211 | if (em == NULL) { | |
212 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | |
213 | ERR_R_MALLOC_FAILURE); | |
214 | goto cleanup; | |
215 | } | |
582ad5d4 | 216 | |
75f5e944 AP |
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 | for (from += flen, em += num, i = 0; i < num; i++) { | |
224 | mask = ~constant_time_is_zero(flen); | |
225 | flen -= 1 & mask; | |
226 | from -= 1 & mask; | |
227 | *--em = *from & mask; | |
582ad5d4 | 228 | } |
0f113f3e MC |
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 | */ | |
d7f5e5ae | 235 | good = constant_time_is_zero(em[0]); |
0f113f3e | 236 | |
d7f5e5ae BE |
237 | maskedseed = em + 1; |
238 | maskeddb = em + 1 + mdlen; | |
0f113f3e MC |
239 | |
240 | if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) | |
241 | goto cleanup; | |
242 | for (i = 0; i < mdlen; i++) | |
243 | seed[i] ^= maskedseed[i]; | |
244 | ||
245 | if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) | |
246 | goto cleanup; | |
247 | for (i = 0; i < dblen; i++) | |
248 | db[i] ^= maskeddb[i]; | |
249 | ||
250 | if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL)) | |
251 | goto cleanup; | |
252 | ||
253 | good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen)); | |
254 | ||
255 | found_one_byte = 0; | |
256 | for (i = mdlen; i < dblen; i++) { | |
257 | /* | |
258 | * Padding consists of a number of 0-bytes, followed by a 1. | |
259 | */ | |
260 | unsigned int equals1 = constant_time_eq(db[i], 1); | |
261 | unsigned int equals0 = constant_time_is_zero(db[i]); | |
262 | one_index = constant_time_select_int(~found_one_byte & equals1, | |
263 | i, one_index); | |
264 | found_one_byte |= equals1; | |
265 | good &= (found_one_byte | equals0); | |
266 | } | |
267 | ||
268 | 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 | */ | |
0f113f3e MC |
275 | msg_index = one_index + 1; |
276 | mlen = dblen - msg_index; | |
277 | ||
75f5e944 | 278 | /* |
d7f5e5ae | 279 | * For good measure, do this check in constant time as well. |
75f5e944 AP |
280 | */ |
281 | good &= constant_time_ge(tlen, mlen); | |
282 | ||
283 | /* | |
9c0cf214 BE |
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)). | |
75f5e944 | 292 | */ |
d7f5e5ae BE |
293 | tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen), |
294 | dblen - mdlen - 1, tlen); | |
9c0cf214 BE |
295 | for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) { |
296 | mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0); | |
297 | for (i = mdlen + 1; i < dblen - msg_index; i++) | |
298 | db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]); | |
299 | } | |
300 | for (i = 0; i < tlen; i++) { | |
301 | mask = good & constant_time_lt(i, mlen); | |
302 | to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]); | |
0f113f3e MC |
303 | } |
304 | ||
afb638f1 | 305 | #ifndef FIPS_MODE |
0f113f3e MC |
306 | /* |
307 | * To avoid chosen ciphertext attacks, the error message should not | |
308 | * reveal which kind of decoding error happened. | |
afb638f1 MC |
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. | |
0f113f3e MC |
313 | */ |
314 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | |
315 | RSA_R_OAEP_DECODING_ERROR); | |
75f5e944 | 316 | err_clear_last_constant_time(1 & good); |
afb638f1 | 317 | #endif |
0f113f3e | 318 | cleanup: |
82eba370 | 319 | OPENSSL_cleanse(seed, sizeof(seed)); |
e670db01 BE |
320 | OPENSSL_clear_free(db, dblen); |
321 | OPENSSL_clear_free(em, num); | |
75f5e944 AP |
322 | |
323 | return constant_time_select_int(good, mlen, -1); | |
0f113f3e | 324 | } |
a4949896 | 325 | |
ad7e17dd P |
326 | /* |
327 | * Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B. | |
328 | * The variables are named differently to NIST: | |
329 | * mask (T) and len (maskLen)are the returned mask. | |
330 | * seed (mgfSeed). | |
331 | * The range checking steps inm the process are performed outside. | |
332 | */ | |
499fca2d | 333 | int PKCS1_MGF1(unsigned char *mask, long len, |
0f113f3e MC |
334 | const unsigned char *seed, long seedlen, const EVP_MD *dgst) |
335 | { | |
336 | long i, outlen = 0; | |
337 | unsigned char cnt[4]; | |
bfb0641f | 338 | EVP_MD_CTX *c = EVP_MD_CTX_new(); |
0f113f3e MC |
339 | unsigned char md[EVP_MAX_MD_SIZE]; |
340 | int mdlen; | |
341 | int rv = -1; | |
342 | ||
6e59a892 RL |
343 | if (c == NULL) |
344 | goto err; | |
345 | mdlen = EVP_MD_size(dgst); | |
0f113f3e MC |
346 | if (mdlen < 0) |
347 | goto err; | |
ad7e17dd | 348 | /* step 4 */ |
0f113f3e | 349 | for (i = 0; outlen < len; i++) { |
ad7e17dd | 350 | /* step 4a: D = I2BS(counter, 4) */ |
0f113f3e MC |
351 | cnt[0] = (unsigned char)((i >> 24) & 255); |
352 | cnt[1] = (unsigned char)((i >> 16) & 255); | |
353 | cnt[2] = (unsigned char)((i >> 8)) & 255; | |
354 | cnt[3] = (unsigned char)(i & 255); | |
ad7e17dd | 355 | /* step 4b: T =T || hash(mgfSeed || D) */ |
6e59a892 RL |
356 | if (!EVP_DigestInit_ex(c, dgst, NULL) |
357 | || !EVP_DigestUpdate(c, seed, seedlen) | |
358 | || !EVP_DigestUpdate(c, cnt, 4)) | |
0f113f3e MC |
359 | goto err; |
360 | if (outlen + mdlen <= len) { | |
6e59a892 | 361 | if (!EVP_DigestFinal_ex(c, mask + outlen, NULL)) |
0f113f3e MC |
362 | goto err; |
363 | outlen += mdlen; | |
364 | } else { | |
6e59a892 | 365 | if (!EVP_DigestFinal_ex(c, md, NULL)) |
0f113f3e MC |
366 | goto err; |
367 | memcpy(mask + outlen, md, len - outlen); | |
368 | outlen = len; | |
369 | } | |
370 | } | |
371 | rv = 0; | |
372 | err: | |
82eba370 | 373 | OPENSSL_cleanse(md, sizeof(md)); |
bfb0641f | 374 | EVP_MD_CTX_free(c); |
0f113f3e MC |
375 | return rv; |
376 | } |