Skip to content

Instantly share code, notes, and snippets.

@itzmeanjan
Created July 6, 2023 15:32
Show Gist options
  • Save itzmeanjan/448f97f9c49d781a5eb3ddd6ea6e7364 to your computer and use it in GitHub Desktop.
Save itzmeanjan/448f97f9c49d781a5eb3ddd6ea6e7364 to your computer and use it in GitHub Desktop.
Script for generating KATs for SHA3 Hash functions and Extendable output functions, defined in FIPS 202 Standard
#!/usr/bin/python
import random
import hashlib
MIN_MSG_LEN = 0 # minimum input message byte length
MAX_MSG_LEN = 1024 # maximum input message byte length
SQUEEZED_LEN = 256 # number of bytes squeezed from Xof
def gen_rand_bytes(n: int) -> bytes:
"""
Generate (not cryptographically) random n -bytes message.
"""
return bytes([random.randint(0x00, 0xFF) for _ in range(n)])
def gen_sha3_224_kat(file: str):
"""
Given path to KAT file, it generates SHA3-224 known answer tests.
"""
with open(file, mode="w") as fd:
for mlen in range(MIN_MSG_LEN, MAX_MSG_LEN + 1):
msg = gen_rand_bytes(mlen)
md = hashlib.sha3_224(msg).digest()
fd.write(f"MSG_BYTE_LEN = {mlen}\n")
fd.write(f"MSG = {msg.hex()}\n")
fd.write(f"MD = {md.hex()}\n\n")
def gen_sha3_256_kat(file: str):
"""
Given path to KAT file, it generates SHA3-256 known answer tests.
"""
with open(file, mode="w") as fd:
for mlen in range(MIN_MSG_LEN, MAX_MSG_LEN + 1):
msg = gen_rand_bytes(mlen)
md = hashlib.sha3_256(msg).digest()
fd.write(f"MSG_BYTE_LEN = {mlen}\n")
fd.write(f"MSG = {msg.hex()}\n")
fd.write(f"MD = {md.hex()}\n\n")
def gen_sha3_384_kat(file: str):
"""
Given path to KAT file, it generates SHA3-384 known answer tests.
"""
with open(file, mode="w") as fd:
for mlen in range(MIN_MSG_LEN, MAX_MSG_LEN + 1):
msg = gen_rand_bytes(mlen)
md = hashlib.sha3_384(msg).digest()
fd.write(f"MSG_BYTE_LEN = {mlen}\n")
fd.write(f"MSG = {msg.hex()}\n")
fd.write(f"MD = {md.hex()}\n\n")
def gen_sha3_512_kat(file: str):
"""
Given path to KAT file, it generates SHA3-512 known answer tests.
"""
with open(file, mode="w") as fd:
for mlen in range(MIN_MSG_LEN, MAX_MSG_LEN + 1):
msg = gen_rand_bytes(mlen)
md = hashlib.sha3_512(msg).digest()
fd.write(f"MSG_BYTE_LEN = {mlen}\n")
fd.write(f"MSG = {msg.hex()}\n")
fd.write(f"MD = {md.hex()}\n\n")
def gen_shake128_kat(file: str):
"""
Given path to KAT file, it generates Shake128 Xof's known answer tests.
"""
with open(file, mode="w") as fd:
for mlen in range(MIN_MSG_LEN, MAX_MSG_LEN + 1):
msg = gen_rand_bytes(mlen)
out = hashlib.shake_128(msg).digest(SQUEEZED_LEN)
fd.write(f"MSG_BYTE_LEN = {mlen}\n")
fd.write(f"MSG = {msg.hex()}\n")
fd.write(f"SQUEEZED = {out.hex()}\n\n")
def gen_shake256_kat(file: str):
"""
Given path to KAT file, it generates Shake256 Xof's known answer tests.
"""
with open(file, mode="w") as fd:
for mlen in range(MIN_MSG_LEN, MAX_MSG_LEN + 1):
msg = gen_rand_bytes(mlen)
out = hashlib.shake_256(msg).digest(SQUEEZED_LEN)
fd.write(f"MSG_BYTE_LEN = {mlen}\n")
fd.write(f"MSG = {msg.hex()}\n")
fd.write(f"SQUEEZED = {out.hex()}\n\n")
def main():
gen_sha3_224_kat("sha3_224.kat")
print("Generated SHA3-224 KAT file.")
gen_sha3_256_kat("sha3_256.kat")
print("Generated SHA3-256 KAT file.")
gen_sha3_384_kat("sha3_384.kat")
print("Generated SHA3-384 KAT file.")
gen_sha3_512_kat("sha3_512.kat")
print("Generated SHA3-512 KAT file.")
gen_shake128_kat("shake128.kat")
print("Generated Shake128 KAT file.")
gen_shake256_kat("shake256.kat")
print("Generated Shake256 KAT file.")
if __name__ == "__main__":
random.seed(0)
main()
@itzmeanjan
Copy link
Author

Issue following command for generating KAT files

$ python gen_fips202_kat.py
Generated SHA3-224 KAT file.
Generated SHA3-256 KAT file.
Generated SHA3-384 KAT file.
Generated SHA3-512 KAT file.
Generated Shake128 KAT file.
Generated Shake256 KAT file.

Compare SHA256 checksums of KAT files.

$ sha256sum *.kat
602bc661fc447701644922577356a9e8e6e03d660243140d28910f93f805143b  sha3_224.kat
8a6feb162bf3391fe571f8f665f77fe76b56c5d98edd4f2c85df13591f44b350  sha3_256.kat
2eebb939d7f2e1baba97386acf39901a8405ce6ea018d800d2440296921f3b4b  sha3_384.kat
d8e06ebb69f2f5af2789b6914226818e448f2c4258bd503373712c22b4970a02  sha3_512.kat
ad29ac31af2267ada6c1484db102ea9ef0f5a5f98656ea61f67b1c753151e9dc  shake128.kat
5ea713f3ac0cff03ef32c86fa542473c78d57024d7ca040a396906f96f38cbb3  shake256.kat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment