]>
Commit | Line | Data |
---|---|---|
27af42f9 | 1 | /* |
0d664759 | 2 | * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. |
27af42f9 | 3 | * |
48f4ad77 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
27af42f9 DSH |
8 | */ |
9 | ||
c0804614 | 10 | #include <openssl/evp.h> |
3653d0c2 | 11 | #include <openssl/core_numbers.h> |
2f545ae4 KR |
12 | #include "internal/refcount.h" |
13 | ||
4803717f PY |
14 | /* |
15 | * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag | |
16 | * values in evp.h | |
17 | */ | |
18 | #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400 | |
19 | ||
27af42f9 | 20 | struct evp_pkey_ctx_st { |
ff64702b MC |
21 | EVP_KEYEXCH *exchange; |
22 | void *exchprovctx; | |
23 | ||
24 | /* Legacy fields below */ | |
25 | ||
27af42f9 DSH |
26 | /* Method associated with this operation */ |
27 | const EVP_PKEY_METHOD *pmeth; | |
28 | /* Engine that implements this method or NULL if builtin */ | |
29 | ENGINE *engine; | |
30 | /* Key: may be NULL */ | |
31 | EVP_PKEY *pkey; | |
32 | /* Peer key for key agreement, may be NULL */ | |
33 | EVP_PKEY *peerkey; | |
34 | /* Actual operation */ | |
35 | int operation; | |
36 | /* Algorithm specific data */ | |
37 | void *data; | |
38 | /* Application specific data */ | |
39 | void *app_data; | |
40 | /* Keygen callback */ | |
41 | EVP_PKEY_gen_cb *pkey_gencb; | |
42 | /* implementation specific keygen data */ | |
43 | int *keygen_info; | |
44 | int keygen_info_count; | |
45 | } /* EVP_PKEY_CTX */ ; | |
46 | ||
47 | #define EVP_PKEY_FLAG_DYNAMIC 1 | |
48 | ||
49 | struct evp_pkey_method_st { | |
50 | int pkey_id; | |
51 | int flags; | |
52 | int (*init) (EVP_PKEY_CTX *ctx); | |
9fdcc21f | 53 | int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src); |
27af42f9 DSH |
54 | void (*cleanup) (EVP_PKEY_CTX *ctx); |
55 | int (*paramgen_init) (EVP_PKEY_CTX *ctx); | |
56 | int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); | |
57 | int (*keygen_init) (EVP_PKEY_CTX *ctx); | |
58 | int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); | |
59 | int (*sign_init) (EVP_PKEY_CTX *ctx); | |
60 | int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |
61 | const unsigned char *tbs, size_t tbslen); | |
62 | int (*verify_init) (EVP_PKEY_CTX *ctx); | |
63 | int (*verify) (EVP_PKEY_CTX *ctx, | |
64 | const unsigned char *sig, size_t siglen, | |
65 | const unsigned char *tbs, size_t tbslen); | |
66 | int (*verify_recover_init) (EVP_PKEY_CTX *ctx); | |
67 | int (*verify_recover) (EVP_PKEY_CTX *ctx, | |
68 | unsigned char *rout, size_t *routlen, | |
69 | const unsigned char *sig, size_t siglen); | |
70 | int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
71 | int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |
72 | EVP_MD_CTX *mctx); | |
73 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
74 | int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen, | |
75 | EVP_MD_CTX *mctx); | |
76 | int (*encrypt_init) (EVP_PKEY_CTX *ctx); | |
77 | int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, | |
78 | const unsigned char *in, size_t inlen); | |
79 | int (*decrypt_init) (EVP_PKEY_CTX *ctx); | |
80 | int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, | |
81 | const unsigned char *in, size_t inlen); | |
82 | int (*derive_init) (EVP_PKEY_CTX *ctx); | |
83 | int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); | |
84 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2); | |
85 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value); | |
f723c98e DSH |
86 | int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
87 | const unsigned char *tbs, size_t tbslen); | |
88 | int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, | |
89 | size_t siglen, const unsigned char *tbs, | |
90 | size_t tbslen); | |
2aee35d3 | 91 | int (*check) (EVP_PKEY *pkey); |
b0004708 PY |
92 | int (*public_check) (EVP_PKEY *pkey); |
93 | int (*param_check) (EVP_PKEY *pkey); | |
0a8fdef7 PY |
94 | |
95 | int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
27af42f9 DSH |
96 | } /* EVP_PKEY_METHOD */ ; |
97 | ||
a8eba56e | 98 | DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD) |
4a1f3f27 | 99 | |
27af42f9 | 100 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); |
8f463dbd DSH |
101 | |
102 | extern const EVP_PKEY_METHOD cmac_pkey_meth; | |
103 | extern const EVP_PKEY_METHOD dh_pkey_meth; | |
104 | extern const EVP_PKEY_METHOD dhx_pkey_meth; | |
105 | extern const EVP_PKEY_METHOD dsa_pkey_meth; | |
106 | extern const EVP_PKEY_METHOD ec_pkey_meth; | |
ddb634fe | 107 | extern const EVP_PKEY_METHOD sm2_pkey_meth; |
262bd85f | 108 | extern const EVP_PKEY_METHOD ecx25519_pkey_meth; |
13735cfe | 109 | extern const EVP_PKEY_METHOD ecx448_pkey_meth; |
42a3008a | 110 | extern const EVP_PKEY_METHOD ed25519_pkey_meth; |
13735cfe | 111 | extern const EVP_PKEY_METHOD ed448_pkey_meth; |
8f463dbd DSH |
112 | extern const EVP_PKEY_METHOD hmac_pkey_meth; |
113 | extern const EVP_PKEY_METHOD rsa_pkey_meth; | |
6577e008 | 114 | extern const EVP_PKEY_METHOD rsa_pss_pkey_meth; |
cefa762e | 115 | extern const EVP_PKEY_METHOD scrypt_pkey_meth; |
1eff3485 | 116 | extern const EVP_PKEY_METHOD tls1_prf_pkey_meth; |
aacfb134 | 117 | extern const EVP_PKEY_METHOD hkdf_pkey_meth; |
52ad5b60 | 118 | extern const EVP_PKEY_METHOD poly1305_pkey_meth; |
3f5616d7 | 119 | extern const EVP_PKEY_METHOD siphash_pkey_meth; |
2db6bf6f | 120 | |
567db2c1 RL |
121 | /* struct evp_mac_impl_st is defined by the implementation */ |
122 | typedef struct evp_mac_impl_st EVP_MAC_IMPL; | |
123 | struct evp_mac_st { | |
124 | int type; | |
125 | EVP_MAC_IMPL *(*new) (void); | |
7ed66e26 | 126 | EVP_MAC_IMPL *(*dup) (const EVP_MAC_IMPL *macsrc); |
567db2c1 RL |
127 | void (*free) (EVP_MAC_IMPL *macctx); |
128 | size_t (*size) (EVP_MAC_IMPL *macctx); | |
129 | int (*init) (EVP_MAC_IMPL *macctx); | |
130 | int (*update) (EVP_MAC_IMPL *macctx, const unsigned char *data, | |
131 | size_t datalen); | |
132 | int (*final) (EVP_MAC_IMPL *macctx, unsigned char *out); | |
133 | int (*ctrl) (EVP_MAC_IMPL *macctx, int cmd, va_list args); | |
134 | int (*ctrl_str) (EVP_MAC_IMPL *macctx, const char *type, const char *value); | |
135 | }; | |
136 | ||
c3a261f8 | 137 | extern const EVP_MAC blake2b_mac_meth; |
33e113b0 | 138 | extern const EVP_MAC blake2s_mac_meth; |
f71faf27 | 139 | extern const EVP_MAC cmac_meth; |
afc580b9 | 140 | extern const EVP_MAC gmac_meth; |
6723f867 | 141 | extern const EVP_MAC hmac_meth; |
6e624a64 SL |
142 | extern const EVP_MAC kmac128_meth; |
143 | extern const EVP_MAC kmac256_meth; | |
c89d9cda | 144 | extern const EVP_MAC siphash_meth; |
c1da4b2a | 145 | extern const EVP_MAC poly1305_meth; |
f71faf27 | 146 | |
6e624a64 SL |
147 | /* Internal keccak algorithms used for KMAC */ |
148 | const EVP_MD *evp_keccak_kmac128(void); | |
149 | const EVP_MD *evp_keccak_kmac256(void); | |
150 | ||
567db2c1 RL |
151 | /* |
152 | * This function is internal for now, but can be made external when needed. | |
153 | * The documentation would read: | |
154 | * | |
155 | * EVP_add_mac() adds the MAC implementation C<mac> to the internal | |
156 | * object database. | |
157 | */ | |
158 | int EVP_add_mac(const EVP_MAC *mac); | |
d2ba8123 | 159 | int EVP_add_kdf(const EVP_KDF *kdf); |
567db2c1 | 160 | |
5a285add DM |
161 | /* struct evp_kdf_impl_st is defined by the implementation */ |
162 | typedef struct evp_kdf_impl_st EVP_KDF_IMPL; | |
d2ba8123 | 163 | struct evp_kdf_st { |
5a285add DM |
164 | int type; |
165 | EVP_KDF_IMPL *(*new) (void); | |
166 | void (*free) (EVP_KDF_IMPL *impl); | |
167 | void (*reset) (EVP_KDF_IMPL *impl); | |
168 | int (*ctrl) (EVP_KDF_IMPL *impl, int cmd, va_list args); | |
169 | int (*ctrl_str) (EVP_KDF_IMPL *impl, const char *type, const char *value); | |
170 | size_t (*size) (EVP_KDF_IMPL *impl); | |
171 | int (*derive) (EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen); | |
d2ba8123 | 172 | }; |
5a285add | 173 | |
d2ba8123 SL |
174 | extern const EVP_KDF pbkdf2_kdf_meth; |
175 | extern const EVP_KDF scrypt_kdf_meth; | |
176 | extern const EVP_KDF tls1_prf_kdf_meth; | |
177 | extern const EVP_KDF hkdf_kdf_meth; | |
178 | extern const EVP_KDF sshkdf_kdf_meth; | |
179 | extern const EVP_KDF ss_kdf_meth; | |
8bbeaaa4 | 180 | extern const EVP_KDF x963_kdf_meth; |
1aec7716 | 181 | extern const EVP_KDF x942_kdf_meth; |
5a285add | 182 | |
2db6bf6f | 183 | struct evp_md_st { |
3653d0c2 | 184 | /* nid */ |
2db6bf6f | 185 | int type; |
3653d0c2 MC |
186 | |
187 | /* Legacy structure members */ | |
188 | /* TODO(3.0): Remove these */ | |
2db6bf6f RL |
189 | int pkey_type; |
190 | int md_size; | |
191 | unsigned long flags; | |
192 | int (*init) (EVP_MD_CTX *ctx); | |
193 | int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); | |
194 | int (*final) (EVP_MD_CTX *ctx, unsigned char *md); | |
195 | int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from); | |
196 | int (*cleanup) (EVP_MD_CTX *ctx); | |
197 | int block_size; | |
198 | int ctx_size; /* how big does the ctx->md_data need to be */ | |
199 | /* control function */ | |
200 | int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2); | |
3653d0c2 MC |
201 | |
202 | /* New structure members */ | |
203 | /* TODO(3.0): Remove above comment when legacy has gone */ | |
6b9e3724 | 204 | char *name; |
3653d0c2 MC |
205 | OSSL_PROVIDER *prov; |
206 | CRYPTO_REF_COUNT refcnt; | |
207 | CRYPTO_RWLOCK *lock; | |
208 | OSSL_OP_digest_newctx_fn *newctx; | |
209 | OSSL_OP_digest_init_fn *dinit; | |
210 | OSSL_OP_digest_update_fn *dupdate; | |
211 | OSSL_OP_digest_final_fn *dfinal; | |
212 | OSSL_OP_digest_digest_fn *digest; | |
213 | OSSL_OP_digest_freectx_fn *freectx; | |
8c8cf0d9 | 214 | OSSL_OP_digest_dupctx_fn *dupctx; |
d5e5e2ff | 215 | OSSL_OP_digest_get_params_fn *get_params; |
2893111f RL |
216 | OSSL_OP_digest_ctx_set_params_fn *ctx_set_params; |
217 | OSSL_OP_digest_ctx_get_params_fn *ctx_get_params; | |
ae3ff60e RL |
218 | OSSL_OP_digest_gettable_params_fn *gettable_params; |
219 | OSSL_OP_digest_settable_ctx_params_fn *settable_ctx_params; | |
220 | OSSL_OP_digest_gettable_ctx_params_fn *gettable_ctx_params; | |
3653d0c2 | 221 | |
2db6bf6f RL |
222 | } /* EVP_MD */ ; |
223 | ||
e79f8773 RL |
224 | struct evp_cipher_st { |
225 | int nid; | |
df05f2ce | 226 | |
e79f8773 RL |
227 | int block_size; |
228 | /* Default value for variable length ciphers */ | |
229 | int key_len; | |
230 | int iv_len; | |
df05f2ce MC |
231 | |
232 | /* Legacy structure members */ | |
233 | /* TODO(3.0): Remove these */ | |
e79f8773 RL |
234 | /* Various flags */ |
235 | unsigned long flags; | |
236 | /* init key */ | |
237 | int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
238 | const unsigned char *iv, int enc); | |
239 | /* encrypt/decrypt data */ | |
240 | int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out, | |
241 | const unsigned char *in, size_t inl); | |
242 | /* cleanup ctx */ | |
243 | int (*cleanup) (EVP_CIPHER_CTX *); | |
244 | /* how big ctx->cipher_data needs to be */ | |
245 | int ctx_size; | |
246 | /* Populate a ASN1_TYPE with parameters */ | |
247 | int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); | |
248 | /* Get parameters from a ASN1_TYPE */ | |
249 | int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); | |
250 | /* Miscellaneous operations */ | |
251 | int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr); | |
252 | /* Application data */ | |
253 | void *app_data; | |
df05f2ce MC |
254 | |
255 | /* New structure members */ | |
256 | /* TODO(3.0): Remove above comment when legacy has gone */ | |
6b9e3724 | 257 | char *name; |
df05f2ce MC |
258 | OSSL_PROVIDER *prov; |
259 | CRYPTO_REF_COUNT refcnt; | |
260 | CRYPTO_RWLOCK *lock; | |
261 | OSSL_OP_cipher_newctx_fn *newctx; | |
262 | OSSL_OP_cipher_encrypt_init_fn *einit; | |
263 | OSSL_OP_cipher_decrypt_init_fn *dinit; | |
264 | OSSL_OP_cipher_update_fn *cupdate; | |
265 | OSSL_OP_cipher_final_fn *cfinal; | |
718b133a | 266 | OSSL_OP_cipher_cipher_fn *ccipher; |
df05f2ce MC |
267 | OSSL_OP_cipher_freectx_fn *freectx; |
268 | OSSL_OP_cipher_dupctx_fn *dupctx; | |
df05f2ce | 269 | OSSL_OP_cipher_get_params_fn *get_params; |
718b133a MC |
270 | OSSL_OP_cipher_ctx_get_params_fn *ctx_get_params; |
271 | OSSL_OP_cipher_ctx_set_params_fn *ctx_set_params; | |
ae3ff60e RL |
272 | OSSL_OP_cipher_gettable_params_fn *gettable_params; |
273 | OSSL_OP_cipher_gettable_ctx_params_fn *gettable_ctx_params; | |
274 | OSSL_OP_cipher_settable_ctx_params_fn *settable_ctx_params; | |
e79f8773 RL |
275 | } /* EVP_CIPHER */ ; |
276 | ||
277 | /* Macros to code block cipher wrappers */ | |
278 | ||
279 | /* Wrapper functions for each cipher mode */ | |
280 | ||
44ab2dfd MC |
281 | #define EVP_C_DATA(kstruct, ctx) \ |
282 | ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx)) | |
e79f8773 RL |
283 | |
284 | #define BLOCK_CIPHER_ecb_loop() \ | |
285 | size_t i, bl; \ | |
286 | bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \ | |
e8aa8b6c | 287 | if (inl < bl) return 1;\ |
e79f8773 | 288 | inl -= bl; \ |
e8aa8b6c | 289 | for (i=0; i <= inl; i+=bl) |
e79f8773 RL |
290 | |
291 | #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | |
292 | static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
293 | {\ | |
294 | BLOCK_CIPHER_ecb_loop() \ | |
295 | cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \ | |
296 | return 1;\ | |
297 | } | |
298 | ||
299 | #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2)) | |
300 | ||
301 | #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ | |
302 | static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
303 | {\ | |
304 | while(inl>=EVP_MAXCHUNK) {\ | |
305 | int num = EVP_CIPHER_CTX_num(ctx);\ | |
306 | cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \ | |
307 | EVP_CIPHER_CTX_set_num(ctx, num);\ | |
308 | inl-=EVP_MAXCHUNK;\ | |
309 | in +=EVP_MAXCHUNK;\ | |
310 | out+=EVP_MAXCHUNK;\ | |
311 | }\ | |
312 | if (inl) {\ | |
313 | int num = EVP_CIPHER_CTX_num(ctx);\ | |
314 | cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \ | |
315 | EVP_CIPHER_CTX_set_num(ctx, num);\ | |
316 | }\ | |
317 | return 1;\ | |
318 | } | |
319 | ||
320 | #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | |
321 | static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
322 | {\ | |
323 | while(inl>=EVP_MAXCHUNK) \ | |
324 | {\ | |
325 | cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\ | |
326 | inl-=EVP_MAXCHUNK;\ | |
327 | in +=EVP_MAXCHUNK;\ | |
328 | out+=EVP_MAXCHUNK;\ | |
329 | }\ | |
330 | if (inl)\ | |
331 | cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\ | |
332 | return 1;\ | |
333 | } | |
334 | ||
335 | #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | |
336 | static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
337 | {\ | |
e8aa8b6c F |
338 | size_t chunk = EVP_MAXCHUNK;\ |
339 | if (cbits == 1) chunk >>= 3;\ | |
340 | if (inl < chunk) chunk = inl;\ | |
341 | while (inl && inl >= chunk)\ | |
342 | {\ | |
343 | int num = EVP_CIPHER_CTX_num(ctx);\ | |
344 | cprefix##_cfb##cbits##_encrypt(in, out, (long) \ | |
345 | ((cbits == 1) \ | |
346 | && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \ | |
604e591e | 347 | ? chunk*8 : chunk), \ |
e8aa8b6c F |
348 | &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx),\ |
349 | &num, EVP_CIPHER_CTX_encrypting(ctx));\ | |
350 | EVP_CIPHER_CTX_set_num(ctx, num);\ | |
351 | inl -= chunk;\ | |
352 | in += chunk;\ | |
353 | out += chunk;\ | |
354 | if (inl < chunk) chunk = inl;\ | |
355 | }\ | |
356 | return 1;\ | |
e79f8773 RL |
357 | } |
358 | ||
359 | #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ | |
360 | BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | |
361 | BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | |
362 | BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | |
363 | BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) | |
364 | ||
365 | #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \ | |
366 | key_len, iv_len, flags, init_key, cleanup, \ | |
367 | set_asn1, get_asn1, ctrl) \ | |
368 | static const EVP_CIPHER cname##_##mode = { \ | |
369 | nid##_##nmode, block_size, key_len, iv_len, \ | |
370 | flags | EVP_CIPH_##MODE##_MODE, \ | |
371 | init_key, \ | |
372 | cname##_##mode##_cipher, \ | |
373 | cleanup, \ | |
374 | sizeof(kstruct), \ | |
375 | set_asn1, get_asn1,\ | |
376 | ctrl, \ | |
377 | NULL \ | |
378 | }; \ | |
379 | const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; } | |
380 | ||
381 | #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \ | |
382 | iv_len, flags, init_key, cleanup, set_asn1, \ | |
383 | get_asn1, ctrl) \ | |
384 | BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \ | |
385 | iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | |
386 | ||
387 | #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \ | |
388 | iv_len, cbits, flags, init_key, cleanup, \ | |
389 | set_asn1, get_asn1, ctrl) \ | |
390 | BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \ | |
391 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ | |
392 | get_asn1, ctrl) | |
393 | ||
394 | #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \ | |
395 | iv_len, cbits, flags, init_key, cleanup, \ | |
396 | set_asn1, get_asn1, ctrl) \ | |
397 | BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \ | |
398 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ | |
399 | get_asn1, ctrl) | |
400 | ||
401 | #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ | |
402 | flags, init_key, cleanup, set_asn1, \ | |
403 | get_asn1, ctrl) \ | |
404 | BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ | |
405 | 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | |
406 | ||
407 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | |
408 | nid, block_size, key_len, iv_len, cbits, flags, \ | |
409 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
410 | BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \ | |
411 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
412 | BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |
413 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
414 | BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |
415 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
416 | BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \ | |
417 | init_key, cleanup, set_asn1, get_asn1, ctrl) | |
418 | ||
419 | /*- | |
420 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | |
421 | nid, block_size, key_len, iv_len, flags,\ | |
422 | init_key, cleanup, set_asn1, get_asn1, ctrl)\ | |
423 | static const EVP_CIPHER cname##_cbc = {\ | |
424 | nid##_cbc, block_size, key_len, iv_len, \ | |
425 | flags | EVP_CIPH_CBC_MODE,\ | |
426 | init_key,\ | |
427 | cname##_cbc_cipher,\ | |
428 | cleanup,\ | |
429 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
430 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
431 | set_asn1, get_asn1,\ | |
432 | ctrl, \ | |
433 | NULL \ | |
434 | };\ | |
435 | const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\ | |
436 | static const EVP_CIPHER cname##_cfb = {\ | |
437 | nid##_cfb64, 1, key_len, iv_len, \ | |
438 | flags | EVP_CIPH_CFB_MODE,\ | |
439 | init_key,\ | |
440 | cname##_cfb_cipher,\ | |
441 | cleanup,\ | |
442 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
443 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
444 | set_asn1, get_asn1,\ | |
445 | ctrl,\ | |
446 | NULL \ | |
447 | };\ | |
448 | const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\ | |
449 | static const EVP_CIPHER cname##_ofb = {\ | |
450 | nid##_ofb64, 1, key_len, iv_len, \ | |
451 | flags | EVP_CIPH_OFB_MODE,\ | |
452 | init_key,\ | |
453 | cname##_ofb_cipher,\ | |
454 | cleanup,\ | |
455 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
456 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
457 | set_asn1, get_asn1,\ | |
458 | ctrl,\ | |
459 | NULL \ | |
460 | };\ | |
461 | const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\ | |
462 | static const EVP_CIPHER cname##_ecb = {\ | |
463 | nid##_ecb, block_size, key_len, iv_len, \ | |
464 | flags | EVP_CIPH_ECB_MODE,\ | |
465 | init_key,\ | |
466 | cname##_ecb_cipher,\ | |
467 | cleanup,\ | |
468 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
469 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
470 | set_asn1, get_asn1,\ | |
471 | ctrl,\ | |
472 | NULL \ | |
473 | };\ | |
474 | const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } | |
475 | */ | |
476 | ||
477 | #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \ | |
478 | block_size, key_len, iv_len, cbits, \ | |
479 | flags, init_key, \ | |
480 | cleanup, set_asn1, get_asn1, ctrl) \ | |
481 | BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ | |
482 | BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \ | |
483 | cbits, flags, init_key, cleanup, set_asn1, \ | |
484 | get_asn1, ctrl) | |
485 | ||
486 | #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \ | |
487 | BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ | |
488 | BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ | |
489 | NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ | |
490 | (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \ | |
491 | cipher##_init_key, NULL, NULL, NULL, NULL) | |
492 | ||
3aeb9348 | 493 | |
13735cfe MC |
494 | # ifndef OPENSSL_NO_EC |
495 | ||
496 | #define X25519_KEYLEN 32 | |
497 | #define X448_KEYLEN 56 | |
498 | #define ED448_KEYLEN 57 | |
499 | ||
500 | #define MAX_KEYLEN ED448_KEYLEN | |
501 | ||
502 | typedef struct { | |
503 | unsigned char pubkey[MAX_KEYLEN]; | |
504 | unsigned char *privkey; | |
505 | } ECX_KEY; | |
506 | ||
507 | #endif | |
508 | ||
3aeb9348 DSH |
509 | /* |
510 | * Type needs to be a bit field Sub-type needs to be for variations on the | |
511 | * method, as in, can it do arbitrary encryption.... | |
512 | */ | |
513 | struct evp_pkey_st { | |
a94a3e0d | 514 | /* == Legacy attributes == */ |
3aeb9348 DSH |
515 | int type; |
516 | int save_type; | |
3aeb9348 DSH |
517 | const EVP_PKEY_ASN1_METHOD *ameth; |
518 | ENGINE *engine; | |
d19b01ad | 519 | ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */ |
3aeb9348 | 520 | union { |
a4cb54d2 | 521 | void *ptr; |
3aeb9348 DSH |
522 | # ifndef OPENSSL_NO_RSA |
523 | struct rsa_st *rsa; /* RSA */ | |
524 | # endif | |
525 | # ifndef OPENSSL_NO_DSA | |
526 | struct dsa_st *dsa; /* DSA */ | |
527 | # endif | |
528 | # ifndef OPENSSL_NO_DH | |
529 | struct dh_st *dh; /* DH */ | |
530 | # endif | |
531 | # ifndef OPENSSL_NO_EC | |
532 | struct ec_key_st *ec; /* ECC */ | |
13735cfe | 533 | ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */ |
3aeb9348 DSH |
534 | # endif |
535 | } pkey; | |
a94a3e0d RL |
536 | |
537 | /* == Common attributes == */ | |
538 | CRYPTO_REF_COUNT references; | |
03273d61 | 539 | CRYPTO_RWLOCK *lock; |
a94a3e0d RL |
540 | STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */ |
541 | int save_parameters; | |
542 | ||
543 | /* == Provider attributes == */ | |
544 | /* | |
545 | * To support transparent export/import between providers that | |
546 | * support the methods for it, and still not having to do the | |
547 | * export/import every time a key is used, we maintain a cache | |
548 | * of imported key, indexed by provider address. | |
549 | * pkeys[0] is *always* the "original" key. | |
550 | */ | |
551 | struct { | |
552 | EVP_KEYMGMT *keymgmt; | |
553 | void *provkey; | |
554 | } pkeys[10]; | |
70a1f7b4 RL |
555 | /* |
556 | * If there is a legacy key assigned to this structure, we keep | |
557 | * a copy of that key's dirty count. | |
558 | */ | |
559 | size_t dirty_cnt_copy; | |
3aeb9348 | 560 | } /* EVP_PKEY */ ; |
7b9f8f7f MC |
561 | |
562 | ||
b3599dbb MC |
563 | void openssl_add_all_ciphers_int(void); |
564 | void openssl_add_all_digests_int(void); | |
0145dd32 | 565 | void openssl_add_all_macs_int(void); |
d2ba8123 | 566 | void openssl_add_all_kdfs_int(void); |
b3599dbb | 567 | void evp_cleanup_int(void); |
0822e89a | 568 | void evp_app_cleanup_int(void); |
9d6fcd42 | 569 | |
4cae07fe | 570 | /* KEYMGMT helper functions */ |
70a1f7b4 | 571 | void *evp_keymgmt_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt); |
4cae07fe | 572 | void evp_keymgmt_clear_pkey_cache(EVP_PKEY *pk); |
70a1f7b4 | 573 | |
fa9faf01 RL |
574 | /* KEYMGMT provider interface functions */ |
575 | void *evp_keymgmt_importdomparams(const EVP_KEYMGMT *keymgmt, | |
576 | const OSSL_PARAM params[]); | |
577 | void *evp_keymgmt_gendomparams(const EVP_KEYMGMT *keymgmt, | |
578 | const OSSL_PARAM params[]); | |
579 | void evp_keymgmt_freedomparams(const EVP_KEYMGMT *keymgmt, | |
580 | void *provdomparams); | |
581 | int evp_keymgmt_exportdomparams(const EVP_KEYMGMT *keymgmt, | |
582 | void *provdomparams, OSSL_PARAM params[]); | |
583 | const OSSL_PARAM * | |
584 | evp_keymgmt_importdomparam_types(const EVP_KEYMGMT *keymgmt); | |
585 | const OSSL_PARAM * | |
586 | evp_keymgmt_exportdomparam_types(const EVP_KEYMGMT *keymgmt); | |
587 | ||
588 | void *evp_keymgmt_importkey(const EVP_KEYMGMT *keymgmt, | |
589 | const OSSL_PARAM params[]); | |
590 | void *evp_keymgmt_genkey(const EVP_KEYMGMT *keymgmt, void *domparams, | |
591 | const OSSL_PARAM params[]); | |
592 | void *evp_keymgmt_loadkey(const EVP_KEYMGMT *keymgmt, | |
593 | void *id, size_t idlen); | |
594 | void evp_keymgmt_freekey(const EVP_KEYMGMT *keymgmt, void *provkey); | |
595 | int evp_keymgmt_exportkey(const EVP_KEYMGMT *keymgmt, | |
596 | void *provkey, OSSL_PARAM params[]); | |
597 | const OSSL_PARAM *evp_keymgmt_importkey_types(const EVP_KEYMGMT *keymgmt); | |
598 | const OSSL_PARAM *evp_keymgmt_exportkey_types(const EVP_KEYMGMT *keymgmt); | |
599 | ||
46f4e1be | 600 | /* Pulling defines out of C source files */ |
9d6fcd42 TS |
601 | |
602 | #define EVP_RC4_KEY_SIZE 16 | |
603 | #ifndef TLS1_1_VERSION | |
604 | # define TLS1_1_VERSION 0x0302 | |
605 | #endif | |
c0804614 MC |
606 | |
607 | void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags); | |
608 | ||
609 | /* EVP_ENCODE_CTX flags */ | |
3fd59700 MC |
610 | /* Don't generate new lines when encoding */ |
611 | #define EVP_ENCODE_CTX_NO_NEWLINES 1 | |
612 | /* Use the SRP base64 alphabet instead of the standard one */ | |
613 | #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2 |