]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_local.h
Update gost-engine submodule to match EVP_MAC renaming
[thirdparty/openssl.git] / crypto / rsa / rsa_local.h
CommitLineData
9862e9aa 1/*
33388b44 2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
9862e9aa 3 *
2a7b6f39 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
9862e9aa 7 * https://www.openssl.org/source/license.html
9862e9aa
RL
8 */
9
ae4186b0
DMSP
10#ifndef OSSL_CRYPTO_RSA_LOCAL_H
11#define OSSL_CRYPTO_RSA_LOCAL_H
8240d5fa 12
9862e9aa 13#include <openssl/rsa.h>
2f545ae4 14#include "internal/refcount.h"
15671090 15#include "crypto/rsa.h"
9862e9aa 16
f9085209
BE
17#define RSA_MAX_PRIME_NUM 5
18#define RSA_MIN_MODULUS_BITS 512
665d899f
PY
19
20typedef 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
29DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
30DEFINE_STACK_OF(RSA_PRIME_INFO)
31
9862e9aa
RL
32struct rsa_st {
33 /*
ca7f7b95
RL
34 * #legacy
35 * The first field is used to pickup errors where this is passed
36 * instead of an EVP_PKEY. It is always zero.
37 * THIS MUST REMAIN THE FIRST FIELD.
9862e9aa 38 */
ca7f7b95
RL
39 int dummy_zero;
40
41 OPENSSL_CTX *libctx;
6a32a3c0 42 int32_t version;
9862e9aa
RL
43 const RSA_METHOD *meth;
44 /* functional reference if 'meth' is ENGINE-provided */
45 ENGINE *engine;
46 BIGNUM *n;
47 BIGNUM *e;
48 BIGNUM *d;
49 BIGNUM *p;
50 BIGNUM *q;
51 BIGNUM *dmp1;
52 BIGNUM *dmq1;
53 BIGNUM *iqmp;
15671090
RL
54
55 /*
56 * If a PSS only key this contains the parameter restrictions.
57 * There are two structures for the same thing, used in different cases.
58 */
59 /* This is used uniquely by OpenSSL provider implementations. */
60 RSA_PSS_PARAMS_30 pss_params;
61#ifndef FIPS_MODULE
62 /* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */
d16d0b71 63 RSA_PSS_PARAMS *pss;
15671090
RL
64#endif
65
f844f9eb 66#ifndef FIPS_MODULE
665d899f
PY
67 /* for multi-prime RSA, defined in RFC 8017 */
68 STACK_OF(RSA_PRIME_INFO) *prime_infos;
d16d0b71 69 /* Be careful using this if the RSA structure is shared */
9862e9aa 70 CRYPTO_EX_DATA ex_data;
a3327784 71#endif
2f545ae4 72 CRYPTO_REF_COUNT references;
9862e9aa
RL
73 int flags;
74 /* Used to cache montgomery values */
75 BN_MONT_CTX *_method_mod_n;
76 BN_MONT_CTX *_method_mod_p;
77 BN_MONT_CTX *_method_mod_q;
78 /*
79 * all BIGNUM values are actually in the following data, if it is not
80 * NULL
81 */
82 char *bignum_data;
83 BN_BLINDING *blinding;
84 BN_BLINDING *mt_blinding;
85 CRYPTO_RWLOCK *lock;
29be6023
RL
86
87 int dirty_cnt;
9862e9aa
RL
88};
89
b72c9121
RL
90struct rsa_meth_st {
91 char *name;
92 int (*rsa_pub_enc) (int flen, const unsigned char *from,
93 unsigned char *to, RSA *rsa, int padding);
94 int (*rsa_pub_dec) (int flen, const unsigned char *from,
95 unsigned char *to, RSA *rsa, int padding);
96 int (*rsa_priv_enc) (int flen, const unsigned char *from,
97 unsigned char *to, RSA *rsa, int padding);
98 int (*rsa_priv_dec) (int flen, const unsigned char *from,
99 unsigned char *to, RSA *rsa, int padding);
100 /* Can be null */
101 int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
102 /* Can be null */
103 int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
104 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
105 /* called at new */
106 int (*init) (RSA *rsa);
107 /* called at free */
108 int (*finish) (RSA *rsa);
109 /* RSA_METHOD_FLAG_* things */
110 int flags;
111 /* may be needed! */
112 char *app_data;
113 /*
114 * New sign and verify functions: some libraries don't allow arbitrary
115 * data to be signed/verified: this allows them to be used. Note: for
116 * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
117 * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
118 */
119 int (*rsa_sign) (int type,
120 const unsigned char *m, unsigned int m_length,
121 unsigned char *sigret, unsigned int *siglen,
122 const RSA *rsa);
123 int (*rsa_verify) (int dtype, const unsigned char *m,
124 unsigned int m_length, const unsigned char *sigbuf,
125 unsigned int siglen, const RSA *rsa);
126 /*
127 * If this callback is NULL, the builtin software RSA key-gen will be
128 * used. This is for behavioural compatibility whilst the code gets
129 * rewired, but one day it would be nice to assume there are no such
130 * things as "builtin software" implementations.
131 */
132 int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
665d899f
PY
133 int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
134 BIGNUM *e, BN_GENCB *cb);
b72c9121
RL
135};
136
87ee7b22
DSH
137/* Macros to test if a pkey or ctx is for a PSS key */
138#define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
139#define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
47e42b3c
DSH
140
141RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
142 const EVP_MD *mgf1md, int saltlen);
cfd81c6d
DSH
143int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
144 const EVP_MD **pmgf1md, int *psaltlen);
665d899f
PY
145/* internal function to clear and free multi-prime parameters */
146void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
147void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
148RSA_PRIME_INFO *rsa_multip_info_new(void);
149int rsa_multip_calc_product(RSA *rsa);
0122add6 150int rsa_multip_cap(int bits);
8240d5fa 151
8240d5fa
SL
152int rsa_sp800_56b_validate_strength(int nbits, int strength);
153int rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
154 int nbits);
155int rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
156 BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
157 BIGNUM *p1q1);
158
159int rsa_check_public_exponent(const BIGNUM *e);
160int rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx);
161int rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx);
162int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx);
163int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx);
164
165int rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx);
166int rsa_sp800_56b_check_public(const RSA *rsa);
167int rsa_sp800_56b_check_private(const RSA *rsa);
168int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
169 int strength, int nbits);
170int rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
171 BN_GENCB *cb);
172
173int rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
174 const BIGNUM *e, BN_CTX *ctx);
175int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,
176 BIGNUM *Xpout, const BIGNUM *Xp,
177 const BIGNUM *Xp1, const BIGNUM *Xp2,
178 BIGNUM *q1, BIGNUM *q2, BIGNUM *Xqout,
179 const BIGNUM *Xq, const BIGNUM *Xq1,
180 const BIGNUM *Xq2, int nbits,
181 const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
182
0f2deef5
MC
183int rsa_padding_add_SSLv23_with_libctx(OPENSSL_CTX *libctx, unsigned char *to,
184 int tlen, const unsigned char *from,
185 int flen);
186int rsa_padding_add_PKCS1_type_2_with_libctx(OPENSSL_CTX *libctx,
187 unsigned char *to, int tlen,
188 const unsigned char *from,
189 int flen);
0f2deef5 190
ae4186b0 191#endif /* OSSL_CRYPTO_RSA_LOCAL_H */