Update GHASH

This commit is contained in:
Zhi Guan
2026-06-14 16:24:30 +08:00
parent 25ce3134f8
commit 1d8cf6117c
5 changed files with 69 additions and 13 deletions

View File

@@ -108,6 +108,7 @@ int test_ghash(void)
format_print(stderr, 0, 2, "C = %s\n", ghash_tests[i].C);
format_bytes(stderr, 0, 2, "GHASH(H,A,C) = ", out, 16);
format_print(stderr, 0, 2, " = %s\n\n", ghash_tests[i].T);
return -1;
}
}
@@ -115,6 +116,43 @@ int test_ghash(void)
return 1;
}
int test_ghash_update(void)
{
uint8_t H[16];
uint8_t A[32];
uint8_t C[80];
uint8_t C_alias[80];
uint8_t T[16];
uint8_t out[16];
size_t Hlen, Alen, Clen, Tlen;
GHASH_CTX ghash_ctx;
hex_to_bytes(ghash_tests[3].H, strlen(ghash_tests[3].H), H, &Hlen);
hex_to_bytes(ghash_tests[3].A, strlen(ghash_tests[3].A), A, &Alen);
hex_to_bytes(ghash_tests[3].C, strlen(ghash_tests[3].C), C, &Clen);
hex_to_bytes(ghash_tests[3].T, strlen(ghash_tests[3].T), T, &Tlen);
ghash_init(&ghash_ctx, H, A, Alen);
ghash_update(&ghash_ctx, C, 7);
ghash_update(&ghash_ctx, NULL, 0);
ghash_update(&ghash_ctx, C + 7, Clen - 7);
ghash_finish(&ghash_ctx, out);
if (memcmp(out, T, Tlen) != 0) {
error_print();
return -1;
}
memcpy(C_alias, C, Clen);
ghash(H, A, Alen, C_alias, Clen, C_alias);
if (memcmp(C_alias, T, Tlen) != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int speed_ghash(void)
{
GHASH_CTX ghash_ctx;
@@ -148,6 +186,7 @@ static int speed_ghash(void)
int main(int argc, char **argv)
{
if (test_ghash() != 1) goto err;
if (test_ghash_update() != 1) goto err;
#if ENABLE_TEST_SPEED
speed_ghash();