1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
|
/* wolfkmod.c -- wolfssl FreeBSD kernel module.
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef WOLFSSL_BSDKM
/* freebsd system includes */
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#if defined(BSDKM_CRYPTO_REGISTER)
#include <opencrypto/cryptodev.h>
#include <sys/bus.h>
#include "cryptodev_if.h"
#endif
/* wolf includes */
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef WOLFCRYPT_ONLY
#include <wolfssl/version.h>
#else
#include <wolfssl/ssl.h>
#endif
#ifdef HAVE_FIPS
#ifdef USE_CONTESTMUTEX
#error USE_CONTESTMUTEX is incompatible with WOLFSSL_BSDKM
#endif
#include <wolfssl/wolfcrypt/fips_test.h>
#endif /* HAVE_FIPS */
#if !defined(NO_CRYPT_TEST)
#include <wolfcrypt/test/test.h>
#endif
#if defined(WOLFSSL_KERNEL_BENCHMARKS)
#include <wolfcrypt/benchmark/benchmark.h>
#endif
#include <wolfssl/wolfcrypt/random.h>
MALLOC_DEFINE(M_WOLFSSL, "libwolfssl", "wolfSSL kernel memory");
#if defined(BSDKM_CRYPTO_REGISTER)
#include "bsdkm/wolfkmod_aes.c"
#endif
/* common functions. */
static int wolfkmod_init(void);
static int wolfkmod_cleanup(void);
#if !defined(BSDKM_CRYPTO_REGISTER)
/* functions specific to a pure kernel module library build. */
static int wolfkmod_load(void);
static int wolfkmod_unload(void);
#else
/* functions specific to a kernel crypto driver module build. */
static void wolfkdriv_identify(driver_t * driver, device_t parent);
static int wolfkdriv_probe(device_t dev);
static int wolfkdriv_attach(device_t dev);
static int wolfkdriv_detach(device_t dev);
static int wolfkdriv_probesession(device_t dev,
const struct crypto_session_params *csp);
static int wolfkdriv_newsession(device_t dev, crypto_session_t cses,
const struct crypto_session_params *csp);
static void wolfkdriv_freesession(device_t dev, crypto_session_t cses);
static int wolfkdriv_process(device_t dev, struct cryptop *crp, int hint);
#endif /* !BSDKM_CRYPTO_REGISTER */
#if defined(WOLFSSL_AESNI) || defined(WOLFSSL_KERNEL_BENCHMARKS)
#include "bsdkm/x86_vecreg.c"
#endif /* WOLFSSL_AESNI || WOLFSSL_KERNEL_BENCHMARKS*/
#ifdef HAVE_FIPS
#define WOLFKMOD_FIPS_ERR_MSG(hash) ({ \
printf("In-core integrity hash check failure.\n"); \
if ((hash)) \
printf("Rebuild with \"WOLFCRYPT_FIPS_CORE_HASH_VALUE=%s\".\n", \
hash); \
else \
printf("error: could not compute new hash. " \
"Contact customer support.\n"); \
})
static void wolfkmod_fips_cb(int ok, int err, const char * hash)
{
if ((!ok) || (err != 0)) {
printf("error: libwolfssl FIPS error: %s\n",
wc_GetErrorString(err));
}
if (err == WC_NO_ERR_TRACE(IN_CORE_FIPS_E)) {
WOLFKMOD_FIPS_ERR_MSG(hash);
}
}
#endif /* HAVE_FIPS */
static int wolfkmod_init(void)
{
int error = 0;
#if defined(WOLFSSL_AESNI) || defined(WOLFSSL_KERNEL_BENCHMARKS)
error = wolfkmod_vecreg_init();
if (error != 0) {
printf("error: wolfkmod_vecreg_init: %d\n", error);
return (ECANCELED);
}
#endif /* WOLFSSL_AESNI || WOLFSSL_KERNEL_BENCHMARKS*/
#ifdef HAVE_FIPS
error = wolfCrypt_SetCb_fips(wolfkmod_fips_cb);
if (error != 0) {
printf("error: wolfCrypt_SetCb_fips failed: %s\n",
wc_GetErrorString(error));
return (ECANCELED);
}
fipsEntry();
error = wolfCrypt_GetStatus_fips();
if (error != 0) {
printf("error: wolfCrypt_GetStatus_fips failed: %d: %s\n",
error, wc_GetErrorString(error));
if (error == WC_NO_ERR_TRACE(IN_CORE_FIPS_E)) {
const char *newhash = wolfCrypt_GetCoreHash_fips();
WOLFKMOD_FIPS_ERR_MSG(newhash);
}
return (ECANCELED);
}
#endif /* HAVE_FIPS */
#ifdef WC_RNG_SEED_CB
error = wc_SetSeed_Cb(WC_GENERATE_SEED_DEFAULT);
if (error < 0) {
printf("error: wc_SetSeed_Cb failed: %d\n", error);
return (ECANCELED);
}
#endif /* WC_RNG_SEED_CB */
#ifdef WOLFCRYPT_ONLY
error = wolfCrypt_Init();
if (error != 0) {
printf("error: wolfCrypt_Init failed: %s\n", wc_GetErrorString(error));
return (ECANCELED);
}
#else
error = wolfSSL_Init();
if (error != WOLFSSL_SUCCESS) {
printf("error: wolfSSL_Init failed: %s\n", wc_GetErrorString(error));
return (ECANCELED);
}
#endif /* WOLFCRYPT_ONLY */
#ifdef HAVE_FIPS
error = wc_RunAllCast_fips();
if (error != 0) {
printf("error: wc_RunAllCast_fips failed with "
"return value %d\n", error);
return (ECANCELED);
}
else {
printf("info: FIPS 140-3 wolfCrypt-fips v%d.%d.%d%s%s startup "
"self-test succeeded.\n",
#ifdef HAVE_FIPS_VERSION_MAJOR
HAVE_FIPS_VERSION_MAJOR,
#else
HAVE_FIPS_VERSION,
#endif
#ifdef HAVE_FIPS_VERSION_MINOR
HAVE_FIPS_VERSION_MINOR,
#else
0,
#endif
#ifdef HAVE_FIPS_VERSION_PATCH
HAVE_FIPS_VERSION_PATCH,
#else
0,
#endif
#ifdef HAVE_FIPS_VERSION_PORT
"-",
HAVE_FIPS_VERSION_PORT
#else
"",
""
#endif
);
}
#endif /* HAVE_FIPS */
return (0);
}
static int wolfkmod_cleanup(void)
{
int error = 0;
#ifdef WOLFCRYPT_ONLY
error = wolfCrypt_Cleanup();
if (error != 0) {
printf("error: wolfCrypt_Cleanup failed: %s\n",
wc_GetErrorString(error));
error = ECANCELED;
goto wolfkmod_cleanup_out;
}
#else
error = wolfSSL_Cleanup();
if (error != WOLFSSL_SUCCESS) {
printf("error: wolfSSL_Cleanup failed: %s\n",
wc_GetErrorString(error));
error = ECANCELED;
goto wolfkmod_cleanup_out;
}
#endif /* WOLFCRYPT_ONLY */
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
printf("info: libwolfssl " LIBWOLFSSL_VERSION_STRING
" cleanup complete.\n");
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
error = 0;
wolfkmod_cleanup_out:
#if defined(WOLFSSL_AESNI) || defined(WOLFSSL_KERNEL_BENCHMARKS)
wolfkmod_vecreg_exit();
#endif /* WOLFSSL_AESNI || WOLFSSL_KERNEL_BENCHMARKS*/
return (error);
}
#if !defined(BSDKM_CRYPTO_REGISTER)
static int wolfkmod_load(void)
{
int error = 0;
error = wolfkmod_init();
if (error != 0) {
return (ECANCELED);
}
#ifndef NO_CRYPT_TEST
error = wolfcrypt_test(NULL);
if (error != 0) {
printf("error: wolfcrypt test failed: %d\n", error);
(void)wolfkmod_cleanup();
return (ECANCELED);
}
printf("info: wolfCrypt self-test passed.\n");
#endif /* NO_CRYPT_TEST */
#ifdef WOLFSSL_KERNEL_BENCHMARKS
error = benchmark_test(NULL);
if (error != 0) {
printf("error: wolfcrypt benchmark failed: %d\n", error);
(void)wolfkmod_cleanup();
return (ECANCELED);
}
printf("info: wolfCrypt benchmark passed.\n");
#endif /* WOLFSSL_KERNEL_BENCHMARKS */
printf("info: libwolfssl loaded\n");
return (0);
}
static int wolfkmod_unload(void)
{
int error = 0;
#ifdef HAVE_FIPS
error = wc_RunAllCast_fips();
if (error != 0) {
printf("error: wc_RunAllCast_fips failed at shutdown with "
"return value %d\n", error);
}
else
printf("info: wolfCrypt FIPS re-self-test succeeded at unload: "
"all algorithms re-verified.\n");
#endif
error = wolfkmod_cleanup();
if (error == 0) {
printf("info: libwolfssl unloaded\n");
}
return (error);
}
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
static const char * wolfkmod_event_to_str(modeventtype_t what)
{
switch (what) {
case MOD_LOAD:
return "MOD_LOAD";
case MOD_UNLOAD:
return "MOD_UNLOAD";
case MOD_SHUTDOWN:
return "MOD_SHUTDOWN";
case MOD_QUIESCE:
return "MOD_QUIESCE";
}
}
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
/* see /usr/include/sys/module.h for more info. */
static int
wolfkmod_event(struct module * m, int what, void * arg)
{
int error = 0;
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
printf("info: wolfkmod_event: %s\n", wolfkmod_event_to_str(what));
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
switch (what) {
case MOD_LOAD:
error = wolfkmod_load();
break;
case MOD_UNLOAD:
error = wolfkmod_unload();
break;
case MOD_SHUTDOWN:
case MOD_QUIESCE:
default:
error = EOPNOTSUPP;
}
(void)m;
(void)arg;
return (error);
}
#endif /* !BSDKM_CRYPTO_REGISTER */
#if defined(BSDKM_CRYPTO_REGISTER)
/* wolfkdriv device driver software context. */
struct wolfkdriv_softc {
int32_t crid;
device_t dev;
};
struct km_aes_ctx {
Aes aes_encrypt;
Aes aes_decrypt;
};
typedef struct km_aes_ctx km_aes_ctx;
struct wolfkdriv_session {
km_aes_ctx aes_ctx;
int32_t crid;
int type;
int ivlen;
int klen;
};
typedef struct wolfkdriv_session wolfkdriv_session_t;
static void km_AesFree(Aes * aes) {
if (aes == NULL) {
return;
}
wc_AesFree(aes);
#if defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0)
ForceZero(aes, sizeof(*aes));
#endif
}
static void wolfkdriv_aes_ctx_clear(km_aes_ctx * ctx)
{
if (ctx != NULL) {
km_AesFree(&ctx->aes_encrypt);
km_AesFree(&ctx->aes_decrypt);
}
#ifdef WOLFKM_DEBUG_AES
printf("info: exiting km_AesExitCommon\n");
#endif /* WOLFKM_DEBUG_AES */
}
static void wolfkdriv_identify(driver_t * driver, device_t parent)
{
(void)driver;
/* don't double add wolfkdriv child. */
if (device_find_child(parent, "libwolf", -1) != NULL) {
return;
}
BUS_ADD_CHILD(parent, 10, "libwolf", -1);
}
static int wolfkdriv_probe(device_t dev)
{
device_set_desc(dev, "wolfSSL crypto");
return (BUS_PROBE_DEFAULT);
}
/*
* unregister libwolfssl crypto driver
*/
static void wolfkdriv_unregister(struct wolfkdriv_softc * softc)
{
if (softc && softc->crid >= 0) {
crypto_unregister_all(softc->crid);
device_printf(softc->dev, "info: crid unregistered: %d\n", softc->crid);
softc->crid = -1;
}
return;
}
static int wolfkdriv_attach(device_t dev)
{
struct wolfkdriv_softc * softc = NULL;
int flags = CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC |
CRYPTOCAP_F_ACCEL_SOFTWARE | CRYPTOCAP_F_HARDWARE;
int ret = 0;
int crid = 0;
int error = 0;
ret = wolfkmod_init();
if (ret != 0) {
return (ECANCELED);
}
/**
* register wolfcrypt algs here with crypto_get_driverid.
*
* The crid is the literal index into the kernel crypto_drivers array:
* - crid >= 0 is valid.
* - crid < 0 is error.
* */
softc = device_get_softc(dev);
softc->dev = dev;
softc->crid = crypto_get_driverid(dev, sizeof(wolfkdriv_session_t), flags);
if (softc->crid < 0) {
device_printf(dev, "error: crypto_get_driverid failed: %d\n",
softc->crid);
return (ENXIO);
}
/*
* various sanity checks
*/
/* 1. we should find ourself by name */
crid = crypto_find_driver("libwolf");
if (crid != softc->crid) {
device_printf(dev, "error: attach: got crid %d, expected %d\n", crid,
softc->crid);
error = ENXIO;
goto attach_out;
}
/* 2. test various algs */
error = wolfkdriv_test_aes(dev, crid);
if (error) {
device_printf(dev, "error: attach: test_aes: %d\n", error);
error = ENXIO;
goto attach_out;
}
device_printf(dev, "info: driver loaded: %d\n", crid);
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: exiting attach\n");
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
attach_out:
if (error) {
wolfkdriv_unregister(softc);
error = ENXIO;
}
return (error);
}
static int wolfkdriv_detach(device_t dev)
{
struct wolfkdriv_softc * softc = NULL;
int ret = 0;
ret = wolfkmod_cleanup();
if (ret == 0) {
/* unregister wolfcrypt algs */
softc = device_get_softc(dev);
wolfkdriv_unregister(softc);
}
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: exiting detach\n");
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return (0);
}
static int wolfkdriv_probesession(device_t dev,
const struct crypto_session_params *csp)
{
struct wolfkdriv_softc * softc = NULL;
int error = CRYPTODEV_PROBE_ACCEL_SOFTWARE;
softc = device_get_softc(dev);
switch (csp->csp_mode) {
case CSP_MODE_CIPHER:
switch (csp->csp_cipher_alg) {
case CRYPTO_AES_CBC:
break;
default:
error = EINVAL;
break;
}
break;
case CSP_MODE_AEAD:
switch (csp->csp_cipher_alg) {
case CRYPTO_AES_NIST_GCM_16:
break;
default:
error = EINVAL;
break;
}
break;
case CSP_MODE_DIGEST:
case CSP_MODE_ETA:
default:
error = EINVAL;
break;
}
(void)softc;
(void)csp;
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: probesession: mode=%d, cipher_alg=%d, error=%d\n",
csp->csp_mode, csp->csp_cipher_alg, error);
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return (error);
}
static int wolfkdriv_newsession_aes(device_t dev,
wolfkdriv_session_t * session,
const struct crypto_session_params *csp)
{
int error = 0;
int klen = csp->csp_cipher_klen; /* key len in bytes */
switch (csp->csp_cipher_alg) {
case CRYPTO_AES_NIST_GCM_16:
session->type = CRYPTO_AES_NIST_GCM_16;
break;
case CRYPTO_AES_CBC:
session->type = CRYPTO_AES_CBC;
break;
default:
return (EOPNOTSUPP);
}
if (klen != 16 && klen != 24 && klen != 32) {
device_printf(dev, "info: newsession_cipher: invalid klen: %d\n", klen);
return (EINVAL);
}
session->klen = klen;
session->ivlen = csp->csp_ivlen;
/* encrypt */
error = wc_AesInit(&session->aes_ctx.aes_encrypt, NULL, INVALID_DEVID);
if (error) {
device_printf(dev, "error: newsession_cipher: aes init: %d\n", error);
goto newsession_cipher_out;
}
if (session->type == CRYPTO_AES_CBC) {
/* Need a separate decrypt structure for aes-cbc. */
error = wc_AesInit(&session->aes_ctx.aes_decrypt, NULL, INVALID_DEVID);
if (error) {
device_printf(dev, "error: newsession_cipher: aes init: %d\n",
error);
goto newsession_cipher_out;
}
}
newsession_cipher_out:
if (error != 0) {
wolfkdriv_aes_ctx_clear(&session->aes_ctx);
return (EINVAL);
}
return (error);
}
static int wolfkdriv_newsession(device_t dev, crypto_session_t cses,
const struct crypto_session_params *csp)
{
wolfkdriv_session_t * session = NULL;
int error = 0;
/* get the wolfkdriv_session_t context */
session = crypto_get_driver_session(cses);
switch (csp->csp_mode) {
case CSP_MODE_DIGEST:
case CSP_MODE_ETA:
device_printf(dev, "info: not supported: %d\n", csp->csp_mode);
error = EOPNOTSUPP;
break;
case CSP_MODE_CIPHER:
case CSP_MODE_AEAD:
error = wolfkdriv_newsession_aes(dev, session, csp);
break;
default:
__assert_unreachable();
}
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: newsession: mode=%d, cipher_alg=%d, error=%d\n",
csp->csp_mode, csp->csp_cipher_alg, error);
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return (error);
}
static void
wolfkdriv_freesession(device_t dev, crypto_session_t cses)
{
wolfkdriv_session_t * session = NULL;
(void)dev;
/* get the wolfkdriv_session_t context */
session = crypto_get_driver_session(cses);
/* clean it up */
wolfkdriv_aes_ctx_clear(&session->aes_ctx);
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: exiting freesession\n");
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return;
}
static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
struct cryptop * crp,
const struct crypto_session_params * csp)
{
struct crypto_buffer_cursor cc_in;
struct crypto_buffer_cursor cc_out;
const unsigned char * in_block = NULL;
const unsigned char * in_seg = NULL;
unsigned char * out_block = NULL;
unsigned char * out_seg = NULL;
Aes aes;
uint8_t iv[WC_AES_BLOCK_SIZE];
uint8_t block[EALG_MAX_BLOCK_LEN];
size_t data_len = 0;
size_t seg_len = 0;
size_t in_len = 0;
size_t out_len = 0;
int error = 0;
int is_encrypt = 0;
int type = AES_ENCRYPTION;
if (csp->csp_cipher_alg != CRYPTO_AES_CBC) {
error = EINVAL;
goto cbc_work_out;
}
data_len = crp->crp_payload_length;
if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
is_encrypt = 1;
type = AES_ENCRYPTION;
memcpy(&aes, &session->aes_ctx.aes_encrypt, sizeof(aes));
}
else {
is_encrypt = 0;
type = AES_DECRYPTION;
memcpy(&aes, &session->aes_ctx.aes_decrypt, sizeof(aes));
}
/* must be multiple of block size */
if (data_len % WC_AES_BLOCK_SIZE) {
error = EINVAL;
goto cbc_work_out;
}
crypto_read_iv(crp, iv);
error = wc_AesSetKey(&aes, csp->csp_cipher_key,
csp->csp_cipher_klen, iv, type);
if (error) {
device_printf(dev, "error: wc_AesSetKey: %d\n", error);
goto cbc_work_out;
}
/* set up the crypto buffers */
crypto_cursor_init(&cc_in, &crp->crp_buf);
crypto_cursor_advance(&cc_in, crp->crp_payload_start);
in_seg = crypto_cursor_segment(&cc_in, &in_len);
/* handle if the user supplied a separate out buffer. */
if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
crypto_cursor_init(&cc_out, &crp->crp_obuf);
crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
}
else {
cc_out = cc_in;
}
out_seg = crypto_cursor_segment(&cc_out, &out_len);
while (data_len) {
/* set up input buffers */
if (in_len < WC_AES_BLOCK_SIZE) {
/* less than a block in segment */
crypto_cursor_copydata(&cc_in, WC_AES_BLOCK_SIZE, block);
in_block = block;
in_len = WC_AES_BLOCK_SIZE;
}
else {
in_block = in_seg;
}
/* set up output buffers */
if (out_len < WC_AES_BLOCK_SIZE) {
out_block = block;
out_len = WC_AES_BLOCK_SIZE;
}
else {
out_block = out_seg;
}
/* choose which of data_len, in_len, out_len, is shorter.
* round down to multiple of aes block size. */
seg_len = rounddown(MIN(data_len, MIN(in_len, out_len)),
WC_AES_BLOCK_SIZE);
if (is_encrypt) {
error = wc_AesCbcEncrypt(&aes, out_block, in_block, seg_len);
if (error) {
device_printf(dev, "error: wc_AesCbcEncrypt: %d\n", error);
goto cbc_work_out;
}
}
else {
error = wc_AesCbcDecrypt(&aes, out_block, in_block, seg_len);
if (error) {
device_printf(dev, "error: wc_AesCbcEncrypt: %d\n", error);
goto cbc_work_out;
}
}
if (out_block == block) {
/* we used the block as local output buffer. copy to cc_out,
* and grab the next out cursor segment. */
crypto_cursor_copyback(&cc_out, WC_AES_BLOCK_SIZE, block);
out_seg = crypto_cursor_segment(&cc_out, &out_len);
} else {
/* we worked directly in cc_out. advance the cursor. */
crypto_cursor_advance(&cc_out, seg_len);
out_seg += seg_len;
out_len -= seg_len;
}
if (in_block == block) {
/* grab a new in cursor segment. */
in_seg = crypto_cursor_segment(&cc_in, &in_len);
} else {
/* else advance existing in cursor. */
crypto_cursor_advance(&cc_in, seg_len);
in_seg += seg_len;
in_len -= seg_len;
}
data_len -= seg_len;
}
cbc_work_out:
/* cleanup. */
wc_ForceZero(iv, sizeof(iv));
wc_ForceZero(block, sizeof(block));
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: cbc_work: mode=%d, cipher_alg=%d, "
"payload_length=%d, error=%d\n",
csp->csp_mode, csp->csp_cipher_alg, crp->crp_payload_length,
error);
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return (error);
}
static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
struct cryptop * crp,
const struct crypto_session_params * csp)
{
struct crypto_buffer_cursor cc_in;
struct crypto_buffer_cursor cc_out;
const unsigned char * in_seg = NULL;
unsigned char * out_seg = NULL;
Aes aes;
uint8_t iv[WC_AES_BLOCK_SIZE];
uint8_t auth_tag[WC_AES_BLOCK_SIZE];
size_t data_len = 0;
size_t seg_len = 0;
size_t in_len = 0;
size_t out_len = 0;
int error = 0;
int is_encrypt = 0;
memcpy(&aes, &session->aes_ctx.aes_encrypt, sizeof(aes));
if (csp->csp_cipher_alg != CRYPTO_AES_NIST_GCM_16) {
error = EINVAL;
goto gcm_work_out;
}
data_len = crp->crp_payload_length;
if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
is_encrypt = 1;
}
else {
is_encrypt = 0;
}
error = wc_AesGcmSetKey(&aes, csp->csp_cipher_key,
csp->csp_cipher_klen);
if (error) {
device_printf(dev, "error: wc_AesGcmSetKey: %d\n", error);
goto gcm_work_out;
}
crypto_read_iv(crp, iv);
error = wc_AesGcmInit(&aes, NULL /* key */, 0 /* keylen */,
iv, csp->csp_ivlen);
if (error) {
device_printf(dev, "error: wc_AesGcmInit: %d\n", error);
goto gcm_work_out;
}
/* process aad first */
if (crp->crp_aad != NULL) {
/* they passed aad in separate buffer. */
if (is_encrypt) {
error = wc_AesGcmEncryptUpdate(&aes, NULL, NULL, 0,
crp->crp_aad, crp->crp_aad_length);
}
else {
error = wc_AesGcmDecryptUpdate(&aes, NULL, NULL, 0,
crp->crp_aad, crp->crp_aad_length);
}
if (error) {
error = EINVAL;
goto gcm_work_out;
}
}
else {
/* we need to pull aad out of crp->crp_buf from crp_aad_start. */
size_t aad_len = 0;
crypto_cursor_init(&cc_in, &crp->crp_buf);
crypto_cursor_advance(&cc_in, crp->crp_aad_start);
for (aad_len = crp->crp_aad_length; aad_len > 0; aad_len -= seg_len) {
in_seg = crypto_cursor_segment(&cc_in, &in_len);
seg_len = MIN(aad_len, in_len);
if (is_encrypt) {
error = wc_AesGcmEncryptUpdate(&aes, NULL, NULL, 0,
in_seg, seg_len);
}
else {
error = wc_AesGcmDecryptUpdate(&aes, NULL, NULL, 0,
in_seg, seg_len);
}
if (error) {
error = EINVAL;
goto gcm_work_out;
}
crypto_cursor_advance(&cc_in, seg_len);
}
}
/*
* process cipher/plaintext next
*/
/* set up the crypto buffers */
crypto_cursor_init(&cc_in, &crp->crp_buf);
crypto_cursor_advance(&cc_in, crp->crp_payload_start);
in_seg = crypto_cursor_segment(&cc_in, &in_len);
/* handle if the user supplied a separate out buffer. */
if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
crypto_cursor_init(&cc_out, &crp->crp_obuf);
crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
}
else {
cc_out = cc_in;
}
out_seg = crypto_cursor_segment(&cc_out, &out_len);
while (data_len) {
/* process through the available segments. */
in_seg = crypto_cursor_segment(&cc_in, &in_len);
out_seg = crypto_cursor_segment(&cc_out, &out_len);
seg_len = MIN(data_len, MIN(in_len, out_len));
if (is_encrypt) {
error = wc_AesGcmEncryptUpdate(&aes, out_seg, in_seg, seg_len,
NULL, 0);
if (error) {
device_printf(dev, "error: wc_AesGcmEncrypt: %d\n", error);
goto gcm_work_out;
}
}
else {
error = wc_AesGcmDecryptUpdate(&aes, out_seg, in_seg, seg_len,
NULL, 0);
if (error) {
device_printf(dev, "error: wc_AesGcmDecrypt: %d\n", error);
goto gcm_work_out;
}
}
/* advance the cursors by amount processed */
crypto_cursor_advance(&cc_in, seg_len);
crypto_cursor_advance(&cc_out, seg_len);
data_len -= seg_len;
}
/* process auth tag finally */
if (is_encrypt) {
error = wc_AesGcmEncryptFinal(&aes, auth_tag, WC_AES_BLOCK_SIZE);
if (error == 0) {
crypto_copyback(crp, crp->crp_digest_start, WC_AES_BLOCK_SIZE,
auth_tag);
}
}
else {
crypto_copydata(crp, crp->crp_digest_start, WC_AES_BLOCK_SIZE,
auth_tag);
error = wc_AesGcmDecryptFinal(&aes, auth_tag, WC_AES_BLOCK_SIZE);
if (error) {
error = EBADMSG;
}
}
gcm_work_out:
/* cleanup. */
wc_ForceZero(iv, sizeof(iv));
wc_ForceZero(auth_tag, sizeof(auth_tag));
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: gcm_work: mode=%d, cipher_alg=%d, "
"payload_length=%d, error=%d\n",
csp->csp_mode, csp->csp_cipher_alg, crp->crp_payload_length,
error);
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return (error);
}
static int wolfkdriv_process(device_t dev, struct cryptop * crp, int hint)
{
const struct crypto_session_params * csp = NULL;
wolfkdriv_session_t * session = NULL;
int error = 0;
(void)hint;
session = crypto_get_driver_session(crp->crp_session);
csp = crypto_get_params(crp->crp_session);
switch (csp->csp_mode) {
case CSP_MODE_CIPHER:
error = wolfkdriv_cbc_work(dev, session, crp, csp);
break;
case CSP_MODE_DIGEST:
case CSP_MODE_ETA:
error = EINVAL;
break;
case CSP_MODE_AEAD:
error = wolfkdriv_gcm_work(dev, session, crp, csp);
break;
default:
__assert_unreachable();
}
crp->crp_etype = error;
crypto_done(crp);
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: process: mode=%d, cipher_alg=%d, error=%d\n",
csp->csp_mode, csp->csp_cipher_alg, error);
#endif /* WOLFSSL_BSDKM_VERBOSE_DEBUG */
return (error);
}
/*
* wolfkmod as a crypto device driver.
*/
static device_method_t wolfkdriv_methods[] = {
/* device interface methods: called during device setup, etc. */
DEVMETHOD(device_identify, wolfkdriv_identify),
DEVMETHOD(device_probe, wolfkdriv_probe),
DEVMETHOD(device_attach, wolfkdriv_attach),
DEVMETHOD(device_detach, wolfkdriv_detach),
/* crypto device session methods: called during crypto session setup,
* work, etc. */
DEVMETHOD(cryptodev_probesession, wolfkdriv_probesession),
DEVMETHOD(cryptodev_newsession, wolfkdriv_newsession),
DEVMETHOD(cryptodev_freesession, wolfkdriv_freesession),
DEVMETHOD(cryptodev_process, wolfkdriv_process),
DEVMETHOD_END
};
static driver_t wolfkdriv_driver = {
.name = "libwolf",
.methods = wolfkdriv_methods,
.size = sizeof(struct wolfkdriv_softc),
};
/* on x86, software-only drivers usually attach to nexus bus. */
DRIVER_MODULE(libwolfssl, nexus, wolfkdriv_driver, NULL, NULL);
#endif /* BSDKM_CRYPTO_REGISTER */
#if !defined(BSDKM_CRYPTO_REGISTER)
/*
* wolfkmod as a pure kernel module.
*/
static moduledata_t libwolfmod = {
#ifdef HAVE_FIPS
"libwolfssl_fips", /* module name */
#else
"libwolfssl", /* module name */
#endif /* HAVE_FIPS */
wolfkmod_event, /* module event handler */
NULL /* extra data, unused */
};
DECLARE_MODULE(libwolfssl, libwolfmod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
#endif /* !BSDKM_CRYPTO_REGISTER */
MODULE_VERSION(libwolfssl, 1);
#endif /* WOLFSSL_BSDKM */
|