2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
56 #include <openssl/engine.h>
57 #include <openssl/sha.h>
58 #include <openssl/aes.h>
59 #include <openssl/rsa.h>
60 #include <openssl/evp.h>
61 #include <openssl/async.h>
62 #include <openssl/bn.h>
63 #include <openssl/crypto.h>
64 #include <openssl/ssl.h>
65 #include <openssl/modes.h>
67 #if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
77 #define DASYNC_LIB_NAME "DASYNC"
78 #include "e_dasync_err.c"
80 /* Engine Id and Name */
81 static const char *engine_dasync_id = "dasync";
82 static const char *engine_dasync_name = "Dummy Async engine support";
85 /* Engine Lifetime functions */
86 static int dasync_destroy(ENGINE *e);
87 static int dasync_init(ENGINE *e);
88 static int dasync_finish(ENGINE *e);
89 void engine_load_dasync_int(void);
92 /* Set up digests. Just SHA1 for now */
93 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
94 const int **nids, int nid);
96 static void dummy_pause_job(void);
99 static int dasync_sha1_init(EVP_MD_CTX *ctx);
100 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
102 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
105 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
106 * engine bind and can then be reused many times.
108 static EVP_MD *_hidden_sha1_md = NULL;
109 static const EVP_MD *dasync_sha1(void)
111 return _hidden_sha1_md;
113 static void destroy_digests(void)
115 EVP_MD_meth_free(_hidden_sha1_md);
116 _hidden_sha1_md = NULL;
119 static int dasync_digest_nids(const int **nids)
121 static int digest_nids[2] = { 0, 0 };
127 if ((md = dasync_sha1()) != NULL)
128 digest_nids[pos++] = EVP_MD_type(md);
129 digest_nids[pos] = 0;
138 static int dasync_pub_enc(int flen, const unsigned char *from,
139 unsigned char *to, RSA *rsa, int padding);
140 static int dasync_pub_dec(int flen, const unsigned char *from,
141 unsigned char *to, RSA *rsa, int padding);
142 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
143 unsigned char *to, RSA *rsa, int padding);
144 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
145 unsigned char *to, RSA *rsa, int padding);
146 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
149 static int dasync_rsa_init(RSA *rsa);
150 static int dasync_rsa_finish(RSA *rsa);
152 static RSA_METHOD *dasync_rsa_method = NULL;
156 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
158 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
159 const unsigned char *iv, int enc);
160 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
161 const unsigned char *in, size_t inl);
162 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
164 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
166 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
167 const unsigned char *key,
168 const unsigned char *iv,
170 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
172 const unsigned char *in,
174 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
176 struct dasync_pipeline_ctx {
177 void *inner_cipher_data;
178 unsigned int numpipes;
179 unsigned char **inbufs;
180 unsigned char **outbufs;
183 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
188 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
189 * during engine bind and can then be reused many times.
191 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
192 static const EVP_CIPHER *dasync_aes_128_cbc(void)
194 return _hidden_aes_128_cbc;
198 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
199 * once only during engine bind and can then be reused many times.
201 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
202 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
204 return _hidden_aes_128_cbc_hmac_sha1;
207 static void destroy_ciphers(void)
209 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
210 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
211 _hidden_aes_128_cbc = NULL;
212 _hidden_aes_128_cbc_hmac_sha1 = NULL;
215 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
216 const int **nids, int nid);
218 static int dasync_cipher_nids[] = {
220 NID_aes_128_cbc_hmac_sha1,
224 static int bind_dasync(ENGINE *e)
226 /* Setup RSA_METHOD */
227 if ((dasync_rsa_method = RSA_meth_new("Dummy Async RSA method", 0)) == NULL
228 || RSA_meth_set_pub_enc(dasync_rsa_method, dasync_pub_enc) == 0
229 || RSA_meth_set_pub_dec(dasync_rsa_method, dasync_pub_dec) == 0
230 || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_enc) == 0
231 || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_dec) == 0
232 || RSA_meth_set_mod_exp(dasync_rsa_method, dasync_rsa_mod_exp) == 0
233 || RSA_meth_set_bn_mod_exp(dasync_rsa_method, BN_mod_exp_mont) == 0
234 || RSA_meth_set_init(dasync_rsa_method, dasync_rsa_init) == 0
235 || RSA_meth_set_finish(dasync_rsa_method, dasync_rsa_finish) == 0) {
236 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
240 /* Ensure the dasync error handling is set up */
241 ERR_load_DASYNC_strings();
243 if (!ENGINE_set_id(e, engine_dasync_id)
244 || !ENGINE_set_name(e, engine_dasync_name)
245 || !ENGINE_set_RSA(e, dasync_rsa_method)
246 || !ENGINE_set_digests(e, dasync_digests)
247 || !ENGINE_set_ciphers(e, dasync_ciphers)
248 || !ENGINE_set_destroy_function(e, dasync_destroy)
249 || !ENGINE_set_init_function(e, dasync_init)
250 || !ENGINE_set_finish_function(e, dasync_finish)) {
251 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
256 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
257 * supplied by this engine
259 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
260 if (_hidden_sha1_md == NULL
261 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
262 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
263 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
264 sizeof(EVP_MD *) + sizeof(SHA_CTX))
265 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
266 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
267 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
268 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
269 EVP_MD_meth_free(_hidden_sha1_md);
270 _hidden_sha1_md = NULL;
273 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
276 if (_hidden_aes_128_cbc == NULL
277 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
278 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
279 EVP_CIPH_FLAG_DEFAULT_ASN1
281 | EVP_CIPH_FLAG_PIPELINE)
282 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
283 dasync_aes128_init_key)
284 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
285 dasync_aes128_cbc_cipher)
286 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
287 dasync_aes128_cbc_cleanup)
288 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
289 dasync_aes128_cbc_ctrl)
290 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
291 sizeof(struct dasync_pipeline_ctx))) {
292 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
293 _hidden_aes_128_cbc = NULL;
296 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
297 NID_aes_128_cbc_hmac_sha1,
300 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
301 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
302 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
304 | EVP_CIPH_FLAG_DEFAULT_ASN1
305 | EVP_CIPH_FLAG_AEAD_CIPHER
306 | EVP_CIPH_FLAG_PIPELINE)
307 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
308 dasync_aes128_cbc_hmac_sha1_init_key)
309 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
310 dasync_aes128_cbc_hmac_sha1_cipher)
311 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
312 dasync_aes128_cbc_hmac_sha1_cleanup)
313 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
314 dasync_aes128_cbc_hmac_sha1_ctrl)
315 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
316 sizeof(struct dasync_pipeline_ctx))) {
317 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
318 _hidden_aes_128_cbc_hmac_sha1 = NULL;
324 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
325 static int bind_helper(ENGINE *e, const char *id)
327 if (id && (strcmp(id, engine_dasync_id) != 0))
334 IMPLEMENT_DYNAMIC_CHECK_FN()
335 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
338 static ENGINE *engine_dasync(void)
340 ENGINE *ret = ENGINE_new();
343 if (!bind_dasync(ret)) {
350 void engine_load_dasync_int(void)
352 ENGINE *toadd = engine_dasync();
360 static int dasync_init(ENGINE *e)
366 static int dasync_finish(ENGINE *e)
372 static int dasync_destroy(ENGINE *e)
376 RSA_meth_free(dasync_rsa_method);
377 ERR_unload_DASYNC_strings();
381 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
382 const int **nids, int nid)
386 /* We are returning a list of supported nids */
387 return dasync_digest_nids(nids);
389 /* We are being asked for a specific digest */
392 *digest = dasync_sha1();
402 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
403 const int **nids, int nid)
406 if (cipher == NULL) {
407 /* We are returning a list of supported nids */
408 *nids = dasync_cipher_nids;
409 return (sizeof(dasync_cipher_nids) -
410 1) / sizeof(dasync_cipher_nids[0]);
412 /* We are being asked for a specific cipher */
414 case NID_aes_128_cbc:
415 *cipher = dasync_aes_128_cbc();
417 case NID_aes_128_cbc_hmac_sha1:
418 *cipher = dasync_aes_128_cbc_hmac_sha1();
428 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
429 OSSL_ASYNC_FD readfd, void *pvwritefd)
431 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
432 #if defined(ASYNC_WIN)
434 CloseHandle(*pwritefd);
435 #elif defined(ASYNC_POSIX)
439 OPENSSL_free(pwritefd);
442 #define DUMMY_CHAR 'X'
444 static void dummy_pause_job(void) {
446 ASYNC_WAIT_CTX *waitctx;
447 OSSL_ASYNC_FD pipefds[2] = {0, 0};
448 OSSL_ASYNC_FD *writefd;
449 #if defined(ASYNC_WIN)
450 DWORD numwritten, numread;
451 char buf = DUMMY_CHAR;
452 #elif defined(ASYNC_POSIX)
453 char buf = DUMMY_CHAR;
456 if ((job = ASYNC_get_current_job()) == NULL)
459 waitctx = ASYNC_get_wait_ctx(job);
461 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
462 (void **)&writefd)) {
463 pipefds[1] = *writefd;
465 writefd = OPENSSL_malloc(sizeof(*writefd));
468 #if defined(ASYNC_WIN)
469 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
470 OPENSSL_free(writefd);
473 #elif defined(ASYNC_POSIX)
474 if (pipe(pipefds) != 0) {
475 OPENSSL_free(writefd);
479 *writefd = pipefds[1];
481 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
482 writefd, wait_cleanup)) {
483 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
488 * In the Dummy async engine we are cheating. We signal that the job
489 * is complete by waking it before the call to ASYNC_pause_job(). A real
490 * async engine would only wake when the job was actually complete
492 #if defined(ASYNC_WIN)
493 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
494 #elif defined(ASYNC_POSIX)
495 if (write(pipefds[1], &buf, 1) < 0)
499 /* Ignore errors - we carry on anyway */
502 /* Clear the wake signal */
503 #if defined(ASYNC_WIN)
504 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
505 #elif defined(ASYNC_POSIX)
506 if (read(pipefds[0], &buf, 1) < 0)
512 * SHA1 implementation. At the moment we just defer to the standard
516 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
517 static int dasync_sha1_init(EVP_MD_CTX *ctx)
521 return SHA1_Init(data(ctx));
524 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
529 return SHA1_Update(data(ctx), data, (size_t)count);
532 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
536 return SHA1_Final(md, data(ctx));
543 static int dasync_pub_enc(int flen, const unsigned char *from,
544 unsigned char *to, RSA *rsa, int padding) {
545 /* Ignore errors - we carry on anyway */
547 return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
548 (flen, from, to, rsa, padding);
551 static int dasync_pub_dec(int flen, const unsigned char *from,
552 unsigned char *to, RSA *rsa, int padding) {
553 /* Ignore errors - we carry on anyway */
555 return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
556 (flen, from, to, rsa, padding);
559 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
560 unsigned char *to, RSA *rsa, int padding)
562 /* Ignore errors - we carry on anyway */
564 return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
565 (flen, from, to, rsa, padding);
568 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
569 unsigned char *to, RSA *rsa, int padding)
571 /* Ignore errors - we carry on anyway */
573 return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
574 (flen, from, to, rsa, padding);
577 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
579 /* Ignore errors - we carry on anyway */
581 return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
584 static int dasync_rsa_init(RSA *rsa)
586 return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
588 static int dasync_rsa_finish(RSA *rsa)
590 return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
593 /* Cipher helper functions */
595 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
596 void *ptr, int aeadcapable)
599 struct dasync_pipeline_ctx *pipe_ctx =
600 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
602 if (pipe_ctx == NULL)
606 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
607 pipe_ctx->numpipes = arg;
608 pipe_ctx->outbufs = (unsigned char **)ptr;
611 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
612 pipe_ctx->numpipes = arg;
613 pipe_ctx->inbufs = (unsigned char **)ptr;
616 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
617 pipe_ctx->numpipes = arg;
618 pipe_ctx->lens = (size_t *)ptr;
621 case EVP_CTRL_AEAD_SET_MAC_KEY:
624 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
625 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
626 (ctx, type, arg, ptr);
627 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
630 case EVP_CTRL_AEAD_TLS1_AAD:
632 unsigned char *p = ptr;
635 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
638 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
641 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
642 EVP_AEAD_TLS1_AAD_LEN);
645 len = p[arg - 2] << 8 | p[arg - 1];
648 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
649 len -= AES_BLOCK_SIZE;
652 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
653 & -AES_BLOCK_SIZE) - len;
655 return SHA_DIGEST_LENGTH;
666 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
667 const unsigned char *key,
668 const unsigned char *iv, int enc,
669 const EVP_CIPHER *cipher)
672 struct dasync_pipeline_ctx *pipe_ctx =
673 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
675 if (pipe_ctx->inner_cipher_data == NULL
676 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
677 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
678 EVP_CIPHER_impl_ctx_size(cipher));
679 if (pipe_ctx->inner_cipher_data == NULL) {
680 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
681 ERR_R_MALLOC_FAILURE);
686 pipe_ctx->numpipes = 0;
687 pipe_ctx->aadctr = 0;
689 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
690 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
691 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
696 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
697 const unsigned char *in, size_t inl,
698 const EVP_CIPHER *cipher)
701 unsigned int i, pipes;
702 struct dasync_pipeline_ctx *pipe_ctx =
703 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
705 pipes = pipe_ctx->numpipes;
706 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
708 if (pipe_ctx->aadctr != 0) {
709 if (pipe_ctx->aadctr != 1)
711 EVP_CIPHER_meth_get_ctrl(cipher)
712 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
713 EVP_AEAD_TLS1_AAD_LEN,
714 pipe_ctx->tlsaad[0]);
716 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
719 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
721 for (i = 0; i < pipes; i++) {
722 if (pipe_ctx->aadctr > 0) {
723 EVP_CIPHER_meth_get_ctrl(cipher)
724 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
725 EVP_AEAD_TLS1_AAD_LEN,
726 pipe_ctx->tlsaad[i]);
728 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
729 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
732 pipe_ctx->numpipes = 0;
734 pipe_ctx->aadctr = 0;
735 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
739 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
740 const EVP_CIPHER *cipher)
742 struct dasync_pipeline_ctx *pipe_ctx =
743 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
745 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
746 EVP_CIPHER_impl_ctx_size(cipher));
752 * AES128 CBC Implementation
755 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
758 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
761 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
762 const unsigned char *iv, int enc)
764 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
767 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
768 const unsigned char *in, size_t inl)
770 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
773 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
775 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
780 * AES128 CBC HMAC SHA1 Implementation
783 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
786 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
789 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
790 const unsigned char *key,
791 const unsigned char *iv,
794 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
795 EVP_aes_128_cbc_hmac_sha1());
798 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
800 const unsigned char *in,
803 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
806 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
808 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());