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
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_THREADS)
37 #include "e_dasync_err.c"
39 /* Engine Id and Name */
40 static const char *engine_dasync_id
= "dasync";
41 static const char *engine_dasync_name
= "Dummy Async engine support";
44 /* Engine Lifetime functions */
45 static int dasync_destroy(ENGINE
*e
);
46 static int dasync_init(ENGINE
*e
);
47 static int dasync_finish(ENGINE
*e
);
48 void engine_load_dasync_int(void);
51 /* Set up digests. Just SHA1 for now */
52 static int dasync_digests(ENGINE
*e
, const EVP_MD
**digest
,
53 const int **nids
, int nid
);
55 static void dummy_pause_job(void);
58 static int dasync_sha1_init(EVP_MD_CTX
*ctx
);
59 static int dasync_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
61 static int dasync_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
64 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
65 * engine bind and can then be reused many times.
67 static EVP_MD
*_hidden_sha1_md
= NULL
;
68 static const EVP_MD
*dasync_sha1(void)
70 return _hidden_sha1_md
;
72 static void destroy_digests(void)
74 EVP_MD_meth_free(_hidden_sha1_md
);
75 _hidden_sha1_md
= NULL
;
78 static int dasync_digest_nids(const int **nids
)
80 static int digest_nids
[2] = { 0, 0 };
86 if ((md
= dasync_sha1()) != NULL
)
87 digest_nids
[pos
++] = EVP_MD_type(md
);
97 static int dasync_pub_enc(int flen
, const unsigned char *from
,
98 unsigned char *to
, RSA
*rsa
, int padding
);
99 static int dasync_pub_dec(int flen
, const unsigned char *from
,
100 unsigned char *to
, RSA
*rsa
, int padding
);
101 static int dasync_rsa_priv_enc(int flen
, const unsigned char *from
,
102 unsigned char *to
, RSA
*rsa
, int padding
);
103 static int dasync_rsa_priv_dec(int flen
, const unsigned char *from
,
104 unsigned char *to
, RSA
*rsa
, int padding
);
105 static int dasync_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
,
108 static int dasync_rsa_init(RSA
*rsa
);
109 static int dasync_rsa_finish(RSA
*rsa
);
111 static RSA_METHOD
*dasync_rsa_method
= NULL
;
115 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
117 static int dasync_aes128_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
118 const unsigned char *iv
, int enc
);
119 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
120 const unsigned char *in
, size_t inl
);
121 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX
*ctx
);
123 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
,
125 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
126 const unsigned char *key
,
127 const unsigned char *iv
,
129 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
,
131 const unsigned char *in
,
133 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX
*ctx
);
135 struct dasync_pipeline_ctx
{
136 void *inner_cipher_data
;
137 unsigned int numpipes
;
138 unsigned char **inbufs
;
139 unsigned char **outbufs
;
141 unsigned char tlsaad
[SSL_MAX_PIPELINES
][EVP_AEAD_TLS1_AAD_LEN
];
146 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
147 * during engine bind and can then be reused many times.
149 static EVP_CIPHER
*_hidden_aes_128_cbc
= NULL
;
150 static const EVP_CIPHER
*dasync_aes_128_cbc(void)
152 return _hidden_aes_128_cbc
;
156 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
157 * once only during engine bind and can then be reused many times.
159 * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
160 * which is implemented only if the AES-NI instruction set extension is available
161 * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
162 * be available either.
164 * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
165 * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
167 static EVP_CIPHER
*_hidden_aes_128_cbc_hmac_sha1
= NULL
;
168 static const EVP_CIPHER
*dasync_aes_128_cbc_hmac_sha1(void)
170 return _hidden_aes_128_cbc_hmac_sha1
;
173 static void destroy_ciphers(void)
175 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
176 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1
);
177 _hidden_aes_128_cbc
= NULL
;
178 _hidden_aes_128_cbc_hmac_sha1
= NULL
;
181 static int dasync_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
182 const int **nids
, int nid
);
184 static int dasync_cipher_nids
[] = {
186 NID_aes_128_cbc_hmac_sha1
,
190 static int bind_dasync(ENGINE
*e
)
192 /* Setup RSA_METHOD */
193 if ((dasync_rsa_method
= RSA_meth_new("Dummy Async RSA method", 0)) == NULL
194 || RSA_meth_set_pub_enc(dasync_rsa_method
, dasync_pub_enc
) == 0
195 || RSA_meth_set_pub_dec(dasync_rsa_method
, dasync_pub_dec
) == 0
196 || RSA_meth_set_priv_enc(dasync_rsa_method
, dasync_rsa_priv_enc
) == 0
197 || RSA_meth_set_priv_dec(dasync_rsa_method
, dasync_rsa_priv_dec
) == 0
198 || RSA_meth_set_mod_exp(dasync_rsa_method
, dasync_rsa_mod_exp
) == 0
199 || RSA_meth_set_bn_mod_exp(dasync_rsa_method
, BN_mod_exp_mont
) == 0
200 || RSA_meth_set_init(dasync_rsa_method
, dasync_rsa_init
) == 0
201 || RSA_meth_set_finish(dasync_rsa_method
, dasync_rsa_finish
) == 0) {
202 DASYNCerr(DASYNC_F_BIND_DASYNC
, DASYNC_R_INIT_FAILED
);
206 /* Ensure the dasync error handling is set up */
207 ERR_load_DASYNC_strings();
209 if (!ENGINE_set_id(e
, engine_dasync_id
)
210 || !ENGINE_set_name(e
, engine_dasync_name
)
211 || !ENGINE_set_RSA(e
, dasync_rsa_method
)
212 || !ENGINE_set_digests(e
, dasync_digests
)
213 || !ENGINE_set_ciphers(e
, dasync_ciphers
)
214 || !ENGINE_set_destroy_function(e
, dasync_destroy
)
215 || !ENGINE_set_init_function(e
, dasync_init
)
216 || !ENGINE_set_finish_function(e
, dasync_finish
)) {
217 DASYNCerr(DASYNC_F_BIND_DASYNC
, DASYNC_R_INIT_FAILED
);
222 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
223 * supplied by this engine
225 _hidden_sha1_md
= EVP_MD_meth_new(NID_sha1
, NID_sha1WithRSAEncryption
);
226 if (_hidden_sha1_md
== NULL
227 || !EVP_MD_meth_set_result_size(_hidden_sha1_md
, SHA_DIGEST_LENGTH
)
228 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md
, SHA_CBLOCK
)
229 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md
,
230 sizeof(EVP_MD
*) + sizeof(SHA_CTX
))
231 || !EVP_MD_meth_set_flags(_hidden_sha1_md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
232 || !EVP_MD_meth_set_init(_hidden_sha1_md
, dasync_sha1_init
)
233 || !EVP_MD_meth_set_update(_hidden_sha1_md
, dasync_sha1_update
)
234 || !EVP_MD_meth_set_final(_hidden_sha1_md
, dasync_sha1_final
)) {
235 EVP_MD_meth_free(_hidden_sha1_md
);
236 _hidden_sha1_md
= NULL
;
239 _hidden_aes_128_cbc
= EVP_CIPHER_meth_new(NID_aes_128_cbc
,
242 if (_hidden_aes_128_cbc
== NULL
243 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc
,16)
244 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc
,
245 EVP_CIPH_FLAG_DEFAULT_ASN1
247 | EVP_CIPH_FLAG_PIPELINE
)
248 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc
,
249 dasync_aes128_init_key
)
250 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc
,
251 dasync_aes128_cbc_cipher
)
252 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc
,
253 dasync_aes128_cbc_cleanup
)
254 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc
,
255 dasync_aes128_cbc_ctrl
)
256 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc
,
257 sizeof(struct dasync_pipeline_ctx
))) {
258 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
259 _hidden_aes_128_cbc
= NULL
;
262 _hidden_aes_128_cbc_hmac_sha1
= EVP_CIPHER_meth_new(
263 NID_aes_128_cbc_hmac_sha1
,
266 if (_hidden_aes_128_cbc_hmac_sha1
== NULL
267 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1
,16)
268 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1
,
270 | EVP_CIPH_FLAG_DEFAULT_ASN1
271 | EVP_CIPH_FLAG_AEAD_CIPHER
272 | EVP_CIPH_FLAG_PIPELINE
)
273 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1
,
274 dasync_aes128_cbc_hmac_sha1_init_key
)
275 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1
,
276 dasync_aes128_cbc_hmac_sha1_cipher
)
277 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1
,
278 dasync_aes128_cbc_hmac_sha1_cleanup
)
279 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1
,
280 dasync_aes128_cbc_hmac_sha1_ctrl
)
281 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1
,
282 sizeof(struct dasync_pipeline_ctx
))) {
283 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1
);
284 _hidden_aes_128_cbc_hmac_sha1
= NULL
;
290 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
291 static int bind_helper(ENGINE
*e
, const char *id
)
293 if (id
&& (strcmp(id
, engine_dasync_id
) != 0))
300 IMPLEMENT_DYNAMIC_CHECK_FN()
301 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
304 static ENGINE
*engine_dasync(void)
306 ENGINE
*ret
= ENGINE_new();
309 if (!bind_dasync(ret
)) {
316 void engine_load_dasync_int(void)
318 ENGINE
*toadd
= engine_dasync();
326 static int dasync_init(ENGINE
*e
)
332 static int dasync_finish(ENGINE
*e
)
338 static int dasync_destroy(ENGINE
*e
)
342 RSA_meth_free(dasync_rsa_method
);
343 ERR_unload_DASYNC_strings();
347 static int dasync_digests(ENGINE
*e
, const EVP_MD
**digest
,
348 const int **nids
, int nid
)
352 /* We are returning a list of supported nids */
353 return dasync_digest_nids(nids
);
355 /* We are being asked for a specific digest */
358 *digest
= dasync_sha1();
368 static int dasync_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
369 const int **nids
, int nid
)
372 if (cipher
== NULL
) {
373 /* We are returning a list of supported nids */
374 *nids
= dasync_cipher_nids
;
375 return (sizeof(dasync_cipher_nids
) -
376 1) / sizeof(dasync_cipher_nids
[0]);
378 /* We are being asked for a specific cipher */
380 case NID_aes_128_cbc
:
381 *cipher
= dasync_aes_128_cbc();
383 case NID_aes_128_cbc_hmac_sha1
:
384 *cipher
= dasync_aes_128_cbc_hmac_sha1();
394 static void wait_cleanup(ASYNC_WAIT_CTX
*ctx
, const void *key
,
395 OSSL_ASYNC_FD readfd
, void *pvwritefd
)
397 OSSL_ASYNC_FD
*pwritefd
= (OSSL_ASYNC_FD
*)pvwritefd
;
398 #if defined(ASYNC_WIN)
400 CloseHandle(*pwritefd
);
401 #elif defined(ASYNC_POSIX)
405 OPENSSL_free(pwritefd
);
408 #define DUMMY_CHAR 'X'
410 static void dummy_pause_job(void) {
412 ASYNC_WAIT_CTX
*waitctx
;
413 ASYNC_callback_fn callback
;
415 OSSL_ASYNC_FD pipefds
[2] = {0, 0};
416 OSSL_ASYNC_FD
*writefd
;
417 #if defined(ASYNC_WIN)
418 DWORD numwritten
, numread
;
419 char buf
= DUMMY_CHAR
;
420 #elif defined(ASYNC_POSIX)
421 char buf
= DUMMY_CHAR
;
424 if ((job
= ASYNC_get_current_job()) == NULL
)
427 waitctx
= ASYNC_get_wait_ctx(job
);
429 if (ASYNC_WAIT_CTX_get_callback(waitctx
, &callback
, &callback_arg
) && callback
!= NULL
) {
431 * In the Dummy async engine we are cheating. We call the callback that the job
432 * is complete before the call to ASYNC_pause_job(). A real
433 * async engine would only call the callback when the job was actually complete
435 (*callback
)(callback_arg
);
441 if (ASYNC_WAIT_CTX_get_fd(waitctx
, engine_dasync_id
, &pipefds
[0],
442 (void **)&writefd
)) {
443 pipefds
[1] = *writefd
;
445 writefd
= OPENSSL_malloc(sizeof(*writefd
));
448 #if defined(ASYNC_WIN)
449 if (CreatePipe(&pipefds
[0], &pipefds
[1], NULL
, 256) == 0) {
450 OPENSSL_free(writefd
);
453 #elif defined(ASYNC_POSIX)
454 if (pipe(pipefds
) != 0) {
455 OPENSSL_free(writefd
);
459 *writefd
= pipefds
[1];
461 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx
, engine_dasync_id
, pipefds
[0],
462 writefd
, wait_cleanup
)) {
463 wait_cleanup(waitctx
, engine_dasync_id
, pipefds
[0], writefd
);
468 * In the Dummy async engine we are cheating. We signal that the job
469 * is complete by waking it before the call to ASYNC_pause_job(). A real
470 * async engine would only wake when the job was actually complete
472 #if defined(ASYNC_WIN)
473 WriteFile(pipefds
[1], &buf
, 1, &numwritten
, NULL
);
474 #elif defined(ASYNC_POSIX)
475 if (write(pipefds
[1], &buf
, 1) < 0)
479 /* Ignore errors - we carry on anyway */
482 /* Clear the wake signal */
483 #if defined(ASYNC_WIN)
484 ReadFile(pipefds
[0], &buf
, 1, &numread
, NULL
);
485 #elif defined(ASYNC_POSIX)
486 if (read(pipefds
[0], &buf
, 1) < 0)
492 * SHA1 implementation. At the moment we just defer to the standard
496 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
497 static int dasync_sha1_init(EVP_MD_CTX
*ctx
)
501 return SHA1_Init(data(ctx
));
504 static int dasync_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
509 return SHA1_Update(data(ctx
), data
, (size_t)count
);
512 static int dasync_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
516 return SHA1_Final(md
, data(ctx
));
523 static int dasync_pub_enc(int flen
, const unsigned char *from
,
524 unsigned char *to
, RSA
*rsa
, int padding
) {
525 /* Ignore errors - we carry on anyway */
527 return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
528 (flen
, from
, to
, rsa
, padding
);
531 static int dasync_pub_dec(int flen
, const unsigned char *from
,
532 unsigned char *to
, RSA
*rsa
, int padding
) {
533 /* Ignore errors - we carry on anyway */
535 return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
536 (flen
, from
, to
, rsa
, padding
);
539 static int dasync_rsa_priv_enc(int flen
, const unsigned char *from
,
540 unsigned char *to
, RSA
*rsa
, int padding
)
542 /* Ignore errors - we carry on anyway */
544 return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
545 (flen
, from
, to
, rsa
, padding
);
548 static int dasync_rsa_priv_dec(int flen
, const unsigned char *from
,
549 unsigned char *to
, RSA
*rsa
, int padding
)
551 /* Ignore errors - we carry on anyway */
553 return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
554 (flen
, from
, to
, rsa
, padding
);
557 static int dasync_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
559 /* Ignore errors - we carry on anyway */
561 return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0
, I
, rsa
, ctx
);
564 static int dasync_rsa_init(RSA
*rsa
)
566 return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa
);
568 static int dasync_rsa_finish(RSA
*rsa
)
570 return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa
);
573 /* Cipher helper functions */
575 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
576 void *ptr
, int aeadcapable
)
579 struct dasync_pipeline_ctx
*pipe_ctx
=
580 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
582 if (pipe_ctx
== NULL
)
586 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
:
587 pipe_ctx
->numpipes
= arg
;
588 pipe_ctx
->outbufs
= (unsigned char **)ptr
;
591 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS
:
592 pipe_ctx
->numpipes
= arg
;
593 pipe_ctx
->inbufs
= (unsigned char **)ptr
;
596 case EVP_CTRL_SET_PIPELINE_INPUT_LENS
:
597 pipe_ctx
->numpipes
= arg
;
598 pipe_ctx
->lens
= (size_t *)ptr
;
601 case EVP_CTRL_AEAD_SET_MAC_KEY
:
604 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
->inner_cipher_data
);
605 ret
= EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
606 (ctx
, type
, arg
, ptr
);
607 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
);
610 case EVP_CTRL_AEAD_TLS1_AAD
:
612 unsigned char *p
= ptr
;
615 if (!aeadcapable
|| arg
!= EVP_AEAD_TLS1_AAD_LEN
)
618 if (pipe_ctx
->aadctr
>= SSL_MAX_PIPELINES
)
621 memcpy(pipe_ctx
->tlsaad
[pipe_ctx
->aadctr
], ptr
,
622 EVP_AEAD_TLS1_AAD_LEN
);
625 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
627 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
628 if ((p
[arg
- 4] << 8 | p
[arg
- 3]) >= TLS1_1_VERSION
) {
629 if (len
< AES_BLOCK_SIZE
)
631 len
-= AES_BLOCK_SIZE
;
634 return ((len
+ SHA_DIGEST_LENGTH
+ AES_BLOCK_SIZE
)
635 & -AES_BLOCK_SIZE
) - len
;
637 return SHA_DIGEST_LENGTH
;
648 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX
*ctx
,
649 const unsigned char *key
,
650 const unsigned char *iv
, int enc
,
651 const EVP_CIPHER
*cipher
)
654 struct dasync_pipeline_ctx
*pipe_ctx
=
655 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
657 if (pipe_ctx
->inner_cipher_data
== NULL
658 && EVP_CIPHER_impl_ctx_size(cipher
) != 0) {
659 pipe_ctx
->inner_cipher_data
= OPENSSL_zalloc(
660 EVP_CIPHER_impl_ctx_size(cipher
));
661 if (pipe_ctx
->inner_cipher_data
== NULL
) {
662 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER
,
663 ERR_R_MALLOC_FAILURE
);
668 pipe_ctx
->numpipes
= 0;
669 pipe_ctx
->aadctr
= 0;
671 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
->inner_cipher_data
);
672 ret
= EVP_CIPHER_meth_get_init(cipher
)(ctx
, key
, iv
, enc
);
673 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
);
678 static int dasync_cipher_helper(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
679 const unsigned char *in
, size_t inl
,
680 const EVP_CIPHER
*cipher
)
683 unsigned int i
, pipes
;
684 struct dasync_pipeline_ctx
*pipe_ctx
=
685 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
687 pipes
= pipe_ctx
->numpipes
;
688 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
->inner_cipher_data
);
690 if (pipe_ctx
->aadctr
!= 0) {
691 if (pipe_ctx
->aadctr
!= 1)
693 EVP_CIPHER_meth_get_ctrl(cipher
)
694 (ctx
, EVP_CTRL_AEAD_TLS1_AAD
,
695 EVP_AEAD_TLS1_AAD_LEN
,
696 pipe_ctx
->tlsaad
[0]);
698 ret
= EVP_CIPHER_meth_get_do_cipher(cipher
)
701 if (pipe_ctx
->aadctr
> 0 && pipe_ctx
->aadctr
!= pipes
)
703 for (i
= 0; i
< pipes
; i
++) {
704 if (pipe_ctx
->aadctr
> 0) {
705 EVP_CIPHER_meth_get_ctrl(cipher
)
706 (ctx
, EVP_CTRL_AEAD_TLS1_AAD
,
707 EVP_AEAD_TLS1_AAD_LEN
,
708 pipe_ctx
->tlsaad
[i
]);
710 ret
= ret
&& EVP_CIPHER_meth_get_do_cipher(cipher
)
711 (ctx
, pipe_ctx
->outbufs
[i
], pipe_ctx
->inbufs
[i
],
714 pipe_ctx
->numpipes
= 0;
716 pipe_ctx
->aadctr
= 0;
717 EVP_CIPHER_CTX_set_cipher_data(ctx
, pipe_ctx
);
721 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX
*ctx
,
722 const EVP_CIPHER
*cipher
)
724 struct dasync_pipeline_ctx
*pipe_ctx
=
725 (struct dasync_pipeline_ctx
*)EVP_CIPHER_CTX_get_cipher_data(ctx
);
727 OPENSSL_clear_free(pipe_ctx
->inner_cipher_data
,
728 EVP_CIPHER_impl_ctx_size(cipher
));
734 * AES128 CBC Implementation
737 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
740 return dasync_cipher_ctrl_helper(ctx
, type
, arg
, ptr
, 0);
743 static int dasync_aes128_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
744 const unsigned char *iv
, int enc
)
746 return dasync_cipher_init_key_helper(ctx
, key
, iv
, enc
, EVP_aes_128_cbc());
749 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
750 const unsigned char *in
, size_t inl
)
752 return dasync_cipher_helper(ctx
, out
, in
, inl
, EVP_aes_128_cbc());
755 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX
*ctx
)
757 return dasync_cipher_cleanup_helper(ctx
, EVP_aes_128_cbc());
762 * AES128 CBC HMAC SHA1 Implementation
765 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
,
768 return dasync_cipher_ctrl_helper(ctx
, type
, arg
, ptr
, 1);
771 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
772 const unsigned char *key
,
773 const unsigned char *iv
,
777 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
778 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
780 return dasync_cipher_init_key_helper(ctx
, key
, iv
, enc
,
781 EVP_aes_128_cbc_hmac_sha1());
784 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
,
786 const unsigned char *in
,
789 return dasync_cipher_helper(ctx
, out
, in
, inl
, EVP_aes_128_cbc_hmac_sha1());
792 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX
*ctx
)
795 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
796 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
798 return dasync_cipher_cleanup_helper(ctx
, EVP_aes_128_cbc_hmac_sha1());