]> git.ipfire.org Git - thirdparty/openssl.git/blame - engines/e_dasync.c
Correct top for EC/DSA nonces if BN_DEBUG is on
[thirdparty/openssl.git] / engines / e_dasync.c
CommitLineData
a14e9ff7 1/*
b6461792 2 * Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
a14e9ff7 3 *
ab3fa1c0 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
440e5d80
RS
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
a14e9ff7
MC
8 */
9
cf8e8cba
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
85d843c8
P
13/*
14 * SHA-1 low level APIs are deprecated for public use, but still ok for
15 * internal use. Note, that due to symbols not being exported, only the
eb4129e1 16 * #defines and structures can be accessed, in this case SHA_CBLOCK and
85d843c8
P
17 * sizeof(SHA_CTX).
18 */
19#include "internal/deprecated.h"
20
c5f87134 21#include <openssl/opensslconf.h>
f1f5ee17
AP
22#if defined(_WIN32)
23# include <windows.h>
24#endif
25
a14e9ff7
MC
26#include <stdio.h>
27#include <string.h>
28
29#include <openssl/engine.h>
30#include <openssl/sha.h>
2f2c9caa 31#include <openssl/aes.h>
a14e9ff7
MC
32#include <openssl/rsa.h>
33#include <openssl/evp.h>
34#include <openssl/async.h>
35#include <openssl/bn.h>
7b9f8f7f 36#include <openssl/crypto.h>
98ee7543
MC
37#include <openssl/ssl.h>
38#include <openssl/modes.h>
a14e9ff7 39
6ce66ce0 40#if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
ff75a257
MC
41# undef ASYNC_POSIX
42# define ASYNC_POSIX
43# include <unistd.h>
44#elif defined(_WIN32)
45# undef ASYNC_WIN
46# define ASYNC_WIN
ff75a257
MC
47#endif
48
a14e9ff7
MC
49#include "e_dasync_err.c"
50
51/* Engine Id and Name */
52static const char *engine_dasync_id = "dasync";
53static const char *engine_dasync_name = "Dummy Async engine support";
54
55
56/* Engine Lifetime functions */
57static int dasync_destroy(ENGINE *e);
58static int dasync_init(ENGINE *e);
59static int dasync_finish(ENGINE *e);
b3599dbb 60void engine_load_dasync_int(void);
a14e9ff7
MC
61
62
63/* Set up digests. Just SHA1 for now */
64static int dasync_digests(ENGINE *e, const EVP_MD **digest,
65 const int **nids, int nid);
66
f4da39d2 67static void dummy_pause_job(void);
a14e9ff7
MC
68
69/* SHA1 */
46a283c0
MC
70static int dasync_sha1_init(EVP_MD_CTX *ctx);
71static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
652d4a8c 72 size_t count);
46a283c0 73static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
a14e9ff7 74
11780ac3
MC
75/*
76 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
77 * engine bind and can then be reused many times.
78 */
cddcea8c
RL
79static EVP_MD *_hidden_sha1_md = NULL;
80static const EVP_MD *dasync_sha1(void)
81{
cddcea8c
RL
82 return _hidden_sha1_md;
83}
84static void destroy_digests(void)
85{
86 EVP_MD_meth_free(_hidden_sha1_md);
87 _hidden_sha1_md = NULL;
88}
11780ac3 89
cddcea8c
RL
90static int dasync_digest_nids(const int **nids)
91{
92 static int digest_nids[2] = { 0, 0 };
93 static int pos = 0;
94 static int init = 0;
95
96 if (!init) {
97 const EVP_MD *md;
98 if ((md = dasync_sha1()) != NULL)
ed576acd 99 digest_nids[pos++] = EVP_MD_get_type(md);
cddcea8c
RL
100 digest_nids[pos] = 0;
101 init = 1;
102 }
103 *nids = digest_nids;
104 return pos;
105}
a14e9ff7
MC
106
107/* RSA */
c5f87134
P
108static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
109 const int **pnids, int nid);
110
111static int dasync_rsa_init(EVP_PKEY_CTX *ctx);
112static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx);
113static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx);
114static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
115static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx);
116static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
117static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx);
118static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
119 size_t *outlen, const unsigned char *in,
120 size_t inlen);
121static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx);
122static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
123 size_t *outlen, const unsigned char *in,
124 size_t inlen);
125static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
126static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
127 const char *value);
128
129static EVP_PKEY_METHOD *dasync_rsa;
130static const EVP_PKEY_METHOD *dasync_rsa_orig;
a14e9ff7 131
98ee7543
MC
132/* AES */
133
134static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
135 void *ptr);
98ee7543
MC
136static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
137 const unsigned char *iv, int enc);
98ee7543
MC
138static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
139 const unsigned char *in, size_t inl);
98ee7543
MC
140static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
141
bd363ef3
BE
142static int dasync_aes256_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
143 void *ptr);
144static int dasync_aes256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
145 const unsigned char *iv, int enc);
146static int dasync_aes256_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
147 const unsigned char *in, size_t inl);
148static int dasync_aes256_ctr_cleanup(EVP_CIPHER_CTX *ctx);
149
2f2c9caa
MC
150static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
151 int arg, void *ptr);
152static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
153 const unsigned char *key,
154 const unsigned char *iv,
155 int enc);
156static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
157 unsigned char *out,
158 const unsigned char *in,
159 size_t inl);
160static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
161
e38c2e85 162struct dasync_pipeline_ctx {
98ee7543 163 void *inner_cipher_data;
98ee7543
MC
164 unsigned int numpipes;
165 unsigned char **inbufs;
166 unsigned char **outbufs;
167 size_t *lens;
2f2c9caa
MC
168 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
169 unsigned int aadctr;
98ee7543
MC
170};
171
11780ac3
MC
172/*
173 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
174 * during engine bind and can then be reused many times.
175 */
98ee7543
MC
176static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
177static const EVP_CIPHER *dasync_aes_128_cbc(void)
178{
11780ac3
MC
179 return _hidden_aes_128_cbc;
180}
181
bd363ef3
BE
182static EVP_CIPHER *_hidden_aes_256_ctr = NULL;
183static const EVP_CIPHER *dasync_aes_256_ctr(void)
184{
185 return _hidden_aes_256_ctr;
186}
187
11780ac3
MC
188/*
189 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
190 * once only during engine bind and can then be reused many times.
a4a0a1eb
DMSP
191 *
192 * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
193 * which is implemented only if the AES-NI instruction set extension is available
194 * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
195 * be available either.
196 *
197 * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
198 * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
11780ac3
MC
199 */
200static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
201static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
202{
203 return _hidden_aes_128_cbc_hmac_sha1;
204}
205
206static void destroy_ciphers(void)
207{
208 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
bd363ef3 209 EVP_CIPHER_meth_free(_hidden_aes_256_ctr);
11780ac3
MC
210 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
211 _hidden_aes_128_cbc = NULL;
bd363ef3 212 _hidden_aes_256_ctr = NULL;
11780ac3
MC
213 _hidden_aes_128_cbc_hmac_sha1 = NULL;
214}
215
216static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
217 const int **nids, int nid);
218
219static int dasync_cipher_nids[] = {
220 NID_aes_128_cbc,
bd363ef3 221 NID_aes_256_ctr,
11780ac3
MC
222 NID_aes_128_cbc_hmac_sha1,
223 0
224};
225
226static int bind_dasync(ENGINE *e)
227{
c5f87134 228 /* Setup RSA */
c5f87134 229 if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
59cd0bc1
DB
230 || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA,
231 EVP_PKEY_FLAG_AUTOARGLEN)) == NULL)
b72c9121 232 return 0;
c5f87134
P
233 EVP_PKEY_meth_set_init(dasync_rsa, dasync_rsa_init);
234 EVP_PKEY_meth_set_cleanup(dasync_rsa, dasync_rsa_cleanup);
235 EVP_PKEY_meth_set_paramgen(dasync_rsa, dasync_rsa_paramgen_init,
236 dasync_rsa_paramgen);
237 EVP_PKEY_meth_set_keygen(dasync_rsa, dasync_rsa_keygen_init,
238 dasync_rsa_keygen);
239 EVP_PKEY_meth_set_encrypt(dasync_rsa, dasync_rsa_encrypt_init,
240 dasync_rsa_encrypt);
241 EVP_PKEY_meth_set_decrypt(dasync_rsa, dasync_rsa_decrypt_init,
242 dasync_rsa_decrypt);
243 EVP_PKEY_meth_set_ctrl(dasync_rsa, dasync_rsa_ctrl,
244 dasync_rsa_ctrl_str);
b72c9121 245
11780ac3
MC
246 /* Ensure the dasync error handling is set up */
247 ERR_load_DASYNC_strings();
248
249 if (!ENGINE_set_id(e, engine_dasync_id)
250 || !ENGINE_set_name(e, engine_dasync_name)
c5f87134 251 || !ENGINE_set_pkey_meths(e, dasync_pkey)
11780ac3
MC
252 || !ENGINE_set_digests(e, dasync_digests)
253 || !ENGINE_set_ciphers(e, dasync_ciphers)
254 || !ENGINE_set_destroy_function(e, dasync_destroy)
255 || !ENGINE_set_init_function(e, dasync_init)
256 || !ENGINE_set_finish_function(e, dasync_finish)) {
257 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
258 return 0;
259 }
260
261 /*
262 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
263 * supplied by this engine
264 */
265 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
266 if (_hidden_sha1_md == NULL
267 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
268 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
269 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
270 sizeof(EVP_MD *) + sizeof(SHA_CTX))
271 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
272 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
273 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
274 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
275 EVP_MD_meth_free(_hidden_sha1_md);
276 _hidden_sha1_md = NULL;
277 }
278
279 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
280 16 /* block size */,
281 16 /* key len */);
98ee7543
MC
282 if (_hidden_aes_128_cbc == NULL
283 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
284 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
285 EVP_CIPH_FLAG_DEFAULT_ASN1
286 | EVP_CIPH_CBC_MODE
a0cbc2d2
MC
287 | EVP_CIPH_FLAG_PIPELINE
288 | EVP_CIPH_CUSTOM_COPY)
98ee7543
MC
289 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
290 dasync_aes128_init_key)
291 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
292 dasync_aes128_cbc_cipher)
293 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
294 dasync_aes128_cbc_cleanup)
295 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
296 dasync_aes128_cbc_ctrl)
297 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
e38c2e85 298 sizeof(struct dasync_pipeline_ctx))) {
98ee7543
MC
299 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
300 _hidden_aes_128_cbc = NULL;
301 }
98ee7543 302
bd363ef3
BE
303 _hidden_aes_256_ctr = EVP_CIPHER_meth_new(NID_aes_256_ctr,
304 1 /* block size */,
305 32 /* key len */);
306 if (_hidden_aes_256_ctr == NULL
307 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_256_ctr,16)
308 || !EVP_CIPHER_meth_set_flags(_hidden_aes_256_ctr,
309 EVP_CIPH_FLAG_DEFAULT_ASN1
310 | EVP_CIPH_CTR_MODE
311 | EVP_CIPH_FLAG_PIPELINE
312 | EVP_CIPH_CUSTOM_COPY)
313 || !EVP_CIPHER_meth_set_init(_hidden_aes_256_ctr,
314 dasync_aes256_init_key)
315 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_256_ctr,
316 dasync_aes256_ctr_cipher)
317 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_256_ctr,
318 dasync_aes256_ctr_cleanup)
319 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_256_ctr,
320 dasync_aes256_ctr_ctrl)
321 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_256_ctr,
322 sizeof(struct dasync_pipeline_ctx))) {
323 EVP_CIPHER_meth_free(_hidden_aes_256_ctr);
324 _hidden_aes_256_ctr = NULL;
325 }
326
11780ac3
MC
327 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
328 NID_aes_128_cbc_hmac_sha1,
329 16 /* block size */,
330 16 /* key len */);
2f2c9caa
MC
331 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
332 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
333 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
334 EVP_CIPH_CBC_MODE
335 | EVP_CIPH_FLAG_DEFAULT_ASN1
336 | EVP_CIPH_FLAG_AEAD_CIPHER
a0cbc2d2
MC
337 | EVP_CIPH_FLAG_PIPELINE
338 | EVP_CIPH_CUSTOM_COPY)
2f2c9caa
MC
339 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
340 dasync_aes128_cbc_hmac_sha1_init_key)
341 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
342 dasync_aes128_cbc_hmac_sha1_cipher)
343 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
344 dasync_aes128_cbc_hmac_sha1_cleanup)
345 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
346 dasync_aes128_cbc_hmac_sha1_ctrl)
347 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
e38c2e85 348 sizeof(struct dasync_pipeline_ctx))) {
2f2c9caa
MC
349 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
350 _hidden_aes_128_cbc_hmac_sha1 = NULL;
351 }
a14e9ff7
MC
352
353 return 1;
354}
355
c5f87134
P
356static void destroy_pkey(void)
357{
59cd0bc1
DB
358 /*
359 * We don't actually need to free the dasync_rsa method since this is
360 * automatically freed for us by libcrypto.
361 */
c5f87134
P
362 dasync_rsa_orig = NULL;
363 dasync_rsa = NULL;
364}
365
a14e9ff7
MC
366# ifndef OPENSSL_NO_DYNAMIC_ENGINE
367static int bind_helper(ENGINE *e, const char *id)
368{
369 if (id && (strcmp(id, engine_dasync_id) != 0))
370 return 0;
371 if (!bind_dasync(e))
372 return 0;
373 return 1;
374}
375
376IMPLEMENT_DYNAMIC_CHECK_FN()
377 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
378# endif
379
380static ENGINE *engine_dasync(void)
381{
382 ENGINE *ret = ENGINE_new();
383 if (!ret)
384 return NULL;
385 if (!bind_dasync(ret)) {
386 ENGINE_free(ret);
387 return NULL;
388 }
389 return ret;
390}
391
b3599dbb 392void engine_load_dasync_int(void)
a14e9ff7
MC
393{
394 ENGINE *toadd = engine_dasync();
395 if (!toadd)
396 return;
b9b2135d 397 ERR_set_mark();
a14e9ff7 398 ENGINE_add(toadd);
b9b2135d
MC
399 /*
400 * If the "add" worked, it gets a structural reference. So either way, we
401 * release our just-created reference.
402 */
a14e9ff7 403 ENGINE_free(toadd);
b9b2135d
MC
404 /*
405 * If the "add" didn't work, it was probably a conflict because it was
406 * already added (eg. someone calling ENGINE_load_blah then calling
407 * ENGINE_load_builtin_engines() perhaps).
408 */
409 ERR_pop_to_mark();
a14e9ff7
MC
410}
411
412static int dasync_init(ENGINE *e)
413{
414 return 1;
415}
416
417
418static int dasync_finish(ENGINE *e)
419{
420 return 1;
421}
422
423
424static int dasync_destroy(ENGINE *e)
425{
cddcea8c 426 destroy_digests();
11780ac3 427 destroy_ciphers();
c5f87134 428 destroy_pkey();
a14e9ff7
MC
429 ERR_unload_DASYNC_strings();
430 return 1;
431}
432
c5f87134
P
433static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
434 const int **pnids, int nid)
435{
436 static const int rnid = EVP_PKEY_RSA;
437
438 if (pmeth == NULL) {
439 *pnids = &rnid;
440 return 1;
441 }
442
443 if (nid == EVP_PKEY_RSA) {
444 *pmeth = dasync_rsa;
445 return 1;
446 }
447
448 *pmeth = NULL;
449 return 0;
450}
451
a14e9ff7
MC
452static int dasync_digests(ENGINE *e, const EVP_MD **digest,
453 const int **nids, int nid)
454{
455 int ok = 1;
456 if (!digest) {
457 /* We are returning a list of supported nids */
cddcea8c 458 return dasync_digest_nids(nids);
a14e9ff7
MC
459 }
460 /* We are being asked for a specific digest */
461 switch (nid) {
462 case NID_sha1:
cddcea8c 463 *digest = dasync_sha1();
a14e9ff7
MC
464 break;
465 default:
466 ok = 0;
467 *digest = NULL;
468 break;
469 }
470 return ok;
471}
472
98ee7543
MC
473static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
474 const int **nids, int nid)
475{
476 int ok = 1;
11780ac3 477 if (cipher == NULL) {
98ee7543
MC
478 /* We are returning a list of supported nids */
479 *nids = dasync_cipher_nids;
480 return (sizeof(dasync_cipher_nids) -
481 1) / sizeof(dasync_cipher_nids[0]);
482 }
483 /* We are being asked for a specific cipher */
484 switch (nid) {
485 case NID_aes_128_cbc:
486 *cipher = dasync_aes_128_cbc();
487 break;
bd363ef3
BE
488 case NID_aes_256_ctr:
489 *cipher = dasync_aes_256_ctr();
490 break;
2f2c9caa
MC
491 case NID_aes_128_cbc_hmac_sha1:
492 *cipher = dasync_aes_128_cbc_hmac_sha1();
493 break;
98ee7543
MC
494 default:
495 ok = 0;
496 *cipher = NULL;
497 break;
498 }
499 return ok;
500}
501
ff75a257
MC
502static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
503 OSSL_ASYNC_FD readfd, void *pvwritefd)
504{
505 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
506#if defined(ASYNC_WIN)
507 CloseHandle(readfd);
508 CloseHandle(*pwritefd);
509#elif defined(ASYNC_POSIX)
510 close(readfd);
511 close(*pwritefd);
512#endif
513 OPENSSL_free(pwritefd);
514}
515
516#define DUMMY_CHAR 'X'
517
f4da39d2
MC
518static void dummy_pause_job(void) {
519 ASYNC_JOB *job;
ff75a257 520 ASYNC_WAIT_CTX *waitctx;
9f5a87fd 521 ASYNC_callback_fn callback;
bbaeadb0 522 void *callback_arg;
ff75a257
MC
523 OSSL_ASYNC_FD pipefds[2] = {0, 0};
524 OSSL_ASYNC_FD *writefd;
525#if defined(ASYNC_WIN)
526 DWORD numwritten, numread;
527 char buf = DUMMY_CHAR;
528#elif defined(ASYNC_POSIX)
529 char buf = DUMMY_CHAR;
530#endif
f4da39d2
MC
531
532 if ((job = ASYNC_get_current_job()) == NULL)
533 return;
534
ff75a257
MC
535 waitctx = ASYNC_get_wait_ctx(job);
536
9f5a87fd
PY
537 if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
538 /*
539 * In the Dummy async engine we are cheating. We call the callback that the job
540 * is complete before the call to ASYNC_pause_job(). A real
541 * async engine would only call the callback when the job was actually complete
542 */
543 (*callback)(callback_arg);
544 ASYNC_pause_job();
545 return;
546 }
547
548
ff75a257
MC
549 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
550 (void **)&writefd)) {
551 pipefds[1] = *writefd;
552 } else {
553 writefd = OPENSSL_malloc(sizeof(*writefd));
554 if (writefd == NULL)
555 return;
556#if defined(ASYNC_WIN)
557 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
558 OPENSSL_free(writefd);
559 return;
560 }
561#elif defined(ASYNC_POSIX)
562 if (pipe(pipefds) != 0) {
563 OPENSSL_free(writefd);
564 return;
565 }
566#endif
567 *writefd = pipefds[1];
568
f479eab2
F
569 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
570 writefd, wait_cleanup)) {
ff75a257
MC
571 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
572 return;
573 }
574 }
f4da39d2
MC
575 /*
576 * In the Dummy async engine we are cheating. We signal that the job
577 * is complete by waking it before the call to ASYNC_pause_job(). A real
578 * async engine would only wake when the job was actually complete
579 */
ff75a257
MC
580#if defined(ASYNC_WIN)
581 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
582#elif defined(ASYNC_POSIX)
b8972eda
AG
583 if (write(pipefds[1], &buf, 1) < 0)
584 return;
ff75a257 585#endif
f4da39d2
MC
586
587 /* Ignore errors - we carry on anyway */
588 ASYNC_pause_job();
589
ff75a257
MC
590 /* Clear the wake signal */
591#if defined(ASYNC_WIN)
592 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
593#elif defined(ASYNC_POSIX)
b8972eda
AG
594 if (read(pipefds[0], &buf, 1) < 0)
595 return;
ff75a257 596#endif
f4da39d2
MC
597}
598
a14e9ff7
MC
599/*
600 * SHA1 implementation. At the moment we just defer to the standard
601 * implementation
602 */
46a283c0 603static int dasync_sha1_init(EVP_MD_CTX *ctx)
a14e9ff7 604{
f4da39d2 605 dummy_pause_job();
a14e9ff7 606
85d843c8 607 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
a14e9ff7
MC
608}
609
46a283c0 610static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
652d4a8c 611 size_t count)
a14e9ff7 612{
f4da39d2 613 dummy_pause_job();
a14e9ff7 614
85d843c8 615 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
a14e9ff7
MC
616}
617
46a283c0 618static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
a14e9ff7 619{
f4da39d2 620 dummy_pause_job();
a14e9ff7 621
85d843c8 622 return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
a14e9ff7
MC
623}
624
e38c2e85 625/* Cipher helper functions */
98ee7543 626
e38c2e85 627static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
a0cbc2d2
MC
628 void *ptr, int aeadcapable,
629 const EVP_CIPHER *ciph)
98ee7543
MC
630{
631 int ret;
e38c2e85
MC
632 struct dasync_pipeline_ctx *pipe_ctx =
633 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
634
635 if (pipe_ctx == NULL)
636 return 0;
637
638 switch (type) {
a0cbc2d2
MC
639 case EVP_CTRL_COPY:
640 {
641 size_t sz = EVP_CIPHER_impl_ctx_size(ciph);
642 void *inner_cipher_data = OPENSSL_malloc(sz);
643
644 if (inner_cipher_data == NULL)
645 return -1;
646 memcpy(inner_cipher_data, pipe_ctx->inner_cipher_data, sz);
647 pipe_ctx->inner_cipher_data = inner_cipher_data;
648 }
649 break;
650
2f2c9caa
MC
651 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
652 pipe_ctx->numpipes = arg;
653 pipe_ctx->outbufs = (unsigned char **)ptr;
654 break;
655
656 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
657 pipe_ctx->numpipes = arg;
658 pipe_ctx->inbufs = (unsigned char **)ptr;
659 break;
660
661 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
662 pipe_ctx->numpipes = arg;
663 pipe_ctx->lens = (size_t *)ptr;
664 break;
665
666 case EVP_CTRL_AEAD_SET_MAC_KEY:
e38c2e85
MC
667 if (!aeadcapable)
668 return -1;
2f2c9caa
MC
669 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
670 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
671 (ctx, type, arg, ptr);
672 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
673 return ret;
674
675 case EVP_CTRL_AEAD_TLS1_AAD:
676 {
677 unsigned char *p = ptr;
678 unsigned int len;
679
e38c2e85 680 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
2f2c9caa
MC
681 return -1;
682
683 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
684 return -1;
685
686 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
687 EVP_AEAD_TLS1_AAD_LEN);
688 pipe_ctx->aadctr++;
689
690 len = p[arg - 2] << 8 | p[arg - 1];
691
ed576acd 692 if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
2f2c9caa 693 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
a68d3505
RS
694 if (len < AES_BLOCK_SIZE)
695 return 0;
2f2c9caa
MC
696 len -= AES_BLOCK_SIZE;
697 }
698
699 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
700 & -AES_BLOCK_SIZE) - len;
701 } else {
702 return SHA_DIGEST_LENGTH;
703 }
704 }
705
2f2c9caa
MC
706 default:
707 return 0;
708 }
709
710 return 1;
711}
712
e38c2e85
MC
713static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
714 const unsigned char *key,
715 const unsigned char *iv, int enc,
716 const EVP_CIPHER *cipher)
2f2c9caa
MC
717{
718 int ret;
e38c2e85
MC
719 struct dasync_pipeline_ctx *pipe_ctx =
720 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
721
722 if (pipe_ctx->inner_cipher_data == NULL
e38c2e85
MC
723 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
724 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
725 EVP_CIPHER_impl_ctx_size(cipher));
e077455e 726 if (pipe_ctx->inner_cipher_data == NULL)
2f2c9caa 727 return 0;
2f2c9caa
MC
728 }
729
730 pipe_ctx->numpipes = 0;
e38c2e85 731 pipe_ctx->aadctr = 0;
2f2c9caa
MC
732
733 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
e38c2e85 734 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
2f2c9caa
MC
735 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
736
737 return ret;
738}
739
e38c2e85
MC
740static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
741 const unsigned char *in, size_t inl,
742 const EVP_CIPHER *cipher)
2f2c9caa
MC
743{
744 int ret = 1;
745 unsigned int i, pipes;
e38c2e85
MC
746 struct dasync_pipeline_ctx *pipe_ctx =
747 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
748
749 pipes = pipe_ctx->numpipes;
750 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
751 if (pipes == 0) {
752 if (pipe_ctx->aadctr != 0) {
753 if (pipe_ctx->aadctr != 1)
754 return -1;
e38c2e85 755 EVP_CIPHER_meth_get_ctrl(cipher)
2f2c9caa
MC
756 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
757 EVP_AEAD_TLS1_AAD_LEN,
758 pipe_ctx->tlsaad[0]);
759 }
e38c2e85 760 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
2f2c9caa
MC
761 (ctx, out, in, inl);
762 } else {
763 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
764 return -1;
765 for (i = 0; i < pipes; i++) {
766 if (pipe_ctx->aadctr > 0) {
e38c2e85 767 EVP_CIPHER_meth_get_ctrl(cipher)
2f2c9caa
MC
768 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
769 EVP_AEAD_TLS1_AAD_LEN,
770 pipe_ctx->tlsaad[i]);
771 }
e38c2e85 772 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
2f2c9caa
MC
773 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
774 pipe_ctx->lens[i]);
775 }
776 pipe_ctx->numpipes = 0;
777 }
778 pipe_ctx->aadctr = 0;
779 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
780 return ret;
781}
782
e38c2e85
MC
783static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
784 const EVP_CIPHER *cipher)
2f2c9caa 785{
e38c2e85
MC
786 struct dasync_pipeline_ctx *pipe_ctx =
787 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
788
789 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
e38c2e85 790 EVP_CIPHER_impl_ctx_size(cipher));
2f2c9caa
MC
791
792 return 1;
793}
e38c2e85
MC
794
795/*
796 * AES128 CBC Implementation
797 */
798
799static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
800 void *ptr)
801{
a0cbc2d2 802 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_128_cbc());
e38c2e85
MC
803}
804
805static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
806 const unsigned char *iv, int enc)
807{
808 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
809}
810
811static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
812 const unsigned char *in, size_t inl)
813{
814 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
815}
816
817static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
818{
819 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
820}
821
bd363ef3
BE
822static int dasync_aes256_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
823 void *ptr)
824{
825 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_256_ctr());
826}
827
828static int dasync_aes256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
829 const unsigned char *iv, int enc)
830{
831 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_256_ctr());
832}
833
834static int dasync_aes256_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
835 const unsigned char *in, size_t inl)
836{
837 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_256_ctr());
838}
839
840static int dasync_aes256_ctr_cleanup(EVP_CIPHER_CTX *ctx)
841{
842 return dasync_cipher_cleanup_helper(ctx, EVP_aes_256_ctr());
843}
844
e38c2e85
MC
845
846/*
847 * AES128 CBC HMAC SHA1 Implementation
848 */
849
850static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
851 int arg, void *ptr)
852{
a0cbc2d2 853 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1, EVP_aes_128_cbc_hmac_sha1());
e38c2e85
MC
854}
855
856static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
857 const unsigned char *key,
858 const unsigned char *iv,
859 int enc)
860{
a4a0a1eb
DMSP
861 /*
862 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
863 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
864 */
e38c2e85
MC
865 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
866 EVP_aes_128_cbc_hmac_sha1());
867}
868
869static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
870 unsigned char *out,
871 const unsigned char *in,
872 size_t inl)
873{
874 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
875}
876
877static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
878{
a4a0a1eb
DMSP
879 /*
880 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
881 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
882 */
e38c2e85
MC
883 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
884}
c5f87134
P
885
886
887/*
888 * RSA implementation
889 */
890static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
891{
892 static int (*pinit)(EVP_PKEY_CTX *ctx);
893
894 if (pinit == NULL)
895 EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
896 return pinit(ctx);
897}
898
899static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
900{
901 static void (*pcleanup)(EVP_PKEY_CTX *ctx);
902
903 if (pcleanup == NULL)
904 EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
905 pcleanup(ctx);
906}
907
908static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
909{
910 static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
911
912 if (pparamgen_init == NULL)
913 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
59cd0bc1 914 return pparamgen_init != NULL ? pparamgen_init(ctx) : 1;
c5f87134
P
915}
916
917static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
918{
919 static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
920
921 if (pparamgen == NULL)
922 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
59cd0bc1 923 return pparamgen != NULL ? pparamgen(ctx, pkey) : 1;
c5f87134
P
924}
925
926static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
927{
928 static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
929
930 if (pkeygen_init == NULL)
931 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
59cd0bc1 932 return pkeygen_init != NULL ? pkeygen_init(ctx) : 1;
c5f87134
P
933}
934
935static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
936{
937 static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
938
939 if (pkeygen == NULL)
940 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
941 return pkeygen(ctx, pkey);
942}
943
944static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
945{
946 static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
947
948 if (pencrypt_init == NULL)
949 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
59cd0bc1 950 return pencrypt_init != NULL ? pencrypt_init(ctx) : 1;
c5f87134
P
951}
952
953static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
954 size_t *outlen, const unsigned char *in,
955 size_t inlen)
956{
957 static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
958 size_t *outlen, const unsigned char *in,
959 size_t inlen);
960
961 if (pencryptfn == NULL)
962 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
963 return pencryptfn(ctx, out, outlen, in, inlen);
964}
965
966static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
967{
968 static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
969
970 if (pdecrypt_init == NULL)
971 EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
59cd0bc1 972 return pdecrypt_init != NULL ? pdecrypt_init(ctx) : 1;
c5f87134
P
973}
974
975static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
976 size_t *outlen, const unsigned char *in,
977 size_t inlen)
978{
979 static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
980 size_t *outlen, const unsigned char *in,
981 size_t inlen);
982
983 if (pdecrypt == NULL)
c91f0ca9 984 EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, NULL, &pdecrypt);
c5f87134
P
985 return pdecrypt(ctx, out, outlen, in, inlen);
986}
987
988static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
989{
990 static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
991
992 if (pctrl == NULL)
993 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
994 return pctrl(ctx, type, p1, p2);
995}
996
997static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
998 const char *value)
999{
1000 static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
1001 const char *value);
1002
1003 if (pctrl_str == NULL)
1004 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
1005 return pctrl_str(ctx, type, value);
1006}