forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes.py
More file actions
143 lines (122 loc) · 4.5 KB
/
aes.py
File metadata and controls
143 lines (122 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import aesio
from binascii import hexlify, unhexlify
# doc example
key = b"Sixteen byte key"
inp = b"CircuitPython!!!" # Note: 16-bytes long
outp = bytearray(len(inp))
cipher = aesio.AES(key, aesio.MODE_ECB)
cipher.encrypt_into(inp, outp)
print(str(hexlify(outp), ""))
cipher = aesio.AES(key, aesio.MODE_ECB)
cipher.decrypt_into(outp, outp)
print(str(outp, ""))
print()
print("ECB")
# ECB mode test vector, from the aes.c source
plaintext = unhexlify(
"6bc1bee22e409f96e93d7e117393172a"
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710"
)
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
cyphertext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_ECB)
for i in range(0, len(plaintext), 16):
output = memoryview(cyphertext)[i : i + 16]
cipher.encrypt_into(plaintext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_ECB)
for i in range(0, len(plaintext), 16):
output = memoryview(plaintext)[i : i + 16]
cipher.decrypt_into(cyphertext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
print("CBC")
# CBC128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p50
plaintext = unhexlify(
"6bc1bee22e409f96e93d7e117393172a"
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710"
)
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
iv = unhexlify("000102030405060708090a0b0c0d0e0f")
cyphertext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv)
for i in range(0, len(plaintext), 16):
output = memoryview(cyphertext)[i : i + 16]
cipher.encrypt_into(plaintext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv)
for i in range(0, len(plaintext), 16):
output = memoryview(plaintext)[i : i + 16]
cipher.decrypt_into(cyphertext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
print("CTR")
# CTR128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p55
plaintext = unhexlify(
"6bc1bee22e409f96e93d7e117393172a"
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710"
)
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
counter = unhexlify("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
cyphertext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
for i in range(0, len(plaintext), 16):
output = memoryview(cyphertext)[i : i + 16]
cipher.encrypt_into(plaintext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
for i in range(0, len(plaintext), 16):
output = memoryview(plaintext)[i : i + 16]
cipher.decrypt_into(cyphertext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
print("truncated-CTR-1")
## Truncated CTR test case
plaintext = unhexlify("6bc1bee22e409f96e93d7e117393172a" "ae2d")
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
counter = unhexlify("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
cyphertext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
for i in range(0, len(plaintext), 16):
output = memoryview(cyphertext)[i : i + 16]
cipher.encrypt_into(plaintext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
for i in range(0, len(plaintext), 16):
output = memoryview(plaintext)[i : i + 16]
cipher.decrypt_into(cyphertext[i : i + 16], output)
print(str(hexlify(output), ""))
print()
print("truncated-CTR-2")
## Truncated CTR test case #2
plaintext = unhexlify("6bc1bee22e409f96e93d7e117393172a" "ae")
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
counter = unhexlify("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
cyphertext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
cipher.encrypt_into(plaintext, cyphertext)
for i in range(0, len(plaintext), 16):
output = memoryview(cyphertext)[i : i + 16]
print(str(hexlify(output), ""))
print()
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
cipher.decrypt_into(cyphertext, plaintext)
for i in range(0, len(plaintext), 16):
output = memoryview(plaintext)[i : i + 16]
print(str(hexlify(output), ""))
print()