]>
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 | ||
706457b7 | 23 | #include "internal/constant_time.h" |
a4949896 | 24 | |
474e469b | 25 | #include <stdio.h> |
b39fc560 | 26 | #include "internal/cryptlib.h" |
474e469b | 27 | #include <openssl/bn.h> |
474e469b RS |
28 | #include <openssl/evp.h> |
29 | #include <openssl/rand.h> | |
30 | #include <openssl/sha.h> | |
706457b7 | 31 | #include "rsa_local.h" |
a4949896 | 32 | |
6b691a5c | 33 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, |
0f113f3e MC |
34 | const unsigned char *from, int flen, |
35 | const unsigned char *param, int plen) | |
36 | { | |
37 | return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen, | |
38 | param, plen, NULL, NULL); | |
39 | } | |
271fef0e | 40 | |
ad7e17dd P |
41 | /* |
42 | * Perform ihe padding as per NIST 800-56B 7.2.2.3 | |
43 | * from (K) is the key material. | |
44 | * param (A) is the additional input. | |
45 | * Step numbers are included here but not in the constant time inverse below | |
46 | * to avoid complicating an already difficult enough function. | |
47 | */ | |
271fef0e | 48 | int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, |
0f113f3e MC |
49 | const unsigned char *from, int flen, |
50 | const unsigned char *param, int plen, | |
51 | const EVP_MD *md, const EVP_MD *mgf1md) | |
52 | { | |
82eba370 | 53 | int rv = 0; |
0f113f3e MC |
54 | int i, emlen = tlen - 1; |
55 | unsigned char *db, *seed; | |
82eba370 SL |
56 | unsigned char *dbmask = NULL; |
57 | unsigned char seedmask[EVP_MAX_MD_SIZE]; | |
58 | int mdlen, dbmask_len = 0; | |
0f113f3e MC |
59 | |
60 | if (md == NULL) | |
61 | md = EVP_sha1(); | |
62 | if (mgf1md == NULL) | |
63 | mgf1md = md; | |
64 | ||
65 | mdlen = EVP_MD_size(md); | |
66 | ||
ad7e17dd | 67 | /* step 2b: check KLen > nLen - 2 HLen - 2 */ |
0f113f3e MC |
68 | if (flen > emlen - 2 * mdlen - 1) { |
69 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, | |
70 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | |
71 | return 0; | |
72 | } | |
73 | ||
74 | if (emlen < 2 * mdlen + 1) { | |
75 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, | |
76 | RSA_R_KEY_SIZE_TOO_SMALL); | |
77 | return 0; | |
78 | } | |
79 | ||
ad7e17dd | 80 | /* step 3i: EM = 00000000 || maskedMGF || maskedDB */ |
0f113f3e MC |
81 | to[0] = 0; |
82 | seed = to + 1; | |
83 | db = to + mdlen + 1; | |
84 | ||
ad7e17dd | 85 | /* step 3a: hash the additional input */ |
0f113f3e | 86 | if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL)) |
82eba370 | 87 | goto err; |
ad7e17dd | 88 | /* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */ |
0f113f3e | 89 | memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1); |
ad7e17dd | 90 | /* step 3c: DB = HA || PS || 00000001 || K */ |
0f113f3e MC |
91 | db[emlen - flen - mdlen - 1] = 0x01; |
92 | memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen); | |
ad7e17dd | 93 | /* step 3d: generate random byte string */ |
0f113f3e | 94 | if (RAND_bytes(seed, mdlen) <= 0) |
82eba370 | 95 | goto err; |
0f113f3e | 96 | |
82eba370 SL |
97 | dbmask_len = emlen - mdlen; |
98 | dbmask = OPENSSL_malloc(dbmask_len); | |
0f113f3e MC |
99 | if (dbmask == NULL) { |
100 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE); | |
82eba370 | 101 | goto err; |
0f113f3e MC |
102 | } |
103 | ||
ad7e17dd | 104 | /* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */ |
82eba370 | 105 | if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0) |
c6d215e0 | 106 | goto err; |
ad7e17dd | 107 | /* step 3f: maskedDB = DB XOR dbMask */ |
82eba370 | 108 | for (i = 0; i < dbmask_len; i++) |
0f113f3e MC |
109 | db[i] ^= dbmask[i]; |
110 | ||
ad7e17dd | 111 | /* step 3g: mgfSeed = MGF(maskedDB, HLen) */ |
82eba370 | 112 | if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0) |
c6d215e0 | 113 | goto err; |
ad7e17dd | 114 | /* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */ |
0f113f3e MC |
115 | for (i = 0; i < mdlen; i++) |
116 | seed[i] ^= seedmask[i]; | |
82eba370 | 117 | rv = 1; |
c6d215e0 BE |
118 | |
119 | err: | |
82eba370 SL |
120 | OPENSSL_cleanse(seedmask, sizeof(seedmask)); |
121 | OPENSSL_clear_free(dbmask, dbmask_len); | |
122 | return rv; | |
0f113f3e | 123 | } |
a4949896 | 124 | |
6b691a5c | 125 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, |
0f113f3e MC |
126 | const unsigned char *from, int flen, int num, |
127 | const unsigned char *param, int plen) | |
128 | { | |
129 | return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num, | |
130 | param, plen, NULL, NULL); | |
131 | } | |
271fef0e DSH |
132 | |
133 | int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, | |
0f113f3e MC |
134 | const unsigned char *from, int flen, |
135 | int num, const unsigned char *param, | |
136 | int plen, const EVP_MD *md, | |
137 | const EVP_MD *mgf1md) | |
138 | { | |
e670db01 | 139 | int i, dblen = 0, mlen = -1, one_index = 0, msg_index; |
75f5e944 | 140 | unsigned int good = 0, found_one_byte, mask; |
0f113f3e MC |
141 | const unsigned char *maskedseed, *maskeddb; |
142 | /* | |
143 | * |em| is the encoded message, zero-padded to exactly |num| bytes: em = | |
144 | * Y || maskedSeed || maskedDB | |
145 | */ | |
146 | unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE], | |
147 | phash[EVP_MAX_MD_SIZE]; | |
148 | int mdlen; | |
149 | ||
150 | if (md == NULL) | |
151 | md = EVP_sha1(); | |
152 | if (mgf1md == NULL) | |
153 | mgf1md = md; | |
154 | ||
155 | mdlen = EVP_MD_size(md); | |
156 | ||
157 | if (tlen <= 0 || flen <= 0) | |
158 | return -1; | |
159 | /* | |
160 | * |num| is the length of the modulus; |flen| is the length of the | |
161 | * encoded message. Therefore, for any |from| that was obtained by | |
162 | * decrypting a ciphertext, we must have |flen| <= |num|. Similarly, | |
d7f5e5ae | 163 | * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of |
0f113f3e MC |
164 | * the ciphertext, see PKCS #1 v2.2, section 7.1.2. |
165 | * This does not leak any side-channel information. | |
166 | */ | |
75f5e944 AP |
167 | if (num < flen || num < 2 * mdlen + 2) { |
168 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | |
169 | RSA_R_OAEP_DECODING_ERROR); | |
170 | return -1; | |
171 | } | |
0f113f3e MC |
172 | |
173 | dblen = num - mdlen - 1; | |
174 | db = OPENSSL_malloc(dblen); | |
582ad5d4 | 175 | if (db == NULL) { |
0f113f3e MC |
176 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE); |
177 | goto cleanup; | |
178 | } | |
179 | ||
75f5e944 AP |
180 | em = OPENSSL_malloc(num); |
181 | if (em == NULL) { | |
182 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | |
183 | ERR_R_MALLOC_FAILURE); | |
184 | goto cleanup; | |
185 | } | |
582ad5d4 | 186 | |
75f5e944 AP |
187 | /* |
188 | * Caller is encouraged to pass zero-padded message created with | |
189 | * BN_bn2binpad. Trouble is that since we can't read out of |from|'s | |
190 | * bounds, it's impossible to have an invariant memory access pattern | |
191 | * in case |from| was not zero-padded in advance. | |
192 | */ | |
193 | for (from += flen, em += num, i = 0; i < num; i++) { | |
194 | mask = ~constant_time_is_zero(flen); | |
195 | flen -= 1 & mask; | |
196 | from -= 1 & mask; | |
197 | *--em = *from & mask; | |
582ad5d4 | 198 | } |
0f113f3e MC |
199 | |
200 | /* | |
201 | * The first byte must be zero, however we must not leak if this is | |
202 | * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA | |
203 | * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001). | |
204 | */ | |
d7f5e5ae | 205 | good = constant_time_is_zero(em[0]); |
0f113f3e | 206 | |
d7f5e5ae BE |
207 | maskedseed = em + 1; |
208 | maskeddb = em + 1 + mdlen; | |
0f113f3e MC |
209 | |
210 | if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) | |
211 | goto cleanup; | |
212 | for (i = 0; i < mdlen; i++) | |
213 | seed[i] ^= maskedseed[i]; | |
214 | ||
215 | if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) | |
216 | goto cleanup; | |
217 | for (i = 0; i < dblen; i++) | |
218 | db[i] ^= maskeddb[i]; | |
219 | ||
220 | if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL)) | |
221 | goto cleanup; | |
222 | ||
223 | good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen)); | |
224 | ||
225 | found_one_byte = 0; | |
226 | for (i = mdlen; i < dblen; i++) { | |
227 | /* | |
228 | * Padding consists of a number of 0-bytes, followed by a 1. | |
229 | */ | |
230 | unsigned int equals1 = constant_time_eq(db[i], 1); | |
231 | unsigned int equals0 = constant_time_is_zero(db[i]); | |
232 | one_index = constant_time_select_int(~found_one_byte & equals1, | |
233 | i, one_index); | |
234 | found_one_byte |= equals1; | |
235 | good &= (found_one_byte | equals0); | |
236 | } | |
237 | ||
238 | good &= found_one_byte; | |
239 | ||
240 | /* | |
241 | * At this point |good| is zero unless the plaintext was valid, | |
242 | * so plaintext-awareness ensures timing side-channels are no longer a | |
243 | * concern. | |
244 | */ | |
0f113f3e MC |
245 | msg_index = one_index + 1; |
246 | mlen = dblen - msg_index; | |
247 | ||
75f5e944 | 248 | /* |
d7f5e5ae | 249 | * For good measure, do this check in constant time as well. |
75f5e944 AP |
250 | */ |
251 | good &= constant_time_ge(tlen, mlen); | |
252 | ||
253 | /* | |
9c0cf214 BE |
254 | * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left. |
255 | * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|. | |
256 | * Otherwise leave |to| unchanged. | |
257 | * Copy the memory back in a way that does not reveal the size of | |
258 | * the data being copied via a timing side channel. This requires copying | |
259 | * parts of the buffer multiple times based on the bits set in the real | |
260 | * length. Clear bits do a non-copy with identical access pattern. | |
261 | * The loop below has overall complexity of O(N*log(N)). | |
75f5e944 | 262 | */ |
d7f5e5ae BE |
263 | tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen), |
264 | dblen - mdlen - 1, tlen); | |
9c0cf214 BE |
265 | for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) { |
266 | mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0); | |
267 | for (i = mdlen + 1; i < dblen - msg_index; i++) | |
268 | db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]); | |
269 | } | |
270 | for (i = 0; i < tlen; i++) { | |
271 | mask = good & constant_time_lt(i, mlen); | |
272 | to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]); | |
0f113f3e MC |
273 | } |
274 | ||
0f113f3e MC |
275 | /* |
276 | * To avoid chosen ciphertext attacks, the error message should not | |
277 | * reveal which kind of decoding error happened. | |
278 | */ | |
279 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | |
280 | RSA_R_OAEP_DECODING_ERROR); | |
75f5e944 | 281 | err_clear_last_constant_time(1 & good); |
0f113f3e | 282 | cleanup: |
82eba370 | 283 | OPENSSL_cleanse(seed, sizeof(seed)); |
e670db01 BE |
284 | OPENSSL_clear_free(db, dblen); |
285 | OPENSSL_clear_free(em, num); | |
75f5e944 AP |
286 | |
287 | return constant_time_select_int(good, mlen, -1); | |
0f113f3e | 288 | } |
a4949896 | 289 | |
ad7e17dd P |
290 | /* |
291 | * Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B. | |
292 | * The variables are named differently to NIST: | |
293 | * mask (T) and len (maskLen)are the returned mask. | |
294 | * seed (mgfSeed). | |
295 | * The range checking steps inm the process are performed outside. | |
296 | */ | |
499fca2d | 297 | int PKCS1_MGF1(unsigned char *mask, long len, |
0f113f3e MC |
298 | const unsigned char *seed, long seedlen, const EVP_MD *dgst) |
299 | { | |
300 | long i, outlen = 0; | |
301 | unsigned char cnt[4]; | |
bfb0641f | 302 | EVP_MD_CTX *c = EVP_MD_CTX_new(); |
0f113f3e MC |
303 | unsigned char md[EVP_MAX_MD_SIZE]; |
304 | int mdlen; | |
305 | int rv = -1; | |
306 | ||
6e59a892 RL |
307 | if (c == NULL) |
308 | goto err; | |
309 | mdlen = EVP_MD_size(dgst); | |
0f113f3e MC |
310 | if (mdlen < 0) |
311 | goto err; | |
ad7e17dd | 312 | /* step 4 */ |
0f113f3e | 313 | for (i = 0; outlen < len; i++) { |
ad7e17dd | 314 | /* step 4a: D = I2BS(counter, 4) */ |
0f113f3e MC |
315 | cnt[0] = (unsigned char)((i >> 24) & 255); |
316 | cnt[1] = (unsigned char)((i >> 16) & 255); | |
317 | cnt[2] = (unsigned char)((i >> 8)) & 255; | |
318 | cnt[3] = (unsigned char)(i & 255); | |
ad7e17dd | 319 | /* step 4b: T =T || hash(mgfSeed || D) */ |
6e59a892 RL |
320 | if (!EVP_DigestInit_ex(c, dgst, NULL) |
321 | || !EVP_DigestUpdate(c, seed, seedlen) | |
322 | || !EVP_DigestUpdate(c, cnt, 4)) | |
0f113f3e MC |
323 | goto err; |
324 | if (outlen + mdlen <= len) { | |
6e59a892 | 325 | if (!EVP_DigestFinal_ex(c, mask + outlen, NULL)) |
0f113f3e MC |
326 | goto err; |
327 | outlen += mdlen; | |
328 | } else { | |
6e59a892 | 329 | if (!EVP_DigestFinal_ex(c, md, NULL)) |
0f113f3e MC |
330 | goto err; |
331 | memcpy(mask + outlen, md, len - outlen); | |
332 | outlen = len; | |
333 | } | |
334 | } | |
335 | rv = 0; | |
336 | err: | |
82eba370 | 337 | OPENSSL_cleanse(md, sizeof(md)); |
bfb0641f | 338 | EVP_MD_CTX_free(c); |
0f113f3e MC |
339 | return rv; |
340 | } |