1 From 78e7b1cc7119622645bc5a8542c55b6c95dc7868 Mon Sep 17 00:00:00 2001
2 From: Eneas U de Queiroz <cote2004-github@yahoo.com>
3 Date: Tue, 6 Nov 2018 22:54:07 -0200
4 Subject: eng_devcrypto: add command to dump driver info
6 This is useful to determine the kernel driver running each algorithm.
8 Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
10 Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
11 Reviewed-by: Richard Levitte <levitte@openssl.org>
12 (Merged from https://github.com/openssl/openssl/pull/7585)
14 diff --git a/crypto/engine/eng_devcrypto.c b/crypto/engine/eng_devcrypto.c
15 index 5ec38ca8f3..64dc6b891d 100644
16 --- a/crypto/engine/eng_devcrypto.c
17 +++ b/crypto/engine/eng_devcrypto.c
18 @@ -50,16 +50,20 @@ static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
20 struct driver_info_st {
21 enum devcrypto_status_t {
22 - DEVCRYPTO_STATUS_UNUSABLE = -1, /* session open failed */
23 - DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
24 - DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
25 + DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
26 + DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
27 + DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
28 + DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
29 + DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
32 enum devcrypto_accelerated_t {
33 - DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
34 - DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
35 - DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
36 + DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
37 + DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
38 + DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
44 static int clean_devcrypto_session(struct session_op *sess) {
45 @@ -415,7 +419,7 @@ static void prepare_cipher_methods(void)
46 sess.cipher = cipher_data[i].devcryptoid;
47 sess.keylen = cipher_data[i].keylen;
48 if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
49 - cipher_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
50 + cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
54 @@ -443,19 +447,24 @@ static void prepare_cipher_methods(void)
56 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
57 sizeof(struct cipher_ctx))) {
58 - cipher_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
59 + cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
60 EVP_CIPHER_meth_free(known_cipher_methods[i]);
61 known_cipher_methods[i] = NULL;
63 cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
66 - if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0)
67 + if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
68 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
69 - else if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
70 - cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
72 - cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
74 + cipher_driver_info[i].driver_name =
75 + OPENSSL_strndup(siop.cipher_info.cra_driver_name,
76 + CRYPTODEV_MAX_ALG_NAME);
77 + if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
78 + cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
80 + cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
82 #endif /* CIOCGSESSINFO */
84 ioctl(cfd, CIOCFSESSION, &sess.ses);
85 @@ -505,8 +514,11 @@ static void destroy_all_cipher_methods(void)
89 - for (i = 0; i < OSSL_NELEM(cipher_data); i++)
90 + for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
91 destroy_cipher_method(cipher_data[i].nid);
92 + OPENSSL_free(cipher_driver_info[i].driver_name);
93 + cipher_driver_info[i].driver_name = NULL;
97 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
98 @@ -550,6 +562,40 @@ static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
102 +static void dump_cipher_info(void)
107 + fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
109 +#ifndef CIOCGSESSINFO
110 + fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
112 + for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
113 + name = OBJ_nid2sn(cipher_data[i].nid);
114 + fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
115 + name ? name : "unknown", cipher_data[i].nid,
116 + cipher_data[i].devcryptoid);
117 + if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
118 + fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
121 + fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
122 + cipher_driver_info[i].driver_name : "unknown");
123 + if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
124 + fprintf(stderr, "(hw accelerated)");
125 + else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
126 + fprintf(stderr, "(software)");
128 + fprintf(stderr, "(acceleration status unknown)");
129 + if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
130 + fprintf (stderr, ". Cipher setup failed");
131 + fprintf(stderr, "\n");
133 + fprintf(stderr, "\n");
137 * We only support digests if the cryptodev implementation supports multiple
138 * data updates and session copying. Otherwise, we would be forced to maintain
139 @@ -812,31 +858,36 @@ static void prepare_digest_methods(void)
140 sess1.mac = digest_data[i].devcryptoid;
142 if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
143 - digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
144 + digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
149 /* gather hardware acceleration info from the driver */
150 siop.ses = sess1.ses;
151 - if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0)
152 + if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
153 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
154 - else if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
155 - digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
157 - digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
159 + digest_driver_info[i].driver_name =
160 + OPENSSL_strndup(siop.hash_info.cra_driver_name,
161 + CRYPTODEV_MAX_ALG_NAME);
162 + if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
163 + digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
165 + digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
169 /* digest must be capable of hash state copy */
170 sess2.mac = sess1.mac;
171 if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
172 - digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
173 + digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
176 cphash.src_ses = sess1.ses;
177 cphash.dst_ses = sess2.ses;
178 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
179 - digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
180 + digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
183 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
184 @@ -852,7 +903,7 @@ static void prepare_digest_methods(void)
185 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
186 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
187 sizeof(struct digest_ctx))) {
188 - digest_driver_info[i].status = DEVCRYPTO_STATUS_UNUSABLE;
189 + digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
190 EVP_MD_meth_free(known_digest_methods[i]);
191 known_digest_methods[i] = NULL;
193 @@ -894,8 +945,11 @@ static void destroy_all_digest_methods(void)
197 - for (i = 0; i < OSSL_NELEM(digest_data); i++)
198 + for (i = 0; i < OSSL_NELEM(digest_data); i++) {
199 destroy_digest_method(digest_data[i].nid);
200 + OPENSSL_free(digest_driver_info[i].driver_name);
201 + digest_driver_info[i].driver_name = NULL;
205 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
206 @@ -939,6 +993,43 @@ static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
210 +static void dump_digest_info(void)
215 + fprintf (stderr, "Information about digests supported by the /dev/crypto"
217 +#ifndef CIOCGSESSINFO
218 + fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
221 + for (i = 0; i < OSSL_NELEM(digest_data); i++) {
222 + name = OBJ_nid2sn(digest_data[i].nid);
223 + fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
224 + name ? name : "unknown", digest_data[i].nid,
225 + digest_data[i].devcryptoid,
226 + digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
227 + if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
228 + fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
231 + if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
232 + fprintf(stderr, " (hw accelerated)");
233 + else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
234 + fprintf(stderr, " (software)");
236 + fprintf(stderr, " (acceleration status unknown)");
237 + if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
238 + fprintf (stderr, ". Cipher setup failed\n");
239 + else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
240 + fprintf(stderr, ", CIOCCPHASH failed\n");
242 + fprintf(stderr, ", CIOCCPHASH capable\n");
244 + fprintf(stderr, "\n");
249 /******************************************************************************
250 @@ -983,6 +1074,11 @@ static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
251 ENGINE_CMD_FLAG_STRING},
254 + {DEVCRYPTO_CMD_DUMP_INFO,
256 + "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
257 + ENGINE_CMD_FLAG_NO_INPUT},
262 @@ -1051,6 +1147,13 @@ static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
264 #endif /* IMPLEMENT_DIGEST */
266 + case DEVCRYPTO_CMD_DUMP_INFO:
267 + dump_cipher_info();
268 +#ifdef IMPLEMENT_DIGEST
269 + dump_digest_info();