]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_oaep.c
rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(): fix check of |md|
[thirdparty/openssl.git] / crypto / rsa / rsa_oaep.c
1 /*
2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 /*
24 * RSA low level APIs are deprecated for public use, but still ok for
25 * internal use.
26 */
27 #include "internal/deprecated.h"
28
29 #include "internal/constant_time.h"
30
31 #include <stdio.h>
32 #include "internal/cryptlib.h"
33 #include <openssl/bn.h>
34 #include <openssl/evp.h>
35 #include <openssl/rand.h>
36 #include <openssl/sha.h>
37 #include "rsa_local.h"
38
39 int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
40 const unsigned char *from, int flen,
41 const unsigned char *param, int plen)
42 {
43 return rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(NULL, to, tlen, from,
44 flen, param, plen, NULL,
45 NULL);
46 }
47
48 /*
49 * Perform ihe padding as per NIST 800-56B 7.2.2.3
50 * from (K) is the key material.
51 * param (A) is the additional input.
52 * Step numbers are included here but not in the constant time inverse below
53 * to avoid complicating an already difficult enough function.
54 */
55 int rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(OPENSSL_CTX *libctx,
56 unsigned char *to, int tlen,
57 const unsigned char *from,
58 int flen,
59 const unsigned char *param,
60 int plen, const EVP_MD *md,
61 const EVP_MD *mgf1md)
62 {
63 int rv = 0;
64 int i, emlen = tlen - 1;
65 unsigned char *db, *seed;
66 unsigned char *dbmask = NULL;
67 unsigned char seedmask[EVP_MAX_MD_SIZE];
68 int mdlen, dbmask_len = 0;
69
70 if (md == NULL) {
71 #ifndef FIPS_MODULE
72 md = EVP_sha1();
73 #else
74 RSAerr(0, ERR_R_PASSED_NULL_PARAMETER);
75 return 0;
76 #endif
77 }
78 if (mgf1md == NULL)
79 mgf1md = md;
80
81 mdlen = EVP_MD_size(md);
82
83 /* step 2b: check KLen > nLen - 2 HLen - 2 */
84 if (flen > emlen - 2 * mdlen - 1) {
85 RSAerr(0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
86 return 0;
87 }
88
89 if (emlen < 2 * mdlen + 1) {
90 RSAerr(0, RSA_R_KEY_SIZE_TOO_SMALL);
91 return 0;
92 }
93
94 /* step 3i: EM = 00000000 || maskedMGF || maskedDB */
95 to[0] = 0;
96 seed = to + 1;
97 db = to + mdlen + 1;
98
99 /* step 3a: hash the additional input */
100 if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
101 goto err;
102 /* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */
103 memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
104 /* step 3c: DB = HA || PS || 00000001 || K */
105 db[emlen - flen - mdlen - 1] = 0x01;
106 memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
107 /* step 3d: generate random byte string */
108 if (RAND_bytes_ex(libctx, seed, mdlen) <= 0)
109 goto err;
110
111 dbmask_len = emlen - mdlen;
112 dbmask = OPENSSL_malloc(dbmask_len);
113 if (dbmask == NULL) {
114 RSAerr(0, ERR_R_MALLOC_FAILURE);
115 goto err;
116 }
117
118 /* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */
119 if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
120 goto err;
121 /* step 3f: maskedDB = DB XOR dbMask */
122 for (i = 0; i < dbmask_len; i++)
123 db[i] ^= dbmask[i];
124
125 /* step 3g: mgfSeed = MGF(maskedDB, HLen) */
126 if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
127 goto err;
128 /* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */
129 for (i = 0; i < mdlen; i++)
130 seed[i] ^= seedmask[i];
131 rv = 1;
132
133 err:
134 OPENSSL_cleanse(seedmask, sizeof(seedmask));
135 OPENSSL_clear_free(dbmask, dbmask_len);
136 return rv;
137 }
138
139 int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
140 const unsigned char *from, int flen,
141 const unsigned char *param, int plen,
142 const EVP_MD *md, const EVP_MD *mgf1md)
143 {
144 return rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(NULL, to, tlen, from,
145 flen, param, plen, md,
146 mgf1md);
147 }
148
149 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
150 const unsigned char *from, int flen, int num,
151 const unsigned char *param, int plen)
152 {
153 return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
154 param, plen, NULL, NULL);
155 }
156
157 int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
158 const unsigned char *from, int flen,
159 int num, const unsigned char *param,
160 int plen, const EVP_MD *md,
161 const EVP_MD *mgf1md)
162 {
163 int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
164 unsigned int good = 0, found_one_byte, mask;
165 const unsigned char *maskedseed, *maskeddb;
166 /*
167 * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
168 * Y || maskedSeed || maskedDB
169 */
170 unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
171 phash[EVP_MAX_MD_SIZE];
172 int mdlen;
173
174 if (md == NULL) {
175 #ifndef FIPS_MODULE
176 md = EVP_sha1();
177 #else
178 RSAerr(0, ERR_R_PASSED_NULL_PARAMETER);
179 return -1;
180 #endif
181 }
182
183 if (mgf1md == NULL)
184 mgf1md = md;
185
186 mdlen = EVP_MD_size(md);
187
188 if (tlen <= 0 || flen <= 0)
189 return -1;
190 /*
191 * |num| is the length of the modulus; |flen| is the length of the
192 * encoded message. Therefore, for any |from| that was obtained by
193 * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
194 * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of
195 * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
196 * This does not leak any side-channel information.
197 */
198 if (num < flen || num < 2 * mdlen + 2) {
199 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
200 RSA_R_OAEP_DECODING_ERROR);
201 return -1;
202 }
203
204 dblen = num - mdlen - 1;
205 db = OPENSSL_malloc(dblen);
206 if (db == NULL) {
207 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
208 goto cleanup;
209 }
210
211 em = OPENSSL_malloc(num);
212 if (em == NULL) {
213 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
214 ERR_R_MALLOC_FAILURE);
215 goto cleanup;
216 }
217
218 /*
219 * Caller is encouraged to pass zero-padded message created with
220 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
221 * bounds, it's impossible to have an invariant memory access pattern
222 * in case |from| was not zero-padded in advance.
223 */
224 for (from += flen, em += num, i = 0; i < num; i++) {
225 mask = ~constant_time_is_zero(flen);
226 flen -= 1 & mask;
227 from -= 1 & mask;
228 *--em = *from & mask;
229 }
230
231 /*
232 * The first byte must be zero, however we must not leak if this is
233 * true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
234 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
235 */
236 good = constant_time_is_zero(em[0]);
237
238 maskedseed = em + 1;
239 maskeddb = em + 1 + mdlen;
240
241 if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
242 goto cleanup;
243 for (i = 0; i < mdlen; i++)
244 seed[i] ^= maskedseed[i];
245
246 if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
247 goto cleanup;
248 for (i = 0; i < dblen; i++)
249 db[i] ^= maskeddb[i];
250
251 if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
252 goto cleanup;
253
254 good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
255
256 found_one_byte = 0;
257 for (i = mdlen; i < dblen; i++) {
258 /*
259 * Padding consists of a number of 0-bytes, followed by a 1.
260 */
261 unsigned int equals1 = constant_time_eq(db[i], 1);
262 unsigned int equals0 = constant_time_is_zero(db[i]);
263 one_index = constant_time_select_int(~found_one_byte & equals1,
264 i, one_index);
265 found_one_byte |= equals1;
266 good &= (found_one_byte | equals0);
267 }
268
269 good &= found_one_byte;
270
271 /*
272 * At this point |good| is zero unless the plaintext was valid,
273 * so plaintext-awareness ensures timing side-channels are no longer a
274 * concern.
275 */
276 msg_index = one_index + 1;
277 mlen = dblen - msg_index;
278
279 /*
280 * For good measure, do this check in constant time as well.
281 */
282 good &= constant_time_ge(tlen, mlen);
283
284 /*
285 * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left.
286 * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|.
287 * Otherwise leave |to| unchanged.
288 * Copy the memory back in a way that does not reveal the size of
289 * the data being copied via a timing side channel. This requires copying
290 * parts of the buffer multiple times based on the bits set in the real
291 * length. Clear bits do a non-copy with identical access pattern.
292 * The loop below has overall complexity of O(N*log(N)).
293 */
294 tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
295 dblen - mdlen - 1, tlen);
296 for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) {
297 mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0);
298 for (i = mdlen + 1; i < dblen - msg_index; i++)
299 db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]);
300 }
301 for (i = 0; i < tlen; i++) {
302 mask = good & constant_time_lt(i, mlen);
303 to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]);
304 }
305
306 #ifndef FIPS_MODULE
307 /*
308 * To avoid chosen ciphertext attacks, the error message should not
309 * reveal which kind of decoding error happened.
310 *
311 * This trick doesn't work in the FIPS provider because libcrypto manages
312 * the error stack. Instead we opt not to put an error on the stack at all
313 * in case of padding failure in the FIPS provider.
314 */
315 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
316 RSA_R_OAEP_DECODING_ERROR);
317 err_clear_last_constant_time(1 & good);
318 #endif
319 cleanup:
320 OPENSSL_cleanse(seed, sizeof(seed));
321 OPENSSL_clear_free(db, dblen);
322 OPENSSL_clear_free(em, num);
323
324 return constant_time_select_int(good, mlen, -1);
325 }
326
327 /*
328 * Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B.
329 * The variables are named differently to NIST:
330 * mask (T) and len (maskLen)are the returned mask.
331 * seed (mgfSeed).
332 * The range checking steps inm the process are performed outside.
333 */
334 int PKCS1_MGF1(unsigned char *mask, long len,
335 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
336 {
337 long i, outlen = 0;
338 unsigned char cnt[4];
339 EVP_MD_CTX *c = EVP_MD_CTX_new();
340 unsigned char md[EVP_MAX_MD_SIZE];
341 int mdlen;
342 int rv = -1;
343
344 if (c == NULL)
345 goto err;
346 mdlen = EVP_MD_size(dgst);
347 if (mdlen < 0)
348 goto err;
349 /* step 4 */
350 for (i = 0; outlen < len; i++) {
351 /* step 4a: D = I2BS(counter, 4) */
352 cnt[0] = (unsigned char)((i >> 24) & 255);
353 cnt[1] = (unsigned char)((i >> 16) & 255);
354 cnt[2] = (unsigned char)((i >> 8)) & 255;
355 cnt[3] = (unsigned char)(i & 255);
356 /* step 4b: T =T || hash(mgfSeed || D) */
357 if (!EVP_DigestInit_ex(c, dgst, NULL)
358 || !EVP_DigestUpdate(c, seed, seedlen)
359 || !EVP_DigestUpdate(c, cnt, 4))
360 goto err;
361 if (outlen + mdlen <= len) {
362 if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
363 goto err;
364 outlen += mdlen;
365 } else {
366 if (!EVP_DigestFinal_ex(c, md, NULL))
367 goto err;
368 memcpy(mask + outlen, md, len - outlen);
369 outlen = len;
370 }
371 }
372 rv = 0;
373 err:
374 OPENSSL_cleanse(md, sizeof(md));
375 EVP_MD_CTX_free(c);
376 return rv;
377 }