|
| 1 | +/* freebsd system includes */ |
| 2 | +#include <sys/param.h> |
| 3 | +#include <sys/module.h> |
| 4 | +#include <sys/kernel.h> |
| 5 | +#include <sys/libkern.h> |
| 6 | +#include <sys/malloc.h> |
| 7 | +#include <sys/systm.h> |
| 8 | + |
| 9 | +/* wolfssl includes */ |
| 10 | +#include <wolfssl/options.h> |
| 11 | +#include <wolfssl/wolfcrypt/settings.h> |
| 12 | +#include <wolfssl/wolfcrypt/aes.h> |
| 13 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 14 | + |
| 15 | +MALLOC_DEFINE(M_BSD_EXAMPLE, "bsd_example", "example kernel memory"); |
| 16 | + |
| 17 | +static int wc_aes_test(void); |
| 18 | +const char * ko_name = "bsdkm_example"; |
| 19 | + |
| 20 | +static int |
| 21 | +example_loader(struct module * m, int what, void * arg) |
| 22 | +{ |
| 23 | + int ret = 0; |
| 24 | + switch (what) { |
| 25 | + case MOD_LOAD: |
| 26 | + printf("info: %s: running wc_aes_test()\n", ko_name); |
| 27 | + ret = wc_aes_test(); |
| 28 | + if (ret != 0) { |
| 29 | + return ECANCELED; |
| 30 | + } |
| 31 | + break; |
| 32 | + case MOD_UNLOAD: |
| 33 | + printf("info: %s: unload\n", ko_name); |
| 34 | + break; |
| 35 | + default: |
| 36 | + printf("info: %s: not implemented: %d\n", ko_name, what); |
| 37 | + return EOPNOTSUPP; |
| 38 | + } |
| 39 | + |
| 40 | + return 0; |
| 41 | +} |
| 42 | + |
| 43 | +static int wc_aes_test(void) |
| 44 | +{ |
| 45 | + int ret = 0; |
| 46 | +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
| 47 | + Aes *aes = NULL; |
| 48 | +#else |
| 49 | + Aes aes[1]; |
| 50 | +#endif |
| 51 | + |
| 52 | + /* "Now is the time for all " w/o trailing 0 */ |
| 53 | + const byte msg[] = { |
| 54 | + 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74, |
| 55 | + 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20, |
| 56 | + 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20 |
| 57 | + }; |
| 58 | + const byte verify[] = |
| 59 | + { |
| 60 | + 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53, |
| 61 | + 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb |
| 62 | + }; |
| 63 | + /* padded to 16-bytes */ |
| 64 | + const byte key[] = "0123456789abcdef "; |
| 65 | + /* padded to 16-bytes */ |
| 66 | + const byte iv[] = "1234567890abcdef "; |
| 67 | + byte cipher[WC_AES_BLOCK_SIZE * 4]; |
| 68 | + |
| 69 | +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
| 70 | + if ((aes = (Aes *)malloc(sizeof(*aes), M_BSD_EXAMPLE, M_WAITOK | M_ZERO)) == NULL) { |
| 71 | + printf("error: %s: xts aes alloc failed\n", ko_name); |
| 72 | + return MEMORY_E; |
| 73 | + } |
| 74 | +#endif |
| 75 | + |
| 76 | + ret = wc_AesInit(aes, NULL, INVALID_DEVID); |
| 77 | + if (ret) { |
| 78 | + printf("error: %s: wc_AesXtsInit returned: %d\n", ko_name, ret); |
| 79 | + goto wc_aes_test_end; |
| 80 | + } |
| 81 | + |
| 82 | + ret = wc_AesSetKey(aes, key, WC_AES_BLOCK_SIZE, iv, AES_ENCRYPTION); |
| 83 | + if (ret) { |
| 84 | + printf("error: %s: wc_AesSetKey returned: %d\n", ko_name, ret); |
| 85 | + goto wc_aes_test_end; |
| 86 | + } |
| 87 | + |
| 88 | + memset(cipher, 0, sizeof(cipher)); |
| 89 | + ret = wc_AesCbcEncrypt(aes, cipher, msg, WC_AES_BLOCK_SIZE); |
| 90 | + if (ret) { |
| 91 | + printf("error: %s: wc_AesCbcEncrypt returned: %d\n", ko_name, ret); |
| 92 | + goto wc_aes_test_end; |
| 93 | + } |
| 94 | + |
| 95 | + if (XMEMCMP(cipher, verify, WC_AES_BLOCK_SIZE)) { |
| 96 | + printf("error: %s: wc_AesCbcDecrypt failed cipher-verify compare\n", |
| 97 | + ko_name); |
| 98 | + ret = -1; |
| 99 | + goto wc_aes_test_end; |
| 100 | + } |
| 101 | + |
| 102 | + if (ret == 0) { |
| 103 | + printf("info: %s: wc_aes_test good\n", ko_name); |
| 104 | + } |
| 105 | + |
| 106 | +wc_aes_test_end: |
| 107 | + wc_AesFree(aes); |
| 108 | +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
| 109 | + if (aes) { |
| 110 | + free(aes, M_BSD_EXAMPLE); |
| 111 | + aes = NULL; |
| 112 | + } |
| 113 | +#endif |
| 114 | + |
| 115 | + return ret; |
| 116 | +} |
| 117 | + |
| 118 | +static moduledata_t example_mod = { |
| 119 | + "bsdkm_example", /* name */ |
| 120 | + example_loader, /* loader */ |
| 121 | + NULL /* extra data */ |
| 122 | +}; |
| 123 | + |
| 124 | +DECLARE_MODULE(bsdkm_example, example_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); |
| 125 | +MODULE_DEPEND(bsdkm_example, libwolfssl, 1, 1, 1); |
0 commit comments