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