5f1e2e196c0ff17661f3572509db8b0e48e7ede3
[openssl.git] / crypto / rsa / rsa_oaep.c
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. */
4
5 /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
6
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.
11  * 
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.
19  */
20
21 #define OPENSSL_FIPSAPI
22
23 #include "constant_time_locl.h"
24
25 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
26 #include <stdio.h>
27 #include "cryptlib.h"
28 #include <openssl/bn.h>
29 #include <openssl/rsa.h>
30 #include <openssl/evp.h>
31 #include <openssl/rand.h>
32 #include <openssl/sha.h>
33
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)
37         {
38         return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
39                                                 param, plen, NULL, NULL);
40         }
41
42 int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
43         const unsigned char *from, int flen,
44         const unsigned char *param, int plen,
45         const EVP_MD *md, const EVP_MD *mgf1md)
46         {
47         int i, emlen = tlen - 1;
48         unsigned char *db, *seed;
49         unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
50         int mdlen;
51
52         if (md == NULL)
53                 md = EVP_sha1();
54         if (mgf1md == NULL)
55                 mgf1md = md;
56
57         mdlen = EVP_MD_size(md);
58
59         if (flen > emlen - 2 * mdlen - 1)
60                 {
61                 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
62                    RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
63                 return 0;
64                 }
65
66         if (emlen < 2 * mdlen + 1)
67                 {
68                 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, RSA_R_KEY_SIZE_TOO_SMALL);
69                 return 0;
70                 }
71
72         to[0] = 0;
73         seed = to + 1;
74         db = to + mdlen + 1;
75
76         if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
77                 return 0;
78         memset(db + mdlen, 0,
79                 emlen - flen - 2 * mdlen - 1);
80         db[emlen - flen - mdlen - 1] = 0x01;
81         memcpy(db + emlen - flen - mdlen, from, (unsigned int) flen);
82         if (RAND_bytes(seed, mdlen) <= 0)
83                 return 0;
84 #ifdef PKCS_TESTVECT
85         memcpy(seed,
86            "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
87            20);
88 #endif
89
90         dbmask = OPENSSL_malloc(emlen - mdlen);
91         if (dbmask == NULL)
92                 {
93                 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
94                 return 0;
95                 }
96
97         if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
98                 return 0;
99         for (i = 0; i < emlen - mdlen; i++)
100                 db[i] ^= dbmask[i];
101
102         if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
103                 return 0;
104         for (i = 0; i < mdlen; i++)
105                 seed[i] ^= seedmask[i];
106
107         OPENSSL_free(dbmask);
108         return 1;
109         }
110
111 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
112         const unsigned char *from, int flen, int num,
113         const unsigned char *param, int plen)
114         {
115         return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from , flen, num,
116                                                         param, plen,
117                                                         NULL, NULL);
118         }
119
120 int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
121         const unsigned char *from, int flen, int num,
122         const unsigned char *param, int plen,
123         const EVP_MD *md, const EVP_MD *mgf1md)
124         {
125         int i, dblen, mlen = -1, one_index = 0, msg_index;
126         unsigned int good, found_one_byte;
127         const unsigned char *maskedseed, *maskeddb;
128         /* |em| is the encoded message, zero-padded to exactly |num| bytes:
129          * em = Y || maskedSeed || maskedDB */
130         unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
131                 phash[EVP_MAX_MD_SIZE];
132         int mdlen;
133
134         if (md == NULL)
135                 md = EVP_sha1();
136         if (mgf1md == NULL)
137                 mgf1md = md;
138
139         mdlen = EVP_MD_size(md);
140
141         if (tlen <= 0 || flen <= 0)
142                 return -1;
143         /*
144          * |num| is the length of the modulus; |flen| is the length of the
145          * encoded message. Therefore, for any |from| that was obtained by
146          * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
147          * num < 2 * mdlen + 2 must hold for the modulus irrespective of
148          * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
149          * This does not leak any side-channel information.
150          */
151         if (num < flen || num < 2 * mdlen + 2)
152                 goto decoding_err;
153
154         dblen = num - mdlen - 1;
155         db = OPENSSL_malloc(dblen);
156         em = OPENSSL_malloc(num);
157         if (db == NULL || em == NULL)
158                 {
159                 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
160                 goto cleanup;
161                 }
162
163         /*
164          * Always do this zero-padding copy (even when num == flen) to avoid
165          * leaking that information. The copy still leaks some side-channel
166          * information, but it's impossible to have a fixed  memory access
167          * pattern since we can't read out of the bounds of |from|.
168          *
169          * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
170          */
171         memset(em, 0, num);
172         memcpy(em + num - flen, from, flen);
173
174         /*
175          * The first byte must be zero, however we must not leak if this is
176          * true. See James H. Manger, "A Chosen Ciphertext  Attack on RSA
177          * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
178          */
179         good = constant_time_is_zero(em[0]);
180
181         maskedseed = em + 1;
182         maskeddb = em + 1 + mdlen;
183
184         if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
185                 goto cleanup;
186         for (i = 0; i < mdlen; i++)
187                 seed[i] ^= maskedseed[i];
188
189         if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
190                 goto cleanup;
191         for (i = 0; i < dblen; i++)
192                 db[i] ^= maskeddb[i];
193
194         if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
195                 goto cleanup;
196
197         good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
198
199         found_one_byte = 0;
200         for (i = mdlen; i < dblen; i++)
201                 {
202                 /* Padding consists of a number of 0-bytes, followed by a 1. */
203                 unsigned int equals1 = constant_time_eq(db[i], 1);
204                 unsigned int equals0 = constant_time_is_zero(db[i]);
205                 one_index = constant_time_select_int(~found_one_byte & equals1,
206                         i, one_index);
207                 found_one_byte |= equals1;
208                 good &= (found_one_byte | equals0);
209                 }
210
211         good &= found_one_byte;
212
213         /*
214          * At this point |good| is zero unless the plaintext was valid,
215          * so plaintext-awareness ensures timing side-channels are no longer a
216          * concern.
217          */
218         if (!good)
219                 goto decoding_err;
220
221         msg_index = one_index + 1;
222         mlen = dblen - msg_index;
223
224         if (tlen < mlen)
225                 {
226                 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);
227                 mlen = -1;
228                 }
229         else
230                 {
231                 memcpy(to, db + msg_index, mlen);
232                 goto cleanup;
233                 }
234
235 decoding_err:
236         /* To avoid chosen ciphertext attacks, the error message should not reveal
237          * which kind of decoding error happened. */
238         RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_OAEP_DECODING_ERROR);
239 cleanup:
240         if (db != NULL) OPENSSL_free(db);
241         if (em != NULL) OPENSSL_free(em);
242         return mlen;
243         }
244
245 int PKCS1_MGF1(unsigned char *mask, long len,
246         const unsigned char *seed, long seedlen, const EVP_MD *dgst)
247         {
248         long i, outlen = 0;
249         unsigned char cnt[4];
250         EVP_MD_CTX c;
251         unsigned char md[EVP_MAX_MD_SIZE];
252         int mdlen;
253         int rv = -1;
254
255         EVP_MD_CTX_init(&c);
256         mdlen = M_EVP_MD_size(dgst);
257         if (mdlen < 0)
258                 goto err;
259         for (i = 0; outlen < len; i++)
260                 {
261                 cnt[0] = (unsigned char)((i >> 24) & 255);
262                 cnt[1] = (unsigned char)((i >> 16) & 255);
263                 cnt[2] = (unsigned char)((i >> 8)) & 255;
264                 cnt[3] = (unsigned char)(i & 255);
265                 if (!EVP_DigestInit_ex(&c,dgst, NULL)
266                         || !EVP_DigestUpdate(&c, seed, seedlen)
267                         || !EVP_DigestUpdate(&c, cnt, 4))
268                         goto err;
269                 if (outlen + mdlen <= len)
270                         {
271                         if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
272                                 goto err;
273                         outlen += mdlen;
274                         }
275                 else
276                         {
277                         if (!EVP_DigestFinal_ex(&c, md, NULL))
278                                 goto err;
279                         memcpy(mask + outlen, md, len - outlen);
280                         outlen = len;
281                         }
282                 }
283         rv = 0;
284         err:
285         EVP_MD_CTX_cleanup(&c);
286         return rv;
287         }
288
289 #endif