499262ff918afac328e0a201381827d1f75bfe93
[openssl.git] / engines / e_dasync.c
1 /*
2  * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2015 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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/)"
23  *
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.
28  *
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.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
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  * ====================================================================
51  */
52
53 #include <stdio.h>
54 #include <string.h>
55
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>
66
67 #if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
68 # undef ASYNC_POSIX
69 # define ASYNC_POSIX
70 # include <unistd.h>
71 #elif defined(_WIN32)
72 # undef ASYNC_WIN
73 # define ASYNC_WIN
74 # include <windows.h>
75 #endif
76
77 #define DASYNC_LIB_NAME "DASYNC"
78 #include "e_dasync_err.c"
79
80 /* Engine Id and Name */
81 static const char *engine_dasync_id = "dasync";
82 static const char *engine_dasync_name = "Dummy Async engine support";
83
84
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);
90
91
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);
95
96 static void dummy_pause_job(void);
97
98 /* SHA1 */
99 static int dasync_sha1_init(EVP_MD_CTX *ctx);
100 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
101                              size_t count);
102 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
103
104 static EVP_MD *_hidden_sha1_md = NULL;
105 static const EVP_MD *dasync_sha1(void)
106 {
107     if (_hidden_sha1_md == NULL) {
108         EVP_MD *md;
109
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);
120             md = NULL;
121         }
122         _hidden_sha1_md = md;
123     }
124     return _hidden_sha1_md;
125 }
126 static void destroy_digests(void)
127 {
128     EVP_MD_meth_free(_hidden_sha1_md);
129     _hidden_sha1_md = NULL;
130 }
131 static int dasync_digest_nids(const int **nids)
132 {
133     static int digest_nids[2] = { 0, 0 };
134     static int pos = 0;
135     static int init = 0;
136
137     if (!init) {
138         const EVP_MD *md;
139         if ((md = dasync_sha1()) != NULL)
140             digest_nids[pos++] = EVP_MD_type(md);
141         digest_nids[pos] = 0;
142         init = 1;
143     }
144     *nids = digest_nids;
145     return pos;
146 }
147
148 /* RSA */
149
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,
159                               BN_CTX *ctx);
160
161 static int dasync_rsa_init(RSA *rsa);
162 static int dasync_rsa_finish(RSA *rsa);
163
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 */
174     0,                          /* flags */
175     NULL,                       /* app_data */
176     0,                          /* rsa_sign */
177     0,                          /* rsa_verify */
178     NULL                        /* rsa_keygen */
179 };
180
181
182 /* AES */
183
184 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
185                                   void *ptr);
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);
191
192 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
193                                              int arg, void *ptr);
194 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
195                                                  const unsigned char *key,
196                                                  const unsigned char *iv,
197                                                  int enc);
198 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
199                                                unsigned char *out,
200                                                const unsigned char *in,
201                                                size_t inl);
202 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
203
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;
210     size_t *lens;
211     int enc;
212     unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
213     unsigned int aadctr;
214 };
215
216 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
217 static const EVP_CIPHER *dasync_aes_128_cbc(void)
218 {
219     if (_hidden_aes_128_cbc == NULL)
220         _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
221                                                   16 /* block size */,
222                                                   16 /* key len */);
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
227                                           | EVP_CIPH_CBC_MODE
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;
241     }
242     return _hidden_aes_128_cbc;
243 }
244
245 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
246 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
247 {
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,
251                                                   16 /* block size */,
252                                                   16 /* key len */);
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,
256                                             EVP_CIPH_CBC_MODE
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;
272     }
273     return _hidden_aes_128_cbc_hmac_sha1;
274 }
275
276 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
277                                    const int **nids, int nid);
278
279 static int dasync_cipher_nids[] = {
280     NID_aes_128_cbc,
281     NID_aes_128_cbc_hmac_sha1,
282     0
283 };
284
285 static int bind_dasync(ENGINE *e)
286 {
287     /* Ensure the dasync error handling is set up */
288     ERR_load_DASYNC_strings();
289
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);
299         return 0;
300     }
301
302     return 1;
303 }
304
305 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
306 static int bind_helper(ENGINE *e, const char *id)
307 {
308     if (id && (strcmp(id, engine_dasync_id) != 0))
309         return 0;
310     if (!bind_dasync(e))
311         return 0;
312     return 1;
313 }
314
315 IMPLEMENT_DYNAMIC_CHECK_FN()
316     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
317 # endif
318
319 static ENGINE *engine_dasync(void)
320 {
321     ENGINE *ret = ENGINE_new();
322     if (!ret)
323         return NULL;
324     if (!bind_dasync(ret)) {
325         ENGINE_free(ret);
326         return NULL;
327     }
328     return ret;
329 }
330
331 void engine_load_dasync_internal(void)
332 {
333     ENGINE *toadd = engine_dasync();
334     if (!toadd)
335         return;
336     ENGINE_add(toadd);
337     ENGINE_free(toadd);
338     ERR_clear_error();
339 }
340
341 static int dasync_init(ENGINE *e)
342 {
343     return 1;
344 }
345
346
347 static int dasync_finish(ENGINE *e)
348 {
349     return 1;
350 }
351
352
353 static int dasync_destroy(ENGINE *e)
354 {
355     destroy_digests();
356     ERR_unload_DASYNC_strings();
357     return 1;
358 }
359
360 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
361                           const int **nids, int nid)
362 {
363     int ok = 1;
364     if (!digest) {
365         /* We are returning a list of supported nids */
366         return dasync_digest_nids(nids);
367     }
368     /* We are being asked for a specific digest */
369     switch (nid) {
370     case NID_sha1:
371         *digest = dasync_sha1();
372         break;
373     default:
374         ok = 0;
375         *digest = NULL;
376         break;
377     }
378     return ok;
379 }
380
381 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
382                                    const int **nids, int nid)
383 {
384     int ok = 1;
385     if (!cipher) {
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]);
390     }
391     /* We are being asked for a specific cipher */
392     switch (nid) {
393     case NID_aes_128_cbc:
394         *cipher = dasync_aes_128_cbc();
395         break;
396     case NID_aes_128_cbc_hmac_sha1:
397         *cipher = dasync_aes_128_cbc_hmac_sha1();
398         break;
399     default:
400         ok = 0;
401         *cipher = NULL;
402         break;
403     }
404     return ok;
405 }
406
407 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
408                          OSSL_ASYNC_FD readfd, void *pvwritefd)
409 {
410     OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
411 #if defined(ASYNC_WIN)
412     CloseHandle(readfd);
413     CloseHandle(*pwritefd);
414 #elif defined(ASYNC_POSIX)
415     close(readfd);
416     close(*pwritefd);
417 #endif
418     OPENSSL_free(pwritefd);
419 }
420
421 #define DUMMY_CHAR 'X'
422
423 static void dummy_pause_job(void) {
424     ASYNC_JOB *job;
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;
433 #endif
434
435     if ((job = ASYNC_get_current_job()) == NULL)
436         return;
437
438     waitctx = ASYNC_get_wait_ctx(job);
439
440     if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
441                               (void **)&writefd)) {
442         pipefds[1] = *writefd;
443     } else {
444         writefd = OPENSSL_malloc(sizeof(*writefd));
445         if (writefd == NULL)
446             return;
447 #if defined(ASYNC_WIN)
448         if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
449             OPENSSL_free(writefd);
450             return;
451         }
452 #elif defined(ASYNC_POSIX)
453         if (pipe(pipefds) != 0) {
454             OPENSSL_free(writefd);
455             return;
456         }
457 #endif
458         *writefd = pipefds[1];
459
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);
463             return;
464         }
465     }
466     /*
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
470      */
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)
475         return;
476 #endif
477
478     /* Ignore errors - we carry on anyway */
479     ASYNC_pause_job();
480
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)
486         return;
487 #endif
488 }
489
490 /*
491  * SHA1 implementation. At the moment we just defer to the standard
492  * implementation
493  */
494 #undef data
495 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
496 static int dasync_sha1_init(EVP_MD_CTX *ctx)
497 {
498     dummy_pause_job();
499
500     return SHA1_Init(data(ctx));
501 }
502
503 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
504                              size_t count)
505 {
506     dummy_pause_job();
507
508     return SHA1_Update(data(ctx), data, (size_t)count);
509 }
510
511 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
512 {
513     dummy_pause_job();
514
515     return SHA1_Final(md, data(ctx));
516 }
517
518 /*
519  * RSA implementation
520  */
521
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 */
525     dummy_pause_job();
526     return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
527 }
528
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 */
532     dummy_pause_job();
533     return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
534 }
535
536 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
537                       unsigned char *to, RSA *rsa, int padding)
538 {
539     /* Ignore errors - we carry on anyway */
540     dummy_pause_job();
541     return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
542 }
543
544 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
545                       unsigned char *to, RSA *rsa, int padding)
546 {
547     /* Ignore errors - we carry on anyway */
548     dummy_pause_job();
549     return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
550 }
551
552 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
553 {
554     /* Ignore errors - we carry on anyway */
555     dummy_pause_job();
556     return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
557 }
558
559 static int dasync_rsa_init(RSA *rsa)
560 {
561     return RSA_PKCS1_OpenSSL()->init(rsa);
562 }
563 static int dasync_rsa_finish(RSA *rsa)
564 {
565     return RSA_PKCS1_OpenSSL()->finish(rsa);
566 }
567
568 /*
569  * AES128 Implementation
570  */
571
572 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
573                                   void *ptr)
574 {
575     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
576         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
577
578     if (pipe_ctx == NULL)
579         return 0;
580
581     switch (type) {
582         case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
583             pipe_ctx->numpipes = arg;
584             pipe_ctx->outbufs = (unsigned char **)ptr;
585             break;
586
587         case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
588             pipe_ctx->numpipes = arg;
589             pipe_ctx->inbufs = (unsigned char **)ptr;
590             break;
591
592         case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
593             pipe_ctx->numpipes = arg;
594             pipe_ctx->lens = (size_t *)ptr;
595             break;
596
597         default:
598             return 0;
599     }
600
601     return 1;
602 }
603
604 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
605                              const unsigned char *iv, int enc)
606 {
607     int ret;
608     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
609         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
610
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);
618             return 0;
619         }
620     }
621
622     pipe_ctx->numpipes = 0;
623     pipe_ctx->aadctr = 0;
624
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);
628
629     return ret;
630 }
631
632 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
633                                const unsigned char *in, size_t inl)
634 {
635     int ret = 1;
636     unsigned int i, pipes;
637     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
638         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
639
640     pipes = pipe_ctx->numpipes;
641     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
642     if (pipes == 0) {
643         ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())
644                                            (ctx, out, in, inl);
645     } else {
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],
649                                     pipe_ctx->inbufs[i],
650                                     pipe_ctx->lens[i]);
651         }
652         pipe_ctx->numpipes = 0;
653     }
654     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
655     return ret;
656 }
657
658 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
659 {
660     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
661         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
662
663     OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
664                        EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()));
665
666     return 1;
667 }
668
669
670 /*
671  * AES128 CBC HMAC SHA1 Implementation
672  */
673
674 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
675                                              int arg, void *ptr)
676 {
677     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
678         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
679     int ret;
680
681     if (pipe_ctx == NULL)
682         return 0;
683
684     switch (type) {
685         case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
686             pipe_ctx->numpipes = arg;
687             pipe_ctx->outbufs = (unsigned char **)ptr;
688             break;
689
690         case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
691             pipe_ctx->numpipes = arg;
692             pipe_ctx->inbufs = (unsigned char **)ptr;
693             break;
694
695         case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
696             pipe_ctx->numpipes = arg;
697             pipe_ctx->lens = (size_t *)ptr;
698             break;
699
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);
705             return ret;
706
707         case EVP_CTRL_AEAD_TLS1_AAD:
708         {
709             unsigned char *p = ptr;
710             unsigned int len;
711
712             if (arg != EVP_AEAD_TLS1_AAD_LEN)
713                 return -1;
714
715             if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
716                 return -1;
717
718             memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
719                    EVP_AEAD_TLS1_AAD_LEN);
720             pipe_ctx->aadctr++;
721
722             len = p[arg - 2] << 8 | p[arg - 1];
723
724             if (pipe_ctx->enc) {
725                 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
726                     len -= AES_BLOCK_SIZE;
727                 }
728
729                 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
730                         & -AES_BLOCK_SIZE) - len;
731             } else {
732                 return SHA_DIGEST_LENGTH;
733             }
734         }
735
736
737         default:
738             return 0;
739     }
740
741     return 1;
742 }
743
744 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
745                                                  const unsigned char *key,
746                                                  const unsigned char *iv,
747                                                  int enc)
748 {
749     int ret;
750     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
751         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
752
753     if (pipe_ctx->inner_cipher_data == NULL
754             && EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc_hmac_sha1())
755                != 0) {
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);
762             return 0;
763         }
764     }
765
766     pipe_ctx->numpipes = 0;
767     pipe_ctx->enc = enc;
768
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())
771                                   (ctx, key, iv, enc);
772     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
773
774     return ret;
775 }
776
777 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
778                                                unsigned char *out,
779                                                const unsigned char *in,
780                                                size_t inl)
781 {
782     int ret = 1;
783     unsigned int i, pipes;
784     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
785         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
786
787     pipes = pipe_ctx->numpipes;
788     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
789     if (pipes == 0) {
790         if (pipe_ctx->aadctr != 0) {
791             if (pipe_ctx->aadctr != 1)
792                 return -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]);
797         }
798         ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc_hmac_sha1())
799                                            (ctx, out, in, inl);
800     } else {
801         if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
802             return -1;
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]);
809             }
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],
813                                  pipe_ctx->lens[i]);
814         }
815         pipe_ctx->numpipes = 0;
816     }
817     pipe_ctx->aadctr = 0;
818     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
819     return ret;
820 }
821
822 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
823 {
824     struct aes_128_cbc_pipeline_ctx *pipe_ctx =
825         (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
826
827     OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
828                EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc_hmac_sha1()));
829
830     return 1;
831 }