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 #define OPENSSL_FIPSAPI
24 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
27 #include <openssl/bn.h>
28 #include <openssl/rsa.h>
29 #include <openssl/evp.h>
30 #include <openssl/rand.h>
31 #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)
60 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
61 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
65 if (emlen < 2 * mdlen + 1)
67 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, RSA_R_KEY_SIZE_TOO_SMALL);
75 if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
78 emlen - flen - 2 * mdlen - 1);
79 db[emlen - flen - mdlen - 1] = 0x01;
80 memcpy(db + emlen - flen - mdlen, from, (unsigned int) flen);
81 if (RAND_bytes(seed, mdlen) <= 0)
85 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
89 dbmask = OPENSSL_malloc(emlen - mdlen);
92 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
96 if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
98 for (i = 0; i < emlen - mdlen; i++)
101 if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
103 for (i = 0; i < mdlen; i++)
104 seed[i] ^= seedmask[i];
106 OPENSSL_free(dbmask);
110 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
111 const unsigned char *from, int flen, int num,
112 const unsigned char *param, int plen)
114 return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from , flen, num,
119 int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
120 const unsigned char *from, int flen, int num,
121 const unsigned char *param, int plen,
122 const EVP_MD *md, const EVP_MD *mgf1md)
124 int i, dblen, mlen = -1;
125 const unsigned char *maskeddb;
127 unsigned char *db = NULL, seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE];
128 unsigned char *padded_from;
137 mdlen = EVP_MD_size(md);
139 if (--num < 2 * mdlen + 1)
140 /* 'num' is the length of the modulus, i.e. does not depend on the
141 * particular ciphertext. */
147 /* signalling this error immediately after detection might allow
148 * for side-channel attacks (e.g. timing if 'plen' is huge
149 * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
150 * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
151 * so we use a 'bad' flag */
154 flen = num; /* don't overflow the memcpy to padded_from */
158 db = OPENSSL_malloc(dblen + num);
161 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
165 /* Always do this zero-padding copy (even when lzero == 0)
166 * to avoid leaking timing info about the value of lzero. */
167 padded_from = db + dblen;
168 memset(padded_from, 0, lzero);
169 memcpy(padded_from + lzero, from, flen);
171 maskeddb = padded_from + mdlen;
173 if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
175 for (i = 0; i < mdlen; i++)
176 seed[i] ^= padded_from[i];
178 if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
180 for (i = 0; i < dblen; i++)
181 db[i] ^= maskeddb[i];
183 if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
186 if (CRYPTO_memcmp(db, phash, mdlen) != 0 || bad)
190 for (i = mdlen; i < dblen; i++)
193 if (i == dblen || db[i] != 0x01)
197 /* everything looks OK */
202 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);
206 memcpy(to, db + i, mlen);
213 /* to avoid chosen ciphertext attacks, the error message should not reveal
214 * which kind of decoding error happened */
215 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_OAEP_DECODING_ERROR);
216 if (db != NULL) OPENSSL_free(db);
220 int PKCS1_MGF1(unsigned char *mask, long len,
221 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
224 unsigned char cnt[4];
226 unsigned char md[EVP_MAX_MD_SIZE];
231 mdlen = M_EVP_MD_size(dgst);
234 for (i = 0; outlen < len; i++)
236 cnt[0] = (unsigned char)((i >> 24) & 255);
237 cnt[1] = (unsigned char)((i >> 16) & 255);
238 cnt[2] = (unsigned char)((i >> 8)) & 255;
239 cnt[3] = (unsigned char)(i & 255);
240 if (!EVP_DigestInit_ex(&c,dgst, NULL)
241 || !EVP_DigestUpdate(&c, seed, seedlen)
242 || !EVP_DigestUpdate(&c, cnt, 4))
244 if (outlen + mdlen <= len)
246 if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
252 if (!EVP_DigestFinal_ex(&c, md, NULL))
254 memcpy(mask + outlen, md, len - outlen);
260 EVP_MD_CTX_cleanup(&c);