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.
22 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
25 #include <openssl/bn.h>
26 #include <openssl/rsa.h>
27 #include <openssl/evp.h>
28 #include <openssl/rand.h>
29 #include <openssl/sha.h>
31 static int MGF1(unsigned char *mask, long len,
32 const unsigned char *seed, long seedlen);
34 int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
35 const unsigned char *from, int flen,
36 const unsigned char *param, int plen)
38 int i, emlen = tlen - 1;
39 unsigned char *db, *seed;
40 unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
42 if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
44 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
45 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
49 if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
51 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
57 db = to + SHA_DIGEST_LENGTH + 1;
59 if (!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 if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH) < 0)
82 for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
85 if (MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH) < 0)
87 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
88 seed[i] ^= seedmask[i];
94 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
95 const unsigned char *from, int flen, int num,
96 const unsigned char *param, int plen)
98 int i, dblen, mlen = -1;
99 const unsigned char *maskeddb;
101 unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
102 unsigned char *padded_from;
105 if (--num < 2 * SHA_DIGEST_LENGTH + 1)
106 /* 'num' is the length of the modulus, i.e. does not depend on the
107 * particular ciphertext. */
113 /* signalling this error immediately after detection might allow
114 * for side-channel attacks (e.g. timing if 'plen' is huge
115 * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
116 * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
117 * so we use a 'bad' flag */
120 flen = num; /* don't overflow the memcpy to padded_from */
123 dblen = num - SHA_DIGEST_LENGTH;
124 db = OPENSSL_malloc(dblen + num);
127 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
131 /* Always do this zero-padding copy (even when lzero == 0)
132 * to avoid leaking timing info about the value of lzero. */
133 padded_from = db + dblen;
134 memset(padded_from, 0, lzero);
135 memcpy(padded_from + lzero, from, flen);
137 maskeddb = padded_from + SHA_DIGEST_LENGTH;
139 if (MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen))
141 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
142 seed[i] ^= padded_from[i];
144 if (MGF1(db, dblen, seed, SHA_DIGEST_LENGTH))
146 for (i = 0; i < dblen; i++)
147 db[i] ^= maskeddb[i];
149 if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
152 if (CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
156 for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
159 if (i == dblen || db[i] != 0x01)
163 /* everything looks OK */
168 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
172 memcpy(to, db + i, mlen);
179 /* to avoid chosen ciphertext attacks, the error message should not reveal
180 * which kind of decoding error happened */
181 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
182 if (db != NULL) OPENSSL_free(db);
186 int PKCS1_MGF1(unsigned char *mask, long len,
187 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
190 unsigned char cnt[4];
192 unsigned char md[EVP_MAX_MD_SIZE];
197 mdlen = EVP_MD_size(dgst);
200 for (i = 0; outlen < len; i++)
202 cnt[0] = (unsigned char)((i >> 24) & 255);
203 cnt[1] = (unsigned char)((i >> 16) & 255);
204 cnt[2] = (unsigned char)((i >> 8)) & 255;
205 cnt[3] = (unsigned char)(i & 255);
206 if (!EVP_DigestInit_ex(&c,dgst, NULL)
207 || !EVP_DigestUpdate(&c, seed, seedlen)
208 || !EVP_DigestUpdate(&c, cnt, 4))
210 if (outlen + mdlen <= len)
212 if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
218 if (!EVP_DigestFinal_ex(&c, md, NULL))
220 memcpy(mask + outlen, md, len - outlen);
226 EVP_MD_CTX_cleanup(&c);
230 static int MGF1(unsigned char *mask, long len, const unsigned char *seed,
233 return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());