| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "cryptoperf.h" |
| 21 |
#include <limits.h> |
| 22 |
#include <sys/user.h> |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static int cp_aead_init_test(struct cp_test *test, int enc, int ccm) |
| 29 |
{ |
| 30 |
struct cp_test_param *params = test->test_params; |
| 31 |
unsigned char *input = NULL; |
| 32 |
unsigned char *output = NULL; |
| 33 |
#define MAX_KEYLEN 64 |
| 34 |
#define TAGLEN 16 |
| 35 |
#define BLOCKLEN 16 |
| 36 |
unsigned char data[MAX_KEYLEN]; |
| 37 |
unsigned char ivrand[MAX_KEYLEN]; |
| 38 |
unsigned char *ivdata = NULL; |
| 39 |
uint32_t ivlen = 0; |
| 40 |
size_t pagesize = (size_t)sysconf(_SC_PAGESIZE); |
| 41 |
|
| 42 |
if (pagesize > ULONG_MAX) { |
| 43 |
printf(DRIVER_NAME": unable to determine the page size\n"); |
| 44 |
return -errno; |
| 45 |
} |
| 46 |
|
| 47 |
dbg("Initializing AEAD test %s\n", test->testname); |
| 48 |
if (!test->driver_name) { |
| 49 |
printf(DRIVER_NAME": missing test definition information for %s\n", |
| 50 |
test->testname); |
| 51 |
return -EFAULT; |
| 52 |
} |
| 53 |
|
| 54 |
if (kcapi_aead_init(&test->u.aead.handle, test->driver_name, |
| 55 |
params->aio ? KCAPI_INIT_AIO : 0)) { |
| 56 |
printf(DRIVER_NAME": could not allocate aead handle for " |
| 57 |
"%s\n", test->driver_name); |
| 58 |
goto out; |
| 59 |
} |
| 60 |
|
| 61 |
if (test->u.aead.keysize > MAX_KEYLEN) { |
| 62 |
printf(DRIVER_NAME": key length for cipher %s too large\n", |
| 63 |
test->driver_name); |
| 64 |
goto out; |
| 65 |
} |
| 66 |
|
| 67 |
cp_read_random(data, test->u.aead.keysize); |
| 68 |
if (kcapi_aead_setkey(test->u.aead.handle, data, |
| 69 |
test->u.aead.keysize)) { |
| 70 |
printf(DRIVER_NAME": key could not be set\n"); |
| 71 |
goto out; |
| 72 |
} |
| 73 |
|
| 74 |
cp_read_random(ivrand, BLOCKLEN); |
| 75 |
if (ccm) { |
| 76 |
if (kcapi_aead_ccm_nonce_to_iv(ivrand, 10, &ivdata, &ivlen)) |
| 77 |
goto out; |
| 78 |
} else { |
| 79 |
if (kcapi_pad_iv(test->u.aead.handle, ivrand, |
| 80 |
kcapi_aead_blocksize(test->u.aead.handle), |
| 81 |
&ivdata, &ivlen)) |
| 82 |
goto out; |
| 83 |
} |
| 84 |
test->u.aead.iv = ivdata; |
| 85 |
|
| 86 |
if (kcapi_aead_settaglen(test->u.aead.handle, TAGLEN)) { |
| 87 |
printf(DRIVER_NAME ": Setting of authentication tag length failed\n"); |
| 88 |
goto out; |
| 89 |
} |
| 90 |
|
| 91 |
if (enc) { |
| 92 |
test->u.aead.indatalen = kcapi_aead_inbuflen_enc( |
| 93 |
test->u.aead.handle, BLOCKLEN * params->len, |
| 94 |
test->u.aead.assoclen, TAGLEN); |
| 95 |
test->u.aead.outdatalen = kcapi_aead_outbuflen_enc( |
| 96 |
test->u.aead.handle, BLOCKLEN * params->len, |
| 97 |
test->u.aead.assoclen, TAGLEN); |
| 98 |
} else { |
| 99 |
test->u.aead.indatalen = kcapi_aead_inbuflen_dec( |
| 100 |
test->u.aead.handle, BLOCKLEN * params->len, |
| 101 |
test->u.aead.assoclen, TAGLEN); |
| 102 |
test->u.aead.outdatalen = kcapi_aead_outbuflen_dec( |
| 103 |
test->u.aead.handle, BLOCKLEN * params->len, |
| 104 |
test->u.aead.assoclen, TAGLEN); |
| 105 |
} |
| 106 |
|
| 107 |
if (posix_memalign((void *)&input, pagesize, |
| 108 |
test->u.aead.indatalen * |
| 109 |
(params->aio ? params->aio : 1))) { |
| 110 |
printf(DRIVER_NAME": could not allocate input buffer for " |
| 111 |
"%s\n", test->driver_name); |
| 112 |
goto out; |
| 113 |
} |
| 114 |
if (posix_memalign((void *)&output, pagesize, |
| 115 |
test->u.aead.outdatalen * |
| 116 |
(params->aio ? params->aio : 1))) { |
| 117 |
printf(DRIVER_NAME": could not allocate output buffer for " |
| 118 |
"%s\n", test->driver_name); |
| 119 |
goto out; |
| 120 |
} |
| 121 |
kcapi_aead_setassoclen(test->u.aead.handle, test->u.aead.assoclen); |
| 122 |
|
| 123 |
test->u.aead.input = input; |
| 124 |
test->u.aead.output = output; |
| 125 |
|
| 126 |
if (enc) { |
| 127 |
cp_read_random(input, test->u.aead.indatalen); |
| 128 |
} else { |
| 129 |
ssize_t ret = 0; |
| 130 |
|
| 131 |
cp_read_random(output, test->u.aead.outdatalen); |
| 132 |
ret = kcapi_aead_encrypt(test->u.aead.handle, |
| 133 |
test->u.aead.output, |
| 134 |
test->u.aead.outdatalen, |
| 135 |
test->u.aead.iv, |
| 136 |
test->u.aead.input, |
| 137 |
test->u.aead.indatalen, 0); |
| 138 |
if (ret < 0) { |
| 139 |
printf(DRIVER_NAME": could not create ciphertext for " |
| 140 |
"%s (%zd)\n", test->driver_name, ret); |
| 141 |
goto out; |
| 142 |
} |
| 143 |
|
| 144 |
memcpy(test->u.aead.input, test->u.aead.output, |
| 145 |
test->u.aead.assoclen); |
| 146 |
|
| 147 |
ret = kcapi_aead_decrypt(test->u.aead.handle, |
| 148 |
test->u.aead.input, |
| 149 |
test->u.aead.indatalen, |
| 150 |
test->u.aead.iv, |
| 151 |
test->u.aead.output, |
| 152 |
test->u.aead.outdatalen, |
| 153 |
params->accesstype); |
| 154 |
if (ret < 0) { |
| 155 |
printf(DRIVER_NAME": could not decrypt ciphertext for " |
| 156 |
"%s (%zd)\n", test->driver_name, ret); |
| 157 |
goto out; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (params->aio) { |
| 162 |
unsigned int i; |
| 163 |
|
| 164 |
if (posix_memalign((void *)&test->u.aead.iniov, BLOCKLEN, |
| 165 |
params->aio * sizeof(struct iovec))) { |
| 166 |
printf(DRIVER_NAME": could not allocate iniov buffer\n"); |
| 167 |
goto out; |
| 168 |
} |
| 169 |
if (posix_memalign((void *)&test->u.aead.outiov, BLOCKLEN, |
| 170 |
params->aio * sizeof(struct iovec))) { |
| 171 |
free(test->u.aead.iniov); |
| 172 |
printf(DRIVER_NAME": could not allocate outiov buffer\n"); |
| 173 |
goto out; |
| 174 |
} |
| 175 |
|
| 176 |
for (i = 0; i < params->aio; i++) { |
| 177 |
ssize_t ret = 0; |
| 178 |
|
| 179 |
test->u.aead.iniov[i].iov_base = input; |
| 180 |
test->u.aead.iniov[i].iov_len = test->u.aead.indatalen; |
| 181 |
test->u.aead.outiov[i].iov_base = output; |
| 182 |
test->u.aead.outiov[i].iov_len = test->u.aead.outdatalen; |
| 183 |
|
| 184 |
if (!i) { |
| 185 |
input += test->u.aead.indatalen; |
| 186 |
output += test->u.aead.outdatalen; |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
if (enc) { |
| 191 |
cp_read_random(input, test->u.aead.indatalen); |
| 192 |
} else { |
| 193 |
|
| 194 |
cp_read_random(output, test->u.aead.outdatalen); |
| 195 |
ret = kcapi_aead_encrypt(test->u.aead.handle, |
| 196 |
output, |
| 197 |
test->u.aead.outdatalen, |
| 198 |
test->u.aead.iv, |
| 199 |
input, |
| 200 |
test->u.aead.indatalen, 0); |
| 201 |
if (ret < 0) { |
| 202 |
printf(DRIVER_NAME": could not create ciphertext for " |
| 203 |
"%s (%zd)\n", test->driver_name, ret); |
| 204 |
goto out; |
| 205 |
} |
| 206 |
|
| 207 |
memcpy(input, output, test->u.aead.assoclen); |
| 208 |
|
| 209 |
ret = kcapi_aead_decrypt(test->u.aead.handle, |
| 210 |
input, |
| 211 |
test->u.aead.indatalen, |
| 212 |
test->u.aead.iv, |
| 213 |
output, |
| 214 |
test->u.aead.outdatalen, |
| 215 |
params->accesstype); |
| 216 |
if (ret < 0) { |
| 217 |
printf(DRIVER_NAME": could not decrypt ciphertext for " |
| 218 |
"%s (%zd)\n", test->driver_name, ret); |
| 219 |
goto out; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
input += test->u.aead.indatalen; |
| 224 |
output += test->u.aead.outdatalen; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return 0; |
| 229 |
|
| 230 |
out: |
| 231 |
kcapi_cipher_destroy(test->u.aead.handle); |
| 232 |
if (ivdata) |
| 233 |
free(ivdata); |
| 234 |
if (input) |
| 235 |
free(input); |
| 236 |
if (output) |
| 237 |
free(output); |
| 238 |
return -ENOMEM; |
| 239 |
} |
| 240 |
|
| 241 |
static int cp_aead_init_enc_dflt(struct cp_test *test) |
| 242 |
{ |
| 243 |
return cp_aead_init_test(test, 1, 0); |
| 244 |
} |
| 245 |
|
| 246 |
static int cp_aead_init_enc_ccm(struct cp_test *test) |
| 247 |
{ |
| 248 |
return cp_aead_init_test(test, 1, 1); |
| 249 |
} |
| 250 |
|
| 251 |
static int cp_aead_init_dec_dflt(struct cp_test *test) |
| 252 |
{ |
| 253 |
return cp_aead_init_test(test, 0, 0); |
| 254 |
} |
| 255 |
|
| 256 |
static int cp_aead_init_dec_ccm(struct cp_test *test) |
| 257 |
{ |
| 258 |
return cp_aead_init_test(test, 0, 1); |
| 259 |
} |
| 260 |
|
| 261 |
static void cp_aead_fini_test(struct cp_test *test) |
| 262 |
{ |
| 263 |
struct cp_test_param *params = test->test_params; |
| 264 |
|
| 265 |
dbg("Cleaning up asynchronous symmetric test %s\n", test->testname); |
| 266 |
free(test->u.aead.input); |
| 267 |
free(test->u.aead.output); |
| 268 |
free(test->u.aead.iv); |
| 269 |
if (params->aio) { |
| 270 |
free(test->u.aead.iniov); |
| 271 |
free(test->u.aead.outiov); |
| 272 |
} |
| 273 |
kcapi_cipher_destroy(test->u.aead.handle); |
| 274 |
} |
| 275 |
|
| 276 |
static size_t cp_ablkcipher_enc_test(struct cp_test *test) |
| 277 |
{ |
| 278 |
struct cp_test_param *params = test->test_params; |
| 279 |
|
| 280 |
if (params->aio) |
| 281 |
kcapi_aead_encrypt_aio(test->u.aead.handle, |
| 282 |
test->u.aead.iniov, |
| 283 |
test->u.aead.outiov, |
| 284 |
params->aio, |
| 285 |
test->u.aead.iv, |
| 286 |
params->accesstype); |
| 287 |
else |
| 288 |
kcapi_aead_encrypt(test->u.aead.handle, |
| 289 |
test->u.aead.input, |
| 290 |
test->u.aead.indatalen, |
| 291 |
test->u.aead.iv, |
| 292 |
test->u.aead.output, |
| 293 |
test->u.aead.outdatalen, |
| 294 |
params->accesstype); |
| 295 |
return test->u.aead.outdatalen; |
| 296 |
} |
| 297 |
|
| 298 |
static size_t cp_ablkcipher_dec_test(struct cp_test *test) |
| 299 |
{ |
| 300 |
struct cp_test_param *params = test->test_params; |
| 301 |
|
| 302 |
if (params->aio) |
| 303 |
kcapi_aead_decrypt_aio(test->u.aead.handle, |
| 304 |
test->u.aead.iniov, |
| 305 |
test->u.aead.outiov, |
| 306 |
params->aio, |
| 307 |
test->u.aead.iv, |
| 308 |
params->accesstype); |
| 309 |
else |
| 310 |
kcapi_aead_decrypt(test->u.aead.handle, |
| 311 |
test->u.aead.input, |
| 312 |
test->u.aead.indatalen, |
| 313 |
test->u.aead.iv, |
| 314 |
test->u.aead.output, |
| 315 |
test->u.aead.outdatalen, |
| 316 |
params->accesstype); |
| 317 |
return test->u.aead.outdatalen; |
| 318 |
} |
| 319 |
|
| 320 |
struct cp_aead_tests { |
| 321 |
char *testname; |
| 322 |
char *driver_name; |
| 323 |
unsigned int keysize; |
| 324 |
unsigned int assoclen; |
| 325 |
unsigned int ccm; |
| 326 |
}; |
| 327 |
|
| 328 |
static const struct cp_aead_tests testcases[] = { |
| 329 |
|
| 330 |
{ "AES(G) GCM(G) 128", "gcm(aes-generic)", 16, 16, 0 }, |
| 331 |
{ "AES(G) GCM(G) 192", "gcm(aes-generic)", 24, 16, 0 }, |
| 332 |
{ "AES(G) GCM(G) 256", "gcm(aes-generic)", 32, 16, 0 }, |
| 333 |
{ "AES(AESNI) GCM(ASM-RFC) 128", "rfc4106-gcm-aesni", 20, 16, 0 }, |
| 334 |
{ "AES(AESNI) GCM(ASM-RFC) 192", "rfc4106-gcm-aesni", 28, 16, 0 }, |
| 335 |
{ "AES(AESNI) GCM(ASM-RFC) 256", "rfc4106-gcm-aesni", 36, 16, 0 }, |
| 336 |
{ "AES(AESNI) GCM(G) 128", "gcm(aes-aesni)", 16, 16, 0 }, |
| 337 |
{ "AES(AESNI) GCM(G) 192", "gcm(aes-aesni)", 24, 16, 0 }, |
| 338 |
{ "AES(AESNI) GCM(G) 256", "gcm(aes-aesni)", 32, 16, 0 }, |
| 339 |
{ "AES(G) CCM(G) 128", "ccm(aes-generic)", 16, 16, 1 }, |
| 340 |
{ "AES(G) CCM(G) 192", "ccm(aes-generic)", 24, 16, 1 }, |
| 341 |
{ "AES(G) CCM(G) 256", "ccm(aes-generic)", 32, 16, 1 }, |
| 342 |
{ "AES(AESNI) CCM(G) 128", "ccm(aes-aesni)", 16, 16, 1 }, |
| 343 |
{ "AES(AESNI) CCM(G) 192", "ccm(aes-aesni)", 24, 16, 1 }, |
| 344 |
{ "AES(AESNI) CCM(G) 256", "ccm(aes-aesni)", 32, 16, 1 }, |
| 345 |
{ "SM4(G) GCM(G)", "gcm(sm4-generic)", 16, 16, 0 }, |
| 346 |
{ "SM4(AVX) GCM(G)", "gcm(sm4-aesni-avx)", 16, 16, 0 }, |
| 347 |
{ "SM4(AVX2) GCM(G)", "gcm(sm4-aesni-avx2)", 16, 16, 0 }, |
| 348 |
{ "SM4(G) CCM(G)", "ccm(sm4-generic)", 16, 16, 0 }, |
| 349 |
{ "SM4(AVX) CCM(G)", "ccm(sm4-aesni-avx)", 16, 16, 0 }, |
| 350 |
{ "SM4(AVX2) CCM(G)", "ccm(sm4-aesni-avx2)", 16, 16, 0 }, |
| 351 |
|
| 352 |
{ "AES(CAAM) GCM(CAAM) 128", "gcm-aes-caam", 16, 16, 0 }, |
| 353 |
{ "AES(CAAM) GCM(CAAM) 192", "gcm-aes-caam", 24, 16, 0 }, |
| 354 |
{ "AES(CAAM) GCM(CAAM) 256", "gcm-aes-caam", 32, 16, 0 }, |
| 355 |
|
| 356 |
{ "AES(STM32) GCM(STM32) 128", "stm32-gcm-aes", 16, 16, 0 }, |
| 357 |
{ "AES(STM32) GCM(STM32) 192", "stm32-gcm-aes", 24, 16, 0 }, |
| 358 |
{ "AES(STM32) GCM(STM32) 256", "stm32-gcm-aes", 32, 16, 0 }, |
| 359 |
|
| 360 |
{ "AES(STM32) CCM(STM32) 128", "stm32-ccm-aes", 16, 16, 1 }, |
| 361 |
{ "AES(STM32) CCM(STM32) 192", "stm32-ccm-aes", 24, 16, 1 }, |
| 362 |
{ "AES(STM32) CCM(STM32) 256", "stm32-ccm-aes", 32, 16, 1 }, |
| 363 |
|
| 364 |
{ "AES(ARM64 CE) GCM(ARM64 CE) 128", "gcm-aes-ce", 16, 16, 0 }, |
| 365 |
{ "AES(ARM64 CE) GCM(ARM64 CE) 192", "gcm-aes-ce", 24, 16, 0 }, |
| 366 |
{ "AES(ARM64 CE) GCM(ARM64 CE) 256", "gcm-aes-ce", 32, 16, 0 }, |
| 367 |
|
| 368 |
{ "AES(ARM64 CE) CCM(ARM64 CE) 128", "ccm-aes-ce", 16, 16, 1 }, |
| 369 |
{ "AES(ARM64 CE) CCM(ARM64 CE) 192", "ccm-aes-ce", 24, 16, 1 }, |
| 370 |
{ "AES(ARM64 CE) CCM(ARM64 CE) 256", "ccm-aes-ce", 32, 16, 1 }, |
| 371 |
}; |
| 372 |
|
| 373 |
static struct cp_test cp_aead_testdef[(2 * (ARRAY_SIZE(testcases)))]; |
| 374 |
|
| 375 |
void cp_aead_register(struct cp_test **aead_test, size_t *entries) |
| 376 |
{ |
| 377 |
size_t i = 0; |
| 378 |
size_t j = 0; |
| 379 |
|
| 380 |
for (i = 0, j = 0; |
| 381 |
i < (ARRAY_SIZE(testcases)) && j < (2 * ARRAY_SIZE(testcases)); |
| 382 |
i++, j++) { |
| 383 |
int enc = 0; |
| 384 |
for (enc = 0; enc < 2; enc++) { |
| 385 |
j += (size_t)enc; |
| 386 |
cp_aead_testdef[j].enc = enc; |
| 387 |
cp_aead_testdef[j].testname = testcases[i].testname; |
| 388 |
cp_aead_testdef[j].driver_name = testcases[i].driver_name; |
| 389 |
cp_aead_testdef[j].type = "aead"; |
| 390 |
cp_aead_testdef[j].exectime = DFLT_EXECTIME; |
| 391 |
cp_aead_testdef[j].u.aead.keysize = testcases[i].keysize; |
| 392 |
cp_aead_testdef[j].u.aead.assoclen = testcases[i].assoclen; |
| 393 |
if (enc) { |
| 394 |
if (testcases[i].ccm) |
| 395 |
cp_aead_testdef[j].init_test = |
| 396 |
cp_aead_init_enc_ccm; |
| 397 |
else |
| 398 |
cp_aead_testdef[j].init_test = |
| 399 |
cp_aead_init_enc_dflt; |
| 400 |
} else { |
| 401 |
if (testcases[i].ccm) |
| 402 |
cp_aead_testdef[j].init_test = |
| 403 |
cp_aead_init_dec_ccm; |
| 404 |
else |
| 405 |
cp_aead_testdef[j].init_test = |
| 406 |
cp_aead_init_dec_dflt; |
| 407 |
} |
| 408 |
cp_aead_testdef[j].fini_test = cp_aead_fini_test; |
| 409 |
if (enc) |
| 410 |
cp_aead_testdef[j].exec_test = cp_ablkcipher_enc_test; |
| 411 |
else |
| 412 |
cp_aead_testdef[j].exec_test = cp_ablkcipher_dec_test; |
| 413 |
} |
| 414 |
} |
| 415 |
*aead_test = &cp_aead_testdef[0]; |
| 416 |
*entries = j; |
| 417 |
} |