Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/opencrypto/blake2_test.c
Show First 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | for (i = 0; i < sizeof(key2b); i++) | ||||
key2b[i] = (uint8_t)i; | key2b[i] = (uint8_t)i; | ||||
for (i = 0; i < sizeof(key2s); i++) | for (i = 0; i < sizeof(key2s); i++) | ||||
key2s[i] = (uint8_t)i; | key2s[i] = (uint8_t)i; | ||||
for (i = 0; i < sizeof(katbuf); i++) | for (i = 0; i < sizeof(katbuf); i++) | ||||
katbuf[i] = (uint8_t)i; | katbuf[i] = (uint8_t)i; | ||||
} | } | ||||
static int | static int | ||||
lookup_crid(int fd, const char *devname) | |||||
{ | |||||
struct crypt_find_op find; | |||||
find.crid = -1; | |||||
strlcpy(find.name, devname, sizeof(find.name)); | |||||
ATF_REQUIRE(ioctl(fd, CIOCFINDDEV, &find) != -1); | |||||
return (find.crid); | |||||
} | |||||
static int | |||||
get_handle_fd(void) | get_handle_fd(void) | ||||
{ | { | ||||
int dc_fd, fd; | int dc_fd, fd; | ||||
dc_fd = open("/dev/crypto", O_RDWR); | dc_fd = open("/dev/crypto", O_RDWR); | ||||
/* | /* | ||||
* Why do we do this dance instead of just operating on /dev/crypto | * Why do we do this dance instead of just operating on /dev/crypto | ||||
Show All 33 Lines | do_cryptop(int fd, int ses, size_t inlen, void *out) | ||||
cop.ses = ses; | cop.ses = ses; | ||||
cop.len = inlen; | cop.len = inlen; | ||||
cop.src = katbuf; | cop.src = katbuf; | ||||
cop.mac = out; | cop.mac = out; | ||||
ATF_CHECK_MSG(ioctl(fd, CIOCCRYPT, &cop) >= 0, "ioctl(CIOCCRYPT)"); | ATF_CHECK_MSG(ioctl(fd, CIOCCRYPT, &cop) >= 0, "ioctl(CIOCCRYPT)"); | ||||
} | } | ||||
static void | static void | ||||
test_blake2b_vectors(int crid, const char *modname) | test_blake2b_vectors(const char *devname, const char *modname) | ||||
{ | { | ||||
uint8_t hash[BLAKE2B_OUTBYTES]; | uint8_t hash[BLAKE2B_OUTBYTES]; | ||||
int fd, ses; | int crid, fd, ses; | ||||
size_t i; | size_t i; | ||||
ATF_REQUIRE_KERNEL_MODULE(modname); | ATF_REQUIRE_KERNEL_MODULE(modname); | ||||
ATF_REQUIRE_KERNEL_MODULE("cryptodev"); | ATF_REQUIRE_KERNEL_MODULE("cryptodev"); | ||||
initialize_constant_buffers(); | initialize_constant_buffers(); | ||||
fd = get_handle_fd(); | fd = get_handle_fd(); | ||||
crid = lookup_crid(fd, devname); | |||||
ses = create_session(fd, CRYPTO_BLAKE2B, crid, key2b, sizeof(key2b)); | ses = create_session(fd, CRYPTO_BLAKE2B, crid, key2b, sizeof(key2b)); | ||||
cem: I’d prefer to do the lookup_crid on a statement of its own. Just a style nit. | |||||
for (i = 0; i < sizeof(katbuf); i++) { | for (i = 0; i < sizeof(katbuf); i++) { | ||||
do_cryptop(fd, ses, i, hash); | do_cryptop(fd, ses, i, hash); | ||||
ATF_CHECK_EQ_MSG( | ATF_CHECK_EQ_MSG( | ||||
memcmp(hash, blake2b_keyed_kat[i], sizeof(hash)), | memcmp(hash, blake2b_keyed_kat[i], sizeof(hash)), | ||||
0, | 0, | ||||
"different at %zu", i); | "different at %zu", i); | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
test_blake2s_vectors(int crid, const char *modname) | test_blake2s_vectors(const char *devname, const char *modname) | ||||
{ | { | ||||
uint8_t hash[BLAKE2S_OUTBYTES]; | uint8_t hash[BLAKE2S_OUTBYTES]; | ||||
int fd, ses; | int crid, fd, ses; | ||||
size_t i; | size_t i; | ||||
ATF_REQUIRE_KERNEL_MODULE(modname); | ATF_REQUIRE_KERNEL_MODULE(modname); | ||||
ATF_REQUIRE_KERNEL_MODULE("cryptodev"); | ATF_REQUIRE_KERNEL_MODULE("cryptodev"); | ||||
initialize_constant_buffers(); | initialize_constant_buffers(); | ||||
fd = get_handle_fd(); | fd = get_handle_fd(); | ||||
crid = lookup_crid(fd, devname); | |||||
ses = create_session(fd, CRYPTO_BLAKE2S, crid, key2s, sizeof(key2s)); | ses = create_session(fd, CRYPTO_BLAKE2S, crid, key2s, sizeof(key2s)); | ||||
for (i = 0; i < sizeof(katbuf); i++) { | for (i = 0; i < sizeof(katbuf); i++) { | ||||
do_cryptop(fd, ses, i, hash); | do_cryptop(fd, ses, i, hash); | ||||
ATF_CHECK_EQ_MSG( | ATF_CHECK_EQ_MSG( | ||||
memcmp(hash, blake2s_keyed_kat[i], sizeof(hash)), | memcmp(hash, blake2s_keyed_kat[i], sizeof(hash)), | ||||
0, | 0, | ||||
"different at %zu", i); | "different at %zu", i); | ||||
} | } | ||||
} | } | ||||
ATF_TC_WITHOUT_HEAD(blake2b_vectors); | ATF_TC_WITHOUT_HEAD(blake2b_vectors); | ||||
ATF_TC_BODY(blake2b_vectors, tc) | ATF_TC_BODY(blake2b_vectors, tc) | ||||
{ | { | ||||
ATF_REQUIRE_SYSCTL_INT("kern.cryptodevallowsoft", 1); | ATF_REQUIRE_SYSCTL_INT("kern.cryptodevallowsoft", 1); | ||||
test_blake2b_vectors(CRYPTO_FLAG_SOFTWARE, "nexus/cryptosoft"); | test_blake2b_vectors("cryptosoft0", "nexus/cryptosoft"); | ||||
} | } | ||||
ATF_TC_WITHOUT_HEAD(blake2s_vectors); | ATF_TC_WITHOUT_HEAD(blake2s_vectors); | ||||
ATF_TC_BODY(blake2s_vectors, tc) | ATF_TC_BODY(blake2s_vectors, tc) | ||||
{ | { | ||||
ATF_REQUIRE_SYSCTL_INT("kern.cryptodevallowsoft", 1); | ATF_REQUIRE_SYSCTL_INT("kern.cryptodevallowsoft", 1); | ||||
test_blake2s_vectors(CRYPTO_FLAG_SOFTWARE, "nexus/cryptosoft"); | test_blake2s_vectors("cryptosoft0", "nexus/cryptosoft"); | ||||
} | } | ||||
#if defined(__i386__) || defined(__amd64__) | #if defined(__i386__) || defined(__amd64__) | ||||
ATF_TC_WITHOUT_HEAD(blake2b_vectors_x86); | ATF_TC_WITHOUT_HEAD(blake2b_vectors_x86); | ||||
ATF_TC_BODY(blake2b_vectors_x86, tc) | ATF_TC_BODY(blake2b_vectors_x86, tc) | ||||
{ | { | ||||
if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false)) | ATF_REQUIRE_SYSCTL_INT("kern.cryptodevallowsoft", 1); | ||||
atf_tc_skip("https://bugs.freebsd.org/245825"); | test_blake2b_vectors("blaketwo0", "nexus/blake2"); | ||||
test_blake2b_vectors(CRYPTO_FLAG_HARDWARE, "nexus/blake2"); | |||||
} | } | ||||
ATF_TC_WITHOUT_HEAD(blake2s_vectors_x86); | ATF_TC_WITHOUT_HEAD(blake2s_vectors_x86); | ||||
ATF_TC_BODY(blake2s_vectors_x86, tc) | ATF_TC_BODY(blake2s_vectors_x86, tc) | ||||
{ | { | ||||
if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false)) | ATF_REQUIRE_SYSCTL_INT("kern.cryptodevallowsoft", 1); | ||||
atf_tc_skip("https://bugs.freebsd.org/245825"); | test_blake2s_vectors("blaketwo0", "nexus/blake2"); | ||||
test_blake2s_vectors(CRYPTO_FLAG_HARDWARE, "nexus/blake2"); | |||||
} | } | ||||
#endif | #endif | ||||
ATF_TP_ADD_TCS(tp) | ATF_TP_ADD_TCS(tp) | ||||
{ | { | ||||
ATF_TP_ADD_TC(tp, blake2b_vectors); | ATF_TP_ADD_TC(tp, blake2b_vectors); | ||||
ATF_TP_ADD_TC(tp, blake2s_vectors); | ATF_TP_ADD_TC(tp, blake2s_vectors); | ||||
#if defined(__i386__) || defined(__amd64__) | #if defined(__i386__) || defined(__amd64__) | ||||
ATF_TP_ADD_TC(tp, blake2b_vectors_x86); | ATF_TP_ADD_TC(tp, blake2b_vectors_x86); | ||||
ATF_TP_ADD_TC(tp, blake2s_vectors_x86); | ATF_TP_ADD_TC(tp, blake2s_vectors_x86); | ||||
#endif | #endif | ||||
return (atf_no_error()); | return (atf_no_error()); | ||||
} | } |
I’d prefer to do the lookup_crid on a statement of its own. Just a style nit.