Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/evp/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_EVP_INTERNAL_H
5 #define OPENSSL_HEADER_EVP_INTERNAL_H
6
7 #include <openssl/base.h>
8
9 #include <openssl/rsa.h>
10 #include <openssl/hmac.h>
11 #include <openssl/evp.h>
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
16
17 // EVP_MD_CTX_FLAG_KEEP_PKEY_CTX ensures |md_ctx->pctx| is not freed up in
18 // |EVP_MD_CTX_cleanup|. Only intended for internal use when |*pctx| was set
19 // externally with |EVP_MD_CTX_set_pkey_ctx|.
20 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
21
22 // EVP_MD_CTX_HMAC causes the |EVP_MD|'s |init| function not to
23 // be called, the |update| member not to be copied from the |EVP_MD| in
24 // |EVP_DigestInit_ex| and for |md_data| not to be initialised.
25 // This is an implementation detail of |EVP_PKEY_HMAC|.
26 #define EVP_MD_CTX_HMAC 0x0800
27
28 typedef struct evp_pkey_method_st EVP_PKEY_METHOD;
29
30 struct evp_pkey_asn1_method_st {
31 int pkey_id;
32 uint8_t oid[11];
33 uint8_t oid_len;
34
35 const char *pem_str;
36 const char *info;
37
38 // pub_decode decodes |params| and |key| as a SubjectPublicKeyInfo
39 // and writes the result into |out|. It returns one on success and zero on
40 // error. |params| is the AlgorithmIdentifier after the OBJECT IDENTIFIER
41 // type field, and |key| is the contents of the subjectPublicKey with the
42 // leading padding byte checked and removed. Although X.509 uses BIT STRINGs
43 // to represent SubjectPublicKeyInfo, every key type defined encodes the key
44 // as a byte string with the same conversion to BIT STRING.
45 int (*pub_decode)(EVP_PKEY *out, CBS *oid, CBS *params, CBS *key);
46
47 // pub_encode encodes |key| as a SubjectPublicKeyInfo and appends the result
48 // to |out|. It returns one on success and zero on error.
49 int (*pub_encode)(CBB *out, const EVP_PKEY *key);
50
51 int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
52
53 // priv_decode decodes |params| and |key| as a PrivateKeyInfo and writes the
54 // result into |out|. It returns one on success and zero on error. |params| is
55 // the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, and |key|
56 // is the contents of the OCTET STRING privateKey field.
57 int (*priv_decode)(EVP_PKEY *out, CBS *oid, CBS *params, CBS *key, CBS *pubkey);
58
59 // priv_encode encodes |key| as a PrivateKeyInfo and appends the result to
60 // |out|. It returns one on success and zero on error.
61 int (*priv_encode)(CBB *out, const EVP_PKEY *key);
62
63 // priv_encode_v2 encodes |key| as a OneAsymmetricKey (RFC 5958) and appends
64 // the result to |out|. It returns one on success and zero on error.
65 int (*priv_encode_v2)(CBB *out, const EVP_PKEY *key);
66
67 int (*set_priv_raw)(EVP_PKEY *pkey, const uint8_t *privkey, size_t privkey_len, const uint8_t *pubkey, size_t pubkey_len);
68 int (*set_pub_raw)(EVP_PKEY *pkey, const uint8_t *in, size_t len);
69 int (*get_priv_raw)(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len);
70 int (*get_pub_raw)(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len);
71 int (*get_priv_seed)(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len);
72
73 // pkey_opaque returns 1 if the |pk| is opaque. Opaque keys are backed by
74 // custom implementations which do not expose key material and parameters.
75 int (*pkey_opaque)(const EVP_PKEY *pk);
76
77 int (*pkey_size)(const EVP_PKEY *pk);
78 int (*pkey_bits)(const EVP_PKEY *pk);
79
80 int (*param_missing)(const EVP_PKEY *pk);
81 int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
82 int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
83
84 void (*pkey_free)(EVP_PKEY *pkey);
85 }; // EVP_PKEY_ASN1_METHOD
86
87 struct evp_pkey_st {
88 CRYPTO_refcount_t references;
89
90 // type contains one of the EVP_PKEY_* values or NID_undef and determines
91 // which element (if any) of the |pkey| union is valid.
92 int type;
93
94 union {
95 void *ptr;
96 RSA *rsa;
97 DSA *dsa;
98 DH *dh;
99 EC_KEY *ec;
100 KEM_KEY *kem_key;
101 PQDSA_KEY *pqdsa_key;
102 } pkey;
103
104 // ameth contains a pointer to a method table that contains many ASN.1
105 // methods for the key type.
106 const EVP_PKEY_ASN1_METHOD *ameth;
107 } /* EVP_PKEY */;
108
109 #define EVP_PKEY_OP_UNDEFINED 0
110 #define EVP_PKEY_OP_KEYGEN (1 << 2)
111 #define EVP_PKEY_OP_SIGN (1 << 3)
112 #define EVP_PKEY_OP_VERIFY (1 << 4)
113 #define EVP_PKEY_OP_VERIFYRECOVER (1 << 5)
114 #define EVP_PKEY_OP_ENCRYPT (1 << 6)
115 #define EVP_PKEY_OP_DECRYPT (1 << 7)
116 #define EVP_PKEY_OP_DERIVE (1 << 8)
117 #define EVP_PKEY_OP_PARAMGEN (1 << 9)
118
119 #define EVP_PKEY_OP_TYPE_SIG \
120 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYRECOVER)
121
122 #define EVP_PKEY_OP_TYPE_CRYPT (EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT)
123
124 #define EVP_PKEY_OP_TYPE_NOGEN \
125 (EVP_PKEY_OP_SIG | EVP_PKEY_OP_CRYPT | EVP_PKEY_OP_DERIVE)
126
127 #define EVP_PKEY_OP_TYPE_GEN (EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_PARAMGEN)
128
129 // EVP_PKEY_CTX_ctrl performs |cmd| on |ctx|. The |keytype| and |optype|
130 // arguments can be -1 to specify that any type and operation are acceptable,
131 // otherwise |keytype| must match the type of |ctx| and the bits of |optype|
132 // must intersect the operation flags set on |ctx|.
133 //
134 // The |p1| and |p2| arguments depend on the value of |cmd|.
135 //
136 // It returns one on success and zero on error.
137 OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
138 int cmd, int p1, void *p2);
139
140 // EVP_PKEY_CTX_md sets the message digest type for a specific operation.
141 // This function is deprecated and should not be used in new code.
142 //
143 // |ctx| is the context to operate on.
144 // |optype| is the operation type (e.g., EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_OP_KEYGEN).
145 // |cmd| is the specific command (e.g., EVP_PKEY_CTRL_MD).
146 // |md| is the name of the message digest algorithm to use.
147 //
148 // It returns 1 for success and 0 or a negative value for failure.
149 OPENSSL_EXPORT int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md);
150
151 // EVP_RSA_PKEY_CTX_ctrl is a wrapper of |EVP_PKEY_CTX_ctrl|.
152 // Before calling |EVP_PKEY_CTX_ctrl|, a check is added to make sure
153 // the |ctx->pmeth->pkey_id| is either |EVP_PKEY_RSA| or |EVP_PKEY_RSA_PSS|.
154 int EVP_RSA_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2);
155
156 #define EVP_PKEY_CTRL_MD 1
157 #define EVP_PKEY_CTRL_GET_MD 2
158 #define EVP_PKEY_CTRL_SIGNING_CONTEXT 3
159 #define EVP_PKEY_CTRL_GET_SIGNING_CONTEXT 4
160
161 // EVP_PKEY_CTRL_PEER_KEY is called with different values of |p1|:
162 // 0: Is called from |EVP_PKEY_derive_set_peer| and |p2| contains a peer key.
163 // If the return value is <= 0, the key is rejected.
164 // 1: Is called at the end of |EVP_PKEY_derive_set_peer| and |p2| contains a
165 // peer key. If the return value is <= 0, the key is rejected.
166 // 2: Is called with |p2| == NULL to test whether the peer's key was used.
167 // (EC)DH always return one in this case.
168 // 3: Is called with |p2| == NULL to set whether the peer's key was used.
169 // (EC)DH always return one in this case. This was only used for GOST.
170 #define EVP_PKEY_CTRL_PEER_KEY 3
171
172 // EVP_PKEY_ALG_CTRL is the base value from which key-type specific ctrl
173 // commands are numbered.
174 #define EVP_PKEY_ALG_CTRL 0x1000
175
176 #define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1)
177 #define EVP_PKEY_CTRL_GET_RSA_PADDING (EVP_PKEY_ALG_CTRL + 2)
178 #define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 3)
179 #define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 4)
180 #define EVP_PKEY_CTRL_RSA_KEYGEN_BITS (EVP_PKEY_ALG_CTRL + 5)
181 #define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP (EVP_PKEY_ALG_CTRL + 6)
182 #define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 7)
183 #define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 8)
184 #define EVP_PKEY_CTRL_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 9)
185 #define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 10)
186 #define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 11)
187 #define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12)
188 #define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 13)
189 #define EVP_PKEY_CTRL_HKDF_MODE (EVP_PKEY_ALG_CTRL + 14)
190 #define EVP_PKEY_CTRL_HKDF_MD (EVP_PKEY_ALG_CTRL + 15)
191 #define EVP_PKEY_CTRL_HKDF_KEY (EVP_PKEY_ALG_CTRL + 16)
192 #define EVP_PKEY_CTRL_HKDF_SALT (EVP_PKEY_ALG_CTRL + 17)
193 #define EVP_PKEY_CTRL_HKDF_INFO (EVP_PKEY_ALG_CTRL + 18)
194 #define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 19)
195 #define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 20)
196 #define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 21)
197 #define EVP_PKEY_CTRL_SET_MAC_KEY (EVP_PKEY_ALG_CTRL + 22)
198 #define EVP_PKEY_CTRL_DSA_PARAMGEN_BITS (EVP_PKEY_ALG_CTRL + 23)
199 #define EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS (EVP_PKEY_ALG_CTRL + 24)
200 #define EVP_PKEY_CTRL_DSA_PARAMGEN_MD (EVP_PKEY_ALG_CTRL + 25)
201
202 // EVP_PKEY_CTX_KEYGEN_INFO_COUNT is the maximum array length for
203 // |EVP_PKEY_CTX->keygen_info|. The array length corresponds to the number of
204 // arguments |BN_GENCB|'s callback function handles.
205 //
206 // |ctx->keygen_info| map to the following values in |BN_GENCB|:
207 // 1. |ctx->keygen_info[0]| -> |event|
208 // 2. |ctx->keygen_info[1]| -> |n|
209 #define EVP_PKEY_CTX_KEYGEN_INFO_COUNT 2
210
211 struct evp_pkey_ctx_st {
212 // Method associated with this operation
213 const EVP_PKEY_METHOD *pmeth;
214 // Engine that implements this method or NULL if builtin
215 ENGINE *engine;
216 // Key: may be NULL
217 EVP_PKEY *pkey;
218 // Peer key for key agreement, may be NULL
219 EVP_PKEY *peerkey;
220 // operation contains one of the |EVP_PKEY_OP_*| values.
221 int operation;
222 // Algorithm specific data
223 void *data;
224 // Application specific data used by the callback.
225 void *app_data;
226 // Callback and specific keygen data that is mapped to |BN_GENCB| for relevant
227 // implementations. This is only used for DSA, DH, and RSA in OpenSSL. AWS-LC
228 // only supports RSA as of now.
229 // See |EVP_PKEY_CTX_get_keygen_info| for more details.
230 EVP_PKEY_gen_cb *pkey_gencb;
231 int keygen_info[EVP_PKEY_CTX_KEYGEN_INFO_COUNT];
232 }; // EVP_PKEY_CTX
233
234 struct evp_pkey_method_st {
235 int pkey_id;
236
237 int (*init)(EVP_PKEY_CTX *ctx);
238 int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
239 void (*cleanup)(EVP_PKEY_CTX *ctx);
240
241 int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
242
243 int (*sign_init)(EVP_PKEY_CTX *ctx);
244 int (*sign)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
245 const uint8_t *tbs, size_t tbslen);
246
247 int (*sign_message)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
248 const uint8_t *tbs, size_t tbslen);
249 int (*verify_init)(EVP_PKEY_CTX *ctx);
250 int (*verify)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
251 const uint8_t *tbs, size_t tbslen);
252
253 int (*verify_message)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
254 const uint8_t *tbs, size_t tbslen);
255
256 int (*verify_recover)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
257 const uint8_t *sig, size_t sig_len);
258
259 int (*encrypt)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
260 const uint8_t *in, size_t inlen);
261
262 int (*decrypt)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
263 const uint8_t *in, size_t inlen);
264
265 int (*derive)(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *keylen);
266
267 int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
268
269 int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
270
271 int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
272
273 // Encapsulate, encapsulate_deterministic, keygen_deterministic, and
274 // decapsulate are operations defined for a Key Encapsulation Mechanism (KEM).
275 int (*keygen_deterministic)(EVP_PKEY_CTX *ctx,
276 EVP_PKEY *pkey,
277 const uint8_t *seed,
278 size_t *seed_len);
279
280 int (*encapsulate_deterministic)(EVP_PKEY_CTX *ctx,
281 uint8_t *ciphertext,
282 size_t *ciphertext_len,
283 uint8_t *shared_secret,
284 size_t *shared_secret_len,
285 const uint8_t *seed,
286 size_t *seed_len);
287
288 int (*encapsulate)(EVP_PKEY_CTX *ctx,
289 uint8_t *ciphertext, size_t *ciphertext_len,
290 uint8_t *shared_secret, size_t *shared_secret_len);
291
292 int (*decapsulate)(EVP_PKEY_CTX *ctx,
293 uint8_t *shared_secret, size_t *shared_secret_len,
294 const uint8_t *ciphertext, size_t ciphertext_len);
295 }; // EVP_PKEY_METHOD
296
297 // used_for_hmac indicates if |ctx| is used specifically for the |EVP_PKEY_HMAC|
298 // operation.
299 int used_for_hmac(EVP_MD_CTX *ctx);
300
301 typedef struct {
302 uint8_t *key;
303 size_t key_len;
304 } HMAC_KEY;
305
306 typedef struct {
307 const EVP_MD *md; // MD for HMAC use.
308 HMAC_CTX ctx;
309 HMAC_KEY ktmp;
310 } HMAC_PKEY_CTX;
311
312 // HMAC_KEY_set copies provided key into hmac_key. It frees any existing key
313 // on hmac_key. It returns 1 on success, and 0 otherwise.
314 int HMAC_KEY_set(HMAC_KEY* hmac_key, const uint8_t* key, const size_t key_len);
315 // HMAC_KEY_copy allocates and a new |HMAC_KEY| with identical contents (internal use).
316 int HMAC_KEY_copy(HMAC_KEY* dest, HMAC_KEY* src);
317 // HMAC_KEY_new allocates and zeroizes a |HMAC_KEY| for internal use.
318 HMAC_KEY *HMAC_KEY_new(void);
319
320 typedef struct {
321 // key is the concatenation of the private seed and public key. It is stored
322 // as a single 64-bit array to allow passing to |ED25519_sign|. If
323 // |has_private| is false, the first 32 bytes are uninitialized and the public
324 // key is in the last 32 bytes.
325 uint8_t key[64];
326 char has_private;
327 } ED25519_KEY;
328
329 // evp_pkey_set_cb_translate translates |ctx|'s |pkey_gencb| and sets it as the
330 // callback function for |cb|.
331 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
332
333 #define ED25519_PUBLIC_KEY_OFFSET 32
334 #define FIPS_EVP_PKEY_METHODS 9
335 #define NON_FIPS_EVP_PKEY_METHODS 3
336 #define ASN1_EVP_PKEY_METHODS 11
337
338 struct fips_evp_pkey_methods {
339 const EVP_PKEY_METHOD * methods[FIPS_EVP_PKEY_METHODS];
340 };
341
342 const EVP_PKEY_METHOD *EVP_PKEY_rsa_pkey_meth(void);
343 const EVP_PKEY_METHOD *EVP_PKEY_rsa_pss_pkey_meth(void);
344 const EVP_PKEY_METHOD *EVP_PKEY_ec_pkey_meth(void);
345 const EVP_PKEY_METHOD *EVP_PKEY_hkdf_pkey_meth(void);
346 const EVP_PKEY_METHOD *EVP_PKEY_hmac_pkey_meth(void);
347 const EVP_PKEY_METHOD *EVP_PKEY_ed25519_pkey_meth(void);
348 const EVP_PKEY_METHOD *EVP_PKEY_kem_pkey_meth(void);
349 const EVP_PKEY_METHOD *EVP_PKEY_pqdsa_pkey_meth(void);
350 const EVP_PKEY_METHOD *EVP_PKEY_ed25519ph_pkey_meth(void);
351
352 struct evp_pkey_ctx_signature_context_params_st {
353 const uint8_t *context;
354 size_t context_len;
355 }; // EVP_PKEY_CTX_SIGNATURE_CONTEXT_PARAMS
356
357 #if defined(__cplusplus)
358 } // extern C
359 #endif
360
361 #endif // OPENSSL_HEADER_EVP_INTERNAL_H