2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
56 #include <openssl/engine.h>
57 #include <openssl/sha.h>
58 #include <openssl/rsa.h>
59 #include <openssl/evp.h>
60 #include <openssl/async.h>
61 #include <openssl/bn.h>
62 #include <openssl/crypto.h>
63 #include <openssl/ssl.h>
64 #include <openssl/modes.h>
66 #if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
76 #define DASYNC_LIB_NAME "DASYNC"
77 #include "e_dasync_err.c"
79 /* Engine Id and Name */
80 static const char *engine_dasync_id = "dasync";
81 static const char *engine_dasync_name = "Dummy Async engine support";
84 /* Engine Lifetime functions */
85 static int dasync_destroy(ENGINE *e);
86 static int dasync_init(ENGINE *e);
87 static int dasync_finish(ENGINE *e);
88 void engine_load_dasync_internal(void);
91 /* Set up digests. Just SHA1 for now */
92 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
93 const int **nids, int nid);
95 static void dummy_pause_job(void);
98 static int dasync_sha1_init(EVP_MD_CTX *ctx);
99 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
101 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
103 static EVP_MD *_hidden_sha1_md = NULL;
104 static const EVP_MD *dasync_sha1(void)
106 if (_hidden_sha1_md == NULL) {
109 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
110 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
111 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
112 || !EVP_MD_meth_set_app_datasize(md,
113 sizeof(EVP_MD *) + sizeof(SHA_CTX))
114 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
115 || !EVP_MD_meth_set_init(md, dasync_sha1_init)
116 || !EVP_MD_meth_set_update(md, dasync_sha1_update)
117 || !EVP_MD_meth_set_final(md, dasync_sha1_final)) {
118 EVP_MD_meth_free(md);
121 _hidden_sha1_md = md;
123 return _hidden_sha1_md;
125 static void destroy_digests(void)
127 EVP_MD_meth_free(_hidden_sha1_md);
128 _hidden_sha1_md = NULL;
130 static int dasync_digest_nids(const int **nids)
132 static int digest_nids[2] = { 0, 0 };
138 if ((md = dasync_sha1()) != NULL)
139 digest_nids[pos++] = EVP_MD_type(md);
140 digest_nids[pos] = 0;
149 static int dasync_pub_enc(int flen, const unsigned char *from,
150 unsigned char *to, RSA *rsa, int padding);
151 static int dasync_pub_dec(int flen, const unsigned char *from,
152 unsigned char *to, RSA *rsa, int padding);
153 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
154 unsigned char *to, RSA *rsa, int padding);
155 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
156 unsigned char *to, RSA *rsa, int padding);
157 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
160 static int dasync_rsa_init(RSA *rsa);
161 static int dasync_rsa_finish(RSA *rsa);
163 static RSA_METHOD dasync_rsa_method = {
164 "Dummy Async RSA method",
165 dasync_pub_enc, /* pub_enc */
166 dasync_pub_dec, /* pub_dec */
167 dasync_rsa_priv_enc, /* priv_enc */
168 dasync_rsa_priv_dec, /* priv_dec */
169 dasync_rsa_mod_exp, /* rsa_mod_exp */
170 BN_mod_exp_mont, /* bn_mod_exp */
171 dasync_rsa_init, /* init */
172 dasync_rsa_finish, /* finish */
177 NULL /* rsa_keygen */
183 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
186 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
187 const unsigned char *iv, int enc);
189 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
190 const unsigned char *in, size_t inl);
192 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
194 struct aes_128_cbc_pipeline_ctx {
195 void *inner_cipher_data;
196 unsigned char dummy[256];
197 unsigned int numpipes;
198 unsigned char **inbufs;
199 unsigned char **outbufs;
203 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
204 static const EVP_CIPHER *dasync_aes_128_cbc(void)
206 if (_hidden_aes_128_cbc == NULL)
207 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
210 if (_hidden_aes_128_cbc == NULL
211 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
212 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
213 EVP_CIPH_FLAG_DEFAULT_ASN1
215 | EVP_CIPH_FLAG_PIPELINE)
216 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
217 dasync_aes128_init_key)
218 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
219 dasync_aes128_cbc_cipher)
220 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
221 dasync_aes128_cbc_cleanup)
222 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
223 dasync_aes128_cbc_ctrl)
224 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
225 sizeof(struct aes_128_cbc_pipeline_ctx))) {
226 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
227 _hidden_aes_128_cbc = NULL;
229 return _hidden_aes_128_cbc;
233 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
234 const int **nids, int nid);
236 # ifdef NID_aes_128_cbc_hmac_sha256
237 static int dasync_cipher_nids[] = {
242 static int dasync_cipher_nids[] = { 0 };
245 static int bind_dasync(ENGINE *e)
247 /* Ensure the dasync error handling is set up */
248 ERR_load_DASYNC_strings();
250 if (!ENGINE_set_id(e, engine_dasync_id)
251 || !ENGINE_set_name(e, engine_dasync_name)
252 || !ENGINE_set_RSA(e, &dasync_rsa_method)
253 || !ENGINE_set_digests(e, dasync_digests)
254 || !ENGINE_set_ciphers(e, dasync_ciphers)
255 || !ENGINE_set_destroy_function(e, dasync_destroy)
256 || !ENGINE_set_init_function(e, dasync_init)
257 || !ENGINE_set_finish_function(e, dasync_finish)) {
258 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
265 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
266 static int bind_helper(ENGINE *e, const char *id)
268 if (id && (strcmp(id, engine_dasync_id) != 0))
275 IMPLEMENT_DYNAMIC_CHECK_FN()
276 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
279 static ENGINE *engine_dasync(void)
281 ENGINE *ret = ENGINE_new();
284 if (!bind_dasync(ret)) {
291 void engine_load_dasync_internal(void)
293 ENGINE *toadd = engine_dasync();
301 static int dasync_init(ENGINE *e)
307 static int dasync_finish(ENGINE *e)
313 static int dasync_destroy(ENGINE *e)
316 ERR_unload_DASYNC_strings();
320 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
321 const int **nids, int nid)
325 /* We are returning a list of supported nids */
326 return dasync_digest_nids(nids);
328 /* We are being asked for a specific digest */
331 *digest = dasync_sha1();
341 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
342 const int **nids, int nid)
346 /* We are returning a list of supported nids */
347 *nids = dasync_cipher_nids;
348 return (sizeof(dasync_cipher_nids) -
349 1) / sizeof(dasync_cipher_nids[0]);
351 /* We are being asked for a specific cipher */
353 case NID_aes_128_cbc:
354 *cipher = dasync_aes_128_cbc();
364 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
365 OSSL_ASYNC_FD readfd, void *pvwritefd)
367 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
368 #if defined(ASYNC_WIN)
370 CloseHandle(*pwritefd);
371 #elif defined(ASYNC_POSIX)
375 OPENSSL_free(pwritefd);
378 #define DUMMY_CHAR 'X'
380 static void dummy_pause_job(void) {
382 ASYNC_WAIT_CTX *waitctx;
383 OSSL_ASYNC_FD pipefds[2] = {0, 0};
384 OSSL_ASYNC_FD *writefd;
385 #if defined(ASYNC_WIN)
386 DWORD numwritten, numread;
387 char buf = DUMMY_CHAR;
388 #elif defined(ASYNC_POSIX)
389 char buf = DUMMY_CHAR;
392 if ((job = ASYNC_get_current_job()) == NULL)
395 waitctx = ASYNC_get_wait_ctx(job);
397 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
398 (void **)&writefd)) {
399 pipefds[1] = *writefd;
401 writefd = OPENSSL_malloc(sizeof(*writefd));
404 #if defined(ASYNC_WIN)
405 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
406 OPENSSL_free(writefd);
409 #elif defined(ASYNC_POSIX)
410 if (pipe(pipefds) != 0) {
411 OPENSSL_free(writefd);
415 *writefd = pipefds[1];
417 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
418 writefd, wait_cleanup)) {
419 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
424 * In the Dummy async engine we are cheating. We signal that the job
425 * is complete by waking it before the call to ASYNC_pause_job(). A real
426 * async engine would only wake when the job was actually complete
428 #if defined(ASYNC_WIN)
429 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
430 #elif defined(ASYNC_POSIX)
431 if (write(pipefds[1], &buf, 1) < 0)
435 /* Ignore errors - we carry on anyway */
438 /* Clear the wake signal */
439 #if defined(ASYNC_WIN)
440 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
441 #elif defined(ASYNC_POSIX)
442 if (read(pipefds[0], &buf, 1) < 0)
448 * SHA1 implementation. At the moment we just defer to the standard
452 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
453 static int dasync_sha1_init(EVP_MD_CTX *ctx)
457 return SHA1_Init(data(ctx));
460 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
465 return SHA1_Update(data(ctx), data, (size_t)count);
468 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
472 return SHA1_Final(md, data(ctx));
479 static int dasync_pub_enc(int flen, const unsigned char *from,
480 unsigned char *to, RSA *rsa, int padding) {
481 /* Ignore errors - we carry on anyway */
483 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
486 static int dasync_pub_dec(int flen, const unsigned char *from,
487 unsigned char *to, RSA *rsa, int padding) {
488 /* Ignore errors - we carry on anyway */
490 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
493 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
494 unsigned char *to, RSA *rsa, int padding)
496 /* Ignore errors - we carry on anyway */
498 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
501 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
502 unsigned char *to, RSA *rsa, int padding)
504 /* Ignore errors - we carry on anyway */
506 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
509 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
511 /* Ignore errors - we carry on anyway */
513 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
516 static int dasync_rsa_init(RSA *rsa)
518 return RSA_PKCS1_OpenSSL()->init(rsa);
520 static int dasync_rsa_finish(RSA *rsa)
522 return RSA_PKCS1_OpenSSL()->finish(rsa);
526 * AES128 Implementation
529 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
532 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
533 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
535 if (pipe_ctx == NULL)
539 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
540 pipe_ctx->numpipes = arg;
541 pipe_ctx->outbufs = (unsigned char **)ptr;
544 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
545 pipe_ctx->numpipes = arg;
546 pipe_ctx->inbufs = (unsigned char **)ptr;
549 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
550 pipe_ctx->numpipes = arg;
551 pipe_ctx->lens = (size_t *)ptr;
561 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
562 const unsigned char *iv, int enc)
565 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
566 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
568 if (pipe_ctx->inner_cipher_data == NULL
569 && EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()) != 0) {
570 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
571 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()));
572 if (pipe_ctx->inner_cipher_data == NULL) {
573 DASYNCerr(DASYNC_F_DASYNC_AES128_INIT_KEY,
574 ERR_R_MALLOC_FAILURE);
579 pipe_ctx->numpipes = 0;
581 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
582 ret = EVP_CIPHER_meth_get_init(EVP_aes_128_cbc())(ctx, key, iv, enc);
583 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
588 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
589 const unsigned char *in, size_t inl)
592 unsigned int i, pipes;
593 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
594 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
596 pipes = pipe_ctx->numpipes;
597 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
599 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())
602 for (i = 0; i < pipes; i++) {
603 ret = ret && EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())
604 (ctx, pipe_ctx->outbufs[i],
608 pipe_ctx->numpipes = 0;
610 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
614 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
616 struct aes_128_cbc_pipeline_ctx *pipe_ctx =
617 (struct aes_128_cbc_pipeline_ctx *)EVP_CIPHER_CTX_cipher_data(ctx);
619 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
620 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc()));