1 /* crypto/rsa/rsa_oaep.c */
3 * Written by Ulf Moeller. This software is distributed on an "AS IS" basis,
4 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
7 /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
10 * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
11 * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
12 * proof for the original OAEP scheme, which EME-OAEP is based on. A new
13 * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
14 * "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
15 * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
16 * for the underlying permutation: "partial-one-wayness" instead of
17 * one-wayness. For the RSA function, this is an equivalent notion.
20 #include "constant_time_locl.h"
22 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
24 # include "cryptlib.h"
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 int RSA_padding_add_PKCS1_OAEP(unsigned char *to
, int tlen
,
32 const unsigned char *from
, int flen
,
33 const unsigned char *param
, int plen
)
35 return RSA_padding_add_PKCS1_OAEP_mgf1(to
, tlen
, from
, flen
,
36 param
, plen
, NULL
, NULL
);
39 int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to
, int tlen
,
40 const unsigned char *from
, int flen
,
41 const unsigned char *param
, int plen
,
42 const EVP_MD
*md
, const EVP_MD
*mgf1md
)
44 int i
, emlen
= tlen
- 1;
45 unsigned char *db
, *seed
;
46 unsigned char *dbmask
, seedmask
[EVP_MAX_MD_SIZE
];
54 mdlen
= EVP_MD_size(md
);
56 if (flen
> emlen
- 2 * mdlen
- 1) {
57 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1
,
58 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
62 if (emlen
< 2 * mdlen
+ 1) {
63 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1
,
64 RSA_R_KEY_SIZE_TOO_SMALL
);
72 if (!EVP_Digest((void *)param
, plen
, db
, NULL
, md
, NULL
))
74 memset(db
+ mdlen
, 0, emlen
- flen
- 2 * mdlen
- 1);
75 db
[emlen
- flen
- mdlen
- 1] = 0x01;
76 memcpy(db
+ emlen
- flen
- mdlen
, from
, (unsigned int)flen
);
77 if (RAND_bytes(seed
, mdlen
) <= 0)
81 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
85 dbmask
= OPENSSL_malloc(emlen
- mdlen
);
87 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1
, ERR_R_MALLOC_FAILURE
);
91 if (PKCS1_MGF1(dbmask
, emlen
- mdlen
, seed
, mdlen
, mgf1md
) < 0)
93 for (i
= 0; i
< emlen
- mdlen
; i
++)
96 if (PKCS1_MGF1(seedmask
, mdlen
, db
, emlen
- mdlen
, mgf1md
) < 0)
98 for (i
= 0; i
< mdlen
; i
++)
99 seed
[i
] ^= seedmask
[i
];
101 OPENSSL_free(dbmask
);
105 OPENSSL_free(dbmask
);
109 int RSA_padding_check_PKCS1_OAEP(unsigned char *to
, int tlen
,
110 const unsigned char *from
, int flen
, int num
,
111 const unsigned char *param
, int plen
)
113 return RSA_padding_check_PKCS1_OAEP_mgf1(to
, tlen
, from
, flen
, num
,
114 param
, plen
, NULL
, NULL
);
117 int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to
, int tlen
,
118 const unsigned char *from
, int flen
,
119 int num
, const unsigned char *param
,
120 int plen
, const EVP_MD
*md
,
121 const EVP_MD
*mgf1md
)
123 int i
, dblen
= 0, mlen
= -1, one_index
= 0, msg_index
;
124 unsigned int good
= 0, found_one_byte
, mask
;
125 const unsigned char *maskedseed
, *maskeddb
;
127 * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
128 * Y || maskedSeed || maskedDB
130 unsigned char *db
= NULL
, *em
= NULL
, seed
[EVP_MAX_MD_SIZE
],
131 phash
[EVP_MAX_MD_SIZE
];
139 mdlen
= EVP_MD_size(md
);
141 if (tlen
<= 0 || flen
<= 0)
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.
151 if (num
< flen
|| num
< 2 * mdlen
+ 2) {
152 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1
,
153 RSA_R_OAEP_DECODING_ERROR
);
157 dblen
= num
- mdlen
- 1;
158 db
= OPENSSL_malloc(dblen
);
160 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1
, ERR_R_MALLOC_FAILURE
);
164 em
= OPENSSL_malloc(num
);
166 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1
,
167 ERR_R_MALLOC_FAILURE
);
172 * Caller is encouraged to pass zero-padded message created with
173 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
174 * bounds, it's impossible to have an invariant memory access pattern
175 * in case |from| was not zero-padded in advance.
177 for (from
+= flen
, em
+= num
, i
= 0; i
< num
; i
++) {
178 mask
= ~constant_time_is_zero(flen
);
181 *--em
= *from
& mask
;
186 * The first byte must be zero, however we must not leak if this is
187 * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
188 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
190 good
= constant_time_is_zero(from
[0]);
192 maskedseed
= from
+ 1;
193 maskeddb
= from
+ 1 + mdlen
;
195 if (PKCS1_MGF1(seed
, mdlen
, maskeddb
, dblen
, mgf1md
))
197 for (i
= 0; i
< mdlen
; i
++)
198 seed
[i
] ^= maskedseed
[i
];
200 if (PKCS1_MGF1(db
, dblen
, seed
, mdlen
, mgf1md
))
202 for (i
= 0; i
< dblen
; i
++)
203 db
[i
] ^= maskeddb
[i
];
205 if (!EVP_Digest((void *)param
, plen
, phash
, NULL
, md
, NULL
))
208 good
&= constant_time_is_zero(CRYPTO_memcmp(db
, phash
, mdlen
));
211 for (i
= mdlen
; i
< dblen
; i
++) {
213 * Padding consists of a number of 0-bytes, followed by a 1.
215 unsigned int equals1
= constant_time_eq(db
[i
], 1);
216 unsigned int equals0
= constant_time_is_zero(db
[i
]);
217 one_index
= constant_time_select_int(~found_one_byte
& equals1
,
219 found_one_byte
|= equals1
;
220 good
&= (found_one_byte
| equals0
);
223 good
&= found_one_byte
;
226 * At this point |good| is zero unless the plaintext was valid,
227 * so plaintext-awareness ensures timing side-channels are no longer a
230 msg_index
= one_index
+ 1;
231 mlen
= dblen
- msg_index
;
234 * For good measure, do this check in constant tine as well.
236 good
&= constant_time_ge(tlen
, mlen
);
239 * Even though we can't fake result's length, we can pretend copying
240 * |tlen| bytes where |mlen| bytes would be real. Last |tlen| of |dblen|
241 * bytes are viewed as circular buffer with start at |tlen|-|mlen'|,
242 * where |mlen'| is "saturated" |mlen| value. Deducing information
243 * about failure or |mlen| would take attacker's ability to observe
244 * memory access pattern with byte granularity *as it occurs*. It
245 * should be noted that failure is indistinguishable from normal
246 * operation if |tlen| is fixed by protocol.
248 tlen
= constant_time_select_int(constant_time_lt(dblen
, tlen
), dblen
, tlen
);
249 msg_index
= constant_time_select_int(good
, msg_index
, dblen
- tlen
);
250 mlen
= dblen
- msg_index
;
251 for (from
= db
+ msg_index
, mask
= good
, i
= 0; i
< tlen
; i
++) {
252 unsigned int equals
= constant_time_eq(i
, mlen
);
254 from
-= dblen
& equals
; /* if (i == dblen) rewind */
255 mask
&= mask
^ equals
; /* if (i == dblen) mask = 0 */
256 to
[i
] = constant_time_select_8(mask
, from
[i
], to
[i
]);
260 * To avoid chosen ciphertext attacks, the error message should not
261 * reveal which kind of decoding error happened.
263 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1
,
264 RSA_R_OAEP_DECODING_ERROR
);
265 err_clear_last_constant_time(1 & good
);
267 OPENSSL_cleanse(seed
, sizeof(seed
));
268 OPENSSL_cleanse(db
, dblen
);
270 OPENSSL_cleanse(em
, num
);
273 return constant_time_select_int(good
, mlen
, -1);
276 int PKCS1_MGF1(unsigned char *mask
, long len
,
277 const unsigned char *seed
, long seedlen
, const EVP_MD
*dgst
)
280 unsigned char cnt
[4];
282 unsigned char md
[EVP_MAX_MD_SIZE
];
287 mdlen
= EVP_MD_size(dgst
);
290 for (i
= 0; outlen
< len
; i
++) {
291 cnt
[0] = (unsigned char)((i
>> 24) & 255);
292 cnt
[1] = (unsigned char)((i
>> 16) & 255);
293 cnt
[2] = (unsigned char)((i
>> 8)) & 255;
294 cnt
[3] = (unsigned char)(i
& 255);
295 if (!EVP_DigestInit_ex(&c
, dgst
, NULL
)
296 || !EVP_DigestUpdate(&c
, seed
, seedlen
)
297 || !EVP_DigestUpdate(&c
, cnt
, 4))
299 if (outlen
+ mdlen
<= len
) {
300 if (!EVP_DigestFinal_ex(&c
, mask
+ outlen
, NULL
))
304 if (!EVP_DigestFinal_ex(&c
, md
, NULL
))
306 memcpy(mask
+ outlen
, md
, len
- outlen
);
312 EVP_MD_CTX_cleanup(&c
);