2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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/evp.h>
20 #include <openssl/err.h>
21 #include <openssl/engine.h>
22 #include <openssl/objects.h>
23 #include <crypto/cryptodev.h>
25 #include "internal/engine.h"
27 #ifdef CRYPTO_ALGORITHM_MIN
28 # define CHECK_BSD_STYLE_MACROS
31 /******************************************************************************
35 * Because they all do the same basic operation, we have only one set of
36 * method functions for them all to share, and a mapping table between
37 * NIDs and cryptodev IDs, with all the necessary size data.
43 struct session_op sess;
45 /* to pass from init to do_cipher */
46 const unsigned char *iv;
47 int op; /* COP_ENCRYPT or COP_DECRYPT */
50 static const struct cipher_data_st {
58 #ifndef OPENSSL_NO_DES
59 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
60 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
63 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
65 #ifndef OPENSSL_NO_CAST
66 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
68 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
69 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
70 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
71 #ifndef OPENSSL_NO_RC4
72 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
74 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
75 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
76 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
77 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
79 #if 0 /* Not yet supported */
80 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
81 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
83 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
84 { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
85 { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
86 { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
88 #if 0 /* Not yet supported */
89 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
90 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
91 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
93 #ifndef OPENSSL_NO_CAMELLIA
94 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
95 CRYPTO_CAMELLIA_CBC },
96 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
97 CRYPTO_CAMELLIA_CBC },
98 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
99 CRYPTO_CAMELLIA_CBC },
103 static size_t get_cipher_data_index(int nid)
107 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
108 if (nid == cipher_data[i].nid)
112 * Code further down must make sure that only NIDs in the table above
113 * are used. If any other NID reaches this function, there's a grave
114 * coding error further down.
116 assert("Code that never should be reached" == NULL);
120 static const struct cipher_data_st *get_cipher_data(int nid)
122 return &cipher_data[get_cipher_data_index(nid)];
126 * Following are the three necessary functions to map OpenSSL functionality
130 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
131 const unsigned char *iv, int enc)
133 struct cipher_ctx *cipher_ctx =
134 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
135 const struct cipher_data_st *cipher_d =
136 get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
138 if ((cipher_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
139 SYSerr(SYS_F_OPEN, errno);
143 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
144 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
145 cipher_ctx->sess.keylen = cipher_d->keylen;
146 cipher_ctx->sess.key = (void *)key;
147 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
148 if (ioctl(cipher_ctx->cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
149 SYSerr(SYS_F_IOCTL, errno);
150 close(cipher_ctx->cfd);
157 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
158 const unsigned char *in, size_t inl)
160 struct cipher_ctx *cipher_ctx =
161 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
162 struct crypt_op cryp;
163 #if !defined(COP_FLAG_WRITE_IV)
164 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
167 memset(&cryp, 0, sizeof(cryp));
168 cryp.ses = cipher_ctx->sess.ses;
170 cryp.src = (void *)in;
171 cryp.dst = (void *)out;
172 cryp.iv = (void *)EVP_CIPHER_CTX_iv_noconst(ctx);
173 cryp.op = cipher_ctx->op;
174 #if !defined(COP_FLAG_WRITE_IV)
177 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
178 assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
179 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
180 unsigned char *ivptr = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
182 memcpy(saved_iv, ivptr, EVP_CIPHER_CTX_iv_length(ctx));
186 cryp.flags = COP_FLAG_WRITE_IV;
189 if (ioctl(cipher_ctx->cfd, CIOCCRYPT, &cryp) < 0) {
190 SYSerr(SYS_F_IOCTL, errno);
194 #if !defined(COP_FLAG_WRITE_IV)
195 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
196 unsigned char *ivptr = saved_iv;
198 assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
199 if (!EVP_CIPHER_CTX_encrypting(ctx))
200 ivptr = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
202 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), ivptr,
203 EVP_CIPHER_CTX_iv_length(ctx));
210 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
212 struct cipher_ctx *cipher_ctx =
213 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
215 if (ioctl(cipher_ctx->cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
216 SYSerr(SYS_F_IOCTL, errno);
219 if (close(cipher_ctx->cfd) < 0) {
220 SYSerr(SYS_F_CLOSE, errno);
228 * Keep a table of known nids and associated methods.
229 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
230 * cipher_data[] above, which known_cipher_methods[] is.
232 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
233 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
234 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
236 static void prepare_cipher_methods()
239 struct session_op sess;
242 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
245 memset(&sess, 0, sizeof(sess));
246 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
248 for (i = 0, known_cipher_nids_amount = 0;
249 i < OSSL_NELEM(cipher_data); i++) {
252 * Check that the algo is really availably by trying to open and close
255 sess.cipher = cipher_data[i].devcryptoid;
256 sess.keylen = cipher_data[i].keylen;
257 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
258 || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
261 if ((known_cipher_methods[i] =
262 EVP_CIPHER_meth_new(cipher_data[i].nid,
263 cipher_data[i].blocksize,
264 cipher_data[i].keylen)) == NULL
265 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
266 cipher_data[i].ivlen)
267 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
269 | EVP_CIPH_FLAG_DEFAULT_ASN1)
270 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
271 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
273 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
275 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
276 sizeof(struct cipher_ctx))) {
277 EVP_CIPHER_meth_free(known_cipher_methods[i]);
278 known_cipher_methods[i] = NULL;
280 known_cipher_nids[known_cipher_nids_amount++] =
288 static const EVP_CIPHER *get_cipher_method(int nid)
290 size_t i = get_cipher_data_index(nid);
294 return known_cipher_methods[i];
297 static int get_cipher_nids(const int **nids)
299 *nids = known_cipher_nids;
300 return known_cipher_nids_amount;
303 static void destroy_cipher_method(int nid)
305 size_t i = get_cipher_data_index(nid);
307 EVP_CIPHER_meth_free(known_cipher_methods[i]);
308 known_cipher_methods[i] = NULL;
311 static void destroy_all_cipher_methods()
315 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
316 destroy_cipher_method(cipher_data[i].nid);
319 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
320 const int **nids, int nid)
323 return get_cipher_nids(nids);
325 *cipher = get_cipher_method(nid);
327 return *cipher != NULL;
331 * We only support digests if the cryptodev implementation supports multiple
332 * data updates and session copying. Otherwise, we would be forced to maintain
333 * a cache, which is perilous if there's a lot of data coming in (if someone
334 * wants to checksum an OpenSSL tarball, for example).
336 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
337 #define IMPLEMENT_DIGEST
339 /******************************************************************************
343 * Because they all do the same basic operation, we have only one set of
344 * method functions for them all to share, and a mapping table between
345 * NIDs and cryptodev IDs, with all the necessary size data.
351 struct session_op sess;
355 static const struct digest_data_st {
360 #ifndef OPENSSL_NO_MD5
361 { NID_md5, 16, CRYPTO_MD5 },
363 { NID_sha1, 20, CRYPTO_SHA1 },
364 #ifndef OPENSSL_NO_RMD160
365 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
366 { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
369 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
370 { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
372 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
373 { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
375 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
376 { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
378 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
379 { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
383 static size_t get_digest_data_index(int nid)
387 for (i = 0; i < OSSL_NELEM(digest_data); i++)
388 if (nid == digest_data[i].nid)
392 * Code further down must make sure that only NIDs in the table above
393 * are used. If any other NID reaches this function, there's a grave
394 * coding error further down.
396 assert("Code that never should be reached" == NULL);
400 static const struct digest_data_st *get_digest_data(int nid)
402 return &digest_data[get_digest_data_index(nid)];
406 * Following are the four necessary functions to map OpenSSL functionality
410 static int digest_init(EVP_MD_CTX *ctx)
412 struct digest_ctx *digest_ctx =
413 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
414 const struct digest_data_st *digest_d =
415 get_digest_data(EVP_MD_CTX_type(ctx));
417 if (digest_ctx->init == 0
418 && (digest_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
419 SYSerr(SYS_F_OPEN, errno);
423 digest_ctx->init = 1;
425 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
426 digest_ctx->sess.mac = digest_d->devcryptoid;
427 if (ioctl(digest_ctx->cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
428 SYSerr(SYS_F_IOCTL, errno);
429 close(digest_ctx->cfd);
436 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
437 void *res, unsigned int flags)
439 struct crypt_op cryp;
441 memset(&cryp, 0, sizeof(cryp));
442 cryp.ses = ctx->sess.ses;
444 cryp.src = (void *)src;
448 return ioctl(ctx->cfd, CIOCCRYPT, &cryp);
451 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
453 struct digest_ctx *digest_ctx =
454 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
459 if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
460 SYSerr(SYS_F_IOCTL, errno);
467 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
469 struct digest_ctx *digest_ctx =
470 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
472 if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
473 SYSerr(SYS_F_IOCTL, errno);
476 if (ioctl(digest_ctx->cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
477 SYSerr(SYS_F_IOCTL, errno);
484 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
486 struct digest_ctx *digest_from =
487 (struct digest_ctx *)EVP_MD_CTX_md_data(from);
488 struct digest_ctx *digest_to =
489 (struct digest_ctx *)EVP_MD_CTX_md_data(to);
490 struct cphash_op cphash;
492 if (digest_from == NULL)
495 if (digest_from->init != 1) {
496 SYSerr(SYS_F_IOCTL, EINVAL);
500 if (!digest_init(to)) {
501 SYSerr(SYS_F_IOCTL, errno);
505 cphash.src_ses = digest_from->sess.ses;
506 cphash.dst_ses = digest_to->sess.ses;
507 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
508 SYSerr(SYS_F_IOCTL, errno);
514 static int digest_cleanup(EVP_MD_CTX *ctx)
516 struct digest_ctx *digest_ctx =
517 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
519 if (close(digest_ctx->cfd) < 0) {
520 SYSerr(SYS_F_CLOSE, errno);
528 * Keep a table of known nids and associated methods.
529 * Note that known_digest_nids[] isn't necessarily indexed the same way as
530 * digest_data[] above, which known_digest_methods[] is.
532 static int known_digest_nids[OSSL_NELEM(digest_data)];
533 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
534 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
536 static void prepare_digest_methods()
539 struct session_op sess;
542 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
545 memset(&sess, 0, sizeof(sess));
547 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
551 * Check that the algo is really availably by trying to open and close
554 sess.mac = digest_data[i].devcryptoid;
555 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
556 || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
559 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
561 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
562 digest_data[i].digestlen)
563 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
564 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
565 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
566 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
567 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
568 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
569 sizeof(struct digest_ctx))) {
570 EVP_MD_meth_free(known_digest_methods[i]);
571 known_digest_methods[i] = NULL;
573 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
580 static const EVP_MD *get_digest_method(int nid)
582 size_t i = get_digest_data_index(nid);
586 return known_digest_methods[i];
589 static int get_digest_nids(const int **nids)
591 *nids = known_digest_nids;
592 return known_digest_nids_amount;
595 static void destroy_digest_method(int nid)
597 size_t i = get_digest_data_index(nid);
599 EVP_MD_meth_free(known_digest_methods[i]);
600 known_digest_methods[i] = NULL;
603 static void destroy_all_digest_methods()
607 for (i = 0; i < OSSL_NELEM(digest_data); i++)
608 destroy_digest_method(digest_data[i].nid);
611 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
612 const int **nids, int nid)
615 return get_digest_nids(nids);
617 *digest = get_digest_method(nid);
619 return *digest != NULL;
624 /******************************************************************************
630 static int devcrypto_unload(ENGINE *e)
632 destroy_all_cipher_methods();
633 #ifdef IMPLEMENT_DIGEST
634 destroy_all_digest_methods();
639 * This engine is always built into libcrypto, so it doesn't offer any
640 * ability to be dynamically loadable.
642 void engine_load_devcrypto_int()
646 if (access("/dev/crypto", R_OK | W_OK) < 0) {
648 "/dev/crypto not present, not enabling devcrypto engine\n");
652 prepare_cipher_methods();
653 #ifdef IMPLEMENT_DIGEST
654 prepare_digest_methods();
657 if ((e = ENGINE_new()) == NULL)
660 if (!ENGINE_set_id(e, "devcrypto")
661 || !ENGINE_set_name(e, "/dev/crypto engine")
662 || !ENGINE_set_destroy_function(e, devcrypto_unload)
665 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
666 * implementations, it seems to only exist in FreeBSD, and regarding the
667 * parameters in its crypt_kop, the manual crypto(4) has this to say:
669 * The semantics of these arguments are currently undocumented.
671 * Reading through the FreeBSD source code doesn't give much more than
672 * their CRK_MOD_EXP implementation for ubsec.
674 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
675 * structure as well as the command (CRK_*) in cryptodev.h, but no support
676 * seems to be implemented at all for the moment.
678 * At the time of writing, it seems impossible to write proper support for
679 * FreeBSD's asym features without some very deep knowledge and access to
680 * specific kernel modules.
682 * /Richard Levitte, 2017-05-11
685 # ifndef OPENSSL_NO_RSA
686 || !ENGINE_set_RSA(e, devcrypto_rsa)
688 # ifndef OPENSSL_NO_DSA
689 || !ENGINE_set_DSA(e, devcrypto_dsa)
691 # ifndef OPENSSL_NO_DH
692 || !ENGINE_set_DH(e, devcrypto_dh)
694 # ifndef OPENSSL_NO_EC
695 || !ENGINE_set_EC(e, devcrypto_ec)
698 || !ENGINE_set_ciphers(e, devcrypto_ciphers)
699 #ifdef IMPLEMENT_DIGEST
700 || !ENGINE_set_digests(e, devcrypto_digests)
708 ENGINE_free(e); /* Loose our local reference */