1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 /* ====================================================================
5 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com). */
56 #include <openssl/rsa.h>
62 #include <openssl/bn.h>
63 #include <openssl/digest.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
66 #include <openssl/rand.h>
67 #include <openssl/sha.h>
70 #include "../../internal.h"
73 #define RSA_PKCS1_PADDING_SIZE 11
75 int RSA_padding_add_PKCS1_type_1(uint8_t *to, size_t to_len,
76 const uint8_t *from, size_t from_len) {
77 // See RFC 8017, section 9.2.
78 if (to_len < RSA_PKCS1_PADDING_SIZE) {
79 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
83 if (from_len > to_len - RSA_PKCS1_PADDING_SIZE) {
84 OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
90 OPENSSL_memset(to + 2, 0xff, to_len - 3 - from_len);
91 to[to_len - from_len - 1] = 0;
92 OPENSSL_memcpy(to + to_len - from_len, from, from_len);
96 int RSA_padding_check_PKCS1_type_1(uint8_t *out, size_t *out_len,
97 size_t max_out, const uint8_t *from,
99 // See RFC 8017, section 9.2. This is part of signature verification and thus
100 // does not need to run in constant-time.
102 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL);
107 if (from[0] != 0 || from[1] != 1) {
108 OPENSSL_PUT_ERROR(RSA, RSA_R_BLOCK_TYPE_IS_NOT_01);
112 // Scan over padded data, looking for the 00.
114 for (pad = 2 /* header */; pad < from_len; pad++) {
115 if (from[pad] == 0x00) {
119 if (from[pad] != 0xff) {
120 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT);
125 if (pad == from_len) {
126 OPENSSL_PUT_ERROR(RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING);
130 if (pad < 2 /* header */ + 8) {
131 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_PAD_BYTE_COUNT);
138 if (from_len - pad > max_out) {
139 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
143 OPENSSL_memcpy(out, from + pad, from_len - pad);
144 *out_len = from_len - pad;
148 static int rand_nonzero(uint8_t *out, size_t len) {
149 if (!RAND_bytes(out, len)) {
153 for (size_t i = 0; i < len; i++) {
154 while (out[i] == 0) {
155 if (!RAND_bytes(out + i, 1)) {
164 int RSA_padding_add_PKCS1_type_2(uint8_t *to, size_t to_len,
165 const uint8_t *from, size_t from_len) {
166 // See RFC 8017, section 7.2.1.
167 if (to_len < RSA_PKCS1_PADDING_SIZE) {
168 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
172 if (from_len > to_len - RSA_PKCS1_PADDING_SIZE) {
173 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
180 size_t padding_len = to_len - 3 - from_len;
181 if (!rand_nonzero(to + 2, padding_len)) {
185 to[2 + padding_len] = 0;
186 OPENSSL_memcpy(to + to_len - from_len, from, from_len);
190 int RSA_padding_check_PKCS1_type_2(uint8_t *out, size_t *out_len,
191 size_t max_out, const uint8_t *from,
194 OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
198 // PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography
199 // Standard", section 7.2.2.
200 if (from_len < RSA_PKCS1_PADDING_SIZE) {
201 // |from| is zero-padded to the size of the RSA modulus, a public value, so
202 // this can be rejected in non-constant time.
203 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
207 crypto_word_t first_byte_is_zero = constant_time_eq_w(from[0], 0);
208 crypto_word_t second_byte_is_two = constant_time_eq_w(from[1], 2);
210 crypto_word_t zero_index = 0, looking_for_index = CONSTTIME_TRUE_W;
211 for (size_t i = 2; i < from_len; i++) {
212 crypto_word_t equals0 = constant_time_is_zero_w(from[i]);
214 constant_time_select_w(looking_for_index & equals0, i, zero_index);
215 looking_for_index = constant_time_select_w(equals0, 0, looking_for_index);
218 // The input must begin with 00 02.
219 crypto_word_t valid_index = first_byte_is_zero;
220 valid_index &= second_byte_is_two;
222 // We must have found the end of PS.
223 valid_index &= ~looking_for_index;
225 // PS must be at least 8 bytes long, and it starts two bytes into |from|.
226 valid_index &= constant_time_ge_w(zero_index, 2 + 8);
228 // Skip the zero byte.
231 // NOTE: Although this logic attempts to be constant time, the API contracts
232 // of this function and |RSA_decrypt| with |RSA_PKCS1_PADDING| make it
233 // impossible to completely avoid Bleichenbacher's attack. Consumers should
234 // use |RSA_PADDING_NONE| and perform the padding check in constant-time
235 // combined with a swap to a random session key or other mitigation.
237 OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR);
241 const size_t msg_len = from_len - zero_index;
242 if (msg_len > max_out) {
243 // This shouldn't happen because this function is always called with
244 // |max_out| as the key size and |from_len| is bounded by the key size.
245 OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR);
249 OPENSSL_memcpy(out, &from[zero_index], msg_len);
254 int RSA_padding_add_none(uint8_t *to, size_t to_len, const uint8_t *from,
256 if (from_len > to_len) {
257 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
261 if (from_len < to_len) {
262 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL);
266 OPENSSL_memcpy(to, from, from_len);
270 static int PKCS1_MGF1(uint8_t *out, size_t len, const uint8_t *seed,
271 size_t seed_len, const EVP_MD *md) {
274 EVP_MD_CTX_init(&ctx);
276 size_t md_len = EVP_MD_size(md);
278 for (uint32_t i = 0; len > 0; i++) {
280 counter[0] = (uint8_t)(i >> 24);
281 counter[1] = (uint8_t)(i >> 16);
282 counter[2] = (uint8_t)(i >> 8);
283 counter[3] = (uint8_t)i;
284 if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
285 !EVP_DigestUpdate(&ctx, seed, seed_len) ||
286 !EVP_DigestUpdate(&ctx, counter, sizeof(counter))) {
291 if (!EVP_DigestFinal_ex(&ctx, out, NULL)) {
297 uint8_t digest[EVP_MAX_MD_SIZE];
298 if (!EVP_DigestFinal_ex(&ctx, digest, NULL)) {
301 OPENSSL_memcpy(out, digest, len);
309 EVP_MD_CTX_cleanup(&ctx);
313 int RSA_padding_add_PKCS1_OAEP_mgf1(uint8_t *to, size_t to_len,
314 const uint8_t *from, size_t from_len,
315 const uint8_t *param, size_t param_len,
316 const EVP_MD *md, const EVP_MD *mgf1md) {
320 if (mgf1md == NULL) {
324 size_t mdlen = EVP_MD_size(md);
326 if (to_len < 2 * mdlen + 2) {
327 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
331 size_t emlen = to_len - 1;
332 if (from_len > emlen - 2 * mdlen - 1) {
333 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
337 if (emlen < 2 * mdlen + 1) {
338 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
343 uint8_t *seed = to + 1;
344 uint8_t *db = to + mdlen + 1;
346 if (!EVP_Digest(param, param_len, db, NULL, md, NULL)) {
349 OPENSSL_memset(db + mdlen, 0, emlen - from_len - 2 * mdlen - 1);
350 db[emlen - from_len - mdlen - 1] = 0x01;
351 OPENSSL_memcpy(db + emlen - from_len - mdlen, from, from_len);
352 if (!RAND_bytes(seed, mdlen)) {
356 uint8_t *dbmask = OPENSSL_malloc(emlen - mdlen);
357 if (dbmask == NULL) {
358 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
363 if (!PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md)) {
366 for (size_t i = 0; i < emlen - mdlen; i++) {
370 uint8_t seedmask[EVP_MAX_MD_SIZE];
371 if (!PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md)) {
374 for (size_t i = 0; i < mdlen; i++) {
375 seed[i] ^= seedmask[i];
380 OPENSSL_free(dbmask);
384 int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *out, size_t *out_len,
385 size_t max_out, const uint8_t *from,
386 size_t from_len, const uint8_t *param,
387 size_t param_len, const EVP_MD *md,
388 const EVP_MD *mgf1md) {
394 if (mgf1md == NULL) {
398 size_t mdlen = EVP_MD_size(md);
400 // The encoded message is one byte smaller than the modulus to ensure that it
401 // doesn't end up greater than the modulus. Thus there's an extra "+1" here
402 // compared to https://tools.ietf.org/html/rfc2437#section-9.1.1.2.
403 if (from_len < 1 + 2*mdlen + 1) {
404 // 'from_len' is the length of the modulus, i.e. does not depend on the
405 // particular ciphertext.
409 size_t dblen = from_len - mdlen - 1;
410 db = OPENSSL_malloc(dblen);
412 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
416 const uint8_t *maskedseed = from + 1;
417 const uint8_t *maskeddb = from + 1 + mdlen;
419 uint8_t seed[EVP_MAX_MD_SIZE];
420 if (!PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) {
423 for (size_t i = 0; i < mdlen; i++) {
424 seed[i] ^= maskedseed[i];
427 if (!PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) {
430 for (size_t i = 0; i < dblen; i++) {
431 db[i] ^= maskeddb[i];
434 uint8_t phash[EVP_MAX_MD_SIZE];
435 if (!EVP_Digest(param, param_len, phash, NULL, md, NULL)) {
439 crypto_word_t bad = ~constant_time_is_zero_w(CRYPTO_memcmp(db, phash, mdlen));
440 bad |= ~constant_time_is_zero_w(from[0]);
442 crypto_word_t looking_for_one_byte = CONSTTIME_TRUE_W;
443 size_t one_index = 0;
444 for (size_t i = mdlen; i < dblen; i++) {
445 crypto_word_t equals1 = constant_time_eq_w(db[i], 1);
446 crypto_word_t equals0 = constant_time_eq_w(db[i], 0);
448 constant_time_select_w(looking_for_one_byte & equals1, i, one_index);
449 looking_for_one_byte =
450 constant_time_select_w(equals1, 0, looking_for_one_byte);
451 bad |= looking_for_one_byte & ~equals0;
454 bad |= looking_for_one_byte;
461 size_t mlen = dblen - one_index;
462 if (max_out < mlen) {
463 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
467 OPENSSL_memcpy(out, db + one_index, mlen);
473 // to avoid chosen ciphertext attacks, the error message should not reveal
474 // which kind of decoding error happened
475 OPENSSL_PUT_ERROR(RSA, RSA_R_OAEP_DECODING_ERROR);
481 static const uint8_t kPSSZeroes[] = {0, 0, 0, 0, 0, 0, 0, 0};
483 int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const uint8_t *mHash,
484 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
485 const uint8_t *EM, int sLen) {
488 int maskedDBLen, MSBits, emLen;
493 uint8_t H_[EVP_MAX_MD_SIZE];
494 EVP_MD_CTX_init(&ctx);
496 if (mgf1Hash == NULL) {
500 hLen = EVP_MD_size(Hash);
502 // Negative sLen has special meanings:
504 // -2 salt length is autorecovered from signature
508 } else if (sLen == -2) {
510 } else if (sLen < -2) {
511 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
515 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
516 emLen = RSA_size(rsa);
517 if (EM[0] & (0xFF << MSBits)) {
518 OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID);
525 if (emLen < (int)hLen + 2 || emLen < ((int)hLen + sLen + 2)) {
526 // sLen can be small negative
527 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
530 if (EM[emLen - 1] != 0xbc) {
531 OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID);
534 maskedDBLen = emLen - hLen - 1;
535 H = EM + maskedDBLen;
536 DB = OPENSSL_malloc(maskedDBLen);
538 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
541 if (!PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash)) {
544 for (i = 0; i < maskedDBLen; i++) {
548 DB[0] &= 0xFF >> (8 - MSBits);
550 for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) {
553 if (DB[i++] != 0x1) {
554 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED);
557 if (sLen >= 0 && (maskedDBLen - i) != sLen) {
558 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
561 if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
562 !EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) ||
563 !EVP_DigestUpdate(&ctx, mHash, hLen) ||
564 !EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i) ||
565 !EVP_DigestFinal_ex(&ctx, H_, NULL)) {
568 if (OPENSSL_memcmp(H_, H, hLen)) {
569 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
577 EVP_MD_CTX_cleanup(&ctx);
582 int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
583 const unsigned char *mHash,
584 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
587 size_t maskedDBLen, MSBits, emLen;
589 unsigned char *H, *salt = NULL, *p;
591 if (mgf1Hash == NULL) {
595 hLen = EVP_MD_size(Hash);
597 if (BN_is_zero(rsa->n)) {
598 OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
602 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
603 emLen = RSA_size(rsa);
610 if (emLen < hLen + 2) {
611 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
615 // Negative sLenRequested has special meanings:
617 // -2 salt length is maximized
620 if (sLenRequested == -1) {
622 } else if (sLenRequested == -2) {
623 sLen = emLen - hLen - 2;
624 } else if (sLenRequested < 0) {
625 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
628 sLen = (size_t)sLenRequested;
631 if (emLen - hLen - 2 < sLen) {
632 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
637 salt = OPENSSL_malloc(sLen);
639 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
642 if (!RAND_bytes(salt, sLen)) {
646 maskedDBLen = emLen - hLen - 1;
647 H = EM + maskedDBLen;
650 EVP_MD_CTX_init(&ctx);
651 int digest_ok = EVP_DigestInit_ex(&ctx, Hash, NULL) &&
652 EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) &&
653 EVP_DigestUpdate(&ctx, mHash, hLen) &&
654 EVP_DigestUpdate(&ctx, salt, sLen) &&
655 EVP_DigestFinal_ex(&ctx, H, NULL);
656 EVP_MD_CTX_cleanup(&ctx);
661 // Generate dbMask in place then perform XOR on it
662 if (!PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) {
668 // Initial PS XORs with all zeroes which is a NOP so just update
669 // pointer. Note from a test above this value is guaranteed to
671 p += emLen - sLen - hLen - 2;
674 for (size_t i = 0; i < sLen; i++) {
679 EM[0] &= 0xFF >> (8 - MSBits);
682 // H is already in place so just set final 0xbc
684 EM[emLen - 1] = 0xbc;