2 * Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* EVP_MD_CTX related stuff */
12 #include <openssl/core_numbers.h>
14 #define EVP_CTRL_RET_UNSUPPORTED -1
17 struct evp_md_ctx_st {
18 const EVP_MD *reqdigest; /* The original requested digest */
20 ENGINE *engine; /* functional reference if 'digest' is
24 /* Public key context for sign/verify */
26 /* Update function: usually copied from EVP_MD */
27 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
31 EVP_MD *fetched_digest;
34 struct evp_cipher_ctx_st {
35 const EVP_CIPHER *cipher;
36 ENGINE *engine; /* functional reference if 'cipher' is
38 int encrypt; /* encrypt or decrypt */
39 int buf_len; /* number we have left */
40 unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
41 unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
42 unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
43 int num; /* used by cfb/ofb/ctr mode */
44 /* FIXME: Should this even exist? It appears unused */
45 void *app_data; /* application stuff */
46 int key_len; /* May change for variable length cipher */
47 unsigned long flags; /* Various flags */
48 void *cipher_data; /* per EVP data */
51 unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
55 EVP_CIPHER *fetched_cipher;
56 } /* EVP_CIPHER_CTX */ ;
58 struct evp_mac_ctx_st {
59 EVP_MAC *meth; /* Method structure */
60 void *data; /* Individual method data */
63 struct evp_kdf_ctx_st {
64 EVP_KDF *meth; /* Method structure */
65 void *data; /* Algorithm-specific data */
68 struct evp_keymgmt_st {
69 int id; /* libcrypto internal */
73 CRYPTO_REF_COUNT refcnt;
76 /* Domain parameter routines */
77 OSSL_OP_keymgmt_importdomparams_fn *importdomparams;
78 OSSL_OP_keymgmt_gendomparams_fn *gendomparams;
79 OSSL_OP_keymgmt_freedomparams_fn *freedomparams;
80 OSSL_OP_keymgmt_exportdomparams_fn *exportdomparams;
81 OSSL_OP_keymgmt_importdomparam_types_fn *importdomparam_types;
82 OSSL_OP_keymgmt_exportdomparam_types_fn *exportdomparam_types;
85 OSSL_OP_keymgmt_importkey_fn *importkey;
86 OSSL_OP_keymgmt_genkey_fn *genkey;
87 OSSL_OP_keymgmt_loadkey_fn *loadkey;
88 OSSL_OP_keymgmt_freekey_fn *freekey;
89 OSSL_OP_keymgmt_exportkey_fn *exportkey;
90 OSSL_OP_keymgmt_importkey_types_fn *importkey_types;
91 OSSL_OP_keymgmt_exportkey_types_fn *exportkey_types;
94 struct keymgmt_data_st {
96 const char *properties;
99 struct evp_keyexch_st {
102 CRYPTO_REF_COUNT refcnt;
105 EVP_KEYMGMT *keymgmt;
107 OSSL_OP_keyexch_newctx_fn *newctx;
108 OSSL_OP_keyexch_init_fn *init;
109 OSSL_OP_keyexch_set_peer_fn *set_peer;
110 OSSL_OP_keyexch_derive_fn *derive;
111 OSSL_OP_keyexch_freectx_fn *freectx;
112 OSSL_OP_keyexch_dupctx_fn *dupctx;
113 OSSL_OP_keyexch_set_params_fn *set_params;
116 struct evp_signature_st {
119 CRYPTO_REF_COUNT refcnt;
122 EVP_KEYMGMT *keymgmt;
124 OSSL_OP_signature_newctx_fn *newctx;
125 OSSL_OP_signature_sign_init_fn *sign_init;
126 OSSL_OP_signature_sign_fn *sign;
127 OSSL_OP_signature_verify_init_fn *verify_init;
128 OSSL_OP_signature_verify_fn *verify;
129 OSSL_OP_signature_verify_recover_init_fn *verify_recover_init;
130 OSSL_OP_signature_verify_recover_fn *verify_recover;
131 OSSL_OP_signature_freectx_fn *freectx;
132 OSSL_OP_signature_dupctx_fn *dupctx;
133 OSSL_OP_signature_set_params_fn *set_params;
134 } /* EVP_SIGNATURE */;
136 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
137 int passlen, ASN1_TYPE *param,
138 const EVP_CIPHER *c, const EVP_MD *md,
141 struct evp_Encode_Ctx_st {
142 /* number saved in a partial encode/decode */
145 * The length is either the output line length (in input bytes) or the
146 * shortest input line length that is ok. Once decoding begins, the
147 * length is adjusted up each time a longer line is decoded
151 unsigned char enc_data[80];
152 /* number read on current line */
157 typedef struct evp_pbe_st EVP_PBE_CTL;
158 DEFINE_STACK_OF(EVP_PBE_CTL)
160 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
162 #include <openssl/ossl_typ.h>
163 #include <openssl/core.h>
165 void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id,
166 const char *algorithm, const char *properties,
167 void *(*new_method)(const char *name,
168 const OSSL_DISPATCH *fns,
172 int (*up_ref_method)(void *),
173 void (*free_method)(void *));
174 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
175 void (*user_fn)(void *method, void *arg),
177 void *(*new_method)(const char *name,
178 const OSSL_DISPATCH *fns,
182 void (*free_method)(void *));
184 /* Internal structure constructors for fetched methods */
185 EVP_MD *evp_md_new(void);
186 EVP_CIPHER *evp_cipher_new(void);
188 /* Helper functions to avoid duplicating code */
191 * These methods implement different ways to pass a params array to the
192 * provider. They will return one of these values:
194 * -2 if the method doesn't come from a provider
195 * (evp_do_param will return this to the called)
196 * -1 if the provider doesn't offer the desired function
197 * (evp_do_param will raise an error and return 0)
198 * or the return value from the desired function
199 * (evp_do_param will return it to the caller)
201 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
202 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
203 OSSL_PARAM params[]);
204 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
205 OSSL_PARAM params[]);
206 int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
207 int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
208 OSSL_PARAM params[]);
209 int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
210 OSSL_PARAM params[]);
212 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
214 #define M_check_autoarg(ctx, arg, arglen, err) \
215 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
216 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
219 EVPerr(err, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
226 if (*arglen < pksize) { \
227 EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \