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
32 * ONE global file descriptor for all sessions. This allows operations
33 * such as digest session data copying (see digest_copy()), but is also
34 * saner... why re-open /dev/crypto for every session?
38 /******************************************************************************
42 * Because they all do the same basic operation, we have only one set of
43 * method functions for them all to share, and a mapping table between
44 * NIDs and cryptodev IDs, with all the necessary size data.
49 struct session_op sess;
51 /* to pass from init to do_cipher */
52 const unsigned char *iv;
53 int op; /* COP_ENCRYPT or COP_DECRYPT */
56 static const struct cipher_data_st {
64 #ifndef OPENSSL_NO_DES
65 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
66 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
69 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
71 #ifndef OPENSSL_NO_CAST
72 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
74 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
75 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
76 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
77 #ifndef OPENSSL_NO_RC4
78 { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
80 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
81 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
82 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
83 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
85 #if 0 /* Not yet supported */
86 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
87 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
89 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
90 { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
91 { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
92 { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
94 #if 0 /* Not yet supported */
95 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
96 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
97 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
99 #ifndef OPENSSL_NO_CAMELLIA
100 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
101 CRYPTO_CAMELLIA_CBC },
102 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
103 CRYPTO_CAMELLIA_CBC },
104 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
105 CRYPTO_CAMELLIA_CBC },
109 static size_t get_cipher_data_index(int nid)
113 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
114 if (nid == cipher_data[i].nid)
118 * Code further down must make sure that only NIDs in the table above
119 * are used. If any other NID reaches this function, there's a grave
120 * coding error further down.
122 assert("Code that never should be reached" == NULL);
126 static const struct cipher_data_st *get_cipher_data(int nid)
128 return &cipher_data[get_cipher_data_index(nid)];
132 * Following are the three necessary functions to map OpenSSL functionality
136 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
137 const unsigned char *iv, int enc)
139 struct cipher_ctx *cipher_ctx =
140 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
141 const struct cipher_data_st *cipher_d =
142 get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
144 memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
145 cipher_ctx->sess.cipher = cipher_d->devcryptoid;
146 cipher_ctx->sess.keylen = cipher_d->keylen;
147 cipher_ctx->sess.key = (void *)key;
148 cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
149 if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
150 SYSerr(SYS_F_IOCTL, errno);
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(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(cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
216 SYSerr(SYS_F_IOCTL, errno);
224 * Keep a table of known nids and associated methods.
225 * Note that known_cipher_nids[] isn't necessarily indexed the same way as
226 * cipher_data[] above, which known_cipher_methods[] is.
228 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
229 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
230 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
232 static void prepare_cipher_methods(void)
235 struct session_op sess;
237 memset(&sess, 0, sizeof(sess));
238 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
240 for (i = 0, known_cipher_nids_amount = 0;
241 i < OSSL_NELEM(cipher_data); i++) {
244 * Check that the algo is really availably by trying to open and close
247 sess.cipher = cipher_data[i].devcryptoid;
248 sess.keylen = cipher_data[i].keylen;
249 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
250 || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
253 if ((known_cipher_methods[i] =
254 EVP_CIPHER_meth_new(cipher_data[i].nid,
255 cipher_data[i].blocksize,
256 cipher_data[i].keylen)) == NULL
257 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
258 cipher_data[i].ivlen)
259 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
261 | EVP_CIPH_FLAG_DEFAULT_ASN1)
262 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
263 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
265 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
267 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
268 sizeof(struct cipher_ctx))) {
269 EVP_CIPHER_meth_free(known_cipher_methods[i]);
270 known_cipher_methods[i] = NULL;
272 known_cipher_nids[known_cipher_nids_amount++] =
278 static const EVP_CIPHER *get_cipher_method(int nid)
280 size_t i = get_cipher_data_index(nid);
284 return known_cipher_methods[i];
287 static int get_cipher_nids(const int **nids)
289 *nids = known_cipher_nids;
290 return known_cipher_nids_amount;
293 static void destroy_cipher_method(int nid)
295 size_t i = get_cipher_data_index(nid);
297 EVP_CIPHER_meth_free(known_cipher_methods[i]);
298 known_cipher_methods[i] = NULL;
301 static void destroy_all_cipher_methods(void)
305 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
306 destroy_cipher_method(cipher_data[i].nid);
309 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
310 const int **nids, int nid)
313 return get_cipher_nids(nids);
315 *cipher = get_cipher_method(nid);
317 return *cipher != NULL;
321 * We only support digests if the cryptodev implementation supports multiple
322 * data updates and session copying. Otherwise, we would be forced to maintain
323 * a cache, which is perilous if there's a lot of data coming in (if someone
324 * wants to checksum an OpenSSL tarball, for example).
326 #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
327 #define IMPLEMENT_DIGEST
329 /******************************************************************************
333 * Because they all do the same basic operation, we have only one set of
334 * method functions for them all to share, and a mapping table between
335 * NIDs and cryptodev IDs, with all the necessary size data.
340 struct session_op sess;
341 /* This signals that the init function was called, not that it succeeded. */
345 static const struct digest_data_st {
350 #ifndef OPENSSL_NO_MD5
351 { NID_md5, 16, CRYPTO_MD5 },
353 { NID_sha1, 20, CRYPTO_SHA1 },
354 #ifndef OPENSSL_NO_RMD160
355 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
356 { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
359 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
360 { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
362 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
363 { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
365 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
366 { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
368 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
369 { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
373 static size_t get_digest_data_index(int nid)
377 for (i = 0; i < OSSL_NELEM(digest_data); i++)
378 if (nid == digest_data[i].nid)
382 * Code further down must make sure that only NIDs in the table above
383 * are used. If any other NID reaches this function, there's a grave
384 * coding error further down.
386 assert("Code that never should be reached" == NULL);
390 static const struct digest_data_st *get_digest_data(int nid)
392 return &digest_data[get_digest_data_index(nid)];
396 * Following are the four necessary functions to map OpenSSL functionality
400 static int digest_init(EVP_MD_CTX *ctx)
402 struct digest_ctx *digest_ctx =
403 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
404 const struct digest_data_st *digest_d =
405 get_digest_data(EVP_MD_CTX_type(ctx));
407 digest_ctx->init_called = 1;
409 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
410 digest_ctx->sess.mac = digest_d->devcryptoid;
411 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
412 SYSerr(SYS_F_IOCTL, errno);
419 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
420 void *res, unsigned int flags)
422 struct crypt_op cryp;
424 memset(&cryp, 0, sizeof(cryp));
425 cryp.ses = ctx->sess.ses;
427 cryp.src = (void *)src;
431 return ioctl(cfd, CIOCCRYPT, &cryp);
434 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
436 struct digest_ctx *digest_ctx =
437 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
442 if (digest_ctx == NULL)
445 if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
446 SYSerr(SYS_F_IOCTL, errno);
453 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
455 struct digest_ctx *digest_ctx =
456 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
458 if (md == NULL || digest_ctx == NULL)
460 if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
461 SYSerr(SYS_F_IOCTL, errno);
464 if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
465 SYSerr(SYS_F_IOCTL, errno);
472 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
474 struct digest_ctx *digest_from =
475 (struct digest_ctx *)EVP_MD_CTX_md_data(from);
476 struct digest_ctx *digest_to =
477 (struct digest_ctx *)EVP_MD_CTX_md_data(to);
478 struct cphash_op cphash;
480 if (digest_from == NULL || digest_from->init_called != 1)
483 if (!digest_init(to)) {
484 SYSerr(SYS_F_IOCTL, errno);
488 cphash.src_ses = digest_from->sess.ses;
489 cphash.dst_ses = digest_to->sess.ses;
490 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
491 SYSerr(SYS_F_IOCTL, errno);
497 static int digest_cleanup(EVP_MD_CTX *ctx)
503 * Keep a table of known nids and associated methods.
504 * Note that known_digest_nids[] isn't necessarily indexed the same way as
505 * digest_data[] above, which known_digest_methods[] is.
507 static int known_digest_nids[OSSL_NELEM(digest_data)];
508 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
509 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
511 static void prepare_digest_methods(void)
514 struct session_op sess;
516 memset(&sess, 0, sizeof(sess));
518 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
522 * Check that the algo is really availably by trying to open and close
525 sess.mac = digest_data[i].devcryptoid;
526 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
527 || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
530 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
532 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
533 digest_data[i].digestlen)
534 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
535 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
536 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
537 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
538 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
539 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
540 sizeof(struct digest_ctx))) {
541 EVP_MD_meth_free(known_digest_methods[i]);
542 known_digest_methods[i] = NULL;
544 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
549 static const EVP_MD *get_digest_method(int nid)
551 size_t i = get_digest_data_index(nid);
555 return known_digest_methods[i];
558 static int get_digest_nids(const int **nids)
560 *nids = known_digest_nids;
561 return known_digest_nids_amount;
564 static void destroy_digest_method(int nid)
566 size_t i = get_digest_data_index(nid);
568 EVP_MD_meth_free(known_digest_methods[i]);
569 known_digest_methods[i] = NULL;
572 static void destroy_all_digest_methods(void)
576 for (i = 0; i < OSSL_NELEM(digest_data); i++)
577 destroy_digest_method(digest_data[i].nid);
580 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
581 const int **nids, int nid)
584 return get_digest_nids(nids);
586 *digest = get_digest_method(nid);
588 return *digest != NULL;
593 /******************************************************************************
599 static int devcrypto_unload(ENGINE *e)
601 destroy_all_cipher_methods();
602 #ifdef IMPLEMENT_DIGEST
603 destroy_all_digest_methods();
611 * This engine is always built into libcrypto, so it doesn't offer any
612 * ability to be dynamically loadable.
614 void engine_load_devcrypto_int()
618 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
619 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
623 if ((e = ENGINE_new()) == NULL
624 || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
627 * We know that devcrypto_unload() won't be called when one of the
628 * above two calls have failed, so we close cfd explicitly here to
629 * avoid leaking resources.
635 prepare_cipher_methods();
636 #ifdef IMPLEMENT_DIGEST
637 prepare_digest_methods();
640 if (!ENGINE_set_id(e, "devcrypto")
641 || !ENGINE_set_name(e, "/dev/crypto engine")
644 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
645 * implementations, it seems to only exist in FreeBSD, and regarding the
646 * parameters in its crypt_kop, the manual crypto(4) has this to say:
648 * The semantics of these arguments are currently undocumented.
650 * Reading through the FreeBSD source code doesn't give much more than
651 * their CRK_MOD_EXP implementation for ubsec.
653 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
654 * structure as well as the command (CRK_*) in cryptodev.h, but no support
655 * seems to be implemented at all for the moment.
657 * At the time of writing, it seems impossible to write proper support for
658 * FreeBSD's asym features without some very deep knowledge and access to
659 * specific kernel modules.
661 * /Richard Levitte, 2017-05-11
664 # ifndef OPENSSL_NO_RSA
665 || !ENGINE_set_RSA(e, devcrypto_rsa)
667 # ifndef OPENSSL_NO_DSA
668 || !ENGINE_set_DSA(e, devcrypto_dsa)
670 # ifndef OPENSSL_NO_DH
671 || !ENGINE_set_DH(e, devcrypto_dh)
673 # ifndef OPENSSL_NO_EC
674 || !ENGINE_set_EC(e, devcrypto_ec)
677 || !ENGINE_set_ciphers(e, devcrypto_ciphers)
678 #ifdef IMPLEMENT_DIGEST
679 || !ENGINE_set_digests(e, devcrypto_digests)
687 ENGINE_free(e); /* Loose our local reference */