History

lib/crypto: tests: Add KUnit test suite for AES-GCM

Add a KUnit test suite for the AES-GCM library API. It consists of: - All the shared test cases from aead-test-template.h. These include extensive consistency tests, a "Monte-Carlo test", and a benchmark. - Tests against hardcoded AES-GCM test vectors from external sources.

Eric Biggers committed 3 months ago

Commit b09bd2d1 parent 2aeef505 files · +494 −2Browse files

Showing the changes to scripts (1 of 5 files). Show all

Unified · Split

scripts/crypto/gen-aead-testvecs.py+11−2
@@ -41,6 +41,15 @@ def gen_monte_carlo_checksum(alg): · 40 unmodified lines
4141 key, tag_length=tag_len
4242 )
4343 ct_and_tag = ccm.encrypt(nonce, pt, ad)
44+ elif alg == "aes-gcm":
45+ key_len = [16, 24, 32][data_len % 3]
46+ key = rand_bytes(key_len)
47+ nonce = rand_bytes(12)
48+ tag_len = [4, 8, 12, 13, 14, 15, 16][data_len % 7]
49+ gcm = cryptography.hazmat.primitives.ciphers.aead.AESGCM(key)
50+ # python-cryptography supports only 16-byte GCM tags. However, in
51+ # GCM, shorter tags are simply truncated. Do that below.
52+ ct_and_tag = gcm.encrypt(nonce, pt, ad)[: data_len + tag_len]
4453
4554 blake2s.update(ct_and_tag)
4655
@@ -53,8 +62,8 @@ def gen_monte_carlo_checksum(alg): · 6 unmodified lines
5362 print("};")
5463
5564
56−if len(sys.argv) != 2 or sys.argv[1] not in ("aes-ccm"):
57− sys.stderr.write("Usage: gen-aead-testvecs.py [aes-ccm]\n")
65+if len(sys.argv) != 2 or sys.argv[1] not in ("aes-ccm", "aes-gcm"):
66+ sys.stderr.write("Usage: gen-aead-testvecs.py [aes-ccm|aes-gcm]\n")
5867 sys.exit(1)
5968
6069 gen_monte_carlo_checksum(sys.argv[1])