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(NO_SHA) && !defined(NO_SHA1)
25 #include <openssl/bn.h>
26 #include <openssl/rsa.h>
27 #include <openssl/sha.h>
28 #include <openssl/rand.h>
30 int MGF1(unsigned char *mask, long len,
31 unsigned char *seed, long seedlen);
33 int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
34 unsigned char *from, int flen,
35 unsigned char *param, int plen)
37 int i, emlen = tlen - 1;
38 unsigned char *db, *seed;
39 unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
41 if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
43 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
44 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
48 if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
50 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
54 dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
57 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
63 db = to + SHA_DIGEST_LENGTH + 1;
65 SHA1(param, plen, db);
66 memset(db + SHA_DIGEST_LENGTH, 0,
67 emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
68 db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
69 memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
70 if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
74 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
78 MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
79 for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
82 MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
83 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
84 seed[i] ^= seedmask[i];
90 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
91 unsigned char *from, int flen, int num, unsigned char *param,
94 int i, dblen, mlen = -1;
95 unsigned char *maskeddb;
97 unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
100 if (--num < 2 * SHA_DIGEST_LENGTH + 1)
101 /* 'num' is the length of the modulus, i.e. does not depend on the
102 * particular ciphertext. */
110 /* signalling this error immediately after detection might allow
111 * for side-channel attacks (e.g. timing if 'plen' is huge
112 * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
113 * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
114 * so we use a 'bad' flag */
118 maskeddb = from - lzero + SHA_DIGEST_LENGTH;
120 dblen = num - SHA_DIGEST_LENGTH;
121 db = OPENSSL_malloc(dblen);
124 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
128 MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
129 for (i = lzero; i < SHA_DIGEST_LENGTH; i++)
130 seed[i] ^= from[i - lzero];
132 MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
133 for (i = 0; i < dblen; i++)
134 db[i] ^= maskeddb[i];
136 SHA1(param, plen, phash);
138 if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
142 for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
145 if (db[i] != 0x01 || i++ >= dblen)
149 /* everything looks OK */
154 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
158 memcpy(to, db + i, mlen);
165 /* to avoid chosen ciphertext attacks, the error message should not reveal
166 * which kind of decoding error happened */
167 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
168 if (db != NULL) OPENSSL_free(db);
172 int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen)
175 unsigned char cnt[4];
177 unsigned char md[SHA_DIGEST_LENGTH];
179 for (i = 0; outlen < len; i++)
181 cnt[0] = (unsigned char)((i >> 24) & 255);
182 cnt[1] = (unsigned char)((i >> 16) & 255);
183 cnt[2] = (unsigned char)((i >> 8)) & 255;
184 cnt[3] = (unsigned char)(i & 255);
186 SHA1_Update(&c, seed, seedlen);
187 SHA1_Update(&c, cnt, 4);
188 if (outlen + SHA_DIGEST_LENGTH <= len)
190 SHA1_Final(mask + outlen, &c);
191 outlen += SHA_DIGEST_LENGTH;
196 memcpy(mask + outlen, md, len - outlen);