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 int dasync_digest_nids[] = { NID_sha1, 0 };
87 static int digest_sha1_init(EVP_MD_CTX *ctx);
88 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
90 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
92 static const EVP_MD digest_sha1 = {
94 NID_sha1WithRSAEncryption,
96 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
102 EVP_PKEY_NULL_method,
104 sizeof(EVP_MD *) + sizeof(SHA_CTX),
109 static int dasync_pub_enc(int flen, const unsigned char *from,
110 unsigned char *to, RSA *rsa, int padding);
111 static int dasync_pub_dec(int flen, const unsigned char *from,
112 unsigned char *to, RSA *rsa, int padding);
113 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
114 unsigned char *to, RSA *rsa, int padding);
115 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
116 unsigned char *to, RSA *rsa, int padding);
117 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
120 static int dasync_rsa_init(RSA *rsa);
121 static int dasync_rsa_finish(RSA *rsa);
123 static RSA_METHOD dasync_rsa_method = {
124 "Dummy Async RSA method",
125 dasync_pub_enc, /* pub_enc */
126 dasync_pub_dec, /* pub_dec */
127 dasync_rsa_priv_enc, /* priv_enc */
128 dasync_rsa_priv_dec, /* priv_dec */
129 dasync_rsa_mod_exp, /* rsa_mod_exp */
130 BN_mod_exp_mont, /* bn_mod_exp */
131 dasync_rsa_init, /* init */
132 dasync_rsa_finish, /* finish */
137 NULL /* rsa_keygen */
141 static int bind_dasync(ENGINE *e)
143 /* Ensure the dasync error handling is set up */
144 ERR_load_DASYNC_strings();
146 if (!ENGINE_set_id(e, engine_dasync_id)
147 || !ENGINE_set_name(e, engine_dasync_name)
148 || !ENGINE_set_RSA(e, &dasync_rsa_method)
149 || !ENGINE_set_digests(e, dasync_digests)
150 || !ENGINE_set_destroy_function(e, dasync_destroy)
151 || !ENGINE_set_init_function(e, dasync_init)
152 || !ENGINE_set_finish_function(e, dasync_finish)) {
153 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
160 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
161 static int bind_helper(ENGINE *e, const char *id)
163 if (id && (strcmp(id, engine_dasync_id) != 0))
170 IMPLEMENT_DYNAMIC_CHECK_FN()
171 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
174 static ENGINE *engine_dasync(void)
176 ENGINE *ret = ENGINE_new();
179 if (!bind_dasync(ret)) {
186 void ENGINE_load_dasync(void)
188 ENGINE *toadd = engine_dasync();
196 static int dasync_init(ENGINE *e)
202 static int dasync_finish(ENGINE *e)
208 static int dasync_destroy(ENGINE *e)
210 ERR_unload_DASYNC_strings();
214 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
215 const int **nids, int nid)
219 /* We are returning a list of supported nids */
220 *nids = dasync_digest_nids;
221 return (sizeof(dasync_digest_nids) -
222 1) / sizeof(dasync_digest_nids[0]);
224 /* We are being asked for a specific digest */
227 *digest = &digest_sha1;
239 * SHA1 implementation. At the moment we just defer to the standard
243 #define data(ctx) ((SHA_CTX *)(ctx)->md_data)
244 static int digest_sha1_init(EVP_MD_CTX *ctx)
246 /* Ignore errors - we carry on anyway */
249 return SHA1_Init(data(ctx));
252 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
255 /* Ignore errors - we carry on anyway */
258 return SHA1_Update(data(ctx), data, (size_t)count);
261 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
263 /* Ignore errors - we carry on anyway */
266 return SHA1_Final(md, data(ctx));
273 static int dasync_pub_enc(int flen, const unsigned char *from,
274 unsigned char *to, RSA *rsa, int padding) {
275 /* Ignore errors - we carry on anyway */
277 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
280 static int dasync_pub_dec(int flen, const unsigned char *from,
281 unsigned char *to, RSA *rsa, int padding) {
282 /* Ignore errors - we carry on anyway */
284 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
287 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
288 unsigned char *to, RSA *rsa, int padding)
290 /* Ignore errors - we carry on anyway */
292 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
295 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
296 unsigned char *to, RSA *rsa, int padding)
298 /* Ignore errors - we carry on anyway */
300 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
303 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
305 /* Ignore errors - we carry on anyway */
307 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
310 static int dasync_rsa_init(RSA *rsa)
312 return RSA_PKCS1_OpenSSL()->init(rsa);
314 static int dasync_rsa_finish(RSA *rsa)
316 return RSA_PKCS1_OpenSSL()->finish(rsa);