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) < 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) < 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. Otherwise, we would be forced to maintain a cache, which is
333 * perilous if there's a lot of data coming in (if someone wants to checksum
334 * an OpenSSL tarball, for example).
336 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
338 /******************************************************************************
342 * Because they all do the same basic operation, we have only one set of
343 * method functions for them all to share, and a mapping table between
344 * NIDs and cryptodev IDs, with all the necessary size data.
350 struct session_op sess;
354 static const struct digest_data_st {
359 #ifndef OPENSSL_NO_MD5
360 { NID_md5, 16, CRYPTO_MD5 },
362 { NID_sha1, 20, CRYPTO_SHA1 },
363 #ifndef OPENSSL_NO_RMD160
364 # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
365 { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
368 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
369 { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
371 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
372 { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
374 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
375 { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
377 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
378 { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
382 static size_t get_digest_data_index(int nid)
386 for (i = 0; i < OSSL_NELEM(digest_data); i++)
387 if (nid == digest_data[i].nid)
391 * Code further down must make sure that only NIDs in the table above
392 * are used. If any other NID reaches this function, there's a grave
393 * coding error further down.
395 assert("Code that never should be reached" == NULL);
399 static const struct digest_data_st *get_digest_data(int nid)
401 return &digest_data[get_digest_data_index(nid)];
405 * Following are the four necessary functions to map OpenSSL functionality
409 static int digest_init(EVP_MD_CTX *ctx)
411 struct digest_ctx *digest_ctx =
412 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
413 const struct digest_data_st *digest_d =
414 get_digest_data(EVP_MD_CTX_type(ctx));
416 if (digest_ctx->init == 0
417 && (digest_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
418 SYSerr(SYS_F_OPEN, errno);
422 digest_ctx->init = 1;
424 memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
425 digest_ctx->sess.mac = digest_d->devcryptoid;
426 if (ioctl(digest_ctx->cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
427 SYSerr(SYS_F_IOCTL, errno);
428 close(digest_ctx->cfd);
435 static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
436 void *res, unsigned int flags)
438 struct crypt_op cryp;
440 memset(&cryp, 0, sizeof(cryp));
441 cryp.ses = ctx->sess.ses;
443 cryp.src = (void *)src;
447 return ioctl(ctx->cfd, CIOCCRYPT, &cryp);
450 static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
452 struct digest_ctx *digest_ctx =
453 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
458 if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
459 SYSerr(SYS_F_IOCTL, errno);
466 static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
468 struct digest_ctx *digest_ctx =
469 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
471 if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
472 SYSerr(SYS_F_IOCTL, errno);
475 if (ioctl(digest_ctx->cfd, CIOCFSESSION, &digest_ctx->sess) < 0) {
476 SYSerr(SYS_F_IOCTL, errno);
483 static int digest_cleanup(EVP_MD_CTX *ctx)
485 struct digest_ctx *digest_ctx =
486 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
488 if (close(digest_ctx->cfd) < 0) {
489 SYSerr(SYS_F_CLOSE, errno);
497 * Keep a table of known nids and associated methods.
498 * Note that known_digest_nids[] isn't necessarily indexed the same way as
499 * digest_data[] above, which known_digest_methods[] is.
501 static int known_digest_nids[OSSL_NELEM(digest_data)];
502 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
503 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
505 static void prepare_digest_methods()
508 struct session_op sess;
511 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
514 memset(&sess, 0, sizeof(sess));
516 for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
520 * Check that the algo is really availably by trying to open and close
523 sess.mac = digest_data[i].devcryptoid;
524 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
525 || ioctl(cfd, CIOCFSESSION, &sess) < 0)
528 if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
530 || !EVP_MD_meth_set_result_size(known_digest_methods[i],
531 digest_data[i].digestlen)
532 || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
533 || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
534 || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
535 || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
536 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
537 sizeof(struct digest_ctx))) {
538 EVP_MD_meth_free(known_digest_methods[i]);
539 known_digest_methods[i] = NULL;
541 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()
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 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
602 destroy_all_digest_methods();
607 * This engine is always built into libcrypto, so it doesn't offer any
608 * ability to be dynamically loadable.
610 void engine_load_devcrypto_int()
614 if (access("/dev/crypto", R_OK | W_OK) < 0) {
616 "/dev/crypto not present, not enabling devcrypto engine\n");
620 prepare_cipher_methods();
621 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
622 prepare_digest_methods();
625 if ((e = ENGINE_new()) == NULL)
628 if (!ENGINE_set_id(e, "devcrypto")
629 || !ENGINE_set_name(e, "/dev/crypto engine")
630 || !ENGINE_set_destroy_function(e, devcrypto_unload)
633 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
634 * implementations, it seems to only exist in FreeBSD, and regarding the
635 * parameters in its crypt_kop, the manual crypto(4) has this to say:
637 * The semantics of these arguments are currently undocumented.
639 * Reading through the FreeBSD source code doesn't give much more than
640 * their CRK_MOD_EXP implementation for ubsec.
642 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
643 * structure as well as the command (CRK_*) in cryptodev.h, but no support
644 * seems to be implemented at all for the moment.
646 * At the time of writing, it seems impossible to write proper support for
647 * FreeBSD's asym features without some very deep knowledge and access to
648 * specific kernel modules.
650 * /Richard Levitte, 2017-05-11
653 # ifndef OPENSSL_NO_RSA
654 || !ENGINE_set_RSA(e, devcrypto_rsa)
656 # ifndef OPENSSL_NO_DSA
657 || !ENGINE_set_DSA(e, devcrypto_dsa)
659 # ifndef OPENSSL_NO_DH
660 || !ENGINE_set_DH(e, devcrypto_dh)
662 # ifndef OPENSSL_NO_EC
663 || !ENGINE_set_EC(e, devcrypto_ec)
666 || !ENGINE_set_ciphers(e, devcrypto_ciphers)
667 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
668 || !ENGINE_set_digests(e, devcrypto_digests)
676 ENGINE_free(e); /* Loose our local reference */