2 * Copyright 2015-2018 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
11 * SHA-1 low level APIs are deprecated for public use, but still ok for
12 * internal use. Note, that due to symbols not being exported, only the
13 * #defines and strucures can be accessed, in this case SHA_CBLOCK and
16 #include "internal/deprecated.h"
18 #include <openssl/opensslconf.h>
26 #include <openssl/engine.h>
27 #include <openssl/sha.h>
28 #include <openssl/aes.h>
29 #include <openssl/rsa.h>
30 #include <openssl/evp.h>
31 #include <openssl/async.h>
32 #include <openssl/bn.h>
33 #include <openssl/crypto.h>
34 #include <openssl/ssl.h>
35 #include <openssl/modes.h>
37 #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
46 #include "e_dasync_err.c"
48 /* Engine Id and Name */
49 static const char *engine_dasync_id = "dasync";
50 static const char *engine_dasync_name = "Dummy Async engine support";
53 /* Engine Lifetime functions */
54 static int dasync_destroy(ENGINE *e);
55 static int dasync_init(ENGINE *e);
56 static int dasync_finish(ENGINE *e);
57 void engine_load_dasync_int(void);
60 /* Set up digests. Just SHA1 for now */
61 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
62 const int **nids, int nid);
64 static void dummy_pause_job(void);
67 static int dasync_sha1_init(EVP_MD_CTX *ctx);
68 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
70 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
73 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
74 * engine bind and can then be reused many times.
76 static EVP_MD *_hidden_sha1_md = NULL;
77 static const EVP_MD *dasync_sha1(void)
79 return _hidden_sha1_md;
81 static void destroy_digests(void)
83 EVP_MD_meth_free(_hidden_sha1_md);
84 _hidden_sha1_md = NULL;
87 static int dasync_digest_nids(const int **nids)
89 static int digest_nids[2] = { 0, 0 };
95 if ((md = dasync_sha1()) != NULL)
96 digest_nids[pos++] = EVP_MD_type(md);
105 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
106 const int **pnids, int nid);
108 static int dasync_rsa_init(EVP_PKEY_CTX *ctx);
109 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx);
110 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx);
111 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
112 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx);
113 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
114 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx);
115 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
116 size_t *outlen, const unsigned char *in,
118 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx);
119 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
120 size_t *outlen, const unsigned char *in,
122 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
123 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
126 static EVP_PKEY_METHOD *dasync_rsa;
127 static const EVP_PKEY_METHOD *dasync_rsa_orig;
131 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
133 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
134 const unsigned char *iv, int enc);
135 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
136 const unsigned char *in, size_t inl);
137 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
139 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
141 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
142 const unsigned char *key,
143 const unsigned char *iv,
145 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
147 const unsigned char *in,
149 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
151 struct dasync_pipeline_ctx {
152 void *inner_cipher_data;
153 unsigned int numpipes;
154 unsigned char **inbufs;
155 unsigned char **outbufs;
157 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
162 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
163 * during engine bind and can then be reused many times.
165 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
166 static const EVP_CIPHER *dasync_aes_128_cbc(void)
168 return _hidden_aes_128_cbc;
172 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
173 * once only during engine bind and can then be reused many times.
175 * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
176 * which is implemented only if the AES-NI instruction set extension is available
177 * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
178 * be available either.
180 * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
181 * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
183 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
184 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
186 return _hidden_aes_128_cbc_hmac_sha1;
189 static void destroy_ciphers(void)
191 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
192 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
193 _hidden_aes_128_cbc = NULL;
194 _hidden_aes_128_cbc_hmac_sha1 = NULL;
197 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
198 const int **nids, int nid);
200 static int dasync_cipher_nids[] = {
202 NID_aes_128_cbc_hmac_sha1,
206 static int bind_dasync(ENGINE *e)
210 if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
211 || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)) == NULL)
213 EVP_PKEY_meth_set_init(dasync_rsa, dasync_rsa_init);
214 EVP_PKEY_meth_set_cleanup(dasync_rsa, dasync_rsa_cleanup);
215 EVP_PKEY_meth_set_paramgen(dasync_rsa, dasync_rsa_paramgen_init,
216 dasync_rsa_paramgen);
217 EVP_PKEY_meth_set_keygen(dasync_rsa, dasync_rsa_keygen_init,
219 EVP_PKEY_meth_set_encrypt(dasync_rsa, dasync_rsa_encrypt_init,
221 EVP_PKEY_meth_set_decrypt(dasync_rsa, dasync_rsa_decrypt_init,
223 EVP_PKEY_meth_set_ctrl(dasync_rsa, dasync_rsa_ctrl,
224 dasync_rsa_ctrl_str);
226 /* Ensure the dasync error handling is set up */
227 ERR_load_DASYNC_strings();
229 if (!ENGINE_set_id(e, engine_dasync_id)
230 || !ENGINE_set_name(e, engine_dasync_name)
231 || !ENGINE_set_pkey_meths(e, dasync_pkey)
232 || !ENGINE_set_digests(e, dasync_digests)
233 || !ENGINE_set_ciphers(e, dasync_ciphers)
234 || !ENGINE_set_destroy_function(e, dasync_destroy)
235 || !ENGINE_set_init_function(e, dasync_init)
236 || !ENGINE_set_finish_function(e, dasync_finish)) {
237 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
242 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
243 * supplied by this engine
245 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
246 if (_hidden_sha1_md == NULL
247 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
248 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
249 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
250 sizeof(EVP_MD *) + sizeof(SHA_CTX))
251 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
252 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
253 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
254 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
255 EVP_MD_meth_free(_hidden_sha1_md);
256 _hidden_sha1_md = NULL;
259 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
262 if (_hidden_aes_128_cbc == NULL
263 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
264 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
265 EVP_CIPH_FLAG_DEFAULT_ASN1
267 | EVP_CIPH_FLAG_PIPELINE)
268 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
269 dasync_aes128_init_key)
270 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
271 dasync_aes128_cbc_cipher)
272 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
273 dasync_aes128_cbc_cleanup)
274 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
275 dasync_aes128_cbc_ctrl)
276 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
277 sizeof(struct dasync_pipeline_ctx))) {
278 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
279 _hidden_aes_128_cbc = NULL;
282 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
283 NID_aes_128_cbc_hmac_sha1,
286 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
287 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
288 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
290 | EVP_CIPH_FLAG_DEFAULT_ASN1
291 | EVP_CIPH_FLAG_AEAD_CIPHER
292 | EVP_CIPH_FLAG_PIPELINE)
293 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
294 dasync_aes128_cbc_hmac_sha1_init_key)
295 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
296 dasync_aes128_cbc_hmac_sha1_cipher)
297 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
298 dasync_aes128_cbc_hmac_sha1_cleanup)
299 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
300 dasync_aes128_cbc_hmac_sha1_ctrl)
301 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
302 sizeof(struct dasync_pipeline_ctx))) {
303 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
304 _hidden_aes_128_cbc_hmac_sha1 = NULL;
310 static void destroy_pkey(void)
312 EVP_PKEY_meth_free(dasync_rsa);
313 dasync_rsa_orig = NULL;
317 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
318 static int bind_helper(ENGINE *e, const char *id)
320 if (id && (strcmp(id, engine_dasync_id) != 0))
327 IMPLEMENT_DYNAMIC_CHECK_FN()
328 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
331 static ENGINE *engine_dasync(void)
333 ENGINE *ret = ENGINE_new();
336 if (!bind_dasync(ret)) {
343 void engine_load_dasync_int(void)
345 ENGINE *toadd = engine_dasync();
353 static int dasync_init(ENGINE *e)
359 static int dasync_finish(ENGINE *e)
365 static int dasync_destroy(ENGINE *e)
370 ERR_unload_DASYNC_strings();
374 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
375 const int **pnids, int nid)
377 static const int rnid = EVP_PKEY_RSA;
384 if (nid == EVP_PKEY_RSA) {
393 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
394 const int **nids, int nid)
398 /* We are returning a list of supported nids */
399 return dasync_digest_nids(nids);
401 /* We are being asked for a specific digest */
404 *digest = dasync_sha1();
414 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
415 const int **nids, int nid)
418 if (cipher == NULL) {
419 /* We are returning a list of supported nids */
420 *nids = dasync_cipher_nids;
421 return (sizeof(dasync_cipher_nids) -
422 1) / sizeof(dasync_cipher_nids[0]);
424 /* We are being asked for a specific cipher */
426 case NID_aes_128_cbc:
427 *cipher = dasync_aes_128_cbc();
429 case NID_aes_128_cbc_hmac_sha1:
430 *cipher = dasync_aes_128_cbc_hmac_sha1();
440 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
441 OSSL_ASYNC_FD readfd, void *pvwritefd)
443 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
444 #if defined(ASYNC_WIN)
446 CloseHandle(*pwritefd);
447 #elif defined(ASYNC_POSIX)
451 OPENSSL_free(pwritefd);
454 #define DUMMY_CHAR 'X'
456 static void dummy_pause_job(void) {
458 ASYNC_WAIT_CTX *waitctx;
459 ASYNC_callback_fn callback;
461 OSSL_ASYNC_FD pipefds[2] = {0, 0};
462 OSSL_ASYNC_FD *writefd;
463 #if defined(ASYNC_WIN)
464 DWORD numwritten, numread;
465 char buf = DUMMY_CHAR;
466 #elif defined(ASYNC_POSIX)
467 char buf = DUMMY_CHAR;
470 if ((job = ASYNC_get_current_job()) == NULL)
473 waitctx = ASYNC_get_wait_ctx(job);
475 if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
477 * In the Dummy async engine we are cheating. We call the callback that the job
478 * is complete before the call to ASYNC_pause_job(). A real
479 * async engine would only call the callback when the job was actually complete
481 (*callback)(callback_arg);
487 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
488 (void **)&writefd)) {
489 pipefds[1] = *writefd;
491 writefd = OPENSSL_malloc(sizeof(*writefd));
494 #if defined(ASYNC_WIN)
495 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
496 OPENSSL_free(writefd);
499 #elif defined(ASYNC_POSIX)
500 if (pipe(pipefds) != 0) {
501 OPENSSL_free(writefd);
505 *writefd = pipefds[1];
507 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
508 writefd, wait_cleanup)) {
509 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
514 * In the Dummy async engine we are cheating. We signal that the job
515 * is complete by waking it before the call to ASYNC_pause_job(). A real
516 * async engine would only wake when the job was actually complete
518 #if defined(ASYNC_WIN)
519 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
520 #elif defined(ASYNC_POSIX)
521 if (write(pipefds[1], &buf, 1) < 0)
525 /* Ignore errors - we carry on anyway */
528 /* Clear the wake signal */
529 #if defined(ASYNC_WIN)
530 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
531 #elif defined(ASYNC_POSIX)
532 if (read(pipefds[0], &buf, 1) < 0)
538 * SHA1 implementation. At the moment we just defer to the standard
541 static int dasync_sha1_init(EVP_MD_CTX *ctx)
545 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
548 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
553 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
556 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
560 return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
563 /* Cipher helper functions */
565 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
566 void *ptr, int aeadcapable)
569 struct dasync_pipeline_ctx *pipe_ctx =
570 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
572 if (pipe_ctx == NULL)
576 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
577 pipe_ctx->numpipes = arg;
578 pipe_ctx->outbufs = (unsigned char **)ptr;
581 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
582 pipe_ctx->numpipes = arg;
583 pipe_ctx->inbufs = (unsigned char **)ptr;
586 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
587 pipe_ctx->numpipes = arg;
588 pipe_ctx->lens = (size_t *)ptr;
591 case EVP_CTRL_AEAD_SET_MAC_KEY:
594 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
595 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
596 (ctx, type, arg, ptr);
597 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
600 case EVP_CTRL_AEAD_TLS1_AAD:
602 unsigned char *p = ptr;
605 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
608 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
611 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
612 EVP_AEAD_TLS1_AAD_LEN);
615 len = p[arg - 2] << 8 | p[arg - 1];
617 if (EVP_CIPHER_CTX_encrypting(ctx)) {
618 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
619 if (len < AES_BLOCK_SIZE)
621 len -= AES_BLOCK_SIZE;
624 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
625 & -AES_BLOCK_SIZE) - len;
627 return SHA_DIGEST_LENGTH;
638 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
639 const unsigned char *key,
640 const unsigned char *iv, int enc,
641 const EVP_CIPHER *cipher)
644 struct dasync_pipeline_ctx *pipe_ctx =
645 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
647 if (pipe_ctx->inner_cipher_data == NULL
648 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
649 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
650 EVP_CIPHER_impl_ctx_size(cipher));
651 if (pipe_ctx->inner_cipher_data == NULL) {
652 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
653 ERR_R_MALLOC_FAILURE);
658 pipe_ctx->numpipes = 0;
659 pipe_ctx->aadctr = 0;
661 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
662 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
663 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
668 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
669 const unsigned char *in, size_t inl,
670 const EVP_CIPHER *cipher)
673 unsigned int i, pipes;
674 struct dasync_pipeline_ctx *pipe_ctx =
675 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
677 pipes = pipe_ctx->numpipes;
678 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
680 if (pipe_ctx->aadctr != 0) {
681 if (pipe_ctx->aadctr != 1)
683 EVP_CIPHER_meth_get_ctrl(cipher)
684 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
685 EVP_AEAD_TLS1_AAD_LEN,
686 pipe_ctx->tlsaad[0]);
688 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
691 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
693 for (i = 0; i < pipes; i++) {
694 if (pipe_ctx->aadctr > 0) {
695 EVP_CIPHER_meth_get_ctrl(cipher)
696 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
697 EVP_AEAD_TLS1_AAD_LEN,
698 pipe_ctx->tlsaad[i]);
700 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
701 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
704 pipe_ctx->numpipes = 0;
706 pipe_ctx->aadctr = 0;
707 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
711 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
712 const EVP_CIPHER *cipher)
714 struct dasync_pipeline_ctx *pipe_ctx =
715 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
717 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
718 EVP_CIPHER_impl_ctx_size(cipher));
724 * AES128 CBC Implementation
727 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
730 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
733 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
734 const unsigned char *iv, int enc)
736 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
739 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
740 const unsigned char *in, size_t inl)
742 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
745 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
747 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
752 * AES128 CBC HMAC SHA1 Implementation
755 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
758 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
761 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
762 const unsigned char *key,
763 const unsigned char *iv,
767 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
768 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
770 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
771 EVP_aes_128_cbc_hmac_sha1());
774 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
776 const unsigned char *in,
779 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
782 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
785 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
786 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
788 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
795 static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
797 static int (*pinit)(EVP_PKEY_CTX *ctx);
800 EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
804 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
806 static void (*pcleanup)(EVP_PKEY_CTX *ctx);
808 if (pcleanup == NULL)
809 EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
813 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
815 static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
817 if (pparamgen_init == NULL)
818 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
819 return pparamgen_init(ctx);
822 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
824 static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
826 if (pparamgen == NULL)
827 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
828 return pparamgen(ctx, pkey);
831 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
833 static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
835 if (pkeygen_init == NULL)
836 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
837 return pkeygen_init(ctx);
840 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
842 static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
845 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
846 return pkeygen(ctx, pkey);
849 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
851 static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
853 if (pencrypt_init == NULL)
854 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
855 return pencrypt_init(ctx);
858 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
859 size_t *outlen, const unsigned char *in,
862 static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
863 size_t *outlen, const unsigned char *in,
866 if (pencryptfn == NULL)
867 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
868 return pencryptfn(ctx, out, outlen, in, inlen);
871 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
873 static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
875 if (pdecrypt_init == NULL)
876 EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
877 return pdecrypt_init(ctx);
880 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
881 size_t *outlen, const unsigned char *in,
884 static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
885 size_t *outlen, const unsigned char *in,
888 if (pdecrypt == NULL)
889 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pdecrypt);
890 return pdecrypt(ctx, out, outlen, in, inlen);
893 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
895 static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
898 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
899 return pctrl(ctx, type, p1, p2);
902 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
905 static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
908 if (pctrl_str == NULL)
909 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
910 return pctrl_str(ctx, type, value);