Skip to content

Commit 33815fc

Browse files
author
Ivan Nikolchev
committed
Add NIST test vectors for AES-CBC
1 parent 27157e0 commit 33815fc

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# compatibility with Python 2.6, for that we need unittest2 package,
2+
# which is not available on 3.3 or 3.4
3+
try:
4+
import unittest2 as unittest
5+
except ImportError:
6+
import unittest
7+
8+
from tlslite.utils.rijndael import Rijndael
9+
from tlslite.utils.python_aes import Python_AES
10+
11+
12+
class TestAESCBC(unittest.TestCase):
13+
def test___init__(self):
14+
key = bytearray(16)
15+
aesCBC = Python_AES(key, 2, bytearray(b'\x00' * 16))
16+
17+
self.assertIsNotNone(aesCBC)
18+
19+
def test___init___with_invalid_key(self):
20+
key = bytearray(8)
21+
22+
with self.assertRaises(AssertionError):
23+
aesCBC = Python_AES(key, 2, bytearray(b'\x00' * 16))
24+
25+
def test___init___with_invalid_iv(self):
26+
key = bytearray(16)
27+
28+
with self.assertRaises(AssertionError):
29+
aesCBC = Python_AES(key, 2, bytearray(b'\x00' * 8))
30+
31+
def test_encrypt_with_test_vector_1(self):
32+
33+
key = bytearray(b'\x2b\x7e\x15\x16\x28\xae\xd2'
34+
b'\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c')
35+
36+
IV = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
37+
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
38+
39+
plaintext = bytearray(b'\x6b\xc1\xbe\xe2\x2e\x40\x9f'
40+
b'\x96\xe9\x3d\x7e\x11\x73\x93'
41+
b'\x17\x2a\xae\x2d\x8a\x57\x1e'
42+
b'\x03\xac\x9c\x9e\xb7\x6f\xac'
43+
b'\x45\xaf\x8e\x51\x30\xc8\x1c'
44+
b'\x46\xa3\x5c\xe4\x11\xe5\xfb'
45+
b'\xc1\x19\x1a\x0a\x52\xef\xf6'
46+
b'\x9f\x24\x45\xdf\x4f\x9b\x17'
47+
b'\xad\x2b\x41\x7b\xe6\x6c\x37\x10')
48+
49+
ciphertext = bytearray(b'\x76\x49\xab\xac\x81\x19\xb2\x46'
50+
b'\xce\xe9\x8e\x9b\x12\xe9\x19\x7d'
51+
b'\x50\x86\xcb\x9b\x50\x72\x19\xee'
52+
b'\x95\xdb\x11\x3a\x91\x76\x78\xb2'
53+
b'\x73\xbe\xd6\xb8\xe3\xc1\x74\x3b'
54+
b'\x71\x16\xe6\x9e\x22\x22\x95\x16'
55+
b'\x3f\xf1\xca\xa1\x68\x1f\xac\x09'
56+
b'\x12\x0e\xca\x30\x75\x86\xe1\xa7')
57+
58+
aesCBC = Python_AES(key, 2, IV)
59+
self.assertEqual(aesCBC.encrypt(plaintext), ciphertext)
60+
61+
def test_decrypt_with_test_vector_1(self):
62+
63+
key = bytearray(b'\x2b\x7e\x15\x16\x28\xae\xd2'
64+
b'\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c')
65+
66+
IV = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
67+
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
68+
69+
plaintext = bytearray(b'\x6b\xc1\xbe\xe2\x2e\x40\x9f'
70+
b'\x96\xe9\x3d\x7e\x11\x73\x93'
71+
b'\x17\x2a\xae\x2d\x8a\x57\x1e'
72+
b'\x03\xac\x9c\x9e\xb7\x6f\xac'
73+
b'\x45\xaf\x8e\x51\x30\xc8\x1c'
74+
b'\x46\xa3\x5c\xe4\x11\xe5\xfb'
75+
b'\xc1\x19\x1a\x0a\x52\xef\xf6'
76+
b'\x9f\x24\x45\xdf\x4f\x9b\x17'
77+
b'\xad\x2b\x41\x7b\xe6\x6c\x37\x10')
78+
79+
ciphertext = bytearray(b'\x76\x49\xab\xac\x81\x19\xb2\x46'
80+
b'\xce\xe9\x8e\x9b\x12\xe9\x19\x7d'
81+
b'\x50\x86\xcb\x9b\x50\x72\x19\xee'
82+
b'\x95\xdb\x11\x3a\x91\x76\x78\xb2'
83+
b'\x73\xbe\xd6\xb8\xe3\xc1\x74\x3b'
84+
b'\x71\x16\xe6\x9e\x22\x22\x95\x16'
85+
b'\x3f\xf1\xca\xa1\x68\x1f\xac\x09'
86+
b'\x12\x0e\xca\x30\x75\x86\xe1\xa7')
87+
88+
aesCBC = Python_AES(key, 2, IV)
89+
self.assertEqual(aesCBC.decrypt(ciphertext), plaintext)
90+
91+
def test_encrypt_with_test_vector_2(self):
92+
93+
key = bytearray(b'\x8e\x73\xb0\xf7\xda\x0e\x64\x52'
94+
b'\xc8\x10\xf3\x2b\x80\x90\x79\xe5'
95+
b'\x62\xf8\xea\xd2\x52\x2c\x6b\x7b')
96+
97+
IV = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
98+
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
99+
100+
plaintext = bytearray(b'\x6b\xc1\xbe\xe2\x2e\x40\x9f'
101+
b'\x96\xe9\x3d\x7e\x11\x73\x93'
102+
b'\x17\x2a\xae\x2d\x8a\x57\x1e'
103+
b'\x03\xac\x9c\x9e\xb7\x6f\xac'
104+
b'\x45\xaf\x8e\x51\x30\xc8\x1c'
105+
b'\x46\xa3\x5c\xe4\x11\xe5\xfb'
106+
b'\xc1\x19\x1a\x0a\x52\xef\xf6'
107+
b'\x9f\x24\x45\xdf\x4f\x9b\x17'
108+
b'\xad\x2b\x41\x7b\xe6\x6c\x37\x10')
109+
110+
ciphertext = bytearray(b'\x4f\x02\x1d\xb2\x43\xbc\x63\x3d'
111+
b'\x71\x78\x18\x3a\x9f\xa0\x71\xe8'
112+
b'\xb4\xd9\xad\xa9\xad\x7d\xed\xf4'
113+
b'\xe5\xe7\x38\x76\x3f\x69\x14\x5a'
114+
b'\x57\x1b\x24\x20\x12\xfb\x7a\xe0'
115+
b'\x7f\xa9\xba\xac\x3d\xf1\x02\xe0'
116+
b'\x08\xb0\xe2\x79\x88\x59\x88\x81'
117+
b'\xd9\x20\xa9\xe6\x4f\x56\x15\xcd')
118+
119+
aesCBC = Python_AES(key, 2, IV)
120+
self.assertEqual(aesCBC.encrypt(plaintext), ciphertext)
121+
122+
def test_decrypt_with_test_vector_2(self):
123+
124+
key = bytearray(b'\x8e\x73\xb0\xf7\xda\x0e\x64\x52'
125+
b'\xc8\x10\xf3\x2b\x80\x90\x79\xe5'
126+
b'\x62\xf8\xea\xd2\x52\x2c\x6b\x7b')
127+
128+
IV = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
129+
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
130+
131+
plaintext = bytearray(b'\x6b\xc1\xbe\xe2\x2e\x40\x9f'
132+
b'\x96\xe9\x3d\x7e\x11\x73\x93'
133+
b'\x17\x2a\xae\x2d\x8a\x57\x1e'
134+
b'\x03\xac\x9c\x9e\xb7\x6f\xac'
135+
b'\x45\xaf\x8e\x51\x30\xc8\x1c'
136+
b'\x46\xa3\x5c\xe4\x11\xe5\xfb'
137+
b'\xc1\x19\x1a\x0a\x52\xef\xf6'
138+
b'\x9f\x24\x45\xdf\x4f\x9b\x17'
139+
b'\xad\x2b\x41\x7b\xe6\x6c\x37\x10')
140+
141+
ciphertext = bytearray(b'\x4f\x02\x1d\xb2\x43\xbc\x63\x3d'
142+
b'\x71\x78\x18\x3a\x9f\xa0\x71\xe8'
143+
b'\xb4\xd9\xad\xa9\xad\x7d\xed\xf4'
144+
b'\xe5\xe7\x38\x76\x3f\x69\x14\x5a'
145+
b'\x57\x1b\x24\x20\x12\xfb\x7a\xe0'
146+
b'\x7f\xa9\xba\xac\x3d\xf1\x02\xe0'
147+
b'\x08\xb0\xe2\x79\x88\x59\x88\x81'
148+
b'\xd9\x20\xa9\xe6\x4f\x56\x15\xcd')
149+
150+
aesCBC = Python_AES(key, 2, IV)
151+
self.assertEqual(aesCBC.decrypt(ciphertext), plaintext)
152+
153+
def test_encrypt_with_test_vector_3(self):
154+
155+
key = bytearray(b'\x60\x3d\xeb\x10\x15\xca\x71\xbe'
156+
b'\x2b\x73\xae\xf0\x85\x7d\x77\x81'
157+
b'\x1f\x35\x2c\x07\x3b\x61\x08\xd7'
158+
b'\x2d\x98\x10\xa3\x09\x14\xdf\xf4')
159+
160+
IV = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
161+
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
162+
163+
plaintext = bytearray(b'\x6b\xc1\xbe\xe2\x2e\x40\x9f'
164+
b'\x96\xe9\x3d\x7e\x11\x73\x93'
165+
b'\x17\x2a\xae\x2d\x8a\x57\x1e'
166+
b'\x03\xac\x9c\x9e\xb7\x6f\xac'
167+
b'\x45\xaf\x8e\x51\x30\xc8\x1c'
168+
b'\x46\xa3\x5c\xe4\x11\xe5\xfb'
169+
b'\xc1\x19\x1a\x0a\x52\xef\xf6'
170+
b'\x9f\x24\x45\xdf\x4f\x9b\x17'
171+
b'\xad\x2b\x41\x7b\xe6\x6c\x37\x10')
172+
173+
ciphertext = bytearray(b'\xf5\x8c\x4c\x04\xd6\xe5\xf1\xba'
174+
b'\x77\x9e\xab\xfb\x5f\x7b\xfb\xd6'
175+
b'\x9c\xfc\x4e\x96\x7e\xdb\x80\x8d'
176+
b'\x67\x9f\x77\x7b\xc6\x70\x2c\x7d'
177+
b'\x39\xf2\x33\x69\xa9\xd9\xba\xcf'
178+
b'\xa5\x30\xe2\x63\x04\x23\x14\x61'
179+
b'\xb2\xeb\x05\xe2\xc3\x9b\xe9\xfc'
180+
b'\xda\x6c\x19\x07\x8c\x6a\x9d\x1b')
181+
182+
aesCBC = Python_AES(key, 2, IV)
183+
self.assertEqual(aesCBC.encrypt(plaintext), ciphertext)
184+
185+
def test_decrypt_with_test_vector_3(self):
186+
187+
key = bytearray(b'\x60\x3d\xeb\x10\x15\xca\x71\xbe'
188+
b'\x2b\x73\xae\xf0\x85\x7d\x77\x81'
189+
b'\x1f\x35\x2c\x07\x3b\x61\x08\xd7'
190+
b'\x2d\x98\x10\xa3\x09\x14\xdf\xf4')
191+
192+
IV = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
193+
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
194+
195+
plaintext = bytearray(b'\x6b\xc1\xbe\xe2\x2e\x40\x9f'
196+
b'\x96\xe9\x3d\x7e\x11\x73\x93'
197+
b'\x17\x2a\xae\x2d\x8a\x57\x1e'
198+
b'\x03\xac\x9c\x9e\xb7\x6f\xac'
199+
b'\x45\xaf\x8e\x51\x30\xc8\x1c'
200+
b'\x46\xa3\x5c\xe4\x11\xe5\xfb'
201+
b'\xc1\x19\x1a\x0a\x52\xef\xf6'
202+
b'\x9f\x24\x45\xdf\x4f\x9b\x17'
203+
b'\xad\x2b\x41\x7b\xe6\x6c\x37\x10')
204+
205+
ciphertext = bytearray(b'\xf5\x8c\x4c\x04\xd6\xe5\xf1\xba'
206+
b'\x77\x9e\xab\xfb\x5f\x7b\xfb\xd6'
207+
b'\x9c\xfc\x4e\x96\x7e\xdb\x80\x8d'
208+
b'\x67\x9f\x77\x7b\xc6\x70\x2c\x7d'
209+
b'\x39\xf2\x33\x69\xa9\xd9\xba\xcf'
210+
b'\xa5\x30\xe2\x63\x04\x23\x14\x61'
211+
b'\xb2\xeb\x05\xe2\xc3\x9b\xe9\xfc'
212+
b'\xda\x6c\x19\x07\x8c\x6a\x9d\x1b')
213+
214+
aesCBC = Python_AES(key, 2, IV)
215+
self.assertEqual(aesCBC.decrypt(ciphertext), plaintext)

0 commit comments

Comments
 (0)