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 /* #define ENGINE_DEVCRYPTO_DEBUG */
28 #if CRYPTO_ALGORITHM_MIN < CRYPTO_ALGORITHM_MAX
29 # define CHECK_BSD_STYLE_MACROS
32 #define engine_devcrypto_id "devcrypto"
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_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
45 static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
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 unknown */
63 DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
69 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
70 void engine_load_devcrypto_int(void);
73 static int clean_devcrypto_session(struct session_op *sess) {
74 if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
75 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
78 memset(sess, 0, sizeof(struct session_op));
82 /******************************************************************************
86 * Because they all do the same basic operation, we have only one set of
87 * method functions for them all to share, and a mapping table between
88 * NIDs and cryptodev IDs, with all the necessary size data.
93 struct session_op sess;
94 int op; /* COP_ENCRYPT or COP_DECRYPT */
95 unsigned long mode; /* EVP_CIPH_*_MODE */
97 /* to handle ctr mode being a stream cipher */
98 unsigned char partial[EVP_MAX_BLOCK_LENGTH];
99 unsigned int blocksize, num;
102 static const struct cipher_data_st {
110 #ifndef OPENSSL_NO_DES
111 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
112 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
114 #ifndef OPENSSL_NO_BF
115 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
117 #ifndef OPENSSL_NO_CAST
118 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
120 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
121 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
122 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
123 #ifndef OPENSSL_NO_RC4
124 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
126 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
127 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
128 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
129 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
131 #if 0 /* Not yet supported */
132 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
133 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
135 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
136 { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
137 { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
138 { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
140 #if 0 /* Not yet supported */
141 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
142 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
143 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
145 #ifndef OPENSSL_NO_CAMELLIA
146 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
147 CRYPTO_CAMELLIA_CBC },
148 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
149 CRYPTO_CAMELLIA_CBC },
150 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
151 CRYPTO_CAMELLIA_CBC },
155 static size_t find_cipher_data_index(int nid)
159 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
160 if (nid == cipher_data[i].nid)
165 static size_t get_cipher_data_index(int nid)
167 size_t i = find_cipher_data_index(nid);
173 * Code further down must make sure that only NIDs in the table above
174 * are used. If any other NID reaches this function, there's a grave
175 * coding error further down.
177 assert("Code that never should be reached" == NULL);
181 static const struct cipher_data_st *get_cipher_data(int nid)
183 return &cipher_data[get_cipher_data_index(nid)];
187 * Following are the three necessary functions to map OpenSSL functionality
191 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
192 const unsigned char *iv, int enc)
194 struct cipher_ctx *cipher_ctx =
195 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
196 const struct cipher_data_st *cipher_d =
197 get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
199 /* cleanup a previous session */
200 if (cipher_ctx->sess.ses != 0 &&
201 clean_devcrypto_session(&cipher_ctx->sess) == 0)
204 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
205 cipher_ctx->sess.keylen = cipher_d->keylen;
206 cipher_ctx->sess.key = (void *)key;
207 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
208 cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
209 cipher_ctx->blocksize = cipher_d->blocksize;
210 if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
211 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
218 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
219 const unsigned char *in, size_t inl)
221 struct cipher_ctx *cipher_ctx =
222 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
223 struct crypt_op cryp;
224 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
225 #if !defined(COP_FLAG_WRITE_IV)
226 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
227 const unsigned char *ivptr;
228 size_t nblocks, ivlen;
231 memset(&cryp, 0, sizeof(cryp));
232 cryp.ses = cipher_ctx->sess.ses;
234 cryp.src = (void *)in;
235 cryp.dst = (void *)out;
236 cryp.iv = (void *)iv;
237 cryp.op = cipher_ctx->op;
238 #if !defined(COP_FLAG_WRITE_IV)
241 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
243 switch (cipher_ctx->mode) {
244 case EVP_CIPH_CBC_MODE:
245 assert(inl >= ivlen);
246 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
247 ivptr = in + inl - ivlen;
248 memcpy(saved_iv, ivptr, ivlen);
252 case EVP_CIPH_CTR_MODE:
255 default: /* should not happen */
259 cryp.flags = COP_FLAG_WRITE_IV;
262 if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
263 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
267 #if !defined(COP_FLAG_WRITE_IV)
269 switch (cipher_ctx->mode) {
270 case EVP_CIPH_CBC_MODE:
271 assert(inl >= ivlen);
272 if (EVP_CIPHER_CTX_encrypting(ctx))
273 ivptr = out + inl - ivlen;
277 memcpy(iv, ivptr, ivlen);
280 case EVP_CIPH_CTR_MODE:
281 nblocks = (inl + cipher_ctx->blocksize - 1)
282 / cipher_ctx->blocksize;
285 nblocks += iv[ivlen];
286 iv[ivlen] = (uint8_t) nblocks;
291 default: /* should not happen */
299 static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
300 const unsigned char *in, size_t inl)
302 struct cipher_ctx *cipher_ctx =
303 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
306 /* initial partial block */
307 while (cipher_ctx->num && inl) {
308 (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
310 cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
314 if (inl > (unsigned int) cipher_ctx->blocksize) {
315 nblocks = inl/cipher_ctx->blocksize;
316 len = nblocks * cipher_ctx->blocksize;
317 if (cipher_do_cipher(ctx, out, in, len) < 1)
324 /* final partial block */
326 memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
327 if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
328 cipher_ctx->blocksize) < 1)
331 out[cipher_ctx->num] = in[cipher_ctx->num]
332 ^ cipher_ctx->partial[cipher_ctx->num];
340 static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
342 struct cipher_ctx *cipher_ctx =
343 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
344 EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
345 struct cipher_ctx *to_cipher_ctx;
350 if (cipher_ctx == NULL)
352 /* when copying the context, a new session needs to be initialized */
354 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(to_ctx);
355 memset(&to_cipher_ctx->sess, 0, sizeof(to_cipher_ctx->sess));
356 return cipher_init(to_ctx, (void *)cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
357 (cipher_ctx->op == COP_ENCRYPT));
360 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
370 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
372 struct cipher_ctx *cipher_ctx =
373 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
375 return clean_devcrypto_session(&cipher_ctx->sess);
379 * Keep tables of known nids, associated methods, selected ciphers, and driver
381 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
382 * cipher_data[] above, which the other tables are.
384 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
385 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
386 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
387 static int selected_ciphers[OSSL_NELEM(cipher_data)];
388 static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
391 static int devcrypto_test_cipher(size_t cipher_data_index)
393 return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
394 && selected_ciphers[cipher_data_index] == 1
395 && (cipher_driver_info[cipher_data_index].accelerated
396 == DEVCRYPTO_ACCELERATED
397 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
398 || (cipher_driver_info[cipher_data_index].accelerated
399 != DEVCRYPTO_NOT_ACCELERATED
400 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
403 static void prepare_cipher_methods(void)
406 struct session_op sess;
407 unsigned long cipher_mode;
409 struct session_info_op siop;
412 memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
414 memset(&sess, 0, sizeof(sess));
415 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
417 for (i = 0, known_cipher_nids_amount = 0;
418 i < OSSL_NELEM(cipher_data); i++) {
420 selected_ciphers[i] = 1;
422 * Check that the cipher is usable
424 sess.cipher = cipher_data[i].devcryptoid;
425 sess.keylen = cipher_data[i].keylen;
426 if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
427 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
431 cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
433 if ((known_cipher_methods[i] =
434 EVP_CIPHER_meth_new(cipher_data[i].nid,
435 cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
436 cipher_data[i].blocksize,
437 cipher_data[i].keylen)) == NULL
438 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
439 cipher_data[i].ivlen)
440 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
442 | EVP_CIPH_CUSTOM_COPY
444 | EVP_CIPH_FLAG_DEFAULT_ASN1)
445 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
446 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
447 cipher_mode == EVP_CIPH_CTR_MODE ?
450 || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
451 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
453 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
454 sizeof(struct cipher_ctx))) {
455 cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
456 EVP_CIPHER_meth_free(known_cipher_methods[i]);
457 known_cipher_methods[i] = NULL;
459 cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
462 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
463 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
465 cipher_driver_info[i].driver_name =
466 OPENSSL_strndup(siop.cipher_info.cra_driver_name,
467 CRYPTODEV_MAX_ALG_NAME);
468 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
469 cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
471 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
473 #endif /* CIOCGSESSINFO */
475 ioctl(cfd, CIOCFSESSION, &sess.ses);
476 if (devcrypto_test_cipher(i)) {
477 known_cipher_nids[known_cipher_nids_amount++] =
483 static void rebuild_known_cipher_nids(ENGINE *e)
487 for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
488 if (devcrypto_test_cipher(i))
489 known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
491 ENGINE_unregister_ciphers(e);
492 ENGINE_register_ciphers(e);
495 static const EVP_CIPHER *get_cipher_method(int nid)
497 size_t i = get_cipher_data_index(nid);
501 return known_cipher_methods[i];
504 static int get_cipher_nids(const int **nids)
506 *nids = known_cipher_nids;
507 return known_cipher_nids_amount;
510 static void destroy_cipher_method(int nid)
512 size_t i = get_cipher_data_index(nid);
514 EVP_CIPHER_meth_free(known_cipher_methods[i]);
515 known_cipher_methods[i] = NULL;
518 static void destroy_all_cipher_methods(void)
522 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
523 destroy_cipher_method(cipher_data[i].nid);
524 OPENSSL_free(cipher_driver_info[i].driver_name);
525 cipher_driver_info[i].driver_name = NULL;
529 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
530 const int **nids, int nid)
533 return get_cipher_nids(nids);
535 *cipher = get_cipher_method(nid);
537 return *cipher != NULL;
540 static void devcrypto_select_all_ciphers(int *cipher_list)
544 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
548 static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
550 int *cipher_list = (int *)usr;
552 const EVP_CIPHER *EVP;
557 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
559 EVP = EVP_get_cipherbyname(name);
561 fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
562 else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
565 fprintf(stderr, "devcrypto: cipher %s not available\n", name);
570 static void dump_cipher_info(void)
575 fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
577 #ifndef CIOCGSESSINFO
578 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
580 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
581 name = OBJ_nid2sn(cipher_data[i].nid);
582 fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
583 name ? name : "unknown", cipher_data[i].nid,
584 cipher_data[i].devcryptoid);
585 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
586 fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
589 fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
590 cipher_driver_info[i].driver_name : "unknown");
591 if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
592 fprintf(stderr, "(hw accelerated)");
593 else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
594 fprintf(stderr, "(software)");
596 fprintf(stderr, "(acceleration status unknown)");
597 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
598 fprintf (stderr, ". Cipher setup failed");
599 fprintf(stderr, "\n");
601 fprintf(stderr, "\n");
605 * We only support digests if the cryptodev implementation supports multiple
606 * data updates and session copying. Otherwise, we would be forced to maintain
607 * a cache, which is perilous if there's a lot of data coming in (if someone
608 * wants to checksum an OpenSSL tarball, for example).
610 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
611 #define IMPLEMENT_DIGEST
613 /******************************************************************************
617 * Because they all do the same basic operation, we have only one set of
618 * method functions for them all to share, and a mapping table between
619 * NIDs and cryptodev IDs, with all the necessary size data.
624 struct session_op sess;
625 /* This signals that the init function was called, not that it succeeded. */
627 unsigned char digest_res[HASH_MAX_LEN];
630 static const struct digest_data_st {
636 #ifndef OPENSSL_NO_MD5
637 { NID_md5, /* MD5_CBLOCK */ 64, 16, CRYPTO_MD5 },
639 { NID_sha1, SHA_CBLOCK, 20, CRYPTO_SHA1 },
640 #ifndef OPENSSL_NO_RMD160
641 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
642 { NID_ripemd160, /* RIPEMD160_CBLOCK */ 64, 20, CRYPTO_RIPEMD160 },
645 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
646 { NID_sha224, SHA256_CBLOCK, 224 / 8, CRYPTO_SHA2_224 },
648 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
649 { NID_sha256, SHA256_CBLOCK, 256 / 8, CRYPTO_SHA2_256 },
651 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
652 { NID_sha384, SHA512_CBLOCK, 384 / 8, CRYPTO_SHA2_384 },
654 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
655 { NID_sha512, SHA512_CBLOCK, 512 / 8, CRYPTO_SHA2_512 },
659 static size_t find_digest_data_index(int nid)
663 for (i = 0; i < OSSL_NELEM(digest_data); i++)
664 if (nid == digest_data[i].nid)
669 static size_t get_digest_data_index(int nid)
671 size_t i = find_digest_data_index(nid);
677 * Code further down must make sure that only NIDs in the table above
678 * are used. If any other NID reaches this function, there's a grave
679 * coding error further down.
681 assert("Code that never should be reached" == NULL);
685 static const struct digest_data_st *get_digest_data(int nid)
687 return &digest_data[get_digest_data_index(nid)];
691 * Following are the five necessary functions to map OpenSSL functionality
692 * with cryptodev: init, update, final, cleanup, and copy.
695 static int digest_init(EVP_MD_CTX *ctx)
697 struct digest_ctx *digest_ctx =
698 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
699 const struct digest_data_st *digest_d =
700 get_digest_data(EVP_MD_CTX_type(ctx));
702 digest_ctx->init_called = 1;
704 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
705 digest_ctx->sess.mac = digest_d->devcryptoid;
706 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
707 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
713 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
714 void *res, unsigned int flags)
716 struct crypt_op cryp;
718 memset(&cryp, 0, sizeof(cryp));
719 cryp.ses = ctx->sess.ses;
721 cryp.src = (void *)src;
725 return ioctl(cfd, CIOCCRYPT, &cryp);
728 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
730 struct digest_ctx *digest_ctx =
731 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
736 if (digest_ctx == NULL)
739 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
740 if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
742 } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
746 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
750 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
752 struct digest_ctx *digest_ctx =
753 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
755 if (md == NULL || digest_ctx == NULL)
758 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
759 memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
760 } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
761 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
768 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
770 struct digest_ctx *digest_from =
771 (struct digest_ctx *)EVP_MD_CTX_md_data(from);
772 struct digest_ctx *digest_to =
773 (struct digest_ctx *)EVP_MD_CTX_md_data(to);
774 struct cphash_op cphash;
776 if (digest_from == NULL || digest_from->init_called != 1)
779 if (!digest_init(to)) {
780 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
784 cphash.src_ses = digest_from->sess.ses;
785 cphash.dst_ses = digest_to->sess.ses;
786 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
787 ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()");
793 static int digest_cleanup(EVP_MD_CTX *ctx)
795 struct digest_ctx *digest_ctx =
796 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
798 if (digest_ctx == NULL)
801 return clean_devcrypto_session(&digest_ctx->sess);
805 * Keep tables of known nids, associated methods, selected digests, and
807 * Note that known_digest_nids[] isn't necessarily indexed the same way as
808 * digest_data[] above, which the other tables are.
810 static int known_digest_nids[OSSL_NELEM(digest_data)];
811 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
812 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
813 static int selected_digests[OSSL_NELEM(digest_data)];
814 static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
816 static int devcrypto_test_digest(size_t digest_data_index)
818 return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
819 && selected_digests[digest_data_index] == 1
820 && (digest_driver_info[digest_data_index].accelerated
821 == DEVCRYPTO_ACCELERATED
822 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
823 || (digest_driver_info[digest_data_index].accelerated
824 != DEVCRYPTO_NOT_ACCELERATED
825 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
828 static void rebuild_known_digest_nids(ENGINE *e)
832 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
833 if (devcrypto_test_digest(i))
834 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
836 ENGINE_unregister_digests(e);
837 ENGINE_register_digests(e);
840 static void prepare_digest_methods(void)
843 struct session_op sess1, sess2;
845 struct session_info_op siop;
847 struct cphash_op cphash;
849 memset(&digest_driver_info, 0, sizeof(digest_driver_info));
851 memset(&sess1, 0, sizeof(sess1));
852 memset(&sess2, 0, sizeof(sess2));
854 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
857 selected_digests[i] = 1;
860 * Check that the digest is usable
862 sess1.mac = digest_data[i].devcryptoid;
864 if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
865 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
870 /* gather hardware acceleration info from the driver */
871 siop.ses = sess1.ses;
872 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
873 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
875 digest_driver_info[i].driver_name =
876 OPENSSL_strndup(siop.hash_info.cra_driver_name,
877 CRYPTODEV_MAX_ALG_NAME);
878 if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
879 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
881 digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
885 /* digest must be capable of hash state copy */
886 sess2.mac = sess1.mac;
887 if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
888 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
891 cphash.src_ses = sess1.ses;
892 cphash.dst_ses = sess2.ses;
893 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
894 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
897 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
899 || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i],
900 digest_data[i].blocksize)
901 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
902 digest_data[i].digestlen)
903 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
904 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
905 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
906 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
907 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
908 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
909 sizeof(struct digest_ctx))) {
910 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
911 EVP_MD_meth_free(known_digest_methods[i]);
912 known_digest_methods[i] = NULL;
915 digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
917 ioctl(cfd, CIOCFSESSION, &sess1.ses);
919 ioctl(cfd, CIOCFSESSION, &sess2.ses);
920 if (devcrypto_test_digest(i))
921 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
925 static const EVP_MD *get_digest_method(int nid)
927 size_t i = get_digest_data_index(nid);
931 return known_digest_methods[i];
934 static int get_digest_nids(const int **nids)
936 *nids = known_digest_nids;
937 return known_digest_nids_amount;
940 static void destroy_digest_method(int nid)
942 size_t i = get_digest_data_index(nid);
944 EVP_MD_meth_free(known_digest_methods[i]);
945 known_digest_methods[i] = NULL;
948 static void destroy_all_digest_methods(void)
952 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
953 destroy_digest_method(digest_data[i].nid);
954 OPENSSL_free(digest_driver_info[i].driver_name);
955 digest_driver_info[i].driver_name = NULL;
959 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
960 const int **nids, int nid)
963 return get_digest_nids(nids);
965 *digest = get_digest_method(nid);
967 return *digest != NULL;
970 static void devcrypto_select_all_digests(int *digest_list)
974 for (i = 0; i < OSSL_NELEM(digest_data); i++)
978 static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
980 int *digest_list = (int *)usr;
987 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
989 EVP = EVP_get_digestbyname(name);
991 fprintf(stderr, "devcrypto: unknown digest %s\n", name);
992 else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
995 fprintf(stderr, "devcrypto: digest %s not available\n", name);
1000 static void dump_digest_info(void)
1005 fprintf (stderr, "Information about digests supported by the /dev/crypto"
1007 #ifndef CIOCGSESSINFO
1008 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
1011 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
1012 name = OBJ_nid2sn(digest_data[i].nid);
1013 fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
1014 name ? name : "unknown", digest_data[i].nid,
1015 digest_data[i].devcryptoid,
1016 digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
1017 if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
1018 fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
1021 if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
1022 fprintf(stderr, " (hw accelerated)");
1023 else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
1024 fprintf(stderr, " (software)");
1026 fprintf(stderr, " (acceleration status unknown)");
1027 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
1028 fprintf (stderr, ". Cipher setup failed\n");
1029 else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
1030 fprintf(stderr, ", CIOCCPHASH failed\n");
1032 fprintf(stderr, ", CIOCCPHASH capable\n");
1034 fprintf(stderr, "\n");
1039 /******************************************************************************
1045 #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
1046 #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
1047 #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
1048 #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
1050 static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
1051 #ifdef CIOCGSESSINFO
1052 {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
1054 "specifies whether to use software (not accelerated) drivers ("
1055 OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
1056 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
1057 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
1058 "=use if acceleration can't be determined) [default="
1059 OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
1060 ENGINE_CMD_FLAG_NUMERIC},
1063 {DEVCRYPTO_CMD_CIPHERS,
1065 "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
1066 ENGINE_CMD_FLAG_STRING},
1068 #ifdef IMPLEMENT_DIGEST
1069 {DEVCRYPTO_CMD_DIGESTS,
1071 "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
1072 ENGINE_CMD_FLAG_STRING},
1075 {DEVCRYPTO_CMD_DUMP_INFO,
1077 "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
1078 ENGINE_CMD_FLAG_NO_INPUT},
1083 static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1087 #ifdef CIOCGSESSINFO
1088 case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
1090 case DEVCRYPTO_REQUIRE_ACCELERATED:
1091 case DEVCRYPTO_USE_SOFTWARE:
1092 case DEVCRYPTO_REJECT_SOFTWARE:
1095 fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
1098 if (use_softdrivers == i)
1100 use_softdrivers = i;
1101 #ifdef IMPLEMENT_DIGEST
1102 rebuild_known_digest_nids(e);
1104 rebuild_known_cipher_nids(e);
1106 #endif /* CIOCGSESSINFO */
1108 case DEVCRYPTO_CMD_CIPHERS:
1111 if (strcasecmp((const char *)p, "ALL") == 0) {
1112 devcrypto_select_all_ciphers(selected_ciphers);
1113 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1114 memset(selected_ciphers, 0, sizeof(selected_ciphers));
1116 new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
1117 if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
1118 OPENSSL_free(new_list);
1121 memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
1122 OPENSSL_free(new_list);
1124 rebuild_known_cipher_nids(e);
1127 #ifdef IMPLEMENT_DIGEST
1128 case DEVCRYPTO_CMD_DIGESTS:
1131 if (strcasecmp((const char *)p, "ALL") == 0) {
1132 devcrypto_select_all_digests(selected_digests);
1133 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1134 memset(selected_digests, 0, sizeof(selected_digests));
1136 new_list=OPENSSL_zalloc(sizeof(selected_digests));
1137 if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
1138 OPENSSL_free(new_list);
1141 memcpy(selected_digests, new_list, sizeof(selected_digests));
1142 OPENSSL_free(new_list);
1144 rebuild_known_digest_nids(e);
1146 #endif /* IMPLEMENT_DIGEST */
1148 case DEVCRYPTO_CMD_DUMP_INFO:
1150 #ifdef IMPLEMENT_DIGEST
1161 /******************************************************************************
1170 static int open_devcrypto(void)
1175 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
1176 #ifndef ENGINE_DEVCRYPTO_DEBUG
1177 if (errno != ENOENT)
1179 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
1186 static int close_devcrypto(void)
1195 fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
1201 static int devcrypto_unload(ENGINE *e)
1203 destroy_all_cipher_methods();
1204 #ifdef IMPLEMENT_DIGEST
1205 destroy_all_digest_methods();
1213 static int bind_devcrypto(ENGINE *e) {
1215 if (!ENGINE_set_id(e, engine_devcrypto_id)
1216 || !ENGINE_set_name(e, "/dev/crypto engine")
1217 || !ENGINE_set_destroy_function(e, devcrypto_unload)
1218 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
1219 || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
1222 prepare_cipher_methods();
1223 #ifdef IMPLEMENT_DIGEST
1224 prepare_digest_methods();
1227 return (ENGINE_set_ciphers(e, devcrypto_ciphers)
1228 #ifdef IMPLEMENT_DIGEST
1229 && ENGINE_set_digests(e, devcrypto_digests)
1232 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
1233 * implementations, it seems to only exist in FreeBSD, and regarding the
1234 * parameters in its crypt_kop, the manual crypto(4) has this to say:
1236 * The semantics of these arguments are currently undocumented.
1238 * Reading through the FreeBSD source code doesn't give much more than
1239 * their CRK_MOD_EXP implementation for ubsec.
1241 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
1242 * structure as well as the command (CRK_*) in cryptodev.h, but no support
1243 * seems to be implemented at all for the moment.
1245 * At the time of writing, it seems impossible to write proper support for
1246 * FreeBSD's asym features without some very deep knowledge and access to
1247 * specific kernel modules.
1249 * /Richard Levitte, 2017-05-11
1252 # ifndef OPENSSL_NO_RSA
1253 && ENGINE_set_RSA(e, devcrypto_rsa)
1255 # ifndef OPENSSL_NO_DSA
1256 && ENGINE_set_DSA(e, devcrypto_dsa)
1258 # ifndef OPENSSL_NO_DH
1259 && ENGINE_set_DH(e, devcrypto_dh)
1261 # ifndef OPENSSL_NO_EC
1262 && ENGINE_set_EC(e, devcrypto_ec)
1268 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
1270 * In case this engine is built into libcrypto, then it doesn't offer any
1271 * ability to be dynamically loadable.
1273 void engine_load_devcrypto_int(void)
1277 if (!open_devcrypto())
1280 if ((e = ENGINE_new()) == NULL
1281 || !bind_devcrypto(e)) {
1288 ENGINE_free(e); /* Loose our local reference */
1294 static int bind_helper(ENGINE *e, const char *id)
1296 if ((id && (strcmp(id, engine_devcrypto_id) != 0))
1297 || !open_devcrypto())
1299 if (!bind_devcrypto(e)) {
1306 IMPLEMENT_DYNAMIC_CHECK_FN()
1307 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)