Fix dasync engine
[openssl.git] / engines / e_dasync.c
1 /*
2  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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     ASYNC_callback_fn callback;
406     void * callback_arg;
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;
414 #endif
415
416     if ((job = ASYNC_get_current_job()) == NULL)
417         return;
418
419     waitctx = ASYNC_get_wait_ctx(job);
420
421     if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
422         /*
423          * In the Dummy async engine we are cheating. We call the callback that the job
424          * is complete before the call to ASYNC_pause_job(). A real
425          * async engine would only call the callback when the job was actually complete
426          */
427         (*callback)(callback_arg);
428         ASYNC_pause_job();
429         return;
430     }
431
432
433     if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
434                               (void **)&writefd)) {
435         pipefds[1] = *writefd;
436     } else {
437         writefd = OPENSSL_malloc(sizeof(*writefd));
438         if (writefd == NULL)
439             return;
440 #if defined(ASYNC_WIN)
441         if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
442             OPENSSL_free(writefd);
443             return;
444         }
445 #elif defined(ASYNC_POSIX)
446         if (pipe(pipefds) != 0) {
447             OPENSSL_free(writefd);
448             return;
449         }
450 #endif
451         *writefd = pipefds[1];
452
453         if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
454                                         writefd, wait_cleanup)) {
455             wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
456             return;
457         }
458     }
459     /*
460      * In the Dummy async engine we are cheating. We signal that the job
461      * is complete by waking it before the call to ASYNC_pause_job(). A real
462      * async engine would only wake when the job was actually complete
463      */
464 #if defined(ASYNC_WIN)
465     WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
466 #elif defined(ASYNC_POSIX)
467     if (write(pipefds[1], &buf, 1) < 0)
468         return;
469 #endif
470
471     /* Ignore errors - we carry on anyway */
472     ASYNC_pause_job();
473
474     /* Clear the wake signal */
475 #if defined(ASYNC_WIN)
476     ReadFile(pipefds[0], &buf, 1, &numread, NULL);
477 #elif defined(ASYNC_POSIX)
478     if (read(pipefds[0], &buf, 1) < 0)
479         return;
480 #endif
481 }
482
483 /*
484  * SHA1 implementation. At the moment we just defer to the standard
485  * implementation
486  */
487 #undef data
488 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
489 static int dasync_sha1_init(EVP_MD_CTX *ctx)
490 {
491     dummy_pause_job();
492
493     return SHA1_Init(data(ctx));
494 }
495
496 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
497                              size_t count)
498 {
499     dummy_pause_job();
500
501     return SHA1_Update(data(ctx), data, (size_t)count);
502 }
503
504 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
505 {
506     dummy_pause_job();
507
508     return SHA1_Final(md, data(ctx));
509 }
510
511 /*
512  * RSA implementation
513  */
514
515 static int dasync_pub_enc(int flen, const unsigned char *from,
516                     unsigned char *to, RSA *rsa, int padding) {
517     /* Ignore errors - we carry on anyway */
518     dummy_pause_job();
519     return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
520         (flen, from, to, rsa, padding);
521 }
522
523 static int dasync_pub_dec(int flen, const unsigned char *from,
524                     unsigned char *to, RSA *rsa, int padding) {
525     /* Ignore errors - we carry on anyway */
526     dummy_pause_job();
527     return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
528         (flen, from, to, rsa, padding);
529 }
530
531 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
532                       unsigned char *to, RSA *rsa, int padding)
533 {
534     /* Ignore errors - we carry on anyway */
535     dummy_pause_job();
536     return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
537         (flen, from, to, rsa, padding);
538 }
539
540 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
541                       unsigned char *to, RSA *rsa, int padding)
542 {
543     /* Ignore errors - we carry on anyway */
544     dummy_pause_job();
545     return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
546         (flen, from, to, rsa, padding);
547 }
548
549 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
550 {
551     /* Ignore errors - we carry on anyway */
552     dummy_pause_job();
553     return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
554 }
555
556 static int dasync_rsa_init(RSA *rsa)
557 {
558     return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
559 }
560 static int dasync_rsa_finish(RSA *rsa)
561 {
562     return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
563 }
564
565 /* Cipher helper functions */
566
567 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
568                                      void *ptr, int aeadcapable)
569 {
570     int ret;
571     struct dasync_pipeline_ctx *pipe_ctx =
572         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
573
574     if (pipe_ctx == NULL)
575         return 0;
576
577     switch (type) {
578         case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
579             pipe_ctx->numpipes = arg;
580             pipe_ctx->outbufs = (unsigned char **)ptr;
581             break;
582
583         case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
584             pipe_ctx->numpipes = arg;
585             pipe_ctx->inbufs = (unsigned char **)ptr;
586             break;
587
588         case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
589             pipe_ctx->numpipes = arg;
590             pipe_ctx->lens = (size_t *)ptr;
591             break;
592
593         case EVP_CTRL_AEAD_SET_MAC_KEY:
594             if (!aeadcapable)
595                 return -1;
596             EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
597             ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
598                                           (ctx, type, arg, ptr);
599             EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
600             return ret;
601
602         case EVP_CTRL_AEAD_TLS1_AAD:
603         {
604             unsigned char *p = ptr;
605             unsigned int len;
606
607             if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
608                 return -1;
609
610             if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
611                 return -1;
612
613             memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
614                    EVP_AEAD_TLS1_AAD_LEN);
615             pipe_ctx->aadctr++;
616
617             len = p[arg - 2] << 8 | p[arg - 1];
618
619             if (EVP_CIPHER_CTX_encrypting(ctx)) {
620                 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
621                     if (len < AES_BLOCK_SIZE)
622                         return 0;
623                     len -= AES_BLOCK_SIZE;
624                 }
625
626                 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
627                         & -AES_BLOCK_SIZE) - len;
628             } else {
629                 return SHA_DIGEST_LENGTH;
630             }
631         }
632
633         default:
634             return 0;
635     }
636
637     return 1;
638 }
639
640 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
641                                          const unsigned char *key,
642                                          const unsigned char *iv, int enc,
643                                          const EVP_CIPHER *cipher)
644 {
645     int ret;
646     struct dasync_pipeline_ctx *pipe_ctx =
647         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
648
649     if (pipe_ctx->inner_cipher_data == NULL
650             && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
651         pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
652             EVP_CIPHER_impl_ctx_size(cipher));
653         if (pipe_ctx->inner_cipher_data == NULL) {
654             DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
655                         ERR_R_MALLOC_FAILURE);
656             return 0;
657         }
658     }
659
660     pipe_ctx->numpipes = 0;
661     pipe_ctx->aadctr = 0;
662
663     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
664     ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
665     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
666
667     return ret;
668 }
669
670 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
671                                 const unsigned char *in, size_t inl,
672                                 const EVP_CIPHER *cipher)
673 {
674     int ret = 1;
675     unsigned int i, pipes;
676     struct dasync_pipeline_ctx *pipe_ctx =
677         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
678
679     pipes = pipe_ctx->numpipes;
680     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
681     if (pipes == 0) {
682         if (pipe_ctx->aadctr != 0) {
683             if (pipe_ctx->aadctr != 1)
684                 return -1;
685             EVP_CIPHER_meth_get_ctrl(cipher)
686                                     (ctx, EVP_CTRL_AEAD_TLS1_AAD,
687                                      EVP_AEAD_TLS1_AAD_LEN,
688                                      pipe_ctx->tlsaad[0]);
689         }
690         ret = EVP_CIPHER_meth_get_do_cipher(cipher)
691                                            (ctx, out, in, inl);
692     } else {
693         if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
694             return -1;
695         for (i = 0; i < pipes; i++) {
696             if (pipe_ctx->aadctr > 0) {
697                 EVP_CIPHER_meth_get_ctrl(cipher)
698                                         (ctx, EVP_CTRL_AEAD_TLS1_AAD,
699                                          EVP_AEAD_TLS1_AAD_LEN,
700                                          pipe_ctx->tlsaad[i]);
701             }
702             ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
703                                 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
704                                  pipe_ctx->lens[i]);
705         }
706         pipe_ctx->numpipes = 0;
707     }
708     pipe_ctx->aadctr = 0;
709     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
710     return ret;
711 }
712
713 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
714                                         const EVP_CIPHER *cipher)
715 {
716     struct dasync_pipeline_ctx *pipe_ctx =
717         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
718
719     OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
720                        EVP_CIPHER_impl_ctx_size(cipher));
721
722     return 1;
723 }
724
725 /*
726  * AES128 CBC Implementation
727  */
728
729 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
730                                   void *ptr)
731 {
732     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
733 }
734
735 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
736                              const unsigned char *iv, int enc)
737 {
738     return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
739 }
740
741 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
742                                const unsigned char *in, size_t inl)
743 {
744     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
745 }
746
747 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
748 {
749     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
750 }
751
752
753 /*
754  * AES128 CBC HMAC SHA1 Implementation
755  */
756
757 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
758                                              int arg, void *ptr)
759 {
760     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
761 }
762
763 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
764                                                 const unsigned char *key,
765                                                 const unsigned char *iv,
766                                                 int enc)
767 {
768     return dasync_cipher_init_key_helper(ctx, key, iv, enc,
769                                          EVP_aes_128_cbc_hmac_sha1());
770 }
771
772 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
773                                                unsigned char *out,
774                                                const unsigned char *in,
775                                                size_t inl)
776 {
777     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
778 }
779
780 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
781 {
782     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
783 }