2 * Copyright 2015-2018 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
11 * SHA-1 low level APIs are deprecated for public use, but still ok for
12 * internal use. Note, that due to symbols not being exported, only the
13 * #defines and strucures can be accessed, in this case SHA_CBLOCK and
16 #include "internal/deprecated.h"
25 #include <openssl/engine.h>
26 #include <openssl/sha.h>
27 #include <openssl/aes.h>
28 #include <openssl/rsa.h>
29 #include <openssl/evp.h>
30 #include <openssl/async.h>
31 #include <openssl/bn.h>
32 #include <openssl/crypto.h>
33 #include <openssl/ssl.h>
34 #include <openssl/modes.h>
36 #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
45 #include "e_dasync_err.c"
47 /* Engine Id and Name */
48 static const char *engine_dasync_id = "dasync";
49 static const char *engine_dasync_name = "Dummy Async engine support";
52 /* Engine Lifetime functions */
53 static int dasync_destroy(ENGINE *e);
54 static int dasync_init(ENGINE *e);
55 static int dasync_finish(ENGINE *e);
56 void engine_load_dasync_int(void);
59 /* Set up digests. Just SHA1 for now */
60 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
61 const int **nids, int nid);
63 static void dummy_pause_job(void);
66 static int dasync_sha1_init(EVP_MD_CTX *ctx);
67 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
69 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
72 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
73 * engine bind and can then be reused many times.
75 static EVP_MD *_hidden_sha1_md = NULL;
76 static const EVP_MD *dasync_sha1(void)
78 return _hidden_sha1_md;
80 static void destroy_digests(void)
82 EVP_MD_meth_free(_hidden_sha1_md);
83 _hidden_sha1_md = NULL;
86 static int dasync_digest_nids(const int **nids)
88 static int digest_nids[2] = { 0, 0 };
94 if ((md = dasync_sha1()) != NULL)
95 digest_nids[pos++] = EVP_MD_type(md);
105 static int dasync_pub_enc(int flen, const unsigned char *from,
106 unsigned char *to, RSA *rsa, int padding);
107 static int dasync_pub_dec(int flen, const unsigned char *from,
108 unsigned char *to, RSA *rsa, int padding);
109 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
110 unsigned char *to, RSA *rsa, int padding);
111 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
112 unsigned char *to, RSA *rsa, int padding);
113 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
116 static int dasync_rsa_init(RSA *rsa);
117 static int dasync_rsa_finish(RSA *rsa);
119 static RSA_METHOD *dasync_rsa_method = NULL;
123 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
125 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
126 const unsigned char *iv, int enc);
127 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
128 const unsigned char *in, size_t inl);
129 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
131 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
133 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
134 const unsigned char *key,
135 const unsigned char *iv,
137 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
139 const unsigned char *in,
141 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
143 struct dasync_pipeline_ctx {
144 void *inner_cipher_data;
145 unsigned int numpipes;
146 unsigned char **inbufs;
147 unsigned char **outbufs;
149 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
154 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
155 * during engine bind and can then be reused many times.
157 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
158 static const EVP_CIPHER *dasync_aes_128_cbc(void)
160 return _hidden_aes_128_cbc;
164 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
165 * once only during engine bind and can then be reused many times.
167 * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
168 * which is implemented only if the AES-NI instruction set extension is available
169 * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
170 * be available either.
172 * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
173 * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
175 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
176 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
178 return _hidden_aes_128_cbc_hmac_sha1;
181 static void destroy_ciphers(void)
183 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
184 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
185 _hidden_aes_128_cbc = NULL;
186 _hidden_aes_128_cbc_hmac_sha1 = NULL;
189 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
190 const int **nids, int nid);
192 static int dasync_cipher_nids[] = {
194 NID_aes_128_cbc_hmac_sha1,
198 static int bind_dasync(ENGINE *e)
200 /* Setup RSA_METHOD */
201 if ((dasync_rsa_method = RSA_meth_new("Dummy Async RSA method", 0)) == NULL
202 || RSA_meth_set_pub_enc(dasync_rsa_method, dasync_pub_enc) == 0
203 || RSA_meth_set_pub_dec(dasync_rsa_method, dasync_pub_dec) == 0
204 || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_enc) == 0
205 || RSA_meth_set_priv_dec(dasync_rsa_method, dasync_rsa_priv_dec) == 0
206 || RSA_meth_set_mod_exp(dasync_rsa_method, dasync_rsa_mod_exp) == 0
207 || RSA_meth_set_bn_mod_exp(dasync_rsa_method, BN_mod_exp_mont) == 0
208 || RSA_meth_set_init(dasync_rsa_method, dasync_rsa_init) == 0
209 || RSA_meth_set_finish(dasync_rsa_method, dasync_rsa_finish) == 0) {
210 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
214 /* Ensure the dasync error handling is set up */
215 ERR_load_DASYNC_strings();
217 if (!ENGINE_set_id(e, engine_dasync_id)
218 || !ENGINE_set_name(e, engine_dasync_name)
219 || !ENGINE_set_RSA(e, dasync_rsa_method)
220 || !ENGINE_set_digests(e, dasync_digests)
221 || !ENGINE_set_ciphers(e, dasync_ciphers)
222 || !ENGINE_set_destroy_function(e, dasync_destroy)
223 || !ENGINE_set_init_function(e, dasync_init)
224 || !ENGINE_set_finish_function(e, dasync_finish)) {
225 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
230 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
231 * supplied by this engine
233 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
234 if (_hidden_sha1_md == NULL
235 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
236 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
237 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
238 sizeof(EVP_MD *) + sizeof(SHA_CTX))
239 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
240 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
241 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
242 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
243 EVP_MD_meth_free(_hidden_sha1_md);
244 _hidden_sha1_md = NULL;
247 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
250 if (_hidden_aes_128_cbc == NULL
251 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
252 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
253 EVP_CIPH_FLAG_DEFAULT_ASN1
255 | EVP_CIPH_FLAG_PIPELINE)
256 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
257 dasync_aes128_init_key)
258 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
259 dasync_aes128_cbc_cipher)
260 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
261 dasync_aes128_cbc_cleanup)
262 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
263 dasync_aes128_cbc_ctrl)
264 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
265 sizeof(struct dasync_pipeline_ctx))) {
266 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
267 _hidden_aes_128_cbc = NULL;
270 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
271 NID_aes_128_cbc_hmac_sha1,
274 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
275 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
276 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
278 | EVP_CIPH_FLAG_DEFAULT_ASN1
279 | EVP_CIPH_FLAG_AEAD_CIPHER
280 | EVP_CIPH_FLAG_PIPELINE)
281 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
282 dasync_aes128_cbc_hmac_sha1_init_key)
283 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
284 dasync_aes128_cbc_hmac_sha1_cipher)
285 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
286 dasync_aes128_cbc_hmac_sha1_cleanup)
287 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
288 dasync_aes128_cbc_hmac_sha1_ctrl)
289 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
290 sizeof(struct dasync_pipeline_ctx))) {
291 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
292 _hidden_aes_128_cbc_hmac_sha1 = NULL;
298 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
299 static int bind_helper(ENGINE *e, const char *id)
301 if (id && (strcmp(id, engine_dasync_id) != 0))
308 IMPLEMENT_DYNAMIC_CHECK_FN()
309 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
312 static ENGINE *engine_dasync(void)
314 ENGINE *ret = ENGINE_new();
317 if (!bind_dasync(ret)) {
324 void engine_load_dasync_int(void)
326 ENGINE *toadd = engine_dasync();
334 static int dasync_init(ENGINE *e)
340 static int dasync_finish(ENGINE *e)
346 static int dasync_destroy(ENGINE *e)
350 RSA_meth_free(dasync_rsa_method);
351 ERR_unload_DASYNC_strings();
355 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
356 const int **nids, int nid)
360 /* We are returning a list of supported nids */
361 return dasync_digest_nids(nids);
363 /* We are being asked for a specific digest */
366 *digest = dasync_sha1();
376 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
377 const int **nids, int nid)
380 if (cipher == NULL) {
381 /* We are returning a list of supported nids */
382 *nids = dasync_cipher_nids;
383 return (sizeof(dasync_cipher_nids) -
384 1) / sizeof(dasync_cipher_nids[0]);
386 /* We are being asked for a specific cipher */
388 case NID_aes_128_cbc:
389 *cipher = dasync_aes_128_cbc();
391 case NID_aes_128_cbc_hmac_sha1:
392 *cipher = dasync_aes_128_cbc_hmac_sha1();
402 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
403 OSSL_ASYNC_FD readfd, void *pvwritefd)
405 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
406 #if defined(ASYNC_WIN)
408 CloseHandle(*pwritefd);
409 #elif defined(ASYNC_POSIX)
413 OPENSSL_free(pwritefd);
416 #define DUMMY_CHAR 'X'
418 static void dummy_pause_job(void) {
420 ASYNC_WAIT_CTX *waitctx;
421 ASYNC_callback_fn callback;
423 OSSL_ASYNC_FD pipefds[2] = {0, 0};
424 OSSL_ASYNC_FD *writefd;
425 #if defined(ASYNC_WIN)
426 DWORD numwritten, numread;
427 char buf = DUMMY_CHAR;
428 #elif defined(ASYNC_POSIX)
429 char buf = DUMMY_CHAR;
432 if ((job = ASYNC_get_current_job()) == NULL)
435 waitctx = ASYNC_get_wait_ctx(job);
437 if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
439 * In the Dummy async engine we are cheating. We call the callback that the job
440 * is complete before the call to ASYNC_pause_job(). A real
441 * async engine would only call the callback when the job was actually complete
443 (*callback)(callback_arg);
449 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
450 (void **)&writefd)) {
451 pipefds[1] = *writefd;
453 writefd = OPENSSL_malloc(sizeof(*writefd));
456 #if defined(ASYNC_WIN)
457 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
458 OPENSSL_free(writefd);
461 #elif defined(ASYNC_POSIX)
462 if (pipe(pipefds) != 0) {
463 OPENSSL_free(writefd);
467 *writefd = pipefds[1];
469 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
470 writefd, wait_cleanup)) {
471 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
476 * In the Dummy async engine we are cheating. We signal that the job
477 * is complete by waking it before the call to ASYNC_pause_job(). A real
478 * async engine would only wake when the job was actually complete
480 #if defined(ASYNC_WIN)
481 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
482 #elif defined(ASYNC_POSIX)
483 if (write(pipefds[1], &buf, 1) < 0)
487 /* Ignore errors - we carry on anyway */
490 /* Clear the wake signal */
491 #if defined(ASYNC_WIN)
492 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
493 #elif defined(ASYNC_POSIX)
494 if (read(pipefds[0], &buf, 1) < 0)
500 * SHA1 implementation. At the moment we just defer to the standard
503 static int dasync_sha1_init(EVP_MD_CTX *ctx)
507 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
510 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
515 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
518 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
522 return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
529 static int dasync_pub_enc(int flen, const unsigned char *from,
530 unsigned char *to, RSA *rsa, int padding) {
531 /* Ignore errors - we carry on anyway */
533 return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
534 (flen, from, to, rsa, padding);
537 static int dasync_pub_dec(int flen, const unsigned char *from,
538 unsigned char *to, RSA *rsa, int padding) {
539 /* Ignore errors - we carry on anyway */
541 return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
542 (flen, from, to, rsa, padding);
545 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
546 unsigned char *to, RSA *rsa, int padding)
548 /* Ignore errors - we carry on anyway */
550 return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
551 (flen, from, to, rsa, padding);
554 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
555 unsigned char *to, RSA *rsa, int padding)
557 /* Ignore errors - we carry on anyway */
559 return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
560 (flen, from, to, rsa, padding);
563 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
565 /* Ignore errors - we carry on anyway */
567 return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
570 static int dasync_rsa_init(RSA *rsa)
572 return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
574 static int dasync_rsa_finish(RSA *rsa)
576 return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
579 /* Cipher helper functions */
581 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
582 void *ptr, int aeadcapable)
585 struct dasync_pipeline_ctx *pipe_ctx =
586 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
588 if (pipe_ctx == NULL)
592 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
593 pipe_ctx->numpipes = arg;
594 pipe_ctx->outbufs = (unsigned char **)ptr;
597 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
598 pipe_ctx->numpipes = arg;
599 pipe_ctx->inbufs = (unsigned char **)ptr;
602 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
603 pipe_ctx->numpipes = arg;
604 pipe_ctx->lens = (size_t *)ptr;
607 case EVP_CTRL_AEAD_SET_MAC_KEY:
610 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
611 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
612 (ctx, type, arg, ptr);
613 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
616 case EVP_CTRL_AEAD_TLS1_AAD:
618 unsigned char *p = ptr;
621 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
624 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
627 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
628 EVP_AEAD_TLS1_AAD_LEN);
631 len = p[arg - 2] << 8 | p[arg - 1];
633 if (EVP_CIPHER_CTX_encrypting(ctx)) {
634 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
635 if (len < AES_BLOCK_SIZE)
637 len -= AES_BLOCK_SIZE;
640 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
641 & -AES_BLOCK_SIZE) - len;
643 return SHA_DIGEST_LENGTH;
654 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
655 const unsigned char *key,
656 const unsigned char *iv, int enc,
657 const EVP_CIPHER *cipher)
660 struct dasync_pipeline_ctx *pipe_ctx =
661 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
663 if (pipe_ctx->inner_cipher_data == NULL
664 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
665 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
666 EVP_CIPHER_impl_ctx_size(cipher));
667 if (pipe_ctx->inner_cipher_data == NULL) {
668 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
669 ERR_R_MALLOC_FAILURE);
674 pipe_ctx->numpipes = 0;
675 pipe_ctx->aadctr = 0;
677 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
678 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
679 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
684 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
685 const unsigned char *in, size_t inl,
686 const EVP_CIPHER *cipher)
689 unsigned int i, pipes;
690 struct dasync_pipeline_ctx *pipe_ctx =
691 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
693 pipes = pipe_ctx->numpipes;
694 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
696 if (pipe_ctx->aadctr != 0) {
697 if (pipe_ctx->aadctr != 1)
699 EVP_CIPHER_meth_get_ctrl(cipher)
700 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
701 EVP_AEAD_TLS1_AAD_LEN,
702 pipe_ctx->tlsaad[0]);
704 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
707 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
709 for (i = 0; i < pipes; i++) {
710 if (pipe_ctx->aadctr > 0) {
711 EVP_CIPHER_meth_get_ctrl(cipher)
712 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
713 EVP_AEAD_TLS1_AAD_LEN,
714 pipe_ctx->tlsaad[i]);
716 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
717 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
720 pipe_ctx->numpipes = 0;
722 pipe_ctx->aadctr = 0;
723 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
727 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
728 const EVP_CIPHER *cipher)
730 struct dasync_pipeline_ctx *pipe_ctx =
731 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
733 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
734 EVP_CIPHER_impl_ctx_size(cipher));
740 * AES128 CBC Implementation
743 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
746 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
749 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
750 const unsigned char *iv, int enc)
752 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
755 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
756 const unsigned char *in, size_t inl)
758 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
761 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
763 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
768 * AES128 CBC HMAC SHA1 Implementation
771 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
774 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
777 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
778 const unsigned char *key,
779 const unsigned char *iv,
783 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
784 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
786 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
787 EVP_aes_128_cbc_hmac_sha1());
790 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
792 const unsigned char *in,
795 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
798 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
801 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
802 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
804 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());