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