1 /* $OpenBSD: rsa_oaep.c,v 1.35 2022/02/20 19:16:34 tb Exp $ */
3 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
56 /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
58 /* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
59 * <URL: http://www.shoup.net/papers/oaep.ps.Z>
60 * for problems with the security proof for the
61 * original OAEP scheme, which EME-OAEP is based on.
63 * A new proof can be found in E. Fujisaki, T. Okamoto,
64 * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
65 * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
66 * The new proof has stronger requirements for the
67 * underlying permutation: "partial-one-wayness" instead
68 * of one-wayness. For the RSA function, this is
69 * an equivalent notion.
76 #include <openssl/bn.h>
77 #include <openssl/err.h>
78 #include <openssl/evp.h>
79 #include <openssl/rsa.h>
80 #include <openssl/sha.h>
82 #include "constant_time_locl.h"
87 RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
88 const unsigned char *from, int flen, const unsigned char *param, int plen)
90 return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen, param,
95 RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
96 const unsigned char *from, int flen, const unsigned char *param, int plen,
97 const EVP_MD *md, const EVP_MD *mgf1md)
99 int i, emlen = tlen - 1;
100 unsigned char *db, *seed;
101 unsigned char *dbmask = NULL;
102 unsigned char seedmask[EVP_MAX_MD_SIZE];
103 int mdlen, dbmask_len = 0;
111 if ((mdlen = EVP_MD_size(md)) <= 0)
114 if (flen > emlen - 2 * mdlen - 1) {
115 RSAerror(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
119 if (emlen < 2 * mdlen + 1) {
120 RSAerror(RSA_R_KEY_SIZE_TOO_SMALL);
128 if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
131 memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
132 db[emlen - flen - mdlen - 1] = 0x01;
133 memcpy(db + emlen - flen - mdlen, from, flen);
134 arc4random_buf(seed, mdlen);
136 dbmask_len = emlen - mdlen;
137 if ((dbmask = malloc(dbmask_len)) == NULL) {
138 RSAerror(ERR_R_MALLOC_FAILURE);
142 if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
144 for (i = 0; i < dbmask_len; i++)
146 if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
148 for (i = 0; i < mdlen; i++)
149 seed[i] ^= seedmask[i];
154 explicit_bzero(seedmask, sizeof(seedmask));
155 freezero(dbmask, dbmask_len);
161 RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
162 const unsigned char *from, int flen, int num, const unsigned char *param,
165 return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
166 param, plen, NULL, NULL);
170 RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
171 const unsigned char *from, int flen, int num, const unsigned char *param,
172 int plen, const EVP_MD *md, const EVP_MD *mgf1md)
174 int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
175 unsigned int good = 0, found_one_byte, mask;
176 const unsigned char *maskedseed, *maskeddb;
177 unsigned char seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE];
178 unsigned char *db = NULL, *em = NULL;
186 if ((mdlen = EVP_MD_size(md)) <= 0)
189 if (tlen <= 0 || flen <= 0)
193 * |num| is the length of the modulus; |flen| is the length of the
194 * encoded message. Therefore, for any |from| that was obtained by
195 * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
196 * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective
197 * of the ciphertext, see PKCS #1 v2.2, section 7.1.2.
198 * This does not leak any side-channel information.
200 if (num < flen || num < 2 * mdlen + 2) {
201 RSAerror(RSA_R_OAEP_DECODING_ERROR);
205 dblen = num - mdlen - 1;
206 if ((db = malloc(dblen)) == NULL) {
207 RSAerror(ERR_R_MALLOC_FAILURE);
210 if ((em = malloc(num)) == NULL) {
211 RSAerror(ERR_R_MALLOC_FAILURE);
216 * Caller is encouraged to pass zero-padded message created with
217 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
218 * bounds, it's impossible to have an invariant memory access pattern
219 * in case |from| was not zero-padded in advance.
221 for (from += flen, em += num, i = 0; i < num; i++) {
222 mask = ~constant_time_is_zero(flen);
225 *--em = *from & mask;
229 * The first byte must be zero, however we must not leak if this is
230 * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
231 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
233 good = constant_time_is_zero(em[0]);
236 maskeddb = em + 1 + mdlen;
238 if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
240 for (i = 0; i < mdlen; i++)
241 seed[i] ^= maskedseed[i];
243 if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
245 for (i = 0; i < dblen; i++)
246 db[i] ^= maskeddb[i];
248 if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
251 good &= constant_time_is_zero(timingsafe_memcmp(db, phash, mdlen));
254 for (i = mdlen; i < dblen; i++) {
256 * Padding consists of a number of 0-bytes, followed by a 1.
258 unsigned int equals1 = constant_time_eq(db[i], 1);
259 unsigned int equals0 = constant_time_is_zero(db[i]);
261 one_index = constant_time_select_int(~found_one_byte & equals1,
263 found_one_byte |= equals1;
264 good &= (found_one_byte | equals0);
267 good &= found_one_byte;
270 * At this point |good| is zero unless the plaintext was valid,
271 * so plaintext-awareness ensures timing side-channels are no longer a
274 msg_index = one_index + 1;
275 mlen = dblen - msg_index;
278 * For good measure, do this check in constant time as well.
280 good &= constant_time_ge(tlen, mlen);
283 * Even though we can't fake result's length, we can pretend copying
284 * |tlen| bytes where |mlen| bytes would be real. The last |tlen| of
285 * |dblen| bytes are viewed as a circular buffer starting at |tlen|-|mlen'|,
286 * where |mlen'| is the "saturated" |mlen| value. Deducing information
287 * about failure or |mlen| would require an attacker to observe
288 * memory access patterns with byte granularity *as it occurs*. It
289 * should be noted that failure is indistinguishable from normal
290 * operation if |tlen| is fixed by protocol.
292 tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
293 dblen - mdlen - 1, tlen);
294 msg_index = constant_time_select_int(good, msg_index, dblen - tlen);
295 mlen = dblen - msg_index;
296 for (mask = good, i = 0; i < tlen; i++) {
297 unsigned int equals = constant_time_eq(msg_index, dblen);
299 msg_index -= tlen & equals; /* rewind at EOF */
300 mask &= ~equals; /* mask = 0 at EOF */
301 to[i] = constant_time_select_8(mask, db[msg_index++], to[i]);
305 * To avoid chosen ciphertext attacks, the error message should not
306 * reveal which kind of decoding error happened.
308 RSAerror(RSA_R_OAEP_DECODING_ERROR);
309 err_clear_last_constant_time(1 & good);
312 explicit_bzero(seed, sizeof(seed));
316 return constant_time_select_int(good, mlen, -1);
320 PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed,
321 long seedlen, const EVP_MD *dgst)
324 unsigned char cnt[4];
326 unsigned char md[EVP_MAX_MD_SIZE];
331 mdlen = EVP_MD_size(dgst);
334 for (i = 0; outlen < len; i++) {
335 cnt[0] = (unsigned char)((i >> 24) & 255);
336 cnt[1] = (unsigned char)((i >> 16) & 255);
337 cnt[2] = (unsigned char)((i >> 8)) & 255;
338 cnt[3] = (unsigned char)(i & 255);
339 if (!EVP_DigestInit_ex(&c, dgst, NULL) ||
340 !EVP_DigestUpdate(&c, seed, seedlen) ||
341 !EVP_DigestUpdate(&c, cnt, 4))
343 if (outlen + mdlen <= len) {
344 if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
348 if (!EVP_DigestFinal_ex(&c, md, NULL))
350 memcpy(mask + outlen, md, len - outlen);
356 EVP_MD_CTX_cleanup(&c);