| @@ -247,72 +247,149 @@ static void test_shake256_nist(struct kunit *test) · 246 unmodified lines |
| 247 | "SHAKE256 gives wrong output for NIST.1600"); | 247 | "SHAKE256 gives wrong output for NIST.1600"); |
| 248 | } | 248 | } |
| 249 | | 249 | |
| 250 | /* | 250 | static void shake(int alg, const u8 *in, size_t in_len, u8 *out, size_t out_len) |
| 251 | * Output tiling test of SHAKE256; equal output tiles barring the last. A | | |
| 252 | * series of squeezings of the same context should, if laid end-to-end, match a | | |
| 253 | * single squeezing of the combined size. | | |
| 254 | */ | | |
| 255 | static void test_shake256_tiling(struct kunit *test) | | |
| 256 | { | 251 | { |
| 257 | struct shake_ctx ctx; | 252 | if (alg == 0) |
| 258 | u8 out[8 + SHA3_512_DIGEST_SIZE + 8]; | 253 | shake128(in, in_len, out, out_len); |
| | 254 | else |
| | 255 | shake256(in, in_len, out, out_len); |
| | 256 | } |
| 259 | | 257 | |
| 260 | for (int tile_size = 1; tile_size < SHAKE256_DEFAULT_SIZE; tile_size++) { | 258 | static void shake_init(struct shake_ctx *ctx, int alg) |
| 261 | int left = SHAKE256_DEFAULT_SIZE; | 259 | { |
| 262 | u8 *p = out + 8; | 260 | if (alg == 0) |
| | 261 | shake128_init(ctx); |
| | 262 | else |
| | 263 | shake256_init(ctx); |
| | 264 | } |
| 263 | | 265 | |
| 264 | memset(out, 0, sizeof(out)); | 266 | /* |
| 265 | shake256_init(&ctx); | 267 | * Test each of SHAKE128 and SHAKE256 with all input lengths 0 through 4096, for |
| 266 | shake_update(&ctx, test_sha3_sample, | 268 | * both input and output. The input and output lengths cycle through the values |
| 267 | sizeof(test_sha3_sample) - 1); | 269 | * together, so we do 4096 tests total. To verify all the SHAKE outputs, |
| 268 | while (left > 0) { | 270 | * compute and verify the SHA3-256 digest of all of them concatenated together. |
| 269 | int part = umin(tile_size, left); | 271 | */ |
| | 272 | static void test_shake_all_lens_up_to_4096(struct kunit *test) |
| | 273 | { |
| | 274 | struct sha3_ctx main_ctx; |
| | 275 | const size_t max_len = 4096; |
| | 276 | u8 *const in = test_buf; |
| | 277 | u8 *const out = &test_buf[TEST_BUF_LEN - max_len]; |
| | 278 | u8 main_hash[SHA3_256_DIGEST_SIZE]; |
| | 279 | |
| | 280 | KUNIT_ASSERT_LE(test, 2 * max_len, TEST_BUF_LEN); |
| | 281 | |
| | 282 | rand_bytes_seeded_from_len(in, max_len); |
| | 283 | for (int alg = 0; alg < 2; alg++) { |
| | 284 | sha3_256_init(&main_ctx); |
| | 285 | for (size_t in_len = 0; in_len <= max_len; in_len++) { |
| | 286 | size_t out_len = (in_len * 293) % (max_len + 1); |
| | 287 | |
| | 288 | shake(alg, in, in_len, out, out_len); |
| | 289 | sha3_update(&main_ctx, out, out_len); |
| | 290 | } |
| | 291 | sha3_final(&main_ctx, main_hash); |
| | 292 | if (alg == 0) |
| | 293 | KUNIT_ASSERT_MEMEQ_MSG(test, main_hash, |
| | 294 | shake128_testvec_consolidated, |
| | 295 | sizeof(main_hash), |
| | 296 | "shake128() gives wrong output"); |
| | 297 | else |
| | 298 | KUNIT_ASSERT_MEMEQ_MSG(test, main_hash, |
| | 299 | shake256_testvec_consolidated, |
| | 300 | sizeof(main_hash), |
| | 301 | "shake256() gives wrong output"); |
| | 302 | } |
| | 303 | } |
| 270 | | 304 | |
| 271 | shake_squeeze(&ctx, p, part); | 305 | /* |
| 272 | p += part; | 306 | * Test that a sequence of SHAKE squeezes gives the same output as a single |
| 273 | left -= part; | 307 | * squeeze of the same total length. |
| | 308 | */ |
| | 309 | static void test_shake_multiple_squeezes(struct kunit *test) |
| | 310 | { |
| | 311 | const size_t max_len = 512; |
| | 312 | u8 *ref_out; |
| | 313 | |
| | 314 | KUNIT_ASSERT_GE(test, TEST_BUF_LEN, 2 * max_len); |
| | 315 | |
| | 316 | ref_out = kunit_kzalloc(test, max_len, GFP_KERNEL); |
| | 317 | KUNIT_ASSERT_NOT_NULL(test, ref_out); |
| | 318 | |
| | 319 | for (int i = 0; i < 2000; i++) { |
| | 320 | const int alg = rand32() % 2; |
| | 321 | const size_t in_len = rand_length(max_len); |
| | 322 | const size_t out_len = rand_length(max_len); |
| | 323 | const size_t in_offs = rand_offset(max_len - in_len); |
| | 324 | const size_t out_offs = rand_offset(max_len - out_len); |
| | 325 | u8 *const in = &test_buf[in_offs]; |
| | 326 | u8 *const out = &test_buf[out_offs]; |
| | 327 | struct shake_ctx ctx; |
| | 328 | size_t remaining_len, j, num_parts; |
| | 329 | |
| | 330 | rand_bytes(in, in_len); |
| | 331 | rand_bytes(out, out_len); |
| | 332 | |
| | 333 | /* Compute the output using the one-shot function. */ |
| | 334 | shake(alg, in, in_len, ref_out, out_len); |
| | 335 | |
| | 336 | /* Compute the output using a random sequence of squeezes. */ |
| | 337 | shake_init(&ctx, alg); |
| | 338 | shake_update(&ctx, in, in_len); |
| | 339 | remaining_len = out_len; |
| | 340 | j = 0; |
| | 341 | num_parts = 0; |
| | 342 | while (rand_bool()) { |
| | 343 | size_t part_len = rand_length(remaining_len); |
| | 344 | |
| | 345 | shake_squeeze(&ctx, &out[j], part_len); |
| | 346 | num_parts++; |
| | 347 | j += part_len; |
| | 348 | remaining_len -= part_len; |
| | 349 | } |
| | 350 | if (remaining_len != 0 || rand_bool()) { |
| | 351 | shake_squeeze(&ctx, &out[j], remaining_len); |
| | 352 | num_parts++; |
| 274 | } | 353 | } |
| 275 | | 354 | |
| 276 | KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256, sizeof(test_shake256), | 355 | /* Verify that the outputs are the same. */ |
| 277 | "SHAKE tile %u gives wrong output", tile_size); | 356 | KUNIT_ASSERT_MEMEQ_MSG( |
| | 357 | test, out, ref_out, out_len, |
| | 358 | "Multi-squeeze test failed with in_len=%zu in_offs=%zu out_len=%zu out_offs=%zu num_parts=%zu alg=%d", |
| | 359 | in_len, in_offs, out_len, out_offs, num_parts, alg); |
| 278 | } | 360 | } |
| 279 | } | 361 | } |
| 280 | | 362 | |
| 281 | /* | 363 | /* |
| 282 | * Output tiling test of SHAKE256; output tiles getting gradually smaller and | 364 | * Test that SHAKE operations on buffers immediately followed by an unmapped |
| 283 | * then cycling round to medium sized ones. A series of squeezings of the same | 365 | * page work as expected. This catches out-of-bounds memory accesses even if |
| 284 | * context should, if laid end-to-end, match a single squeezing of the combined | 366 | * they occur in assembly code. |
| 285 | * size. | | |
| 286 | */ | 367 | */ |
| 287 | static void test_shake256_tiling2(struct kunit *test) | 368 | static void test_shake_with_guarded_bufs(struct kunit *test) |
| 288 | { | 369 | { |
| 289 | struct shake_ctx ctx; | 370 | const size_t max_len = 512; |
| 290 | u8 out[8 + SHA3_512_DIGEST_SIZE + 8]; | 371 | u8 *reg_buf; |
| 291 | | 372 | |
| 292 | for (int first_tile_size = 3; | 373 | KUNIT_ASSERT_GE(test, TEST_BUF_LEN, max_len); |
| 293 | first_tile_size < SHAKE256_DEFAULT_SIZE; | | |
| 294 | first_tile_size++) { | | |
| 295 | int tile_size = first_tile_size; | | |
| 296 | int left = SHAKE256_DEFAULT_SIZE; | | |
| 297 | u8 *p = out + 8; | | |
| 298 | | | |
| 299 | memset(out, 0, sizeof(out)); | | |
| 300 | shake256_init(&ctx); | | |
| 301 | shake_update(&ctx, test_sha3_sample, | | |
| 302 | sizeof(test_sha3_sample) - 1); | | |
| 303 | while (left > 0) { | | |
| 304 | int part = umin(tile_size, left); | | |
| 305 | | | |
| 306 | shake_squeeze(&ctx, p, part); | | |
| 307 | p += part; | | |
| 308 | left -= part; | | |
| 309 | tile_size--; | | |
| 310 | if (tile_size < 1) | | |
| 311 | tile_size = 5; | | |
| 312 | } | | |
| 313 | | 374 | |
| 314 | KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256, sizeof(test_shake256), | 375 | reg_buf = kunit_kzalloc(test, max_len, GFP_KERNEL); |
| 315 | "SHAKE tile %u gives wrong output", tile_size); | 376 | KUNIT_ASSERT_NOT_NULL(test, reg_buf); |
| | 377 | |
| | 378 | for (int alg = 0; alg < 2; alg++) { |
| | 379 | for (size_t len = 0; len <= max_len; len++) { |
| | 380 | u8 *guarded_buf = &test_buf[TEST_BUF_LEN - len]; |
| | 381 | |
| | 382 | rand_bytes(reg_buf, len); |
| | 383 | memcpy(guarded_buf, reg_buf, len); |
| | 384 | |
| | 385 | shake(alg, reg_buf, len, reg_buf, len); |
| | 386 | shake(alg, guarded_buf, len, guarded_buf, len); |
| | 387 | |
| | 388 | KUNIT_ASSERT_MEMEQ_MSG( |
| | 389 | test, reg_buf, guarded_buf, len, |
| | 390 | "Guard page test failed with len=%zu alg=%d", |
| | 391 | len, alg); |
| | 392 | } |
| 316 | } | 393 | } |
| 317 | } | 394 | } |
| 318 | | 395 | |
| @@ -326,8 +403,9 @@ static struct kunit_case sha3_test_cases[] = { · 7 unmodified lines |
| 326 | KUNIT_CASE(test_shake256_basic), | 403 | KUNIT_CASE(test_shake256_basic), |
| 327 | KUNIT_CASE(test_shake128_nist), | 404 | KUNIT_CASE(test_shake128_nist), |
| 328 | KUNIT_CASE(test_shake256_nist), | 405 | KUNIT_CASE(test_shake256_nist), |
| 329 | KUNIT_CASE(test_shake256_tiling), | 406 | KUNIT_CASE(test_shake_all_lens_up_to_4096), |
| 330 | KUNIT_CASE(test_shake256_tiling2), | 407 | KUNIT_CASE(test_shake_multiple_squeezes), |
| | 408 | KUNIT_CASE(test_shake_with_guarded_bufs), |
| 331 | KUNIT_CASE(benchmark_hash), | 409 | KUNIT_CASE(benchmark_hash), |
| 332 | {}, | 410 | {}, |
| 333 | }; | 411 | }; |