Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/rsa/internal.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_RSA_INTERNAL_H
5 #define OPENSSL_HEADER_RSA_INTERNAL_H
6
7 #include <openssl/base.h>
8
9 #include <openssl/bn.h>
10 #include <openssl/rsa.h>
11
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
16
17 typedef struct bn_blinding_st BN_BLINDING;
18
19 struct rsa_meth_st {
20 void *app_data;
21
22 int (*init)(RSA *rsa);
23 int (*finish)(RSA *rsa);
24
25 // size returns the size of the RSA modulus in bytes.
26 size_t (*size)(const RSA *rsa);
27
28 // Set via |RSA_meth_set_sign|. The default behavior for |sign| is
29 // implemented in |RSA_sign|. If custom functionality is provided, |sign|
30 // will be invoked within |RSA_sign|.
31 int (*sign)(int type, const uint8_t *m, unsigned int m_length,
32 uint8_t *sigret, unsigned int *siglen, const RSA *rsa);
33
34 // Set via |RSA_meth_set_priv_enc|. |sign_raw| is equivalent to the
35 // |priv_enc| field of OpenSSL's |RSA_METHOD| struct. The default behavior
36 // for |sign_raw| is implemented in |RSA_sign_raw|. If custom
37 // functionality is provided, |sign_raw| will be invoked within
38 // |RSA_sign_raw|.
39 int (*sign_raw)(int max_out, const uint8_t *in, uint8_t *out, RSA *rsa,
40 int padding);
41
42 // Set via |RSA_meth_set_pub_dec|. |verify_raw| is equivalent to the
43 // |pub_dec| field of OpenSSL's |RSA_METHOD| struct. The default behavior
44 // for |verify_raw| is implemented in |RSA_verify_raw|. If custom
45 // functionality is provided, |verify_raw| will be invoked within
46 // |RSA_verify_raw|.
47 int (*verify_raw)(int max_out, const uint8_t *in, uint8_t *out, RSA *rsa,
48 int padding);
49
50 // Set via |RSA_meth_set_priv_dec|. |decrypt| is equivalent to the
51 // |priv_dec| field of OpenSSL's |RSA_METHOD| struct. The default behavior
52 // for |decrypt| is implemented in |RSA_decrypt|. If custom
53 // functionality is provided, |decrypt| will be invoked within
54 // |RSA_decrypt|.
55 int (*decrypt)(int max_out, const uint8_t *in, uint8_t *out, RSA *rsa,
56 int padding);
57
58 // Set via |RSA_meth_set_pub_enc|. |encrypt| is equivalent to the
59 // |pub_enc| field of OpenSSL's |RSA_METHOD| struct. The default behavior
60 // for |encrypt| is implemented in |RSA_encrypt|. If custom
61 // functionality is provided, |encrypt| will be invoked within
62 // |RSA_encrypt|.
63 int (*encrypt)(int max_out, const uint8_t *in, uint8_t *out, RSA *rsa,
64 int padding);
65
66 // private_transform takes a big-endian integer from |in|, calculates the
67 // d'th power of it, modulo the RSA modulus and writes the result as a
68 // big-endian integer to |out|. Both |in| and |out| are |len| bytes long and
69 // |len| is always equal to |RSA_size(rsa)|. If the result of the transform
70 // can be represented in fewer than |len| bytes, then |out| must be zero
71 // padded on the left.
72 //
73 // It returns one on success and zero otherwise.
74 //
75 // RSA decrypt and sign operations will call this, thus an ENGINE might wish
76 // to override it in order to avoid having to implement the padding
77 // functionality demanded by those, higher level, operations.
78 int (*private_transform)(RSA *rsa, uint8_t *out, const uint8_t *in,
79 size_t len);
80
81 int flags;
82 };
83
84 struct rsa_st {
85 const RSA_METHOD *meth;
86
87 BIGNUM *n;
88 BIGNUM *e;
89 BIGNUM *d;
90 BIGNUM *p;
91 BIGNUM *q;
92 BIGNUM *dmp1;
93 BIGNUM *dmq1;
94 BIGNUM *iqmp;
95
96 // If a PSS only key this contains the parameter restrictions.
97 RSASSA_PSS_PARAMS *pss;
98
99 // be careful using this if the RSA structure is shared
100 CRYPTO_EX_DATA ex_data;
101 CRYPTO_refcount_t references;
102 int flags;
103
104 CRYPTO_MUTEX lock;
105
106 // Used to cache montgomery values. The creation of these values is protected
107 // by |lock|.
108 BN_MONT_CTX *mont_n;
109 BN_MONT_CTX *mont_p;
110 BN_MONT_CTX *mont_q;
111
112 // The following fields are copies of |d|, |dmp1|, and |dmq1|, respectively,
113 // but with the correct widths to prevent side channels. These must use
114 // separate copies due to threading concerns caused by OpenSSL's API
115 // mistakes. See https://github.com/openssl/openssl/issues/5158 and
116 // the |freeze_private_key| implementation.
117 BIGNUM *d_fixed, *dmp1_fixed, *dmq1_fixed;
118
119 // iqmp_mont is q^-1 mod p in Montgomery form, using |mont_p|.
120 BIGNUM *iqmp_mont;
121
122 // num_blindings contains the size of the |blindings| and |blindings_inuse|
123 // arrays. This member and the |blindings_inuse| array are protected by
124 // |lock|.
125 size_t num_blindings;
126 // blindings is an array of BN_BLINDING structures that can be reserved by a
127 // thread by locking |lock| and changing the corresponding element in
128 // |blindings_inuse| from 0 to 1.
129 BN_BLINDING **blindings;
130 unsigned char *blindings_inuse;
131 uint64_t blinding_fork_generation;
132
133 // private_key_frozen is one if the key has been used for a private key
134 // operation and may no longer be mutated.
135 unsigned private_key_frozen:1;
136 };
137
138
139 #define RSA_PKCS1_PADDING_SIZE 11
140
141 // Default implementations of RSA operations.
142
143 size_t rsa_default_size(const RSA *rsa);
144 int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
145 size_t max_out, const uint8_t *in, size_t in_len,
146 int padding);
147 int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
148 size_t len);
149
150
151 BN_BLINDING *BN_BLINDING_new(void);
152 void BN_BLINDING_free(BN_BLINDING *b);
153 void BN_BLINDING_invalidate(BN_BLINDING *b);
154 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, const BIGNUM *e,
155 const BN_MONT_CTX *mont_ctx, BN_CTX *ctx);
156 int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_MONT_CTX *mont_ctx,
157 BN_CTX *ctx);
158
159 int RSA_padding_add_PKCS1_type_1(uint8_t *to, size_t to_len,
160 const uint8_t *from, size_t from_len);
161 int RSA_padding_check_PKCS1_type_1(uint8_t *out, size_t *out_len,
162 size_t max_out, const uint8_t *from,
163 size_t from_len);
164 int RSA_padding_add_none(uint8_t *to, size_t to_len, const uint8_t *from,
165 size_t from_len);
166
167 // rsa_private_transform_no_self_test calls either the method-specific
168 // |private_transform| function (if given) or the generic one. See the comment
169 // for |private_transform| in |rsa_meth_st|.
170 int rsa_private_transform_no_self_test(RSA *rsa, uint8_t *out,
171 const uint8_t *in, size_t len);
172
173 // rsa_private_transform acts the same as |rsa_private_transform_no_self_test|
174 // but, in FIPS mode, performs an RSA self test before calling the default RSA
175 // implementation.
176 int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
177 size_t len);
178
179 // rsa_invalidate_key is called after |rsa| has been mutated, to invalidate
180 // fields derived from the original structure. This function assumes exclusive
181 // access to |rsa|. In particular, no other thread may be concurrently signing,
182 // etc., with |rsa|.
183 void rsa_invalidate_key(RSA *rsa);
184
185
186 // This constant is exported for test purposes.
187 extern const BN_ULONG kBoringSSLRSASqrtTwo[];
188 extern const size_t kBoringSSLRSASqrtTwoLen;
189
190 // Functions that avoid self-tests.
191 //
192 // Self-tests need to call functions that don't try and ensure that the
193 // self-tests have passed. These functions, in turn, need to limit themselves
194 // to such functions too.
195 //
196 // These functions are the same as their public versions, but skip the self-test
197 // check.
198
199 int rsa_verify_no_self_test(int hash_nid, const uint8_t *digest,
200 size_t digest_len, const uint8_t *sig,
201 size_t sig_len, RSA *rsa);
202
203 int rsa_verify_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out,
204 size_t max_out, const uint8_t *in,
205 size_t in_len, int padding);
206
207 int rsa_sign_no_self_test(int hash_nid, const uint8_t *digest,
208 size_t digest_len, uint8_t *out, unsigned *out_len,
209 RSA *rsa);
210
211 // rsa_digestsign_no_self_test calculates the digest and calls
212 // |rsa_sign_no_self_test|, which doesn't try to run the self-test first. This
213 // is for use in the self tests themselves, to prevent an infinite loop.
214 int rsa_digestsign_no_self_test(const EVP_MD *md, const uint8_t *input,
215 size_t in_len, uint8_t *out, unsigned *out_len,
216 RSA *rsa);
217
218 // rsa_digestverify_no_self_test calculates the digest and calls
219 // |rsa_verify_no_self_test|, which doesn't try to run the self-test first. This
220 // is for use in the self tests themselves, to prevent an infinite loop.
221 int rsa_digestverify_no_self_test(const EVP_MD *md, const uint8_t *input,
222 size_t in_len, const uint8_t *sig,
223 size_t sig_len, RSA *rsa);
224
225 // Performs several checks on the public component of the given RSA key.
226 // See the implemetation in |rsa.c| for details.
227 int is_public_component_of_rsa_key_good(const RSA *key);
228
229 OPENSSL_EXPORT const RSASSA_PSS_PARAMS *RSA_get0_ssa_pss_params(const RSA *rsa);
230
231 #if defined(__cplusplus)
232 } // extern C
233 #endif
234
235 #endif // OPENSSL_HEADER_RSA_INTERNAL_H