1 From 0000000000000000000000000000000000000000 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 1b0d523bea..ee3e2eb075 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 0d420e50aa..3fcd81de7a 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 # define CHECK_BSD_STYLE_MACROS
135 +#define engine_devcrypto_id "devcrypto"
138 * ONE global file descriptor for all sessions. This allows operations
139 * such as digest session data copying (see digest_copy()), but is also
140 * saner... why re-open /dev/crypto for every session?
143 +static int cfd = -1;
144 #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
145 #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
146 #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
148 -#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
149 -static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
150 +#define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
151 +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
154 * cipher/digest status & acceleration definitions
155 @@ -341,6 +343,7 @@ static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
156 struct cipher_ctx *to_cipher_ctx;
161 if (cipher_ctx == NULL)
163 @@ -702,7 +705,6 @@ static int digest_init(EVP_MD_CTX *ctx)
164 SYSerr(SYS_F_IOCTL, errno);
171 @@ -1058,7 +1060,7 @@ static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
172 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
173 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
174 "=use if acceleration can't be determined) [default="
175 - OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
176 + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
177 ENGINE_CMD_FLAG_NUMERIC},
180 @@ -1166,55 +1168,70 @@ static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
184 -static int devcrypto_unload(ENGINE *e)
186 - destroy_all_cipher_methods();
187 -#ifdef IMPLEMENT_DIGEST
188 - destroy_all_digest_methods();
196 - * This engine is always built into libcrypto, so it doesn't offer any
197 - * ability to be dynamically loadable.
198 + * Opens /dev/crypto
200 -void engine_load_devcrypto_int()
201 +static int open_devcrypto(void)
207 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
208 #ifndef ENGINE_DEVCRYPTO_DEBUG
211 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
216 - if ((e = ENGINE_new()) == NULL
217 - || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
220 - * We know that devcrypto_unload() won't be called when one of the
221 - * above two calls have failed, so we close cfd explicitly here to
222 - * avoid leaking resources.
229 +static int close_devcrypto(void)
238 + fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
244 - prepare_cipher_methods();
245 +static int devcrypto_unload(ENGINE *e)
247 + destroy_all_cipher_methods();
248 #ifdef IMPLEMENT_DIGEST
249 - prepare_digest_methods();
250 + destroy_all_digest_methods();
253 - if (!ENGINE_set_id(e, "devcrypto")
259 +static int bind_devcrypto(ENGINE *e) {
261 + if (!ENGINE_set_id(e, engine_devcrypto_id)
262 || !ENGINE_set_name(e, "/dev/crypto engine")
263 + || !ENGINE_set_destroy_function(e, devcrypto_unload)
264 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
265 - || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
266 + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
269 + prepare_cipher_methods();
270 +#ifdef IMPLEMENT_DIGEST
271 + prepare_digest_methods();
274 + return (ENGINE_set_ciphers(e, devcrypto_ciphers)
275 +#ifdef IMPLEMENT_DIGEST
276 + && ENGINE_set_digests(e, devcrypto_digests)
279 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
280 * implementations, it seems to only exist in FreeBSD, and regarding the
281 @@ -1237,23 +1254,36 @@ void engine_load_devcrypto_int()
284 # ifndef OPENSSL_NO_RSA
285 - || !ENGINE_set_RSA(e, devcrypto_rsa)
286 + && ENGINE_set_RSA(e, devcrypto_rsa)
288 # ifndef OPENSSL_NO_DSA
289 - || !ENGINE_set_DSA(e, devcrypto_dsa)
290 + && ENGINE_set_DSA(e, devcrypto_dsa)
292 # ifndef OPENSSL_NO_DH
293 - || !ENGINE_set_DH(e, devcrypto_dh)
294 + && ENGINE_set_DH(e, devcrypto_dh)
296 # ifndef OPENSSL_NO_EC
297 - || !ENGINE_set_EC(e, devcrypto_ec)
298 + && ENGINE_set_EC(e, devcrypto_ec)
301 - || !ENGINE_set_ciphers(e, devcrypto_ciphers)
302 -#ifdef IMPLEMENT_DIGEST
303 - || !ENGINE_set_digests(e, devcrypto_digests)
309 +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
311 + * In case this engine is built into libcrypto, then it doesn't offer any
312 + * ability to be dynamically loadable.
314 +void engine_load_devcrypto_int(void)
318 + if (!open_devcrypto())
321 + if ((e = ENGINE_new()) == NULL
322 + || !bind_devcrypto(e)) {
327 @@ -1262,3 +1292,22 @@ void engine_load_devcrypto_int()
328 ENGINE_free(e); /* Loose our local reference */
334 +static int bind_helper(ENGINE *e, const char *id)
336 + if ((id && (strcmp(id, engine_devcrypto_id) != 0))
337 + || !open_devcrypto())
339 + if (!bind_devcrypto(e)) {
346 +IMPLEMENT_DYNAMIC_CHECK_FN()
347 +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)