2 * Copyright 2015-2016 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
17 #include <openssl/engine.h>
18 #include <openssl/sha.h>
19 #include <openssl/aes.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
22 #include <openssl/async.h>
23 #include <openssl/bn.h>
24 #include <openssl/crypto.h>
25 #include <openssl/ssl.h>
26 #include <openssl/modes.h>
28 #if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
37 #define DASYNC_LIB_NAME "DASYNC"
38 #include "e_dasync_err.c"
40 /* Engine Id and Name */
41 static const char *engine_dasync_id
= "dasync";
42 static const char *engine_dasync_name
= "Dummy Async engine support";
45 /* Engine Lifetime functions */
46 static int dasync_destroy(ENGINE
*e
);
47 static int dasync_init(ENGINE
*e
);
48 static int dasync_finish(ENGINE
*e
);
49 void engine_load_dasync_int(void);
52 /* Set up digests. Just SHA1 for now */
53 static int dasync_digests(ENGINE
*e
, const EVP_MD
**digest
,
54 const int **nids
, int nid
);
56 static void dummy_pause_job(void);
59 static int dasync_sha1_init(EVP_MD_CTX
*ctx
);
60 static int dasync_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
62 static int dasync_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
65 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
66 * engine bind and can then be reused many times.
68 static EVP_MD
*_hidden_sha1_md
= NULL
;
69 static const EVP_MD
*dasync_sha1(void)
71 return _hidden_sha1_md
;
73 static void destroy_digests(void)
75 EVP_MD_meth_free(_hidden_sha1_md
);
76 _hidden_sha1_md
= NULL
;
79 static int dasync_digest_nids(const int **nids
)
81 static int digest_nids
[2] = { 0, 0 };
87 if ((md
= dasync_sha1()) != NULL
)
88 digest_nids
[pos
++] = EVP_MD_type(md
);
98 static int dasync_pub_enc(int flen
, const unsigned char *from
,
99 unsigned char *to
, RSA
*rsa
, int padding
);
100 static int dasync_pub_dec(int flen
, const unsigned char *from
,
101 unsigned char *to
, RSA
*rsa
, int padding
);
102 static int dasync_rsa_priv_enc(int flen
, const unsigned char *from
,
103 unsigned char *to
, RSA
*rsa
, int padding
);
104 static int dasync_rsa_priv_dec(int flen
, const unsigned char *from
,
105 unsigned char *to
, RSA
*rsa
, int padding
);
106 static int dasync_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
,
109 static int dasync_rsa_init(RSA
*rsa
);
110 static int dasync_rsa_finish(RSA
*rsa
);
112 static RSA_METHOD
*dasync_rsa_method
= NULL
;
116 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
118 static int dasync_aes128_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
119 const unsigned char *iv
, int enc
);
120 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
121 const unsigned char *in
, size_t inl
);
122 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX
*ctx
);
124 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
,
126 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
127 const unsigned char *key
,
128 const unsigned char *iv
,
130 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
,
132 const unsigned char *in
,
134 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX
*ctx
);
136 struct dasync_pipeline_ctx
{
137 void *inner_cipher_data
;
138 unsigned int numpipes
;
139 unsigned char **inbufs
;
140 unsigned char **outbufs
;
143 unsigned char tlsaad
[SSL_MAX_PIPELINES
][EVP_AEAD_TLS1_AAD_LEN
];
148 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
149 * during engine bind and can then be reused many times.
151 static EVP_CIPHER
*_hidden_aes_128_cbc
= NULL
;
152 static const EVP_CIPHER
*dasync_aes_128_cbc(void)
154 return _hidden_aes_128_cbc
;
158 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
159 * once only during engine bind and can then be reused many times.
161 static EVP_CIPHER
*_hidden_aes_128_cbc_hmac_sha1
= NULL
;
162 static const EVP_CIPHER
*dasync_aes_128_cbc_hmac_sha1(void)
164 return _hidden_aes_128_cbc_hmac_sha1
;
167 static void destroy_ciphers(void)
169 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
170 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1
);
171 _hidden_aes_128_cbc
= NULL
;
172 _hidden_aes_128_cbc_hmac_sha1
= NULL
;
175 static int dasync_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
176 const int **nids
, int nid
);
178 static int dasync_cipher_nids
[] = {
180 NID_aes_128_cbc_hmac_sha1
,
184 static int bind_dasync(ENGINE
*e
)
186 /* Setup RSA_METHOD */
187 if ((dasync_rsa_method
= RSA_meth_new("Dummy Async RSA method", 0)) == NULL
188 || RSA_meth_set_pub_enc(dasync_rsa_method
, dasync_pub_enc
) == 0
189 || RSA_meth_set_pub_dec(dasync_rsa_method
, dasync_pub_dec
) == 0
190 || RSA_meth_set_priv_enc(dasync_rsa_method
, dasync_rsa_priv_enc
) == 0
191 || RSA_meth_set_priv_dec(dasync_rsa_method
, dasync_rsa_priv_dec
) == 0
192 || RSA_meth_set_mod_exp(dasync_rsa_method
, dasync_rsa_mod_exp
) == 0
193 || RSA_meth_set_bn_mod_exp(dasync_rsa_method
, BN_mod_exp_mont
) == 0
194 || RSA_meth_set_init(dasync_rsa_method
, dasync_rsa_init
) == 0
195 || RSA_meth_set_finish(dasync_rsa_method
, dasync_rsa_finish
) == 0) {
196 DASYNCerr(DASYNC_F_BIND_DASYNC
, DASYNC_R_INIT_FAILED
);
200 /* Ensure the dasync error handling is set up */
201 ERR_load_DASYNC_strings();
203 if (!ENGINE_set_id(e
, engine_dasync_id
)
204 || !ENGINE_set_name(e
, engine_dasync_name
)
205 || !ENGINE_set_RSA(e
, dasync_rsa_method
)
206 || !ENGINE_set_digests(e
, dasync_digests
)
207 || !ENGINE_set_ciphers(e
, dasync_ciphers
)
208 || !ENGINE_set_destroy_function(e
, dasync_destroy
)
209 || !ENGINE_set_init_function(e
, dasync_init
)
210 || !ENGINE_set_finish_function(e
, dasync_finish
)) {
211 DASYNCerr(DASYNC_F_BIND_DASYNC
, DASYNC_R_INIT_FAILED
);
216 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
217 * supplied by this engine
219 _hidden_sha1_md
= EVP_MD_meth_new(NID_sha1
, NID_sha1WithRSAEncryption
);
220 if (_hidden_sha1_md
== NULL
221 || !EVP_MD_meth_set_result_size(_hidden_sha1_md
, SHA_DIGEST_LENGTH
)
222 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md
, SHA_CBLOCK
)
223 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md
,
224 sizeof(EVP_MD
*) + sizeof(SHA_CTX
))
225 || !EVP_MD_meth_set_flags(_hidden_sha1_md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
226 || !EVP_MD_meth_set_init(_hidden_sha1_md
, dasync_sha1_init
)
227 || !EVP_MD_meth_set_update(_hidden_sha1_md
, dasync_sha1_update
)
228 || !EVP_MD_meth_set_final(_hidden_sha1_md
, dasync_sha1_final
)) {
229 EVP_MD_meth_free(_hidden_sha1_md
);
230 _hidden_sha1_md
= NULL
;
233 _hidden_aes_128_cbc
= EVP_CIPHER_meth_new(NID_aes_128_cbc
,
236 if (_hidden_aes_128_cbc
== NULL
237 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc
,16)
238 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc
,
239 EVP_CIPH_FLAG_DEFAULT_ASN1
241 | EVP_CIPH_FLAG_PIPELINE
)
242 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc
,
243 dasync_aes128_init_key
)
244 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc
,
245 dasync_aes128_cbc_cipher
)
246 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc
,
247 dasync_aes128_cbc_cleanup
)
248 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc
,
249 dasync_aes128_cbc_ctrl
)
250 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc
,
251 sizeof(struct dasync_pipeline_ctx
))) {
252 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
253 _hidden_aes_128_cbc
= NULL
;
256 _hidden_aes_128_cbc_hmac_sha1
= EVP_CIPHER_meth_new(
257 NID_aes_128_cbc_hmac_sha1
,
260 if (_hidden_aes_128_cbc_hmac_sha1
== NULL
261 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1
,16)
262 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1
,
264 | EVP_CIPH_FLAG_DEFAULT_ASN1
265 | EVP_CIPH_FLAG_AEAD_CIPHER
266 | EVP_CIPH_FLAG_PIPELINE
)
267 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1
,
268 dasync_aes128_cbc_hmac_sha1_init_key
)
269 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1
,
270 dasync_aes128_cbc_hmac_sha1_cipher
)
271 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1
,
272 dasync_aes128_cbc_hmac_sha1_cleanup
)
273 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1
,
274 dasync_aes128_cbc_hmac_sha1_ctrl
)
275 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1
,
276 sizeof(struct dasync_pipeline_ctx
))) {
277 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1
);
278 _hidden_aes_128_cbc_hmac_sha1
= NULL
;
284 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
285 static int bind_helper(ENGINE
*e
, const char *id
)
287 if (id
&& (strcmp(id
, engine_dasync_id
) != 0))
294 IMPLEMENT_DYNAMIC_CHECK_FN()
295 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
298 static ENGINE
*engine_dasync(void)
300 ENGINE
*ret
= ENGINE_new();
303 if (!bind_dasync(ret
)) {
310 void engine_load_dasync_int(void)
312 ENGINE
*toadd
= engine_dasync();
320 static int dasync_init(ENGINE
*e
)
326 static int dasync_finish(ENGINE
*e
)
332 static int dasync_destroy(ENGINE
*e
)
336 RSA_meth_free(dasync_rsa_method
);
337 ERR_unload_DASYNC_strings();
341 static int dasync_digests(ENGINE
*e
, const EVP_MD
**digest
,
342 const int **nids
, int nid
)
346 /* We are returning a list of supported nids */
347 return dasync_digest_nids(nids
);
349 /* We are being asked for a specific digest */
352 *digest
= dasync_sha1();
362 static int dasync_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
363 const int **nids
, int nid
)
366 if (cipher
== NULL
) {
367 /* We are returning a list of supported nids */
368 *nids
= dasync_cipher_nids
;
369 return (sizeof(dasync_cipher_nids
) -
370 1) / sizeof(dasync_cipher_nids
[0]);
372 /* We are being asked for a specific cipher */
374 case NID_aes_128_cbc
:
375 *cipher
= dasync_aes_128_cbc();
377 case NID_aes_128_cbc_hmac_sha1
:
378 *cipher
= dasync_aes_128_cbc_hmac_sha1();
388 static void wait_cleanup(ASYNC_WAIT_CTX
*ctx
, const void *key
,
389 OSSL_ASYNC_FD readfd
, void *pvwritefd
)
391 OSSL_ASYNC_FD
*pwritefd
= (OSSL_ASYNC_FD
*)pvwritefd
;
392 #if defined(ASYNC_WIN)
394 CloseHandle(*pwritefd
);
395 #elif defined(ASYNC_POSIX)
399 OPENSSL_free(pwritefd
);
402 #define DUMMY_CHAR 'X'
404 static void dummy_pause_job(void) {
406 ASYNC_WAIT_CTX
*waitctx
;
407 OSSL_ASYNC_FD pipefds
[2] = {0, 0};
408 OSSL_ASYNC_FD
*writefd
;
409 #if defined(ASYNC_WIN)
410 DWORD numwritten
, numread
;
411 char buf
= DUMMY_CHAR
;
412 #elif defined(ASYNC_POSIX)
413 char buf
= DUMMY_CHAR
;
416 if ((job
= ASYNC_get_current_job()) == NULL
)
419 waitctx
= ASYNC_get_wait_ctx(job
);
421 if (ASYNC_WAIT_CTX_get_fd(waitctx
, engine_dasync_id
, &pipefds
[0],
422 (void **)&writefd
)) {
423 pipefds
[1] = *writefd
;
425 writefd
= OPENSSL_malloc(sizeof(*writefd
));
428 #if defined(ASYNC_WIN)
429 if (CreatePipe(&pipefds
[0], &pipefds
[1], NULL
, 256) == 0) {
430 OPENSSL_free(writefd
);
433 #elif defined(ASYNC_POSIX)
434 if (pipe(pipefds
) != 0) {
435 OPENSSL_free(writefd
);
439 *writefd
= pipefds
[1];
441 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx
, engine_dasync_id
, pipefds
[0],
442 writefd
, wait_cleanup
)) {
443 wait_cleanup(waitctx
, engine_dasync_id
, pipefds
[0], writefd
);
448 * In the Dummy async engine we are cheating. We signal that the job
449 * is complete by waking it before the call to ASYNC_pause_job(). A real
450 * async engine would only wake when the job was actually complete
452 #if defined(ASYNC_WIN)
453 WriteFile(pipefds
[1], &buf
, 1, &numwritten
, NULL
);
454 #elif defined(ASYNC_POSIX)
455 if (write(pipefds
[1], &buf
, 1) < 0)
459 /* Ignore errors - we carry on anyway */
462 /* Clear the wake signal */
463 #if defined(ASYNC_WIN)
464 ReadFile(pipefds
[0], &buf
, 1, &numread
, NULL
);
465 #elif defined(ASYNC_POSIX)
466 if (read(pipefds
[0], &buf
, 1) < 0)
472 * SHA1 implementation. At the moment we just defer to the standard
476 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
477 static int dasync_sha1_init(EVP_MD_CTX
*ctx
)
481 return SHA1_Init(data(ctx
));
484 static int dasync_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
489 return SHA1_Update(data(ctx
), data
, (size_t)count
);
492 static int dasync_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
496 return SHA1_Final(md
, data(ctx
));
503 static int dasync_pub_enc(int flen
, const unsigned char *from
,
504 unsigned char *to
, RSA
*rsa
, int padding
) {
505 /* Ignore errors - we carry on anyway */
507 return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
508 (flen
, from
, to
, rsa
, padding
);
511 static int dasync_pub_dec(int flen
, const unsigned char *from
,
512 unsigned char *to
, RSA
*rsa
, int padding
) {
513 /* Ignore errors - we carry on anyway */
515 return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
516 (flen
, from
, to
, rsa
, padding
);
519 static int dasync_rsa_priv_enc(int flen
, const unsigned char *from
,
520 unsigned char *to
, RSA
*rsa
, int padding
)
522 /* Ignore errors - we carry on anyway */
524 return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
525 (flen
, from
, to
, rsa
, padding
);
528 static int dasync_rsa_priv_dec(int flen
, const unsigned char *from
,
529 unsigned char *to
, RSA
*rsa
, int padding
)
531 /* Ignore errors - we carry on anyway */
533 return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
534 (flen
, from
, to
, rsa
, padding
);
537 static int dasync_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
539 /* Ignore errors - we carry on anyway */
541 return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0
, I
, rsa
, ctx
);
544 static int dasync_rsa_init(RSA
*rsa
)
546 return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa
);
548 static int dasync_rsa_finish(RSA
*rsa
)
550 return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa
);
553 /* Cipher helper functions */
555 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
556 void *ptr
, int aeadcapable
)
559 struct dasync_pipeline_ctx
*pipe_ctx
=
560 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
562 if (pipe_ctx
== NULL
)
566 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
:
567 pipe_ctx
->numpipes
= arg
;
568 pipe_ctx
->outbufs
= (unsigned char **)ptr
;
571 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS
:
572 pipe_ctx
->numpipes
= arg
;
573 pipe_ctx
->inbufs
= (unsigned char **)ptr
;
576 case EVP_CTRL_SET_PIPELINE_INPUT_LENS
:
577 pipe_ctx
->numpipes
= arg
;
578 pipe_ctx
->lens
= (size_t *)ptr
;
581 case EVP_CTRL_AEAD_SET_MAC_KEY
:
584 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
->inner_cipher_data
);
585 ret
= EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
586 (ctx
, type
, arg
, ptr
);
587 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
);
590 case EVP_CTRL_AEAD_TLS1_AAD
:
592 unsigned char *p
= ptr
;
595 if (!aeadcapable
|| arg
!= EVP_AEAD_TLS1_AAD_LEN
)
598 if (pipe_ctx
->aadctr
>= SSL_MAX_PIPELINES
)
601 memcpy(pipe_ctx
->tlsaad
[pipe_ctx
->aadctr
], ptr
,
602 EVP_AEAD_TLS1_AAD_LEN
);
605 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
608 if ((p
[arg
- 4] << 8 | p
[arg
- 3]) >= TLS1_1_VERSION
) {
609 if (len
< AES_BLOCK_SIZE
)
611 len
-= AES_BLOCK_SIZE
;
614 return ((len
+ SHA_DIGEST_LENGTH
+ AES_BLOCK_SIZE
)
615 & -AES_BLOCK_SIZE
) - len
;
617 return SHA_DIGEST_LENGTH
;
628 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX
*ctx
,
629 const unsigned char *key
,
630 const unsigned char *iv
, int enc
,
631 const EVP_CIPHER
*cipher
)
634 struct dasync_pipeline_ctx
*pipe_ctx
=
635 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
637 if (pipe_ctx
->inner_cipher_data
== NULL
638 && EVP_CIPHER_impl_ctx_size(cipher
) != 0) {
639 pipe_ctx
->inner_cipher_data
= OPENSSL_zalloc(
640 EVP_CIPHER_impl_ctx_size(cipher
));
641 if (pipe_ctx
->inner_cipher_data
== NULL
) {
642 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER
,
643 ERR_R_MALLOC_FAILURE
);
648 pipe_ctx
->numpipes
= 0;
649 pipe_ctx
->aadctr
= 0;
651 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
->inner_cipher_data
);
652 ret
= EVP_CIPHER_meth_get_init(cipher
)(ctx
, key
, iv
, enc
);
653 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
);
658 static int dasync_cipher_helper(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
659 const unsigned char *in
, size_t inl
,
660 const EVP_CIPHER
*cipher
)
663 unsigned int i
, pipes
;
664 struct dasync_pipeline_ctx
*pipe_ctx
=
665 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
667 pipes
= pipe_ctx
->numpipes
;
668 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
->inner_cipher_data
);
670 if (pipe_ctx
->aadctr
!= 0) {
671 if (pipe_ctx
->aadctr
!= 1)
673 EVP_CIPHER_meth_get_ctrl(cipher
)
674 (ctx
, EVP_CTRL_AEAD_TLS1_AAD
,
675 EVP_AEAD_TLS1_AAD_LEN
,
676 pipe_ctx
->tlsaad
[0]);
678 ret
= EVP_CIPHER_meth_get_do_cipher(cipher
)
681 if (pipe_ctx
->aadctr
> 0 && pipe_ctx
->aadctr
!= pipes
)
683 for (i
= 0; i
< pipes
; i
++) {
684 if (pipe_ctx
->aadctr
> 0) {
685 EVP_CIPHER_meth_get_ctrl(cipher
)
686 (ctx
, EVP_CTRL_AEAD_TLS1_AAD
,
687 EVP_AEAD_TLS1_AAD_LEN
,
688 pipe_ctx
->tlsaad
[i
]);
690 ret
= ret
&& EVP_CIPHER_meth_get_do_cipher(cipher
)
691 (ctx
, pipe_ctx
->outbufs
[i
], pipe_ctx
->inbufs
[i
],
694 pipe_ctx
->numpipes
= 0;
696 pipe_ctx
->aadctr
= 0;
697 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
);
701 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX
*ctx
,
702 const EVP_CIPHER
*cipher
)
704 struct dasync_pipeline_ctx
*pipe_ctx
=
705 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
707 OPENSSL_clear_free(pipe_ctx
->inner_cipher_data
,
708 EVP_CIPHER_impl_ctx_size(cipher
));
714 * AES128 CBC Implementation
717 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
720 return dasync_cipher_ctrl_helper(ctx
, type
, arg
, ptr
, 0);
723 static int dasync_aes128_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
724 const unsigned char *iv
, int enc
)
726 return dasync_cipher_init_key_helper(ctx
, key
, iv
, enc
, EVP_aes_128_cbc());
729 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
730 const unsigned char *in
, size_t inl
)
732 return dasync_cipher_helper(ctx
, out
, in
, inl
, EVP_aes_128_cbc());
735 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX
*ctx
)
737 return dasync_cipher_cleanup_helper(ctx
, EVP_aes_128_cbc());
742 * AES128 CBC HMAC SHA1 Implementation
745 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
,
748 return dasync_cipher_ctrl_helper(ctx
, type
, arg
, ptr
, 1);
751 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
752 const unsigned char *key
,
753 const unsigned char *iv
,
756 return dasync_cipher_init_key_helper(ctx
, key
, iv
, enc
,
757 EVP_aes_128_cbc_hmac_sha1());
760 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
,
762 const unsigned char *in
,
765 return dasync_cipher_helper(ctx
, out
, in
, inl
, EVP_aes_128_cbc_hmac_sha1());
768 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX
*ctx
)
770 return dasync_cipher_cleanup_helper(ctx
, EVP_aes_128_cbc_hmac_sha1());