]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/include/internal/evp_int.h
Implement AES CBC ciphers in the default provider
[thirdparty/openssl.git] / crypto / include / internal / evp_int.h
1 /*
2 * Copyright 2015-2018 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 #include <openssl/evp.h>
11 #include <openssl/core_numbers.h>
12 #include "internal/refcount.h"
13
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
20 struct evp_pkey_ctx_st {
21 /* Method associated with this operation */
22 const EVP_PKEY_METHOD *pmeth;
23 /* Engine that implements this method or NULL if builtin */
24 ENGINE *engine;
25 /* Key: may be NULL */
26 EVP_PKEY *pkey;
27 /* Peer key for key agreement, may be NULL */
28 EVP_PKEY *peerkey;
29 /* Actual operation */
30 int operation;
31 /* Algorithm specific data */
32 void *data;
33 /* Application specific data */
34 void *app_data;
35 /* Keygen callback */
36 EVP_PKEY_gen_cb *pkey_gencb;
37 /* implementation specific keygen data */
38 int *keygen_info;
39 int keygen_info_count;
40 } /* EVP_PKEY_CTX */ ;
41
42 #define EVP_PKEY_FLAG_DYNAMIC 1
43
44 struct evp_pkey_method_st {
45 int pkey_id;
46 int flags;
47 int (*init) (EVP_PKEY_CTX *ctx);
48 int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
49 void (*cleanup) (EVP_PKEY_CTX *ctx);
50 int (*paramgen_init) (EVP_PKEY_CTX *ctx);
51 int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
52 int (*keygen_init) (EVP_PKEY_CTX *ctx);
53 int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
54 int (*sign_init) (EVP_PKEY_CTX *ctx);
55 int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
56 const unsigned char *tbs, size_t tbslen);
57 int (*verify_init) (EVP_PKEY_CTX *ctx);
58 int (*verify) (EVP_PKEY_CTX *ctx,
59 const unsigned char *sig, size_t siglen,
60 const unsigned char *tbs, size_t tbslen);
61 int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
62 int (*verify_recover) (EVP_PKEY_CTX *ctx,
63 unsigned char *rout, size_t *routlen,
64 const unsigned char *sig, size_t siglen);
65 int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
66 int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
67 EVP_MD_CTX *mctx);
68 int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
69 int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
70 EVP_MD_CTX *mctx);
71 int (*encrypt_init) (EVP_PKEY_CTX *ctx);
72 int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
73 const unsigned char *in, size_t inlen);
74 int (*decrypt_init) (EVP_PKEY_CTX *ctx);
75 int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
76 const unsigned char *in, size_t inlen);
77 int (*derive_init) (EVP_PKEY_CTX *ctx);
78 int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
79 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
80 int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
81 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
82 const unsigned char *tbs, size_t tbslen);
83 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
84 size_t siglen, const unsigned char *tbs,
85 size_t tbslen);
86 int (*check) (EVP_PKEY *pkey);
87 int (*public_check) (EVP_PKEY *pkey);
88 int (*param_check) (EVP_PKEY *pkey);
89
90 int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
91 } /* EVP_PKEY_METHOD */ ;
92
93 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
94
95 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
96
97 extern const EVP_PKEY_METHOD cmac_pkey_meth;
98 extern const EVP_PKEY_METHOD dh_pkey_meth;
99 extern const EVP_PKEY_METHOD dhx_pkey_meth;
100 extern const EVP_PKEY_METHOD dsa_pkey_meth;
101 extern const EVP_PKEY_METHOD ec_pkey_meth;
102 extern const EVP_PKEY_METHOD sm2_pkey_meth;
103 extern const EVP_PKEY_METHOD ecx25519_pkey_meth;
104 extern const EVP_PKEY_METHOD ecx448_pkey_meth;
105 extern const EVP_PKEY_METHOD ed25519_pkey_meth;
106 extern const EVP_PKEY_METHOD ed448_pkey_meth;
107 extern const EVP_PKEY_METHOD hmac_pkey_meth;
108 extern const EVP_PKEY_METHOD rsa_pkey_meth;
109 extern const EVP_PKEY_METHOD rsa_pss_pkey_meth;
110 extern const EVP_PKEY_METHOD scrypt_pkey_meth;
111 extern const EVP_PKEY_METHOD tls1_prf_pkey_meth;
112 extern const EVP_PKEY_METHOD hkdf_pkey_meth;
113 extern const EVP_PKEY_METHOD poly1305_pkey_meth;
114 extern const EVP_PKEY_METHOD siphash_pkey_meth;
115
116 /* struct evp_mac_impl_st is defined by the implementation */
117 typedef struct evp_mac_impl_st EVP_MAC_IMPL;
118 struct evp_mac_st {
119 int type;
120 EVP_MAC_IMPL *(*new) (void);
121 int (*copy) (EVP_MAC_IMPL *macdst, EVP_MAC_IMPL *macsrc);
122 void (*free) (EVP_MAC_IMPL *macctx);
123 size_t (*size) (EVP_MAC_IMPL *macctx);
124 int (*init) (EVP_MAC_IMPL *macctx);
125 int (*update) (EVP_MAC_IMPL *macctx, const unsigned char *data,
126 size_t datalen);
127 int (*final) (EVP_MAC_IMPL *macctx, unsigned char *out);
128 int (*ctrl) (EVP_MAC_IMPL *macctx, int cmd, va_list args);
129 int (*ctrl_str) (EVP_MAC_IMPL *macctx, const char *type, const char *value);
130 };
131
132 extern const EVP_MAC blake2b_mac_meth;
133 extern const EVP_MAC blake2s_mac_meth;
134 extern const EVP_MAC cmac_meth;
135 extern const EVP_MAC gmac_meth;
136 extern const EVP_MAC hmac_meth;
137 extern const EVP_MAC kmac128_meth;
138 extern const EVP_MAC kmac256_meth;
139 extern const EVP_MAC siphash_meth;
140 extern const EVP_MAC poly1305_meth;
141
142 /* Internal keccak algorithms used for KMAC */
143 const EVP_MD *evp_keccak_kmac128(void);
144 const EVP_MD *evp_keccak_kmac256(void);
145
146 /*
147 * This function is internal for now, but can be made external when needed.
148 * The documentation would read:
149 *
150 * EVP_add_mac() adds the MAC implementation C<mac> to the internal
151 * object database.
152 */
153 int EVP_add_mac(const EVP_MAC *mac);
154
155 /* struct evp_kdf_impl_st is defined by the implementation */
156 typedef struct evp_kdf_impl_st EVP_KDF_IMPL;
157 typedef struct {
158 int type;
159 EVP_KDF_IMPL *(*new) (void);
160 void (*free) (EVP_KDF_IMPL *impl);
161 void (*reset) (EVP_KDF_IMPL *impl);
162 int (*ctrl) (EVP_KDF_IMPL *impl, int cmd, va_list args);
163 int (*ctrl_str) (EVP_KDF_IMPL *impl, const char *type, const char *value);
164 size_t (*size) (EVP_KDF_IMPL *impl);
165 int (*derive) (EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen);
166 } EVP_KDF_METHOD;
167
168 extern const EVP_KDF_METHOD pbkdf2_kdf_meth;
169 extern const EVP_KDF_METHOD scrypt_kdf_meth;
170 extern const EVP_KDF_METHOD tls1_prf_kdf_meth;
171 extern const EVP_KDF_METHOD hkdf_kdf_meth;
172 extern const EVP_KDF_METHOD sshkdf_kdf_meth;
173 extern const EVP_KDF_METHOD ss_kdf_meth;
174
175 struct evp_md_st {
176 /* nid */
177 int type;
178
179 /* Legacy structure members */
180 /* TODO(3.0): Remove these */
181 int pkey_type;
182 int md_size;
183 unsigned long flags;
184 int (*init) (EVP_MD_CTX *ctx);
185 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
186 int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
187 int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
188 int (*cleanup) (EVP_MD_CTX *ctx);
189 int block_size;
190 int ctx_size; /* how big does the ctx->md_data need to be */
191 /* control function */
192 int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
193
194 /* New structure members */
195 /* TODO(3.0): Remove above comment when legacy has gone */
196 OSSL_PROVIDER *prov;
197 CRYPTO_REF_COUNT refcnt;
198 CRYPTO_RWLOCK *lock;
199 OSSL_OP_digest_newctx_fn *newctx;
200 OSSL_OP_digest_init_fn *dinit;
201 OSSL_OP_digest_update_fn *dupdate;
202 OSSL_OP_digest_final_fn *dfinal;
203 OSSL_OP_digest_digest_fn *digest;
204 OSSL_OP_digest_freectx_fn *freectx;
205 OSSL_OP_digest_dupctx_fn *dupctx;
206 OSSL_OP_digest_size_fn *size;
207 OSSL_OP_digest_block_size_fn *dblock_size;
208
209 } /* EVP_MD */ ;
210
211 struct evp_cipher_st {
212 int nid;
213
214 int block_size;
215 /* Default value for variable length ciphers */
216 int key_len;
217 int iv_len;
218
219 /* Legacy structure members */
220 /* TODO(3.0): Remove these */
221 /* Various flags */
222 unsigned long flags;
223 /* init key */
224 int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
225 const unsigned char *iv, int enc);
226 /* encrypt/decrypt data */
227 int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
228 const unsigned char *in, size_t inl);
229 /* cleanup ctx */
230 int (*cleanup) (EVP_CIPHER_CTX *);
231 /* how big ctx->cipher_data needs to be */
232 int ctx_size;
233 /* Populate a ASN1_TYPE with parameters */
234 int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
235 /* Get parameters from a ASN1_TYPE */
236 int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
237 /* Miscellaneous operations */
238 int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
239 /* Application data */
240 void *app_data;
241
242 /* New structure members */
243 /* TODO(3.0): Remove above comment when legacy has gone */
244 OSSL_PROVIDER *prov;
245 CRYPTO_REF_COUNT refcnt;
246 CRYPTO_RWLOCK *lock;
247 OSSL_OP_cipher_newctx_fn *newctx;
248 OSSL_OP_cipher_encrypt_init_fn *einit;
249 OSSL_OP_cipher_decrypt_init_fn *dinit;
250 OSSL_OP_cipher_update_fn *cupdate;
251 OSSL_OP_cipher_final_fn *cfinal;
252 OSSL_OP_cipher_cipher_fn *ccipher;
253 OSSL_OP_cipher_freectx_fn *freectx;
254 OSSL_OP_cipher_dupctx_fn *dupctx;
255 OSSL_OP_cipher_key_length_fn *key_length;
256 OSSL_OP_cipher_iv_length_fn *iv_length;
257 OSSL_OP_cipher_block_size_fn *blocksize;
258 OSSL_OP_cipher_get_params_fn *get_params;
259 OSSL_OP_cipher_ctx_get_params_fn *ctx_get_params;
260 OSSL_OP_cipher_ctx_set_params_fn *ctx_set_params;
261 } /* EVP_CIPHER */ ;
262
263 /* Macros to code block cipher wrappers */
264
265 /* Wrapper functions for each cipher mode */
266
267 #define EVP_C_DATA(kstruct, ctx) \
268 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
269
270 #define BLOCK_CIPHER_ecb_loop() \
271 size_t i, bl; \
272 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
273 if (inl < bl) return 1;\
274 inl -= bl; \
275 for (i=0; i <= inl; i+=bl)
276
277 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
278 static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
279 {\
280 BLOCK_CIPHER_ecb_loop() \
281 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
282 return 1;\
283 }
284
285 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
286
287 #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
288 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
289 {\
290 while(inl>=EVP_MAXCHUNK) {\
291 int num = EVP_CIPHER_CTX_num(ctx);\
292 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
293 EVP_CIPHER_CTX_set_num(ctx, num);\
294 inl-=EVP_MAXCHUNK;\
295 in +=EVP_MAXCHUNK;\
296 out+=EVP_MAXCHUNK;\
297 }\
298 if (inl) {\
299 int num = EVP_CIPHER_CTX_num(ctx);\
300 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
301 EVP_CIPHER_CTX_set_num(ctx, num);\
302 }\
303 return 1;\
304 }
305
306 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
307 static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
308 {\
309 while(inl>=EVP_MAXCHUNK) \
310 {\
311 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));\
312 inl-=EVP_MAXCHUNK;\
313 in +=EVP_MAXCHUNK;\
314 out+=EVP_MAXCHUNK;\
315 }\
316 if (inl)\
317 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\
318 return 1;\
319 }
320
321 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
322 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
323 {\
324 size_t chunk = EVP_MAXCHUNK;\
325 if (cbits == 1) chunk >>= 3;\
326 if (inl < chunk) chunk = inl;\
327 while (inl && inl >= chunk)\
328 {\
329 int num = EVP_CIPHER_CTX_num(ctx);\
330 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
331 ((cbits == 1) \
332 && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
333 ? chunk*8 : chunk), \
334 &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx),\
335 &num, EVP_CIPHER_CTX_encrypting(ctx));\
336 EVP_CIPHER_CTX_set_num(ctx, num);\
337 inl -= chunk;\
338 in += chunk;\
339 out += chunk;\
340 if (inl < chunk) chunk = inl;\
341 }\
342 return 1;\
343 }
344
345 #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
346 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
347 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
348 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
349 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
350
351 #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
352 key_len, iv_len, flags, init_key, cleanup, \
353 set_asn1, get_asn1, ctrl) \
354 static const EVP_CIPHER cname##_##mode = { \
355 nid##_##nmode, block_size, key_len, iv_len, \
356 flags | EVP_CIPH_##MODE##_MODE, \
357 init_key, \
358 cname##_##mode##_cipher, \
359 cleanup, \
360 sizeof(kstruct), \
361 set_asn1, get_asn1,\
362 ctrl, \
363 NULL \
364 }; \
365 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
366
367 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
368 iv_len, flags, init_key, cleanup, set_asn1, \
369 get_asn1, ctrl) \
370 BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
371 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
372
373 #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
374 iv_len, cbits, flags, init_key, cleanup, \
375 set_asn1, get_asn1, ctrl) \
376 BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
377 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
378 get_asn1, ctrl)
379
380 #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
381 iv_len, cbits, flags, init_key, cleanup, \
382 set_asn1, get_asn1, ctrl) \
383 BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
384 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
385 get_asn1, ctrl)
386
387 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
388 flags, init_key, cleanup, set_asn1, \
389 get_asn1, ctrl) \
390 BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
391 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
392
393 #define BLOCK_CIPHER_defs(cname, kstruct, \
394 nid, block_size, key_len, iv_len, cbits, flags, \
395 init_key, cleanup, set_asn1, get_asn1, ctrl) \
396 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
397 init_key, cleanup, set_asn1, get_asn1, ctrl) \
398 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
399 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
400 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
401 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
402 BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
403 init_key, cleanup, set_asn1, get_asn1, ctrl)
404
405 /*-
406 #define BLOCK_CIPHER_defs(cname, kstruct, \
407 nid, block_size, key_len, iv_len, flags,\
408 init_key, cleanup, set_asn1, get_asn1, ctrl)\
409 static const EVP_CIPHER cname##_cbc = {\
410 nid##_cbc, block_size, key_len, iv_len, \
411 flags | EVP_CIPH_CBC_MODE,\
412 init_key,\
413 cname##_cbc_cipher,\
414 cleanup,\
415 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
416 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
417 set_asn1, get_asn1,\
418 ctrl, \
419 NULL \
420 };\
421 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
422 static const EVP_CIPHER cname##_cfb = {\
423 nid##_cfb64, 1, key_len, iv_len, \
424 flags | EVP_CIPH_CFB_MODE,\
425 init_key,\
426 cname##_cfb_cipher,\
427 cleanup,\
428 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
429 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
430 set_asn1, get_asn1,\
431 ctrl,\
432 NULL \
433 };\
434 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
435 static const EVP_CIPHER cname##_ofb = {\
436 nid##_ofb64, 1, key_len, iv_len, \
437 flags | EVP_CIPH_OFB_MODE,\
438 init_key,\
439 cname##_ofb_cipher,\
440 cleanup,\
441 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
442 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
443 set_asn1, get_asn1,\
444 ctrl,\
445 NULL \
446 };\
447 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
448 static const EVP_CIPHER cname##_ecb = {\
449 nid##_ecb, block_size, key_len, iv_len, \
450 flags | EVP_CIPH_ECB_MODE,\
451 init_key,\
452 cname##_ecb_cipher,\
453 cleanup,\
454 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
455 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
456 set_asn1, get_asn1,\
457 ctrl,\
458 NULL \
459 };\
460 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
461 */
462
463 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
464 block_size, key_len, iv_len, cbits, \
465 flags, init_key, \
466 cleanup, set_asn1, get_asn1, ctrl) \
467 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
468 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
469 cbits, flags, init_key, cleanup, set_asn1, \
470 get_asn1, ctrl)
471
472 #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
473 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
474 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
475 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
476 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
477 cipher##_init_key, NULL, NULL, NULL, NULL)
478
479
480 # ifndef OPENSSL_NO_EC
481
482 #define X25519_KEYLEN 32
483 #define X448_KEYLEN 56
484 #define ED448_KEYLEN 57
485
486 #define MAX_KEYLEN ED448_KEYLEN
487
488 typedef struct {
489 unsigned char pubkey[MAX_KEYLEN];
490 unsigned char *privkey;
491 } ECX_KEY;
492
493 #endif
494
495 /*
496 * Type needs to be a bit field Sub-type needs to be for variations on the
497 * method, as in, can it do arbitrary encryption....
498 */
499 struct evp_pkey_st {
500 int type;
501 int save_type;
502 CRYPTO_REF_COUNT references;
503 const EVP_PKEY_ASN1_METHOD *ameth;
504 ENGINE *engine;
505 ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
506 union {
507 void *ptr;
508 # ifndef OPENSSL_NO_RSA
509 struct rsa_st *rsa; /* RSA */
510 # endif
511 # ifndef OPENSSL_NO_DSA
512 struct dsa_st *dsa; /* DSA */
513 # endif
514 # ifndef OPENSSL_NO_DH
515 struct dh_st *dh; /* DH */
516 # endif
517 # ifndef OPENSSL_NO_EC
518 struct ec_key_st *ec; /* ECC */
519 ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
520 # endif
521 } pkey;
522 int save_parameters;
523 STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
524 CRYPTO_RWLOCK *lock;
525 } /* EVP_PKEY */ ;
526
527
528 void openssl_add_all_ciphers_int(void);
529 void openssl_add_all_digests_int(void);
530 void openssl_add_all_macs_int(void);
531 void evp_cleanup_int(void);
532 void evp_app_cleanup_int(void);
533
534 /* Pulling defines out of C source files */
535
536 #define EVP_RC4_KEY_SIZE 16
537 #ifndef TLS1_1_VERSION
538 # define TLS1_1_VERSION 0x0302
539 #endif
540
541 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
542
543 /* EVP_ENCODE_CTX flags */
544 /* Don't generate new lines when encoding */
545 #define EVP_ENCODE_CTX_NO_NEWLINES 1
546 /* Use the SRP base64 alphabet instead of the standard one */
547 #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2