2 * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
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.
24 * RSA low level APIs are deprecated for public use, but still ok for
27 #include "internal/deprecated.h"
29 #include "internal/constant_time.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"
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)
43 return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
44 param, plen, NULL, NULL);
48 * Perform ihe 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.
54 int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
55 const unsigned char *from, int flen,
56 const unsigned char *param, int plen,
57 const EVP_MD *md, const EVP_MD *mgf1md)
60 int i, emlen = tlen - 1;
61 unsigned char *db, *seed;
62 unsigned char *dbmask = NULL;
63 unsigned char seedmask[EVP_MAX_MD_SIZE];
64 int mdlen, dbmask_len = 0;
70 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
71 ERR_R_PASSED_NULL_PARAMETER);
77 mdlen = EVP_MD_size(md);
79 /* step 2b: check KLen > nLen - 2 HLen - 2 */
80 if (flen > emlen - 2 * mdlen - 1) {
81 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
82 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
86 if (emlen < 2 * mdlen + 1) {
87 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
88 RSA_R_KEY_SIZE_TOO_SMALL);
92 /* step 3i: EM = 00000000 || maskedMGF || maskedDB */
97 /* step 3a: hash the additional input */
98 if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
100 /* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */
101 memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
102 /* step 3c: DB = HA || PS || 00000001 || K */
103 db[emlen - flen - mdlen - 1] = 0x01;
104 memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
105 /* step 3d: generate random byte string */
106 if (RAND_bytes(seed, mdlen) <= 0)
109 dbmask_len = emlen - mdlen;
110 dbmask = OPENSSL_malloc(dbmask_len);
111 if (dbmask == NULL) {
112 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
116 /* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */
117 if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
119 /* step 3f: maskedDB = DB XOR dbMask */
120 for (i = 0; i < dbmask_len; i++)
123 /* step 3g: mgfSeed = MGF(maskedDB, HLen) */
124 if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
126 /* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */
127 for (i = 0; i < mdlen; i++)
128 seed[i] ^= seedmask[i];
132 OPENSSL_cleanse(seedmask, sizeof(seedmask));
133 OPENSSL_clear_free(dbmask, dbmask_len);
137 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
138 const unsigned char *from, int flen, int num,
139 const unsigned char *param, int plen)
141 return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
142 param, plen, NULL, NULL);
145 int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
146 const unsigned char *from, int flen,
147 int num, const unsigned char *param,
148 int plen, const EVP_MD *md,
149 const EVP_MD *mgf1md)
151 int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
152 unsigned int good = 0, found_one_byte, mask;
153 const unsigned char *maskedseed, *maskeddb;
155 * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
156 * Y || maskedSeed || maskedDB
158 unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
159 phash[EVP_MAX_MD_SIZE];
166 RSAerr(0, ERR_R_PASSED_NULL_PARAMETER);
174 mdlen = EVP_MD_size(md);
176 if (tlen <= 0 || flen <= 0)
179 * |num| is the length of the modulus; |flen| is the length of the
180 * encoded message. Therefore, for any |from| that was obtained by
181 * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
182 * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of
183 * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
184 * This does not leak any side-channel information.
186 if (num < flen || num < 2 * mdlen + 2) {
187 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
188 RSA_R_OAEP_DECODING_ERROR);
192 dblen = num - mdlen - 1;
193 db = OPENSSL_malloc(dblen);
195 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
199 em = OPENSSL_malloc(num);
201 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
202 ERR_R_MALLOC_FAILURE);
207 * Caller is encouraged to pass zero-padded message created with
208 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
209 * bounds, it's impossible to have an invariant memory access pattern
210 * in case |from| was not zero-padded in advance.
212 for (from += flen, em += num, i = 0; i < num; i++) {
213 mask = ~constant_time_is_zero(flen);
216 *--em = *from & mask;
220 * The first byte must be zero, however we must not leak if this is
221 * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
222 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
224 good = constant_time_is_zero(em[0]);
227 maskeddb = em + 1 + mdlen;
229 if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
231 for (i = 0; i < mdlen; i++)
232 seed[i] ^= maskedseed[i];
234 if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
236 for (i = 0; i < dblen; i++)
237 db[i] ^= maskeddb[i];
239 if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
242 good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
245 for (i = mdlen; i < dblen; i++) {
247 * Padding consists of a number of 0-bytes, followed by a 1.
249 unsigned int equals1 = constant_time_eq(db[i], 1);
250 unsigned int equals0 = constant_time_is_zero(db[i]);
251 one_index = constant_time_select_int(~found_one_byte & equals1,
253 found_one_byte |= equals1;
254 good &= (found_one_byte | equals0);
257 good &= found_one_byte;
260 * At this point |good| is zero unless the plaintext was valid,
261 * so plaintext-awareness ensures timing side-channels are no longer a
264 msg_index = one_index + 1;
265 mlen = dblen - msg_index;
268 * For good measure, do this check in constant time as well.
270 good &= constant_time_ge(tlen, mlen);
273 * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left.
274 * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|.
275 * Otherwise leave |to| unchanged.
276 * Copy the memory back in a way that does not reveal the size of
277 * the data being copied via a timing side channel. This requires copying
278 * parts of the buffer multiple times based on the bits set in the real
279 * length. Clear bits do a non-copy with identical access pattern.
280 * The loop below has overall complexity of O(N*log(N)).
282 tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
283 dblen - mdlen - 1, tlen);
284 for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) {
285 mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0);
286 for (i = mdlen + 1; i < dblen - msg_index; i++)
287 db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]);
289 for (i = 0; i < tlen; i++) {
290 mask = good & constant_time_lt(i, mlen);
291 to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]);
296 * To avoid chosen ciphertext attacks, the error message should not
297 * reveal which kind of decoding error happened.
299 * This trick doesn't work in the FIPS provider because libcrypto manages
300 * the error stack. Instead we opt not to put an error on the stack at all
301 * in case of padding failure in the FIPS provider.
303 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
304 RSA_R_OAEP_DECODING_ERROR);
305 err_clear_last_constant_time(1 & good);
308 OPENSSL_cleanse(seed, sizeof(seed));
309 OPENSSL_clear_free(db, dblen);
310 OPENSSL_clear_free(em, num);
312 return constant_time_select_int(good, mlen, -1);
316 * Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B.
317 * The variables are named differently to NIST:
318 * mask (T) and len (maskLen)are the returned mask.
320 * The range checking steps inm the process are performed outside.
322 int PKCS1_MGF1(unsigned char *mask, long len,
323 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
326 unsigned char cnt[4];
327 EVP_MD_CTX *c = EVP_MD_CTX_new();
328 unsigned char md[EVP_MAX_MD_SIZE];
334 mdlen = EVP_MD_size(dgst);
338 for (i = 0; outlen < len; i++) {
339 /* step 4a: D = I2BS(counter, 4) */
340 cnt[0] = (unsigned char)((i >> 24) & 255);
341 cnt[1] = (unsigned char)((i >> 16) & 255);
342 cnt[2] = (unsigned char)((i >> 8)) & 255;
343 cnt[3] = (unsigned char)(i & 255);
344 /* step 4b: T =T || hash(mgfSeed || D) */
345 if (!EVP_DigestInit_ex(c, dgst, NULL)
346 || !EVP_DigestUpdate(c, seed, seedlen)
347 || !EVP_DigestUpdate(c, cnt, 4))
349 if (outlen + mdlen <= len) {
350 if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
354 if (!EVP_DigestFinal_ex(c, md, NULL))
356 memcpy(mask + outlen, md, len - outlen);
362 OPENSSL_cleanse(md, sizeof(md));