Adapt for BSD cryptodev.h differences
[openssl.git] / crypto / engine / eng_devcrypto.c
1 /*
2  * Copyright 2017 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 <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <sys/ioctl.h>
15 #include <unistd.h>
16 #include <assert.h>
17
18 #include "e_os.h"
19
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 #include "internal/engine.h"
27
28 #ifdef CRYPTO_ALGORITHM_MIN
29 # define CHECK_BSD_STYLE_MACROS
30 #endif
31
32 /******************************************************************************
33  *
34  * Ciphers
35  *
36  * Because they all do the same basic operation, we have only one set of
37  * method functions for them all to share, and a mapping table between
38  * NIDs and cryptodev IDs, with all the necessary size data.
39  *
40  *****/
41
42 struct cipher_ctx {
43     int cfd;
44     struct session_op sess;
45
46     /* to pass from init to do_cipher */
47     const unsigned char *iv;
48     int op;                      /* COP_ENCRYPT or COP_DECRYPT */
49 };
50
51 static const struct cipher_data_st {
52     int nid;
53     int blocksize;
54     int keylen;
55     int ivlen;
56     int flags;
57     int devcryptoid;
58 } cipher_data[] = {
59 #ifndef OPENSSL_NO_DES
60     { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
61     { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
62 #endif
63 #ifndef OPENSSL_NO_BF
64     { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
65 #endif
66 #ifndef OPENSSL_NO_CAST
67     { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
68 #endif
69     { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
70     { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
71     { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
72 #ifndef OPENSSL_NO_RC4
73     { NID_rc4, 1, 16, 0, CRYPTO_ARC4 },
74 #endif
75 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
76     { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
77     { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
78     { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
79 #endif
80 #if 0                            /* Not yet supported */
81     { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
82     { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
83 #endif
84 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
85     { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
86     { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
87     { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
88 #endif
89 #if 0                            /* Not yet supported */
90     { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
91     { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
92     { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
93 #endif
94 #ifndef OPENSSL_NO_CAMELLIA
95     { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
96       CRYPTO_CAMELLIA_CBC },
97     { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
98       CRYPTO_CAMELLIA_CBC },
99     { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
100       CRYPTO_CAMELLIA_CBC },
101 #endif
102 };
103
104 static size_t get_cipher_data_index(int nid)
105 {
106     size_t i;
107
108     for (i = 0; i < OSSL_NELEM(cipher_data); i++)
109         if (nid == cipher_data[i].nid)
110             return i;
111
112     /*
113      * Code further down must make sure that only NIDs in the table above
114      * are used.  If any other NID reaches this function, there's a grave
115      * coding error further down.
116      */
117     assert("Code that never should be reached" == NULL);
118     return -1;
119 }
120
121 static const struct cipher_data_st *get_cipher_data(int nid)
122 {
123     return &cipher_data[get_cipher_data_index(nid)];
124 }
125
126 /*
127  * Following are the three necessary functions to map OpenSSL functionality
128  * with cryptodev.
129  */
130
131 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
132                        const unsigned char *iv, int enc)
133 {
134     struct cipher_ctx *cipher_ctx =
135         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
136     const struct cipher_data_st *cipher_d =
137         get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
138
139     if ((cipher_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
140         SYSerr(SYS_F_OPEN, errno);
141         return 0;
142     }
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(cipher_ctx->cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
150         SYSerr(SYS_F_IOCTL, errno);
151         close(cipher_ctx->cfd);
152         return 0;
153     }
154
155     return 1;
156 }
157
158 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
159                             const unsigned char *in, size_t inl)
160 {
161     struct cipher_ctx *cipher_ctx =
162         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
163     struct crypt_op cryp;
164 #if !defined(COP_FLAG_WRITE_IV)
165     unsigned char saved_iv[EVP_MAX_IV_LENGTH];
166 #endif
167
168     memset(&cryp, 0, sizeof(cryp));
169     cryp.ses = cipher_ctx->sess.ses;
170     cryp.len = inl;
171     cryp.src = (void *)in;
172     cryp.dst = (void *)out;
173     cryp.iv = (void *)EVP_CIPHER_CTX_iv_noconst(ctx);
174     cryp.op = cipher_ctx->op;
175 #if !defined(COP_FLAG_WRITE_IV)
176     cryp.flags = 0;
177
178     if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
179         assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
180         if (!EVP_CIPHER_CTX_encrypting(ctx)) {
181             unsigned char *ivptr = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
182
183             memcpy(saved_iv, ivptr, EVP_CIPHER_CTX_iv_length(ctx));
184         }
185     }
186 #else
187     cryp.flags = COP_FLAG_WRITE_IV;
188 #endif
189
190     if (ioctl(cipher_ctx->cfd, CIOCCRYPT, &cryp) < 0) {
191         SYSerr(SYS_F_IOCTL, errno);
192         return 0;
193     }
194
195 #if !defined(COP_FLAG_WRITE_IV)
196     if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
197         unsigned char *ivptr = saved_iv;
198
199         assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
200         if (!EVP_CIPHER_CTX_encrypting(ctx))
201             ivptr = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
202
203         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), ivptr,
204                EVP_CIPHER_CTX_iv_length(ctx));
205     }
206 #endif
207
208     return 1;
209 }
210
211 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
212 {
213     struct cipher_ctx *cipher_ctx =
214         (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
215
216     if (ioctl(cipher_ctx->cfd, CIOCFSESSION, &cipher_ctx->sess) < 0) {
217         SYSerr(SYS_F_IOCTL, errno);
218         return 0;
219     }
220     if (close(cipher_ctx->cfd) < 0) {
221         SYSerr(SYS_F_CLOSE, errno);
222         return 0;
223     }
224
225     return 1;
226 }
227
228 /*
229  * Keep a table of known nids and associated methods.
230  * Note that known_cipher_nids[] isn't necessarely indexed the same way as
231  * cipher_data[] above, which known_cipher_methods[] is.
232  */
233 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
234 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
235 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
236
237 static void prepare_cipher_methods()
238 {
239     size_t i;
240     struct session_op sess;
241     int cfd;
242
243     if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
244         return;
245
246     memset(&sess, 0, sizeof(sess));
247     sess.key = (void *)"01234567890123456789012345678901234567890123456789";
248
249     for (i = 0, known_cipher_nids_amount = 0;
250          i < OSSL_NELEM(cipher_data); i++) {
251
252         /*
253          * Check that the algo is really availably by trying to open and close
254          * a session.
255          */
256         sess.cipher = cipher_data[i].devcryptoid;
257         sess.keylen = cipher_data[i].keylen;
258         if (ioctl(cfd, CIOCGSESSION, &sess) < 0
259             || ioctl(cfd, CIOCFSESSION, &sess) < 0)
260             continue;
261
262         if ((known_cipher_methods[i] =
263                  EVP_CIPHER_meth_new(cipher_data[i].nid,
264                                      cipher_data[i].blocksize,
265                                      cipher_data[i].keylen)) == NULL
266             || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
267                                               cipher_data[i].ivlen)
268             || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
269                                           cipher_data[i].flags
270                                           | EVP_CIPH_FLAG_DEFAULT_ASN1)
271             || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
272             || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
273                                               cipher_do_cipher)
274             || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
275                                             cipher_cleanup)
276             || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
277                                                   sizeof(struct cipher_ctx))) {
278             EVP_CIPHER_meth_free(known_cipher_methods[i]);
279             known_cipher_methods[i] = NULL;
280         } else {
281             known_cipher_nids[known_cipher_nids_amount++] =
282                 cipher_data[i].nid;
283         }
284     }
285
286     close(cfd);
287 }
288
289 static const EVP_CIPHER *get_cipher_method(int nid)
290 {
291     size_t i = get_cipher_data_index(nid);
292
293     if (i == (size_t)-1)
294         return NULL;
295     return known_cipher_methods[i];
296 }
297
298 static int get_cipher_nids(const int **nids)
299 {
300     *nids = known_cipher_nids;
301     return known_cipher_nids_amount;
302 }
303
304 static void destroy_cipher_method(int nid)
305 {
306     size_t i = get_cipher_data_index(nid);
307
308     EVP_CIPHER_meth_free(known_cipher_methods[i]);
309     known_cipher_methods[i] = NULL;
310 }
311
312 static void destroy_all_cipher_methods()
313 {
314     size_t i;
315
316     for (i = 0; i < OSSL_NELEM(cipher_data); i++)
317         destroy_cipher_method(cipher_data[i].nid);
318 }
319
320 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
321                              const int **nids, int nid)
322 {
323     if (cipher == NULL)
324         return get_cipher_nids(nids);
325
326     *cipher = get_cipher_method(nid);
327
328     return *cipher != NULL;
329 }
330
331 /*
332  * We only support digests if the cryptodev implementation supports multiple
333  * data updates.  Otherwise, we would be forced to maintain a cache, which is
334  * perilous if there's a lot of data coming in (if someone wants to checksum
335  * an OpenSSL tarball, for example).
336  */
337 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
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) < 0) {
477         SYSerr(SYS_F_IOCTL, errno);
478         return 0;
479     }
480
481     return 1;
482 }
483
484 static int digest_cleanup(EVP_MD_CTX *ctx)
485 {
486     struct digest_ctx *digest_ctx =
487         (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
488
489     if (close(digest_ctx->cfd) < 0) {
490         SYSerr(SYS_F_CLOSE, errno);
491         return 0;
492     }
493
494     return 1;
495 }
496
497 /*
498  * Keep a table of known nids and associated methods.
499  * Note that known_digest_nids[] isn't necessarely indexed the same way as
500  * digest_data[] above, which known_digest_methods[] is.
501  */
502 static int known_digest_nids[OSSL_NELEM(digest_data)];
503 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
504 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
505
506 static void prepare_digest_methods()
507 {
508     size_t i;
509     struct session_op sess;
510     int cfd;
511
512     if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
513         return;
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) < 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_cleanup(known_digest_methods[i], digest_cleanup)
537             || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
538                                              sizeof(struct digest_ctx))) {
539             EVP_MD_meth_free(known_digest_methods[i]);
540             known_digest_methods[i] = NULL;
541         } else {
542             known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
543         }
544     }
545
546     close(cfd);
547 }
548
549 static const EVP_MD *get_digest_method(int nid)
550 {
551     size_t i = get_digest_data_index(nid);
552
553     if (i == (size_t)-1)
554         return NULL;
555     return known_digest_methods[i];
556 }
557
558 static int get_digest_nids(const int **nids)
559 {
560     *nids = known_digest_nids;
561     return known_digest_nids_amount;
562 }
563
564 static void destroy_digest_method(int nid)
565 {
566     size_t i = get_digest_data_index(nid);
567
568     EVP_MD_meth_free(known_digest_methods[i]);
569     known_digest_methods[i] = NULL;
570 }
571
572 static void destroy_all_digest_methods()
573 {
574     size_t i;
575
576     for (i = 0; i < OSSL_NELEM(digest_data); i++)
577         destroy_digest_method(digest_data[i].nid);
578 }
579
580 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
581                              const int **nids, int nid)
582 {
583     if (digest == NULL)
584         return get_digest_nids(nids);
585
586     *digest = get_digest_method(nid);
587
588     return *digest != NULL;
589 }
590
591 #endif
592
593 /******************************************************************************
594  *
595  * LOAD / UNLOAD
596  *
597  *****/
598
599 static int devcrypto_unload(ENGINE *e)
600 {
601     destroy_all_cipher_methods();
602 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
603     destroy_all_digest_methods();
604 #endif
605     return 1;
606 }
607 /*
608  * This engine is always built into libcrypto, so it doesn't offer any
609  * ability to be dynamically loadable.
610  */
611 void engine_load_devcrypto_int()
612 {
613     ENGINE *e = NULL;
614
615     if (access("/dev/crypto", R_OK | W_OK) < 0) {
616         fprintf(stderr,
617                 "/dev/crypto not present, not enabling devcrypto engine\n");
618         return;
619     }
620
621     prepare_cipher_methods();
622 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
623     prepare_digest_methods();
624 #endif
625
626     if ((e = ENGINE_new()) == NULL)
627         return;
628
629     if (!ENGINE_set_id(e, "devcrypto")
630         || !ENGINE_set_name(e, "/dev/crypto engine")
631         || !ENGINE_set_destroy_function(e, devcrypto_unload)
632 #if 0                            /* Not supported yet */
633 # ifndef OPENSSL_NO_RSA
634         || !ENGINE_set_RSA(e, devcrypto_rsa)
635 # endif
636 # ifndef OPENSSL_NO_DSA
637         || !ENGINE_set_DSA(e, devcrypto_dsa)
638 # endif
639 # ifndef OPENSSL_NO_DH
640         || !ENGINE_set_DH(e, devcrypto_dh)
641 # endif
642 # ifndef OPENSSL_NO_EC
643         || !ENGINE_set_EC(e, devcrypto_ec)
644 # endif
645 #endif
646         || !ENGINE_set_ciphers(e, devcrypto_ciphers)
647 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
648         || !ENGINE_set_digests(e, devcrypto_digests)
649 #endif
650         ) {
651         ENGINE_free(e);
652         return;
653     }
654
655     ENGINE_add(e);
656     ENGINE_free(e);          /* Loose our local reference */
657     ERR_clear_error();
658 }