crypto/engine/eng_devcrypto.c: ensure we don't leak resources
[openssl.git] / crypto / engine / eng_devcrypto.c
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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     int init;
342 };
343
344 static const struct digest_data_st {
345     int nid;
346     int digestlen;
347     int devcryptoid;
348 } digest_data[] = {
349 #ifndef OPENSSL_NO_MD5
350     { NID_md5, 16, CRYPTO_MD5 },
351 #endif
352     { NID_sha1, 20, CRYPTO_SHA1 },
353 #ifndef OPENSSL_NO_RMD160
354 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
355     { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
356 # endif
357 #endif
358 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
359     { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
360 #endif
361 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
362     { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
363 #endif
364 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
365     { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
366 #endif
367 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
368     { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
369 #endif
370 };
371
372 static size_t get_digest_data_index(int nid)
373 {
374     size_t i;
375
376     for (i = 0; i < OSSL_NELEM(digest_data); i++)
377         if (nid == digest_data[i].nid)
378             return i;
379
380     /*
381      * Code further down must make sure that only NIDs in the table above
382      * are used.  If any other NID reaches this function, there's a grave
383      * coding error further down.
384      */
385     assert("Code that never should be reached" == NULL);
386     return -1;
387 }
388
389 static const struct digest_data_st *get_digest_data(int nid)
390 {
391     return &digest_data[get_digest_data_index(nid)];
392 }
393
394 /*
395  * Following are the four necessary functions to map OpenSSL functionality
396  * with cryptodev.
397  */
398
399 static int digest_init(EVP_MD_CTX *ctx)
400 {
401     struct digest_ctx *digest_ctx =
402         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
403     const struct digest_data_st *digest_d =
404         get_digest_data(EVP_MD_CTX_type(ctx));
405
406     digest_ctx->init = 1;
407
408     memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
409     digest_ctx->sess.mac = digest_d->devcryptoid;
410     if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
411         SYSerr(SYS_F_IOCTL, errno);
412         return 0;
413     }
414
415     return 1;
416 }
417
418 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
419                      void *res, unsigned int flags)
420 {
421     struct crypt_op cryp;
422
423     memset(&cryp, 0, sizeof(cryp));
424     cryp.ses = ctx->sess.ses;
425     cryp.len = srclen;
426     cryp.src = (void *)src;
427     cryp.dst = NULL;
428     cryp.mac = res;
429     cryp.flags = flags;
430     return ioctl(cfd, CIOCCRYPT, &cryp);
431 }
432
433 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
434 {
435     struct digest_ctx *digest_ctx =
436         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
437
438     if (count == 0)
439         return 1;
440
441     if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
442         SYSerr(SYS_F_IOCTL, errno);
443         return 0;
444     }
445
446     return 1;
447 }
448
449 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
450 {
451     struct digest_ctx *digest_ctx =
452         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
453
454     if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
455         SYSerr(SYS_F_IOCTL, errno);
456         return 0;
457     }
458     if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
459         SYSerr(SYS_F_IOCTL, errno);
460         return 0;
461     }
462
463     return 1;
464 }
465
466 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
467 {
468     struct digest_ctx *digest_from =
469         (struct digest_ctx *)EVP_MD_CTX_md_data(from);
470     struct digest_ctx *digest_to =
471         (struct digest_ctx *)EVP_MD_CTX_md_data(to);
472     struct cphash_op cphash;
473
474     if (digest_from == NULL)
475         return 1;
476
477     if (digest_from->init != 1) {
478         SYSerr(SYS_F_IOCTL, EINVAL);
479         return 0;
480     }
481
482     if (!digest_init(to)) {
483         SYSerr(SYS_F_IOCTL, errno);
484         return 0;
485     }
486
487     cphash.src_ses = digest_from->sess.ses;
488     cphash.dst_ses = digest_to->sess.ses;
489     if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
490         SYSerr(SYS_F_IOCTL, errno);
491         return 0;
492     }
493     return 1;
494 }
495
496 static int digest_cleanup(EVP_MD_CTX *ctx)
497 {
498     return 1;
499 }
500
501 /*
502  * Keep a table of known nids and associated methods.
503  * Note that known_digest_nids[] isn't necessarily indexed the same way as
504  * digest_data[] above, which known_digest_methods[] is.
505  */
506 static int known_digest_nids[OSSL_NELEM(digest_data)];
507 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
508 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
509
510 static void prepare_digest_methods(void)
511 {
512     size_t i;
513     struct session_op sess;
514
515     memset(&sess, 0, sizeof(sess));
516
517     for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
518          i++) {
519
520         /*
521          * Check that the algo is really availably by trying to open and close
522          * a session.
523          */
524         sess.mac = digest_data[i].devcryptoid;
525         if (ioctl(cfd, CIOCGSESSION, &sess) < 0
526             || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
527             continue;
528
529         if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
530                                                        NID_undef)) == NULL
531             || !EVP_MD_meth_set_result_size(known_digest_methods[i],
532                                             digest_data[i].digestlen)
533             || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
534             || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
535             || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
536             || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
537             || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
538             || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
539                                              sizeof(struct digest_ctx))) {
540             EVP_MD_meth_free(known_digest_methods[i]);
541             known_digest_methods[i] = NULL;
542         } else {
543             known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
544         }
545     }
546 }
547
548 static const EVP_MD *get_digest_method(int nid)
549 {
550     size_t i = get_digest_data_index(nid);
551
552     if (i == (size_t)-1)
553         return NULL;
554     return known_digest_methods[i];
555 }
556
557 static int get_digest_nids(const int **nids)
558 {
559     *nids = known_digest_nids;
560     return known_digest_nids_amount;
561 }
562
563 static void destroy_digest_method(int nid)
564 {
565     size_t i = get_digest_data_index(nid);
566
567     EVP_MD_meth_free(known_digest_methods[i]);
568     known_digest_methods[i] = NULL;
569 }
570
571 static void destroy_all_digest_methods(void)
572 {
573     size_t i;
574
575     for (i = 0; i < OSSL_NELEM(digest_data); i++)
576         destroy_digest_method(digest_data[i].nid);
577 }
578
579 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
580                              const int **nids, int nid)
581 {
582     if (digest == NULL)
583         return get_digest_nids(nids);
584
585     *digest = get_digest_method(nid);
586
587     return *digest != NULL;
588 }
589
590 #endif
591
592 /******************************************************************************
593  *
594  * LOAD / UNLOAD
595  *
596  *****/
597
598 static int devcrypto_unload(ENGINE *e)
599 {
600     destroy_all_cipher_methods();
601 #ifdef IMPLEMENT_DIGEST
602     destroy_all_digest_methods();
603 #endif
604
605     close(cfd);
606
607     return 1;
608 }
609 /*
610  * This engine is always built into libcrypto, so it doesn't offer any
611  * ability to be dynamically loadable.
612  */
613 void engine_load_devcrypto_int()
614 {
615     ENGINE *e = NULL;
616
617     if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
618         fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
619         return;
620     }
621
622     prepare_cipher_methods();
623 #ifdef IMPLEMENT_DIGEST
624     prepare_digest_methods();
625 #endif
626
627     if ((e = ENGINE_new()) == NULL
628         || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
629         ENGINE_free(e);
630         /*
631          * We know that devcrypto_unload() won't be called when one of the
632          * above two calls have failed, so we close cfd explicitly here to
633          * avoid leaking resources.
634          */
635         close(cfd);
636         return;
637     }
638
639     if (!ENGINE_set_id(e, "devcrypto")
640         || !ENGINE_set_name(e, "/dev/crypto engine")
641
642 /*
643  * Asymmetric ciphers aren't well supported with /dev/crypto.  Among the BSD
644  * implementations, it seems to only exist in FreeBSD, and regarding the
645  * parameters in its crypt_kop, the manual crypto(4) has this to say:
646  *
647  *    The semantics of these arguments are currently undocumented.
648  *
649  * Reading through the FreeBSD source code doesn't give much more than
650  * their CRK_MOD_EXP implementation for ubsec.
651  *
652  * It doesn't look much better with cryptodev-linux.  They have the crypt_kop
653  * structure as well as the command (CRK_*) in cryptodev.h, but no support
654  * seems to be implemented at all for the moment.
655  *
656  * At the time of writing, it seems impossible to write proper support for
657  * FreeBSD's asym features without some very deep knowledge and access to
658  * specific kernel modules.
659  *
660  * /Richard Levitte, 2017-05-11
661  */
662 #if 0
663 # ifndef OPENSSL_NO_RSA
664         || !ENGINE_set_RSA(e, devcrypto_rsa)
665 # endif
666 # ifndef OPENSSL_NO_DSA
667         || !ENGINE_set_DSA(e, devcrypto_dsa)
668 # endif
669 # ifndef OPENSSL_NO_DH
670         || !ENGINE_set_DH(e, devcrypto_dh)
671 # endif
672 # ifndef OPENSSL_NO_EC
673         || !ENGINE_set_EC(e, devcrypto_ec)
674 # endif
675 #endif
676         || !ENGINE_set_ciphers(e, devcrypto_ciphers)
677 #ifdef IMPLEMENT_DIGEST
678         || !ENGINE_set_digests(e, devcrypto_digests)
679 #endif
680         ) {
681         ENGINE_free(e);
682         return;
683     }
684
685     ENGINE_add(e);
686     ENGINE_free(e);          /* Loose our local reference */
687     ERR_clear_error();
688 }