blob: cca9f5e3e9ea127555dca753a1114b9cc640d9ac [file] [log] [blame]
Matt Caswella14e9ff2015-02-13 23:25:33 +00001/*
2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
3 */
4/* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
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/)"
23 *
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.
28 *
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.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
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 * ====================================================================
51 */
52
53#include <stdio.h>
54#include <string.h>
55
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>
Matt Caswell7b9f8f72016-02-08 16:43:03 +000062#include <openssl/crypto.h>
Matt Caswella14e9ff2015-02-13 23:25:33 +000063
64#define DASYNC_LIB_NAME "DASYNC"
65#include "e_dasync_err.c"
66
67/* Engine Id and Name */
68static const char *engine_dasync_id = "dasync";
69static const char *engine_dasync_name = "Dummy Async engine support";
70
71
72/* Engine Lifetime functions */
73static int dasync_destroy(ENGINE *e);
74static int dasync_init(ENGINE *e);
75static int dasync_finish(ENGINE *e);
Matt Caswell7b9f8f72016-02-08 16:43:03 +000076void engine_load_dasync_internal(void);
Matt Caswella14e9ff2015-02-13 23:25:33 +000077
78
79/* Set up digests. Just SHA1 for now */
80static int dasync_digests(ENGINE *e, const EVP_MD **digest,
81 const int **nids, int nid);
82
Matt Caswellf4da39d2015-07-24 08:15:31 +010083static void dummy_pause_job(void);
Matt Caswella14e9ff2015-02-13 23:25:33 +000084
85/* SHA1 */
Matt Caswell46a283c2015-10-09 16:45:25 +010086static int dasync_sha1_init(EVP_MD_CTX *ctx);
87static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
Kurt Roeckx652d4a82015-11-21 17:58:12 +010088 size_t count);
Matt Caswell46a283c2015-10-09 16:45:25 +010089static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
Matt Caswella14e9ff2015-02-13 23:25:33 +000090
Richard Levittecddcea82015-11-30 10:24:12 +010091static EVP_MD *_hidden_sha1_md = NULL;
92static const EVP_MD *dasync_sha1(void)
93{
94 if (_hidden_sha1_md == NULL) {
95 EVP_MD *md;
96
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);
107 md = NULL;
108 }
109 _hidden_sha1_md = md;
110 }
111 return _hidden_sha1_md;
112}
113static void destroy_digests(void)
114{
115 EVP_MD_meth_free(_hidden_sha1_md);
116 _hidden_sha1_md = NULL;
117}
118static int dasync_digest_nids(const int **nids)
119{
120 static int digest_nids[2] = { 0, 0 };
121 static int pos = 0;
122 static int init = 0;
123
124 if (!init) {
125 const EVP_MD *md;
126 if ((md = dasync_sha1()) != NULL)
127 digest_nids[pos++] = EVP_MD_type(md);
128 digest_nids[pos] = 0;
129 init = 1;
130 }
131 *nids = digest_nids;
132 return pos;
133}
Matt Caswella14e9ff2015-02-13 23:25:33 +0000134
135/* RSA */
136
137static int dasync_pub_enc(int flen, const unsigned char *from,
138 unsigned char *to, RSA *rsa, int padding);
139static int dasync_pub_dec(int flen, const unsigned char *from,
140 unsigned char *to, RSA *rsa, int padding);
141static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
142 unsigned char *to, RSA *rsa, int padding);
143static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
144 unsigned char *to, RSA *rsa, int padding);
145static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
146 BN_CTX *ctx);
147
148static int dasync_rsa_init(RSA *rsa);
149static int dasync_rsa_finish(RSA *rsa);
150
151static 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 */
161 0, /* flags */
162 NULL, /* app_data */
163 0, /* rsa_sign */
164 0, /* rsa_verify */
165 NULL /* rsa_keygen */
166};
167
168
169static int bind_dasync(ENGINE *e)
170{
171 /* Ensure the dasync error handling is set up */
172 ERR_load_DASYNC_strings();
173
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);
182 return 0;
183 }
184
185 return 1;
186}
187
188# ifndef OPENSSL_NO_DYNAMIC_ENGINE
189static int bind_helper(ENGINE *e, const char *id)
190{
191 if (id && (strcmp(id, engine_dasync_id) != 0))
192 return 0;
193 if (!bind_dasync(e))
194 return 0;
195 return 1;
196}
197
198IMPLEMENT_DYNAMIC_CHECK_FN()
199 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
200# endif
201
202static ENGINE *engine_dasync(void)
203{
204 ENGINE *ret = ENGINE_new();
205 if (!ret)
206 return NULL;
207 if (!bind_dasync(ret)) {
208 ENGINE_free(ret);
209 return NULL;
210 }
211 return ret;
212}
213
Matt Caswell7b9f8f72016-02-08 16:43:03 +0000214void engine_load_dasync_internal(void)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000215{
216 ENGINE *toadd = engine_dasync();
217 if (!toadd)
218 return;
219 ENGINE_add(toadd);
220 ENGINE_free(toadd);
221 ERR_clear_error();
222}
223
224static int dasync_init(ENGINE *e)
225{
226 return 1;
227}
228
229
230static int dasync_finish(ENGINE *e)
231{
232 return 1;
233}
234
235
236static int dasync_destroy(ENGINE *e)
237{
Richard Levittecddcea82015-11-30 10:24:12 +0100238 destroy_digests();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000239 ERR_unload_DASYNC_strings();
240 return 1;
241}
242
243static int dasync_digests(ENGINE *e, const EVP_MD **digest,
244 const int **nids, int nid)
245{
246 int ok = 1;
247 if (!digest) {
248 /* We are returning a list of supported nids */
Richard Levittecddcea82015-11-30 10:24:12 +0100249 return dasync_digest_nids(nids);
Matt Caswella14e9ff2015-02-13 23:25:33 +0000250 }
251 /* We are being asked for a specific digest */
252 switch (nid) {
253 case NID_sha1:
Richard Levittecddcea82015-11-30 10:24:12 +0100254 *digest = dasync_sha1();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000255 break;
256 default:
257 ok = 0;
258 *digest = NULL;
259 break;
260 }
261 return ok;
262}
263
Matt Caswellf4da39d2015-07-24 08:15:31 +0100264static void dummy_pause_job(void) {
265 ASYNC_JOB *job;
266
267 if ((job = ASYNC_get_current_job()) == NULL)
268 return;
269
270 /*
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
274 */
275 ASYNC_wake(job);
276
277 /* Ignore errors - we carry on anyway */
278 ASYNC_pause_job();
279
280 ASYNC_clear_wake(job);
281}
282
Matt Caswella14e9ff2015-02-13 23:25:33 +0000283
284/*
285 * SHA1 implementation. At the moment we just defer to the standard
286 * implementation
287 */
288#undef data
Richard Levitte6e59a892015-11-27 14:02:12 +0100289#define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
Matt Caswell46a283c2015-10-09 16:45:25 +0100290static int dasync_sha1_init(EVP_MD_CTX *ctx)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000291{
Matt Caswellf4da39d2015-07-24 08:15:31 +0100292 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000293
294 return SHA1_Init(data(ctx));
295}
296
Matt Caswell46a283c2015-10-09 16:45:25 +0100297static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
Kurt Roeckx652d4a82015-11-21 17:58:12 +0100298 size_t count)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000299{
Matt Caswellf4da39d2015-07-24 08:15:31 +0100300 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000301
302 return SHA1_Update(data(ctx), data, (size_t)count);
303}
304
Matt Caswell46a283c2015-10-09 16:45:25 +0100305static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000306{
Matt Caswellf4da39d2015-07-24 08:15:31 +0100307 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000308
309 return SHA1_Final(md, data(ctx));
310}
311
312/*
313 * RSA implementation
314 */
315
316static 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 */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100319 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000320 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
321}
322
323static 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 */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100326 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000327 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
328}
329
330static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
331 unsigned char *to, RSA *rsa, int padding)
332{
333 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100334 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000335 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
336}
337
338static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
339 unsigned char *to, RSA *rsa, int padding)
340{
341 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100342 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000343 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
344}
345
346static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
347{
348 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100349 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000350 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
351}
352
353static int dasync_rsa_init(RSA *rsa)
354{
355 return RSA_PKCS1_OpenSSL()->init(rsa);
356}
357static int dasync_rsa_finish(RSA *rsa)
358{
359 return RSA_PKCS1_OpenSSL()->finish(rsa);
360}