2016-04-02 21:12:58 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2016-04-02 21:12:58 +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
|
2016-04-02 21:12:58 +08:00
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2019-09-28 06:45:57 +08:00
|
|
|
#ifndef OSSL_CRYPTO_RSA_LOCAL_H
|
|
|
|
#define OSSL_CRYPTO_RSA_LOCAL_H
|
2018-07-05 07:28:51 +08:00
|
|
|
|
2016-08-27 22:01:08 +08:00
|
|
|
#include "internal/refcount.h"
|
RSA: Add a less loaded PSS-parameter structure
RSA_PSS_PARAMS carries with it a lot of baggage in form of X509_ALGOR
and ASN1_INTEGER, which we would rather avoid in our providers.
Therefore, we create a parallell structure - RSA_PSS_PARAMS_30 - that
contains the same information, but uses numeric identities (*) and C
integers (**). This makes it simpler to handle.
Note that neither this structure nor its contents are passed between
libcrypto and the providers. Instead, the numeric identities are
translated to and from names, which are then passed over that
boundary.
For future considerations, we might consider dropping RSA_PSS_PARAMS
entirely. For now, it's still reserved for EVP_PKEY_ASN1_METHOD code,
which RSA_PSS_PARAMS_30 is (almost entirely) reserved for use in our
providers.
(*) We use NIDs in this case, because we already have them and because
only algorithms that libcrypto knows about are permitted in PSS
restrictions. We could use any number series we want, as long as we
know for sure what they represent.
(**) That's for saltlen and for trailerfield, which are never expect
to surpass the set of numbers that fit in a regular 'int'.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11710)
2020-05-02 18:46:55 +08:00
|
|
|
#include "crypto/rsa.h"
|
2016-04-02 21:12:58 +08:00
|
|
|
|
2017-12-11 23:10:36 +08:00
|
|
|
#define RSA_MAX_PRIME_NUM 5
|
2017-08-02 02:19:43 +08:00
|
|
|
|
|
|
|
typedef struct rsa_prime_info_st {
|
|
|
|
BIGNUM *r;
|
|
|
|
BIGNUM *d;
|
|
|
|
BIGNUM *t;
|
|
|
|
/* save product of primes prior to this one */
|
|
|
|
BIGNUM *pp;
|
|
|
|
BN_MONT_CTX *m;
|
|
|
|
} RSA_PRIME_INFO;
|
|
|
|
|
|
|
|
DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
|
|
|
|
DEFINE_STACK_OF(RSA_PRIME_INFO)
|
|
|
|
|
2020-06-17 09:33:16 +08:00
|
|
|
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
|
|
|
|
struct rsa_acvp_test_st {
|
|
|
|
/* optional inputs */
|
|
|
|
BIGNUM *Xp1;
|
|
|
|
BIGNUM *Xp2;
|
|
|
|
BIGNUM *Xq1;
|
|
|
|
BIGNUM *Xq2;
|
|
|
|
BIGNUM *Xp;
|
|
|
|
BIGNUM *Xq;
|
|
|
|
|
|
|
|
/* optional outputs */
|
|
|
|
BIGNUM *p1;
|
|
|
|
BIGNUM *p2;
|
|
|
|
BIGNUM *q1;
|
|
|
|
BIGNUM *q2;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-04-02 21:12:58 +08:00
|
|
|
struct rsa_st {
|
|
|
|
/*
|
2020-03-12 13:26:34 +08:00
|
|
|
* #legacy
|
|
|
|
* The first field is used to pickup errors where this is passed
|
|
|
|
* instead of an EVP_PKEY. It is always zero.
|
|
|
|
* THIS MUST REMAIN THE FIRST FIELD.
|
2016-04-02 21:12:58 +08:00
|
|
|
*/
|
2020-03-12 13:26:34 +08:00
|
|
|
int dummy_zero;
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2017-04-05 19:24:14 +08:00
|
|
|
int32_t version;
|
2016-04-02 21:12:58 +08:00
|
|
|
const RSA_METHOD *meth;
|
|
|
|
/* functional reference if 'meth' is ENGINE-provided */
|
|
|
|
ENGINE *engine;
|
|
|
|
BIGNUM *n;
|
|
|
|
BIGNUM *e;
|
|
|
|
BIGNUM *d;
|
|
|
|
BIGNUM *p;
|
|
|
|
BIGNUM *q;
|
|
|
|
BIGNUM *dmp1;
|
|
|
|
BIGNUM *dmq1;
|
|
|
|
BIGNUM *iqmp;
|
RSA: Add a less loaded PSS-parameter structure
RSA_PSS_PARAMS carries with it a lot of baggage in form of X509_ALGOR
and ASN1_INTEGER, which we would rather avoid in our providers.
Therefore, we create a parallell structure - RSA_PSS_PARAMS_30 - that
contains the same information, but uses numeric identities (*) and C
integers (**). This makes it simpler to handle.
Note that neither this structure nor its contents are passed between
libcrypto and the providers. Instead, the numeric identities are
translated to and from names, which are then passed over that
boundary.
For future considerations, we might consider dropping RSA_PSS_PARAMS
entirely. For now, it's still reserved for EVP_PKEY_ASN1_METHOD code,
which RSA_PSS_PARAMS_30 is (almost entirely) reserved for use in our
providers.
(*) We use NIDs in this case, because we already have them and because
only algorithms that libcrypto knows about are permitted in PSS
restrictions. We could use any number series we want, as long as we
know for sure what they represent.
(**) That's for saltlen and for trailerfield, which are never expect
to surpass the set of numbers that fit in a regular 'int'.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11710)
2020-05-02 18:46:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If a PSS only key this contains the parameter restrictions.
|
|
|
|
* There are two structures for the same thing, used in different cases.
|
|
|
|
*/
|
|
|
|
/* This is used uniquely by OpenSSL provider implementations. */
|
|
|
|
RSA_PSS_PARAMS_30 pss_params;
|
2020-06-17 09:33:16 +08:00
|
|
|
|
|
|
|
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
|
|
|
|
RSA_ACVP_TEST *acvp_test;
|
RSA: Add a less loaded PSS-parameter structure
RSA_PSS_PARAMS carries with it a lot of baggage in form of X509_ALGOR
and ASN1_INTEGER, which we would rather avoid in our providers.
Therefore, we create a parallell structure - RSA_PSS_PARAMS_30 - that
contains the same information, but uses numeric identities (*) and C
integers (**). This makes it simpler to handle.
Note that neither this structure nor its contents are passed between
libcrypto and the providers. Instead, the numeric identities are
translated to and from names, which are then passed over that
boundary.
For future considerations, we might consider dropping RSA_PSS_PARAMS
entirely. For now, it's still reserved for EVP_PKEY_ASN1_METHOD code,
which RSA_PSS_PARAMS_30 is (almost entirely) reserved for use in our
providers.
(*) We use NIDs in this case, because we already have them and because
only algorithms that libcrypto knows about are permitted in PSS
restrictions. We could use any number series we want, as long as we
know for sure what they represent.
(**) That's for saltlen and for trailerfield, which are never expect
to surpass the set of numbers that fit in a regular 'int'.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11710)
2020-05-02 18:46:55 +08:00
|
|
|
#endif
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2020-06-17 09:33:16 +08:00
|
|
|
/* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */
|
|
|
|
RSA_PSS_PARAMS *pss;
|
2017-08-02 02:19:43 +08:00
|
|
|
/* for multi-prime RSA, defined in RFC 8017 */
|
|
|
|
STACK_OF(RSA_PRIME_INFO) *prime_infos;
|
2020-03-15 15:38:00 +08:00
|
|
|
/* Be careful using this if the RSA structure is shared */
|
2016-04-02 21:12:58 +08:00
|
|
|
CRYPTO_EX_DATA ex_data;
|
2020-01-14 09:32:42 +08:00
|
|
|
#endif
|
2016-08-27 22:01:08 +08:00
|
|
|
CRYPTO_REF_COUNT references;
|
2016-04-02 21:12:58 +08:00
|
|
|
int flags;
|
|
|
|
/* Used to cache montgomery values */
|
|
|
|
BN_MONT_CTX *_method_mod_n;
|
|
|
|
BN_MONT_CTX *_method_mod_p;
|
|
|
|
BN_MONT_CTX *_method_mod_q;
|
|
|
|
BN_BLINDING *blinding;
|
|
|
|
BN_BLINDING *mt_blinding;
|
|
|
|
CRYPTO_RWLOCK *lock;
|
2019-10-16 03:31:45 +08:00
|
|
|
|
|
|
|
int dirty_cnt;
|
2016-04-02 21:12:58 +08:00
|
|
|
};
|
|
|
|
|
2016-04-03 00:46:17 +08:00
|
|
|
struct rsa_meth_st {
|
|
|
|
char *name;
|
|
|
|
int (*rsa_pub_enc) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
int (*rsa_pub_dec) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
int (*rsa_priv_enc) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
int (*rsa_priv_dec) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
/* Can be null */
|
|
|
|
int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
|
|
|
|
/* Can be null */
|
|
|
|
int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
|
|
|
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
|
|
|
/* called at new */
|
|
|
|
int (*init) (RSA *rsa);
|
|
|
|
/* called at free */
|
|
|
|
int (*finish) (RSA *rsa);
|
|
|
|
/* RSA_METHOD_FLAG_* things */
|
|
|
|
int flags;
|
|
|
|
/* may be needed! */
|
|
|
|
char *app_data;
|
|
|
|
/*
|
|
|
|
* New sign and verify functions: some libraries don't allow arbitrary
|
|
|
|
* data to be signed/verified: this allows them to be used. Note: for
|
|
|
|
* this to work the RSA_public_decrypt() and RSA_private_encrypt() should
|
2021-03-13 07:35:24 +08:00
|
|
|
* *NOT* be used. RSA_sign(), RSA_verify() should be used instead.
|
2016-04-03 00:46:17 +08:00
|
|
|
*/
|
|
|
|
int (*rsa_sign) (int type,
|
|
|
|
const unsigned char *m, unsigned int m_length,
|
|
|
|
unsigned char *sigret, unsigned int *siglen,
|
|
|
|
const RSA *rsa);
|
|
|
|
int (*rsa_verify) (int dtype, const unsigned char *m,
|
|
|
|
unsigned int m_length, const unsigned char *sigbuf,
|
|
|
|
unsigned int siglen, const RSA *rsa);
|
|
|
|
/*
|
|
|
|
* If this callback is NULL, the builtin software RSA key-gen will be
|
|
|
|
* used. This is for behavioural compatibility whilst the code gets
|
|
|
|
* rewired, but one day it would be nice to assume there are no such
|
|
|
|
* things as "builtin software" implementations.
|
|
|
|
*/
|
|
|
|
int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
|
2017-08-02 02:19:43 +08:00
|
|
|
int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
|
|
|
|
BIGNUM *e, BN_GENCB *cb);
|
2016-04-03 00:46:17 +08:00
|
|
|
};
|
|
|
|
|
2016-12-02 05:46:31 +08:00
|
|
|
/* Macros to test if a pkey or ctx is for a PSS key */
|
|
|
|
#define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
|
|
|
|
#define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
|
2023-08-28 20:48:34 +08:00
|
|
|
int ossl_rsa_multiprime_derive(RSA *rsa, int bits, int primes,
|
|
|
|
BIGNUM *e_value,
|
|
|
|
STACK_OF(BIGNUM) *factors, STACK_OF(BIGNUM) *exps,
|
|
|
|
STACK_OF(BIGNUM) *coeffs);
|
2016-11-21 09:35:30 +08:00
|
|
|
|
2021-03-09 08:14:45 +08:00
|
|
|
RSA_PSS_PARAMS *ossl_rsa_pss_params_create(const EVP_MD *sigmd,
|
|
|
|
const EVP_MD *mgf1md, int saltlen);
|
|
|
|
int ossl_rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
|
|
|
|
const EVP_MD **pmgf1md, int *psaltlen);
|
2017-08-02 02:19:43 +08:00
|
|
|
/* internal function to clear and free multi-prime parameters */
|
2021-03-09 08:14:45 +08:00
|
|
|
void ossl_rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
|
|
|
|
void ossl_rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
|
|
|
|
RSA_PRIME_INFO *ossl_rsa_multip_info_new(void);
|
|
|
|
int ossl_rsa_multip_calc_product(RSA *rsa);
|
|
|
|
int ossl_rsa_multip_cap(int bits);
|
2018-07-05 07:28:51 +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
|
|
|
int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength);
|
|
|
|
int ossl_rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
|
|
|
|
int nbits);
|
|
|
|
int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
|
|
|
|
BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
|
|
|
|
BIGNUM *p1q1);
|
|
|
|
|
|
|
|
int ossl_rsa_check_public_exponent(const BIGNUM *e);
|
|
|
|
int ossl_rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx);
|
|
|
|
int ossl_rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx);
|
|
|
|
int ossl_rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx);
|
|
|
|
int ossl_rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx);
|
|
|
|
|
|
|
|
int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx);
|
|
|
|
int ossl_rsa_sp800_56b_check_public(const RSA *rsa);
|
|
|
|
int ossl_rsa_sp800_56b_check_private(const RSA *rsa);
|
|
|
|
int ossl_rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
|
|
|
|
int strength, int nbits);
|
|
|
|
int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
|
|
|
|
BN_GENCB *cb);
|
|
|
|
|
|
|
|
int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
|
|
|
|
const BIGNUM *e, BN_CTX *ctx);
|
|
|
|
int ossl_rsa_fips186_4_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test,
|
|
|
|
int nbits, const BIGNUM *e, BN_CTX *ctx,
|
|
|
|
BN_GENCB *cb);
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
int ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
|
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
|
|
|
int tlen, const unsigned char *from,
|
|
|
|
int flen);
|
2020-03-12 22:41:45 +08:00
|
|
|
|
2019-09-28 06:45:57 +08:00
|
|
|
#endif /* OSSL_CRYPTO_RSA_LOCAL_H */
|