2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
14 * SHA-1 low level APIs are deprecated for public use, but still ok for
15 * internal use. Note, that due to symbols not being exported, only the
16 * #defines and strucures can be accessed, in this case SHA_CBLOCK and
19 #include "internal/deprecated.h"
21 #include <openssl/opensslconf.h>
29 #include <openssl/engine.h>
30 #include <openssl/sha.h>
31 #include <openssl/aes.h>
32 #include <openssl/rsa.h>
33 #include <openssl/evp.h>
34 #include <openssl/async.h>
35 #include <openssl/bn.h>
36 #include <openssl/crypto.h>
37 #include <openssl/ssl.h>
38 #include <openssl/modes.h>
40 #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
49 #include "e_dasync_err.c"
51 /* Engine Id and Name */
52 static const char *engine_dasync_id = "dasync";
53 static const char *engine_dasync_name = "Dummy Async engine support";
56 /* Engine Lifetime functions */
57 static int dasync_destroy(ENGINE *e);
58 static int dasync_init(ENGINE *e);
59 static int dasync_finish(ENGINE *e);
60 void engine_load_dasync_int(void);
63 /* Set up digests. Just SHA1 for now */
64 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
65 const int **nids, int nid);
67 static void dummy_pause_job(void);
70 static int dasync_sha1_init(EVP_MD_CTX *ctx);
71 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
73 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
76 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
77 * engine bind and can then be reused many times.
79 static EVP_MD *_hidden_sha1_md = NULL;
80 static const EVP_MD *dasync_sha1(void)
82 return _hidden_sha1_md;
84 static void destroy_digests(void)
86 EVP_MD_meth_free(_hidden_sha1_md);
87 _hidden_sha1_md = NULL;
90 static int dasync_digest_nids(const int **nids)
92 static int digest_nids[2] = { 0, 0 };
98 if ((md = dasync_sha1()) != NULL)
99 digest_nids[pos++] = EVP_MD_type(md);
100 digest_nids[pos] = 0;
108 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
109 const int **pnids, int nid);
111 static int dasync_rsa_init(EVP_PKEY_CTX *ctx);
112 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx);
113 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx);
114 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
115 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx);
116 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
117 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx);
118 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
119 size_t *outlen, const unsigned char *in,
121 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx);
122 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
123 size_t *outlen, const unsigned char *in,
125 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
126 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
129 static EVP_PKEY_METHOD *dasync_rsa;
130 static const EVP_PKEY_METHOD *dasync_rsa_orig;
134 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
136 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
137 const unsigned char *iv, int enc);
138 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
139 const unsigned char *in, size_t inl);
140 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
142 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
144 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
145 const unsigned char *key,
146 const unsigned char *iv,
148 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
150 const unsigned char *in,
152 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
154 struct dasync_pipeline_ctx {
155 void *inner_cipher_data;
156 unsigned int numpipes;
157 unsigned char **inbufs;
158 unsigned char **outbufs;
160 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
165 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
166 * during engine bind and can then be reused many times.
168 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
169 static const EVP_CIPHER *dasync_aes_128_cbc(void)
171 return _hidden_aes_128_cbc;
175 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
176 * once only during engine bind and can then be reused many times.
178 * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
179 * which is implemented only if the AES-NI instruction set extension is available
180 * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
181 * be available either.
183 * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
184 * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
186 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
187 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
189 return _hidden_aes_128_cbc_hmac_sha1;
192 static void destroy_ciphers(void)
194 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
195 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
196 _hidden_aes_128_cbc = NULL;
197 _hidden_aes_128_cbc_hmac_sha1 = NULL;
200 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
201 const int **nids, int nid);
203 static int dasync_cipher_nids[] = {
205 NID_aes_128_cbc_hmac_sha1,
209 static int bind_dasync(ENGINE *e)
213 if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
214 || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)) == NULL)
216 EVP_PKEY_meth_set_init(dasync_rsa, dasync_rsa_init);
217 EVP_PKEY_meth_set_cleanup(dasync_rsa, dasync_rsa_cleanup);
218 EVP_PKEY_meth_set_paramgen(dasync_rsa, dasync_rsa_paramgen_init,
219 dasync_rsa_paramgen);
220 EVP_PKEY_meth_set_keygen(dasync_rsa, dasync_rsa_keygen_init,
222 EVP_PKEY_meth_set_encrypt(dasync_rsa, dasync_rsa_encrypt_init,
224 EVP_PKEY_meth_set_decrypt(dasync_rsa, dasync_rsa_decrypt_init,
226 EVP_PKEY_meth_set_ctrl(dasync_rsa, dasync_rsa_ctrl,
227 dasync_rsa_ctrl_str);
229 /* Ensure the dasync error handling is set up */
230 ERR_load_DASYNC_strings();
232 if (!ENGINE_set_id(e, engine_dasync_id)
233 || !ENGINE_set_name(e, engine_dasync_name)
234 || !ENGINE_set_pkey_meths(e, dasync_pkey)
235 || !ENGINE_set_digests(e, dasync_digests)
236 || !ENGINE_set_ciphers(e, dasync_ciphers)
237 || !ENGINE_set_destroy_function(e, dasync_destroy)
238 || !ENGINE_set_init_function(e, dasync_init)
239 || !ENGINE_set_finish_function(e, dasync_finish)) {
240 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
245 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
246 * supplied by this engine
248 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
249 if (_hidden_sha1_md == NULL
250 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
251 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
252 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
253 sizeof(EVP_MD *) + sizeof(SHA_CTX))
254 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
255 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
256 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
257 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
258 EVP_MD_meth_free(_hidden_sha1_md);
259 _hidden_sha1_md = NULL;
262 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
265 if (_hidden_aes_128_cbc == NULL
266 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
267 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
268 EVP_CIPH_FLAG_DEFAULT_ASN1
270 | EVP_CIPH_FLAG_PIPELINE)
271 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
272 dasync_aes128_init_key)
273 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
274 dasync_aes128_cbc_cipher)
275 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
276 dasync_aes128_cbc_cleanup)
277 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
278 dasync_aes128_cbc_ctrl)
279 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
280 sizeof(struct dasync_pipeline_ctx))) {
281 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
282 _hidden_aes_128_cbc = NULL;
285 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
286 NID_aes_128_cbc_hmac_sha1,
289 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
290 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
291 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
293 | EVP_CIPH_FLAG_DEFAULT_ASN1
294 | EVP_CIPH_FLAG_AEAD_CIPHER
295 | EVP_CIPH_FLAG_PIPELINE)
296 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
297 dasync_aes128_cbc_hmac_sha1_init_key)
298 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
299 dasync_aes128_cbc_hmac_sha1_cipher)
300 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
301 dasync_aes128_cbc_hmac_sha1_cleanup)
302 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
303 dasync_aes128_cbc_hmac_sha1_ctrl)
304 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
305 sizeof(struct dasync_pipeline_ctx))) {
306 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
307 _hidden_aes_128_cbc_hmac_sha1 = NULL;
313 static void destroy_pkey(void)
315 EVP_PKEY_meth_free(dasync_rsa);
316 dasync_rsa_orig = NULL;
320 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
321 static int bind_helper(ENGINE *e, const char *id)
323 if (id && (strcmp(id, engine_dasync_id) != 0))
330 IMPLEMENT_DYNAMIC_CHECK_FN()
331 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
334 static ENGINE *engine_dasync(void)
336 ENGINE *ret = ENGINE_new();
339 if (!bind_dasync(ret)) {
346 void engine_load_dasync_int(void)
348 ENGINE *toadd = engine_dasync();
356 static int dasync_init(ENGINE *e)
362 static int dasync_finish(ENGINE *e)
368 static int dasync_destroy(ENGINE *e)
373 ERR_unload_DASYNC_strings();
377 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
378 const int **pnids, int nid)
380 static const int rnid = EVP_PKEY_RSA;
387 if (nid == EVP_PKEY_RSA) {
396 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
397 const int **nids, int nid)
401 /* We are returning a list of supported nids */
402 return dasync_digest_nids(nids);
404 /* We are being asked for a specific digest */
407 *digest = dasync_sha1();
417 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
418 const int **nids, int nid)
421 if (cipher == NULL) {
422 /* We are returning a list of supported nids */
423 *nids = dasync_cipher_nids;
424 return (sizeof(dasync_cipher_nids) -
425 1) / sizeof(dasync_cipher_nids[0]);
427 /* We are being asked for a specific cipher */
429 case NID_aes_128_cbc:
430 *cipher = dasync_aes_128_cbc();
432 case NID_aes_128_cbc_hmac_sha1:
433 *cipher = dasync_aes_128_cbc_hmac_sha1();
443 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
444 OSSL_ASYNC_FD readfd, void *pvwritefd)
446 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
447 #if defined(ASYNC_WIN)
449 CloseHandle(*pwritefd);
450 #elif defined(ASYNC_POSIX)
454 OPENSSL_free(pwritefd);
457 #define DUMMY_CHAR 'X'
459 static void dummy_pause_job(void) {
461 ASYNC_WAIT_CTX *waitctx;
462 ASYNC_callback_fn callback;
464 OSSL_ASYNC_FD pipefds[2] = {0, 0};
465 OSSL_ASYNC_FD *writefd;
466 #if defined(ASYNC_WIN)
467 DWORD numwritten, numread;
468 char buf = DUMMY_CHAR;
469 #elif defined(ASYNC_POSIX)
470 char buf = DUMMY_CHAR;
473 if ((job = ASYNC_get_current_job()) == NULL)
476 waitctx = ASYNC_get_wait_ctx(job);
478 if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
480 * In the Dummy async engine we are cheating. We call the callback that the job
481 * is complete before the call to ASYNC_pause_job(). A real
482 * async engine would only call the callback when the job was actually complete
484 (*callback)(callback_arg);
490 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
491 (void **)&writefd)) {
492 pipefds[1] = *writefd;
494 writefd = OPENSSL_malloc(sizeof(*writefd));
497 #if defined(ASYNC_WIN)
498 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
499 OPENSSL_free(writefd);
502 #elif defined(ASYNC_POSIX)
503 if (pipe(pipefds) != 0) {
504 OPENSSL_free(writefd);
508 *writefd = pipefds[1];
510 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
511 writefd, wait_cleanup)) {
512 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
517 * In the Dummy async engine we are cheating. We signal that the job
518 * is complete by waking it before the call to ASYNC_pause_job(). A real
519 * async engine would only wake when the job was actually complete
521 #if defined(ASYNC_WIN)
522 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
523 #elif defined(ASYNC_POSIX)
524 if (write(pipefds[1], &buf, 1) < 0)
528 /* Ignore errors - we carry on anyway */
531 /* Clear the wake signal */
532 #if defined(ASYNC_WIN)
533 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
534 #elif defined(ASYNC_POSIX)
535 if (read(pipefds[0], &buf, 1) < 0)
541 * SHA1 implementation. At the moment we just defer to the standard
544 static int dasync_sha1_init(EVP_MD_CTX *ctx)
548 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
551 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
556 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
559 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
563 return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
566 /* Cipher helper functions */
568 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
569 void *ptr, int aeadcapable)
572 struct dasync_pipeline_ctx *pipe_ctx =
573 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
575 if (pipe_ctx == NULL)
579 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
580 pipe_ctx->numpipes = arg;
581 pipe_ctx->outbufs = (unsigned char **)ptr;
584 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
585 pipe_ctx->numpipes = arg;
586 pipe_ctx->inbufs = (unsigned char **)ptr;
589 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
590 pipe_ctx->numpipes = arg;
591 pipe_ctx->lens = (size_t *)ptr;
594 case EVP_CTRL_AEAD_SET_MAC_KEY:
597 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
598 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
599 (ctx, type, arg, ptr);
600 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
603 case EVP_CTRL_AEAD_TLS1_AAD:
605 unsigned char *p = ptr;
608 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
611 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
614 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
615 EVP_AEAD_TLS1_AAD_LEN);
618 len = p[arg - 2] << 8 | p[arg - 1];
620 if (EVP_CIPHER_CTX_encrypting(ctx)) {
621 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
622 if (len < AES_BLOCK_SIZE)
624 len -= AES_BLOCK_SIZE;
627 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
628 & -AES_BLOCK_SIZE) - len;
630 return SHA_DIGEST_LENGTH;
641 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
642 const unsigned char *key,
643 const unsigned char *iv, int enc,
644 const EVP_CIPHER *cipher)
647 struct dasync_pipeline_ctx *pipe_ctx =
648 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
650 if (pipe_ctx->inner_cipher_data == NULL
651 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
652 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
653 EVP_CIPHER_impl_ctx_size(cipher));
654 if (pipe_ctx->inner_cipher_data == NULL) {
655 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
656 ERR_R_MALLOC_FAILURE);
661 pipe_ctx->numpipes = 0;
662 pipe_ctx->aadctr = 0;
664 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
665 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
666 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
671 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
672 const unsigned char *in, size_t inl,
673 const EVP_CIPHER *cipher)
676 unsigned int i, pipes;
677 struct dasync_pipeline_ctx *pipe_ctx =
678 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
680 pipes = pipe_ctx->numpipes;
681 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
683 if (pipe_ctx->aadctr != 0) {
684 if (pipe_ctx->aadctr != 1)
686 EVP_CIPHER_meth_get_ctrl(cipher)
687 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
688 EVP_AEAD_TLS1_AAD_LEN,
689 pipe_ctx->tlsaad[0]);
691 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
694 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
696 for (i = 0; i < pipes; i++) {
697 if (pipe_ctx->aadctr > 0) {
698 EVP_CIPHER_meth_get_ctrl(cipher)
699 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
700 EVP_AEAD_TLS1_AAD_LEN,
701 pipe_ctx->tlsaad[i]);
703 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
704 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
707 pipe_ctx->numpipes = 0;
709 pipe_ctx->aadctr = 0;
710 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
714 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
715 const EVP_CIPHER *cipher)
717 struct dasync_pipeline_ctx *pipe_ctx =
718 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
720 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
721 EVP_CIPHER_impl_ctx_size(cipher));
727 * AES128 CBC Implementation
730 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
733 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
736 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
737 const unsigned char *iv, int enc)
739 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
742 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
743 const unsigned char *in, size_t inl)
745 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
748 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
750 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
755 * AES128 CBC HMAC SHA1 Implementation
758 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
761 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
764 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
765 const unsigned char *key,
766 const unsigned char *iv,
770 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
771 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
773 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
774 EVP_aes_128_cbc_hmac_sha1());
777 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
779 const unsigned char *in,
782 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
785 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
788 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
789 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
791 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
798 static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
800 static int (*pinit)(EVP_PKEY_CTX *ctx);
803 EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
807 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
809 static void (*pcleanup)(EVP_PKEY_CTX *ctx);
811 if (pcleanup == NULL)
812 EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
816 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
818 static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
820 if (pparamgen_init == NULL)
821 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
822 return pparamgen_init(ctx);
825 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
827 static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
829 if (pparamgen == NULL)
830 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
831 return pparamgen(ctx, pkey);
834 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
836 static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
838 if (pkeygen_init == NULL)
839 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
840 return pkeygen_init(ctx);
843 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
845 static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
848 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
849 return pkeygen(ctx, pkey);
852 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
854 static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
856 if (pencrypt_init == NULL)
857 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
858 return pencrypt_init(ctx);
861 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
862 size_t *outlen, const unsigned char *in,
865 static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
866 size_t *outlen, const unsigned char *in,
869 if (pencryptfn == NULL)
870 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
871 return pencryptfn(ctx, out, outlen, in, inlen);
874 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
876 static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
878 if (pdecrypt_init == NULL)
879 EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
880 return pdecrypt_init(ctx);
883 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
884 size_t *outlen, const unsigned char *in,
887 static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
888 size_t *outlen, const unsigned char *in,
891 if (pdecrypt == NULL)
892 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pdecrypt);
893 return pdecrypt(ctx, out, outlen, in, inlen);
896 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
898 static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
901 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
902 return pctrl(ctx, type, p1, p2);
905 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
908 static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
911 if (pctrl_str == NULL)
912 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
913 return pctrl_str(ctx, type, value);