1999-02-18 05:11:08 +08:00
|
|
|
/*
|
2024-09-05 15:35:49 +08:00
|
|
|
* Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-18 02:51:34 +08:00
|
|
|
*
|
2018-12-06 20:54:02 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-02-18 05:11:08 +08:00
|
|
|
*/
|
|
|
|
|
2000-12-05 18:30:21 +08:00
|
|
|
/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
|
|
|
|
* http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
|
|
|
|
* proof for the original OAEP scheme, which EME-OAEP is based on. A new
|
2001-01-24 22:59:25 +08:00
|
|
|
* proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
|
|
|
|
* "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
|
|
|
|
* http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
|
|
|
|
* for the underlying permutation: "partial-one-wayness" instead of
|
|
|
|
* one-wayness. For the RSA function, this is an equivalent notion.
|
2000-12-05 18:30:21 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-12 13:03:51 +08:00
|
|
|
/*
|
|
|
|
* RSA low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "internal/constant_time.h"
|
1999-02-18 05:11:08 +08:00
|
|
|
|
2015-01-28 01:34:45 +08:00
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2015-01-28 01:34:45 +08:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "rsa_local.h"
|
1999-02-18 05:11:08 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
|
2000-11-07 06:34:17 +08:00
|
|
|
const unsigned char *from, int flen,
|
|
|
|
const unsigned char *param, int plen)
|
1999-02-18 05:11:08 +08:00
|
|
|
{
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
return ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(NULL, to, tlen, from, flen,
|
|
|
|
param, plen, NULL, NULL);
|
2013-05-22 06:55:50 +08:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/*
|
2021-01-13 22:30:20 +08:00
|
|
|
* Perform the padding as per NIST 800-56B 7.2.2.3
|
2019-04-17 12:24:26 +08:00
|
|
|
* from (K) is the key material.
|
|
|
|
* param (A) is the additional input.
|
|
|
|
* Step numbers are included here but not in the constant time inverse below
|
|
|
|
* to avoid complicating an already difficult enough function.
|
|
|
|
*/
|
2020-10-15 17:55:50 +08:00
|
|
|
int ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(OSSL_LIB_CTX *libctx,
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
unsigned char *to, int tlen,
|
|
|
|
const unsigned char *from, int flen,
|
|
|
|
const unsigned char *param,
|
|
|
|
int plen, const EVP_MD *md,
|
|
|
|
const EVP_MD *mgf1md)
|
2013-05-22 06:55:50 +08:00
|
|
|
{
|
2018-09-03 09:39:50 +08:00
|
|
|
int rv = 0;
|
2001-09-06 17:30:16 +08:00
|
|
|
int i, emlen = tlen - 1;
|
|
|
|
unsigned char *db, *seed;
|
2018-09-03 09:39:50 +08:00
|
|
|
unsigned char *dbmask = NULL;
|
|
|
|
unsigned char seedmask[EVP_MAX_MD_SIZE];
|
|
|
|
int mdlen, dbmask_len = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-05-19 18:52:07 +08:00
|
|
|
if (md == NULL) {
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2013-05-22 06:55:50 +08:00
|
|
|
md = EVP_sha1();
|
2020-01-17 22:47:18 +08:00
|
|
|
#else
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
|
2020-01-17 22:47:18 +08:00
|
|
|
return 0;
|
|
|
|
#endif
|
2020-05-19 18:52:07 +08:00
|
|
|
}
|
2013-05-22 06:55:50 +08:00
|
|
|
if (mgf1md == NULL)
|
|
|
|
mgf1md = md;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2024-05-14 01:07:40 +08:00
|
|
|
#ifdef FIPS_MODULE
|
|
|
|
/* XOF are approved as standalone; Shake256 in Ed448; MGF */
|
2024-08-26 09:14:55 +08:00
|
|
|
if (EVP_MD_xof(md)) {
|
2024-05-14 01:07:40 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
|
|
|
|
return 0;
|
|
|
|
}
|
2024-08-26 09:14:55 +08:00
|
|
|
if (EVP_MD_xof(mgf1md)) {
|
2024-05-14 01:07:40 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
mdlen = EVP_MD_get_size(md);
|
2021-05-31 14:31:18 +08:00
|
|
|
if (mdlen <= 0) {
|
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 2b: check KLen > nLen - 2 HLen - 2 */
|
2013-05-22 06:55:50 +08:00
|
|
|
if (flen > emlen - 2 * mdlen - 1) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
|
2001-09-06 18:42:56 +08:00
|
|
|
return 0;
|
2001-09-06 17:30:16 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-05-22 06:55:50 +08:00
|
|
|
if (emlen < 2 * mdlen + 1) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
|
2001-09-06 18:42:56 +08:00
|
|
|
return 0;
|
2001-09-06 17:30:16 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3i: EM = 00000000 || maskedMGF || maskedDB */
|
2001-09-06 17:30:16 +08:00
|
|
|
to[0] = 0;
|
|
|
|
seed = to + 1;
|
2013-05-22 06:55:50 +08:00
|
|
|
db = to + mdlen + 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3a: hash the additional input */
|
2013-05-22 06:55:50 +08:00
|
|
|
if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
|
2018-09-03 09:39:50 +08:00
|
|
|
goto err;
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3b: zero bytes array of length nLen - KLen - 2 HLen -2 */
|
2013-05-22 06:55:50 +08:00
|
|
|
memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3c: DB = HA || PS || 00000001 || K */
|
2013-05-22 06:55:50 +08:00
|
|
|
db[emlen - flen - mdlen - 1] = 0x01;
|
|
|
|
memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3d: generate random byte string */
|
2021-05-28 12:46:40 +08:00
|
|
|
if (RAND_bytes_ex(libctx, seed, mdlen, 0) <= 0)
|
2018-09-03 09:39:50 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-09-03 09:39:50 +08:00
|
|
|
dbmask_len = emlen - mdlen;
|
|
|
|
dbmask = OPENSSL_malloc(dbmask_len);
|
2022-09-29 19:57:34 +08:00
|
|
|
if (dbmask == NULL)
|
2018-09-03 09:39:50 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3e: dbMask = MGF(mgfSeed, nLen - HLen - 1) */
|
2018-09-03 09:39:50 +08:00
|
|
|
if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
|
2016-12-23 21:35:16 +08:00
|
|
|
goto err;
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3f: maskedDB = DB XOR dbMask */
|
2018-09-03 09:39:50 +08:00
|
|
|
for (i = 0; i < dbmask_len; i++)
|
2001-09-06 17:30:16 +08:00
|
|
|
db[i] ^= dbmask[i];
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 3g: mgfSeed = MGF(maskedDB, HLen) */
|
2018-09-03 09:39:50 +08:00
|
|
|
if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
|
2016-12-23 21:35:16 +08:00
|
|
|
goto err;
|
2019-04-17 12:24:26 +08:00
|
|
|
/* stepo 3h: maskedMGFSeed = mgfSeed XOR mgfSeedMask */
|
2013-05-22 06:55:50 +08:00
|
|
|
for (i = 0; i < mdlen; i++)
|
2001-09-06 17:30:16 +08:00
|
|
|
seed[i] ^= seedmask[i];
|
2018-09-03 09:39:50 +08:00
|
|
|
rv = 1;
|
2016-12-23 21:35:16 +08:00
|
|
|
|
|
|
|
err:
|
2018-09-03 09:39:50 +08:00
|
|
|
OPENSSL_cleanse(seedmask, sizeof(seedmask));
|
|
|
|
OPENSSL_clear_free(dbmask, dbmask_len);
|
|
|
|
return rv;
|
2001-09-06 17:30:16 +08:00
|
|
|
}
|
1999-02-18 05:11:08 +08:00
|
|
|
|
2020-03-12 22:41:45 +08:00
|
|
|
int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
|
const unsigned char *from, int flen,
|
|
|
|
const unsigned char *param, int plen,
|
|
|
|
const EVP_MD *md, const EVP_MD *mgf1md)
|
|
|
|
{
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
return ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(NULL, to, tlen, from, flen,
|
|
|
|
param, plen, md, mgf1md);
|
2020-03-12 22:41:45 +08:00
|
|
|
}
|
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
|
2000-11-07 06:34:17 +08:00
|
|
|
const unsigned char *from, int flen, int num,
|
|
|
|
const unsigned char *param, int plen)
|
1999-02-18 05:11:08 +08:00
|
|
|
{
|
2013-05-22 06:55:50 +08:00
|
|
|
return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
|
|
|
|
param, plen, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
|
const unsigned char *from, int flen,
|
|
|
|
int num, const unsigned char *param,
|
|
|
|
int plen, const EVP_MD *md,
|
|
|
|
const EVP_MD *mgf1md)
|
|
|
|
{
|
2017-08-01 02:52:43 +08:00
|
|
|
int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
|
2018-09-07 03:54:23 +08:00
|
|
|
unsigned int good = 0, found_one_byte, mask;
|
2014-08-29 01:43:49 +08:00
|
|
|
const unsigned char *maskedseed, *maskeddb;
|
|
|
|
/*
|
|
|
|
* |em| is the encoded message, zero-padded to exactly |num| bytes: em =
|
|
|
|
* Y || maskedSeed || maskedDB
|
|
|
|
*/
|
|
|
|
unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
|
|
|
|
phash[EVP_MAX_MD_SIZE];
|
2013-05-22 06:55:50 +08:00
|
|
|
int mdlen;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-01-17 22:47:18 +08:00
|
|
|
if (md == NULL) {
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2013-05-22 06:55:50 +08:00
|
|
|
md = EVP_sha1();
|
2020-01-17 22:47:18 +08:00
|
|
|
#else
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
|
2020-01-17 22:47:18 +08:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-22 06:55:50 +08:00
|
|
|
if (mgf1md == NULL)
|
|
|
|
mgf1md = md;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2024-05-14 01:07:40 +08:00
|
|
|
#ifdef FIPS_MODULE
|
|
|
|
/* XOF are approved as standalone; Shake256 in Ed448; MGF */
|
2024-08-26 09:14:55 +08:00
|
|
|
if (EVP_MD_xof(md)) {
|
2024-05-14 01:07:40 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
|
|
|
|
return -1;
|
|
|
|
}
|
2024-08-26 09:14:55 +08:00
|
|
|
if (EVP_MD_xof(mgf1md)) {
|
2024-05-14 01:07:40 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
mdlen = EVP_MD_get_size(md);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2024-07-16 02:30:16 +08:00
|
|
|
if (tlen <= 0 || flen <= 0 || mdlen <= 0)
|
2014-08-29 01:43:49 +08:00
|
|
|
return -1;
|
|
|
|
/*
|
|
|
|
* |num| is the length of the modulus; |flen| is the length of the
|
|
|
|
* encoded message. Therefore, for any |from| that was obtained by
|
|
|
|
* decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
|
2019-02-28 17:08:18 +08:00
|
|
|
* |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of
|
2014-08-29 01:43:49 +08:00
|
|
|
* the ciphertext, see PKCS #1 v2.2, section 7.1.2.
|
|
|
|
* This does not leak any side-channel information.
|
|
|
|
*/
|
2018-09-07 03:54:23 +08:00
|
|
|
if (num < flen || num < 2 * mdlen + 2) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_OAEP_DECODING_ERROR);
|
2018-09-07 03:54:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2014-08-29 01:43:49 +08:00
|
|
|
dblen = num - mdlen - 1;
|
|
|
|
db = OPENSSL_malloc(dblen);
|
2022-09-29 19:57:34 +08:00
|
|
|
if (db == NULL)
|
2014-08-29 01:43:49 +08:00
|
|
|
goto cleanup;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-09-07 03:54:23 +08:00
|
|
|
em = OPENSSL_malloc(num);
|
2022-09-29 19:57:34 +08:00
|
|
|
if (em == NULL)
|
2018-09-07 03:54:23 +08:00
|
|
|
goto cleanup;
|
2018-02-04 22:24:54 +08:00
|
|
|
|
2018-09-07 03:54:23 +08:00
|
|
|
/*
|
|
|
|
* Caller is encouraged to pass zero-padded message created with
|
|
|
|
* BN_bn2binpad. Trouble is that since we can't read out of |from|'s
|
|
|
|
* bounds, it's impossible to have an invariant memory access pattern
|
|
|
|
* in case |from| was not zero-padded in advance.
|
|
|
|
*/
|
|
|
|
for (from += flen, em += num, i = 0; i < num; i++) {
|
|
|
|
mask = ~constant_time_is_zero(flen);
|
|
|
|
flen -= 1 & mask;
|
|
|
|
from -= 1 & mask;
|
|
|
|
*--em = *from & mask;
|
2018-02-04 22:24:54 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2014-08-29 01:43:49 +08:00
|
|
|
/*
|
|
|
|
* The first byte must be zero, however we must not leak if this is
|
|
|
|
* true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
|
|
|
|
* Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
|
|
|
|
*/
|
2019-02-28 17:08:18 +08:00
|
|
|
good = constant_time_is_zero(em[0]);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-02-28 17:08:18 +08:00
|
|
|
maskedseed = em + 1;
|
|
|
|
maskeddb = em + 1 + mdlen;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-05-22 06:55:50 +08:00
|
|
|
if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
|
2014-08-29 01:43:49 +08:00
|
|
|
goto cleanup;
|
2013-05-22 06:55:50 +08:00
|
|
|
for (i = 0; i < mdlen; i++)
|
2014-08-29 01:43:49 +08:00
|
|
|
seed[i] ^= maskedseed[i];
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-05-22 06:55:50 +08:00
|
|
|
if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
|
2014-08-29 01:43:49 +08:00
|
|
|
goto cleanup;
|
2001-09-06 17:30:16 +08:00
|
|
|
for (i = 0; i < dblen; i++)
|
|
|
|
db[i] ^= maskeddb[i];
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-05-22 06:55:50 +08:00
|
|
|
if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
|
2014-08-29 01:43:49 +08:00
|
|
|
goto cleanup;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2014-08-29 01:43:49 +08:00
|
|
|
good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2014-08-29 01:43:49 +08:00
|
|
|
found_one_byte = 0;
|
|
|
|
for (i = mdlen; i < dblen; i++) {
|
|
|
|
/*
|
|
|
|
* Padding consists of a number of 0-bytes, followed by a 1.
|
|
|
|
*/
|
|
|
|
unsigned int equals1 = constant_time_eq(db[i], 1);
|
|
|
|
unsigned int equals0 = constant_time_is_zero(db[i]);
|
|
|
|
one_index = constant_time_select_int(~found_one_byte & equals1,
|
|
|
|
i, one_index);
|
|
|
|
found_one_byte |= equals1;
|
|
|
|
good &= (found_one_byte | equals0);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2014-08-29 01:43:49 +08:00
|
|
|
good &= found_one_byte;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2014-08-29 01:43:49 +08:00
|
|
|
/*
|
|
|
|
* At this point |good| is zero unless the plaintext was valid,
|
|
|
|
* so plaintext-awareness ensures timing side-channels are no longer a
|
|
|
|
* concern.
|
|
|
|
*/
|
|
|
|
msg_index = one_index + 1;
|
|
|
|
mlen = dblen - msg_index;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-09-07 03:54:23 +08:00
|
|
|
/*
|
2019-02-28 17:08:18 +08:00
|
|
|
* For good measure, do this check in constant time as well.
|
2018-09-07 03:54:23 +08:00
|
|
|
*/
|
|
|
|
good &= constant_time_ge(tlen, mlen);
|
|
|
|
|
|
|
|
/*
|
2019-03-21 05:02:58 +08:00
|
|
|
* Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left.
|
|
|
|
* Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|.
|
|
|
|
* Otherwise leave |to| unchanged.
|
|
|
|
* Copy the memory back in a way that does not reveal the size of
|
|
|
|
* the data being copied via a timing side channel. This requires copying
|
|
|
|
* parts of the buffer multiple times based on the bits set in the real
|
|
|
|
* length. Clear bits do a non-copy with identical access pattern.
|
|
|
|
* The loop below has overall complexity of O(N*log(N)).
|
2018-09-07 03:54:23 +08:00
|
|
|
*/
|
2019-02-28 17:08:18 +08:00
|
|
|
tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
|
|
|
|
dblen - mdlen - 1, tlen);
|
2019-03-21 05:02:58 +08:00
|
|
|
for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) {
|
|
|
|
mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0);
|
|
|
|
for (i = mdlen + 1; i < dblen - msg_index; i++)
|
|
|
|
db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]);
|
|
|
|
}
|
|
|
|
for (i = 0; i < tlen; i++) {
|
|
|
|
mask = good & constant_time_lt(i, mlen);
|
|
|
|
to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]);
|
1999-02-18 05:11:08 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2014-08-29 01:43:49 +08:00
|
|
|
/*
|
|
|
|
* To avoid chosen ciphertext attacks, the error message should not
|
|
|
|
* reveal which kind of decoding error happened.
|
2020-01-17 22:47:18 +08:00
|
|
|
*
|
|
|
|
* This trick doesn't work in the FIPS provider because libcrypto manages
|
|
|
|
* the error stack. Instead we opt not to put an error on the stack at all
|
|
|
|
* in case of padding failure in the FIPS provider.
|
2014-08-29 01:43:49 +08:00
|
|
|
*/
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_OAEP_DECODING_ERROR);
|
2018-09-07 03:54:23 +08:00
|
|
|
err_clear_last_constant_time(1 & good);
|
2020-01-17 22:47:18 +08:00
|
|
|
#endif
|
2014-08-29 01:43:49 +08:00
|
|
|
cleanup:
|
2018-09-03 09:39:50 +08:00
|
|
|
OPENSSL_cleanse(seed, sizeof(seed));
|
2017-08-01 02:52:43 +08:00
|
|
|
OPENSSL_clear_free(db, dblen);
|
|
|
|
OPENSSL_clear_free(em, num);
|
2018-09-07 03:54:23 +08:00
|
|
|
|
|
|
|
return constant_time_select_int(good, mlen, -1);
|
2001-09-06 17:30:16 +08:00
|
|
|
}
|
1999-02-18 05:11:08 +08:00
|
|
|
|
2019-04-17 12:24:26 +08:00
|
|
|
/*
|
|
|
|
* Mask Generation Function corresponding to section 7.2.2.2 of NIST SP 800-56B.
|
|
|
|
* The variables are named differently to NIST:
|
|
|
|
* mask (T) and len (maskLen)are the returned mask.
|
|
|
|
* seed (mgfSeed).
|
|
|
|
* The range checking steps inm the process are performed outside.
|
|
|
|
*/
|
2005-05-29 04:44:02 +08:00
|
|
|
int PKCS1_MGF1(unsigned char *mask, long len,
|
|
|
|
const unsigned char *seed, long seedlen, const EVP_MD *dgst)
|
1999-02-18 05:11:08 +08:00
|
|
|
{
|
2001-09-06 17:30:16 +08:00
|
|
|
long i, outlen = 0;
|
|
|
|
unsigned char cnt[4];
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *c = EVP_MD_CTX_new();
|
2005-05-29 04:44:02 +08:00
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
|
|
int mdlen;
|
2009-09-24 07:43:49 +08:00
|
|
|
int rv = -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (c == NULL)
|
|
|
|
goto err;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
mdlen = EVP_MD_get_size(dgst);
|
2024-08-26 09:14:55 +08:00
|
|
|
if (mdlen <= 0)
|
2009-09-24 07:43:49 +08:00
|
|
|
goto err;
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 4 */
|
2001-09-06 17:30:16 +08:00
|
|
|
for (i = 0; outlen < len; i++) {
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 4a: D = I2BS(counter, 4) */
|
2001-09-06 17:30:16 +08:00
|
|
|
cnt[0] = (unsigned char)((i >> 24) & 255);
|
|
|
|
cnt[1] = (unsigned char)((i >> 16) & 255);
|
|
|
|
cnt[2] = (unsigned char)((i >> 8)) & 255;
|
|
|
|
cnt[3] = (unsigned char)(i & 255);
|
2019-04-17 12:24:26 +08:00
|
|
|
/* step 4b: T =T || hash(mgfSeed || D) */
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestInit_ex(c, dgst, NULL)
|
|
|
|
|| !EVP_DigestUpdate(c, seed, seedlen)
|
|
|
|
|| !EVP_DigestUpdate(c, cnt, 4))
|
2009-09-24 07:43:49 +08:00
|
|
|
goto err;
|
2005-05-29 04:44:02 +08:00
|
|
|
if (outlen + mdlen <= len) {
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
|
2009-09-24 07:43:49 +08:00
|
|
|
goto err;
|
2005-05-29 04:44:02 +08:00
|
|
|
outlen += mdlen;
|
2001-09-06 17:30:16 +08:00
|
|
|
} else {
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestFinal_ex(c, md, NULL))
|
2009-09-24 07:43:49 +08:00
|
|
|
goto err;
|
2001-09-06 17:30:16 +08:00
|
|
|
memcpy(mask + outlen, md, len - outlen);
|
|
|
|
outlen = len;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2001-09-06 17:30:16 +08:00
|
|
|
}
|
2009-09-24 07:43:49 +08:00
|
|
|
rv = 0;
|
|
|
|
err:
|
2018-09-03 09:39:50 +08:00
|
|
|
OPENSSL_cleanse(md, sizeof(md));
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(c);
|
2009-09-24 07:43:49 +08:00
|
|
|
return rv;
|
1999-02-18 05:11:08 +08:00
|
|
|
}
|