2 * Copyright 2017-2021 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
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"
29 /* #define ENGINE_DEVCRYPTO_DEBUG */
31 #if CRYPTO_ALGORITHM_MIN < CRYPTO_ALGORITHM_MAX
32 # define CHECK_BSD_STYLE_MACROS
35 #define engine_devcrypto_id "devcrypto"
38 * Use session2_op on FreeBSD which permits requesting specific
39 * drivers or classes of drivers at session creation time.
42 typedef struct session2_op session_op_t;
44 typedef struct session_op session_op_t;
48 * ONE global file descriptor for all sessions. This allows operations
49 * such as digest session data copying (see digest_copy()), but is also
50 * saner... why re-open /dev/crypto for every session?
53 #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
54 #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
55 #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
57 #define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
58 static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
61 * cipher/digest status & acceleration definitions
62 * Make sure the defaults are set to 0
64 struct driver_info_st {
65 enum devcrypto_status_t {
66 DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
67 DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
68 DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
69 DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
70 DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
73 enum devcrypto_accelerated_t {
74 DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
75 DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unknown */
76 DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
82 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
83 void engine_load_devcrypto_int(void);
86 static int clean_devcrypto_session(session_op_t *sess) {
87 if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
88 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
91 memset(sess, 0, sizeof(*sess));
95 /******************************************************************************
99 * Because they all do the same basic operation, we have only one set of
100 * method functions for them all to share, and a mapping table between
101 * NIDs and cryptodev IDs, with all the necessary size data.
107 int op; /* COP_ENCRYPT or COP_DECRYPT */
108 unsigned long mode; /* EVP_CIPH_*_MODE */
110 /* to handle ctr mode being a stream cipher */
111 unsigned char partial[EVP_MAX_BLOCK_LENGTH];
112 unsigned int blocksize, num;
115 static const struct cipher_data_st {
123 #ifndef OPENSSL_NO_DES
124 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
125 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
127 #ifndef OPENSSL_NO_BF
128 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
130 #ifndef OPENSSL_NO_CAST
131 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
133 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
134 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
135 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
136 #ifndef OPENSSL_NO_RC4
137 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
139 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
140 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
141 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
142 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
144 #if 0 /* Not yet supported */
145 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
146 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
148 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
149 { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
150 { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
151 { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
153 #if 0 /* Not yet supported */
154 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
155 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
156 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
158 #ifndef OPENSSL_NO_CAMELLIA
159 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
160 CRYPTO_CAMELLIA_CBC },
161 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
162 CRYPTO_CAMELLIA_CBC },
163 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
164 CRYPTO_CAMELLIA_CBC },
168 static size_t find_cipher_data_index(int nid)
172 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
173 if (nid == cipher_data[i].nid)
178 static size_t get_cipher_data_index(int nid)
180 size_t i = find_cipher_data_index(nid);
186 * Code further down must make sure that only NIDs in the table above
187 * are used. If any other NID reaches this function, there's a grave
188 * coding error further down.
190 assert("Code that never should be reached" == NULL);
194 static const struct cipher_data_st *get_cipher_data(int nid)
196 return &cipher_data[get_cipher_data_index(nid)];
200 * Following are the three necessary functions to map OpenSSL functionality
204 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
205 const unsigned char *iv, int enc)
207 struct cipher_ctx *cipher_ctx =
208 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
209 const struct cipher_data_st *cipher_d =
210 get_cipher_data(EVP_CIPHER_CTX_get_nid(ctx));
213 /* cleanup a previous session */
214 if (cipher_ctx->sess.ses != 0 &&
215 clean_devcrypto_session(&cipher_ctx->sess) == 0)
218 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
219 cipher_ctx->sess.keylen = cipher_d->keylen;
220 cipher_ctx->sess.key = (void *)key;
221 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
222 cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
223 cipher_ctx->blocksize = cipher_d->blocksize;
225 cipher_ctx->sess.crid = (use_softdrivers == DEVCRYPTO_USE_SOFTWARE) ?
226 CRYPTO_FLAG_SOFTWARE | CRYPTO_FLAG_HARDWARE :
227 CRYPTO_FLAG_HARDWARE;
228 ret = ioctl(cfd, CIOCGSESSION2, &cipher_ctx->sess);
230 ret = ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess);
233 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
240 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
241 const unsigned char *in, size_t inl)
243 struct cipher_ctx *cipher_ctx =
244 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
245 struct crypt_op cryp;
246 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
247 #if !defined(COP_FLAG_WRITE_IV)
248 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
249 const unsigned char *ivptr;
250 size_t nblocks, ivlen;
253 memset(&cryp, 0, sizeof(cryp));
254 cryp.ses = cipher_ctx->sess.ses;
256 cryp.src = (void *)in;
257 cryp.dst = (void *)out;
258 cryp.iv = (void *)iv;
259 cryp.op = cipher_ctx->op;
260 #if !defined(COP_FLAG_WRITE_IV)
263 ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
265 switch (cipher_ctx->mode) {
266 case EVP_CIPH_CBC_MODE:
267 assert(inl >= ivlen);
268 if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
269 ivptr = in + inl - ivlen;
270 memcpy(saved_iv, ivptr, ivlen);
274 case EVP_CIPH_CTR_MODE:
277 default: /* should not happen */
281 cryp.flags = COP_FLAG_WRITE_IV;
284 if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
285 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
289 #if !defined(COP_FLAG_WRITE_IV)
291 switch (cipher_ctx->mode) {
292 case EVP_CIPH_CBC_MODE:
293 assert(inl >= ivlen);
294 if (EVP_CIPHER_CTX_is_encrypting(ctx))
295 ivptr = out + inl - ivlen;
299 memcpy(iv, ivptr, ivlen);
302 case EVP_CIPH_CTR_MODE:
303 nblocks = (inl + cipher_ctx->blocksize - 1)
304 / cipher_ctx->blocksize;
307 nblocks += iv[ivlen];
308 iv[ivlen] = (uint8_t) nblocks;
313 default: /* should not happen */
321 static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
322 const unsigned char *in, size_t inl)
324 struct cipher_ctx *cipher_ctx =
325 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
328 /* initial partial block */
329 while (cipher_ctx->num && inl) {
330 (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
332 cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
336 if (inl > (unsigned int) cipher_ctx->blocksize) {
337 nblocks = inl/cipher_ctx->blocksize;
338 len = nblocks * cipher_ctx->blocksize;
339 if (cipher_do_cipher(ctx, out, in, len) < 1)
346 /* final partial block */
348 memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
349 if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
350 cipher_ctx->blocksize) < 1)
353 out[cipher_ctx->num] = in[cipher_ctx->num]
354 ^ cipher_ctx->partial[cipher_ctx->num];
362 static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
364 struct cipher_ctx *cipher_ctx =
365 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
366 EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
367 struct cipher_ctx *to_cipher_ctx;
372 if (cipher_ctx == NULL)
374 /* when copying the context, a new session needs to be initialized */
376 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(to_ctx);
377 memset(&to_cipher_ctx->sess, 0, sizeof(to_cipher_ctx->sess));
378 return cipher_init(to_ctx, (void *)cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
379 (cipher_ctx->op == COP_ENCRYPT));
382 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
392 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
394 struct cipher_ctx *cipher_ctx =
395 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
397 return clean_devcrypto_session(&cipher_ctx->sess);
401 * Keep tables of known nids, associated methods, selected ciphers, and driver
403 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
404 * cipher_data[] above, which the other tables are.
406 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
407 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
408 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
409 static int selected_ciphers[OSSL_NELEM(cipher_data)];
410 static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
413 static int devcrypto_test_cipher(size_t cipher_data_index)
415 return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
416 && selected_ciphers[cipher_data_index] == 1
417 && (cipher_driver_info[cipher_data_index].accelerated
418 == DEVCRYPTO_ACCELERATED
419 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
420 || (cipher_driver_info[cipher_data_index].accelerated
421 != DEVCRYPTO_NOT_ACCELERATED
422 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
425 static void prepare_cipher_methods(void)
429 unsigned long cipher_mode;
431 struct crypt_find_op fop;
432 enum devcrypto_accelerated_t accelerated;
433 #elif defined(CIOCGSESSINFO)
434 struct session_info_op siop;
437 memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
439 memset(&sess, 0, sizeof(sess));
440 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
442 for (i = 0, known_cipher_nids_amount = 0;
443 i < OSSL_NELEM(cipher_data); i++) {
445 selected_ciphers[i] = 1;
447 * Check that the cipher is usable
449 sess.cipher = cipher_data[i].devcryptoid;
450 sess.keylen = cipher_data[i].keylen;
453 * When using CIOCGSESSION2, first try to allocate a hardware
454 * ("accelerated") session. If that fails, fall back to
455 * allocating a software session.
457 sess.crid = CRYPTO_FLAG_HARDWARE;
458 if (ioctl(cfd, CIOCGSESSION2, &sess) == 0) {
459 accelerated = DEVCRYPTO_ACCELERATED;
461 sess.crid = CRYPTO_FLAG_SOFTWARE;
462 if (ioctl(cfd, CIOCGSESSION2, &sess) < 0) {
463 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
466 accelerated = DEVCRYPTO_NOT_ACCELERATED;
469 if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
470 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
475 cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
477 if ((known_cipher_methods[i] =
478 EVP_CIPHER_meth_new(cipher_data[i].nid,
479 cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
480 cipher_data[i].blocksize,
481 cipher_data[i].keylen)) == NULL
482 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
483 cipher_data[i].ivlen)
484 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
486 | EVP_CIPH_CUSTOM_COPY
488 | EVP_CIPH_FLAG_DEFAULT_ASN1)
489 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
490 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
491 cipher_mode == EVP_CIPH_CTR_MODE ?
494 || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
495 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
497 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
498 sizeof(struct cipher_ctx))) {
499 cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
500 EVP_CIPHER_meth_free(known_cipher_methods[i]);
501 known_cipher_methods[i] = NULL;
503 cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
505 cipher_driver_info[i].accelerated = accelerated;
506 fop.crid = sess.crid;
507 if (ioctl(cfd, CIOCFINDDEV, &fop) == 0) {
508 cipher_driver_info[i].driver_name =
509 OPENSSL_strndup(fop.name, sizeof(fop.name));
511 #elif defined(CIOCGSESSINFO)
513 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
514 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
516 cipher_driver_info[i].driver_name =
517 OPENSSL_strndup(siop.cipher_info.cra_driver_name,
518 CRYPTODEV_MAX_ALG_NAME);
519 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
520 cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
522 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
524 #endif /* CIOCGSESSINFO */
526 ioctl(cfd, CIOCFSESSION, &sess.ses);
527 if (devcrypto_test_cipher(i)) {
528 known_cipher_nids[known_cipher_nids_amount++] =
534 static void rebuild_known_cipher_nids(ENGINE *e)
538 for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
539 if (devcrypto_test_cipher(i))
540 known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
542 ENGINE_unregister_ciphers(e);
543 ENGINE_register_ciphers(e);
546 static const EVP_CIPHER *get_cipher_method(int nid)
548 size_t i = get_cipher_data_index(nid);
552 return known_cipher_methods[i];
555 static int get_cipher_nids(const int **nids)
557 *nids = known_cipher_nids;
558 return known_cipher_nids_amount;
561 static void destroy_cipher_method(int nid)
563 size_t i = get_cipher_data_index(nid);
565 EVP_CIPHER_meth_free(known_cipher_methods[i]);
566 known_cipher_methods[i] = NULL;
569 static void destroy_all_cipher_methods(void)
573 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
574 destroy_cipher_method(cipher_data[i].nid);
575 OPENSSL_free(cipher_driver_info[i].driver_name);
576 cipher_driver_info[i].driver_name = NULL;
580 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
581 const int **nids, int nid)
584 return get_cipher_nids(nids);
586 *cipher = get_cipher_method(nid);
588 return *cipher != NULL;
591 static void devcrypto_select_all_ciphers(int *cipher_list)
595 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
599 static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
601 int *cipher_list = (int *)usr;
603 const EVP_CIPHER *EVP;
608 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
610 EVP = EVP_get_cipherbyname(name);
612 fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
613 else if ((i = find_cipher_data_index(EVP_CIPHER_get_nid(EVP))) != (size_t)-1)
616 fprintf(stderr, "devcrypto: cipher %s not available\n", name);
621 static void dump_cipher_info(void)
626 fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
628 #ifndef CIOCGSESSINFO
629 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
631 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
632 name = OBJ_nid2sn(cipher_data[i].nid);
633 fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
634 name ? name : "unknown", cipher_data[i].nid,
635 cipher_data[i].devcryptoid);
636 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
637 fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
640 fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
641 cipher_driver_info[i].driver_name : "unknown");
642 if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
643 fprintf(stderr, "(hw accelerated)");
644 else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
645 fprintf(stderr, "(software)");
647 fprintf(stderr, "(acceleration status unknown)");
648 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
649 fprintf (stderr, ". Cipher setup failed");
650 fprintf(stderr, "\n");
652 fprintf(stderr, "\n");
656 * We only support digests if the cryptodev implementation supports multiple
657 * data updates and session copying. Otherwise, we would be forced to maintain
658 * a cache, which is perilous if there's a lot of data coming in (if someone
659 * wants to checksum an OpenSSL tarball, for example).
661 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
662 #define IMPLEMENT_DIGEST
664 /******************************************************************************
668 * Because they all do the same basic operation, we have only one set of
669 * method functions for them all to share, and a mapping table between
670 * NIDs and cryptodev IDs, with all the necessary size data.
676 /* This signals that the init function was called, not that it succeeded. */
678 unsigned char digest_res[HASH_MAX_LEN];
681 static const struct digest_data_st {
687 #ifndef OPENSSL_NO_MD5
688 { NID_md5, /* MD5_CBLOCK */ 64, 16, CRYPTO_MD5 },
690 { NID_sha1, SHA_CBLOCK, 20, CRYPTO_SHA1 },
691 #ifndef OPENSSL_NO_RMD160
692 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
693 { NID_ripemd160, /* RIPEMD160_CBLOCK */ 64, 20, CRYPTO_RIPEMD160 },
696 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
697 { NID_sha224, SHA256_CBLOCK, 224 / 8, CRYPTO_SHA2_224 },
699 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
700 { NID_sha256, SHA256_CBLOCK, 256 / 8, CRYPTO_SHA2_256 },
702 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
703 { NID_sha384, SHA512_CBLOCK, 384 / 8, CRYPTO_SHA2_384 },
705 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
706 { NID_sha512, SHA512_CBLOCK, 512 / 8, CRYPTO_SHA2_512 },
710 static size_t find_digest_data_index(int nid)
714 for (i = 0; i < OSSL_NELEM(digest_data); i++)
715 if (nid == digest_data[i].nid)
720 static size_t get_digest_data_index(int nid)
722 size_t i = find_digest_data_index(nid);
728 * Code further down must make sure that only NIDs in the table above
729 * are used. If any other NID reaches this function, there's a grave
730 * coding error further down.
732 assert("Code that never should be reached" == NULL);
736 static const struct digest_data_st *get_digest_data(int nid)
738 return &digest_data[get_digest_data_index(nid)];
742 * Following are the five necessary functions to map OpenSSL functionality
743 * with cryptodev: init, update, final, cleanup, and copy.
746 static int digest_init(EVP_MD_CTX *ctx)
748 struct digest_ctx *digest_ctx =
749 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
750 const struct digest_data_st *digest_d =
751 get_digest_data(EVP_MD_CTX_get_type(ctx));
753 digest_ctx->init_called = 1;
755 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
756 digest_ctx->sess.mac = digest_d->devcryptoid;
757 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
758 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
764 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
765 void *res, unsigned int flags)
767 struct crypt_op cryp;
769 memset(&cryp, 0, sizeof(cryp));
770 cryp.ses = ctx->sess.ses;
772 cryp.src = (void *)src;
776 return ioctl(cfd, CIOCCRYPT, &cryp);
779 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
781 struct digest_ctx *digest_ctx =
782 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
787 if (digest_ctx == NULL)
790 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
791 if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
793 } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
797 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
801 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
803 struct digest_ctx *digest_ctx =
804 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
806 if (md == NULL || digest_ctx == NULL)
809 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
810 memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_get_size(ctx));
811 } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
812 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
819 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
821 struct digest_ctx *digest_from =
822 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(from);
823 struct digest_ctx *digest_to =
824 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(to);
825 struct cphash_op cphash;
827 if (digest_from == NULL || digest_from->init_called != 1)
830 if (!digest_init(to)) {
831 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
835 cphash.src_ses = digest_from->sess.ses;
836 cphash.dst_ses = digest_to->sess.ses;
837 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
838 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
844 static int digest_cleanup(EVP_MD_CTX *ctx)
846 struct digest_ctx *digest_ctx =
847 (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx);
849 if (digest_ctx == NULL)
852 return clean_devcrypto_session(&digest_ctx->sess);
856 * Keep tables of known nids, associated methods, selected digests, and
858 * Note that known_digest_nids[] isn't necessarily indexed the same way as
859 * digest_data[] above, which the other tables are.
861 static int known_digest_nids[OSSL_NELEM(digest_data)];
862 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
863 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
864 static int selected_digests[OSSL_NELEM(digest_data)];
865 static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
867 static int devcrypto_test_digest(size_t digest_data_index)
869 return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
870 && selected_digests[digest_data_index] == 1
871 && (digest_driver_info[digest_data_index].accelerated
872 == DEVCRYPTO_ACCELERATED
873 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
874 || (digest_driver_info[digest_data_index].accelerated
875 != DEVCRYPTO_NOT_ACCELERATED
876 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
879 static void rebuild_known_digest_nids(ENGINE *e)
883 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
884 if (devcrypto_test_digest(i))
885 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
887 ENGINE_unregister_digests(e);
888 ENGINE_register_digests(e);
891 static void prepare_digest_methods(void)
894 session_op_t sess1, sess2;
896 struct session_info_op siop;
898 struct cphash_op cphash;
900 memset(&digest_driver_info, 0, sizeof(digest_driver_info));
902 memset(&sess1, 0, sizeof(sess1));
903 memset(&sess2, 0, sizeof(sess2));
905 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
908 selected_digests[i] = 1;
911 * Check that the digest is usable
913 sess1.mac = digest_data[i].devcryptoid;
915 if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
916 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
921 /* gather hardware acceleration info from the driver */
922 siop.ses = sess1.ses;
923 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
924 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
926 digest_driver_info[i].driver_name =
927 OPENSSL_strndup(siop.hash_info.cra_driver_name,
928 CRYPTODEV_MAX_ALG_NAME);
929 if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
930 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
932 digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
936 /* digest must be capable of hash state copy */
937 sess2.mac = sess1.mac;
938 if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
939 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
942 cphash.src_ses = sess1.ses;
943 cphash.dst_ses = sess2.ses;
944 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
945 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
948 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
950 || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i],
951 digest_data[i].blocksize)
952 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
953 digest_data[i].digestlen)
954 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
955 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
956 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
957 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
958 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
959 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
960 sizeof(struct digest_ctx))) {
961 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
962 EVP_MD_meth_free(known_digest_methods[i]);
963 known_digest_methods[i] = NULL;
966 digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
968 ioctl(cfd, CIOCFSESSION, &sess1.ses);
970 ioctl(cfd, CIOCFSESSION, &sess2.ses);
971 if (devcrypto_test_digest(i))
972 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
976 static const EVP_MD *get_digest_method(int nid)
978 size_t i = get_digest_data_index(nid);
982 return known_digest_methods[i];
985 static int get_digest_nids(const int **nids)
987 *nids = known_digest_nids;
988 return known_digest_nids_amount;
991 static void destroy_digest_method(int nid)
993 size_t i = get_digest_data_index(nid);
995 EVP_MD_meth_free(known_digest_methods[i]);
996 known_digest_methods[i] = NULL;
999 static void destroy_all_digest_methods(void)
1003 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
1004 destroy_digest_method(digest_data[i].nid);
1005 OPENSSL_free(digest_driver_info[i].driver_name);
1006 digest_driver_info[i].driver_name = NULL;
1010 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
1011 const int **nids, int nid)
1014 return get_digest_nids(nids);
1016 *digest = get_digest_method(nid);
1018 return *digest != NULL;
1021 static void devcrypto_select_all_digests(int *digest_list)
1025 for (i = 0; i < OSSL_NELEM(digest_data); i++)
1029 static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
1031 int *digest_list = (int *)usr;
1038 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
1040 EVP = EVP_get_digestbyname(name);
1042 fprintf(stderr, "devcrypto: unknown digest %s\n", name);
1043 else if ((i = find_digest_data_index(EVP_MD_get_type(EVP))) != (size_t)-1)
1046 fprintf(stderr, "devcrypto: digest %s not available\n", name);
1051 static void dump_digest_info(void)
1056 fprintf (stderr, "Information about digests supported by the /dev/crypto"
1058 #ifndef CIOCGSESSINFO
1059 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
1062 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
1063 name = OBJ_nid2sn(digest_data[i].nid);
1064 fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
1065 name ? name : "unknown", digest_data[i].nid,
1066 digest_data[i].devcryptoid,
1067 digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
1068 if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
1069 fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
1072 if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
1073 fprintf(stderr, " (hw accelerated)");
1074 else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
1075 fprintf(stderr, " (software)");
1077 fprintf(stderr, " (acceleration status unknown)");
1078 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
1079 fprintf (stderr, ". Cipher setup failed\n");
1080 else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
1081 fprintf(stderr, ", CIOCCPHASH failed\n");
1083 fprintf(stderr, ", CIOCCPHASH capable\n");
1085 fprintf(stderr, "\n");
1090 /******************************************************************************
1096 #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
1097 #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
1098 #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
1099 #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
1101 static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
1102 #if defined(CIOCGSESSINFO) || defined(CIOCGSESSION2)
1103 {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
1105 "specifies whether to use software (not accelerated) drivers ("
1106 OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
1107 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
1108 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
1109 "=use if acceleration can't be determined) [default="
1110 OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
1111 ENGINE_CMD_FLAG_NUMERIC},
1114 {DEVCRYPTO_CMD_CIPHERS,
1116 "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
1117 ENGINE_CMD_FLAG_STRING},
1119 #ifdef IMPLEMENT_DIGEST
1120 {DEVCRYPTO_CMD_DIGESTS,
1122 "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
1123 ENGINE_CMD_FLAG_STRING},
1126 {DEVCRYPTO_CMD_DUMP_INFO,
1128 "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
1129 ENGINE_CMD_FLAG_NO_INPUT},
1134 static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1138 #if defined(CIOCGSESSINFO) || defined(CIOCGSESSION2)
1139 case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
1141 case DEVCRYPTO_REQUIRE_ACCELERATED:
1142 case DEVCRYPTO_USE_SOFTWARE:
1143 case DEVCRYPTO_REJECT_SOFTWARE:
1146 fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
1149 if (use_softdrivers == i)
1151 use_softdrivers = i;
1152 #ifdef IMPLEMENT_DIGEST
1153 rebuild_known_digest_nids(e);
1155 rebuild_known_cipher_nids(e);
1157 #endif /* CIOCGSESSINFO || CIOCGSESSION2 */
1159 case DEVCRYPTO_CMD_CIPHERS:
1162 if (strcasecmp((const char *)p, "ALL") == 0) {
1163 devcrypto_select_all_ciphers(selected_ciphers);
1164 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1165 memset(selected_ciphers, 0, sizeof(selected_ciphers));
1167 new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
1168 if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
1169 OPENSSL_free(new_list);
1172 memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
1173 OPENSSL_free(new_list);
1175 rebuild_known_cipher_nids(e);
1178 #ifdef IMPLEMENT_DIGEST
1179 case DEVCRYPTO_CMD_DIGESTS:
1182 if (strcasecmp((const char *)p, "ALL") == 0) {
1183 devcrypto_select_all_digests(selected_digests);
1184 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1185 memset(selected_digests, 0, sizeof(selected_digests));
1187 new_list=OPENSSL_zalloc(sizeof(selected_digests));
1188 if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
1189 OPENSSL_free(new_list);
1192 memcpy(selected_digests, new_list, sizeof(selected_digests));
1193 OPENSSL_free(new_list);
1195 rebuild_known_digest_nids(e);
1197 #endif /* IMPLEMENT_DIGEST */
1199 case DEVCRYPTO_CMD_DUMP_INFO:
1201 #ifdef IMPLEMENT_DIGEST
1212 /******************************************************************************
1221 static int open_devcrypto(void)
1228 if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
1229 #ifndef ENGINE_DEVCRYPTO_DEBUG
1230 if (errno != ENOENT)
1232 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
1237 if (ioctl(fd, CRIOGET, &cfd) < 0) {
1238 fprintf(stderr, "Could not create crypto fd: %s\n", strerror(errno));
1251 static int close_devcrypto(void)
1260 fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
1266 static int devcrypto_unload(ENGINE *e)
1268 destroy_all_cipher_methods();
1269 #ifdef IMPLEMENT_DIGEST
1270 destroy_all_digest_methods();
1278 static int bind_devcrypto(ENGINE *e) {
1280 if (!ENGINE_set_id(e, engine_devcrypto_id)
1281 || !ENGINE_set_name(e, "/dev/crypto engine")
1282 || !ENGINE_set_destroy_function(e, devcrypto_unload)
1283 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
1284 || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
1287 prepare_cipher_methods();
1288 #ifdef IMPLEMENT_DIGEST
1289 prepare_digest_methods();
1292 return (ENGINE_set_ciphers(e, devcrypto_ciphers)
1293 #ifdef IMPLEMENT_DIGEST
1294 && ENGINE_set_digests(e, devcrypto_digests)
1297 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
1298 * implementations, it seems to only exist in FreeBSD, and regarding the
1299 * parameters in its crypt_kop, the manual crypto(4) has this to say:
1301 * The semantics of these arguments are currently undocumented.
1303 * Reading through the FreeBSD source code doesn't give much more than
1304 * their CRK_MOD_EXP implementation for ubsec.
1306 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
1307 * structure as well as the command (CRK_*) in cryptodev.h, but no support
1308 * seems to be implemented at all for the moment.
1310 * At the time of writing, it seems impossible to write proper support for
1311 * FreeBSD's asym features without some very deep knowledge and access to
1312 * specific kernel modules.
1314 * /Richard Levitte, 2017-05-11
1317 && ENGINE_set_RSA(e, devcrypto_rsa)
1318 # ifndef OPENSSL_NO_DSA
1319 && ENGINE_set_DSA(e, devcrypto_dsa)
1321 # ifndef OPENSSL_NO_DH
1322 && ENGINE_set_DH(e, devcrypto_dh)
1324 # ifndef OPENSSL_NO_EC
1325 && ENGINE_set_EC(e, devcrypto_ec)
1331 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
1333 * In case this engine is built into libcrypto, then it doesn't offer any
1334 * ability to be dynamically loadable.
1336 void engine_load_devcrypto_int(void)
1340 if (!open_devcrypto())
1343 if ((e = ENGINE_new()) == NULL
1344 || !bind_devcrypto(e)) {
1353 * If the "add" worked, it gets a structural reference. So either way, we
1354 * release our just-created reference.
1356 ENGINE_free(e); /* Loose our local reference */
1358 * If the "add" didn't work, it was probably a conflict because it was
1359 * already added (eg. someone calling ENGINE_load_blah then calling
1360 * ENGINE_load_builtin_engines() perhaps).
1367 static int bind_helper(ENGINE *e, const char *id)
1369 if ((id && (strcmp(id, engine_devcrypto_id) != 0))
1370 || !open_devcrypto())
1372 if (!bind_devcrypto(e)) {
1379 IMPLEMENT_DYNAMIC_CHECK_FN()
1380 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)