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_internal(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);
104 static EVP_MD *_hidden_sha1_md = NULL;
105 static const EVP_MD *dasync_sha1(void)
107 if (_hidden_sha1_md == NULL) {
110 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
111 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
112 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
113 || !EVP_MD_meth_set_app_datasize(md,
114 sizeof(EVP_MD *) + sizeof(SHA_CTX))
115 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
116 || !EVP_MD_meth_set_init(md, dasync_sha1_init)
117 || !EVP_MD_meth_set_update(md, dasync_sha1_update)
118 || !EVP_MD_meth_set_final(md, dasync_sha1_final)) {
119 EVP_MD_meth_free(md);
122 _hidden_sha1_md = md;
124 return _hidden_sha1_md;
126 static void destroy_digests(void)
128 EVP_MD_meth_free(_hidden_sha1_md);
129 _hidden_sha1_md = NULL;
131 static int dasync_digest_nids(const int **nids)
133 static int digest_nids[2] = { 0, 0 };
139 if ((md = dasync_sha1()) != NULL)
140 digest_nids[pos++] = EVP_MD_type(md);
141 digest_nids[pos] = 0;
150 static int dasync_pub_enc(int flen, const unsigned char *from,
151 unsigned char *to, RSA *rsa, int padding);
152 static int dasync_pub_dec(int flen, const unsigned char *from,
153 unsigned char *to, RSA *rsa, int padding);
154 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
155 unsigned char *to, RSA *rsa, int padding);
156 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
157 unsigned char *to, RSA *rsa, int padding);
158 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
161 static int dasync_rsa_init(RSA *rsa);
162 static int dasync_rsa_finish(RSA *rsa);
164 static RSA_METHOD dasync_rsa_method = {
165 "Dummy Async RSA method",
166 dasync_pub_enc, /* pub_enc */
167 dasync_pub_dec, /* pub_dec */
168 dasync_rsa_priv_enc, /* priv_enc */
169 dasync_rsa_priv_dec, /* priv_dec */
170 dasync_rsa_mod_exp, /* rsa_mod_exp */
171 BN_mod_exp_mont, /* bn_mod_exp */
172 dasync_rsa_init, /* init */
173 dasync_rsa_finish, /* finish */
178 NULL /* rsa_keygen */
184 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
186 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
187 const unsigned char *iv, int enc);
188 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
189 const unsigned char *in, size_t inl);
190 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
192 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
194 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
195 const unsigned char *key,
196 const unsigned char *iv,
198 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
200 const unsigned char *in,
202 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
204 struct aes_128_cbc_pipeline_ctx {
205 void *inner_cipher_data;
206 unsigned char dummy[256];
207 unsigned int numpipes;
208 unsigned char **inbufs;
209 unsigned char **outbufs;
212 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
216 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
217 static const EVP_CIPHER *dasync_aes_128_cbc(void)
219 if (_hidden_aes_128_cbc == NULL)
220 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
223 if (_hidden_aes_128_cbc == NULL
224 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
225 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
226 EVP_CIPH_FLAG_DEFAULT_ASN1
228 | EVP_CIPH_FLAG_PIPELINE)
229 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
230 dasync_aes128_init_key)
231 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
232 dasync_aes128_cbc_cipher)
233 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
234 dasync_aes128_cbc_cleanup)
235 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
236 dasync_aes128_cbc_ctrl)
237 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
238 sizeof(struct aes_128_cbc_pipeline_ctx))) {
239 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
240 _hidden_aes_128_cbc = NULL;
242 return _hidden_aes_128_cbc;
245 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
246 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
248 if (_hidden_aes_128_cbc_hmac_sha1 == NULL)
249 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
250 NID_aes_128_cbc_hmac_sha1,
253 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
254 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
255 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
257 | EVP_CIPH_FLAG_DEFAULT_ASN1
258 | EVP_CIPH_FLAG_AEAD_CIPHER
259 | EVP_CIPH_FLAG_PIPELINE)
260 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
261 dasync_aes128_cbc_hmac_sha1_init_key)
262 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
263 dasync_aes128_cbc_hmac_sha1_cipher)
264 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
265 dasync_aes128_cbc_hmac_sha1_cleanup)
266 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
267 dasync_aes128_cbc_hmac_sha1_ctrl)
268 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
269 sizeof(struct aes_128_cbc_pipeline_ctx))) {
270 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
271 _hidden_aes_128_cbc_hmac_sha1 = NULL;
273 return _hidden_aes_128_cbc_hmac_sha1;
276 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
277 const int **nids, int nid);
279 static int dasync_cipher_nids[] = {
281 NID_aes_128_cbc_hmac_sha1,
285 static int bind_dasync(ENGINE *e)
287 /* Ensure the dasync error handling is set up */
288 ERR_load_DASYNC_strings();
290 if (!ENGINE_set_id(e, engine_dasync_id)
291 || !ENGINE_set_name(e, engine_dasync_name)
292 || !ENGINE_set_RSA(e, &dasync_rsa_method)
293 || !ENGINE_set_digests(e, dasync_digests)
294 || !ENGINE_set_ciphers(e, dasync_ciphers)
295 || !ENGINE_set_destroy_function(e, dasync_destroy)
296 || !ENGINE_set_init_function(e, dasync_init)
297 || !ENGINE_set_finish_function(e, dasync_finish)) {
298 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
305 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
306 static int bind_helper(ENGINE *e, const char *id)
308 if (id && (strcmp(id, engine_dasync_id) != 0))
315 IMPLEMENT_DYNAMIC_CHECK_FN()
316 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
319 static ENGINE *engine_dasync(void)
321 ENGINE *ret = ENGINE_new();
324 if (!bind_dasync(ret)) {
331 void engine_load_dasync_internal(void)
333 ENGINE *toadd = engine_dasync();
341 static int dasync_init(ENGINE *e)
347 static int dasync_finish(ENGINE *e)
353 static int dasync_destroy(ENGINE *e)
356 ERR_unload_DASYNC_strings();
360 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
361 const int **nids, int nid)
365 /* We are returning a list of supported nids */
366 return dasync_digest_nids(nids);
368 /* We are being asked for a specific digest */
371 *digest = dasync_sha1();
381 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
382 const int **nids, int nid)
386 /* We are returning a list of supported nids */
387 *nids = dasync_cipher_nids;
388 return (sizeof(dasync_cipher_nids) -
389 1) / sizeof(dasync_cipher_nids[0]);
391 /* We are being asked for a specific cipher */
393 case NID_aes_128_cbc:
394 *cipher = dasync_aes_128_cbc();
396 case NID_aes_128_cbc_hmac_sha1:
397 *cipher = dasync_aes_128_cbc_hmac_sha1();
407 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
408 OSSL_ASYNC_FD readfd, void *pvwritefd)
410 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
411 #if defined(ASYNC_WIN)
413 CloseHandle(*pwritefd);
414 #elif defined(ASYNC_POSIX)
418 OPENSSL_free(pwritefd);
421 #define DUMMY_CHAR 'X'
423 static void dummy_pause_job(void) {
425 ASYNC_WAIT_CTX *waitctx;
426 OSSL_ASYNC_FD pipefds[2] = {0, 0};
427 OSSL_ASYNC_FD *writefd;
428 #if defined(ASYNC_WIN)
429 DWORD numwritten, numread;
430 char buf = DUMMY_CHAR;
431 #elif defined(ASYNC_POSIX)
432 char buf = DUMMY_CHAR;
435 if ((job = ASYNC_get_current_job()) == NULL)
438 waitctx = ASYNC_get_wait_ctx(job);
440 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
441 (void **)&writefd)) {
442 pipefds[1] = *writefd;
444 writefd = OPENSSL_malloc(sizeof(*writefd));
447 #if defined(ASYNC_WIN)
448 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
449 OPENSSL_free(writefd);
452 #elif defined(ASYNC_POSIX)
453 if (pipe(pipefds) != 0) {
454 OPENSSL_free(writefd);
458 *writefd = pipefds[1];
460 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
461 writefd, wait_cleanup)) {
462 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
467 * In the Dummy async engine we are cheating. We signal that the job
468 * is complete by waking it before the call to ASYNC_pause_job(). A real
469 * async engine would only wake when the job was actually complete
471 #if defined(ASYNC_WIN)
472 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
473 #elif defined(ASYNC_POSIX)
474 if (write(pipefds[1], &buf, 1) < 0)
478 /* Ignore errors - we carry on anyway */
481 /* Clear the wake signal */
482 #if defined(ASYNC_WIN)
483 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
484 #elif defined(ASYNC_POSIX)
485 if (read(pipefds[0], &buf, 1) < 0)
491 * SHA1 implementation. At the moment we just defer to the standard
495 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
496 static int dasync_sha1_init(EVP_MD_CTX *ctx)
500 return SHA1_Init(data(ctx));
503 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
508 return SHA1_Update(data(ctx), data, (size_t)count);
511 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
515 return SHA1_Final(md, data(ctx));
522 static int dasync_pub_enc(int flen, const unsigned char *from,
523 unsigned char *to, RSA *rsa, int padding) {
524 /* Ignore errors - we carry on anyway */
526 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
529 static int dasync_pub_dec(int flen, const unsigned char *from,
530 unsigned char *to, RSA *rsa, int padding) {
531 /* Ignore errors - we carry on anyway */
533 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
536 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
537 unsigned char *to, RSA *rsa, int padding)
539 /* Ignore errors - we carry on anyway */
541 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
544 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
545 unsigned char *to, RSA *rsa, int padding)
547 /* Ignore errors - we carry on anyway */
549 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
552 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
554 /* Ignore errors - we carry on anyway */
556 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
559 static int dasync_rsa_init(RSA *rsa)
561 return RSA_PKCS1_OpenSSL()->init(rsa);
563 static int dasync_rsa_finish(RSA *rsa)
565 return RSA_PKCS1_OpenSSL()->finish(rsa);
569 * AES128 Implementation
572 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
575 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
576 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
578 if (pipe_ctx == NULL)
582 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
583 pipe_ctx->numpipes = arg;
584 pipe_ctx->outbufs = (unsigned char **)ptr;
587 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
588 pipe_ctx->numpipes = arg;
589 pipe_ctx->inbufs = (unsigned char **)ptr;
592 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
593 pipe_ctx->numpipes = arg;
594 pipe_ctx->lens = (size_t *)ptr;
604 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
605 const unsigned char *iv, int enc)
608 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
609 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
611 if (pipe_ctx->inner_cipher_data == NULL
612 && EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()) != 0) {
613 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
614 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()));
615 if (pipe_ctx->inner_cipher_data == NULL) {
616 DASYNCerr(DASYNC_F_DASYNC_AES128_INIT_KEY,
617 ERR_R_MALLOC_FAILURE);
622 pipe_ctx->numpipes = 0;
623 pipe_ctx->aadctr = 0;
625 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
626 ret = EVP_CIPHER_meth_get_init(EVP_aes_128_cbc())(ctx, key, iv, enc);
627 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
632 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
633 const unsigned char *in, size_t inl)
636 unsigned int i, pipes;
637 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
638 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
640 pipes = pipe_ctx->numpipes;
641 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
643 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())
646 for (i = 0; i < pipes; i++) {
647 ret = ret && EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())
648 (ctx, pipe_ctx->outbufs[i],
652 pipe_ctx->numpipes = 0;
654 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
658 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
660 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
661 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
663 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
664 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()));
671 * AES128 CBC HMAC SHA1 Implementation
674 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
677 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
678 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
681 if (pipe_ctx == NULL)
685 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
686 pipe_ctx->numpipes = arg;
687 pipe_ctx->outbufs = (unsigned char **)ptr;
690 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
691 pipe_ctx->numpipes = arg;
692 pipe_ctx->inbufs = (unsigned char **)ptr;
695 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
696 pipe_ctx->numpipes = arg;
697 pipe_ctx->lens = (size_t *)ptr;
700 case EVP_CTRL_AEAD_SET_MAC_KEY:
701 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
702 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
703 (ctx, type, arg, ptr);
704 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
707 case EVP_CTRL_AEAD_TLS1_AAD:
709 unsigned char *p = ptr;
712 if (arg != EVP_AEAD_TLS1_AAD_LEN)
715 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
718 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
719 EVP_AEAD_TLS1_AAD_LEN);
722 len = p[arg - 2] << 8 | p[arg - 1];
725 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
726 len -= AES_BLOCK_SIZE;
729 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
730 & -AES_BLOCK_SIZE) - len;
732 return SHA_DIGEST_LENGTH;
744 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
745 const unsigned char *key,
746 const unsigned char *iv,
750 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
751 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
753 if (pipe_ctx->inner_cipher_data == NULL
754 && EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc_hmac_sha1())
756 pipe_ctx->inner_cipher_data =
757 OPENSSL_zalloc(EVP_CIPHER_impl_ctx_size(
758 EVP_aes_128_cbc_hmac_sha1()));
759 if (pipe_ctx->inner_cipher_data == NULL) {
760 DASYNCerr(DASYNC_F_DASYNC_AES128_CBC_HMAC_SHA1_INIT_KEY,
761 ERR_R_MALLOC_FAILURE);
766 pipe_ctx->numpipes = 0;
769 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
770 ret = EVP_CIPHER_meth_get_init(EVP_aes_128_cbc_hmac_sha1())
772 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
777 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
779 const unsigned char *in,
783 unsigned int i, pipes;
784 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
785 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
787 pipes = pipe_ctx->numpipes;
788 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
790 if (pipe_ctx->aadctr != 0) {
791 if (pipe_ctx->aadctr != 1)
793 EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
794 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
795 EVP_AEAD_TLS1_AAD_LEN,
796 pipe_ctx->tlsaad[0]);
798 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc_hmac_sha1())
801 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
803 for (i = 0; i < pipes; i++) {
804 if (pipe_ctx->aadctr > 0) {
805 EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
806 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
807 EVP_AEAD_TLS1_AAD_LEN,
808 pipe_ctx->tlsaad[i]);
810 ret = ret && EVP_CIPHER_meth_get_do_cipher(
811 EVP_aes_128_cbc_hmac_sha1())
812 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
815 pipe_ctx->numpipes = 0;
817 pipe_ctx->aadctr = 0;
818 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
822 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
824 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
825 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
827 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
828 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc_hmac_sha1()));