1 From f3cef70b34afde3afd13ce3636232d41533b0162 Mon Sep 17 00:00:00 2001
2 From: Eneas U de Queiroz <cote2004-github@yahoo.com>
3 Date: Tue, 6 Nov 2018 10:57:03 -0200
4 Subject: e_devcrypto: make the /dev/crypto engine dynamic
6 Engine has been moved from crypto/engine/eng_devcrypto.c to
9 Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
11 diff --git a/crypto/engine/build.info b/crypto/engine/build.info
12 index e00802a3fd..47fe948966 100644
13 --- a/crypto/engine/build.info
14 +++ b/crypto/engine/build.info
15 @@ -6,6 +6,3 @@ SOURCE[../../libcrypto]=\
16 tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c tb_eckey.c \
17 eng_openssl.c eng_cnf.c eng_dyn.c \
19 -IF[{- !$disabled{devcryptoeng} -}]
20 - SOURCE[../../libcrypto]=eng_devcrypto.c
22 diff --git a/crypto/init.c b/crypto/init.c
23 index 9fc0e8ef68..b387559920 100644
26 @@ -329,18 +329,6 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
27 engine_load_openssl_int();
30 -# ifndef OPENSSL_NO_DEVCRYPTOENG
31 -static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
32 -DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
34 -# ifdef OPENSSL_INIT_DEBUG
35 - fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
36 - "engine_load_devcrypto_int()\n");
38 - engine_load_devcrypto_int();
43 # ifndef OPENSSL_NO_RDRAND
44 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
45 @@ -365,6 +353,18 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
48 # ifndef OPENSSL_NO_STATIC_ENGINE
49 +# ifndef OPENSSL_NO_DEVCRYPTOENG
50 +static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
51 +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
53 +# ifdef OPENSSL_INIT_DEBUG
54 + fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
55 + "engine_load_devcrypto_int()\n");
57 + engine_load_devcrypto_int();
61 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
62 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
63 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
64 @@ -713,11 +713,6 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
65 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
66 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
68 -# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
69 - if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
70 - && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
73 # ifndef OPENSSL_NO_RDRAND
74 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
75 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
76 @@ -727,6 +722,11 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
77 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
79 # ifndef OPENSSL_NO_STATIC_ENGINE
80 +# ifndef OPENSSL_NO_DEVCRYPTOENG
81 + if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
82 + && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
85 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
86 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
87 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
88 diff --git a/engines/build.info b/engines/build.info
89 index 1db771971c..33a25d7004 100644
90 --- a/engines/build.info
91 +++ b/engines/build.info
92 @@ -11,6 +11,9 @@ IF[{- !$disabled{"engine"} -}]
93 IF[{- !$disabled{afalgeng} -}]
94 SOURCE[../libcrypto]=e_afalg.c
96 + IF[{- !$disabled{"devcryptoeng"} -}]
97 + SOURCE[../libcrypto]=e_devcrypto.c
100 IF[{- !$disabled{hw} && !$disabled{'hw-padlock'} -}]
102 @@ -30,6 +33,12 @@ IF[{- !$disabled{"engine"} -}]
103 DEPEND[afalg]=../libcrypto
104 INCLUDE[afalg]= ../include
106 + IF[{- !$disabled{"devcryptoeng"} -}]
108 + SOURCE[devcrypto]=e_devcrypto.c
109 + DEPEND[devcrypto]=../libcrypto
110 + INCLUDE[devcrypto]=../include
113 ENGINES_NO_INST=ossltest dasync
114 SOURCE[dasync]=e_dasync.c
115 diff --git a/crypto/engine/eng_devcrypto.c b/engines/e_devcrypto.c
117 rename from crypto/engine/eng_devcrypto.c
118 rename to engines/e_devcrypto.c
119 index 64dc6b891d..fb5c6e1636 100644
120 --- a/crypto/engine/eng_devcrypto.c
121 +++ b/engines/e_devcrypto.c
123 * https://www.openssl.org/source/license.html
127 +#include "../e_os.h"
129 #include <sys/types.h>
130 #include <sys/stat.h>
132 #include <openssl/objects.h>
133 #include <crypto/cryptodev.h>
135 -#include "internal/engine.h"
137 /* #define ENGINE_DEVCRYPTO_DEBUG */
139 #if CRYPTO_ALGORITHM_MIN < CRYPTO_ALGORITHM_MAX
140 # define CHECK_BSD_STYLE_MACROS
143 +#define engine_devcrypto_id "devcrypto"
146 * ONE global file descriptor for all sessions. This allows operations
147 * such as digest session data copying (see digest_copy()), but is also
148 * saner... why re-open /dev/crypto for every session?
151 +static int cfd = -1;
152 #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
153 #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
154 #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
156 -#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
157 -static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
158 +#define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
159 +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
162 * cipher/digest status & acceleration definitions
163 @@ -66,6 +66,10 @@ struct driver_info_st {
167 +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
168 +void engine_load_devcrypto_int(void);
171 static int clean_devcrypto_session(struct session_op *sess) {
172 if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
173 SYSerr(SYS_F_IOCTL, errno);
174 @@ -341,6 +345,7 @@ static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
175 struct cipher_ctx *to_cipher_ctx;
180 if (cipher_ctx == NULL)
182 @@ -702,7 +707,6 @@ static int digest_init(EVP_MD_CTX *ctx)
183 SYSerr(SYS_F_IOCTL, errno);
190 @@ -1058,7 +1062,7 @@ static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
191 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
192 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
193 "=use if acceleration can't be determined) [default="
194 - OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
195 + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
196 ENGINE_CMD_FLAG_NUMERIC},
199 @@ -1166,55 +1170,70 @@ static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
203 -static int devcrypto_unload(ENGINE *e)
205 - destroy_all_cipher_methods();
206 -#ifdef IMPLEMENT_DIGEST
207 - destroy_all_digest_methods();
215 - * This engine is always built into libcrypto, so it doesn't offer any
216 - * ability to be dynamically loadable.
217 + * Opens /dev/crypto
219 -void engine_load_devcrypto_int()
220 +static int open_devcrypto(void)
226 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
227 #ifndef ENGINE_DEVCRYPTO_DEBUG
230 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
235 - if ((e = ENGINE_new()) == NULL
236 - || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
239 - * We know that devcrypto_unload() won't be called when one of the
240 - * above two calls have failed, so we close cfd explicitly here to
241 - * avoid leaking resources.
248 +static int close_devcrypto(void)
257 + fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
263 - prepare_cipher_methods();
264 +static int devcrypto_unload(ENGINE *e)
266 + destroy_all_cipher_methods();
267 #ifdef IMPLEMENT_DIGEST
268 - prepare_digest_methods();
269 + destroy_all_digest_methods();
272 - if (!ENGINE_set_id(e, "devcrypto")
278 +static int bind_devcrypto(ENGINE *e) {
280 + if (!ENGINE_set_id(e, engine_devcrypto_id)
281 || !ENGINE_set_name(e, "/dev/crypto engine")
282 + || !ENGINE_set_destroy_function(e, devcrypto_unload)
283 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
284 - || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
285 + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
288 + prepare_cipher_methods();
289 +#ifdef IMPLEMENT_DIGEST
290 + prepare_digest_methods();
293 + return (ENGINE_set_ciphers(e, devcrypto_ciphers)
294 +#ifdef IMPLEMENT_DIGEST
295 + && ENGINE_set_digests(e, devcrypto_digests)
298 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
299 * implementations, it seems to only exist in FreeBSD, and regarding the
300 @@ -1237,23 +1256,36 @@ void engine_load_devcrypto_int()
303 # ifndef OPENSSL_NO_RSA
304 - || !ENGINE_set_RSA(e, devcrypto_rsa)
305 + && ENGINE_set_RSA(e, devcrypto_rsa)
307 # ifndef OPENSSL_NO_DSA
308 - || !ENGINE_set_DSA(e, devcrypto_dsa)
309 + && ENGINE_set_DSA(e, devcrypto_dsa)
311 # ifndef OPENSSL_NO_DH
312 - || !ENGINE_set_DH(e, devcrypto_dh)
313 + && ENGINE_set_DH(e, devcrypto_dh)
315 # ifndef OPENSSL_NO_EC
316 - || !ENGINE_set_EC(e, devcrypto_ec)
317 + && ENGINE_set_EC(e, devcrypto_ec)
320 - || !ENGINE_set_ciphers(e, devcrypto_ciphers)
321 -#ifdef IMPLEMENT_DIGEST
322 - || !ENGINE_set_digests(e, devcrypto_digests)
328 +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
330 + * In case this engine is built into libcrypto, then it doesn't offer any
331 + * ability to be dynamically loadable.
333 +void engine_load_devcrypto_int(void)
337 + if (!open_devcrypto())
340 + if ((e = ENGINE_new()) == NULL
341 + || !bind_devcrypto(e)) {
346 @@ -1262,3 +1294,22 @@ void engine_load_devcrypto_int()
347 ENGINE_free(e); /* Loose our local reference */
353 +static int bind_helper(ENGINE *e, const char *id)
355 + if ((id && (strcmp(id, engine_devcrypto_id) != 0))
356 + || !open_devcrypto())
358 + if (!bind_devcrypto(e)) {
365 +IMPLEMENT_DYNAMIC_CHECK_FN()
366 +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)