2 * Copyright 2017 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
11 #include <sys/types.h>
14 #include <sys/ioctl.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 #include "internal/engine.h"
28 #ifdef CRYPTO_ALGORITHM_MIN
29 # define CHECK_BSD_STYLE_MACROS
32 /******************************************************************************
36 * Because they all do the same basic operation, we have only one set of
37 * method functions for them all to share, and a mapping table between
38 * NIDs and cryptodev IDs, with all the necessary size data.
44 struct session_op sess;
46 /* to pass from init to do_cipher */
47 const unsigned char *iv;
48 int op; /* COP_ENCRYPT or COP_DECRYPT */
51 static const struct cipher_data_st {
59 #ifndef OPENSSL_NO_DES
60 { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
61 { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
64 { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
66 #ifndef OPENSSL_NO_CAST
67 { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
69 { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
70 { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
71 { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
72 #ifndef OPENSSL_NO_RC4
73 { NID_rc4, 1, 16, 0, CRYPTO_ARC4 },
75 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
76 { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
77 { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
78 { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
80 #if 0 /* Not yet supported */
81 { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
82 { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
84 #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
85 { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
86 { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
87 { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
89 #if 0 /* Not yet supported */
90 { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
91 { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
92 { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
94 #ifndef OPENSSL_NO_CAMELLIA
95 { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
96 CRYPTO_CAMELLIA_CBC },
97 { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
98 CRYPTO_CAMELLIA_CBC },
99 { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
100 CRYPTO_CAMELLIA_CBC },
104 static size_t get_cipher_data_index(int nid)
108 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
109 if (nid == cipher_data[i].nid)
113 * Code further down must make sure that only NIDs in the table above
114 * are used. If any other NID reaches this function, there's a grave
115 * coding error further down.
117 assert("Code that never should be reached" == NULL);
121 static const struct cipher_data_st *get_cipher_data(int nid)
123 return &cipher_data[get_cipher_data_index(nid)];
127 * Following are the three necessary functions to map OpenSSL functionality
131 static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
132 const unsigned char *iv, int enc)
134 struct cipher_ctx *cipher_ctx =
135 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
136 const struct cipher_data_st *cipher_d =
137 get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
139 if ((cipher_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
140 SYSerr(SYS_F_OPEN, errno);
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(cipher_ctx->cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
150 SYSerr(SYS_F_IOCTL, errno);
151 close(cipher_ctx->cfd);
158 static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
159 const unsigned char *in, size_t inl)
161 struct cipher_ctx *cipher_ctx =
162 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
163 struct crypt_op cryp;
164 #if !defined(COP_FLAG_WRITE_IV)
165 unsigned char saved_iv[EVP_MAX_IV_LENGTH];
168 memset(&cryp, 0, sizeof(cryp));
169 cryp.ses = cipher_ctx->sess.ses;
171 cryp.src = (void *)in;
172 cryp.dst = (void *)out;
173 cryp.iv = (void *)EVP_CIPHER_CTX_iv_noconst(ctx);
174 cryp.op = cipher_ctx->op;
175 #if !defined(COP_FLAG_WRITE_IV)
178 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
179 assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
180 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
181 unsigned char *ivptr = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
183 memcpy(saved_iv, ivptr, EVP_CIPHER_CTX_iv_length(ctx));
187 cryp.flags = COP_FLAG_WRITE_IV;
190 if (ioctl(cipher_ctx->cfd, CIOCCRYPT, &cryp) < 0) {
191 SYSerr(SYS_F_IOCTL, errno);
195 #if !defined(COP_FLAG_WRITE_IV)
196 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
197 unsigned char *ivptr = saved_iv;
199 assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
200 if (!EVP_CIPHER_CTX_encrypting(ctx))
201 ivptr = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
203 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), ivptr,
204 EVP_CIPHER_CTX_iv_length(ctx));
211 static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
213 struct cipher_ctx *cipher_ctx =
214 (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
216 if (ioctl(cipher_ctx->cfd, CIOCFSESSION, &cipher_ctx->sess) < 0) {
217 SYSerr(SYS_F_IOCTL, errno);
220 if (close(cipher_ctx->cfd) < 0) {
221 SYSerr(SYS_F_CLOSE, errno);
229 * Keep a table of known nids and associated methods.
230 * Note that known_cipher_nids[] isn't necessarely indexed the same way as
231 * cipher_data[] above, which known_cipher_methods[] is.
233 static int known_cipher_nids[OSSL_NELEM(cipher_data)];
234 static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
235 static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
237 static void prepare_cipher_methods()
240 struct session_op sess;
243 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
246 memset(&sess, 0, sizeof(sess));
247 sess.key = (void *)"01234567890123456789012345678901234567890123456789";
249 for (i = 0, known_cipher_nids_amount = 0;
250 i < OSSL_NELEM(cipher_data); i++) {
253 * Check that the algo is really availably by trying to open and close
256 sess.cipher = cipher_data[i].devcryptoid;
257 sess.keylen = cipher_data[i].keylen;
258 if (ioctl(cfd, CIOCGSESSION, &sess) < 0
259 || ioctl(cfd, CIOCFSESSION, &sess) < 0)
262 if ((known_cipher_methods[i] =
263 EVP_CIPHER_meth_new(cipher_data[i].nid,
264 cipher_data[i].blocksize,
265 cipher_data[i].keylen)) == NULL
266 || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
267 cipher_data[i].ivlen)
268 || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
270 | EVP_CIPH_FLAG_DEFAULT_ASN1)
271 || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
272 || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
274 || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
276 || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
277 sizeof(struct cipher_ctx))) {
278 EVP_CIPHER_meth_free(known_cipher_methods[i]);
279 known_cipher_methods[i] = NULL;
281 known_cipher_nids[known_cipher_nids_amount++] =
289 static const EVP_CIPHER *get_cipher_method(int nid)
291 size_t i = get_cipher_data_index(nid);
295 return known_cipher_methods[i];
298 static int get_cipher_nids(const int **nids)
300 *nids = known_cipher_nids;
301 return known_cipher_nids_amount;
304 static void destroy_cipher_method(int nid)
306 size_t i = get_cipher_data_index(nid);
308 EVP_CIPHER_meth_free(known_cipher_methods[i]);
309 known_cipher_methods[i] = NULL;
312 static void destroy_all_cipher_methods()
316 for (i = 0; i < OSSL_NELEM(cipher_data); i++)
317 destroy_cipher_method(cipher_data[i].nid);
320 static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
321 const int **nids, int nid)
324 return get_cipher_nids(nids);
326 *cipher = get_cipher_method(nid);
328 return *cipher != NULL;
332 * We only support digests if the cryptodev implementation supports multiple
333 * data updates. Otherwise, we would be forced to maintain a cache, which is
334 * perilous if there's a lot of data coming in (if someone wants to checksum
335 * an OpenSSL tarball, for example).
337 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
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) < 0) {
477 SYSerr(SYS_F_IOCTL, errno);
484 static int digest_cleanup(EVP_MD_CTX *ctx)
486 struct digest_ctx *digest_ctx =
487 (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
489 if (close(digest_ctx->cfd) < 0) {
490 SYSerr(SYS_F_CLOSE, errno);
498 * Keep a table of known nids and associated methods.
499 * Note that known_digest_nids[] isn't necessarely indexed the same way as
500 * digest_data[] above, which known_digest_methods[] is.
502 static int known_digest_nids[OSSL_NELEM(digest_data)];
503 static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
504 static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
506 static void prepare_digest_methods()
509 struct session_op sess;
512 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
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) < 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_cleanup(known_digest_methods[i], digest_cleanup)
537 || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
538 sizeof(struct digest_ctx))) {
539 EVP_MD_meth_free(known_digest_methods[i]);
540 known_digest_methods[i] = NULL;
542 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()
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 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
603 destroy_all_digest_methods();
608 * This engine is always built into libcrypto, so it doesn't offer any
609 * ability to be dynamically loadable.
611 void engine_load_devcrypto_int()
615 if (access("/dev/crypto", R_OK | W_OK) < 0) {
617 "/dev/crypto not present, not enabling devcrypto engine\n");
621 prepare_cipher_methods();
622 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
623 prepare_digest_methods();
626 if ((e = ENGINE_new()) == NULL)
629 if (!ENGINE_set_id(e, "devcrypto")
630 || !ENGINE_set_name(e, "/dev/crypto engine")
631 || !ENGINE_set_destroy_function(e, devcrypto_unload)
634 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
635 * implementations, it seems to only exist in FreeBSD, and regarding the
636 * parameters in its crypt_kop, the manual crypto(4) has this to say:
638 * The semantics of these arguments are currently undocumented.
640 * Reading through the FreeBSD source code doesn't give much more than
641 * their CRK_MOD_EXP implementation for ubsec.
643 * It doesn't look much better with cryptodev-linux. They have the crypt_kop
644 * structure as well as the command (CRK_*) in cryptodev.h, but no support
645 * seems to be implemented at all for the moment.
647 * At the time of writing, it seems impossible to write proper support for
648 * FreeBSD's asym features without some very deep knowledge and access to
649 * specific kernel modules.
651 * /Richard Levitte, 2017-05-11
654 # ifndef OPENSSL_NO_RSA
655 || !ENGINE_set_RSA(e, devcrypto_rsa)
657 # ifndef OPENSSL_NO_DSA
658 || !ENGINE_set_DSA(e, devcrypto_dsa)
660 # ifndef OPENSSL_NO_DH
661 || !ENGINE_set_DH(e, devcrypto_dh)
663 # ifndef OPENSSL_NO_EC
664 || !ENGINE_set_EC(e, devcrypto_ec)
667 || !ENGINE_set_ciphers(e, devcrypto_ciphers)
668 #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
669 || !ENGINE_set_digests(e, devcrypto_digests)
677 ENGINE_free(e); /* Loose our local reference */