VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/include/crypto/evp.h@ 108403

最後變更 在這個檔案從108403是 108206,由 vboxsync 提交於 5 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

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