1 /* crypto/rsa/rsa_oaep.c */
2 /* Written by Ulf Moeller. This software is distributed on an "AS IS"
3 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */
5 /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
7 /* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
8 * <URL: http://www.shoup.net/papers/oaep.ps.Z>
9 * for problems with the security proof for the
10 * original OAEP scheme, which EME-OAEP is based on.
12 * A new proof can be found in E. Fujisaki, T. Okamoto,
13 * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
14 * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
15 * The new proof has stronger requirements for the
16 * underlying permutation: "partial-one-wayness" instead
17 * of one-wayness. For the RSA function, this is
18 * an equivalent notion.
21 #include "../constant_time_locl.h"
23 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
26 #include <openssl/bn.h>
27 #include <openssl/rsa.h>
28 #include <openssl/evp.h>
29 #include <openssl/rand.h>
30 #include <openssl/sha.h>
32 int MGF1(unsigned char *mask, long len,
33 const unsigned char *seed, long seedlen);
35 int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
36 const unsigned char *from, int flen,
37 const unsigned char *param, int plen)
39 int i, emlen = tlen - 1;
40 unsigned char *db, *seed;
41 unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
43 if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
45 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
46 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
50 if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
52 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
58 db = to + SHA_DIGEST_LENGTH + 1;
60 EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
61 memset(db + SHA_DIGEST_LENGTH, 0,
62 emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
63 db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
64 memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
65 if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
69 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
73 dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
76 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
80 MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
81 for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
84 MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
85 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
86 seed[i] ^= seedmask[i];
92 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
93 const unsigned char *from, int flen, int num,
94 const unsigned char *param, int plen)
96 int i, dblen, mlen = -1, one_index = 0, msg_index;
97 unsigned int good, found_one_byte;
98 const unsigned char *maskedseed, *maskeddb;
99 /* |em| is the encoded message, zero-padded to exactly |num| bytes:
100 * em = Y || maskedSeed || maskedDB */
101 unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
102 phash[EVP_MAX_MD_SIZE];
104 if (tlen <= 0 || flen <= 0)
108 * |num| is the length of the modulus; |flen| is the length of the
109 * encoded message. Therefore, for any |from| that was obtained by
110 * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
111 * num < 2 * SHA_DIGEST_LENGTH + 2 must hold for the modulus
112 * irrespective of the ciphertext, see PKCS #1 v2.2, section 7.1.2.
113 * This does not leak any side-channel information.
115 if (num < flen || num < 2 * SHA_DIGEST_LENGTH + 2)
118 dblen = num - SHA_DIGEST_LENGTH - 1;
119 db = OPENSSL_malloc(dblen);
120 em = OPENSSL_malloc(num);
121 if (db == NULL || em == NULL)
123 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
128 * Always do this zero-padding copy (even when num == flen) to avoid
129 * leaking that information. The copy still leaks some side-channel
130 * information, but it's impossible to have a fixed memory access
131 * pattern since we can't read out of the bounds of |from|.
133 * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
136 memcpy(em + num - flen, from, flen);
139 * The first byte must be zero, however we must not leak if this is
140 * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
141 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
143 good = constant_time_is_zero(em[0]);
146 maskeddb = em + 1 + SHA_DIGEST_LENGTH;
148 MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
149 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
150 seed[i] ^= maskedseed[i];
152 MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
153 for (i = 0; i < dblen; i++)
154 db[i] ^= maskeddb[i];
156 EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
158 good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH));
161 for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
163 /* Padding consists of a number of 0-bytes, followed by a 1. */
164 unsigned int equals1 = constant_time_eq(db[i], 1);
165 unsigned int equals0 = constant_time_is_zero(db[i]);
166 one_index = constant_time_select_int(~found_one_byte & equals1,
168 found_one_byte |= equals1;
169 good &= (found_one_byte | equals0);
172 good &= found_one_byte;
175 * At this point |good| is zero unless the plaintext was valid,
176 * so plaintext-awareness ensures timing side-channels are no longer a
182 msg_index = one_index + 1;
183 mlen = dblen - msg_index;
187 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
192 memcpy(to, db + msg_index, mlen);
197 /* To avoid chosen ciphertext attacks, the error message should not reveal
198 * which kind of decoding error happened. */
199 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
201 if (db != NULL) OPENSSL_free(db);
202 if (em != NULL) OPENSSL_free(em);
206 int PKCS1_MGF1(unsigned char *mask, long len,
207 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
210 unsigned char cnt[4];
212 unsigned char md[EVP_MAX_MD_SIZE];
216 mdlen = M_EVP_MD_size(dgst);
217 for (i = 0; outlen < len; i++)
219 cnt[0] = (unsigned char)((i >> 24) & 255);
220 cnt[1] = (unsigned char)((i >> 16) & 255);
221 cnt[2] = (unsigned char)((i >> 8)) & 255;
222 cnt[3] = (unsigned char)(i & 255);
223 EVP_DigestInit_ex(&c,dgst, NULL);
224 EVP_DigestUpdate(&c, seed, seedlen);
225 EVP_DigestUpdate(&c, cnt, 4);
226 if (outlen + mdlen <= len)
228 EVP_DigestFinal_ex(&c, mask + outlen, NULL);
233 EVP_DigestFinal_ex(&c, md, NULL);
234 memcpy(mask + outlen, md, len - outlen);
238 EVP_MD_CTX_cleanup(&c);
242 int MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen)
244 return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());