1 From 1c2fabcdb34e436286b4a8760cfbfbff11ea551a Mon Sep 17 00:00:00 2001
2 From: Eneas U de Queiroz <cote2004-github@yahoo.com>
3 Date: Sat, 3 Nov 2018 15:41:10 -0300
4 Subject: eng_devcrypto: add configuration options
6 USE_SOFTDRIVERS: whether to use software (not accelerated) drivers
7 CIPHERS: list of ciphers to enable
8 DIGESTS: list of digests to enable
10 Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
12 Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
13 Reviewed-by: Richard Levitte <levitte@openssl.org>
14 (Merged from https://github.com/openssl/openssl/pull/7585)
16 diff --git a/crypto/engine/eng_devcrypto.c b/crypto/engine/eng_devcrypto.c
17 index a2c9a966f7..5ec38ca8f3 100644
18 --- a/crypto/engine/eng_devcrypto.c
19 +++ b/crypto/engine/eng_devcrypto.c
24 +#include <openssl/conf.h>
25 #include <openssl/evp.h>
26 #include <openssl/err.h>
27 #include <openssl/engine.h>
29 * saner... why re-open /dev/crypto for every session?
32 +#define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
33 +#define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
34 +#define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
36 +#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
37 +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
40 + * cipher/digest status & acceleration definitions
41 + * Make sure the defaults are set to 0
43 +struct driver_info_st {
44 + enum devcrypto_status_t {
45 + DEVCRYPTO_STATUS_UNUSABLE = -1, /* session open failed */
46 + DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
47 + DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
50 + enum devcrypto_accelerated_t {
51 + DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
52 + DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
53 + DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
57 static int clean_devcrypto_session(struct session_op *sess) {
58 if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
59 @@ -119,13 +144,22 @@ static const struct cipher_data_st {
63 -static size_t get_cipher_data_index(int nid)
64 +static size_t find_cipher_data_index(int nid)
68 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
69 if (nid == cipher_data[i].nid)
74 +static size_t get_cipher_data_index(int nid)
76 + size_t i = find_cipher_data_index(nid);
78 + if (i != (size_t)-1)
82 * Code further down must make sure that only NIDs in the table above
83 @@ -333,19 +367,40 @@ static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
87 - * Keep a table of known nids and associated methods.
88 + * Keep tables of known nids, associated methods, selected ciphers, and driver
90 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
91 - * cipher_data[] above, which known_cipher_methods[] is.
92 + * cipher_data[] above, which the other tables are.
94 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
95 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
96 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
97 +static int selected_ciphers[OSSL_NELEM(cipher_data)];
98 +static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
101 +static int devcrypto_test_cipher(size_t cipher_data_index)
103 + return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
104 + && selected_ciphers[cipher_data_index] == 1
105 + && (cipher_driver_info[cipher_data_index].accelerated
106 + == DEVCRYPTO_ACCELERATED
107 + || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
108 + || (cipher_driver_info[cipher_data_index].accelerated
109 + != DEVCRYPTO_NOT_ACCELERATED
110 + && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
113 static void prepare_cipher_methods(void)
116 struct session_op sess;
117 unsigned long cipher_mode;
118 +#ifdef CIOCGSESSINFO
119 + struct session_info_op siop;
122 + memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
124 memset(&sess, 0, sizeof(sess));
125 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
126 @@ -353,15 +408,16 @@ static void prepare_cipher_methods(void)
127 for (i = 0, known_cipher_nids_amount = 0;
128 i < OSSL_NELEM(cipher_data); i++) {
130 + selected_ciphers[i] = 1;
132 - * Check that the algo is really availably by trying to open and close
134 + * Check that the cipher is usable
136 sess.cipher = cipher_data[i].devcryptoid;
137 sess.keylen = cipher_data[i].keylen;
138 - if (ioctl(cfd, CIOCGSESSION, &sess) < 0
139 - || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
140 + if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
141 + cipher_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
145 cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
147 @@ -387,15 +443,41 @@ static void prepare_cipher_methods(void)
149 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
150 sizeof(struct cipher_ctx))) {
151 + cipher_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
152 EVP_CIPHER_meth_free(known_cipher_methods[i]);
153 known_cipher_methods[i] = NULL;
155 + cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
156 +#ifdef CIOCGSESSINFO
157 + siop.ses = sess.ses;
158 + if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0)
159 + cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
160 + else if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
161 + cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
163 + cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
164 +#endif /* CIOCGSESSINFO */
166 + ioctl(cfd, CIOCFSESSION, &sess.ses);
167 + if (devcrypto_test_cipher(i)) {
168 known_cipher_nids[known_cipher_nids_amount++] =
174 +static void rebuild_known_cipher_nids(ENGINE *e)
178 + for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
179 + if (devcrypto_test_cipher(i))
180 + known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
182 + ENGINE_unregister_ciphers(e);
183 + ENGINE_register_ciphers(e);
186 static const EVP_CIPHER *get_cipher_method(int nid)
188 size_t i = get_cipher_data_index(nid);
189 @@ -438,6 +520,36 @@ static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
190 return *cipher != NULL;
193 +static void devcrypto_select_all_ciphers(int *cipher_list)
197 + for (i = 0; i < OSSL_NELEM(cipher_data); i++)
198 + cipher_list[i] = 1;
201 +static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
203 + int *cipher_list = (int *)usr;
205 + const EVP_CIPHER *EVP;
210 + if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
212 + EVP = EVP_get_cipherbyname(name);
214 + fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
215 + else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
216 + cipher_list[i] = 1;
218 + fprintf(stderr, "devcrypto: cipher %s not available\n", name);
219 + OPENSSL_free(name);
224 * We only support digests if the cryptodev implementation supports multiple
225 * data updates and session copying. Otherwise, we would be forced to maintain
226 @@ -493,13 +605,22 @@ static const struct digest_data_st {
230 -static size_t get_digest_data_index(int nid)
231 +static size_t find_digest_data_index(int nid)
235 for (i = 0; i < OSSL_NELEM(digest_data); i++)
236 if (nid == digest_data[i].nid)
241 +static size_t get_digest_data_index(int nid)
243 + size_t i = find_digest_data_index(nid);
245 + if (i != (size_t)-1)
249 * Code further down must make sure that only NIDs in the table above
250 @@ -516,8 +637,8 @@ static const struct digest_data_st *get_digest_data(int nid)
254 - * Following are the four necessary functions to map OpenSSL functionality
256 + * Following are the five necessary functions to map OpenSSL functionality
257 + * with cryptodev: init, update, final, cleanup, and copy.
260 static int digest_init(EVP_MD_CTX *ctx)
261 @@ -630,52 +751,94 @@ static int digest_cleanup(EVP_MD_CTX *ctx)
262 return clean_devcrypto_session(&digest_ctx->sess);
265 -static int devcrypto_test_digest(size_t digest_data_index)
267 - struct session_op sess1, sess2;
268 - struct cphash_op cphash;
271 - memset(&sess1, 0, sizeof(sess1));
272 - memset(&sess2, 0, sizeof(sess2));
273 - sess1.mac = digest_data[digest_data_index].devcryptoid;
274 - if (ioctl(cfd, CIOCGSESSION, &sess1) < 0)
276 - /* Make sure the driver is capable of hash state copy */
277 - sess2.mac = sess1.mac;
278 - if (ioctl(cfd, CIOCGSESSION, &sess2) >= 0) {
279 - cphash.src_ses = sess1.ses;
280 - cphash.dst_ses = sess2.ses;
281 - if (ioctl(cfd, CIOCCPHASH, &cphash) >= 0)
283 - ioctl(cfd, CIOCFSESSION, &sess2.ses);
285 - ioctl(cfd, CIOCFSESSION, &sess1.ses);
290 - * Keep a table of known nids and associated methods.
291 + * Keep tables of known nids, associated methods, selected digests, and
293 * Note that known_digest_nids[] isn't necessarily indexed the same way as
294 - * digest_data[] above, which known_digest_methods[] is.
295 + * digest_data[] above, which the other tables are.
297 static int known_digest_nids[OSSL_NELEM(digest_data)];
298 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
299 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
300 +static int selected_digests[OSSL_NELEM(digest_data)];
301 +static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
303 +static int devcrypto_test_digest(size_t digest_data_index)
305 + return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
306 + && selected_digests[digest_data_index] == 1
307 + && (digest_driver_info[digest_data_index].accelerated
308 + == DEVCRYPTO_ACCELERATED
309 + || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
310 + || (digest_driver_info[digest_data_index].accelerated
311 + != DEVCRYPTO_NOT_ACCELERATED
312 + && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
315 +static void rebuild_known_digest_nids(ENGINE *e)
319 + for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
320 + if (devcrypto_test_digest(i))
321 + known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
323 + ENGINE_unregister_digests(e);
324 + ENGINE_register_digests(e);
327 static void prepare_digest_methods(void)
330 + struct session_op sess1, sess2;
331 +#ifdef CIOCGSESSINFO
332 + struct session_info_op siop;
334 + struct cphash_op cphash;
336 + memset(&digest_driver_info, 0, sizeof(digest_driver_info));
338 + memset(&sess1, 0, sizeof(sess1));
339 + memset(&sess2, 0, sizeof(sess2));
341 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
344 + selected_digests[i] = 1;
347 - * Check that the algo is usable
348 + * Check that the digest is usable
350 - if (!devcrypto_test_digest(i))
352 + sess1.mac = digest_data[i].devcryptoid;
354 + if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
355 + digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
359 +#ifdef CIOCGSESSINFO
360 + /* gather hardware acceleration info from the driver */
361 + siop.ses = sess1.ses;
362 + if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0)
363 + digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
364 + else if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
365 + digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
367 + digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
370 + /* digest must be capable of hash state copy */
371 + sess2.mac = sess1.mac;
372 + if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
373 + digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
376 + cphash.src_ses = sess1.ses;
377 + cphash.dst_ses = sess2.ses;
378 + if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
379 + digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
382 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
384 || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i],
385 @@ -689,11 +852,18 @@ static void prepare_digest_methods(void)
386 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
387 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
388 sizeof(struct digest_ctx))) {
389 + digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
390 EVP_MD_meth_free(known_digest_methods[i]);
391 known_digest_methods[i] = NULL;
393 - known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
396 + digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
398 + ioctl(cfd, CIOCFSESSION, &sess1.ses);
399 + if (sess2.ses != 0)
400 + ioctl(cfd, CIOCFSESSION, &sess2.ses);
401 + if (devcrypto_test_digest(i))
402 + known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
406 @@ -739,8 +909,154 @@ static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
407 return *digest != NULL;
410 +static void devcrypto_select_all_digests(int *digest_list)
414 + for (i = 0; i < OSSL_NELEM(digest_data); i++)
415 + digest_list[i] = 1;
418 +static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
420 + int *digest_list = (int *)usr;
427 + if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
429 + EVP = EVP_get_digestbyname(name);
431 + fprintf(stderr, "devcrypto: unknown digest %s\n", name);
432 + else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
433 + digest_list[i] = 1;
435 + fprintf(stderr, "devcrypto: digest %s not available\n", name);
436 + OPENSSL_free(name);
442 +/******************************************************************************
448 +#define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
449 +#define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
450 +#define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
451 +#define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
453 +/* Helper macros for CPP string composition */
454 +#ifndef OPENSSL_MSTR
455 +# define OPENSSL_MSTR_HELPER(x) #x
456 +# define OPENSSL_MSTR(x) OPENSSL_MSTR_HELPER(x)
459 +static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
460 +#ifdef CIOCGSESSINFO
461 + {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
463 + "specifies whether to use software (not accelerated) drivers ("
464 + OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
465 + OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
466 + OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
467 + "=use if acceleration can't be determined) [default="
468 + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
469 + ENGINE_CMD_FLAG_NUMERIC},
472 + {DEVCRYPTO_CMD_CIPHERS,
474 + "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
475 + ENGINE_CMD_FLAG_STRING},
477 +#ifdef IMPLEMENT_DIGEST
478 + {DEVCRYPTO_CMD_DIGESTS,
480 + "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
481 + ENGINE_CMD_FLAG_STRING},
487 +static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
491 +#ifdef CIOCGSESSINFO
492 + case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
494 + case DEVCRYPTO_REQUIRE_ACCELERATED:
495 + case DEVCRYPTO_USE_SOFTWARE:
496 + case DEVCRYPTO_REJECT_SOFTWARE:
499 + fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
502 + if (use_softdrivers == i)
504 + use_softdrivers = i;
505 +#ifdef IMPLEMENT_DIGEST
506 + rebuild_known_digest_nids(e);
508 + rebuild_known_cipher_nids(e);
510 +#endif /* CIOCGSESSINFO */
512 + case DEVCRYPTO_CMD_CIPHERS:
515 + if (strcasecmp((const char *)p, "ALL") == 0) {
516 + devcrypto_select_all_ciphers(selected_ciphers);
517 + } else if (strcasecmp((const char*)p, "NONE") == 0) {
518 + memset(selected_ciphers, 0, sizeof(selected_ciphers));
520 + new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
521 + if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
522 + OPENSSL_free(new_list);
525 + memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
526 + OPENSSL_free(new_list);
528 + rebuild_known_cipher_nids(e);
531 +#ifdef IMPLEMENT_DIGEST
532 + case DEVCRYPTO_CMD_DIGESTS:
535 + if (strcasecmp((const char *)p, "ALL") == 0) {
536 + devcrypto_select_all_digests(selected_digests);
537 + } else if (strcasecmp((const char*)p, "NONE") == 0) {
538 + memset(selected_digests, 0, sizeof(selected_digests));
540 + new_list=OPENSSL_zalloc(sizeof(selected_digests));
541 + if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
542 + OPENSSL_free(new_list);
545 + memcpy(selected_digests, new_list, sizeof(selected_digests));
546 + OPENSSL_free(new_list);
548 + rebuild_known_digest_nids(e);
550 +#endif /* IMPLEMENT_DIGEST */
558 /******************************************************************************
561 @@ -793,6 +1109,8 @@ void engine_load_devcrypto_int()
563 if (!ENGINE_set_id(e, "devcrypto")
564 || !ENGINE_set_name(e, "/dev/crypto engine")
565 + || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
566 + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
569 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD