]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_local.h
rsa: add ossl_ prefix to internal rsa_ calls.
[thirdparty/openssl.git] / crypto / rsa / rsa_local.h
1 /*
2 * Copyright 2006-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 #ifndef OSSL_CRYPTO_RSA_LOCAL_H
11 #define OSSL_CRYPTO_RSA_LOCAL_H
12
13 #include "crypto/rsa.h"
14 #include "internal/refcount.h"
15 #include "crypto/rsa.h"
16
17 #define RSA_MAX_PRIME_NUM 5
18 #define RSA_MIN_MODULUS_BITS 512
19
20 typedef struct rsa_prime_info_st {
21 BIGNUM *r;
22 BIGNUM *d;
23 BIGNUM *t;
24 /* save product of primes prior to this one */
25 BIGNUM *pp;
26 BN_MONT_CTX *m;
27 } RSA_PRIME_INFO;
28
29 DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
30 DEFINE_STACK_OF(RSA_PRIME_INFO)
31
32 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
33 struct rsa_acvp_test_st {
34 /* optional inputs */
35 BIGNUM *Xp1;
36 BIGNUM *Xp2;
37 BIGNUM *Xq1;
38 BIGNUM *Xq2;
39 BIGNUM *Xp;
40 BIGNUM *Xq;
41
42 /* optional outputs */
43 BIGNUM *p1;
44 BIGNUM *p2;
45 BIGNUM *q1;
46 BIGNUM *q2;
47 };
48 #endif
49
50 struct rsa_st {
51 /*
52 * #legacy
53 * The first field is used to pickup errors where this is passed
54 * instead of an EVP_PKEY. It is always zero.
55 * THIS MUST REMAIN THE FIRST FIELD.
56 */
57 int dummy_zero;
58
59 OPENSSL_CTX *libctx;
60 int32_t version;
61 const RSA_METHOD *meth;
62 /* functional reference if 'meth' is ENGINE-provided */
63 ENGINE *engine;
64 BIGNUM *n;
65 BIGNUM *e;
66 BIGNUM *d;
67 BIGNUM *p;
68 BIGNUM *q;
69 BIGNUM *dmp1;
70 BIGNUM *dmq1;
71 BIGNUM *iqmp;
72
73 /*
74 * If a PSS only key this contains the parameter restrictions.
75 * There are two structures for the same thing, used in different cases.
76 */
77 /* This is used uniquely by OpenSSL provider implementations. */
78 RSA_PSS_PARAMS_30 pss_params;
79
80 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
81 RSA_ACVP_TEST *acvp_test;
82 #endif
83
84 #ifndef FIPS_MODULE
85 /* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */
86 RSA_PSS_PARAMS *pss;
87 /* for multi-prime RSA, defined in RFC 8017 */
88 STACK_OF(RSA_PRIME_INFO) *prime_infos;
89 /* Be careful using this if the RSA structure is shared */
90 CRYPTO_EX_DATA ex_data;
91 #endif
92 CRYPTO_REF_COUNT references;
93 int flags;
94 /* Used to cache montgomery values */
95 BN_MONT_CTX *_method_mod_n;
96 BN_MONT_CTX *_method_mod_p;
97 BN_MONT_CTX *_method_mod_q;
98 /*
99 * all BIGNUM values are actually in the following data, if it is not
100 * NULL
101 */
102 char *bignum_data;
103 BN_BLINDING *blinding;
104 BN_BLINDING *mt_blinding;
105 CRYPTO_RWLOCK *lock;
106
107 int dirty_cnt;
108 };
109
110 struct rsa_meth_st {
111 char *name;
112 int (*rsa_pub_enc) (int flen, const unsigned char *from,
113 unsigned char *to, RSA *rsa, int padding);
114 int (*rsa_pub_dec) (int flen, const unsigned char *from,
115 unsigned char *to, RSA *rsa, int padding);
116 int (*rsa_priv_enc) (int flen, const unsigned char *from,
117 unsigned char *to, RSA *rsa, int padding);
118 int (*rsa_priv_dec) (int flen, const unsigned char *from,
119 unsigned char *to, RSA *rsa, int padding);
120 /* Can be null */
121 int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
122 /* Can be null */
123 int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
124 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
125 /* called at new */
126 int (*init) (RSA *rsa);
127 /* called at free */
128 int (*finish) (RSA *rsa);
129 /* RSA_METHOD_FLAG_* things */
130 int flags;
131 /* may be needed! */
132 char *app_data;
133 /*
134 * New sign and verify functions: some libraries don't allow arbitrary
135 * data to be signed/verified: this allows them to be used. Note: for
136 * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
137 * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
138 */
139 int (*rsa_sign) (int type,
140 const unsigned char *m, unsigned int m_length,
141 unsigned char *sigret, unsigned int *siglen,
142 const RSA *rsa);
143 int (*rsa_verify) (int dtype, const unsigned char *m,
144 unsigned int m_length, const unsigned char *sigbuf,
145 unsigned int siglen, const RSA *rsa);
146 /*
147 * If this callback is NULL, the builtin software RSA key-gen will be
148 * used. This is for behavioural compatibility whilst the code gets
149 * rewired, but one day it would be nice to assume there are no such
150 * things as "builtin software" implementations.
151 */
152 int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
153 int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
154 BIGNUM *e, BN_GENCB *cb);
155 };
156
157 /* Macros to test if a pkey or ctx is for a PSS key */
158 #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
159 #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
160
161 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
162 const EVP_MD *mgf1md, int saltlen);
163 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
164 const EVP_MD **pmgf1md, int *psaltlen);
165 /* internal function to clear and free multi-prime parameters */
166 void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
167 void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
168 RSA_PRIME_INFO *rsa_multip_info_new(void);
169 int rsa_multip_calc_product(RSA *rsa);
170 int rsa_multip_cap(int bits);
171
172 int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength);
173 int ossl_rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
174 int nbits);
175 int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
176 BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
177 BIGNUM *p1q1);
178
179 int ossl_rsa_check_public_exponent(const BIGNUM *e);
180 int ossl_rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx);
181 int ossl_rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx);
182 int ossl_rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx);
183 int ossl_rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx);
184
185 int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx);
186 int ossl_rsa_sp800_56b_check_public(const RSA *rsa);
187 int ossl_rsa_sp800_56b_check_private(const RSA *rsa);
188 int ossl_rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
189 int strength, int nbits);
190 int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
191 BN_GENCB *cb);
192
193 int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
194 const BIGNUM *e, BN_CTX *ctx);
195 int ossl_rsa_fips186_4_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test,
196 int nbits, const BIGNUM *e, BN_CTX *ctx,
197 BN_GENCB *cb);
198
199 int ossl_rsa_padding_add_SSLv23_ex(OPENSSL_CTX *libctx, unsigned char *to,
200 int tlen, const unsigned char *from,
201 int flen);
202 int ossl_rsa_padding_add_PKCS1_type_2_ex(OPENSSL_CTX *libctx, unsigned char *to,
203 int tlen, const unsigned char *from,
204 int flen);
205
206 #endif /* OSSL_CRYPTO_RSA_LOCAL_H */