2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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.
23 #include "internal/constant_time_locl.h"
26 #include "internal/cryptlib.h"
27 #include <openssl/bn.h>
28 #include <openssl/evp.h>
29 #include <openssl/rand.h>
30 #include <openssl/sha.h>
33 int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
34 const unsigned char *from, int flen,
35 const unsigned char *param, int plen)
37 return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
38 param, plen, NULL, NULL);
41 int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
42 const unsigned char *from, int flen,
43 const unsigned char *param, int plen,
44 const EVP_MD *md, const EVP_MD *mgf1md)
46 int i, emlen = tlen - 1;
47 unsigned char *db, *seed;
48 unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
56 mdlen = EVP_MD_size(md);
58 if (flen > emlen - 2 * mdlen - 1) {
59 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
60 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
64 if (emlen < 2 * mdlen + 1) {
65 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
66 RSA_R_KEY_SIZE_TOO_SMALL);
74 if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
76 memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
77 db[emlen - flen - mdlen - 1] = 0x01;
78 memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
79 if (RAND_bytes(seed, mdlen) <= 0)
82 dbmask = OPENSSL_malloc(emlen - mdlen);
84 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
88 if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
90 for (i = 0; i < emlen - mdlen; i++)
93 if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
95 for (i = 0; i < mdlen; i++)
96 seed[i] ^= seedmask[i];
102 OPENSSL_free(dbmask);
106 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
107 const unsigned char *from, int flen, int num,
108 const unsigned char *param, int plen)
110 return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
111 param, plen, NULL, NULL);
114 int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
115 const unsigned char *from, int flen,
116 int num, const unsigned char *param,
117 int plen, const EVP_MD *md,
118 const EVP_MD *mgf1md)
120 int i, dblen, mlen = -1, one_index = 0, msg_index;
121 unsigned int good, found_one_byte;
122 const unsigned char *maskedseed, *maskeddb;
124 * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
125 * Y || maskedSeed || maskedDB
127 unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
128 phash[EVP_MAX_MD_SIZE];
136 mdlen = EVP_MD_size(md);
138 if (tlen <= 0 || flen <= 0)
141 * |num| is the length of the modulus; |flen| is the length of the
142 * encoded message. Therefore, for any |from| that was obtained by
143 * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
144 * num < 2 * mdlen + 2 must hold for the modulus irrespective of
145 * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
146 * This does not leak any side-channel information.
148 if (num < flen || num < 2 * mdlen + 2)
151 dblen = num - mdlen - 1;
152 db = OPENSSL_malloc(dblen);
153 em = OPENSSL_malloc(num);
154 if (db == NULL || em == NULL) {
155 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
160 * Always do this zero-padding copy (even when num == flen) to avoid
161 * leaking that information. The copy still leaks some side-channel
162 * information, but it's impossible to have a fixed memory access
163 * pattern since we can't read out of the bounds of |from|.
165 * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
168 memcpy(em + num - flen, from, flen);
171 * The first byte must be zero, however we must not leak if this is
172 * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
173 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
175 good = constant_time_is_zero(em[0]);
178 maskeddb = em + 1 + mdlen;
180 if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
182 for (i = 0; i < mdlen; i++)
183 seed[i] ^= maskedseed[i];
185 if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
187 for (i = 0; i < dblen; i++)
188 db[i] ^= maskeddb[i];
190 if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
193 good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
196 for (i = mdlen; i < dblen; i++) {
198 * Padding consists of a number of 0-bytes, followed by a 1.
200 unsigned int equals1 = constant_time_eq(db[i], 1);
201 unsigned int equals0 = constant_time_is_zero(db[i]);
202 one_index = constant_time_select_int(~found_one_byte & equals1,
204 found_one_byte |= equals1;
205 good &= (found_one_byte | equals0);
208 good &= found_one_byte;
211 * At this point |good| is zero unless the plaintext was valid,
212 * so plaintext-awareness ensures timing side-channels are no longer a
218 msg_index = one_index + 1;
219 mlen = dblen - msg_index;
222 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);
225 memcpy(to, db + msg_index, mlen);
231 * To avoid chosen ciphertext attacks, the error message should not
232 * reveal which kind of decoding error happened.
234 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
235 RSA_R_OAEP_DECODING_ERROR);
242 int PKCS1_MGF1(unsigned char *mask, long len,
243 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
246 unsigned char cnt[4];
247 EVP_MD_CTX *c = EVP_MD_CTX_new();
248 unsigned char md[EVP_MAX_MD_SIZE];
254 mdlen = EVP_MD_size(dgst);
257 for (i = 0; outlen < len; i++) {
258 cnt[0] = (unsigned char)((i >> 24) & 255);
259 cnt[1] = (unsigned char)((i >> 16) & 255);
260 cnt[2] = (unsigned char)((i >> 8)) & 255;
261 cnt[3] = (unsigned char)(i & 255);
262 if (!EVP_DigestInit_ex(c, dgst, NULL)
263 || !EVP_DigestUpdate(c, seed, seedlen)
264 || !EVP_DigestUpdate(c, cnt, 4))
266 if (outlen + mdlen <= len) {
267 if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
271 if (!EVP_DigestFinal_ex(c, md, NULL))
273 memcpy(mask + outlen, md, len - outlen);