0fe1d436eecd6b77cf26d12687ae3fbde6dfc848
[openssl.git] / engines / e_dasync.c
1 /*
2  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #if defined(_WIN32)
11 # include <windows.h>
12 #endif
13
14 #include <stdio.h>
15 #include <string.h>
16
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>
27
28 #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
29 # undef ASYNC_POSIX
30 # define ASYNC_POSIX
31 # include <unistd.h>
32 #elif defined(_WIN32)
33 # undef ASYNC_WIN
34 # define ASYNC_WIN
35 #endif
36
37 #include "e_dasync_err.c"
38
39 /* Engine Id and Name */
40 static const char *engine_dasync_id = "dasync";
41 static const char *engine_dasync_name = "Dummy Async engine support";
42
43
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);
49
50
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);
54
55 static void dummy_pause_job(void);
56
57 /* SHA1 */
58 static int dasync_sha1_init(EVP_MD_CTX *ctx);
59 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
60                              size_t count);
61 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
62
63 /*
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.
66  */
67 static EVP_MD *_hidden_sha1_md = NULL;
68 static const EVP_MD *dasync_sha1(void)
69 {
70     return _hidden_sha1_md;
71 }
72 static void destroy_digests(void)
73 {
74     EVP_MD_meth_free(_hidden_sha1_md);
75     _hidden_sha1_md = NULL;
76 }
77
78 static int dasync_digest_nids(const int **nids)
79 {
80     static int digest_nids[2] = { 0, 0 };
81     static int pos = 0;
82     static int init = 0;
83
84     if (!init) {
85         const EVP_MD *md;
86         if ((md = dasync_sha1()) != NULL)
87             digest_nids[pos++] = EVP_MD_type(md);
88         digest_nids[pos] = 0;
89         init = 1;
90     }
91     *nids = digest_nids;
92     return pos;
93 }
94
95 /* RSA */
96
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,
106                               BN_CTX *ctx);
107
108 static int dasync_rsa_init(RSA *rsa);
109 static int dasync_rsa_finish(RSA *rsa);
110
111 static RSA_METHOD *dasync_rsa_method = NULL;
112
113 /* AES */
114
115 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
116                                   void *ptr);
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);
122
123 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
124                                              int arg, void *ptr);
125 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
126                                                  const unsigned char *key,
127                                                  const unsigned char *iv,
128                                                  int enc);
129 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
130                                                unsigned char *out,
131                                                const unsigned char *in,
132                                                size_t inl);
133 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
134
135 struct dasync_pipeline_ctx {
136     void *inner_cipher_data;
137     unsigned int numpipes;
138     unsigned char **inbufs;
139     unsigned char **outbufs;
140     size_t *lens;
141     unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
142     unsigned int aadctr;
143 };
144
145 /*
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.
148  */
149 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
150 static const EVP_CIPHER *dasync_aes_128_cbc(void)
151 {
152     return _hidden_aes_128_cbc;
153 }
154
155 /*
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.
158  */
159 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
160 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
161 {
162     return _hidden_aes_128_cbc_hmac_sha1;
163 }
164
165 static void destroy_ciphers(void)
166 {
167     EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
168     EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
169     _hidden_aes_128_cbc = NULL;
170     _hidden_aes_128_cbc_hmac_sha1 = NULL;
171 }
172
173 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
174                                    const int **nids, int nid);
175
176 static int dasync_cipher_nids[] = {
177     NID_aes_128_cbc,
178     NID_aes_128_cbc_hmac_sha1,
179     0
180 };
181
182 static int bind_dasync(ENGINE *e)
183 {
184     /* Setup RSA_METHOD */
185     if ((dasync_rsa_method = RSA_meth_new("Dummy Async RSA method", 0)) == NULL
186         || RSA_meth_set_pub_enc(dasync_rsa_method, dasync_pub_enc) == 0
187         || RSA_meth_set_pub_dec(dasync_rsa_method, dasync_pub_dec) == 0
188         || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_enc) == 0
189         || RSA_meth_set_priv_dec(dasync_rsa_method, dasync_rsa_priv_dec) == 0
190         || RSA_meth_set_mod_exp(dasync_rsa_method, dasync_rsa_mod_exp) == 0
191         || RSA_meth_set_bn_mod_exp(dasync_rsa_method, BN_mod_exp_mont) == 0
192         || RSA_meth_set_init(dasync_rsa_method, dasync_rsa_init) == 0
193         || RSA_meth_set_finish(dasync_rsa_method, dasync_rsa_finish) == 0) {
194         DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
195         return 0;
196     }
197
198     /* Ensure the dasync error handling is set up */
199     ERR_load_DASYNC_strings();
200
201     if (!ENGINE_set_id(e, engine_dasync_id)
202         || !ENGINE_set_name(e, engine_dasync_name)
203         || !ENGINE_set_RSA(e, dasync_rsa_method)
204         || !ENGINE_set_digests(e, dasync_digests)
205         || !ENGINE_set_ciphers(e, dasync_ciphers)
206         || !ENGINE_set_destroy_function(e, dasync_destroy)
207         || !ENGINE_set_init_function(e, dasync_init)
208         || !ENGINE_set_finish_function(e, dasync_finish)) {
209         DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
210         return 0;
211     }
212
213     /*
214      * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
215      * supplied by this engine
216      */
217     _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
218     if (_hidden_sha1_md == NULL
219         || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
220         || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
221         || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
222                                          sizeof(EVP_MD *) + sizeof(SHA_CTX))
223         || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
224         || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
225         || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
226         || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
227         EVP_MD_meth_free(_hidden_sha1_md);
228         _hidden_sha1_md = NULL;
229     }
230
231     _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
232                                               16 /* block size */,
233                                               16 /* key len */);
234     if (_hidden_aes_128_cbc == NULL
235             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
236             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
237                                           EVP_CIPH_FLAG_DEFAULT_ASN1
238                                           | EVP_CIPH_CBC_MODE
239                                           | EVP_CIPH_FLAG_PIPELINE)
240             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
241                                          dasync_aes128_init_key)
242             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
243                                               dasync_aes128_cbc_cipher)
244             || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
245                                             dasync_aes128_cbc_cleanup)
246             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
247                                          dasync_aes128_cbc_ctrl)
248             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
249                                 sizeof(struct dasync_pipeline_ctx))) {
250         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
251         _hidden_aes_128_cbc = NULL;
252     }
253
254     _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
255                                                 NID_aes_128_cbc_hmac_sha1,
256                                                 16 /* block size */,
257                                                 16 /* key len */);
258     if (_hidden_aes_128_cbc_hmac_sha1 == NULL
259             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
260             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
261                                             EVP_CIPH_CBC_MODE
262                                           | EVP_CIPH_FLAG_DEFAULT_ASN1
263                                           | EVP_CIPH_FLAG_AEAD_CIPHER
264                                           | EVP_CIPH_FLAG_PIPELINE)
265             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
266                                          dasync_aes128_cbc_hmac_sha1_init_key)
267             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
268                                             dasync_aes128_cbc_hmac_sha1_cipher)
269             || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
270                                             dasync_aes128_cbc_hmac_sha1_cleanup)
271             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
272                                          dasync_aes128_cbc_hmac_sha1_ctrl)
273             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
274                                 sizeof(struct dasync_pipeline_ctx))) {
275         EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
276         _hidden_aes_128_cbc_hmac_sha1 = NULL;
277     }
278
279     return 1;
280 }
281
282 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
283 static int bind_helper(ENGINE *e, const char *id)
284 {
285     if (id && (strcmp(id, engine_dasync_id) != 0))
286         return 0;
287     if (!bind_dasync(e))
288         return 0;
289     return 1;
290 }
291
292 IMPLEMENT_DYNAMIC_CHECK_FN()
293     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
294 # endif
295
296 static ENGINE *engine_dasync(void)
297 {
298     ENGINE *ret = ENGINE_new();
299     if (!ret)
300         return NULL;
301     if (!bind_dasync(ret)) {
302         ENGINE_free(ret);
303         return NULL;
304     }
305     return ret;
306 }
307
308 void engine_load_dasync_int(void)
309 {
310     ENGINE *toadd = engine_dasync();
311     if (!toadd)
312         return;
313     ENGINE_add(toadd);
314     ENGINE_free(toadd);
315     ERR_clear_error();
316 }
317
318 static int dasync_init(ENGINE *e)
319 {
320     return 1;
321 }
322
323
324 static int dasync_finish(ENGINE *e)
325 {
326     return 1;
327 }
328
329
330 static int dasync_destroy(ENGINE *e)
331 {
332     destroy_digests();
333     destroy_ciphers();
334     RSA_meth_free(dasync_rsa_method);
335     ERR_unload_DASYNC_strings();
336     return 1;
337 }
338
339 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
340                           const int **nids, int nid)
341 {
342     int ok = 1;
343     if (!digest) {
344         /* We are returning a list of supported nids */
345         return dasync_digest_nids(nids);
346     }
347     /* We are being asked for a specific digest */
348     switch (nid) {
349     case NID_sha1:
350         *digest = dasync_sha1();
351         break;
352     default:
353         ok = 0;
354         *digest = NULL;
355         break;
356     }
357     return ok;
358 }
359
360 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
361                                    const int **nids, int nid)
362 {
363     int ok = 1;
364     if (cipher == NULL) {
365         /* We are returning a list of supported nids */
366         *nids = dasync_cipher_nids;
367         return (sizeof(dasync_cipher_nids) -
368                 1) / sizeof(dasync_cipher_nids[0]);
369     }
370     /* We are being asked for a specific cipher */
371     switch (nid) {
372     case NID_aes_128_cbc:
373         *cipher = dasync_aes_128_cbc();
374         break;
375     case NID_aes_128_cbc_hmac_sha1:
376         *cipher = dasync_aes_128_cbc_hmac_sha1();
377         break;
378     default:
379         ok = 0;
380         *cipher = NULL;
381         break;
382     }
383     return ok;
384 }
385
386 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
387                          OSSL_ASYNC_FD readfd, void *pvwritefd)
388 {
389     OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
390 #if defined(ASYNC_WIN)
391     CloseHandle(readfd);
392     CloseHandle(*pwritefd);
393 #elif defined(ASYNC_POSIX)
394     close(readfd);
395     close(*pwritefd);
396 #endif
397     OPENSSL_free(pwritefd);
398 }
399
400 #define DUMMY_CHAR 'X'
401
402 static void dummy_pause_job(void) {
403     ASYNC_JOB *job;
404     ASYNC_WAIT_CTX *waitctx;
405     OSSL_ASYNC_FD pipefds[2] = {0, 0};
406     OSSL_ASYNC_FD *writefd;
407 #if defined(ASYNC_WIN)
408     DWORD numwritten, numread;
409     char buf = DUMMY_CHAR;
410 #elif defined(ASYNC_POSIX)
411     char buf = DUMMY_CHAR;
412 #endif
413
414     if ((job = ASYNC_get_current_job()) == NULL)
415         return;
416
417     waitctx = ASYNC_get_wait_ctx(job);
418
419     if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
420                               (void **)&writefd)) {
421         pipefds[1] = *writefd;
422     } else {
423         writefd = OPENSSL_malloc(sizeof(*writefd));
424         if (writefd == NULL)
425             return;
426 #if defined(ASYNC_WIN)
427         if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
428             OPENSSL_free(writefd);
429             return;
430         }
431 #elif defined(ASYNC_POSIX)
432         if (pipe(pipefds) != 0) {
433             OPENSSL_free(writefd);
434             return;
435         }
436 #endif
437         *writefd = pipefds[1];
438
439         if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
440                                         writefd, wait_cleanup)) {
441             wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
442             return;
443         }
444     }
445     /*
446      * In the Dummy async engine we are cheating. We signal that the job
447      * is complete by waking it before the call to ASYNC_pause_job(). A real
448      * async engine would only wake when the job was actually complete
449      */
450 #if defined(ASYNC_WIN)
451     WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
452 #elif defined(ASYNC_POSIX)
453     if (write(pipefds[1], &buf, 1) < 0)
454         return;
455 #endif
456
457     /* Ignore errors - we carry on anyway */
458     ASYNC_pause_job();
459
460     /* Clear the wake signal */
461 #if defined(ASYNC_WIN)
462     ReadFile(pipefds[0], &buf, 1, &numread, NULL);
463 #elif defined(ASYNC_POSIX)
464     if (read(pipefds[0], &buf, 1) < 0)
465         return;
466 #endif
467 }
468
469 /*
470  * SHA1 implementation. At the moment we just defer to the standard
471  * implementation
472  */
473 #undef data
474 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
475 static int dasync_sha1_init(EVP_MD_CTX *ctx)
476 {
477     dummy_pause_job();
478
479     return SHA1_Init(data(ctx));
480 }
481
482 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
483                              size_t count)
484 {
485     dummy_pause_job();
486
487     return SHA1_Update(data(ctx), data, (size_t)count);
488 }
489
490 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
491 {
492     dummy_pause_job();
493
494     return SHA1_Final(md, data(ctx));
495 }
496
497 /*
498  * RSA implementation
499  */
500
501 static int dasync_pub_enc(int flen, const unsigned char *from,
502                     unsigned char *to, RSA *rsa, int padding) {
503     /* Ignore errors - we carry on anyway */
504     dummy_pause_job();
505     return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
506         (flen, from, to, rsa, padding);
507 }
508
509 static int dasync_pub_dec(int flen, const unsigned char *from,
510                     unsigned char *to, RSA *rsa, int padding) {
511     /* Ignore errors - we carry on anyway */
512     dummy_pause_job();
513     return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
514         (flen, from, to, rsa, padding);
515 }
516
517 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
518                       unsigned char *to, RSA *rsa, int padding)
519 {
520     /* Ignore errors - we carry on anyway */
521     dummy_pause_job();
522     return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
523         (flen, from, to, rsa, padding);
524 }
525
526 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
527                       unsigned char *to, RSA *rsa, int padding)
528 {
529     /* Ignore errors - we carry on anyway */
530     dummy_pause_job();
531     return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
532         (flen, from, to, rsa, padding);
533 }
534
535 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
536 {
537     /* Ignore errors - we carry on anyway */
538     dummy_pause_job();
539     return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
540 }
541
542 static int dasync_rsa_init(RSA *rsa)
543 {
544     return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
545 }
546 static int dasync_rsa_finish(RSA *rsa)
547 {
548     return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
549 }
550
551 /* Cipher helper functions */
552
553 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
554                                      void *ptr, int aeadcapable)
555 {
556     int ret;
557     struct dasync_pipeline_ctx *pipe_ctx =
558         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
559
560     if (pipe_ctx == NULL)
561         return 0;
562
563     switch (type) {
564         case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
565             pipe_ctx->numpipes = arg;
566             pipe_ctx->outbufs = (unsigned char **)ptr;
567             break;
568
569         case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
570             pipe_ctx->numpipes = arg;
571             pipe_ctx->inbufs = (unsigned char **)ptr;
572             break;
573
574         case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
575             pipe_ctx->numpipes = arg;
576             pipe_ctx->lens = (size_t *)ptr;
577             break;
578
579         case EVP_CTRL_AEAD_SET_MAC_KEY:
580             if (!aeadcapable)
581                 return -1;
582             EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
583             ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
584                                           (ctx, type, arg, ptr);
585             EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
586             return ret;
587
588         case EVP_CTRL_AEAD_TLS1_AAD:
589         {
590             unsigned char *p = ptr;
591             unsigned int len;
592
593             if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
594                 return -1;
595
596             if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
597                 return -1;
598
599             memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
600                    EVP_AEAD_TLS1_AAD_LEN);
601             pipe_ctx->aadctr++;
602
603             len = p[arg - 2] << 8 | p[arg - 1];
604
605             if (EVP_CIPHER_CTX_encrypting(ctx)) {
606                 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
607                     if (len < AES_BLOCK_SIZE)
608                         return 0;
609                     len -= AES_BLOCK_SIZE;
610                 }
611
612                 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
613                         & -AES_BLOCK_SIZE) - len;
614             } else {
615                 return SHA_DIGEST_LENGTH;
616             }
617         }
618
619         default:
620             return 0;
621     }
622
623     return 1;
624 }
625
626 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
627                                          const unsigned char *key,
628                                          const unsigned char *iv, int enc,
629                                          const EVP_CIPHER *cipher)
630 {
631     int ret;
632     struct dasync_pipeline_ctx *pipe_ctx =
633         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
634
635     if (pipe_ctx->inner_cipher_data == NULL
636             && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
637         pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
638             EVP_CIPHER_impl_ctx_size(cipher));
639         if (pipe_ctx->inner_cipher_data == NULL) {
640             DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
641                         ERR_R_MALLOC_FAILURE);
642             return 0;
643         }
644     }
645
646     pipe_ctx->numpipes = 0;
647     pipe_ctx->aadctr = 0;
648
649     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
650     ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
651     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
652
653     return ret;
654 }
655
656 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
657                                 const unsigned char *in, size_t inl,
658                                 const EVP_CIPHER *cipher)
659 {
660     int ret = 1;
661     unsigned int i, pipes;
662     struct dasync_pipeline_ctx *pipe_ctx =
663         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
664
665     pipes = pipe_ctx->numpipes;
666     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
667     if (pipes == 0) {
668         if (pipe_ctx->aadctr != 0) {
669             if (pipe_ctx->aadctr != 1)
670                 return -1;
671             EVP_CIPHER_meth_get_ctrl(cipher)
672                                     (ctx, EVP_CTRL_AEAD_TLS1_AAD,
673                                      EVP_AEAD_TLS1_AAD_LEN,
674                                      pipe_ctx->tlsaad[0]);
675         }
676         ret = EVP_CIPHER_meth_get_do_cipher(cipher)
677                                            (ctx, out, in, inl);
678     } else {
679         if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
680             return -1;
681         for (i = 0; i < pipes; i++) {
682             if (pipe_ctx->aadctr > 0) {
683                 EVP_CIPHER_meth_get_ctrl(cipher)
684                                         (ctx, EVP_CTRL_AEAD_TLS1_AAD,
685                                          EVP_AEAD_TLS1_AAD_LEN,
686                                          pipe_ctx->tlsaad[i]);
687             }
688             ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
689                                 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
690                                  pipe_ctx->lens[i]);
691         }
692         pipe_ctx->numpipes = 0;
693     }
694     pipe_ctx->aadctr = 0;
695     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
696     return ret;
697 }
698
699 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
700                                         const EVP_CIPHER *cipher)
701 {
702     struct dasync_pipeline_ctx *pipe_ctx =
703         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
704
705     OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
706                        EVP_CIPHER_impl_ctx_size(cipher));
707
708     return 1;
709 }
710
711 /*
712  * AES128 CBC Implementation
713  */
714
715 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
716                                   void *ptr)
717 {
718     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
719 }
720
721 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
722                              const unsigned char *iv, int enc)
723 {
724     return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
725 }
726
727 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
728                                const unsigned char *in, size_t inl)
729 {
730     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
731 }
732
733 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
734 {
735     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
736 }
737
738
739 /*
740  * AES128 CBC HMAC SHA1 Implementation
741  */
742
743 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
744                                              int arg, void *ptr)
745 {
746     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
747 }
748
749 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
750                                                 const unsigned char *key,
751                                                 const unsigned char *iv,
752                                                 int enc)
753 {
754     return dasync_cipher_init_key_helper(ctx, key, iv, enc,
755                                          EVP_aes_128_cbc_hmac_sha1());
756 }
757
758 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
759                                                unsigned char *out,
760                                                const unsigned char *in,
761                                                size_t inl)
762 {
763     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
764 }
765
766 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
767 {
768     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
769 }