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 #ifdef CRYPTO_ALGORITHM_MIN
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_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
45 static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
48 * cipher/digest status & acceleration definitions
49 * Make sure the defaults are set to 0
51 struct driver_info_st {
52 enum devcrypto_status_t {
53 DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
54 DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
55 DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
56 DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
57 DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
60 enum devcrypto_accelerated_t {
61 DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
62 DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
63 DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
69 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
70 void engine_load_devcrypto_int(void);
73 /******************************************************************************
77 * Because they all do the same basic operation, we have only one set of
78 * method functions for them all to share, and a mapping table between
79 * NIDs and cryptodev IDs, with all the necessary size data.
84 struct session_op sess;
85 int op; /* COP_ENCRYPT or COP_DECRYPT */
86 unsigned long mode; /* EVP_CIPH_*_MODE */
88 /* to handle ctr mode being a stream cipher */
89 unsigned char partial[EVP_MAX_BLOCK_LENGTH];
90 unsigned int blocksize, num;
93 static const struct cipher_data_st {
101 #ifndef OPENSSL_NO_DES
102 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
103 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
105 #ifndef OPENSSL_NO_BF
106 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
108 #ifndef OPENSSL_NO_CAST
109 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
111 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
112 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
113 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
114 #ifndef OPENSSL_NO_RC4
115 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
117 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
118 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
119 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
120 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
122 #if 0 /* Not yet supported */
123 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
124 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
126 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
127 { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
128 { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
129 { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
131 #if 0 /* Not yet supported */
132 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
133 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
134 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
136 #ifndef OPENSSL_NO_CAMELLIA
137 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
138 CRYPTO_CAMELLIA_CBC },
139 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
140 CRYPTO_CAMELLIA_CBC },
141 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
142 CRYPTO_CAMELLIA_CBC },
146 static size_t find_cipher_data_index(int nid)
150 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
151 if (nid == cipher_data[i].nid)
156 static size_t get_cipher_data_index(int nid)
158 size_t i = find_cipher_data_index(nid);
164 * Code further down must make sure that only NIDs in the table above
165 * are used. If any other NID reaches this function, there's a grave
166 * coding error further down.
168 assert("Code that never should be reached" == NULL);
172 static const struct cipher_data_st *get_cipher_data(int nid)
174 return &cipher_data[get_cipher_data_index(nid)];
178 * Following are the three necessary functions to map OpenSSL functionality
182 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
183 const unsigned char *iv, int enc)
185 struct cipher_ctx *cipher_ctx =
186 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
187 const struct cipher_data_st *cipher_d =
188 get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
190 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
191 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
192 cipher_ctx->sess.keylen = cipher_d->keylen;
193 cipher_ctx->sess.key = (void *)key;
194 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
195 cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
196 cipher_ctx->blocksize = cipher_d->blocksize;
197 if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
198 SYSerr(SYS_F_IOCTL, errno);
205 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
206 const unsigned char *in, size_t inl)
208 struct cipher_ctx *cipher_ctx =
209 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
210 struct crypt_op cryp;
211 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
212 #if !defined(COP_FLAG_WRITE_IV)
213 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
214 const unsigned char *ivptr;
215 size_t nblocks, ivlen;
218 memset(&cryp, 0, sizeof(cryp));
219 cryp.ses = cipher_ctx->sess.ses;
221 cryp.src = (void *)in;
222 cryp.dst = (void *)out;
223 cryp.iv = (void *)iv;
224 cryp.op = cipher_ctx->op;
225 #if !defined(COP_FLAG_WRITE_IV)
228 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
230 switch (cipher_ctx->mode) {
231 case EVP_CIPH_CBC_MODE:
232 assert(inl >= ivlen);
233 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
234 ivptr = in + inl - ivlen;
235 memcpy(saved_iv, ivptr, ivlen);
239 case EVP_CIPH_CTR_MODE:
242 default: /* should not happen */
246 cryp.flags = COP_FLAG_WRITE_IV;
249 if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
250 SYSerr(SYS_F_IOCTL, errno);
254 #if !defined(COP_FLAG_WRITE_IV)
256 switch (cipher_ctx->mode) {
257 case EVP_CIPH_CBC_MODE:
258 assert(inl >= ivlen);
259 if (EVP_CIPHER_CTX_encrypting(ctx))
260 ivptr = out + inl - ivlen;
264 memcpy(iv, ivptr, ivlen);
267 case EVP_CIPH_CTR_MODE:
268 nblocks = (inl + cipher_ctx->blocksize - 1)
269 / cipher_ctx->blocksize;
272 nblocks += iv[ivlen];
273 iv[ivlen] = (uint8_t) nblocks;
278 default: /* should not happen */
286 static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
287 const unsigned char *in, size_t inl)
289 struct cipher_ctx *cipher_ctx =
290 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
293 /* initial partial block */
294 while (cipher_ctx->num && inl) {
295 (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
297 cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
301 if (inl > (unsigned int) cipher_ctx->blocksize) {
302 nblocks = inl/cipher_ctx->blocksize;
303 len = nblocks * cipher_ctx->blocksize;
304 if (cipher_do_cipher(ctx, out, in, len) < 1)
311 /* final partial block */
313 memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
314 if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
315 cipher_ctx->blocksize) < 1)
318 out[cipher_ctx->num] = in[cipher_ctx->num]
319 ^ cipher_ctx->partial[cipher_ctx->num];
327 static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
329 EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
330 struct cipher_ctx *cipher_ctx;
332 if (type == EVP_CTRL_COPY) {
333 /* when copying the context, a new session needs to be initialized */
334 cipher_ctx = (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
335 return (cipher_ctx == NULL)
336 || cipher_init(to_ctx, cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
337 (cipher_ctx->op == COP_ENCRYPT));
343 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
345 struct cipher_ctx *cipher_ctx =
346 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
348 if (ioctl(cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
349 SYSerr(SYS_F_IOCTL, errno);
357 * Keep tables of known nids, associated methods, selected ciphers, and driver
359 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
360 * cipher_data[] above, which the other tables are.
362 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
363 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
364 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
365 static int selected_ciphers[OSSL_NELEM(cipher_data)];
366 static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
369 static int devcrypto_test_cipher(size_t cipher_data_index)
371 return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
372 && selected_ciphers[cipher_data_index] == 1
373 && (cipher_driver_info[cipher_data_index].accelerated
374 == DEVCRYPTO_ACCELERATED
375 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
376 || (cipher_driver_info[cipher_data_index].accelerated
377 != DEVCRYPTO_NOT_ACCELERATED
378 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
381 static void prepare_cipher_methods(void)
384 struct session_op sess;
385 unsigned long cipher_mode;
387 struct session_info_op siop;
390 memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
392 memset(&sess, 0, sizeof(sess));
393 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
395 for (i = 0, known_cipher_nids_amount = 0;
396 i < OSSL_NELEM(cipher_data); i++) {
398 selected_ciphers[i] = 1;
400 * Check that the cipher is usable
402 sess.cipher = cipher_data[i].devcryptoid;
403 sess.keylen = cipher_data[i].keylen;
404 if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
405 cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
409 cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
411 if ((known_cipher_methods[i] =
412 EVP_CIPHER_meth_new(cipher_data[i].nid,
413 cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
414 cipher_data[i].blocksize,
415 cipher_data[i].keylen)) == NULL
416 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
417 cipher_data[i].ivlen)
418 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
420 | EVP_CIPH_CUSTOM_COPY
421 | EVP_CIPH_FLAG_DEFAULT_ASN1)
422 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
423 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
424 cipher_mode == EVP_CIPH_CTR_MODE ?
427 || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
428 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
430 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
431 sizeof(struct cipher_ctx))) {
432 cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
433 EVP_CIPHER_meth_free(known_cipher_methods[i]);
434 known_cipher_methods[i] = NULL;
436 cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
439 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
440 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
442 cipher_driver_info[i].driver_name =
443 OPENSSL_strndup(siop.cipher_info.cra_driver_name,
444 CRYPTODEV_MAX_ALG_NAME);
445 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
446 cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
448 cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
450 #endif /* CIOCGSESSINFO */
452 ioctl(cfd, CIOCFSESSION, &sess.ses);
453 if (devcrypto_test_cipher(i)) {
454 known_cipher_nids[known_cipher_nids_amount++] =
460 static void rebuild_known_cipher_nids(ENGINE *e)
464 for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
465 if (devcrypto_test_cipher(i))
466 known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
468 ENGINE_unregister_ciphers(e);
469 ENGINE_register_ciphers(e);
472 static const EVP_CIPHER *get_cipher_method(int nid)
474 size_t i = get_cipher_data_index(nid);
478 return known_cipher_methods[i];
481 static int get_cipher_nids(const int **nids)
483 *nids = known_cipher_nids;
484 return known_cipher_nids_amount;
487 static void destroy_cipher_method(int nid)
489 size_t i = get_cipher_data_index(nid);
491 EVP_CIPHER_meth_free(known_cipher_methods[i]);
492 known_cipher_methods[i] = NULL;
495 static void destroy_all_cipher_methods(void)
499 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
500 destroy_cipher_method(cipher_data[i].nid);
501 OPENSSL_free(cipher_driver_info[i].driver_name);
502 cipher_driver_info[i].driver_name = NULL;
506 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
507 const int **nids, int nid)
510 return get_cipher_nids(nids);
512 *cipher = get_cipher_method(nid);
514 return *cipher != NULL;
517 static void devcrypto_select_all_ciphers(int *cipher_list)
521 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
525 static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
527 int *cipher_list = (int *)usr;
529 const EVP_CIPHER *EVP;
534 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
536 EVP = EVP_get_cipherbyname(name);
538 fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
539 else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
542 fprintf(stderr, "devcrypto: cipher %s not available\n", name);
547 static void dump_cipher_info(void)
552 fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
554 #ifndef CIOCGSESSINFO
555 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
557 for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
558 name = OBJ_nid2sn(cipher_data[i].nid);
559 fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
560 name ? name : "unknown", cipher_data[i].nid,
561 cipher_data[i].devcryptoid);
562 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
563 fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
566 fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
567 cipher_driver_info[i].driver_name : "unknown");
568 if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
569 fprintf(stderr, "(hw accelerated)");
570 else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
571 fprintf(stderr, "(software)");
573 fprintf(stderr, "(acceleration status unknown)");
574 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
575 fprintf (stderr, ". Cipher setup failed");
576 fprintf(stderr, "\n");
578 fprintf(stderr, "\n");
582 * We only support digests if the cryptodev implementation supports multiple
583 * data updates and session copying. Otherwise, we would be forced to maintain
584 * a cache, which is perilous if there's a lot of data coming in (if someone
585 * wants to checksum an OpenSSL tarball, for example).
587 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
588 #define IMPLEMENT_DIGEST
590 /******************************************************************************
594 * Because they all do the same basic operation, we have only one set of
595 * method functions for them all to share, and a mapping table between
596 * NIDs and cryptodev IDs, with all the necessary size data.
601 struct session_op sess;
602 /* This signals that the init function was called, not that it succeeded. */
604 unsigned char digest_res[HASH_MAX_LEN];
607 static const struct digest_data_st {
612 #ifndef OPENSSL_NO_MD5
613 { NID_md5, 16, CRYPTO_MD5 },
615 { NID_sha1, 20, CRYPTO_SHA1 },
616 #ifndef OPENSSL_NO_RMD160
617 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
618 { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
621 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
622 { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
624 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
625 { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
627 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
628 { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
630 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
631 { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
635 static size_t find_digest_data_index(int nid)
639 for (i = 0; i < OSSL_NELEM(digest_data); i++)
640 if (nid == digest_data[i].nid)
645 static size_t get_digest_data_index(int nid)
647 size_t i = find_digest_data_index(nid);
653 * Code further down must make sure that only NIDs in the table above
654 * are used. If any other NID reaches this function, there's a grave
655 * coding error further down.
657 assert("Code that never should be reached" == NULL);
661 static const struct digest_data_st *get_digest_data(int nid)
663 return &digest_data[get_digest_data_index(nid)];
667 * Following are the five necessary functions to map OpenSSL functionality
668 * with cryptodev: init, update, final, cleanup, and copy.
671 static int digest_init(EVP_MD_CTX *ctx)
673 struct digest_ctx *digest_ctx =
674 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
675 const struct digest_data_st *digest_d =
676 get_digest_data(EVP_MD_CTX_type(ctx));
678 digest_ctx->init_called = 1;
680 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
681 digest_ctx->sess.mac = digest_d->devcryptoid;
682 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
683 SYSerr(SYS_F_IOCTL, errno);
690 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
691 void *res, unsigned int flags)
693 struct crypt_op cryp;
695 memset(&cryp, 0, sizeof(cryp));
696 cryp.ses = ctx->sess.ses;
698 cryp.src = (void *)src;
702 return ioctl(cfd, CIOCCRYPT, &cryp);
705 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
707 struct digest_ctx *digest_ctx =
708 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
713 if (digest_ctx == NULL)
716 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
717 if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
719 } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
723 SYSerr(SYS_F_IOCTL, errno);
727 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
729 struct digest_ctx *digest_ctx =
730 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
732 if (md == NULL || digest_ctx == NULL)
735 if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
736 memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
737 } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
738 SYSerr(SYS_F_IOCTL, errno);
745 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
747 struct digest_ctx *digest_from =
748 (struct digest_ctx *)EVP_MD_CTX_md_data(from);
749 struct digest_ctx *digest_to =
750 (struct digest_ctx *)EVP_MD_CTX_md_data(to);
751 struct cphash_op cphash;
753 if (digest_from == NULL || digest_from->init_called != 1)
756 if (!digest_init(to)) {
757 SYSerr(SYS_F_IOCTL, errno);
761 cphash.src_ses = digest_from->sess.ses;
762 cphash.dst_ses = digest_to->sess.ses;
763 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
764 SYSerr(SYS_F_IOCTL, errno);
770 static int digest_cleanup(EVP_MD_CTX *ctx)
772 struct digest_ctx *digest_ctx =
773 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
775 if (digest_ctx == NULL)
777 if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
778 SYSerr(SYS_F_IOCTL, errno);
785 * Keep tables of known nids, associated methods, selected digests, and
787 * Note that known_digest_nids[] isn't necessarily indexed the same way as
788 * digest_data[] above, which the other tables are.
790 static int known_digest_nids[OSSL_NELEM(digest_data)];
791 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
792 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
793 static int selected_digests[OSSL_NELEM(digest_data)];
794 static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
796 static int devcrypto_test_digest(size_t digest_data_index)
798 return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
799 && selected_digests[digest_data_index] == 1
800 && (digest_driver_info[digest_data_index].accelerated
801 == DEVCRYPTO_ACCELERATED
802 || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
803 || (digest_driver_info[digest_data_index].accelerated
804 != DEVCRYPTO_NOT_ACCELERATED
805 && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
808 static void rebuild_known_digest_nids(ENGINE *e)
812 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
813 if (devcrypto_test_digest(i))
814 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
816 ENGINE_unregister_digests(e);
817 ENGINE_register_digests(e);
820 static void prepare_digest_methods(void)
823 struct session_op sess1, sess2;
825 struct session_info_op siop;
827 struct cphash_op cphash;
829 memset(&digest_driver_info, 0, sizeof(digest_driver_info));
831 memset(&sess1, 0, sizeof(sess1));
832 memset(&sess2, 0, sizeof(sess2));
834 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
837 selected_digests[i] = 1;
840 * Check that the digest is usable
842 sess1.mac = digest_data[i].devcryptoid;
844 if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
845 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
850 /* gather hardware acceleration info from the driver */
851 siop.ses = sess1.ses;
852 if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
853 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
855 digest_driver_info[i].driver_name =
856 OPENSSL_strndup(siop.hash_info.cra_driver_name,
857 CRYPTODEV_MAX_ALG_NAME);
858 if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
859 digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
861 digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
865 /* digest must be capable of hash state copy */
866 sess2.mac = sess1.mac;
867 if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
868 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
871 cphash.src_ses = sess1.ses;
872 cphash.dst_ses = sess2.ses;
873 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
874 digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
877 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
879 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
880 digest_data[i].digestlen)
881 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
882 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
883 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
884 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
885 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
886 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
887 sizeof(struct digest_ctx))) {
888 digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
889 EVP_MD_meth_free(known_digest_methods[i]);
890 known_digest_methods[i] = NULL;
893 digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
895 ioctl(cfd, CIOCFSESSION, &sess1.ses);
897 ioctl(cfd, CIOCFSESSION, &sess2.ses);
898 if (devcrypto_test_digest(i))
899 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
903 static const EVP_MD *get_digest_method(int nid)
905 size_t i = get_digest_data_index(nid);
909 return known_digest_methods[i];
912 static int get_digest_nids(const int **nids)
914 *nids = known_digest_nids;
915 return known_digest_nids_amount;
918 static void destroy_digest_method(int nid)
920 size_t i = get_digest_data_index(nid);
922 EVP_MD_meth_free(known_digest_methods[i]);
923 known_digest_methods[i] = NULL;
926 static void destroy_all_digest_methods(void)
930 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
931 destroy_digest_method(digest_data[i].nid);
932 OPENSSL_free(digest_driver_info[i].driver_name);
933 digest_driver_info[i].driver_name = NULL;
937 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
938 const int **nids, int nid)
941 return get_digest_nids(nids);
943 *digest = get_digest_method(nid);
945 return *digest != NULL;
948 static void devcrypto_select_all_digests(int *digest_list)
952 for (i = 0; i < OSSL_NELEM(digest_data); i++)
956 static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
958 int *digest_list = (int *)usr;
965 if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
967 EVP = EVP_get_digestbyname(name);
969 fprintf(stderr, "devcrypto: unknown digest %s\n", name);
970 else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
973 fprintf(stderr, "devcrypto: digest %s not available\n", name);
978 static void dump_digest_info(void)
983 fprintf (stderr, "Information about digests supported by the /dev/crypto"
985 #ifndef CIOCGSESSINFO
986 fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
989 for (i = 0; i < OSSL_NELEM(digest_data); i++) {
990 name = OBJ_nid2sn(digest_data[i].nid);
991 fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
992 name ? name : "unknown", digest_data[i].nid,
993 digest_data[i].devcryptoid,
994 digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
995 if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
996 fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
999 if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
1000 fprintf(stderr, " (hw accelerated)");
1001 else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
1002 fprintf(stderr, " (software)");
1004 fprintf(stderr, " (acceleration status unknown)");
1005 if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
1006 fprintf (stderr, ". Cipher setup failed\n");
1007 else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
1008 fprintf(stderr, ", CIOCCPHASH failed\n");
1010 fprintf(stderr, ", CIOCCPHASH capable\n");
1012 fprintf(stderr, "\n");
1017 /******************************************************************************
1023 #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
1024 #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
1025 #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
1026 #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
1028 static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
1029 #ifdef CIOCGSESSINFO
1030 {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
1032 "specifies whether to use software (not accelerated) drivers ("
1033 OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
1034 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
1035 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
1036 "=use if acceleration can't be determined) [default="
1037 OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
1038 ENGINE_CMD_FLAG_NUMERIC},
1041 {DEVCRYPTO_CMD_CIPHERS,
1043 "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
1044 ENGINE_CMD_FLAG_STRING},
1046 #ifdef IMPLEMENT_DIGEST
1047 {DEVCRYPTO_CMD_DIGESTS,
1049 "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
1050 ENGINE_CMD_FLAG_STRING},
1053 {DEVCRYPTO_CMD_DUMP_INFO,
1055 "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
1056 ENGINE_CMD_FLAG_NO_INPUT},
1061 static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1065 #ifdef CIOCGSESSINFO
1066 case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
1068 case DEVCRYPTO_REQUIRE_ACCELERATED:
1069 case DEVCRYPTO_USE_SOFTWARE:
1070 case DEVCRYPTO_REJECT_SOFTWARE:
1073 fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
1076 if (use_softdrivers == i)
1078 use_softdrivers = i;
1079 #ifdef IMPLEMENT_DIGEST
1080 rebuild_known_digest_nids(e);
1082 rebuild_known_cipher_nids(e);
1084 #endif /* CIOCGSESSINFO */
1086 case DEVCRYPTO_CMD_CIPHERS:
1089 if (strcasecmp((const char *)p, "ALL") == 0) {
1090 devcrypto_select_all_ciphers(selected_ciphers);
1091 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1092 memset(selected_ciphers, 0, sizeof(selected_ciphers));
1094 new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
1095 if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
1096 OPENSSL_free(new_list);
1099 memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
1100 OPENSSL_free(new_list);
1102 rebuild_known_cipher_nids(e);
1105 #ifdef IMPLEMENT_DIGEST
1106 case DEVCRYPTO_CMD_DIGESTS:
1109 if (strcasecmp((const char *)p, "ALL") == 0) {
1110 devcrypto_select_all_digests(selected_digests);
1111 } else if (strcasecmp((const char*)p, "NONE") == 0) {
1112 memset(selected_digests, 0, sizeof(selected_digests));
1114 new_list=OPENSSL_zalloc(sizeof(selected_digests));
1115 if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
1116 OPENSSL_free(new_list);
1119 memcpy(selected_digests, new_list, sizeof(selected_digests));
1120 OPENSSL_free(new_list);
1122 rebuild_known_digest_nids(e);
1124 #endif /* IMPLEMENT_DIGEST */
1126 case DEVCRYPTO_CMD_DUMP_INFO:
1128 #ifdef IMPLEMENT_DIGEST
1139 /******************************************************************************
1148 static int open_devcrypto(void)
1153 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
1154 #ifndef ENGINE_DEVCRYPTO_DEBUG
1155 if (errno != ENOENT)
1157 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
1164 static int close_devcrypto(void)
1169 if (close(cfd) == 0) {
1170 fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
1176 static int devcrypto_unload(ENGINE *e)
1178 destroy_all_cipher_methods();
1179 #ifdef IMPLEMENT_DIGEST
1180 destroy_all_digest_methods();
1188 static int bind_devcrypto(ENGINE *e) {
1190 if (!ENGINE_set_id(e, engine_devcrypto_id)
1191 || !ENGINE_set_name(e, "/dev/crypto engine")
1192 || !ENGINE_set_destroy_function(e, devcrypto_unload)
1193 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
1194 || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
1197 prepare_cipher_methods();
1198 #ifdef IMPLEMENT_DIGEST
1199 prepare_digest_methods();
1202 return (ENGINE_set_ciphers(e, devcrypto_ciphers)
1203 #ifdef IMPLEMENT_DIGEST
1204 && ENGINE_set_digests(e, devcrypto_digests)
1207 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
1208 * implementations, it seems to only exist in FreeBSD, and regarding the
1209 * parameters in its crypt_kop, the manual crypto(4) has this to say:
1211 * The semantics of these arguments are currently undocumented.
1213 * Reading through the FreeBSD source code doesn't give much more than
1214 * their CRK_MOD_EXP implementation for ubsec.
1216 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
1217 * structure as well as the command (CRK_*) in cryptodev.h, but no support
1218 * seems to be implemented at all for the moment.
1220 * At the time of writing, it seems impossible to write proper support for
1221 * FreeBSD's asym features without some very deep knowledge and access to
1222 * specific kernel modules.
1224 * /Richard Levitte, 2017-05-11
1227 # ifndef OPENSSL_NO_RSA
1228 && ENGINE_set_RSA(e, devcrypto_rsa)
1230 # ifndef OPENSSL_NO_DSA
1231 && ENGINE_set_DSA(e, devcrypto_dsa)
1233 # ifndef OPENSSL_NO_DH
1234 && ENGINE_set_DH(e, devcrypto_dh)
1236 # ifndef OPENSSL_NO_EC
1237 && ENGINE_set_EC(e, devcrypto_ec)
1243 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
1245 * In case this engine is built into libcrypto, then it doesn't offer any
1246 * ability to be dynamically loadable.
1248 void engine_load_devcrypto_int(void)
1252 if (!open_devcrypto())
1255 if ((e = ENGINE_new()) == NULL
1256 || !bind_devcrypto(e)) {
1263 ENGINE_free(e); /* Loose our local reference */
1269 static int bind_helper(ENGINE *e, const char *id)
1271 if ((id && (strcmp(id, engine_devcrypto_id) != 0))
1272 || !open_devcrypto())
1274 if (!bind_devcrypto(e)) {
1281 IMPLEMENT_DYNAMIC_CHECK_FN()
1282 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)