stream.c (9369B)
1 #include <stdint.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "keccak-fast-internal.h" 6 #include "keccak-fast.h" 7 8 void kf_scalar_perm(uint64_t st[25]); 9 void kf_scalar_perm12(uint64_t st[25]); 10 11 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 12 !defined(KECCAK_FAST_NO_BMI) 13 void kf_bmi_perm(uint64_t st[25]); 14 void kf_bmi_perm12(uint64_t st[25]); 15 #endif 16 17 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) 18 int kf_armsha3_available(void); 19 void kf_armsha3_perm(uint64_t st[25]); 20 void kf_armsha3_perm12(uint64_t st[25]); 21 #endif 22 23 typedef int (*init_fn)(kf_ctx *); 24 typedef int (*absorb_fn)(kf_ctx *, const uint8_t *, size_t); 25 typedef int (*squeeze_fn)(kf_ctx *, uint8_t *, size_t); 26 27 typedef struct { 28 const char *name; 29 kf_hash_fn one; 30 init_fn init; 31 absorb_fn absorb; 32 squeeze_fn squeeze; 33 size_t rate; 34 size_t cap; 35 } algo; 36 37 typedef struct { 38 const char *label; 39 kf_perm_fn perm; 40 kf_perm_fn perm12; 41 } kernel; 42 43 #define INLEN_MAX 40000 44 #define OUT_MAX 512 45 46 static uint8_t in[INLEN_MAX + 8]; 47 static uint8_t want[OUT_MAX]; 48 static uint8_t got[OUT_MAX]; 49 static uint8_t ref[OUT_MAX]; 50 51 static unsigned test_no; 52 static unsigned fails; 53 54 static void check(const char *what, int ok) { 55 test_no++; 56 printf("%s %u - %s\n", ok ? "ok" : "not ok", test_no, what); 57 if (!ok) fails++; 58 } 59 60 static void fill(uint8_t *p, size_t n, unsigned seed) { 61 for (size_t i = 0; i < n; i++) p[i] = (uint8_t)(i * 31 + seed * 7 + 1); 62 } 63 64 /* absorb msg in chunks of `chunk`, then squeeze outlen in one call */ 65 static int feed(const algo *a, const uint8_t *msg, size_t inlen, size_t chunk, 66 uint8_t *out, size_t outlen) { 67 kf_ctx ctx; 68 if (a->init(&ctx) != 0) return -1; 69 for (size_t off = 0; off < inlen; off += chunk) { 70 size_t k = inlen - off < chunk ? inlen - off : chunk; 71 if (a->absorb(&ctx, msg + off, k) != 0) return -1; 72 } 73 if (a->absorb(&ctx, msg, 0) != 0) return -1; 74 return a->squeeze(&ctx, out, outlen); 75 } 76 77 /* absorb inlen at once, then squeeze outlen in chunks of `chunk` */ 78 static int feed_sq(const algo *a, uint8_t *out, size_t inlen, size_t chunk, 79 size_t outlen) { 80 kf_ctx ctx; 81 if (a->init(&ctx) != 0) return -1; 82 if (a->absorb(&ctx, in, inlen) != 0) return -1; 83 for (size_t off = 0; off < outlen; off += chunk) { 84 size_t k = outlen - off < chunk ? outlen - off : chunk; 85 if (a->squeeze(&ctx, out + off, k) != 0) return -1; 86 } 87 return 0; 88 } 89 90 int main(void) { 91 const algo algos[] = { 92 {"shake128", kf_shake128, kf_shake128_init, kf_shake128_absorb, 93 kf_shake128_squeeze, 168, 0}, 94 {"shake256", kf_shake256, kf_shake256_init, kf_shake256_absorb, 95 kf_shake256_squeeze, 136, 0}, 96 {"sha3_224", kf_sha3_224, kf_sha3_224_init, kf_sha3_224_absorb, 97 kf_sha3_224_squeeze, 144, 28}, 98 {"sha3_256", kf_sha3_256, kf_sha3_256_init, kf_sha3_256_absorb, 99 kf_sha3_256_squeeze, 136, 32}, 100 {"sha3_384", kf_sha3_384, kf_sha3_384_init, kf_sha3_384_absorb, 101 kf_sha3_384_squeeze, 104, 48}, 102 {"sha3_512", kf_sha3_512, kf_sha3_512_init, kf_sha3_512_absorb, 103 kf_sha3_512_squeeze, 72, 64}, 104 {"turboshake128", kf_turboshake128, kf_turboshake128_init, 105 kf_turboshake128_absorb, kf_turboshake128_squeeze, 168, 0}, 106 {"turboshake256", kf_turboshake256, kf_turboshake256_init, 107 kf_turboshake256_absorb, kf_turboshake256_squeeze, 136, 0}, 108 }; 109 const size_t nalgo = sizeof algos / sizeof algos[0]; 110 111 kernel kernels[3]; 112 int nk = 0; 113 114 kernels[nk++] = (kernel){"scalar", kf_scalar_perm, kf_scalar_perm12}; 115 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 116 !defined(KECCAK_FAST_NO_BMI) 117 #if defined(__clang__) 118 if (__builtin_cpu_supports("bmi")) 119 #else 120 if (__builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2")) 121 #endif 122 kernels[nk++] = (kernel){"scalar+bmi", kf_bmi_perm, kf_bmi_perm12}; 123 #endif 124 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) 125 if (kf_armsha3_available()) 126 kernels[nk++] = (kernel){"arm-sha3", kf_armsha3_perm, kf_armsha3_perm12}; 127 #endif 128 129 fill(in, sizeof in, 5); 130 131 printf("TAP version 13\n"); 132 printf("# streaming sponge\n"); 133 134 for (int k = 0; k < nk; k++) { 135 kf_stream_perm = kernels[k].perm; 136 kf_stream_perm12 = kernels[k].perm12; 137 138 for (size_t a = 0; a < nalgo; a++) { 139 const algo *al = &algos[a]; 140 size_t rate = al->rate; 141 size_t olen = al->cap ? al->cap : 100; 142 size_t ils[8]; 143 size_t chs[7]; 144 char nm[160]; 145 146 ils[0] = 0; 147 ils[1] = 1; 148 ils[2] = rate - 1; 149 ils[3] = rate; 150 ils[4] = rate + 1; 151 ils[5] = 2 * rate + 3; 152 ils[6] = 4096; 153 ils[7] = INLEN_MAX; 154 chs[0] = 1; 155 chs[1] = 3; 156 chs[2] = rate - 1; 157 chs[3] = rate; 158 chs[4] = rate + 1; 159 chs[5] = 4096; 160 chs[6] = 0; /* whole message in one absorb */ 161 162 for (int i = 0; i < 8; i++) { 163 size_t inlen = ils[i]; 164 if (al->one(want, olen, in, inlen) != 0) { 165 snprintf(nm, sizeof nm, "%s %s one-shot %zu", kernels[k].label, 166 al->name, inlen); 167 check(nm, 0); 168 continue; 169 } 170 for (int c = 0; c < 7; c++) { 171 size_t chunk = chs[c] ? chs[c] : (inlen ? inlen : 1); 172 int rc = feed(al, in, inlen, chunk, got, olen); 173 snprintf(nm, sizeof nm, "%s %s in=%zu chunk=%zu", kernels[k].label, 174 al->name, inlen, chunk); 175 check(nm, rc == 0 && memcmp(want, got, olen) == 0); 176 } 177 } 178 179 /* squeeze splits, including past the first output block */ 180 size_t sqs[4] = {olen, 1, 7, rate + 5}; 181 for (int s = 0; s < 4; s++) { 182 int rc = al->one(want, olen, in, 100); 183 rc |= feed_sq(al, got, 100, sqs[s], olen); 184 snprintf(nm, sizeof nm, "%s %s squeeze=%zu split=%zu", 185 kernels[k].label, al->name, olen, sqs[s]); 186 check(nm, rc == 0 && memcmp(want, got, olen) == 0); 187 } 188 189 if (al->cap == 0) { 190 int rc1; 191 int rc2 = al->one(want, 400, in, 1000); 192 kf_ctx ctx; 193 194 rc1 = feed(al, in, 1000, rate - 1, got, 400); 195 snprintf(nm, sizeof nm, "%s %s xof 400 of one block", 196 kernels[k].label, al->name); 197 check(nm, rc1 == 0 && rc2 == 0 && memcmp(want, got, 400) == 0); 198 199 rc1 = al->init(&ctx); 200 rc1 |= al->absorb(&ctx, in, 1000); 201 for (size_t i = 0; i < 400; i++) { 202 if (al->squeeze(&ctx, got + i, 1) != 0) { 203 rc1 = -1; 204 break; 205 } 206 } 207 snprintf(nm, sizeof nm, "%s %s xof byte at a time", kernels[k].label, 208 al->name); 209 check(nm, rc1 == 0 && rc2 == 0 && memcmp(want, got, 400) == 0); 210 } 211 212 /* a context copied mid-stream keeps going */ 213 { 214 kf_ctx c1, c2; 215 int rc = al->init(&c1); 216 rc |= al->absorb(&c1, in, 1234); 217 c2 = c1; 218 rc |= al->absorb(&c1, in + 1234, 2222); 219 rc |= al->squeeze(&c1, got, olen); 220 rc |= al->absorb(&c2, in + 1234, 2222); 221 rc |= al->squeeze(&c2, want, olen); 222 snprintf(nm, sizeof nm, "%s %s ctx copy mid-stream", 223 kernels[k].label, al->name); 224 check(nm, rc == 0 && memcmp(want, got, olen) == 0 && 225 al->one(ref, olen, in, 3456) == 0 && 226 memcmp(ref, got, olen) == 0); 227 } 228 } 229 } 230 231 kf_stream_perm = kf_scalar_perm; 232 kf_stream_perm12 = kf_scalar_perm12; 233 234 { 235 kf_ctx ctx; 236 uint8_t fresh[64]; 237 int rc; 238 239 check("rejects null ctx on init", kf_sha3_256_init(NULL) == -1); 240 check("init binds a permutation", kf_shake256_init(&ctx) == 0); 241 242 rc = kf_shake256_init(&ctx); 243 rc |= kf_shake256_absorb(&ctx, NULL, 4); 244 check("rejects null input with a length", rc == -1); 245 246 rc = kf_shake256_init(&ctx); 247 rc |= kf_shake256_absorb(&ctx, in, 64); 248 rc |= kf_shake256_squeeze(&ctx, NULL, 32); 249 check("rejects null output", rc == -1); 250 251 rc = kf_shake256_init(&ctx); 252 rc |= kf_shake256_absorb(&ctx, in, 64); 253 rc |= kf_shake256_squeeze(&ctx, got, 32); 254 rc |= kf_shake256_absorb(&ctx, in, 8); 255 check("rejects absorb after squeeze", rc == -1); 256 257 check("sha3 rejects outlen past the cap", 258 kf_sha3_224_init(&ctx) == 0 && kf_sha3_224_absorb(&ctx, in, 9) == 0 && 259 kf_sha3_224_squeeze(&ctx, got, 29) == -1); 260 261 rc = kf_sha3_224_init(&ctx); 262 rc |= kf_sha3_224_absorb(&ctx, in, 9); 263 rc |= kf_sha3_224_squeeze(&ctx, got, 27); 264 rc |= kf_sha3_224_squeeze(&ctx, got + 27, 1); 265 check("sha3 accepts an exact split cap", rc == 0); 266 267 check("sha3 rejects one byte past a reached cap", 268 kf_sha3_224_squeeze(&ctx, got, 1) == -1); 269 270 rc = kf_shake256_init(&ctx); 271 rc |= kf_shake256_absorb(&ctx, in, 40); 272 rc |= kf_shake256_squeeze(&ctx, got, 32); 273 kf_shake256_init(&ctx); 274 kf_shake256_absorb(&ctx, in, 40); 275 kf_shake256_squeeze(&ctx, fresh, 32); 276 check("re-init resets a used ctx", 277 rc == 0 && memcmp(got, fresh, 32) == 0 && 278 kf_shake256(ref, 32, in, 40) == 0 && 279 memcmp(ref, fresh, 32) == 0); 280 281 check("dispatched track has a name", kf_stream_name()[0] != '\0'); 282 } 283 284 printf("1..%u\n", test_no); 285 if (fails) printf("# %u of %u assertions failed\n", fails, test_no); 286 printf("# stream backend: %s\n", kf_stream_name()); 287 return fails != 0; 288 }