Do not complain when /dev/crypto does not exist.
[openssl.git] / crypto / engine / eng_devcrypto.c
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
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
8  */
9
10 #include "e_os.h"
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include <unistd.h>
17 #include <assert.h>
18
19 #include <openssl/conf.h>
20 #include <openssl/evp.h>
21 #include <openssl/err.h>
22 #include <openssl/engine.h>
23 #include <openssl/objects.h>
24 #include <crypto/cryptodev.h>
25
26 #include "internal/engine.h"
27
28 /* #define ENGINE_DEVCRYPTO_DEBUG */
29
30 #ifdef CRYPTO_ALGORITHM_MIN
31 # define CHECK_BSD_STYLE_MACROS
32 #endif
33
34 /*
35  * ONE global file descriptor for all sessions.  This allows operations
36  * such as digest session data copying (see digest_copy()), but is also
37  * saner...  why re-open /dev/crypto for every session?
38  */
39 static int cfd;
40 #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
41 #define DEVCRYPTO_USE_SOFTWARE        1 /* allow software drivers */
42 #define DEVCRYPTO_REJECT_SOFTWARE     2 /* only disallow confirmed software drivers */
43
44 #define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
45 static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
46
47 /*
48  * cipher/digest status & acceleration definitions
49  * Make sure the defaults are set to 0
50  */
51 struct driver_info_st {
52     enum devcrypto_status_t {
53         DEVCRYPTO_STATUS_FAILURE         = -3, /* unusable for other reason */
54         DEVCRYPTO_STATUS_NO_CIOCCPHASH   = -2, /* hash state copy not supported */
55         DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
56         DEVCRYPTO_STATUS_UNKNOWN         =  0, /* not tested yet */
57         DEVCRYPTO_STATUS_USABLE          =  1  /* algo can be used */
58     } status;
59
60     enum devcrypto_accelerated_t {
61         DEVCRYPTO_NOT_ACCELERATED        = -1, /* software implemented */
62         DEVCRYPTO_ACCELERATION_UNKNOWN   =  0, /* acceleration support unkown */
63         DEVCRYPTO_ACCELERATED            =  1  /* hardware accelerated */
64     } accelerated;
65
66     char *driver_name;
67 };
68
69 /******************************************************************************
70  *
71  * Ciphers
72  *
73  * Because they all do the same basic operation, we have only one set of
74  * method functions for them all to share, and a mapping table between
75  * NIDs and cryptodev IDs, with all the necessary size data.
76  *
77  *****/
78
79 struct cipher_ctx {
80     struct session_op sess;
81     int op;                      /* COP_ENCRYPT or COP_DECRYPT */
82     unsigned long mode;          /* EVP_CIPH_*_MODE */
83
84     /* to handle ctr mode being a stream cipher */
85     unsigned char partial[EVP_MAX_BLOCK_LENGTH];
86     unsigned int blocksize, num;
87 };
88
89 static const struct cipher_data_st {
90     int nid;
91     int blocksize;
92     int keylen;
93     int ivlen;
94     int flags;
95     int devcryptoid;
96 } cipher_data[] = {
97 #ifndef OPENSSL_NO_DES
98     { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
99     { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
100 #endif
101 #ifndef OPENSSL_NO_BF
102     { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
103 #endif
104 #ifndef OPENSSL_NO_CAST
105     { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
106 #endif
107     { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
108     { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
109     { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
110 #ifndef OPENSSL_NO_RC4
111     { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
112 #endif
113 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
114     { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
115     { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
116     { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
117 #endif
118 #if 0                            /* Not yet supported */
119     { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
120     { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
121 #endif
122 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
123     { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
124     { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
125     { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
126 #endif
127 #if 0                            /* Not yet supported */
128     { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
129     { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
130     { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
131 #endif
132 #ifndef OPENSSL_NO_CAMELLIA
133     { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
134       CRYPTO_CAMELLIA_CBC },
135     { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
136       CRYPTO_CAMELLIA_CBC },
137     { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
138       CRYPTO_CAMELLIA_CBC },
139 #endif
140 };
141
142 static size_t find_cipher_data_index(int nid)
143 {
144     size_t i;
145
146     for (i = 0; i < OSSL_NELEM(cipher_data); i++)
147         if (nid == cipher_data[i].nid)
148             return i;
149     return (size_t)-1;
150 }
151
152 static size_t get_cipher_data_index(int nid)
153 {
154     size_t i = find_cipher_data_index(nid);
155
156     if (i != (size_t)-1)
157         return i;
158
159     /*
160      * Code further down must make sure that only NIDs in the table above
161      * are used.  If any other NID reaches this function, there's a grave
162      * coding error further down.
163      */
164     assert("Code that never should be reached" == NULL);
165     return -1;
166 }
167
168 static const struct cipher_data_st *get_cipher_data(int nid)
169 {
170     return &cipher_data[get_cipher_data_index(nid)];
171 }
172
173 /*
174  * Following are the three necessary functions to map OpenSSL functionality
175  * with cryptodev.
176  */
177
178 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
179                        const unsigned char *iv, int enc)
180 {
181     struct cipher_ctx *cipher_ctx =
182         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
183     const struct cipher_data_st *cipher_d =
184         get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
185
186     memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
187     cipher_ctx->sess.cipher = cipher_d->devcryptoid;
188     cipher_ctx->sess.keylen = cipher_d->keylen;
189     cipher_ctx->sess.key = (void *)key;
190     cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
191     cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
192     cipher_ctx->blocksize = cipher_d->blocksize;
193     if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
194         SYSerr(SYS_F_IOCTL, errno);
195         return 0;
196     }
197
198     return 1;
199 }
200
201 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
202                             const unsigned char *in, size_t inl)
203 {
204     struct cipher_ctx *cipher_ctx =
205         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
206     struct crypt_op cryp;
207     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
208 #if !defined(COP_FLAG_WRITE_IV)
209     unsigned char saved_iv[EVP_MAX_IV_LENGTH];
210     const unsigned char *ivptr;
211     size_t nblocks, ivlen;
212 #endif
213
214     memset(&cryp, 0, sizeof(cryp));
215     cryp.ses = cipher_ctx->sess.ses;
216     cryp.len = inl;
217     cryp.src = (void *)in;
218     cryp.dst = (void *)out;
219     cryp.iv = (void *)iv;
220     cryp.op = cipher_ctx->op;
221 #if !defined(COP_FLAG_WRITE_IV)
222     cryp.flags = 0;
223
224     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
225     if (ivlen > 0)
226         switch (cipher_ctx->mode) {
227         case EVP_CIPH_CBC_MODE:
228             assert(inl >= ivlen);
229             if (!EVP_CIPHER_CTX_encrypting(ctx)) {
230                 ivptr = in + inl - ivlen;
231                 memcpy(saved_iv, ivptr, ivlen);
232             }
233             break;
234
235         case EVP_CIPH_CTR_MODE:
236             break;
237
238         default: /* should not happen */
239             return 0;
240         }
241 #else
242     cryp.flags = COP_FLAG_WRITE_IV;
243 #endif
244
245     if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
246         SYSerr(SYS_F_IOCTL, errno);
247         return 0;
248     }
249
250 #if !defined(COP_FLAG_WRITE_IV)
251     if (ivlen > 0)
252         switch (cipher_ctx->mode) {
253         case EVP_CIPH_CBC_MODE:
254             assert(inl >= ivlen);
255             if (EVP_CIPHER_CTX_encrypting(ctx))
256                 ivptr = out + inl - ivlen;
257             else
258                 ivptr = saved_iv;
259
260             memcpy(iv, ivptr, ivlen);
261             break;
262
263         case EVP_CIPH_CTR_MODE:
264             nblocks = (inl + cipher_ctx->blocksize - 1)
265                       / cipher_ctx->blocksize;
266             do {
267                 ivlen--;
268                 nblocks += iv[ivlen];
269                 iv[ivlen] = (uint8_t) nblocks;
270                 nblocks >>= 8;
271             } while (ivlen);
272             break;
273
274         default: /* should not happen */
275             return 0;
276         }
277 #endif
278
279     return 1;
280 }
281
282 static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
283                          const unsigned char *in, size_t inl)
284 {
285     struct cipher_ctx *cipher_ctx =
286         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
287     size_t nblocks, len;
288
289     /* initial partial block */
290     while (cipher_ctx->num && inl) {
291         (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
292         --inl;
293         cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
294     }
295
296     /* full blocks */
297     if (inl > (unsigned int) cipher_ctx->blocksize) {
298         nblocks = inl/cipher_ctx->blocksize;
299         len = nblocks * cipher_ctx->blocksize;
300         if (cipher_do_cipher(ctx, out, in, len) < 1)
301             return 0;
302         inl -= len;
303         out += len;
304         in += len;
305     }
306
307     /* final partial block */
308     if (inl) {
309         memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
310         if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
311             cipher_ctx->blocksize) < 1)
312             return 0;
313         while (inl--) {
314             out[cipher_ctx->num] = in[cipher_ctx->num]
315                                    ^ cipher_ctx->partial[cipher_ctx->num];
316             cipher_ctx->num++;
317         }
318     }
319
320     return 1;
321 }
322
323 static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
324 {
325     EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
326     struct cipher_ctx *cipher_ctx;
327
328     if (type == EVP_CTRL_COPY) {
329         /* when copying the context, a new session needs to be initialized */
330         cipher_ctx = (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
331         return (cipher_ctx == NULL)
332             || cipher_init(to_ctx, cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
333                            (cipher_ctx->op == COP_ENCRYPT));
334     }
335
336     return -1;
337 }
338
339 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
340 {
341     struct cipher_ctx *cipher_ctx =
342         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
343
344     if (ioctl(cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
345         SYSerr(SYS_F_IOCTL, errno);
346         return 0;
347     }
348
349     return 1;
350 }
351
352 /*
353  * Keep tables of known nids, associated methods, selected ciphers, and driver
354  * info.
355  * Note that known_cipher_nids[] isn't necessarily indexed the same way as
356  * cipher_data[] above, which the other tables are.
357  */
358 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
359 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
360 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
361 static int selected_ciphers[OSSL_NELEM(cipher_data)];
362 static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
363
364
365 static int devcrypto_test_cipher(size_t cipher_data_index)
366 {
367     return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
368             && selected_ciphers[cipher_data_index] == 1
369             && (cipher_driver_info[cipher_data_index].accelerated
370                     == DEVCRYPTO_ACCELERATED
371                 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
372                 || (cipher_driver_info[cipher_data_index].accelerated
373                         != DEVCRYPTO_NOT_ACCELERATED
374                     && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
375 }
376
377 static void prepare_cipher_methods(void)
378 {
379     size_t i;
380     struct session_op sess;
381     unsigned long cipher_mode;
382 #ifdef CIOCGSESSINFO
383     struct session_info_op siop;
384 #endif
385
386     memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
387
388     memset(&sess, 0, sizeof(sess));
389     sess.key = (void *)"01234567890123456789012345678901234567890123456789";
390
391     for (i = 0, known_cipher_nids_amount = 0;
392          i < OSSL_NELEM(cipher_data); i++) {
393
394         selected_ciphers[i] = 1;
395         /*
396          * Check that the cipher is usable
397          */
398         sess.cipher = cipher_data[i].devcryptoid;
399         sess.keylen = cipher_data[i].keylen;
400         if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
401             cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
402             continue;
403         }
404
405         cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
406
407         if ((known_cipher_methods[i] =
408                  EVP_CIPHER_meth_new(cipher_data[i].nid,
409                                      cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
410                                                     cipher_data[i].blocksize,
411                                      cipher_data[i].keylen)) == NULL
412             || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
413                                               cipher_data[i].ivlen)
414             || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
415                                           cipher_data[i].flags
416                                           | EVP_CIPH_CUSTOM_COPY
417                                           | EVP_CIPH_FLAG_DEFAULT_ASN1)
418             || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
419             || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
420                                      cipher_mode == EVP_CIPH_CTR_MODE ?
421                                               ctr_do_cipher :
422                                               cipher_do_cipher)
423             || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
424             || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
425                                             cipher_cleanup)
426             || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
427                                                   sizeof(struct cipher_ctx))) {
428             cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
429             EVP_CIPHER_meth_free(known_cipher_methods[i]);
430             known_cipher_methods[i] = NULL;
431         } else {
432             cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
433 #ifdef CIOCGSESSINFO
434             siop.ses = sess.ses;
435             if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
436                 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
437             } else {
438                 cipher_driver_info[i].driver_name =
439                     OPENSSL_strndup(siop.cipher_info.cra_driver_name,
440                                     CRYPTODEV_MAX_ALG_NAME);
441                 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
442                     cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
443                 else
444                     cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
445             }
446 #endif /* CIOCGSESSINFO */
447         }
448         ioctl(cfd, CIOCFSESSION, &sess.ses);
449         if (devcrypto_test_cipher(i)) {
450             known_cipher_nids[known_cipher_nids_amount++] =
451                 cipher_data[i].nid;
452         }
453     }
454 }
455
456 static void rebuild_known_cipher_nids(ENGINE *e)
457 {
458     size_t i;
459
460     for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
461         if (devcrypto_test_cipher(i))
462             known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
463     }
464     ENGINE_unregister_ciphers(e);
465     ENGINE_register_ciphers(e);
466 }
467
468 static const EVP_CIPHER *get_cipher_method(int nid)
469 {
470     size_t i = get_cipher_data_index(nid);
471
472     if (i == (size_t)-1)
473         return NULL;
474     return known_cipher_methods[i];
475 }
476
477 static int get_cipher_nids(const int **nids)
478 {
479     *nids = known_cipher_nids;
480     return known_cipher_nids_amount;
481 }
482
483 static void destroy_cipher_method(int nid)
484 {
485     size_t i = get_cipher_data_index(nid);
486
487     EVP_CIPHER_meth_free(known_cipher_methods[i]);
488     known_cipher_methods[i] = NULL;
489 }
490
491 static void destroy_all_cipher_methods(void)
492 {
493     size_t i;
494
495     for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
496         destroy_cipher_method(cipher_data[i].nid);
497         OPENSSL_free(cipher_driver_info[i].driver_name);
498         cipher_driver_info[i].driver_name = NULL;
499     }
500 }
501
502 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
503                              const int **nids, int nid)
504 {
505     if (cipher == NULL)
506         return get_cipher_nids(nids);
507
508     *cipher = get_cipher_method(nid);
509
510     return *cipher != NULL;
511 }
512
513 static void devcrypto_select_all_ciphers(int *cipher_list)
514 {
515     size_t i;
516
517     for (i = 0; i < OSSL_NELEM(cipher_data); i++)
518         cipher_list[i] = 1;
519 }
520
521 static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
522 {
523     int *cipher_list = (int *)usr;
524     char *name;
525     const EVP_CIPHER *EVP;
526     size_t i;
527
528     if (len == 0)
529         return 1;
530     if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
531         return 0;
532     EVP = EVP_get_cipherbyname(name);
533     if (EVP == NULL)
534         fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
535     else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
536         cipher_list[i] = 1;
537     else
538         fprintf(stderr, "devcrypto: cipher %s not available\n", name);
539     OPENSSL_free(name);
540     return 1;
541 }
542
543 static void dump_cipher_info(void)
544 {
545     size_t i;
546     const char *name;
547
548     fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
549              " engine:\n");
550 #ifndef CIOCGSESSINFO
551     fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
552 #endif
553     for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
554         name = OBJ_nid2sn(cipher_data[i].nid);
555         fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
556                  name ? name : "unknown", cipher_data[i].nid,
557                  cipher_data[i].devcryptoid);
558         if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
559             fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
560             continue;
561         }
562         fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
563                  cipher_driver_info[i].driver_name : "unknown");
564         if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
565             fprintf(stderr, "(hw accelerated)");
566         else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
567             fprintf(stderr, "(software)");
568         else
569             fprintf(stderr, "(acceleration status unknown)");
570         if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
571             fprintf (stderr, ". Cipher setup failed");
572         fprintf(stderr, "\n");
573     }
574     fprintf(stderr, "\n");
575 }
576
577 /*
578  * We only support digests if the cryptodev implementation supports multiple
579  * data updates and session copying.  Otherwise, we would be forced to maintain
580  * a cache, which is perilous if there's a lot of data coming in (if someone
581  * wants to checksum an OpenSSL tarball, for example).
582  */
583 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
584 #define IMPLEMENT_DIGEST
585
586 /******************************************************************************
587  *
588  * Digests
589  *
590  * Because they all do the same basic operation, we have only one set of
591  * method functions for them all to share, and a mapping table between
592  * NIDs and cryptodev IDs, with all the necessary size data.
593  *
594  *****/
595
596 struct digest_ctx {
597     struct session_op sess;
598     /* This signals that the init function was called, not that it succeeded. */
599     int init_called;
600     unsigned char digest_res[HASH_MAX_LEN];
601 };
602
603 static const struct digest_data_st {
604     int nid;
605     int digestlen;
606     int devcryptoid;
607 } digest_data[] = {
608 #ifndef OPENSSL_NO_MD5
609     { NID_md5, 16, CRYPTO_MD5 },
610 #endif
611     { NID_sha1, 20, CRYPTO_SHA1 },
612 #ifndef OPENSSL_NO_RMD160
613 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
614     { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
615 # endif
616 #endif
617 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
618     { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
619 #endif
620 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
621     { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
622 #endif
623 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
624     { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
625 #endif
626 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
627     { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
628 #endif
629 };
630
631 static size_t find_digest_data_index(int nid)
632 {
633     size_t i;
634
635     for (i = 0; i < OSSL_NELEM(digest_data); i++)
636         if (nid == digest_data[i].nid)
637             return i;
638     return (size_t)-1;
639 }
640
641 static size_t get_digest_data_index(int nid)
642 {
643     size_t i = find_digest_data_index(nid);
644
645     if (i != (size_t)-1)
646         return i;
647
648     /*
649      * Code further down must make sure that only NIDs in the table above
650      * are used.  If any other NID reaches this function, there's a grave
651      * coding error further down.
652      */
653     assert("Code that never should be reached" == NULL);
654     return -1;
655 }
656
657 static const struct digest_data_st *get_digest_data(int nid)
658 {
659     return &digest_data[get_digest_data_index(nid)];
660 }
661
662 /*
663  * Following are the five necessary functions to map OpenSSL functionality
664  * with cryptodev: init, update, final, cleanup, and copy.
665  */
666
667 static int digest_init(EVP_MD_CTX *ctx)
668 {
669     struct digest_ctx *digest_ctx =
670         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
671     const struct digest_data_st *digest_d =
672         get_digest_data(EVP_MD_CTX_type(ctx));
673
674     digest_ctx->init_called = 1;
675
676     memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
677     digest_ctx->sess.mac = digest_d->devcryptoid;
678     if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
679         SYSerr(SYS_F_IOCTL, errno);
680         return 0;
681     }
682
683     return 1;
684 }
685
686 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
687                      void *res, unsigned int flags)
688 {
689     struct crypt_op cryp;
690
691     memset(&cryp, 0, sizeof(cryp));
692     cryp.ses = ctx->sess.ses;
693     cryp.len = srclen;
694     cryp.src = (void *)src;
695     cryp.dst = NULL;
696     cryp.mac = res;
697     cryp.flags = flags;
698     return ioctl(cfd, CIOCCRYPT, &cryp);
699 }
700
701 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
702 {
703     struct digest_ctx *digest_ctx =
704         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
705
706     if (count == 0)
707         return 1;
708
709     if (digest_ctx == NULL)
710         return 0;
711
712     if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
713         if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
714             return 1;
715     } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
716         return 1;
717     }
718
719     SYSerr(SYS_F_IOCTL, errno);
720     return 0;
721 }
722
723 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
724 {
725     struct digest_ctx *digest_ctx =
726         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
727
728     if (md == NULL || digest_ctx == NULL)
729         return 0;
730
731     if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
732         memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
733     } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
734         SYSerr(SYS_F_IOCTL, errno);
735         return 0;
736     }
737
738     return 1;
739 }
740
741 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
742 {
743     struct digest_ctx *digest_from =
744         (struct digest_ctx *)EVP_MD_CTX_md_data(from);
745     struct digest_ctx *digest_to =
746         (struct digest_ctx *)EVP_MD_CTX_md_data(to);
747     struct cphash_op cphash;
748
749     if (digest_from == NULL || digest_from->init_called != 1)
750         return 1;
751
752     if (!digest_init(to)) {
753         SYSerr(SYS_F_IOCTL, errno);
754         return 0;
755     }
756
757     cphash.src_ses = digest_from->sess.ses;
758     cphash.dst_ses = digest_to->sess.ses;
759     if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
760         SYSerr(SYS_F_IOCTL, errno);
761         return 0;
762     }
763     return 1;
764 }
765
766 static int digest_cleanup(EVP_MD_CTX *ctx)
767 {
768     struct digest_ctx *digest_ctx =
769         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
770
771     if (digest_ctx == NULL)
772         return 1;
773     if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
774         SYSerr(SYS_F_IOCTL, errno);
775         return 0;
776     }
777     return 1;
778 }
779
780 /*
781  * Keep tables of known nids, associated methods, selected digests, and
782  * driver info.
783  * Note that known_digest_nids[] isn't necessarily indexed the same way as
784  * digest_data[] above, which the other tables are.
785  */
786 static int known_digest_nids[OSSL_NELEM(digest_data)];
787 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
788 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
789 static int selected_digests[OSSL_NELEM(digest_data)];
790 static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
791
792 static int devcrypto_test_digest(size_t digest_data_index)
793 {
794     return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
795             && selected_digests[digest_data_index] == 1
796             && (digest_driver_info[digest_data_index].accelerated
797                     == DEVCRYPTO_ACCELERATED
798                 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
799                 || (digest_driver_info[digest_data_index].accelerated
800                         != DEVCRYPTO_NOT_ACCELERATED
801                     && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
802 }
803
804 static void rebuild_known_digest_nids(ENGINE *e)
805 {
806     size_t i;
807
808     for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
809         if (devcrypto_test_digest(i))
810             known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
811     }
812     ENGINE_unregister_digests(e);
813     ENGINE_register_digests(e);
814 }
815
816 static void prepare_digest_methods(void)
817 {
818     size_t i;
819     struct session_op sess1, sess2;
820 #ifdef CIOCGSESSINFO
821     struct session_info_op siop;
822 #endif
823     struct cphash_op cphash;
824
825     memset(&digest_driver_info, 0, sizeof(digest_driver_info));
826
827     memset(&sess1, 0, sizeof(sess1));
828     memset(&sess2, 0, sizeof(sess2));
829
830     for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
831          i++) {
832
833         selected_digests[i] = 1;
834
835         /*
836          * Check that the digest is usable
837          */
838         sess1.mac = digest_data[i].devcryptoid;
839         sess2.ses = 0;
840         if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
841             digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
842             goto finish;
843         }
844
845 #ifdef CIOCGSESSINFO
846         /* gather hardware acceleration info from the driver */
847         siop.ses = sess1.ses;
848         if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
849             digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
850         } else {
851             digest_driver_info[i].driver_name =
852                 OPENSSL_strndup(siop.hash_info.cra_driver_name,
853                                 CRYPTODEV_MAX_ALG_NAME);
854             if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
855                 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
856             else
857                 digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
858         }
859 #endif
860
861         /* digest must be capable of hash state copy */
862         sess2.mac = sess1.mac;
863         if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
864             digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
865             goto finish;
866         }
867         cphash.src_ses = sess1.ses;
868         cphash.dst_ses = sess2.ses;
869         if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
870             digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
871             goto finish;
872         }
873         if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
874                                                        NID_undef)) == NULL
875             || !EVP_MD_meth_set_result_size(known_digest_methods[i],
876                                             digest_data[i].digestlen)
877             || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
878             || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
879             || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
880             || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
881             || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
882             || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
883                                              sizeof(struct digest_ctx))) {
884             digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
885             EVP_MD_meth_free(known_digest_methods[i]);
886             known_digest_methods[i] = NULL;
887             goto finish;
888         }
889         digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
890 finish:
891         ioctl(cfd, CIOCFSESSION, &sess1.ses);
892         if (sess2.ses != 0)
893             ioctl(cfd, CIOCFSESSION, &sess2.ses);
894         if (devcrypto_test_digest(i))
895             known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
896     }
897 }
898
899 static const EVP_MD *get_digest_method(int nid)
900 {
901     size_t i = get_digest_data_index(nid);
902
903     if (i == (size_t)-1)
904         return NULL;
905     return known_digest_methods[i];
906 }
907
908 static int get_digest_nids(const int **nids)
909 {
910     *nids = known_digest_nids;
911     return known_digest_nids_amount;
912 }
913
914 static void destroy_digest_method(int nid)
915 {
916     size_t i = get_digest_data_index(nid);
917
918     EVP_MD_meth_free(known_digest_methods[i]);
919     known_digest_methods[i] = NULL;
920 }
921
922 static void destroy_all_digest_methods(void)
923 {
924     size_t i;
925
926     for (i = 0; i < OSSL_NELEM(digest_data); i++) {
927         destroy_digest_method(digest_data[i].nid);
928         OPENSSL_free(digest_driver_info[i].driver_name);
929         digest_driver_info[i].driver_name = NULL;
930     }
931 }
932
933 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
934                              const int **nids, int nid)
935 {
936     if (digest == NULL)
937         return get_digest_nids(nids);
938
939     *digest = get_digest_method(nid);
940
941     return *digest != NULL;
942 }
943
944 static void devcrypto_select_all_digests(int *digest_list)
945 {
946     size_t i;
947
948     for (i = 0; i < OSSL_NELEM(digest_data); i++)
949         digest_list[i] = 1;
950 }
951
952 static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
953 {
954     int *digest_list = (int *)usr;
955     char *name;
956     const EVP_MD *EVP;
957     size_t i;
958
959     if (len == 0)
960         return 1;
961     if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
962         return 0;
963     EVP = EVP_get_digestbyname(name);
964     if (EVP == NULL)
965         fprintf(stderr, "devcrypto: unknown digest %s\n", name);
966     else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
967         digest_list[i] = 1;
968     else
969         fprintf(stderr, "devcrypto: digest %s not available\n", name);
970     OPENSSL_free(name);
971     return 1;
972 }
973
974 static void dump_digest_info(void)
975 {
976     size_t i;
977     const char *name;
978
979     fprintf (stderr, "Information about digests supported by the /dev/crypto"
980              " engine:\n");
981 #ifndef CIOCGSESSINFO
982     fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
983 #endif
984
985     for (i = 0; i < OSSL_NELEM(digest_data); i++) {
986         name = OBJ_nid2sn(digest_data[i].nid);
987         fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
988                  name ? name : "unknown", digest_data[i].nid,
989                  digest_data[i].devcryptoid,
990                  digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
991         if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
992             fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
993             continue;
994         }
995         if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
996             fprintf(stderr, " (hw accelerated)");
997         else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
998             fprintf(stderr, " (software)");
999         else
1000             fprintf(stderr, " (acceleration status unknown)");
1001         if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
1002             fprintf (stderr, ". Cipher setup failed\n");
1003         else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
1004             fprintf(stderr, ", CIOCCPHASH failed\n");
1005         else
1006             fprintf(stderr, ", CIOCCPHASH capable\n");
1007     }
1008     fprintf(stderr, "\n");
1009 }
1010
1011 #endif
1012
1013 /******************************************************************************
1014  *
1015  * CONTROL COMMANDS
1016  *
1017  *****/
1018
1019 #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
1020 #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
1021 #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
1022 #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
1023
1024 static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
1025 #ifdef CIOCGSESSINFO
1026    {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
1027     "USE_SOFTDRIVERS",
1028     "specifies whether to use software (not accelerated) drivers ("
1029         OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
1030         OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
1031         OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
1032         "=use if acceleration can't be determined) [default="
1033         OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
1034     ENGINE_CMD_FLAG_NUMERIC},
1035 #endif
1036
1037    {DEVCRYPTO_CMD_CIPHERS,
1038     "CIPHERS",
1039     "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
1040     ENGINE_CMD_FLAG_STRING},
1041
1042 #ifdef IMPLEMENT_DIGEST
1043    {DEVCRYPTO_CMD_DIGESTS,
1044     "DIGESTS",
1045     "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
1046     ENGINE_CMD_FLAG_STRING},
1047 #endif
1048
1049    {DEVCRYPTO_CMD_DUMP_INFO,
1050     "DUMP_INFO",
1051     "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
1052     ENGINE_CMD_FLAG_NO_INPUT},
1053
1054    {0, NULL, NULL, 0}
1055 };
1056
1057 static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1058 {
1059     int *new_list;
1060     switch (cmd) {
1061 #ifdef CIOCGSESSINFO
1062     case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
1063         switch (i) {
1064         case DEVCRYPTO_REQUIRE_ACCELERATED:
1065         case DEVCRYPTO_USE_SOFTWARE:
1066         case DEVCRYPTO_REJECT_SOFTWARE:
1067             break;
1068         default:
1069             fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
1070             return 0;
1071         }
1072         if (use_softdrivers == i)
1073             return 1;
1074         use_softdrivers = i;
1075 #ifdef IMPLEMENT_DIGEST
1076         rebuild_known_digest_nids(e);
1077 #endif
1078         rebuild_known_cipher_nids(e);
1079         return 1;
1080 #endif /* CIOCGSESSINFO */
1081
1082     case DEVCRYPTO_CMD_CIPHERS:
1083         if (p == NULL)
1084             return 1;
1085         if (strcasecmp((const char *)p, "ALL") == 0) {
1086             devcrypto_select_all_ciphers(selected_ciphers);
1087         } else if (strcasecmp((const char*)p, "NONE") == 0) {
1088             memset(selected_ciphers, 0, sizeof(selected_ciphers));
1089         } else {
1090             new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
1091             if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
1092                 OPENSSL_free(new_list);
1093                 return 0;
1094             }
1095             memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
1096             OPENSSL_free(new_list);
1097         }
1098         rebuild_known_cipher_nids(e);
1099         return 1;
1100
1101 #ifdef IMPLEMENT_DIGEST
1102     case DEVCRYPTO_CMD_DIGESTS:
1103         if (p == NULL)
1104             return 1;
1105         if (strcasecmp((const char *)p, "ALL") == 0) {
1106             devcrypto_select_all_digests(selected_digests);
1107         } else if (strcasecmp((const char*)p, "NONE") == 0) {
1108             memset(selected_digests, 0, sizeof(selected_digests));
1109         } else {
1110             new_list=OPENSSL_zalloc(sizeof(selected_digests));
1111             if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
1112                 OPENSSL_free(new_list);
1113                 return 0;
1114             }
1115             memcpy(selected_digests, new_list, sizeof(selected_digests));
1116             OPENSSL_free(new_list);
1117         }
1118         rebuild_known_digest_nids(e);
1119         return 1;
1120 #endif /* IMPLEMENT_DIGEST */
1121
1122     case DEVCRYPTO_CMD_DUMP_INFO:
1123         dump_cipher_info();
1124 #ifdef IMPLEMENT_DIGEST
1125         dump_digest_info();
1126 #endif
1127         return 1;
1128
1129     default:
1130         break;
1131     }
1132     return 0;
1133 }
1134
1135 /******************************************************************************
1136  *
1137  * LOAD / UNLOAD
1138  *
1139  *****/
1140
1141 static int devcrypto_unload(ENGINE *e)
1142 {
1143     destroy_all_cipher_methods();
1144 #ifdef IMPLEMENT_DIGEST
1145     destroy_all_digest_methods();
1146 #endif
1147
1148     close(cfd);
1149
1150     return 1;
1151 }
1152 /*
1153  * This engine is always built into libcrypto, so it doesn't offer any
1154  * ability to be dynamically loadable.
1155  */
1156 void engine_load_devcrypto_int()
1157 {
1158     ENGINE *e = NULL;
1159
1160     if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
1161 #ifndef ENGINE_DEVCRYPTO_DEBUG
1162         if (errno != ENOENT)
1163 #endif
1164             fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
1165         return;
1166     }
1167
1168     if ((e = ENGINE_new()) == NULL
1169         || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
1170         ENGINE_free(e);
1171         /*
1172          * We know that devcrypto_unload() won't be called when one of the
1173          * above two calls have failed, so we close cfd explicitly here to
1174          * avoid leaking resources.
1175          */
1176         close(cfd);
1177         return;
1178     }
1179
1180     prepare_cipher_methods();
1181 #ifdef IMPLEMENT_DIGEST
1182     prepare_digest_methods();
1183 #endif
1184
1185     if (!ENGINE_set_id(e, "devcrypto")
1186         || !ENGINE_set_name(e, "/dev/crypto engine")
1187         || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
1188         || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
1189
1190 /*
1191  * Asymmetric ciphers aren't well supported with /dev/crypto.  Among the BSD
1192  * implementations, it seems to only exist in FreeBSD, and regarding the
1193  * parameters in its crypt_kop, the manual crypto(4) has this to say:
1194  *
1195  *    The semantics of these arguments are currently undocumented.
1196  *
1197  * Reading through the FreeBSD source code doesn't give much more than
1198  * their CRK_MOD_EXP implementation for ubsec.
1199  *
1200  * It doesn't look much better with cryptodev-linux.  They have the crypt_kop
1201  * structure as well as the command (CRK_*) in cryptodev.h, but no support
1202  * seems to be implemented at all for the moment.
1203  *
1204  * At the time of writing, it seems impossible to write proper support for
1205  * FreeBSD's asym features without some very deep knowledge and access to
1206  * specific kernel modules.
1207  *
1208  * /Richard Levitte, 2017-05-11
1209  */
1210 #if 0
1211 # ifndef OPENSSL_NO_RSA
1212         || !ENGINE_set_RSA(e, devcrypto_rsa)
1213 # endif
1214 # ifndef OPENSSL_NO_DSA
1215         || !ENGINE_set_DSA(e, devcrypto_dsa)
1216 # endif
1217 # ifndef OPENSSL_NO_DH
1218         || !ENGINE_set_DH(e, devcrypto_dh)
1219 # endif
1220 # ifndef OPENSSL_NO_EC
1221         || !ENGINE_set_EC(e, devcrypto_ec)
1222 # endif
1223 #endif
1224         || !ENGINE_set_ciphers(e, devcrypto_ciphers)
1225 #ifdef IMPLEMENT_DIGEST
1226         || !ENGINE_set_digests(e, devcrypto_digests)
1227 #endif
1228         ) {
1229         ENGINE_free(e);
1230         return;
1231     }
1232
1233     ENGINE_add(e);
1234     ENGINE_free(e);          /* Loose our local reference */
1235     ERR_clear_error();
1236 }