| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "cryptoperf.h" |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static int cp_hash_init_test(struct cp_test *test) |
| 29 |
{ |
| 30 |
struct cp_test_param *params = test->test_params; |
| 31 |
unsigned char *scratchpad = NULL; |
| 32 |
#define MAX_KEYLEN 128 |
| 33 |
unsigned char data[MAX_KEYLEN]; |
| 34 |
|
| 35 |
dbg("Initializing hash test %s\n", test->testname); |
| 36 |
if (!test->driver_name) { |
| 37 |
printf(DRIVER_NAME": missing driver_name for %s\n", |
| 38 |
test->testname); |
| 39 |
return -EFAULT; |
| 40 |
} |
| 41 |
|
| 42 |
if (kcapi_md_init(&test->u.hash.handle, test->driver_name, 0)) { |
| 43 |
printf(DRIVER_NAME": could not allocate shash handle for " |
| 44 |
"%s\n", test->driver_name); |
| 45 |
goto out2; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
if (test->u.hash.hmac) { |
| 50 |
if (kcapi_md_blocksize(test->u.hash.handle) > MAX_KEYLEN) { |
| 51 |
printf(DRIVER_NAME": key length for cipher %s too large %u\n", |
| 52 |
test->driver_name, |
| 53 |
kcapi_md_blocksize(test->u.hash.handle)); |
| 54 |
goto out; |
| 55 |
} |
| 56 |
cp_read_random(data, kcapi_md_blocksize(test->u.hash.handle)); |
| 57 |
if (kcapi_md_setkey(test->u.hash.handle, data, |
| 58 |
kcapi_md_blocksize(test->u.hash.handle))) { |
| 59 |
printf(DRIVER_NAME": key could not be set\n"); |
| 60 |
goto out; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if (params->len < 4) |
| 65 |
params->len = 4; |
| 66 |
|
| 67 |
if (posix_memalign((void *)&scratchpad, |
| 68 |
kcapi_md_blocksize(test->u.hash.handle), |
| 69 |
kcapi_md_blocksize(test->u.hash.handle) * params->len)) { |
| 70 |
printf(DRIVER_NAME": could not allocate scratchpad for " |
| 71 |
"%s\n", test->driver_name); |
| 72 |
goto out; |
| 73 |
} |
| 74 |
|
| 75 |
cp_read_random(scratchpad, |
| 76 |
kcapi_md_blocksize(test->u.hash.handle) * params->len); |
| 77 |
|
| 78 |
test->u.hash.inputlen = |
| 79 |
params->len * kcapi_md_blocksize(test->u.hash.handle); |
| 80 |
test->u.hash.scratchpad = scratchpad; |
| 81 |
return 0; |
| 82 |
|
| 83 |
out: |
| 84 |
kcapi_md_destroy(test->u.hash.handle); |
| 85 |
out2: |
| 86 |
if (scratchpad) |
| 87 |
free(scratchpad); |
| 88 |
return -ENOMEM; |
| 89 |
} |
| 90 |
|
| 91 |
static void cp_hash_fini_test(struct cp_test *test) |
| 92 |
{ |
| 93 |
dbg("Cleaning up shash test %s\n", test->testname); |
| 94 |
kcapi_md_destroy(test->u.hash.handle); |
| 95 |
free(test->u.hash.scratchpad); |
| 96 |
} |
| 97 |
|
| 98 |
static size_t cp_hash_test(struct cp_test *test) |
| 99 |
{ |
| 100 |
kcapi_md_digest(test->u.hash.handle, |
| 101 |
test->u.hash.scratchpad, |
| 102 |
test->u.hash.inputlen, |
| 103 |
test->u.hash.scratchpad, |
| 104 |
kcapi_md_digestsize(test->u.hash.handle)); |
| 105 |
return test->u.hash.inputlen; |
| 106 |
} |
| 107 |
|
| 108 |
struct cp_hash_tests { |
| 109 |
char *testname; |
| 110 |
char *driver_name; |
| 111 |
unsigned int hmac; |
| 112 |
}; |
| 113 |
|
| 114 |
static const struct cp_hash_tests testcases[] = { |
| 115 |
{ "SHA-1(G)", "sha1-generic", 0 }, |
| 116 |
{ "SHA-224(G)", "sha224-generic", 0 }, |
| 117 |
{ "SHA-256(G)", "sha256-generic", 0 }, |
| 118 |
{ "SHA-384(G)", "sha384-generic", 0 }, |
| 119 |
{ "SHA-512(G)", "sha512-generic", 0 }, |
| 120 |
{ "SHA-1(SSSE3)", "sha1-ssse3", 0 }, |
| 121 |
{ "SHA-224(SSSE3)", "sha224-ssse3", 0 }, |
| 122 |
{ "SHA-256(SSSE3)", "sha256-ssse3", 0 }, |
| 123 |
{ "SHA-384(SSSE3)", "sha384-ssse3", 0 }, |
| 124 |
{ "SHA-512(SSSE3)", "sha512-ssse3", 0 }, |
| 125 |
{ "SHA-1(AVX)", "sha1-avx", 0 }, |
| 126 |
{ "SHA-224(AVX)", "sha224-avx", 0 }, |
| 127 |
{ "SHA-256(AVX)", "sha256-avx", 0 }, |
| 128 |
{ "SHA-384(AVX)", "sha384-avx", 0 }, |
| 129 |
{ "SHA-512(AVX)", "sha512-avx", 0 }, |
| 130 |
{ "SHA-1(AVX2)", "sha1-avx2", 0 }, |
| 131 |
{ "SHA-224(AVX2)", "sha224-avx2", 0 }, |
| 132 |
{ "SHA-256(AVX2)", "sha256-avx2", 0 }, |
| 133 |
{ "SHA-384(AVX2)", "sha384-avx2", 0 }, |
| 134 |
{ "SHA-512(AVX2)", "sha512-avx2", 0 }, |
| 135 |
{ "SHA-1(MV-CESA)", "mv-sha1", 0 }, |
| 136 |
{ "SHA-256(MV-CESA)", "mv-sha256", 0 }, |
| 137 |
|
| 138 |
{ "SHA-1(CAAM)", "sha1-caam", 0 }, |
| 139 |
{ "SHA-224(CAAM)", "sha224-caam", 0 }, |
| 140 |
{ "SHA-256(CAAM)", "sha256-caam", 0 }, |
| 141 |
|
| 142 |
{ "SHA-1(STM32)", "stm32-sha1", 0 }, |
| 143 |
{ "SHA-224(STM32)", "stm32-sha224", 0 }, |
| 144 |
{ "SHA-256(STM32)", "stm32-sha256", 0 }, |
| 145 |
|
| 146 |
{ "SHA-1(ARM64 CE)", "sha1-ce", 0 }, |
| 147 |
{ "SHA-224(ARM64 CE)", "sha224-ce", 0 }, |
| 148 |
{ "SHA-256(ARM64 CE)", "sha256-ce", 0 }, |
| 149 |
|
| 150 |
{ "HMAC SHA-1(G)", "hmac(sha1-generic)", 1 }, |
| 151 |
{ "HMAC SHA-224(G)", "hmac(sha224-generic)", 1 }, |
| 152 |
{ "HMAC SHA-256(G)", "hmac(sha256-generic)", 1 }, |
| 153 |
{ "HMAC SHA-384(G)", "hmac(sha384-generic)", 1 }, |
| 154 |
{ "HMAC SHA-512(G)", "hmac(sha512-generic)", 1 }, |
| 155 |
{ "HMAC SHA-1(SSSE3)", "hmac(sha1-ssse3)", 1 }, |
| 156 |
{ "HMAC SHA-224(SSSE3)", "hmac(sha224-ssse3)", 1 }, |
| 157 |
{ "HMAC SHA-256(SSSE3)", "hmac(sha256-ssse3)", 1 }, |
| 158 |
{ "HMAC SHA-384(SSSE3)", "hmac(sha384-ssse3)", 1 }, |
| 159 |
{ "HMAC SHA-512(SSSE3)", "hmac(sha512-ssse3)", 1 }, |
| 160 |
{ "HMAC SHA-1(AVX)", "hmac(sha1-avx)", 1 }, |
| 161 |
{ "HMAC SHA-224(AVX)", "hmac(sha224-avx)", 1 }, |
| 162 |
{ "HMAC SHA-256(AVX)", "hmac(sha256-avx)", 1 }, |
| 163 |
{ "HMAC SHA-384(AVX)", "hmac(sha384-avx)", 1 }, |
| 164 |
{ "HMAC SHA-512(AVX)", "hmac(sha512-avx)", 1 }, |
| 165 |
{ "HMAC SHA-1(AVX2)", "hmac(sha1-avx2)", 1 }, |
| 166 |
{ "HMAC SHA-224(AVX2)", "hmac(sha224-avx2)", 1 }, |
| 167 |
{ "HMAC SHA-256(AVX2)", "hmac(sha256-avx2)", 1 }, |
| 168 |
{ "HMAC SHA-384(AVX2)", "hmac(sha384-avx2)", 1 }, |
| 169 |
{ "HMAC SHA-512(AVX2)", "hmac(sha512-avx2)", 1 }, |
| 170 |
{ "HMAC SHA-1(MV-CESA)", "mv-hmac-sha1", 1 }, |
| 171 |
{ "HMAC SHA-256(MV-CESA)", "mv-hmac-sha256", 1 }, |
| 172 |
|
| 173 |
{ "SM3(G)", "sm3-generic", 0 }, |
| 174 |
|
| 175 |
{ "HMAC SHA-1(CAAM)", "hmac-sha1-caam", 1 }, |
| 176 |
{ "HMAC SHA-224(CAAM)", "hmac-sha224-caam", 1 }, |
| 177 |
{ "HMAC SHA-224(CAAM)", "hmac-sha224-caam", 1 }, |
| 178 |
|
| 179 |
{ "HMAC SHA-1(STM32)", "stm32-hmac-sha1", 1 }, |
| 180 |
{ "HMAC SHA-224(STM32)", "stm32-hmac-sha224", 1 }, |
| 181 |
{ "HMAC SHA-256(STM32)", "stm32-hmac-sha256", 1 }, |
| 182 |
|
| 183 |
{ "MD5(G)", "md5-generic", 0 }, |
| 184 |
{ "MD5(MV-CESA)", "mv-md5", 0 }, |
| 185 |
{ "HMAC SM3(G)", "hmac(sm3-generic)", 1 }, |
| 186 |
{ "HMAC MD5(G)", "hmac(md5-generic)", 1 }, |
| 187 |
{ "HMAC MD5(MV-CESA)", "mv-hmac-md5", 1 }, |
| 188 |
|
| 189 |
{ "MD5(CAAM)", "md5-caam", 0 }, |
| 190 |
{ "HMAC MD5(CAAM)", "hmac-md5-caam", 1 }, |
| 191 |
|
| 192 |
{ "MD5(STM32)", "stm32-md5", 0 }, |
| 193 |
{ "HMAC MD5(STM32)", "stm32-hmac-md5", 1 }, |
| 194 |
}; |
| 195 |
|
| 196 |
static struct cp_test cp_hash_testdef[(ARRAY_SIZE(testcases))]; |
| 197 |
|
| 198 |
void cp_hash_register(struct cp_test **hash_test, size_t *entries) |
| 199 |
{ |
| 200 |
size_t i = 0; |
| 201 |
|
| 202 |
for (i = 0; i < ARRAY_SIZE(testcases); i++) { |
| 203 |
cp_hash_testdef[i].enc = 0; |
| 204 |
cp_hash_testdef[i].testname = testcases[i].testname; |
| 205 |
cp_hash_testdef[i].driver_name = testcases[i].driver_name; |
| 206 |
cp_hash_testdef[i].type = "hash"; |
| 207 |
cp_hash_testdef[i].exectime = DFLT_EXECTIME; |
| 208 |
cp_hash_testdef[i].u.hash.hmac = testcases[i].hmac; |
| 209 |
cp_hash_testdef[i].init_test = cp_hash_init_test; |
| 210 |
cp_hash_testdef[i].fini_test = cp_hash_fini_test; |
| 211 |
cp_hash_testdef[i].exec_test = cp_hash_test; |
| 212 |
} |
| 213 |
*hash_test = &cp_hash_testdef[0]; |
| 214 |
*entries = i; |
| 215 |
} |