batch.c (2690B)
1 #include <stdint.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "keccak-fast.h" 6 7 typedef int (*kf_fn)(uint8_t *, size_t, const uint8_t *, size_t); 8 typedef int (*kf_batch_fn)(size_t, const uint8_t *, size_t, uint8_t *, size_t); 9 10 static int fails; 11 static int testn; 12 13 static void check(const char *name, int ok) { 14 testn++; 15 if (ok) { 16 printf("ok %d - %s\n", testn, name); 17 } else { 18 fails++; 19 printf("not ok %d - %s\n", testn, name); 20 } 21 } 22 23 struct algo { 24 const char *name; 25 kf_fn one; 26 kf_batch_fn batch; 27 size_t outlen; 28 }; 29 30 int main(void) { 31 const struct algo algos[] = { 32 {"shake128", kf_shake128, kf_shake128_batch, 32}, 33 {"shake256", kf_shake256, kf_shake256_batch, 64}, 34 {"sha3_224", kf_sha3_224, kf_sha3_224_batch, 28}, 35 {"sha3_256", kf_sha3_256, kf_sha3_256_batch, 32}, 36 {"sha3_384", kf_sha3_384, kf_sha3_384_batch, 48}, 37 {"sha3_512", kf_sha3_512, kf_sha3_512_batch, 64}, 38 {"turboshake128", kf_turboshake128, kf_turboshake128_batch, 32}, 39 {"turboshake256", kf_turboshake256, kf_turboshake256_batch, 64}, 40 }; 41 static const size_t counts[] = {1, 3, 4, 5, 8, 17}; 42 static const size_t inlens[] = {32, 200}; 43 static uint8_t in[17 * 200]; 44 static uint8_t want[17 * 64]; 45 static uint8_t got[17 * 64]; 46 47 printf("1..%zu\n", (sizeof algos / sizeof algos[0]) * 48 (sizeof counts / sizeof counts[0]) * 49 (sizeof inlens / sizeof inlens[0])); 50 51 for (size_t a = 0; a < sizeof algos / sizeof algos[0]; a++) { 52 for (size_t ci = 0; ci < sizeof counts / sizeof counts[0]; ci++) { 53 size_t count = counts[ci]; 54 for (size_t li = 0; li < sizeof inlens / sizeof inlens[0]; li++) { 55 size_t inlen = inlens[li]; 56 size_t outlen = algos[a].outlen; 57 char name[64]; 58 59 for (size_t m = 0; m < count; m++) { 60 for (size_t j = 0; j < inlen; j++) { 61 in[m * inlen + j] = (uint8_t)(j * 31 + m * 7 + 1); 62 } 63 } 64 for (size_t m = 0; m < count; m++) { 65 int rc = algos[a].one(want + m * outlen, outlen, in + m * inlen, 66 inlen); 67 if (rc != 0) { 68 printf("# %s one-shot returned %d\n", algos[a].name, rc); 69 } 70 } 71 72 memset(got, 0, count * outlen); 73 int rc = algos[a].batch(count, in, inlen, got, outlen); 74 snprintf(name, sizeof name, "%s count=%zu inlen=%zu", algos[a].name, 75 count, inlen); 76 check(name, rc == 0 && memcmp(want, got, count * outlen) == 0); 77 } 78 } 79 } 80 81 printf("# batch backend: %s\n", kf_batch_name()); 82 83 if (fails != 0) printf("# %d failed\n", fails); 84 return fails != 0; 85 }