1 /* engines/e_dasync.c */
3 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
57 #include <openssl/engine.h>
58 #include <openssl/sha.h>
59 #include <openssl/rsa.h>
60 #include <openssl/evp.h>
61 #include <openssl/async.h>
62 #include <openssl/bn.h>
64 #define DASYNC_LIB_NAME "DASYNC"
65 #include "e_dasync_err.c"
67 /* Engine Id and Name */
68 static const char *engine_dasync_id = "dasync";
69 static const char *engine_dasync_name = "Dummy Async engine support";
72 /* Engine Lifetime functions */
73 static int dasync_destroy(ENGINE *e);
74 static int dasync_init(ENGINE *e);
75 static int dasync_finish(ENGINE *e);
76 void ENGINE_load_dasync(void);
79 /* Set up digests. Just SHA1 for now */
80 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
81 const int **nids, int nid);
83 static void dummy_pause_job(void);
86 static int dasync_sha1_init(EVP_MD_CTX *ctx);
87 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
89 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
91 static EVP_MD *_hidden_sha1_md = NULL;
92 static const EVP_MD *dasync_sha1(void)
94 if (_hidden_sha1_md == NULL) {
97 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
98 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
99 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
100 || !EVP_MD_meth_set_app_datasize(md,
101 sizeof(EVP_MD *) + sizeof(SHA_CTX))
102 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
103 || !EVP_MD_meth_set_init(md, dasync_sha1_init)
104 || !EVP_MD_meth_set_update(md, dasync_sha1_update)
105 || !EVP_MD_meth_set_final(md, dasync_sha1_final)) {
106 EVP_MD_meth_free(md);
109 _hidden_sha1_md = md;
111 return _hidden_sha1_md;
113 static void destroy_digests(void)
115 EVP_MD_meth_free(_hidden_sha1_md);
116 _hidden_sha1_md = NULL;
118 static int dasync_digest_nids(const int **nids)
120 static int digest_nids[2] = { 0, 0 };
126 if ((md = dasync_sha1()) != NULL)
127 digest_nids[pos++] = EVP_MD_type(md);
128 digest_nids[pos] = 0;
137 static int dasync_pub_enc(int flen, const unsigned char *from,
138 unsigned char *to, RSA *rsa, int padding);
139 static int dasync_pub_dec(int flen, const unsigned char *from,
140 unsigned char *to, RSA *rsa, int padding);
141 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
142 unsigned char *to, RSA *rsa, int padding);
143 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
144 unsigned char *to, RSA *rsa, int padding);
145 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
148 static int dasync_rsa_init(RSA *rsa);
149 static int dasync_rsa_finish(RSA *rsa);
151 static RSA_METHOD dasync_rsa_method = {
152 "Dummy Async RSA method",
153 dasync_pub_enc, /* pub_enc */
154 dasync_pub_dec, /* pub_dec */
155 dasync_rsa_priv_enc, /* priv_enc */
156 dasync_rsa_priv_dec, /* priv_dec */
157 dasync_rsa_mod_exp, /* rsa_mod_exp */
158 BN_mod_exp_mont, /* bn_mod_exp */
159 dasync_rsa_init, /* init */
160 dasync_rsa_finish, /* finish */
165 NULL /* rsa_keygen */
169 static int bind_dasync(ENGINE *e)
171 /* Ensure the dasync error handling is set up */
172 ERR_load_DASYNC_strings();
174 if (!ENGINE_set_id(e, engine_dasync_id)
175 || !ENGINE_set_name(e, engine_dasync_name)
176 || !ENGINE_set_RSA(e, &dasync_rsa_method)
177 || !ENGINE_set_digests(e, dasync_digests)
178 || !ENGINE_set_destroy_function(e, dasync_destroy)
179 || !ENGINE_set_init_function(e, dasync_init)
180 || !ENGINE_set_finish_function(e, dasync_finish)) {
181 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
188 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
189 static int bind_helper(ENGINE *e, const char *id)
191 if (id && (strcmp(id, engine_dasync_id) != 0))
198 IMPLEMENT_DYNAMIC_CHECK_FN()
199 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
202 static ENGINE *engine_dasync(void)
204 ENGINE *ret = ENGINE_new();
207 if (!bind_dasync(ret)) {
214 void ENGINE_load_dasync(void)
216 ENGINE *toadd = engine_dasync();
224 static int dasync_init(ENGINE *e)
230 static int dasync_finish(ENGINE *e)
236 static int dasync_destroy(ENGINE *e)
239 ERR_unload_DASYNC_strings();
243 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
244 const int **nids, int nid)
248 /* We are returning a list of supported nids */
249 return dasync_digest_nids(nids);
251 /* We are being asked for a specific digest */
254 *digest = dasync_sha1();
264 static void dummy_pause_job(void) {
267 if ((job = ASYNC_get_current_job()) == NULL)
271 * In the Dummy async engine we are cheating. We signal that the job
272 * is complete by waking it before the call to ASYNC_pause_job(). A real
273 * async engine would only wake when the job was actually complete
277 /* Ignore errors - we carry on anyway */
280 ASYNC_clear_wake(job);
285 * SHA1 implementation. At the moment we just defer to the standard
289 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
290 static int dasync_sha1_init(EVP_MD_CTX *ctx)
294 return SHA1_Init(data(ctx));
297 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
302 return SHA1_Update(data(ctx), data, (size_t)count);
305 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
309 return SHA1_Final(md, data(ctx));
316 static int dasync_pub_enc(int flen, const unsigned char *from,
317 unsigned char *to, RSA *rsa, int padding) {
318 /* Ignore errors - we carry on anyway */
320 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
323 static int dasync_pub_dec(int flen, const unsigned char *from,
324 unsigned char *to, RSA *rsa, int padding) {
325 /* Ignore errors - we carry on anyway */
327 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
330 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
331 unsigned char *to, RSA *rsa, int padding)
333 /* Ignore errors - we carry on anyway */
335 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
338 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
339 unsigned char *to, RSA *rsa, int padding)
341 /* Ignore errors - we carry on anyway */
343 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
346 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
348 /* Ignore errors - we carry on anyway */
350 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
353 static int dasync_rsa_init(RSA *rsa)
355 return RSA_PKCS1_OpenSSL()->init(rsa);
357 static int dasync_rsa_finish(RSA *rsa)
359 return RSA_PKCS1_OpenSSL()->finish(rsa);