Skip to content

Commit 1654f5f

Browse files
committed
add an aes test
This combines some test vectors from the implementation & a NIST standards document, plus the code from the docstring. The test vectors were eyeball-verified.
1 parent 62895b2 commit 1654f5f

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

tests/circuitpython/aes.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import aesio
2+
from binascii import hexlify, unhexlify
3+
4+
# doc example
5+
key = b"Sixteen byte key"
6+
inp = b"CircuitPython!!!" # Note: 16-bytes long
7+
outp = bytearray(len(inp))
8+
cipher = aesio.AES(key, aesio.MODE_ECB)
9+
cipher.encrypt_into(inp, outp)
10+
print(str(hexlify(outp), ""))
11+
12+
cipher = aesio.AES(key, aesio.MODE_ECB)
13+
cipher.decrypt_into(outp, outp)
14+
print(str(outp, ""))
15+
print()
16+
17+
print("ECB")
18+
# ECB mode test vector, from the aes.c source
19+
plaintext = unhexlify(
20+
"6bc1bee22e409f96e93d7e117393172a"
21+
"ae2d8a571e03ac9c9eb76fac45af8e51"
22+
"30c81c46a35ce411e5fbc1191a0a52ef"
23+
"f69f2445df4f9b17ad2b417be66c3710"
24+
)
25+
26+
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
27+
28+
cyphertext = bytearray(len(plaintext))
29+
cipher = aesio.AES(key, aesio.MODE_ECB)
30+
for i in range(0, len(plaintext), 16):
31+
output = memoryview(cyphertext)[i : i + 16]
32+
cipher.encrypt_into(plaintext[i : i + 16], output)
33+
print(str(hexlify(output), ""))
34+
print()
35+
36+
plaintext = bytearray(len(plaintext))
37+
cipher = aesio.AES(key, aesio.MODE_ECB)
38+
for i in range(0, len(plaintext), 16):
39+
output = memoryview(plaintext)[i : i + 16]
40+
cipher.decrypt_into(cyphertext[i : i + 16], output)
41+
print(str(hexlify(output), ""))
42+
print()
43+
44+
print("CBC")
45+
# CBC128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p50
46+
47+
plaintext = unhexlify(
48+
"6bc1bee22e409f96e93d7e117393172a"
49+
"ae2d8a571e03ac9c9eb76fac45af8e51"
50+
"30c81c46a35ce411e5fbc1191a0a52ef"
51+
"f69f2445df4f9b17ad2b417be66c3710"
52+
)
53+
54+
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
55+
iv = unhexlify("000102030405060708090a0b0c0d0e0f")
56+
cyphertext = bytearray(len(plaintext))
57+
cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv)
58+
for i in range(0, len(plaintext), 16):
59+
output = memoryview(cyphertext)[i : i + 16]
60+
cipher.encrypt_into(plaintext[i : i + 16], output)
61+
print(str(hexlify(output), ""))
62+
print()
63+
64+
plaintext = bytearray(len(plaintext))
65+
cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv)
66+
for i in range(0, len(plaintext), 16):
67+
output = memoryview(plaintext)[i : i + 16]
68+
cipher.decrypt_into(cyphertext[i : i + 16], output)
69+
print(str(hexlify(output), ""))
70+
print()
71+
72+
73+
print("CTR")
74+
# CTR128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p55
75+
76+
plaintext = unhexlify(
77+
"6bc1bee22e409f96e93d7e117393172a"
78+
"ae2d8a571e03ac9c9eb76fac45af8e51"
79+
"30c81c46a35ce411e5fbc1191a0a52ef"
80+
"f69f2445df4f9b17ad2b417be66c3710"
81+
)
82+
83+
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
84+
counter = unhexlify("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
85+
cyphertext = bytearray(len(plaintext))
86+
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
87+
for i in range(0, len(plaintext), 16):
88+
output = memoryview(cyphertext)[i : i + 16]
89+
cipher.encrypt_into(plaintext[i : i + 16], output)
90+
print(str(hexlify(output), ""))
91+
print()
92+
93+
plaintext = bytearray(len(plaintext))
94+
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
95+
for i in range(0, len(plaintext), 16):
96+
output = memoryview(plaintext)[i : i + 16]
97+
cipher.decrypt_into(cyphertext[i : i + 16], output)
98+
print(str(hexlify(output), ""))
99+
print()

tests/circuitpython/aes.py.exp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
abb1a1f98f57409e455ac06e71535ffe
2+
CircuitPython!!!
3+
4+
ECB
5+
3ad77bb40d7a3660a89ecaf32466ef97
6+
f5d3d58503b9699de785895a96fdbaaf
7+
43b1cd7f598ece23881b00e3ed030688
8+
7b0c785e27e8ad3f8223207104725dd4
9+
10+
6bc1bee22e409f96e93d7e117393172a
11+
ae2d8a571e03ac9c9eb76fac45af8e51
12+
30c81c46a35ce411e5fbc1191a0a52ef
13+
f69f2445df4f9b17ad2b417be66c3710
14+
15+
CBC
16+
7649abac8119b246cee98e9b12e9197d
17+
5086cb9b507219ee95db113a917678b2
18+
73bed6b8e3c1743b7116e69e22229516
19+
3ff1caa1681fac09120eca307586e1a7
20+
21+
6bc1bee22e409f96e93d7e117393172a
22+
ae2d8a571e03ac9c9eb76fac45af8e51
23+
30c81c46a35ce411e5fbc1191a0a52ef
24+
f69f2445df4f9b17ad2b417be66c3710
25+
26+
CTR
27+
874d6191b620e3261bef6864990db6ce
28+
9806f66b7970fdff8617187bb9fffdff
29+
5ae4df3edbd5d35e5b4f09020db03eab
30+
1e031dda2fbe03d1792170a0f3009cee
31+
32+
6bc1bee22e409f96e93d7e117393172a
33+
ae2d8a571e03ac9c9eb76fac45af8e51
34+
30c81c46a35ce411e5fbc1191a0a52ef
35+
f69f2445df4f9b17ad2b417be66c3710
36+

0 commit comments

Comments
 (0)