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