2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include <openssl/evp.h>
11 #include "internal/refcount.h"
14 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
17 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
19 struct evp_pkey_ctx_st
{
20 /* Method associated with this operation */
21 const EVP_PKEY_METHOD
*pmeth
;
22 /* Engine that implements this method or NULL if builtin */
24 /* Key: may be NULL */
26 /* Peer key for key agreement, may be NULL */
28 /* Actual operation */
30 /* Algorithm specific data */
32 /* Application specific data */
35 EVP_PKEY_gen_cb
*pkey_gencb
;
36 /* implementation specific keygen data */
38 int keygen_info_count
;
39 } /* EVP_PKEY_CTX */ ;
41 #define EVP_PKEY_FLAG_DYNAMIC 1
43 struct evp_pkey_method_st
{
46 int (*init
) (EVP_PKEY_CTX
*ctx
);
47 int (*copy
) (EVP_PKEY_CTX
*dst
, EVP_PKEY_CTX
*src
);
48 void (*cleanup
) (EVP_PKEY_CTX
*ctx
);
49 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
);
50 int (*paramgen
) (EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
);
51 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
);
52 int (*keygen
) (EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
);
53 int (*sign_init
) (EVP_PKEY_CTX
*ctx
);
54 int (*sign
) (EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
55 const unsigned char *tbs
, size_t tbslen
);
56 int (*verify_init
) (EVP_PKEY_CTX
*ctx
);
57 int (*verify
) (EVP_PKEY_CTX
*ctx
,
58 const unsigned char *sig
, size_t siglen
,
59 const unsigned char *tbs
, size_t tbslen
);
60 int (*verify_recover_init
) (EVP_PKEY_CTX
*ctx
);
61 int (*verify_recover
) (EVP_PKEY_CTX
*ctx
,
62 unsigned char *rout
, size_t *routlen
,
63 const unsigned char *sig
, size_t siglen
);
64 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
65 int (*signctx
) (EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
67 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
68 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
, const unsigned char *sig
, int siglen
,
70 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
);
71 int (*encrypt
) (EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
72 const unsigned char *in
, size_t inlen
);
73 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
);
74 int (*decrypt
) (EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
75 const unsigned char *in
, size_t inlen
);
76 int (*derive_init
) (EVP_PKEY_CTX
*ctx
);
77 int (*derive
) (EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *keylen
);
78 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
);
79 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
, const char *type
, const char *value
);
80 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
81 const unsigned char *tbs
, size_t tbslen
);
82 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
83 size_t siglen
, const unsigned char *tbs
,
85 int (*check
) (EVP_PKEY
*pkey
);
86 int (*public_check
) (EVP_PKEY
*pkey
);
87 int (*param_check
) (EVP_PKEY
*pkey
);
89 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
90 } /* EVP_PKEY_METHOD */ ;
92 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD
)
94 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
);
96 extern const EVP_PKEY_METHOD cmac_pkey_meth
;
97 extern const EVP_PKEY_METHOD dh_pkey_meth
;
98 extern const EVP_PKEY_METHOD dhx_pkey_meth
;
99 extern const EVP_PKEY_METHOD dsa_pkey_meth
;
100 extern const EVP_PKEY_METHOD ec_pkey_meth
;
101 extern const EVP_PKEY_METHOD sm2_pkey_meth
;
102 extern const EVP_PKEY_METHOD ecx25519_pkey_meth
;
103 extern const EVP_PKEY_METHOD ecx448_pkey_meth
;
104 extern const EVP_PKEY_METHOD ed25519_pkey_meth
;
105 extern const EVP_PKEY_METHOD ed448_pkey_meth
;
106 extern const EVP_PKEY_METHOD hmac_pkey_meth
;
107 extern const EVP_PKEY_METHOD rsa_pkey_meth
;
108 extern const EVP_PKEY_METHOD rsa_pss_pkey_meth
;
109 extern const EVP_PKEY_METHOD scrypt_pkey_meth
;
110 extern const EVP_PKEY_METHOD tls1_prf_pkey_meth
;
111 extern const EVP_PKEY_METHOD hkdf_pkey_meth
;
112 extern const EVP_PKEY_METHOD poly1305_pkey_meth
;
113 extern const EVP_PKEY_METHOD siphash_pkey_meth
;
115 /* struct evp_mac_impl_st is defined by the implementation */
116 typedef struct evp_mac_impl_st EVP_MAC_IMPL
;
119 EVP_MAC_IMPL
*(*new) (void);
120 int (*copy
) (EVP_MAC_IMPL
*macdst
, EVP_MAC_IMPL
*macsrc
);
121 void (*free
) (EVP_MAC_IMPL
*macctx
);
122 size_t (*size
) (EVP_MAC_IMPL
*macctx
);
123 int (*init
) (EVP_MAC_IMPL
*macctx
);
124 int (*update
) (EVP_MAC_IMPL
*macctx
, const unsigned char *data
,
126 int (*final
) (EVP_MAC_IMPL
*macctx
, unsigned char *out
);
127 int (*ctrl
) (EVP_MAC_IMPL
*macctx
, int cmd
, va_list args
);
128 int (*ctrl_str
) (EVP_MAC_IMPL
*macctx
, const char *type
, const char *value
);
131 extern const EVP_MAC cmac_meth
;
132 extern const EVP_MAC gmac_meth
;
133 extern const EVP_MAC hmac_meth
;
134 extern const EVP_MAC kmac128_meth
;
135 extern const EVP_MAC kmac256_meth
;
136 extern const EVP_MAC siphash_meth
;
137 extern const EVP_MAC poly1305_meth
;
139 /* Internal keccak algorithms used for KMAC */
140 const EVP_MD
*evp_keccak_kmac128(void);
141 const EVP_MD
*evp_keccak_kmac256(void);
144 * This function is internal for now, but can be made external when needed.
145 * The documentation would read:
147 * EVP_add_mac() adds the MAC implementation C<mac> to the internal
150 int EVP_add_mac(const EVP_MAC
*mac
);
157 int (*init
) (EVP_MD_CTX
*ctx
);
158 int (*update
) (EVP_MD_CTX
*ctx
, const void *data
, size_t count
);
159 int (*final
) (EVP_MD_CTX
*ctx
, unsigned char *md
);
160 int (*copy
) (EVP_MD_CTX
*to
, const EVP_MD_CTX
*from
);
161 int (*cleanup
) (EVP_MD_CTX
*ctx
);
163 int ctx_size
; /* how big does the ctx->md_data need to be */
164 /* control function */
165 int (*md_ctrl
) (EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
);
168 struct evp_cipher_st
{
171 /* Default value for variable length ciphers */
177 int (*init
) (EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
178 const unsigned char *iv
, int enc
);
179 /* encrypt/decrypt data */
180 int (*do_cipher
) (EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
181 const unsigned char *in
, size_t inl
);
183 int (*cleanup
) (EVP_CIPHER_CTX
*);
184 /* how big ctx->cipher_data needs to be */
186 /* Populate a ASN1_TYPE with parameters */
187 int (*set_asn1_parameters
) (EVP_CIPHER_CTX
*, ASN1_TYPE
*);
188 /* Get parameters from a ASN1_TYPE */
189 int (*get_asn1_parameters
) (EVP_CIPHER_CTX
*, ASN1_TYPE
*);
190 /* Miscellaneous operations */
191 int (*ctrl
) (EVP_CIPHER_CTX
*, int type
, int arg
, void *ptr
);
192 /* Application data */
196 /* Macros to code block cipher wrappers */
198 /* Wrapper functions for each cipher mode */
200 #define EVP_C_DATA(kstruct, ctx) \
201 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
203 #define BLOCK_CIPHER_ecb_loop() \
205 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
206 if (inl < bl) return 1;\
208 for (i=0; i <= inl; i+=bl)
210 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
211 static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
213 BLOCK_CIPHER_ecb_loop() \
214 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
218 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
220 #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
221 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
223 while(inl>=EVP_MAXCHUNK) {\
224 int num = EVP_CIPHER_CTX_num(ctx);\
225 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
226 EVP_CIPHER_CTX_set_num(ctx, num);\
232 int num = EVP_CIPHER_CTX_num(ctx);\
233 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
234 EVP_CIPHER_CTX_set_num(ctx, num);\
239 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
240 static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
242 while(inl>=EVP_MAXCHUNK) \
244 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));\
250 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\
254 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
255 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
257 size_t chunk = EVP_MAXCHUNK;\
258 if (cbits == 1) chunk >>= 3;\
259 if (inl < chunk) chunk = inl;\
260 while (inl && inl >= chunk)\
262 int num = EVP_CIPHER_CTX_num(ctx);\
263 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
265 && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
266 ? chunk*8 : chunk), \
267 &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx),\
268 &num, EVP_CIPHER_CTX_encrypting(ctx));\
269 EVP_CIPHER_CTX_set_num(ctx, num);\
273 if (inl < chunk) chunk = inl;\
278 #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
279 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
280 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
281 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
282 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
284 #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
285 key_len, iv_len, flags, init_key, cleanup, \
286 set_asn1, get_asn1, ctrl) \
287 static const EVP_CIPHER cname##_##mode = { \
288 nid##_##nmode, block_size, key_len, iv_len, \
289 flags | EVP_CIPH_##MODE##_MODE, \
291 cname##_##mode##_cipher, \
298 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
300 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
301 iv_len, flags, init_key, cleanup, set_asn1, \
303 BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
304 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
306 #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
307 iv_len, cbits, flags, init_key, cleanup, \
308 set_asn1, get_asn1, ctrl) \
309 BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
310 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
313 #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
314 iv_len, cbits, flags, init_key, cleanup, \
315 set_asn1, get_asn1, ctrl) \
316 BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
317 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
320 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
321 flags, init_key, cleanup, set_asn1, \
323 BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
324 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
326 #define BLOCK_CIPHER_defs(cname, kstruct, \
327 nid, block_size, key_len, iv_len, cbits, flags, \
328 init_key, cleanup, set_asn1, get_asn1, ctrl) \
329 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
330 init_key, cleanup, set_asn1, get_asn1, ctrl) \
331 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
332 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
333 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
334 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
335 BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
336 init_key, cleanup, set_asn1, get_asn1, ctrl)
339 #define BLOCK_CIPHER_defs(cname, kstruct, \
340 nid, block_size, key_len, iv_len, flags,\
341 init_key, cleanup, set_asn1, get_asn1, ctrl)\
342 static const EVP_CIPHER cname##_cbc = {\
343 nid##_cbc, block_size, key_len, iv_len, \
344 flags | EVP_CIPH_CBC_MODE,\
348 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
349 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
354 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
355 static const EVP_CIPHER cname##_cfb = {\
356 nid##_cfb64, 1, key_len, iv_len, \
357 flags | EVP_CIPH_CFB_MODE,\
361 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
362 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
367 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
368 static const EVP_CIPHER cname##_ofb = {\
369 nid##_ofb64, 1, key_len, iv_len, \
370 flags | EVP_CIPH_OFB_MODE,\
374 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
375 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
380 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
381 static const EVP_CIPHER cname##_ecb = {\
382 nid##_ecb, block_size, key_len, iv_len, \
383 flags | EVP_CIPH_ECB_MODE,\
387 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
388 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
393 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
396 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
397 block_size, key_len, iv_len, cbits, \
399 cleanup, set_asn1, get_asn1, ctrl) \
400 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
401 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
402 cbits, flags, init_key, cleanup, set_asn1, \
405 #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
406 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
407 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
408 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
409 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
410 cipher##_init_key, NULL, NULL, NULL, NULL)
413 # ifndef OPENSSL_NO_EC
415 #define X25519_KEYLEN 32
416 #define X448_KEYLEN 56
417 #define ED448_KEYLEN 57
419 #define MAX_KEYLEN ED448_KEYLEN
422 unsigned char pubkey
[MAX_KEYLEN
];
423 unsigned char *privkey
;
429 * Type needs to be a bit field Sub-type needs to be for variations on the
430 * method, as in, can it do arbitrary encryption....
435 CRYPTO_REF_COUNT references
;
436 const EVP_PKEY_ASN1_METHOD
*ameth
;
438 ENGINE
*pmeth_engine
; /* If not NULL public key ENGINE to use */
441 # ifndef OPENSSL_NO_RSA
442 struct rsa_st
*rsa
; /* RSA */
444 # ifndef OPENSSL_NO_DSA
445 struct dsa_st
*dsa
; /* DSA */
447 # ifndef OPENSSL_NO_DH
448 struct dh_st
*dh
; /* DH */
450 # ifndef OPENSSL_NO_EC
451 struct ec_key_st
*ec
; /* ECC */
452 ECX_KEY
*ecx
; /* X25519, X448, Ed25519, Ed448 */
456 STACK_OF(X509_ATTRIBUTE
) *attributes
; /* [ 0 ] */
461 void openssl_add_all_ciphers_int(void);
462 void openssl_add_all_digests_int(void);
463 void openssl_add_all_macs_int(void);
464 void evp_cleanup_int(void);
465 void evp_app_cleanup_int(void);
467 /* Pulling defines out of C source files */
469 #define EVP_RC4_KEY_SIZE 16
470 #ifndef TLS1_1_VERSION
471 # define TLS1_1_VERSION 0x0302
474 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX
*ctx
, unsigned int flags
);
476 /* EVP_ENCODE_CTX flags */
477 /* Don't generate new lines when encoding */
478 #define EVP_ENCODE_CTX_NO_NEWLINES 1
479 /* Use the SRP base64 alphabet instead of the standard one */
480 #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2