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