VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.7/include/crypto/evp.h@ 98133

最後變更 在這個檔案從98133是 97984,由 vboxsync 提交於 2 年 前

libs/openssl-3.0.7: Shut up pragma once warnings for internal headers. Added sed script for doing that. bugref:10317

檔案大小: 36.1 KB
 
1/*
2 * Copyright 2015-2022 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_EVP_H
11# define OSSL_CRYPTO_EVP_H
12# ifndef RT_WITHOUT_PRAGMA_ONCE /* VBOX */
13# pragma once
14# endif /* VBOX */
15
16# include <openssl/evp.h>
17# include <openssl/core_dispatch.h>
18# include "internal/refcount.h"
19# include "crypto/ecx.h"
20
21/*
22 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
23 * values in evp.h
24 */
25#define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
26
27#define evp_pkey_ctx_is_legacy(ctx) \
28 ((ctx)->keymgmt == NULL)
29#define evp_pkey_ctx_is_provided(ctx) \
30 (!evp_pkey_ctx_is_legacy(ctx))
31
32struct evp_pkey_ctx_st {
33 /* Actual operation */
34 int operation;
35
36 /*
37 * Library context, property query, keytype and keymgmt associated with
38 * this context
39 */
40 OSSL_LIB_CTX *libctx;
41 char *propquery;
42 const char *keytype;
43 /* If |pkey| below is set, this field is always a reference to its keymgmt */
44 EVP_KEYMGMT *keymgmt;
45
46 union {
47 struct {
48 void *genctx;
49 } keymgmt;
50
51 struct {
52 EVP_KEYEXCH *exchange;
53 /*
54 * Opaque ctx returned from a providers exchange algorithm
55 * implementation OSSL_FUNC_keyexch_newctx()
56 */
57 void *algctx;
58 } kex;
59
60 struct {
61 EVP_SIGNATURE *signature;
62 /*
63 * Opaque ctx returned from a providers signature algorithm
64 * implementation OSSL_FUNC_signature_newctx()
65 */
66 void *algctx;
67 } sig;
68
69 struct {
70 EVP_ASYM_CIPHER *cipher;
71 /*
72 * Opaque ctx returned from a providers asymmetric cipher algorithm
73 * implementation OSSL_FUNC_asym_cipher_newctx()
74 */
75 void *algctx;
76 } ciph;
77 struct {
78 EVP_KEM *kem;
79 /*
80 * Opaque ctx returned from a providers KEM algorithm
81 * implementation OSSL_FUNC_kem_newctx()
82 */
83 void *algctx;
84 } encap;
85 } op;
86
87 /*
88 * Cached parameters. Inits of operations that depend on these should
89 * call evp_pkey_ctx_use_delayed_data() when the operation has been set
90 * up properly.
91 */
92 struct {
93 /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */
94 char *dist_id_name; /* The name used with EVP_PKEY_CTX_ctrl_str() */
95 void *dist_id; /* The distinguishing ID itself */
96 size_t dist_id_len; /* The length of the distinguishing ID */
97
98 /* Indicators of what has been set. Keep them together! */
99 unsigned int dist_id_set : 1;
100 } cached_parameters;
101
102 /* Application specific data, usually used by the callback */
103 void *app_data;
104 /* Keygen callback */
105 EVP_PKEY_gen_cb *pkey_gencb;
106 /* implementation specific keygen data */
107 int *keygen_info;
108 int keygen_info_count;
109
110 /* Legacy fields below */
111
112 /* EVP_PKEY identity */
113 int legacy_keytype;
114 /* Method associated with this operation */
115 const EVP_PKEY_METHOD *pmeth;
116 /* Engine that implements this method or NULL if builtin */
117 ENGINE *engine;
118 /* Key: may be NULL */
119 EVP_PKEY *pkey;
120 /* Peer key for key agreement, may be NULL */
121 EVP_PKEY *peerkey;
122 /* Algorithm specific data */
123 void *data;
124 /* Indicator if digest_custom needs to be called */
125 unsigned int flag_call_digest_custom:1;
126 /*
127 * Used to support taking custody of memory in the case of a provider being
128 * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This
129 * member should NOT be used for any other purpose and should be removed
130 * when said deprecated API is excised completely.
131 */
132 BIGNUM *rsa_pubexp;
133} /* EVP_PKEY_CTX */ ;
134
135#define EVP_PKEY_FLAG_DYNAMIC 1
136
137struct evp_pkey_method_st {
138 int pkey_id;
139 int flags;
140 int (*init) (EVP_PKEY_CTX *ctx);
141 int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
142 void (*cleanup) (EVP_PKEY_CTX *ctx);
143 int (*paramgen_init) (EVP_PKEY_CTX *ctx);
144 int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
145 int (*keygen_init) (EVP_PKEY_CTX *ctx);
146 int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
147 int (*sign_init) (EVP_PKEY_CTX *ctx);
148 int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
149 const unsigned char *tbs, size_t tbslen);
150 int (*verify_init) (EVP_PKEY_CTX *ctx);
151 int (*verify) (EVP_PKEY_CTX *ctx,
152 const unsigned char *sig, size_t siglen,
153 const unsigned char *tbs, size_t tbslen);
154 int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
155 int (*verify_recover) (EVP_PKEY_CTX *ctx,
156 unsigned char *rout, size_t *routlen,
157 const unsigned char *sig, size_t siglen);
158 int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
159 int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
160 EVP_MD_CTX *mctx);
161 int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
162 int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
163 EVP_MD_CTX *mctx);
164 int (*encrypt_init) (EVP_PKEY_CTX *ctx);
165 int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
166 const unsigned char *in, size_t inlen);
167 int (*decrypt_init) (EVP_PKEY_CTX *ctx);
168 int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
169 const unsigned char *in, size_t inlen);
170 int (*derive_init) (EVP_PKEY_CTX *ctx);
171 int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
172 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
173 int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
174 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
175 const unsigned char *tbs, size_t tbslen);
176 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
177 size_t siglen, const unsigned char *tbs,
178 size_t tbslen);
179 int (*check) (EVP_PKEY *pkey);
180 int (*public_check) (EVP_PKEY *pkey);
181 int (*param_check) (EVP_PKEY *pkey);
182
183 int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
184} /* EVP_PKEY_METHOD */ ;
185
186DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
187
188void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
189
190const EVP_PKEY_METHOD *ossl_dh_pkey_method(void);
191const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void);
192const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void);
193const EVP_PKEY_METHOD *ossl_ec_pkey_method(void);
194const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void);
195const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void);
196const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void);
197const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void);
198const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void);
199const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void);
200
201struct evp_mac_st {
202 OSSL_PROVIDER *prov;
203 int name_id;
204 char *type_name;
205 const char *description;
206
207 CRYPTO_REF_COUNT refcnt;
208 CRYPTO_RWLOCK *lock;
209
210 OSSL_FUNC_mac_newctx_fn *newctx;
211 OSSL_FUNC_mac_dupctx_fn *dupctx;
212 OSSL_FUNC_mac_freectx_fn *freectx;
213 OSSL_FUNC_mac_init_fn *init;
214 OSSL_FUNC_mac_update_fn *update;
215 OSSL_FUNC_mac_final_fn *final;
216 OSSL_FUNC_mac_gettable_params_fn *gettable_params;
217 OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params;
218 OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params;
219 OSSL_FUNC_mac_get_params_fn *get_params;
220 OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params;
221 OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params;
222};
223
224struct evp_kdf_st {
225 OSSL_PROVIDER *prov;
226 int name_id;
227 char *type_name;
228 const char *description;
229 CRYPTO_REF_COUNT refcnt;
230 CRYPTO_RWLOCK *lock;
231
232 OSSL_FUNC_kdf_newctx_fn *newctx;
233 OSSL_FUNC_kdf_dupctx_fn *dupctx;
234 OSSL_FUNC_kdf_freectx_fn *freectx;
235 OSSL_FUNC_kdf_reset_fn *reset;
236 OSSL_FUNC_kdf_derive_fn *derive;
237 OSSL_FUNC_kdf_gettable_params_fn *gettable_params;
238 OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params;
239 OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params;
240 OSSL_FUNC_kdf_get_params_fn *get_params;
241 OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params;
242 OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params;
243};
244
245#define EVP_ORIG_DYNAMIC 0
246#define EVP_ORIG_GLOBAL 1
247#define EVP_ORIG_METH 2
248
249struct evp_md_st {
250 /* nid */
251 int type;
252
253 /* Legacy structure members */
254 int pkey_type;
255 int md_size;
256 unsigned long flags;
257 int origin;
258 int (*init) (EVP_MD_CTX *ctx);
259 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
260 int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
261 int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
262 int (*cleanup) (EVP_MD_CTX *ctx);
263 int block_size;
264 int ctx_size; /* how big does the ctx->md_data need to be */
265 /* control function */
266 int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
267
268 /* New structure members */
269 /* Above comment to be removed when legacy has gone */
270 int name_id;
271 char *type_name;
272 const char *description;
273 OSSL_PROVIDER *prov;
274 CRYPTO_REF_COUNT refcnt;
275 CRYPTO_RWLOCK *lock;
276 OSSL_FUNC_digest_newctx_fn *newctx;
277 OSSL_FUNC_digest_init_fn *dinit;
278 OSSL_FUNC_digest_update_fn *dupdate;
279 OSSL_FUNC_digest_final_fn *dfinal;
280 OSSL_FUNC_digest_digest_fn *digest;
281 OSSL_FUNC_digest_freectx_fn *freectx;
282 OSSL_FUNC_digest_dupctx_fn *dupctx;
283 OSSL_FUNC_digest_get_params_fn *get_params;
284 OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
285 OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
286 OSSL_FUNC_digest_gettable_params_fn *gettable_params;
287 OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
288 OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
289
290} /* EVP_MD */ ;
291
292struct evp_cipher_st {
293 int nid;
294
295 int block_size;
296 /* Default value for variable length ciphers */
297 int key_len;
298 int iv_len;
299
300 /* Legacy structure members */
301 /* Various flags */
302 unsigned long flags;
303 /* How the EVP_CIPHER was created. */
304 int origin;
305 /* init key */
306 int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
307 const unsigned char *iv, int enc);
308 /* encrypt/decrypt data */
309 int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
310 const unsigned char *in, size_t inl);
311 /* cleanup ctx */
312 int (*cleanup) (EVP_CIPHER_CTX *);
313 /* how big ctx->cipher_data needs to be */
314 int ctx_size;
315 /* Populate a ASN1_TYPE with parameters */
316 int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
317 /* Get parameters from a ASN1_TYPE */
318 int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
319 /* Miscellaneous operations */
320 int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
321 /* Application data */
322 void *app_data;
323
324 /* New structure members */
325 /* Above comment to be removed when legacy has gone */
326 int name_id;
327 char *type_name;
328 const char *description;
329 OSSL_PROVIDER *prov;
330 CRYPTO_REF_COUNT refcnt;
331 CRYPTO_RWLOCK *lock;
332 OSSL_FUNC_cipher_newctx_fn *newctx;
333 OSSL_FUNC_cipher_encrypt_init_fn *einit;
334 OSSL_FUNC_cipher_decrypt_init_fn *dinit;
335 OSSL_FUNC_cipher_update_fn *cupdate;
336 OSSL_FUNC_cipher_final_fn *cfinal;
337 OSSL_FUNC_cipher_cipher_fn *ccipher;
338 OSSL_FUNC_cipher_freectx_fn *freectx;
339 OSSL_FUNC_cipher_dupctx_fn *dupctx;
340 OSSL_FUNC_cipher_get_params_fn *get_params;
341 OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
342 OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
343 OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
344 OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
345 OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
346} /* EVP_CIPHER */ ;
347
348/* Macros to code block cipher wrappers */
349
350/* Wrapper functions for each cipher mode */
351
352#define EVP_C_DATA(kstruct, ctx) \
353 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
354
355#define BLOCK_CIPHER_ecb_loop() \
356 size_t i, bl; \
357 bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \
358 if (inl < bl) return 1;\
359 inl -= bl; \
360 for (i=0; i <= inl; i+=bl)
361
362#define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
363static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
364{\
365 BLOCK_CIPHER_ecb_loop() \
366 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \
367 return 1;\
368}
369
370#define EVP_MAXCHUNK ((size_t)1 << 30)
371
372#define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
373 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
374{\
375 while(inl>=EVP_MAXCHUNK) {\
376 int num = EVP_CIPHER_CTX_get_num(ctx);\
377 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
378 EVP_CIPHER_CTX_set_num(ctx, num);\
379 inl-=EVP_MAXCHUNK;\
380 in +=EVP_MAXCHUNK;\
381 out+=EVP_MAXCHUNK;\
382 }\
383 if (inl) {\
384 int num = EVP_CIPHER_CTX_get_num(ctx);\
385 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
386 EVP_CIPHER_CTX_set_num(ctx, num);\
387 }\
388 return 1;\
389}
390
391#define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
392static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
393{\
394 while(inl>=EVP_MAXCHUNK) \
395 {\
396 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\
397 inl-=EVP_MAXCHUNK;\
398 in +=EVP_MAXCHUNK;\
399 out+=EVP_MAXCHUNK;\
400 }\
401 if (inl)\
402 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\
403 return 1;\
404}
405
406#define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
407static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
408{\
409 size_t chunk = EVP_MAXCHUNK;\
410 if (cbits == 1) chunk >>= 3;\
411 if (inl < chunk) chunk = inl;\
412 while (inl && inl >= chunk)\
413 {\
414 int num = EVP_CIPHER_CTX_get_num(ctx);\
415 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
416 ((cbits == 1) \
417 && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
418 ? chunk*8 : chunk), \
419 &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\
420 &num, EVP_CIPHER_CTX_is_encrypting(ctx));\
421 EVP_CIPHER_CTX_set_num(ctx, num);\
422 inl -= chunk;\
423 in += chunk;\
424 out += chunk;\
425 if (inl < chunk) chunk = inl;\
426 }\
427 return 1;\
428}
429
430#define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
431 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
432 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
433 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
434 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
435
436#define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
437 key_len, iv_len, flags, init_key, cleanup, \
438 set_asn1, get_asn1, ctrl) \
439static const EVP_CIPHER cname##_##mode = { \
440 nid##_##nmode, block_size, key_len, iv_len, \
441 flags | EVP_CIPH_##MODE##_MODE, \
442 EVP_ORIG_GLOBAL, \
443 init_key, \
444 cname##_##mode##_cipher, \
445 cleanup, \
446 sizeof(kstruct), \
447 set_asn1, get_asn1,\
448 ctrl, \
449 NULL \
450}; \
451const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
452
453#define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
454 iv_len, flags, init_key, cleanup, set_asn1, \
455 get_asn1, ctrl) \
456BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
457 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
458
459#define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
460 iv_len, cbits, flags, init_key, cleanup, \
461 set_asn1, get_asn1, ctrl) \
462BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
463 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
464 get_asn1, ctrl)
465
466#define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
467 iv_len, cbits, flags, init_key, cleanup, \
468 set_asn1, get_asn1, ctrl) \
469BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
470 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
471 get_asn1, ctrl)
472
473#define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
474 flags, init_key, cleanup, set_asn1, \
475 get_asn1, ctrl) \
476BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
477 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
478
479#define BLOCK_CIPHER_defs(cname, kstruct, \
480 nid, block_size, key_len, iv_len, cbits, flags, \
481 init_key, cleanup, set_asn1, get_asn1, ctrl) \
482BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
483 init_key, cleanup, set_asn1, get_asn1, ctrl) \
484BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
485 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
486BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
487 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
488BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
489 init_key, cleanup, set_asn1, get_asn1, ctrl)
490
491/*-
492#define BLOCK_CIPHER_defs(cname, kstruct, \
493 nid, block_size, key_len, iv_len, flags,\
494 init_key, cleanup, set_asn1, get_asn1, ctrl)\
495static const EVP_CIPHER cname##_cbc = {\
496 nid##_cbc, block_size, key_len, iv_len, \
497 flags | EVP_CIPH_CBC_MODE,\
498 EVP_ORIG_GLOBAL,\
499 init_key,\
500 cname##_cbc_cipher,\
501 cleanup,\
502 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
503 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
504 set_asn1, get_asn1,\
505 ctrl, \
506 NULL \
507};\
508const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
509static const EVP_CIPHER cname##_cfb = {\
510 nid##_cfb64, 1, key_len, iv_len, \
511 flags | EVP_CIPH_CFB_MODE,\
512 EVP_ORIG_GLOBAL,\
513 init_key,\
514 cname##_cfb_cipher,\
515 cleanup,\
516 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
517 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
518 set_asn1, get_asn1,\
519 ctrl,\
520 NULL \
521};\
522const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
523static const EVP_CIPHER cname##_ofb = {\
524 nid##_ofb64, 1, key_len, iv_len, \
525 flags | EVP_CIPH_OFB_MODE,\
526 EVP_ORIG_GLOBAL,\
527 init_key,\
528 cname##_ofb_cipher,\
529 cleanup,\
530 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
531 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
532 set_asn1, get_asn1,\
533 ctrl,\
534 NULL \
535};\
536const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
537static const EVP_CIPHER cname##_ecb = {\
538 nid##_ecb, block_size, key_len, iv_len, \
539 flags | EVP_CIPH_ECB_MODE,\
540 EVP_ORIG_GLOBAL,\
541 init_key,\
542 cname##_ecb_cipher,\
543 cleanup,\
544 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
545 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
546 set_asn1, get_asn1,\
547 ctrl,\
548 NULL \
549};\
550const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
551*/
552
553#define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
554 block_size, key_len, iv_len, cbits, \
555 flags, init_key, \
556 cleanup, set_asn1, get_asn1, ctrl) \
557 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
558 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
559 cbits, flags, init_key, cleanup, set_asn1, \
560 get_asn1, ctrl)
561
562#define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
563 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
564 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
565 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
566 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
567 cipher##_init_key, NULL, NULL, NULL, NULL)
568
569typedef struct {
570 unsigned char iv[EVP_MAX_IV_LENGTH];
571 unsigned int iv_len;
572 unsigned int tag_len;
573} evp_cipher_aead_asn1_params;
574
575int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
576 evp_cipher_aead_asn1_params *params);
577
578int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
579 evp_cipher_aead_asn1_params *params);
580
581/*
582 * To support transparent execution of operation in backends other
583 * than the "origin" key, we support transparent export/import to
584 * those providers, and maintain a cache of the imported keydata,
585 * so we don't need to redo the export/import every time we perform
586 * the same operation in that same provider.
587 * This requires that the "origin" backend (whether it's a legacy or a
588 * provider "origin") implements exports, and that the target provider
589 * has an EVP_KEYMGMT that implements import.
590 */
591typedef struct {
592 EVP_KEYMGMT *keymgmt;
593 void *keydata;
594} OP_CACHE_ELEM;
595
596DEFINE_STACK_OF(OP_CACHE_ELEM)
597
598/*
599 * An EVP_PKEY can have the following states:
600 *
601 * untyped & empty:
602 *
603 * type == EVP_PKEY_NONE && keymgmt == NULL
604 *
605 * typed & empty:
606 *
607 * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
608 * || (keymgmt != NULL && keydata == NULL) ## provider side
609 *
610 * fully assigned:
611 *
612 * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
613 * || (keymgmt != NULL && keydata != NULL) ## provider side
614 *
615 * The easiest way to detect a legacy key is:
616 *
617 * keymgmt == NULL && type != EVP_PKEY_NONE
618 *
619 * The easiest way to detect a provider side key is:
620 *
621 * keymgmt != NULL
622 */
623#define evp_pkey_is_blank(pk) \
624 ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
625#define evp_pkey_is_typed(pk) \
626 ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
627#ifndef FIPS_MODULE
628# define evp_pkey_is_assigned(pk) \
629 ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
630#else
631# define evp_pkey_is_assigned(pk) \
632 ((pk)->keydata != NULL)
633#endif
634#define evp_pkey_is_legacy(pk) \
635 ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
636#define evp_pkey_is_provided(pk) \
637 ((pk)->keymgmt != NULL)
638
639union legacy_pkey_st {
640 void *ptr;
641 struct rsa_st *rsa; /* RSA */
642# ifndef OPENSSL_NO_DSA
643 struct dsa_st *dsa; /* DSA */
644# endif
645# ifndef OPENSSL_NO_DH
646 struct dh_st *dh; /* DH */
647# endif
648# ifndef OPENSSL_NO_EC
649 struct ec_key_st *ec; /* ECC */
650 ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
651# endif
652};
653
654struct evp_pkey_st {
655 /* == Legacy attributes == */
656 int type;
657 int save_type;
658
659# ifndef FIPS_MODULE
660 /*
661 * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
662 * a pointer to a low level key and possibly a pointer to an engine.
663 */
664 const EVP_PKEY_ASN1_METHOD *ameth;
665 ENGINE *engine;
666 ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
667
668 /* Union to store the reference to an origin legacy key */
669 union legacy_pkey_st pkey;
670
671 /* Union to store the reference to a non-origin legacy key */
672 union legacy_pkey_st legacy_cache_pkey;
673# endif
674
675 /* == Common attributes == */
676 CRYPTO_REF_COUNT references;
677 CRYPTO_RWLOCK *lock;
678#ifndef FIPS_MODULE
679 STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
680 int save_parameters;
681 unsigned int foreign:1; /* the low-level key is using an engine or an app-method */
682 CRYPTO_EX_DATA ex_data;
683#endif
684
685 /* == Provider attributes == */
686
687 /*
688 * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
689 * and a pointer to the provider side key data. This is never used at
690 * the same time as the legacy key data above.
691 */
692 EVP_KEYMGMT *keymgmt;
693 void *keydata;
694 /*
695 * If any libcrypto code does anything that may modify the keydata
696 * contents, this dirty counter must be incremented.
697 */
698 size_t dirty_cnt;
699
700 /*
701 * To support transparent execution of operation in backends other
702 * than the "origin" key, we support transparent export/import to
703 * those providers, and maintain a cache of the imported keydata,
704 * so we don't need to redo the export/import every time we perform
705 * the same operation in that same provider.
706 */
707 STACK_OF(OP_CACHE_ELEM) *operation_cache;
708
709 /*
710 * We keep a copy of that "origin"'s dirty count, so we know if the
711 * operation cache needs flushing.
712 */
713 size_t dirty_cnt_copy;
714
715 /* Cache of key object information */
716 struct {
717 int bits;
718 int security_bits;
719 int size;
720 } cache;
721} /* EVP_PKEY */ ;
722
723#define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
724 ((ctx)->operation == EVP_PKEY_OP_SIGN \
725 || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \
726 || (ctx)->operation == EVP_PKEY_OP_VERIFY \
727 || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \
728 || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER)
729
730#define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
731 ((ctx)->operation == EVP_PKEY_OP_DERIVE)
732
733#define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
734 ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
735 || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
736
737#define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
738 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
739 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
740
741#define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \
742 ((ctx)->operation == EVP_PKEY_OP_FROMDATA)
743
744#define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
745 ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
746 || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
747
748void openssl_add_all_ciphers_int(void);
749void openssl_add_all_digests_int(void);
750void evp_cleanup_int(void);
751void evp_app_cleanup_int(void);
752void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
753 EVP_KEYMGMT **keymgmt,
754 const char *propquery);
755#ifndef FIPS_MODULE
756int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
757void *evp_pkey_get_legacy(EVP_PKEY *pk);
758void evp_pkey_free_legacy(EVP_PKEY *x);
759EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf,
760 OSSL_LIB_CTX *libctx, const char *propq);
761#endif
762
763/*
764 * KEYMGMT utility functions
765 */
766
767/*
768 * Key import structure and helper function, to be used as an export callback
769 */
770struct evp_keymgmt_util_try_import_data_st {
771 EVP_KEYMGMT *keymgmt;
772 void *keydata;
773
774 int selection;
775};
776int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
777int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
778 void *keydata);
779EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
780
781int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
782 OSSL_CALLBACK *export_cb, void *export_cbarg);
783void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
784OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
785 EVP_KEYMGMT *keymgmt);
786int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk, int locking);
787int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk,
788 EVP_KEYMGMT *keymgmt, void *keydata);
789void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
790void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
791 int selection, const OSSL_PARAM params[]);
792int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
793int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
794int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
795void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
796 void *genctx, OSSL_CALLBACK *cb, void *cbarg);
797int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
798 void *keydata,
799 char *mdname, size_t mdname_sz);
800const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
801 int op_id);
802
803/*
804 * KEYMGMT provider interface functions
805 */
806void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
807void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
808int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
809 void *keydata, OSSL_PARAM params[]);
810int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
811 void *keydata, const OSSL_PARAM params[]);
812void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
813 const OSSL_PARAM params[]);
814int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
815 void *template);
816int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
817 const OSSL_PARAM params[]);
818void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
819 OSSL_CALLBACK *cb, void *cbarg);
820void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
821
822int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt);
823void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
824 const void *objref, size_t objref_sz);
825
826int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
827int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
828 int selection, int checktype);
829int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
830 const void *keydata1, const void *keydata2,
831 int selection);
832
833int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
834 int selection, const OSSL_PARAM params[]);
835const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
836 int selection);
837int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
838 int selection, OSSL_CALLBACK *param_cb, void *cbarg);
839const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
840 int selection);
841void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt,
842 const void *keydata_from, int selection);
843EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
844 const char *name,
845 const char *properties);
846
847/* Pulling defines out of C source files */
848
849# define EVP_RC4_KEY_SIZE 16
850# ifndef TLS1_1_VERSION
851# define TLS1_1_VERSION 0x0302
852# endif
853
854void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
855
856/* EVP_ENCODE_CTX flags */
857/* Don't generate new lines when encoding */
858#define EVP_ENCODE_CTX_NO_NEWLINES 1
859/* Use the SRP base64 alphabet instead of the standard one */
860#define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
861
862const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
863 const char *name);
864const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx,
865 const char *name);
866
867int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
868 const unsigned char *salt, int saltlen, int iter,
869 const EVP_MD *digest, int keylen,
870 unsigned char *out,
871 OSSL_LIB_CTX *libctx, const char *propq);
872
873# ifndef FIPS_MODULE
874/*
875 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
876 *
877 * Return 1 on success, 0 or negative for errors.
878 *
879 * In particular they return -2 if any of the params is not supported.
880 *
881 * They are not available in FIPS_MODULE as they depend on
882 * - EVP_PKEY_CTX_{get,set}_params()
883 * - EVP_PKEY_CTX_{gettable,settable}_params()
884 *
885 */
886int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
887int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
888
889EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
890 OSSL_LIB_CTX *libctx, const char *propq);
891int evp_pkey_name2type(const char *name);
892const char *evp_pkey_type2name(int type);
893
894int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len);
895int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id);
896int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len);
897
898int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
899# endif /* !defined(FIPS_MODULE) */
900
901int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx);
902int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov);
903
904int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
905 int loadconfig);
906int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
907 int loadconfig, int mirrored);
908char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig);
909
910void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest);
911
912/* Three possible states: */
913# define EVP_PKEY_STATE_UNKNOWN 0
914# define EVP_PKEY_STATE_LEGACY 1
915# define EVP_PKEY_STATE_PROVIDER 2
916int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx);
917
918/* These two must ONLY be called for provider side operations */
919int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx,
920 int keytype, int optype,
921 int cmd, int p1, void *p2);
922int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx,
923 const char *name, const char *value);
924
925/* These two must ONLY be called for legacy operations */
926int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params);
927int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
928
929/* This must ONLY be called for legacy EVP_PKEYs */
930int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params);
931
932/* Same as the public get0 functions but are not const */
933# ifndef OPENSSL_NO_DEPRECATED_3_0
934DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey);
935EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey);
936RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey);
937# endif
938
939/* Get internal identification number routines */
940int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher);
941int evp_cipher_get_number(const EVP_CIPHER *cipher);
942int evp_kdf_get_number(const EVP_KDF *kdf);
943int evp_kem_get_number(const EVP_KEM *wrap);
944int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch);
945int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt);
946int evp_mac_get_number(const EVP_MAC *mac);
947int evp_md_get_number(const EVP_MD *md);
948int evp_rand_get_number(const EVP_RAND *rand);
949int evp_signature_get_number(const EVP_SIGNATURE *signature);
950
951#endif /* OSSL_CRYPTO_EVP_H */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette