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;
344 static const struct digest_data_st {
349 #ifndef OPENSSL_NO_MD5
350 { NID_md5, 16, CRYPTO_MD5 },
352 { NID_sha1, 20, CRYPTO_SHA1 },
353 #ifndef OPENSSL_NO_RMD160
354 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
355 { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
358 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
359 { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
361 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
362 { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
364 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
365 { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
367 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
368 { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
372 static size_t get_digest_data_index(int nid)
376 for (i = 0; i < OSSL_NELEM(digest_data); i++)
377 if (nid == digest_data[i].nid)
381 * Code further down must make sure that only NIDs in the table above
382 * are used. If any other NID reaches this function, there's a grave
383 * coding error further down.
385 assert("Code that never should be reached" == NULL);
389 static const struct digest_data_st *get_digest_data(int nid)
391 return &digest_data[get_digest_data_index(nid)];
395 * Following are the four necessary functions to map OpenSSL functionality
399 static int digest_init(EVP_MD_CTX *ctx)
401 struct digest_ctx *digest_ctx =
402 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
403 const struct digest_data_st *digest_d =
404 get_digest_data(EVP_MD_CTX_type(ctx));
406 digest_ctx->init = 1;
408 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
409 digest_ctx->sess.mac = digest_d->devcryptoid;
410 if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
411 SYSerr(SYS_F_IOCTL, errno);
418 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
419 void *res, unsigned int flags)
421 struct crypt_op cryp;
423 memset(&cryp, 0, sizeof(cryp));
424 cryp.ses = ctx->sess.ses;
426 cryp.src = (void *)src;
430 return ioctl(cfd, CIOCCRYPT, &cryp);
433 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
435 struct digest_ctx *digest_ctx =
436 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
441 if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
442 SYSerr(SYS_F_IOCTL, errno);
449 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
451 struct digest_ctx *digest_ctx =
452 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
454 if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
455 SYSerr(SYS_F_IOCTL, errno);
458 if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
459 SYSerr(SYS_F_IOCTL, errno);
466 static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
468 struct digest_ctx *digest_from =
469 (struct digest_ctx *)EVP_MD_CTX_md_data(from);
470 struct digest_ctx *digest_to =
471 (struct digest_ctx *)EVP_MD_CTX_md_data(to);
472 struct cphash_op cphash;
474 if (digest_from == NULL)
477 if (digest_from->init != 1) {
478 SYSerr(SYS_F_IOCTL, EINVAL);
482 if (!digest_init(to)) {
483 SYSerr(SYS_F_IOCTL, errno);
487 cphash.src_ses = digest_from->sess.ses;
488 cphash.dst_ses = digest_to->sess.ses;
489 if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
490 SYSerr(SYS_F_IOCTL, errno);
496 static int digest_cleanup(EVP_MD_CTX *ctx)
502 * Keep a table of known nids and associated methods.
503 * Note that known_digest_nids[] isn't necessarily indexed the same way as
504 * digest_data[] above, which known_digest_methods[] is.
506 static int known_digest_nids[OSSL_NELEM(digest_data)];
507 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
508 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
510 static void prepare_digest_methods(void)
513 struct session_op sess;
515 memset(&sess, 0, sizeof(sess));
517 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
521 * Check that the algo is really availably by trying to open and close
524 sess.mac = digest_data[i].devcryptoid;
525 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
526 || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
529 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
531 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
532 digest_data[i].digestlen)
533 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
534 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
535 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
536 || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
537 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
538 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
539 sizeof(struct digest_ctx))) {
540 EVP_MD_meth_free(known_digest_methods[i]);
541 known_digest_methods[i] = NULL;
543 known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
548 static const EVP_MD *get_digest_method(int nid)
550 size_t i = get_digest_data_index(nid);
554 return known_digest_methods[i];
557 static int get_digest_nids(const int **nids)
559 *nids = known_digest_nids;
560 return known_digest_nids_amount;
563 static void destroy_digest_method(int nid)
565 size_t i = get_digest_data_index(nid);
567 EVP_MD_meth_free(known_digest_methods[i]);
568 known_digest_methods[i] = NULL;
571 static void destroy_all_digest_methods(void)
575 for (i = 0; i < OSSL_NELEM(digest_data); i++)
576 destroy_digest_method(digest_data[i].nid);
579 static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
580 const int **nids, int nid)
583 return get_digest_nids(nids);
585 *digest = get_digest_method(nid);
587 return *digest != NULL;
592 /******************************************************************************
598 static int devcrypto_unload(ENGINE *e)
600 destroy_all_cipher_methods();
601 #ifdef IMPLEMENT_DIGEST
602 destroy_all_digest_methods();
610 * This engine is always built into libcrypto, so it doesn't offer any
611 * ability to be dynamically loadable.
613 void engine_load_devcrypto_int()
617 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
618 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
622 prepare_cipher_methods();
623 #ifdef IMPLEMENT_DIGEST
624 prepare_digest_methods();
627 if ((e = ENGINE_new()) == NULL)
630 if (!ENGINE_set_id(e, "devcrypto")
631 || !ENGINE_set_name(e, "/dev/crypto engine")
632 || !ENGINE_set_destroy_function(e, devcrypto_unload)
635 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
636 * implementations, it seems to only exist in FreeBSD, and regarding the
637 * parameters in its crypt_kop, the manual crypto(4) has this to say:
639 * The semantics of these arguments are currently undocumented.
641 * Reading through the FreeBSD source code doesn't give much more than
642 * their CRK_MOD_EXP implementation for ubsec.
644 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
645 * structure as well as the command (CRK_*) in cryptodev.h, but no support
646 * seems to be implemented at all for the moment.
648 * At the time of writing, it seems impossible to write proper support for
649 * FreeBSD's asym features without some very deep knowledge and access to
650 * specific kernel modules.
652 * /Richard Levitte, 2017-05-11
655 # ifndef OPENSSL_NO_RSA
656 || !ENGINE_set_RSA(e, devcrypto_rsa)
658 # ifndef OPENSSL_NO_DSA
659 || !ENGINE_set_DSA(e, devcrypto_dsa)
661 # ifndef OPENSSL_NO_DH
662 || !ENGINE_set_DH(e, devcrypto_dh)
664 # ifndef OPENSSL_NO_EC
665 || !ENGINE_set_EC(e, devcrypto_ec)
668 || !ENGINE_set_ciphers(e, devcrypto_ciphers)
669 #ifdef IMPLEMENT_DIGEST
670 || !ENGINE_set_digests(e, devcrypto_digests)
678 ENGINE_free(e); /* Loose our local reference */