2 * Copyright 2017-2023 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
10 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include "internal/e_os.h"
15 #include <sys/types.h>
18 #include <sys/ioctl.h>
22 #include <openssl/conf.h>
23 #include <openssl/evp.h>
24 #include <openssl/err.h>
25 #include <openssl/engine.h>
26 #include <openssl/objects.h>
27 #include "crypto/cryptodev.h"
28 #include "internal/nelem.h"
30 /* #define ENGINE_DEVCRYPTO_DEBUG */
32 #if CRYPTO_ALGORITHM_MIN < CRYPTO_ALGORITHM_MAX
33 # define CHECK_BSD_STYLE_MACROS
36 #define engine_devcrypto_id "devcrypto"
39 * Use session2_op on FreeBSD which permits requesting specific
40 * drivers or classes of drivers at session creation time.
43 typedef struct session2_op session_op_t;
45 typedef struct session_op session_op_t;
49 * ONE global file descriptor for all sessions. This allows operations
50 * such as digest session data copying (see digest_copy()), but is also
51 * saner... why re-open /dev/crypto for every session?
54 #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
55 #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
56 #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
58 #define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
59 static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
62 * cipher/digest status & acceleration definitions
63 * Make sure the defaults are set to 0
65 struct driver_info_st {
66 enum devcrypto_status_t {
67 DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
68 DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
69 DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
70 DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
71 DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
74 enum devcrypto_accelerated_t {
75 DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
76 DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unknown */
77 DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
83 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
84 void engine_load_devcrypto_int(void);
87 static int clean_devcrypto_session(session_op_t *sess) {
88 if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
89 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
92 memset(sess, 0, sizeof(*sess));
96 /******************************************************************************
100 * Because they all do the same basic operation, we have only one set of
101 * method functions for them all to share, and a mapping table between
102 * NIDs and cryptodev IDs, with all the necessary size data.
108 int op; /* COP_ENCRYPT or COP_DECRYPT */
109 unsigned long mode; /* EVP_CIPH_*_MODE */
111 /* to handle ctr mode being a stream cipher */
112 unsigned char partial[EVP_MAX_BLOCK_LENGTH];
113 unsigned int blocksize, num;
116 static const struct cipher_data_st {
124 #ifndef OPENSSL_NO_DES
125 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
126 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
128 #ifndef OPENSSL_NO_BF
129 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
131 #ifndef OPENSSL_NO_CAST
132 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
134 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
135 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
136 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
137 #ifndef OPENSSL_NO_RC4
138 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
140 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
141 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
142 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
143 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
145 #if 0 /* Not yet supported */
146 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
147 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
149 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
150 { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
151 { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
152 { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
154 #if 0 /* Not yet supported */
155 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
156 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
157 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
159 #ifndef OPENSSL_NO_CAMELLIA
160 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
161 CRYPTO_CAMELLIA_CBC },
162 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
163 CRYPTO_CAMELLIA_CBC },
164 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
165 CRYPTO_CAMELLIA_CBC },
169 static size_t find_cipher_data_index(int nid)
173 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
174 if (nid == cipher_data[i].nid)
179 static size_t get_cipher_data_index(int nid)
181 size_t i = find_cipher_data_index(nid);
187 * Code further down must make sure that only NIDs in the table above
188 * are used. If any other NID reaches this function, there's a grave
189 * coding error further down.
191 assert("Code that never should be reached" == NULL);
195 static const struct cipher_data_st *get_cipher_data(int nid)
197 return &cipher_data[get_cipher_data_index(nid)];
201 * Following are the three necessary functions to map OpenSSL functionality
205 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
206 const unsigned char *iv, int enc)
208 struct cipher_ctx *cipher_ctx =
209 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
210 const struct cipher_data_st *cipher_d =
211 get_cipher_data(EVP_CIPHER_CTX_get_nid(ctx));
214 /* cleanup a previous session */
215 if (cipher_ctx->sess.ses != 0 &&
216 clean_devcrypto_session(&cipher_ctx->sess) == 0)
219 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
220 cipher_ctx->sess.keylen = cipher_d->keylen;
221 cipher_ctx->sess.key = (void *)key;
222 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
223 cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
224 cipher_ctx->blocksize = cipher_d->blocksize;
226 cipher_ctx->sess.crid = (use_softdrivers == DEVCRYPTO_USE_SOFTWARE) ?
227 CRYPTO_FLAG_SOFTWARE | CRYPTO_FLAG_HARDWARE :
228 CRYPTO_FLAG_HARDWARE;
229 ret = ioctl(cfd, CIOCGSESSION2, &cipher_ctx->sess);
231 ret = ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess);
234 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
241 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
242 const unsigned char *in, size_t inl)
244 struct cipher_ctx *cipher_ctx =
245 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
246 struct crypt_op cryp;
247 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
248 #if !defined(COP_FLAG_WRITE_IV)
249 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
250 const unsigned char *ivptr;
251 size_t nblocks, ivlen;
254 memset(&cryp, 0, sizeof(cryp));
255 cryp.ses = cipher_ctx->sess.ses;
257 cryp.src = (void *)in;
258 cryp.dst = (void *)out;
259 cryp.iv = (void *)iv;
260 cryp.op = cipher_ctx->op;
261 #if !defined(COP_FLAG_WRITE_IV)
264 ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
266 switch (cipher_ctx->mode) {
267 case EVP_CIPH_CBC_MODE:
268 assert(inl >= ivlen);
269 if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
270 ivptr = in + inl - ivlen;
271 memcpy(saved_iv, ivptr, ivlen);
275 case EVP_CIPH_CTR_MODE:
278 default: /* should not happen */
282 cryp.flags = COP_FLAG_WRITE_IV;
285 if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
286 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
290 #if !defined(COP_FLAG_WRITE_IV)
292 switch (cipher_ctx->mode) {
293 case EVP_CIPH_CBC_MODE:
294 assert(inl >= ivlen);
295 if (EVP_CIPHER_CTX_is_encrypting(ctx))
296 ivptr = out + inl - ivlen;
300 memcpy(iv, ivptr, ivlen);
303 case EVP_CIPH_CTR_MODE:
304 nblocks = (inl + cipher_ctx->blocksize - 1)
305 / cipher_ctx->blocksize;
308 nblocks += iv[ivlen];
309 iv[ivlen] = (uint8_t) nblocks;
314 default: /* should not happen */
322 static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
323 const unsigned char *in, size_t inl)
325 struct cipher_ctx *cipher_ctx =
326 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
329 /* initial partial block */
330 while (cipher_ctx->num && inl) {
331 (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
333 cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
337 if (inl > cipher_ctx->blocksize) {
338 nblocks = inl/cipher_ctx->blocksize;
339 len = nblocks * cipher_ctx->blocksize;
340 if (cipher_do_cipher(ctx, out, in, len) < 1)
347 /* final partial block */
349 memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
350 if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
351 cipher_ctx->blocksize) < 1)
354 out[cipher_ctx->num] = in[cipher_ctx->num]
355 ^ cipher_ctx->partial[cipher_ctx->num];
363 static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
365 struct cipher_ctx *cipher_ctx =
366 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
367 EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
368 struct cipher_ctx *to_cipher_ctx;
373 if (cipher_ctx == NULL)
375 /* when copying the context, a new session needs to be initialized */
377 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(to_ctx);
378 memset(&to_cipher_ctx->sess, 0, sizeof(to_cipher_ctx->sess));
379 return cipher_init(to_ctx, (void *)cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
380 (cipher_ctx->op == COP_ENCRYPT));
383 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
393 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
395 struct cipher_ctx *cipher_ctx =
396 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
398 return clean_devcrypto_session(&cipher_ctx->sess);
402 * Keep tables of known nids, associated methods, selected ciphers, and driver
404 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
405 * cipher_data[] above, which the other tables are.
407 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
408 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
409 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
410 static int selected_ciphers[OSSL_NELEM(cipher_data)];
411 static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
414 static int devcrypto_test_cipher(size_t cipher_data_index)
416 return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
417 && selected_ciphers[cipher_data_index] == 1
418 && (cipher_driver_info[cipher_data_index].accelerated
419 == DEVCRYPTO_ACCELERATED
420 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
421 || (cipher_driver_info[cipher_data_index].accelerated
422 != DEVCRYPTO_NOT_ACCELERATED
423 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
426 static void prepare_cipher_methods(void)
430 unsigned long cipher_mode;
432 struct crypt_find_op fop;
433 enum devcrypto_accelerated_t accelerated;
434 #elif defined(CIOCGSESSINFO)
435 struct session_info_op siop;
438 memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
440 memset(&sess, 0, sizeof(sess));
441 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
443 for (i = 0, known_cipher_nids_amount = 0;
444 i < OSSL_NELEM(cipher_data); i++) {
446 selected_ciphers[i] = 1;
448 * Check that the cipher is usable
450 sess.cipher = cipher_data[i].devcryptoid;
451 sess.keylen = cipher_data[i].keylen;
454 * When using CIOCGSESSION2, first try to allocate a hardware
455 * ("accelerated") session. If that fails, fall back to
456 * allocating a software session.
458 sess.crid = CRYPTO_FLAG_HARDWARE;
459 if (ioctl(cfd, CIOCGSESSION2, &sess) == 0) {
460 accelerated = DEVCRYPTO_ACCELERATED;
462 sess.crid = CRYPTO_FLAG_SOFTWARE;
463 if (ioctl(cfd, CIOCGSESSION2, &sess) < 0) {
464 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
467 accelerated = DEVCRYPTO_NOT_ACCELERATED;
470 if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
471 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
476 cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
478 if ((known_cipher_methods[i] =
479 EVP_CIPHER_meth_new(cipher_data[i].nid,
480 cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
481 cipher_data[i].blocksize,
482 cipher_data[i].keylen)) == NULL
483 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
484 cipher_data[i].ivlen)
485 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
487 | EVP_CIPH_CUSTOM_COPY
489 | EVP_CIPH_FLAG_DEFAULT_ASN1)
490 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
491 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
492 cipher_mode == EVP_CIPH_CTR_MODE ?
495 || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
496 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
498 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
499 sizeof(struct cipher_ctx))) {
500 cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
501 EVP_CIPHER_meth_free(known_cipher_methods[i]);
502 known_cipher_methods[i] = NULL;
504 cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
506 cipher_driver_info[i].accelerated = accelerated;
507 fop.crid = sess.crid;
508 if (ioctl(cfd, CIOCFINDDEV, &fop) == 0) {
509 cipher_driver_info[i].driver_name =
510 OPENSSL_strndup(fop.name, sizeof(fop.name));
512 #elif defined(CIOCGSESSINFO)
514 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
515 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
517 cipher_driver_info[i].driver_name =
518 OPENSSL_strndup(siop.cipher_info.cra_driver_name,
519 CRYPTODEV_MAX_ALG_NAME);
520 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
521 cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
523 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
525 #endif /* CIOCGSESSINFO */
527 ioctl(cfd, CIOCFSESSION, &sess.ses);
528 if (devcrypto_test_cipher(i)) {
529 known_cipher_nids[known_cipher_nids_amount++] =
535 static void rebuild_known_cipher_nids(ENGINE *e)
539 for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
540 if (devcrypto_test_cipher(i))
541 known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
543 ENGINE_unregister_ciphers(e);
544 ENGINE_register_ciphers(e);
547 static const EVP_CIPHER *get_cipher_method(int nid)
549 size_t i = get_cipher_data_index(nid);
553 return known_cipher_methods[i];
556 static int get_cipher_nids(const int **nids)
558 *nids = known_cipher_nids;
559 return known_cipher_nids_amount;
562 static void destroy_cipher_method(int nid)
564 size_t i = get_cipher_data_index(nid);
566 EVP_CIPHER_meth_free(known_cipher_methods[i]);
567 known_cipher_methods[i] = NULL;
570 static void destroy_all_cipher_methods(void)
574 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
575 destroy_cipher_method(cipher_data[i].nid);
576 OPENSSL_free(cipher_driver_info[i].driver_name);
577 cipher_driver_info[i].driver_name = NULL;
581 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
582 const int **nids, int nid)
585 return get_cipher_nids(nids);
587 *cipher = get_cipher_method(nid);
589 return *cipher != NULL;
592 static void devcrypto_select_all_ciphers(int *cipher_list)
596 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
600 static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
602 int *cipher_list = (int *)usr;
604 const EVP_CIPHER *EVP;
609 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
611 EVP = EVP_get_cipherbyname(name);
613 fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
614 else if ((i = find_cipher_data_index(EVP_CIPHER_get_nid(EVP))) != (size_t)-1)
617 fprintf(stderr, "devcrypto: cipher %s not available\n", name);
622 static void dump_cipher_info(void)
627 fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
629 #ifndef CIOCGSESSINFO
630 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
632 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
633 name = OBJ_nid2sn(cipher_data[i].nid);
634 fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
635 name ? name : "unknown", cipher_data[i].nid,
636 cipher_data[i].devcryptoid);
637 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
638 fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
641 fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
642 cipher_driver_info[i].driver_name : "unknown");
643 if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
644 fprintf(stderr, "(hw accelerated)");
645 else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
646 fprintf(stderr, "(software)");
648 fprintf(stderr, "(acceleration status unknown)");
649 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
650 fprintf (stderr, ". Cipher setup failed");
651 fprintf(stderr, "\n");
653 fprintf(stderr, "\n");
657 * We only support digests if the cryptodev implementation supports multiple
658 * data updates and session copying. Otherwise, we would be forced to maintain
659 * a cache, which is perilous if there's a lot of data coming in (if someone
660 * wants to checksum an OpenSSL tarball, for example).
662 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
663 #define IMPLEMENT_DIGEST
665 /******************************************************************************
669 * Because they all do the same basic operation, we have only one set of
670 * method functions for them all to share, and a mapping table between
671 * NIDs and cryptodev IDs, with all the necessary size data.
677 /* This signals that the init function was called, not that it succeeded. */
679 unsigned char digest_res[HASH_MAX_LEN];
682 static const struct digest_data_st {
688 #ifndef OPENSSL_NO_MD5
689 { NID_md5, /* MD5_CBLOCK */ 64, 16, CRYPTO_MD5 },
691 { NID_sha1, SHA_CBLOCK, 20, CRYPTO_SHA1 },
692 #ifndef OPENSSL_NO_RMD160
693 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
694 { NID_ripemd160, /* RIPEMD160_CBLOCK */ 64, 20, CRYPTO_RIPEMD160 },
697 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
698 { NID_sha224, SHA256_CBLOCK, 224 / 8, CRYPTO_SHA2_224 },
700 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
701 { NID_sha256, SHA256_CBLOCK, 256 / 8, CRYPTO_SHA2_256 },
703 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
704 { NID_sha384, SHA512_CBLOCK, 384 / 8, CRYPTO_SHA2_384 },
706 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
707 { NID_sha512, SHA512_CBLOCK, 512 / 8, CRYPTO_SHA2_512 },
711 static size_t find_digest_data_index(int nid)
715 for (i = 0; i < OSSL_NELEM(digest_data); i++)
716 if (nid == digest_data[i].nid)
721 static size_t get_digest_data_index(int nid)
723 size_t i = find_digest_data_index(nid);
729 * Code further down must make sure that only NIDs in the table above
730 * are used. If any other NID reaches this function, there's a grave
731 * coding error further down.
733 assert("Code that never should be reached" == NULL);
737 static const struct digest_data_st *get_digest_data(int nid)
739 return &digest_data[get_digest_data_index(nid)];
743 * Following are the five necessary functions to map OpenSSL functionality
744 * with cryptodev: init, update, final, cleanup, and copy.
747 static int digest_init(EVP_MD_CTX *ctx)
749 struct digest_ctx *digest_ctx =
750 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
751 const struct digest_data_st *digest_d =
752 get_digest_data(EVP_MD_CTX_get_type(ctx));
754 digest_ctx->init_called = 1;
756 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
757 digest_ctx->sess.mac = digest_d->devcryptoid;
758 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
759 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
765 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
766 void *res, unsigned int flags)
768 struct crypt_op cryp;
770 memset(&cryp, 0, sizeof(cryp));
771 cryp.ses = ctx->sess.ses;
773 cryp.src = (void *)src;
777 return ioctl(cfd, CIOCCRYPT, &cryp);
780 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
782 struct digest_ctx *digest_ctx =
783 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
788 if (digest_ctx == NULL)
791 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
792 if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
794 } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
798 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
802 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
804 struct digest_ctx *digest_ctx =
805 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
807 if (md == NULL || digest_ctx == NULL)
810 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
811 memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_get_size(ctx));
812 } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
813 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
820 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
822 struct digest_ctx *digest_from =
823 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(from);
824 struct digest_ctx *digest_to =
825 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(to);
826 struct cphash_op cphash;
828 if (digest_from == NULL || digest_from->init_called != 1)
831 if (!digest_init(to)) {
832 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
836 cphash.src_ses = digest_from->sess.ses;
837 cphash.dst_ses = digest_to->sess.ses;
838 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
839 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
845 static int digest_cleanup(EVP_MD_CTX *ctx)
847 struct digest_ctx *digest_ctx =
848 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
850 if (digest_ctx == NULL)
853 return clean_devcrypto_session(&digest_ctx->sess);
857 * Keep tables of known nids, associated methods, selected digests, and
859 * Note that known_digest_nids[] isn't necessarily indexed the same way as
860 * digest_data[] above, which the other tables are.
862 static int known_digest_nids[OSSL_NELEM(digest_data)];
863 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
864 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
865 static int selected_digests[OSSL_NELEM(digest_data)];
866 static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
868 static int devcrypto_test_digest(size_t digest_data_index)
870 return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
871 && selected_digests[digest_data_index] == 1
872 && (digest_driver_info[digest_data_index].accelerated
873 == DEVCRYPTO_ACCELERATED
874 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
875 || (digest_driver_info[digest_data_index].accelerated
876 != DEVCRYPTO_NOT_ACCELERATED
877 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
880 static void rebuild_known_digest_nids(ENGINE *e)
884 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
885 if (devcrypto_test_digest(i))
886 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
888 ENGINE_unregister_digests(e);
889 ENGINE_register_digests(e);
892 static void prepare_digest_methods(void)
895 session_op_t sess1, sess2;
897 struct session_info_op siop;
899 struct cphash_op cphash;
901 memset(&digest_driver_info, 0, sizeof(digest_driver_info));
903 memset(&sess1, 0, sizeof(sess1));
904 memset(&sess2, 0, sizeof(sess2));
906 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
909 selected_digests[i] = 1;
912 * Check that the digest is usable
914 sess1.mac = digest_data[i].devcryptoid;
916 if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
917 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
922 /* gather hardware acceleration info from the driver */
923 siop.ses = sess1.ses;
924 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
925 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
927 digest_driver_info[i].driver_name =
928 OPENSSL_strndup(siop.hash_info.cra_driver_name,
929 CRYPTODEV_MAX_ALG_NAME);
930 if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
931 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
933 digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
937 /* digest must be capable of hash state copy */
938 sess2.mac = sess1.mac;
939 if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
940 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
943 cphash.src_ses = sess1.ses;
944 cphash.dst_ses = sess2.ses;
945 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
946 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
949 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
951 || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i],
952 digest_data[i].blocksize)
953 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
954 digest_data[i].digestlen)
955 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
956 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
957 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
958 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
959 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
960 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
961 sizeof(struct digest_ctx))) {
962 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
963 EVP_MD_meth_free(known_digest_methods[i]);
964 known_digest_methods[i] = NULL;
967 digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
969 ioctl(cfd, CIOCFSESSION, &sess1.ses);
971 ioctl(cfd, CIOCFSESSION, &sess2.ses);
972 if (devcrypto_test_digest(i))
973 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
977 static const EVP_MD *get_digest_method(int nid)
979 size_t i = get_digest_data_index(nid);
983 return known_digest_methods[i];
986 static int get_digest_nids(const int **nids)
988 *nids = known_digest_nids;
989 return known_digest_nids_amount;
992 static void destroy_digest_method(int nid)
994 size_t i = get_digest_data_index(nid);
996 EVP_MD_meth_free(known_digest_methods[i]);
997 known_digest_methods[i] = NULL;
1000 static void destroy_all_digest_methods(void)
1004 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
1005 destroy_digest_method(digest_data[i].nid);
1006 OPENSSL_free(digest_driver_info[i].driver_name);
1007 digest_driver_info[i].driver_name = NULL;
1011 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
1012 const int **nids, int nid)
1015 return get_digest_nids(nids);
1017 *digest = get_digest_method(nid);
1019 return *digest != NULL;
1022 static void devcrypto_select_all_digests(int *digest_list)
1026 for (i = 0; i < OSSL_NELEM(digest_data); i++)
1030 static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
1032 int *digest_list = (int *)usr;
1039 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
1041 EVP = EVP_get_digestbyname(name);
1043 fprintf(stderr, "devcrypto: unknown digest %s\n", name);
1044 else if ((i = find_digest_data_index(EVP_MD_get_type(EVP))) != (size_t)-1)
1047 fprintf(stderr, "devcrypto: digest %s not available\n", name);
1052 static void dump_digest_info(void)
1057 fprintf (stderr, "Information about digests supported by the /dev/crypto"
1059 #ifndef CIOCGSESSINFO
1060 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
1063 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
1064 name = OBJ_nid2sn(digest_data[i].nid);
1065 fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
1066 name ? name : "unknown", digest_data[i].nid,
1067 digest_data[i].devcryptoid,
1068 digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
1069 if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
1070 fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
1073 if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
1074 fprintf(stderr, " (hw accelerated)");
1075 else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
1076 fprintf(stderr, " (software)");
1078 fprintf(stderr, " (acceleration status unknown)");
1079 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
1080 fprintf (stderr, ". Cipher setup failed\n");
1081 else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
1082 fprintf(stderr, ", CIOCCPHASH failed\n");
1084 fprintf(stderr, ", CIOCCPHASH capable\n");
1086 fprintf(stderr, "\n");
1091 /******************************************************************************
1097 #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
1098 #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
1099 #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
1100 #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
1102 static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
1103 #if defined(CIOCGSESSINFO) || defined(CIOCGSESSION2)
1104 {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
1106 "specifies whether to use software (not accelerated) drivers ("
1107 OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
1108 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
1109 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
1110 "=use if acceleration can't be determined) [default="
1111 OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
1112 ENGINE_CMD_FLAG_NUMERIC},
1115 {DEVCRYPTO_CMD_CIPHERS,
1117 "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
1118 ENGINE_CMD_FLAG_STRING},
1120 #ifdef IMPLEMENT_DIGEST
1121 {DEVCRYPTO_CMD_DIGESTS,
1123 "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
1124 ENGINE_CMD_FLAG_STRING},
1127 {DEVCRYPTO_CMD_DUMP_INFO,
1129 "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
1130 ENGINE_CMD_FLAG_NO_INPUT},
1135 static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1139 #if defined(CIOCGSESSINFO) || defined(CIOCGSESSION2)
1140 case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
1142 case DEVCRYPTO_REQUIRE_ACCELERATED:
1143 case DEVCRYPTO_USE_SOFTWARE:
1144 case DEVCRYPTO_REJECT_SOFTWARE:
1147 fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
1150 if (use_softdrivers == i)
1152 use_softdrivers = i;
1153 #ifdef IMPLEMENT_DIGEST
1154 rebuild_known_digest_nids(e);
1156 rebuild_known_cipher_nids(e);
1158 #endif /* CIOCGSESSINFO || CIOCGSESSION2 */
1160 case DEVCRYPTO_CMD_CIPHERS:
1163 if (OPENSSL_strcasecmp((const char *)p, "ALL") == 0) {
1164 devcrypto_select_all_ciphers(selected_ciphers);
1165 } else if (OPENSSL_strcasecmp((const char*)p, "NONE") == 0) {
1166 memset(selected_ciphers, 0, sizeof(selected_ciphers));
1168 new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
1169 if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
1170 OPENSSL_free(new_list);
1173 memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
1174 OPENSSL_free(new_list);
1176 rebuild_known_cipher_nids(e);
1179 #ifdef IMPLEMENT_DIGEST
1180 case DEVCRYPTO_CMD_DIGESTS:
1183 if (OPENSSL_strcasecmp((const char *)p, "ALL") == 0) {
1184 devcrypto_select_all_digests(selected_digests);
1185 } else if (OPENSSL_strcasecmp((const char*)p, "NONE") == 0) {
1186 memset(selected_digests, 0, sizeof(selected_digests));
1188 new_list=OPENSSL_zalloc(sizeof(selected_digests));
1189 if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
1190 OPENSSL_free(new_list);
1193 memcpy(selected_digests, new_list, sizeof(selected_digests));
1194 OPENSSL_free(new_list);
1196 rebuild_known_digest_nids(e);
1198 #endif /* IMPLEMENT_DIGEST */
1200 case DEVCRYPTO_CMD_DUMP_INFO:
1202 #ifdef IMPLEMENT_DIGEST
1213 /******************************************************************************
1222 static int open_devcrypto(void)
1229 if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
1230 #ifndef ENGINE_DEVCRYPTO_DEBUG
1231 if (errno != ENOENT && errno != ENXIO)
1233 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
1238 if (ioctl(fd, CRIOGET, &cfd) < 0) {
1239 fprintf(stderr, "Could not create crypto fd: %s\n", strerror(errno));
1252 static int close_devcrypto(void)
1261 fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
1267 static int devcrypto_unload(ENGINE *e)
1269 destroy_all_cipher_methods();
1270 #ifdef IMPLEMENT_DIGEST
1271 destroy_all_digest_methods();
1279 static int bind_devcrypto(ENGINE *e) {
1281 if (!ENGINE_set_id(e, engine_devcrypto_id)
1282 || !ENGINE_set_name(e, "/dev/crypto engine")
1283 || !ENGINE_set_destroy_function(e, devcrypto_unload)
1284 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
1285 || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
1288 prepare_cipher_methods();
1289 #ifdef IMPLEMENT_DIGEST
1290 prepare_digest_methods();
1293 return (ENGINE_set_ciphers(e, devcrypto_ciphers)
1294 #ifdef IMPLEMENT_DIGEST
1295 && ENGINE_set_digests(e, devcrypto_digests)
1298 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
1299 * implementations, it seems to only exist in FreeBSD, and regarding the
1300 * parameters in its crypt_kop, the manual crypto(4) has this to say:
1302 * The semantics of these arguments are currently undocumented.
1304 * Reading through the FreeBSD source code doesn't give much more than
1305 * their CRK_MOD_EXP implementation for ubsec.
1307 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
1308 * structure as well as the command (CRK_*) in cryptodev.h, but no support
1309 * seems to be implemented at all for the moment.
1311 * At the time of writing, it seems impossible to write proper support for
1312 * FreeBSD's asym features without some very deep knowledge and access to
1313 * specific kernel modules.
1315 * /Richard Levitte, 2017-05-11
1318 && ENGINE_set_RSA(e, devcrypto_rsa)
1319 # ifndef OPENSSL_NO_DSA
1320 && ENGINE_set_DSA(e, devcrypto_dsa)
1322 # ifndef OPENSSL_NO_DH
1323 && ENGINE_set_DH(e, devcrypto_dh)
1325 # ifndef OPENSSL_NO_EC
1326 && ENGINE_set_EC(e, devcrypto_ec)
1332 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
1334 * In case this engine is built into libcrypto, then it doesn't offer any
1335 * ability to be dynamically loadable.
1337 void engine_load_devcrypto_int(void)
1341 if (!open_devcrypto())
1344 if ((e = ENGINE_new()) == NULL
1345 || !bind_devcrypto(e)) {
1354 * If the "add" worked, it gets a structural reference. So either way, we
1355 * release our just-created reference.
1357 ENGINE_free(e); /* Loose our local reference */
1359 * If the "add" didn't work, it was probably a conflict because it was
1360 * already added (eg. someone calling ENGINE_load_blah then calling
1361 * ENGINE_load_builtin_engines() perhaps).
1368 static int bind_helper(ENGINE *e, const char *id)
1370 if ((id && (strcmp(id, engine_devcrypto_id) != 0))
1371 || !open_devcrypto())
1373 if (!bind_devcrypto(e)) {
1380 IMPLEMENT_DYNAMIC_CHECK_FN()
1381 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)