2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <sys/types.h>
15 #include <sys/ioctl.h>
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>
26 #include "internal/engine.h"
28 /* #define ENGINE_DEVCRYPTO_DEBUG */
30 #ifdef CRYPTO_ALGORITHM_MIN
31 # define CHECK_BSD_STYLE_MACROS
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?
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 */
44 #define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
45 static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
48 * cipher/digest status & acceleration definitions
49 * Make sure the defaults are set to 0
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 */
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 */
69 /******************************************************************************
73 * Because they all do the same basic operation, we have only one set of
74 * method functions for them all to share, and a mapping table between
75 * NIDs and cryptodev IDs, with all the necessary size data.
80 struct session_op sess;
81 int op; /* COP_ENCRYPT or COP_DECRYPT */
82 unsigned long mode; /* EVP_CIPH_*_MODE */
84 /* to handle ctr mode being a stream cipher */
85 unsigned char partial[EVP_MAX_BLOCK_LENGTH];
86 unsigned int blocksize, num;
89 static const struct cipher_data_st {
97 #ifndef OPENSSL_NO_DES
98 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
99 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
101 #ifndef OPENSSL_NO_BF
102 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
104 #ifndef OPENSSL_NO_CAST
105 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
107 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
108 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
109 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
110 #ifndef OPENSSL_NO_RC4
111 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
113 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
114 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
115 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
116 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
118 #if 0 /* Not yet supported */
119 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
120 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
122 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
123 { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
124 { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
125 { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
127 #if 0 /* Not yet supported */
128 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
129 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
130 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
132 #ifndef OPENSSL_NO_CAMELLIA
133 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
134 CRYPTO_CAMELLIA_CBC },
135 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
136 CRYPTO_CAMELLIA_CBC },
137 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
138 CRYPTO_CAMELLIA_CBC },
142 static size_t find_cipher_data_index(int nid)
146 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
147 if (nid == cipher_data[i].nid)
152 static size_t get_cipher_data_index(int nid)
154 size_t i = find_cipher_data_index(nid);
160 * Code further down must make sure that only NIDs in the table above
161 * are used. If any other NID reaches this function, there's a grave
162 * coding error further down.
164 assert("Code that never should be reached" == NULL);
168 static const struct cipher_data_st *get_cipher_data(int nid)
170 return &cipher_data[get_cipher_data_index(nid)];
174 * Following are the three necessary functions to map OpenSSL functionality
178 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
179 const unsigned char *iv, int enc)
181 struct cipher_ctx *cipher_ctx =
182 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
183 const struct cipher_data_st *cipher_d =
184 get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
186 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
187 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
188 cipher_ctx->sess.keylen = cipher_d->keylen;
189 cipher_ctx->sess.key = (void *)key;
190 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
191 cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
192 cipher_ctx->blocksize = cipher_d->blocksize;
193 if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
194 SYSerr(SYS_F_IOCTL, errno);
201 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
202 const unsigned char *in, size_t inl)
204 struct cipher_ctx *cipher_ctx =
205 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
206 struct crypt_op cryp;
207 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
208 #if !defined(COP_FLAG_WRITE_IV)
209 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
210 const unsigned char *ivptr;
211 size_t nblocks, ivlen;
214 memset(&cryp, 0, sizeof(cryp));
215 cryp.ses = cipher_ctx->sess.ses;
217 cryp.src = (void *)in;
218 cryp.dst = (void *)out;
219 cryp.iv = (void *)iv;
220 cryp.op = cipher_ctx->op;
221 #if !defined(COP_FLAG_WRITE_IV)
224 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
226 switch (cipher_ctx->mode) {
227 case EVP_CIPH_CBC_MODE:
228 assert(inl >= ivlen);
229 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
230 ivptr = in + inl - ivlen;
231 memcpy(saved_iv, ivptr, ivlen);
235 case EVP_CIPH_CTR_MODE:
238 default: /* should not happen */
242 cryp.flags = COP_FLAG_WRITE_IV;
245 if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
246 SYSerr(SYS_F_IOCTL, errno);
250 #if !defined(COP_FLAG_WRITE_IV)
252 switch (cipher_ctx->mode) {
253 case EVP_CIPH_CBC_MODE:
254 assert(inl >= ivlen);
255 if (EVP_CIPHER_CTX_encrypting(ctx))
256 ivptr = out + inl - ivlen;
260 memcpy(iv, ivptr, ivlen);
263 case EVP_CIPH_CTR_MODE:
264 nblocks = (inl + cipher_ctx->blocksize - 1)
265 / cipher_ctx->blocksize;
268 nblocks += iv[ivlen];
269 iv[ivlen] = (uint8_t) nblocks;
274 default: /* should not happen */
282 static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
283 const unsigned char *in, size_t inl)
285 struct cipher_ctx *cipher_ctx =
286 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
289 /* initial partial block */
290 while (cipher_ctx->num && inl) {
291 (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
293 cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
297 if (inl > (unsigned int) cipher_ctx->blocksize) {
298 nblocks = inl/cipher_ctx->blocksize;
299 len = nblocks * cipher_ctx->blocksize;
300 if (cipher_do_cipher(ctx, out, in, len) < 1)
307 /* final partial block */
309 memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
310 if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
311 cipher_ctx->blocksize) < 1)
314 out[cipher_ctx->num] = in[cipher_ctx->num]
315 ^ cipher_ctx->partial[cipher_ctx->num];
323 static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
325 EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
326 struct cipher_ctx *cipher_ctx;
328 if (type == EVP_CTRL_COPY) {
329 /* when copying the context, a new session needs to be initialized */
330 cipher_ctx = (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
331 return (cipher_ctx == NULL)
332 || cipher_init(to_ctx, cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
333 (cipher_ctx->op == COP_ENCRYPT));
339 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
341 struct cipher_ctx *cipher_ctx =
342 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
344 if (ioctl(cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
345 SYSerr(SYS_F_IOCTL, errno);
353 * Keep tables of known nids, associated methods, selected ciphers, and driver
355 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
356 * cipher_data[] above, which the other tables are.
358 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
359 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
360 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
361 static int selected_ciphers[OSSL_NELEM(cipher_data)];
362 static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
365 static int devcrypto_test_cipher(size_t cipher_data_index)
367 return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
368 && selected_ciphers[cipher_data_index] == 1
369 && (cipher_driver_info[cipher_data_index].accelerated
370 == DEVCRYPTO_ACCELERATED
371 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
372 || (cipher_driver_info[cipher_data_index].accelerated
373 != DEVCRYPTO_NOT_ACCELERATED
374 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
377 static void prepare_cipher_methods(void)
380 struct session_op sess;
381 unsigned long cipher_mode;
383 struct session_info_op siop;
386 memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
388 memset(&sess, 0, sizeof(sess));
389 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
391 for (i = 0, known_cipher_nids_amount = 0;
392 i < OSSL_NELEM(cipher_data); i++) {
394 selected_ciphers[i] = 1;
396 * Check that the cipher is usable
398 sess.cipher = cipher_data[i].devcryptoid;
399 sess.keylen = cipher_data[i].keylen;
400 if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
401 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
405 cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
407 if ((known_cipher_methods[i] =
408 EVP_CIPHER_meth_new(cipher_data[i].nid,
409 cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
410 cipher_data[i].blocksize,
411 cipher_data[i].keylen)) == NULL
412 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
413 cipher_data[i].ivlen)
414 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
416 | EVP_CIPH_CUSTOM_COPY
417 | EVP_CIPH_FLAG_DEFAULT_ASN1)
418 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
419 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
420 cipher_mode == EVP_CIPH_CTR_MODE ?
423 || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
424 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
426 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
427 sizeof(struct cipher_ctx))) {
428 cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
429 EVP_CIPHER_meth_free(known_cipher_methods[i]);
430 known_cipher_methods[i] = NULL;
432 cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
435 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
436 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
438 cipher_driver_info[i].driver_name =
439 OPENSSL_strndup(siop.cipher_info.cra_driver_name,
440 CRYPTODEV_MAX_ALG_NAME);
441 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
442 cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
444 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
446 #endif /* CIOCGSESSINFO */
448 ioctl(cfd, CIOCFSESSION, &sess.ses);
449 if (devcrypto_test_cipher(i)) {
450 known_cipher_nids[known_cipher_nids_amount++] =
456 static void rebuild_known_cipher_nids(ENGINE *e)
460 for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
461 if (devcrypto_test_cipher(i))
462 known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
464 ENGINE_unregister_ciphers(e);
465 ENGINE_register_ciphers(e);
468 static const EVP_CIPHER *get_cipher_method(int nid)
470 size_t i = get_cipher_data_index(nid);
474 return known_cipher_methods[i];
477 static int get_cipher_nids(const int **nids)
479 *nids = known_cipher_nids;
480 return known_cipher_nids_amount;
483 static void destroy_cipher_method(int nid)
485 size_t i = get_cipher_data_index(nid);
487 EVP_CIPHER_meth_free(known_cipher_methods[i]);
488 known_cipher_methods[i] = NULL;
491 static void destroy_all_cipher_methods(void)
495 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
496 destroy_cipher_method(cipher_data[i].nid);
497 OPENSSL_free(cipher_driver_info[i].driver_name);
498 cipher_driver_info[i].driver_name = NULL;
502 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
503 const int **nids, int nid)
506 return get_cipher_nids(nids);
508 *cipher = get_cipher_method(nid);
510 return *cipher != NULL;
513 static void devcrypto_select_all_ciphers(int *cipher_list)
517 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
521 static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
523 int *cipher_list = (int *)usr;
525 const EVP_CIPHER *EVP;
530 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
532 EVP = EVP_get_cipherbyname(name);
534 fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
535 else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
538 fprintf(stderr, "devcrypto: cipher %s not available\n", name);
543 static void dump_cipher_info(void)
548 fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
550 #ifndef CIOCGSESSINFO
551 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
553 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
554 name = OBJ_nid2sn(cipher_data[i].nid);
555 fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
556 name ? name : "unknown", cipher_data[i].nid,
557 cipher_data[i].devcryptoid);
558 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
559 fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
562 fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
563 cipher_driver_info[i].driver_name : "unknown");
564 if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
565 fprintf(stderr, "(hw accelerated)");
566 else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
567 fprintf(stderr, "(software)");
569 fprintf(stderr, "(acceleration status unknown)");
570 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
571 fprintf (stderr, ". Cipher setup failed");
572 fprintf(stderr, "\n");
574 fprintf(stderr, "\n");
578 * We only support digests if the cryptodev implementation supports multiple
579 * data updates and session copying. Otherwise, we would be forced to maintain
580 * a cache, which is perilous if there's a lot of data coming in (if someone
581 * wants to checksum an OpenSSL tarball, for example).
583 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
584 #define IMPLEMENT_DIGEST
586 /******************************************************************************
590 * Because they all do the same basic operation, we have only one set of
591 * method functions for them all to share, and a mapping table between
592 * NIDs and cryptodev IDs, with all the necessary size data.
597 struct session_op sess;
598 /* This signals that the init function was called, not that it succeeded. */
600 unsigned char digest_res[HASH_MAX_LEN];
603 static const struct digest_data_st {
608 #ifndef OPENSSL_NO_MD5
609 { NID_md5, 16, CRYPTO_MD5 },
611 { NID_sha1, 20, CRYPTO_SHA1 },
612 #ifndef OPENSSL_NO_RMD160
613 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
614 { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
617 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
618 { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
620 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
621 { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
623 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
624 { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
626 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
627 { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
631 static size_t find_digest_data_index(int nid)
635 for (i = 0; i < OSSL_NELEM(digest_data); i++)
636 if (nid == digest_data[i].nid)
641 static size_t get_digest_data_index(int nid)
643 size_t i = find_digest_data_index(nid);
649 * Code further down must make sure that only NIDs in the table above
650 * are used. If any other NID reaches this function, there's a grave
651 * coding error further down.
653 assert("Code that never should be reached" == NULL);
657 static const struct digest_data_st *get_digest_data(int nid)
659 return &digest_data[get_digest_data_index(nid)];
663 * Following are the five necessary functions to map OpenSSL functionality
664 * with cryptodev: init, update, final, cleanup, and copy.
667 static int digest_init(EVP_MD_CTX *ctx)
669 struct digest_ctx *digest_ctx =
670 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
671 const struct digest_data_st *digest_d =
672 get_digest_data(EVP_MD_CTX_type(ctx));
674 digest_ctx->init_called = 1;
676 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
677 digest_ctx->sess.mac = digest_d->devcryptoid;
678 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
679 SYSerr(SYS_F_IOCTL, errno);
686 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
687 void *res, unsigned int flags)
689 struct crypt_op cryp;
691 memset(&cryp, 0, sizeof(cryp));
692 cryp.ses = ctx->sess.ses;
694 cryp.src = (void *)src;
698 return ioctl(cfd, CIOCCRYPT, &cryp);
701 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
703 struct digest_ctx *digest_ctx =
704 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
709 if (digest_ctx == NULL)
712 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
713 if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
715 } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
719 SYSerr(SYS_F_IOCTL, errno);
723 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
725 struct digest_ctx *digest_ctx =
726 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
728 if (md == NULL || digest_ctx == NULL)
731 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
732 memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
733 } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
734 SYSerr(SYS_F_IOCTL, errno);
741 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
743 struct digest_ctx *digest_from =
744 (struct digest_ctx *)EVP_MD_CTX_md_data(from);
745 struct digest_ctx *digest_to =
746 (struct digest_ctx *)EVP_MD_CTX_md_data(to);
747 struct cphash_op cphash;
749 if (digest_from == NULL || digest_from->init_called != 1)
752 if (!digest_init(to)) {
753 SYSerr(SYS_F_IOCTL, errno);
757 cphash.src_ses = digest_from->sess.ses;
758 cphash.dst_ses = digest_to->sess.ses;
759 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
760 SYSerr(SYS_F_IOCTL, errno);
766 static int digest_cleanup(EVP_MD_CTX *ctx)
768 struct digest_ctx *digest_ctx =
769 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
771 if (digest_ctx == NULL)
773 if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
774 SYSerr(SYS_F_IOCTL, errno);
781 * Keep tables of known nids, associated methods, selected digests, and
783 * Note that known_digest_nids[] isn't necessarily indexed the same way as
784 * digest_data[] above, which the other tables are.
786 static int known_digest_nids[OSSL_NELEM(digest_data)];
787 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
788 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
789 static int selected_digests[OSSL_NELEM(digest_data)];
790 static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
792 static int devcrypto_test_digest(size_t digest_data_index)
794 return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
795 && selected_digests[digest_data_index] == 1
796 && (digest_driver_info[digest_data_index].accelerated
797 == DEVCRYPTO_ACCELERATED
798 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
799 || (digest_driver_info[digest_data_index].accelerated
800 != DEVCRYPTO_NOT_ACCELERATED
801 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
804 static void rebuild_known_digest_nids(ENGINE *e)
808 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
809 if (devcrypto_test_digest(i))
810 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
812 ENGINE_unregister_digests(e);
813 ENGINE_register_digests(e);
816 static void prepare_digest_methods(void)
819 struct session_op sess1, sess2;
821 struct session_info_op siop;
823 struct cphash_op cphash;
825 memset(&digest_driver_info, 0, sizeof(digest_driver_info));
827 memset(&sess1, 0, sizeof(sess1));
828 memset(&sess2, 0, sizeof(sess2));
830 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
833 selected_digests[i] = 1;
836 * Check that the digest is usable
838 sess1.mac = digest_data[i].devcryptoid;
840 if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
841 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
846 /* gather hardware acceleration info from the driver */
847 siop.ses = sess1.ses;
848 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
849 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
851 digest_driver_info[i].driver_name =
852 OPENSSL_strndup(siop.hash_info.cra_driver_name,
853 CRYPTODEV_MAX_ALG_NAME);
854 if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
855 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
857 digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
861 /* digest must be capable of hash state copy */
862 sess2.mac = sess1.mac;
863 if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
864 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
867 cphash.src_ses = sess1.ses;
868 cphash.dst_ses = sess2.ses;
869 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
870 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
873 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
875 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
876 digest_data[i].digestlen)
877 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
878 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
879 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
880 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
881 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
882 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
883 sizeof(struct digest_ctx))) {
884 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
885 EVP_MD_meth_free(known_digest_methods[i]);
886 known_digest_methods[i] = NULL;
889 digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
891 ioctl(cfd, CIOCFSESSION, &sess1.ses);
893 ioctl(cfd, CIOCFSESSION, &sess2.ses);
894 if (devcrypto_test_digest(i))
895 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
899 static const EVP_MD *get_digest_method(int nid)
901 size_t i = get_digest_data_index(nid);
905 return known_digest_methods[i];
908 static int get_digest_nids(const int **nids)
910 *nids = known_digest_nids;
911 return known_digest_nids_amount;
914 static void destroy_digest_method(int nid)
916 size_t i = get_digest_data_index(nid);
918 EVP_MD_meth_free(known_digest_methods[i]);
919 known_digest_methods[i] = NULL;
922 static void destroy_all_digest_methods(void)
926 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
927 destroy_digest_method(digest_data[i].nid);
928 OPENSSL_free(digest_driver_info[i].driver_name);
929 digest_driver_info[i].driver_name = NULL;
933 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
934 const int **nids, int nid)
937 return get_digest_nids(nids);
939 *digest = get_digest_method(nid);
941 return *digest != NULL;
944 static void devcrypto_select_all_digests(int *digest_list)
948 for (i = 0; i < OSSL_NELEM(digest_data); i++)
952 static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
954 int *digest_list = (int *)usr;
961 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
963 EVP = EVP_get_digestbyname(name);
965 fprintf(stderr, "devcrypto: unknown digest %s\n", name);
966 else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
969 fprintf(stderr, "devcrypto: digest %s not available\n", name);
974 static void dump_digest_info(void)
979 fprintf (stderr, "Information about digests supported by the /dev/crypto"
981 #ifndef CIOCGSESSINFO
982 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
985 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
986 name = OBJ_nid2sn(digest_data[i].nid);
987 fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
988 name ? name : "unknown", digest_data[i].nid,
989 digest_data[i].devcryptoid,
990 digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
991 if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
992 fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
995 if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
996 fprintf(stderr, " (hw accelerated)");
997 else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
998 fprintf(stderr, " (software)");
1000 fprintf(stderr, " (acceleration status unknown)");
1001 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
1002 fprintf (stderr, ". Cipher setup failed\n");
1003 else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
1004 fprintf(stderr, ", CIOCCPHASH failed\n");
1006 fprintf(stderr, ", CIOCCPHASH capable\n");
1008 fprintf(stderr, "\n");
1013 /******************************************************************************
1019 #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
1020 #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
1021 #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
1022 #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
1024 static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
1025 #ifdef CIOCGSESSINFO
1026 {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
1028 "specifies whether to use software (not accelerated) drivers ("
1029 OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
1030 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
1031 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
1032 "=use if acceleration can't be determined) [default="
1033 OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
1034 ENGINE_CMD_FLAG_NUMERIC},
1037 {DEVCRYPTO_CMD_CIPHERS,
1039 "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
1040 ENGINE_CMD_FLAG_STRING},
1042 #ifdef IMPLEMENT_DIGEST
1043 {DEVCRYPTO_CMD_DIGESTS,
1045 "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
1046 ENGINE_CMD_FLAG_STRING},
1049 {DEVCRYPTO_CMD_DUMP_INFO,
1051 "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
1052 ENGINE_CMD_FLAG_NO_INPUT},
1057 static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1061 #ifdef CIOCGSESSINFO
1062 case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
1064 case DEVCRYPTO_REQUIRE_ACCELERATED:
1065 case DEVCRYPTO_USE_SOFTWARE:
1066 case DEVCRYPTO_REJECT_SOFTWARE:
1069 fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
1072 if (use_softdrivers == i)
1074 use_softdrivers = i;
1075 #ifdef IMPLEMENT_DIGEST
1076 rebuild_known_digest_nids(e);
1078 rebuild_known_cipher_nids(e);
1080 #endif /* CIOCGSESSINFO */
1082 case DEVCRYPTO_CMD_CIPHERS:
1085 if (strcasecmp((const char *)p, "ALL") == 0) {
1086 devcrypto_select_all_ciphers(selected_ciphers);
1087 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1088 memset(selected_ciphers, 0, sizeof(selected_ciphers));
1090 new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
1091 if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
1092 OPENSSL_free(new_list);
1095 memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
1096 OPENSSL_free(new_list);
1098 rebuild_known_cipher_nids(e);
1101 #ifdef IMPLEMENT_DIGEST
1102 case DEVCRYPTO_CMD_DIGESTS:
1105 if (strcasecmp((const char *)p, "ALL") == 0) {
1106 devcrypto_select_all_digests(selected_digests);
1107 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1108 memset(selected_digests, 0, sizeof(selected_digests));
1110 new_list=OPENSSL_zalloc(sizeof(selected_digests));
1111 if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
1112 OPENSSL_free(new_list);
1115 memcpy(selected_digests, new_list, sizeof(selected_digests));
1116 OPENSSL_free(new_list);
1118 rebuild_known_digest_nids(e);
1120 #endif /* IMPLEMENT_DIGEST */
1122 case DEVCRYPTO_CMD_DUMP_INFO:
1124 #ifdef IMPLEMENT_DIGEST
1135 /******************************************************************************
1141 static int devcrypto_unload(ENGINE *e)
1143 destroy_all_cipher_methods();
1144 #ifdef IMPLEMENT_DIGEST
1145 destroy_all_digest_methods();
1153 * This engine is always built into libcrypto, so it doesn't offer any
1154 * ability to be dynamically loadable.
1156 void engine_load_devcrypto_int()
1160 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
1161 #ifndef ENGINE_DEVCRYPTO_DEBUG
1162 if (errno != ENOENT)
1164 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
1168 if ((e = ENGINE_new()) == NULL
1169 || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
1172 * We know that devcrypto_unload() won't be called when one of the
1173 * above two calls have failed, so we close cfd explicitly here to
1174 * avoid leaking resources.
1180 prepare_cipher_methods();
1181 #ifdef IMPLEMENT_DIGEST
1182 prepare_digest_methods();
1185 if (!ENGINE_set_id(e, "devcrypto")
1186 || !ENGINE_set_name(e, "/dev/crypto engine")
1187 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
1188 || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
1191 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
1192 * implementations, it seems to only exist in FreeBSD, and regarding the
1193 * parameters in its crypt_kop, the manual crypto(4) has this to say:
1195 * The semantics of these arguments are currently undocumented.
1197 * Reading through the FreeBSD source code doesn't give much more than
1198 * their CRK_MOD_EXP implementation for ubsec.
1200 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
1201 * structure as well as the command (CRK_*) in cryptodev.h, but no support
1202 * seems to be implemented at all for the moment.
1204 * At the time of writing, it seems impossible to write proper support for
1205 * FreeBSD's asym features without some very deep knowledge and access to
1206 * specific kernel modules.
1208 * /Richard Levitte, 2017-05-11
1211 # ifndef OPENSSL_NO_RSA
1212 || !ENGINE_set_RSA(e, devcrypto_rsa)
1214 # ifndef OPENSSL_NO_DSA
1215 || !ENGINE_set_DSA(e, devcrypto_dsa)
1217 # ifndef OPENSSL_NO_DH
1218 || !ENGINE_set_DH(e, devcrypto_dh)
1220 # ifndef OPENSSL_NO_EC
1221 || !ENGINE_set_EC(e, devcrypto_ec)
1224 || !ENGINE_set_ciphers(e, devcrypto_ciphers)
1225 #ifdef IMPLEMENT_DIGEST
1226 || !ENGINE_set_digests(e, devcrypto_digests)
1234 ENGINE_free(e); /* Loose our local reference */