3.5 ALL NIST KATS
EXERCISE 3.5: ALL NIST KATS
Write a program that will read one of these NIST KAT “rsp” files, and parse out the encryption and decryption KATs. Test and validate your AES library on all vectors on a couple of ECB test files.
# ex3_5.py
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from colorama import Fore, Style
import sys
import os
def encrypt_using_aes_ecb(plaintext: bytes, key: bytes) -> bytes:
= Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
aesCipher = aesCipher.encryptor()
aesEncryptor = aesEncryptor.update(plaintext)
ciphertext return ciphertext
def decrypt_using_aes_ecb(ciphertext: bytes, key: bytes) -> bytes:
= Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
aesCipher = aesCipher.decryptor()
aesDecryptor = aesDecryptor.update(ciphertext)
plaintext return plaintext
class ASingleKAT:
def __init__(self, count: int, key: bytes, plaintext: bytes, ciphertext: bytes):
self.count = count
self.key = key
self.plaintext = plaintext
self.ciphertext = ciphertext
def testInEncryptMode(self):
= encrypt_using_aes_ecb(plaintext=self.plaintext, key=self.key)
result if result == self.ciphertext:
print(Fore.GREEN + f"Test {self.count} passed!")
print(Style.RESET_ALL, end='')
else:
print(Fore.RED + f"Test {self.count} Failed!")
print(Style.RESET_ALL, end='')
def testInDecryptMode(self):
= decrypt_using_aes_ecb(ciphertext=self.ciphertext, key=self.key)
result if result == self.plaintext:
print(Fore.GREEN + f"Test {self.count} passed!")
print(Style.RESET_ALL, end='')
else:
print(Fore.RED + f"Test {self.count} Failed!")
print(Style.RESET_ALL, end='')
@staticmethod
def correctType(kat: dict[str,str]):
'count'] = int(kat['count'])
kat['key'] = bytes.fromhex(kat['key'])
kat['plaintext'] = bytes.fromhex(kat['plaintext'])
kat['ciphertext'] = bytes.fromhex(kat['ciphertext'])
kat[
if __name__ == '__main__':
if len(sys.argv) != 2:
print(f"Usage: python3 {sys.argv[0]} <filename>.rsp")
-1)
exit(= sys.argv[1]
filename if not os.path.exists(filename):
print(f"File {filename} does not exist!")
-1)
exit(with open(filename, 'r') as f:
= True
encrypt_mode
while True:
= f.readline()
single_line if single_line == '':
# end of file is reached.
break
if single_line.startswith('#'):
# this line is a comment. So ignore it.
continue
= single_line.strip()
single_line if len(single_line) == 0:
# this line is an empty line.
continue
if single_line == "[ENCRYPT]":
= True
encrypt_mode continue
if single_line == "[DECRYPT]":
= False
encrypt_mode continue
if single_line.startswith('COUNT'):
= {
kat "count": None,
"key": None,
"plaintext": None,
"ciphertext": None,
} for _ in range(4):
= list(map(lambda x:x.strip(), single_line.split('=')))
data 0].lower()] = data[1]
kat[data[= f.readline()
single_line =kat)
ASingleKAT.correctType(kat= ASingleKAT(**kat)
kat if encrypt_mode:
kat.testInEncryptMode()else:
kat.testInDecryptMode()
The beginning of these NIST KAT files look as follows:
# CAVS 11.1
# Config info for aes_values
# AESVS VarKey test data for ECB
# State : Encrypt and Decrypt
# Key Length : 256
# Generated on Fri Apr 22 15:11:30 2011
[ENCRYPT]
COUNT = 0
KEY = 8000000000000000000000000000000000000000000000000000000000000000
PLAINTEXT = 00000000000000000000000000000000
CIPHERTEXT = e35a6dcb19b201a01ebcfa8aa22b5759
COUNT = 1
KEY = c000000000000000000000000000000000000000000000000000000000000000
PLAINTEXT = 00000000000000000000000000000000
CIPHERTEXT = b29169cdcf2d83e838125a12ee6aa400
COUNT = 2
KEY = e000000000000000000000000000000000000000000000000000000000000000
PLAINTEXT = 00000000000000000000000000000000
CIPHERTEXT = d8f3a72fc3cdf74dfaf6c3e6b97b2fa6
COUNT = 3
KEY = f000000000000000000000000000000000000000000000000000000000000000
PLAINTEXT = 00000000000000000000000000000000
CIPHERTEXT = 1c777679d50037c79491a94da76a9a35
# and goes on and on and on....
Running the above program, we get the following:
You can download these “rsp” files from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/aes/KAT_AES.zip