eng_devcrypto: close session on cleanup, not final
[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/evp.h>
20 #include <openssl/err.h>
21 #include <openssl/engine.h>
22 #include <openssl/objects.h>
23 #include <crypto/cryptodev.h>
24
25 #include "internal/engine.h"
26
27 #ifdef CRYPTO_ALGORITHM_MIN
28 # define CHECK_BSD_STYLE_MACROS
29 #endif
30
31 /*
32  * ONE global file descriptor for all sessions.  This allows operations
33  * such as digest session data copying (see digest_copy()), but is also
34  * saner...  why re-open /dev/crypto for every session?
35  */
36 static int cfd;
37
38 /******************************************************************************
39  *
40  * Ciphers
41  *
42  * Because they all do the same basic operation, we have only one set of
43  * method functions for them all to share, and a mapping table between
44  * NIDs and cryptodev IDs, with all the necessary size data.
45  *
46  *****/
47
48 struct cipher_ctx {
49     struct session_op sess;
50
51     /* to pass from init to do_cipher */
52     const unsigned char *iv;
53     int op;                      /* COP_ENCRYPT or COP_DECRYPT */
54 };
55
56 static const struct cipher_data_st {
57     int nid;
58     int blocksize;
59     int keylen;
60     int ivlen;
61     int flags;
62     int devcryptoid;
63 } cipher_data[] = {
64 #ifndef OPENSSL_NO_DES
65     { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
66     { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
67 #endif
68 #ifndef OPENSSL_NO_BF
69     { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
70 #endif
71 #ifndef OPENSSL_NO_CAST
72     { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
73 #endif
74     { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
75     { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
76     { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
77 #ifndef OPENSSL_NO_RC4
78     { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
79 #endif
80 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
81     { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
82     { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
83     { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
84 #endif
85 #if 0                            /* Not yet supported */
86     { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
87     { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
88 #endif
89 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
90     { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
91     { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
92     { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
93 #endif
94 #if 0                            /* Not yet supported */
95     { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
96     { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
97     { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
98 #endif
99 #ifndef OPENSSL_NO_CAMELLIA
100     { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
101       CRYPTO_CAMELLIA_CBC },
102     { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
103       CRYPTO_CAMELLIA_CBC },
104     { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
105       CRYPTO_CAMELLIA_CBC },
106 #endif
107 };
108
109 static size_t get_cipher_data_index(int nid)
110 {
111     size_t i;
112
113     for (i = 0; i < OSSL_NELEM(cipher_data); i++)
114         if (nid == cipher_data[i].nid)
115             return i;
116
117     /*
118      * Code further down must make sure that only NIDs in the table above
119      * are used.  If any other NID reaches this function, there's a grave
120      * coding error further down.
121      */
122     assert("Code that never should be reached" == NULL);
123     return -1;
124 }
125
126 static const struct cipher_data_st *get_cipher_data(int nid)
127 {
128     return &cipher_data[get_cipher_data_index(nid)];
129 }
130
131 /*
132  * Following are the three necessary functions to map OpenSSL functionality
133  * with cryptodev.
134  */
135
136 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
137                        const unsigned char *iv, int enc)
138 {
139     struct cipher_ctx *cipher_ctx =
140         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
141     const struct cipher_data_st *cipher_d =
142         get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
143
144     memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
145     cipher_ctx->sess.cipher = cipher_d->devcryptoid;
146     cipher_ctx->sess.keylen = cipher_d->keylen;
147     cipher_ctx->sess.key = (void *)key;
148     cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
149     if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
150         SYSerr(SYS_F_IOCTL, errno);
151         return 0;
152     }
153
154     return 1;
155 }
156
157 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
158                             const unsigned char *in, size_t inl)
159 {
160     struct cipher_ctx *cipher_ctx =
161         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
162     struct crypt_op cryp;
163 #if !defined(COP_FLAG_WRITE_IV)
164     unsigned char saved_iv[EVP_MAX_IV_LENGTH];
165 #endif
166
167     memset(&cryp, 0, sizeof(cryp));
168     cryp.ses = cipher_ctx->sess.ses;
169     cryp.len = inl;
170     cryp.src = (void *)in;
171     cryp.dst = (void *)out;
172     cryp.iv = (void *)EVP_CIPHER_CTX_iv_noconst(ctx);
173     cryp.op = cipher_ctx->op;
174 #if !defined(COP_FLAG_WRITE_IV)
175     cryp.flags = 0;
176
177     if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
178         assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
179         if (!EVP_CIPHER_CTX_encrypting(ctx)) {
180             unsigned char *ivptr = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
181
182             memcpy(saved_iv, ivptr, EVP_CIPHER_CTX_iv_length(ctx));
183         }
184     }
185 #else
186     cryp.flags = COP_FLAG_WRITE_IV;
187 #endif
188
189     if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
190         SYSerr(SYS_F_IOCTL, errno);
191         return 0;
192     }
193
194 #if !defined(COP_FLAG_WRITE_IV)
195     if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
196         unsigned char *ivptr = saved_iv;
197
198         assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
199         if (!EVP_CIPHER_CTX_encrypting(ctx))
200             ivptr = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
201
202         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), ivptr,
203                EVP_CIPHER_CTX_iv_length(ctx));
204     }
205 #endif
206
207     return 1;
208 }
209
210 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
211 {
212     struct cipher_ctx *cipher_ctx =
213         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
214
215     if (ioctl(cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
216         SYSerr(SYS_F_IOCTL, errno);
217         return 0;
218     }
219
220     return 1;
221 }
222
223 /*
224  * Keep a table of known nids and associated methods.
225  * Note that known_cipher_nids[] isn't necessarily indexed the same way as
226  * cipher_data[] above, which known_cipher_methods[] is.
227  */
228 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
229 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
230 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
231
232 static void prepare_cipher_methods(void)
233 {
234     size_t i;
235     struct session_op sess;
236
237     memset(&sess, 0, sizeof(sess));
238     sess.key = (void *)"01234567890123456789012345678901234567890123456789";
239
240     for (i = 0, known_cipher_nids_amount = 0;
241          i < OSSL_NELEM(cipher_data); i++) {
242
243         /*
244          * Check that the algo is really availably by trying to open and close
245          * a session.
246          */
247         sess.cipher = cipher_data[i].devcryptoid;
248         sess.keylen = cipher_data[i].keylen;
249         if (ioctl(cfd, CIOCGSESSION, &sess) < 0
250             || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
251             continue;
252
253         if ((known_cipher_methods[i] =
254                  EVP_CIPHER_meth_new(cipher_data[i].nid,
255                                      cipher_data[i].blocksize,
256                                      cipher_data[i].keylen)) == NULL
257             || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
258                                               cipher_data[i].ivlen)
259             || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
260                                           cipher_data[i].flags
261                                           | EVP_CIPH_FLAG_DEFAULT_ASN1)
262             || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
263             || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
264                                               cipher_do_cipher)
265             || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
266                                             cipher_cleanup)
267             || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
268                                                   sizeof(struct cipher_ctx))) {
269             EVP_CIPHER_meth_free(known_cipher_methods[i]);
270             known_cipher_methods[i] = NULL;
271         } else {
272             known_cipher_nids[known_cipher_nids_amount++] =
273                 cipher_data[i].nid;
274         }
275     }
276 }
277
278 static const EVP_CIPHER *get_cipher_method(int nid)
279 {
280     size_t i = get_cipher_data_index(nid);
281
282     if (i == (size_t)-1)
283         return NULL;
284     return known_cipher_methods[i];
285 }
286
287 static int get_cipher_nids(const int **nids)
288 {
289     *nids = known_cipher_nids;
290     return known_cipher_nids_amount;
291 }
292
293 static void destroy_cipher_method(int nid)
294 {
295     size_t i = get_cipher_data_index(nid);
296
297     EVP_CIPHER_meth_free(known_cipher_methods[i]);
298     known_cipher_methods[i] = NULL;
299 }
300
301 static void destroy_all_cipher_methods(void)
302 {
303     size_t i;
304
305     for (i = 0; i < OSSL_NELEM(cipher_data); i++)
306         destroy_cipher_method(cipher_data[i].nid);
307 }
308
309 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
310                              const int **nids, int nid)
311 {
312     if (cipher == NULL)
313         return get_cipher_nids(nids);
314
315     *cipher = get_cipher_method(nid);
316
317     return *cipher != NULL;
318 }
319
320 /*
321  * We only support digests if the cryptodev implementation supports multiple
322  * data updates and session copying.  Otherwise, we would be forced to maintain
323  * a cache, which is perilous if there's a lot of data coming in (if someone
324  * wants to checksum an OpenSSL tarball, for example).
325  */
326 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
327 #define IMPLEMENT_DIGEST
328
329 /******************************************************************************
330  *
331  * Digests
332  *
333  * Because they all do the same basic operation, we have only one set of
334  * method functions for them all to share, and a mapping table between
335  * NIDs and cryptodev IDs, with all the necessary size data.
336  *
337  *****/
338
339 struct digest_ctx {
340     struct session_op sess;
341     /* This signals that the init function was called, not that it succeeded. */
342     int init_called;
343 };
344
345 static const struct digest_data_st {
346     int nid;
347     int digestlen;
348     int devcryptoid;
349 } digest_data[] = {
350 #ifndef OPENSSL_NO_MD5
351     { NID_md5, 16, CRYPTO_MD5 },
352 #endif
353     { NID_sha1, 20, CRYPTO_SHA1 },
354 #ifndef OPENSSL_NO_RMD160
355 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
356     { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
357 # endif
358 #endif
359 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
360     { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
361 #endif
362 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
363     { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
364 #endif
365 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
366     { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
367 #endif
368 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
369     { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
370 #endif
371 };
372
373 static size_t get_digest_data_index(int nid)
374 {
375     size_t i;
376
377     for (i = 0; i < OSSL_NELEM(digest_data); i++)
378         if (nid == digest_data[i].nid)
379             return i;
380
381     /*
382      * Code further down must make sure that only NIDs in the table above
383      * are used.  If any other NID reaches this function, there's a grave
384      * coding error further down.
385      */
386     assert("Code that never should be reached" == NULL);
387     return -1;
388 }
389
390 static const struct digest_data_st *get_digest_data(int nid)
391 {
392     return &digest_data[get_digest_data_index(nid)];
393 }
394
395 /*
396  * Following are the four necessary functions to map OpenSSL functionality
397  * with cryptodev.
398  */
399
400 static int digest_init(EVP_MD_CTX *ctx)
401 {
402     struct digest_ctx *digest_ctx =
403         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
404     const struct digest_data_st *digest_d =
405         get_digest_data(EVP_MD_CTX_type(ctx));
406
407     digest_ctx->init_called = 1;
408
409     memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
410     digest_ctx->sess.mac = digest_d->devcryptoid;
411     if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
412         SYSerr(SYS_F_IOCTL, errno);
413         return 0;
414     }
415
416     return 1;
417 }
418
419 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
420                      void *res, unsigned int flags)
421 {
422     struct crypt_op cryp;
423
424     memset(&cryp, 0, sizeof(cryp));
425     cryp.ses = ctx->sess.ses;
426     cryp.len = srclen;
427     cryp.src = (void *)src;
428     cryp.dst = NULL;
429     cryp.mac = res;
430     cryp.flags = flags;
431     return ioctl(cfd, CIOCCRYPT, &cryp);
432 }
433
434 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
435 {
436     struct digest_ctx *digest_ctx =
437         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
438
439     if (count == 0)
440         return 1;
441
442     if (digest_ctx == NULL)
443         return 0;
444
445     if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
446         SYSerr(SYS_F_IOCTL, errno);
447         return 0;
448     }
449
450     return 1;
451 }
452
453 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
454 {
455     struct digest_ctx *digest_ctx =
456         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
457
458     if (md == NULL || digest_ctx == NULL)
459         return 0;
460     if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
461         SYSerr(SYS_F_IOCTL, errno);
462         return 0;
463     }
464
465     return 1;
466 }
467
468 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
469 {
470     struct digest_ctx *digest_from =
471         (struct digest_ctx *)EVP_MD_CTX_md_data(from);
472     struct digest_ctx *digest_to =
473         (struct digest_ctx *)EVP_MD_CTX_md_data(to);
474     struct cphash_op cphash;
475
476     if (digest_from == NULL || digest_from->init_called != 1)
477         return 1;
478
479     if (!digest_init(to)) {
480         SYSerr(SYS_F_IOCTL, errno);
481         return 0;
482     }
483
484     cphash.src_ses = digest_from->sess.ses;
485     cphash.dst_ses = digest_to->sess.ses;
486     if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
487         SYSerr(SYS_F_IOCTL, errno);
488         return 0;
489     }
490     return 1;
491 }
492
493 static int digest_cleanup(EVP_MD_CTX *ctx)
494 {
495     struct digest_ctx *digest_ctx =
496         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
497
498     if (digest_ctx == NULL)
499         return 1;
500     if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
501         SYSerr(SYS_F_IOCTL, errno);
502         return 0;
503     }
504     return 1;
505 }
506
507 /*
508  * Keep a table of known nids and associated methods.
509  * Note that known_digest_nids[] isn't necessarily indexed the same way as
510  * digest_data[] above, which known_digest_methods[] is.
511  */
512 static int known_digest_nids[OSSL_NELEM(digest_data)];
513 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
514 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
515
516 static void prepare_digest_methods(void)
517 {
518     size_t i;
519     struct session_op sess;
520
521     memset(&sess, 0, sizeof(sess));
522
523     for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
524          i++) {
525
526         /*
527          * Check that the algo is really availably by trying to open and close
528          * a session.
529          */
530         sess.mac = digest_data[i].devcryptoid;
531         if (ioctl(cfd, CIOCGSESSION, &sess) < 0
532             || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
533             continue;
534
535         if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
536                                                        NID_undef)) == NULL
537             || !EVP_MD_meth_set_result_size(known_digest_methods[i],
538                                             digest_data[i].digestlen)
539             || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
540             || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
541             || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
542             || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
543             || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
544             || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
545                                              sizeof(struct digest_ctx))) {
546             EVP_MD_meth_free(known_digest_methods[i]);
547             known_digest_methods[i] = NULL;
548         } else {
549             known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
550         }
551     }
552 }
553
554 static const EVP_MD *get_digest_method(int nid)
555 {
556     size_t i = get_digest_data_index(nid);
557
558     if (i == (size_t)-1)
559         return NULL;
560     return known_digest_methods[i];
561 }
562
563 static int get_digest_nids(const int **nids)
564 {
565     *nids = known_digest_nids;
566     return known_digest_nids_amount;
567 }
568
569 static void destroy_digest_method(int nid)
570 {
571     size_t i = get_digest_data_index(nid);
572
573     EVP_MD_meth_free(known_digest_methods[i]);
574     known_digest_methods[i] = NULL;
575 }
576
577 static void destroy_all_digest_methods(void)
578 {
579     size_t i;
580
581     for (i = 0; i < OSSL_NELEM(digest_data); i++)
582         destroy_digest_method(digest_data[i].nid);
583 }
584
585 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
586                              const int **nids, int nid)
587 {
588     if (digest == NULL)
589         return get_digest_nids(nids);
590
591     *digest = get_digest_method(nid);
592
593     return *digest != NULL;
594 }
595
596 #endif
597
598 /******************************************************************************
599  *
600  * LOAD / UNLOAD
601  *
602  *****/
603
604 static int devcrypto_unload(ENGINE *e)
605 {
606     destroy_all_cipher_methods();
607 #ifdef IMPLEMENT_DIGEST
608     destroy_all_digest_methods();
609 #endif
610
611     close(cfd);
612
613     return 1;
614 }
615 /*
616  * This engine is always built into libcrypto, so it doesn't offer any
617  * ability to be dynamically loadable.
618  */
619 void engine_load_devcrypto_int()
620 {
621     ENGINE *e = NULL;
622
623     if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
624         fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
625         return;
626     }
627
628     if ((e = ENGINE_new()) == NULL
629         || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
630         ENGINE_free(e);
631         /*
632          * We know that devcrypto_unload() won't be called when one of the
633          * above two calls have failed, so we close cfd explicitly here to
634          * avoid leaking resources.
635          */
636         close(cfd);
637         return;
638     }
639
640     prepare_cipher_methods();
641 #ifdef IMPLEMENT_DIGEST
642     prepare_digest_methods();
643 #endif
644
645     if (!ENGINE_set_id(e, "devcrypto")
646         || !ENGINE_set_name(e, "/dev/crypto engine")
647
648 /*
649  * Asymmetric ciphers aren't well supported with /dev/crypto.  Among the BSD
650  * implementations, it seems to only exist in FreeBSD, and regarding the
651  * parameters in its crypt_kop, the manual crypto(4) has this to say:
652  *
653  *    The semantics of these arguments are currently undocumented.
654  *
655  * Reading through the FreeBSD source code doesn't give much more than
656  * their CRK_MOD_EXP implementation for ubsec.
657  *
658  * It doesn't look much better with cryptodev-linux.  They have the crypt_kop
659  * structure as well as the command (CRK_*) in cryptodev.h, but no support
660  * seems to be implemented at all for the moment.
661  *
662  * At the time of writing, it seems impossible to write proper support for
663  * FreeBSD's asym features without some very deep knowledge and access to
664  * specific kernel modules.
665  *
666  * /Richard Levitte, 2017-05-11
667  */
668 #if 0
669 # ifndef OPENSSL_NO_RSA
670         || !ENGINE_set_RSA(e, devcrypto_rsa)
671 # endif
672 # ifndef OPENSSL_NO_DSA
673         || !ENGINE_set_DSA(e, devcrypto_dsa)
674 # endif
675 # ifndef OPENSSL_NO_DH
676         || !ENGINE_set_DH(e, devcrypto_dh)
677 # endif
678 # ifndef OPENSSL_NO_EC
679         || !ENGINE_set_EC(e, devcrypto_ec)
680 # endif
681 #endif
682         || !ENGINE_set_ciphers(e, devcrypto_ciphers)
683 #ifdef IMPLEMENT_DIGEST
684         || !ENGINE_set_digests(e, devcrypto_digests)
685 #endif
686         ) {
687         ENGINE_free(e);
688         return;
689     }
690
691     ENGINE_add(e);
692     ENGINE_free(e);          /* Loose our local reference */
693     ERR_clear_error();
694 }