| 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 |
import unittest |
| 32 |
from binascii import unhexlify |
| 33 |
|
| 34 |
from Crypto.SelfTest.loader import load_test_vectors |
| 35 |
from Crypto.SelfTest.st_common import list_test_cases |
| 36 |
from Crypto.Util.py3compat import tobytes, is_string |
| 37 |
from Crypto.Cipher import AES, DES3, DES |
| 38 |
from Crypto.Hash import SHAKE128 |
| 39 |
|
| 40 |
from Crypto.SelfTest.Cipher.test_CBC import BlockChainingTests |
| 41 |
|
| 42 |
|
| 43 |
def get_tag_random(tag, length): |
| 44 |
return SHAKE128.new(data=tobytes(tag)).read(length) |
| 45 |
|
| 46 |
|
| 47 |
class CfbTests(BlockChainingTests): |
| 48 |
|
| 49 |
aes_mode = AES.MODE_CFB |
| 50 |
des3_mode = DES3.MODE_CFB |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
def test_unaligned_data_128(self): |
| 55 |
plaintexts = [ b"7777777" ] * 100 |
| 56 |
|
| 57 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=8) |
| 58 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 59 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=8) |
| 60 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 61 |
|
| 62 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=128) |
| 63 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 64 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=128) |
| 65 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 66 |
|
| 67 |
def test_unaligned_data_64(self): |
| 68 |
plaintexts = [ b"7777777" ] * 100 |
| 69 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=8) |
| 70 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 71 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=8) |
| 72 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 73 |
|
| 74 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=64) |
| 75 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 76 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=64) |
| 77 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
def test_segment_size_128(self): |
| 82 |
for bits in range(8, 129, 8): |
| 83 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, |
| 84 |
segment_size=bits) |
| 85 |
|
| 86 |
for bits in 0, 7, 9, 127, 129: |
| 87 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CFB, |
| 88 |
self.iv_128, |
| 89 |
segment_size=bits) |
| 90 |
|
| 91 |
def test_segment_size_64(self): |
| 92 |
for bits in range(8, 65, 8): |
| 93 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, |
| 94 |
segment_size=bits) |
| 95 |
|
| 96 |
for bits in 0, 7, 9, 63, 65: |
| 97 |
self.assertRaises(ValueError, DES3.new, self.key_192, AES.MODE_CFB, |
| 98 |
self.iv_64, |
| 99 |
segment_size=bits) |
| 100 |
|
| 101 |
|
| 102 |
class NistCfbVectors(unittest.TestCase): |
| 103 |
|
| 104 |
def _do_kat_aes_test(self, file_name, segment_size): |
| 105 |
|
| 106 |
test_vectors = load_test_vectors(("Cipher", "AES"), |
| 107 |
file_name, |
| 108 |
"AES CFB%d KAT" % segment_size, |
| 109 |
{ "count" : lambda x: int(x) } ) |
| 110 |
if test_vectors is None: |
| 111 |
return |
| 112 |
|
| 113 |
direction = None |
| 114 |
for tv in test_vectors: |
| 115 |
|
| 116 |
|
| 117 |
if is_string(tv): |
| 118 |
direction = tv |
| 119 |
continue |
| 120 |
|
| 121 |
self.description = tv.desc |
| 122 |
cipher = AES.new(tv.key, AES.MODE_CFB, tv.iv, |
| 123 |
segment_size=segment_size) |
| 124 |
if direction == "[ENCRYPT]": |
| 125 |
self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext) |
| 126 |
elif direction == "[DECRYPT]": |
| 127 |
self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext) |
| 128 |
else: |
| 129 |
assert False |
| 130 |
|
| 131 |
|
| 132 |
def _do_mct_aes_test(self, file_name, segment_size): |
| 133 |
|
| 134 |
test_vectors = load_test_vectors(("Cipher", "AES"), |
| 135 |
file_name, |
| 136 |
"AES CFB%d Montecarlo" % segment_size, |
| 137 |
{ "count" : lambda x: int(x) } ) |
| 138 |
if test_vectors is None: |
| 139 |
return |
| 140 |
|
| 141 |
assert(segment_size in (8, 128)) |
| 142 |
|
| 143 |
direction = None |
| 144 |
for tv in test_vectors: |
| 145 |
|
| 146 |
|
| 147 |
if is_string(tv): |
| 148 |
direction = tv |
| 149 |
continue |
| 150 |
|
| 151 |
self.description = tv.desc |
| 152 |
cipher = AES.new(tv.key, AES.MODE_CFB, tv.iv, |
| 153 |
segment_size=segment_size) |
| 154 |
|
| 155 |
def get_input(input_text, output_seq, j): |
| 156 |
|
| 157 |
if segment_size == 128: |
| 158 |
if j >= 2: |
| 159 |
return output_seq[-2] |
| 160 |
return [input_text, tv.iv][j] |
| 161 |
|
| 162 |
if j == 0: |
| 163 |
return input_text |
| 164 |
elif j <= 16: |
| 165 |
return tv.iv[j - 1:j] |
| 166 |
return output_seq[j - 17] |
| 167 |
|
| 168 |
if direction == '[ENCRYPT]': |
| 169 |
cts = [] |
| 170 |
for j in range(1000): |
| 171 |
plaintext = get_input(tv.plaintext, cts, j) |
| 172 |
cts.append(cipher.encrypt(plaintext)) |
| 173 |
self.assertEqual(cts[-1], tv.ciphertext) |
| 174 |
elif direction == '[DECRYPT]': |
| 175 |
pts = [] |
| 176 |
for j in range(1000): |
| 177 |
ciphertext = get_input(tv.ciphertext, pts, j) |
| 178 |
pts.append(cipher.decrypt(ciphertext)) |
| 179 |
self.assertEqual(pts[-1], tv.plaintext) |
| 180 |
else: |
| 181 |
assert False |
| 182 |
|
| 183 |
def _do_tdes_test(self, file_name, segment_size): |
| 184 |
|
| 185 |
test_vectors = load_test_vectors(("Cipher", "TDES"), |
| 186 |
file_name, |
| 187 |
"TDES CFB%d KAT" % segment_size, |
| 188 |
{ "count" : lambda x: int(x) } ) |
| 189 |
if test_vectors is None: |
| 190 |
return |
| 191 |
|
| 192 |
direction = None |
| 193 |
for tv in test_vectors: |
| 194 |
|
| 195 |
|
| 196 |
if is_string(tv): |
| 197 |
direction = tv |
| 198 |
continue |
| 199 |
|
| 200 |
self.description = tv.desc |
| 201 |
if hasattr(tv, "keys"): |
| 202 |
cipher = DES.new(tv.keys, DES.MODE_CFB, tv.iv, |
| 203 |
segment_size=segment_size) |
| 204 |
else: |
| 205 |
if tv.key1 != tv.key3: |
| 206 |
key = tv.key1 + tv.key2 + tv.key3 |
| 207 |
else: |
| 208 |
key = tv.key1 + tv.key2 |
| 209 |
cipher = DES3.new(key, DES3.MODE_CFB, tv.iv, |
| 210 |
segment_size=segment_size) |
| 211 |
if direction == "[ENCRYPT]": |
| 212 |
self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext) |
| 213 |
elif direction == "[DECRYPT]": |
| 214 |
self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext) |
| 215 |
else: |
| 216 |
assert False |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
nist_aes_kat_mmt_files = ( |
| 221 |
|
| 222 |
"CFB?GFSbox128.rsp", |
| 223 |
"CFB?GFSbox192.rsp", |
| 224 |
"CFB?GFSbox256.rsp", |
| 225 |
"CFB?KeySbox128.rsp", |
| 226 |
"CFB?KeySbox192.rsp", |
| 227 |
"CFB?KeySbox256.rsp", |
| 228 |
"CFB?VarKey128.rsp", |
| 229 |
"CFB?VarKey192.rsp", |
| 230 |
"CFB?VarKey256.rsp", |
| 231 |
"CFB?VarTxt128.rsp", |
| 232 |
"CFB?VarTxt192.rsp", |
| 233 |
"CFB?VarTxt256.rsp", |
| 234 |
|
| 235 |
"CFB?MMT128.rsp", |
| 236 |
"CFB?MMT192.rsp", |
| 237 |
"CFB?MMT256.rsp", |
| 238 |
) |
| 239 |
nist_aes_mct_files = ( |
| 240 |
"CFB?MCT128.rsp", |
| 241 |
"CFB?MCT192.rsp", |
| 242 |
"CFB?MCT256.rsp", |
| 243 |
) |
| 244 |
|
| 245 |
for file_gen_name in nist_aes_kat_mmt_files: |
| 246 |
for bits in "8", "128": |
| 247 |
file_name = file_gen_name.replace("?", bits) |
| 248 |
def new_func(self, file_name=file_name, bits=bits): |
| 249 |
self._do_kat_aes_test(file_name, int(bits)) |
| 250 |
setattr(NistCfbVectors, "test_AES_" + file_name, new_func) |
| 251 |
|
| 252 |
for file_gen_name in nist_aes_mct_files: |
| 253 |
for bits in "8", "128": |
| 254 |
file_name = file_gen_name.replace("?", bits) |
| 255 |
def new_func(self, file_name=file_name, bits=bits): |
| 256 |
self._do_mct_aes_test(file_name, int(bits)) |
| 257 |
setattr(NistCfbVectors, "test_AES_" + file_name, new_func) |
| 258 |
del file_name, new_func |
| 259 |
|
| 260 |
nist_tdes_files = ( |
| 261 |
"TCFB?MMT2.rsp", |
| 262 |
"TCFB?MMT3.rsp", |
| 263 |
"TCFB?invperm.rsp", |
| 264 |
"TCFB?permop.rsp", |
| 265 |
"TCFB?subtab.rsp", |
| 266 |
"TCFB?varkey.rsp", |
| 267 |
"TCFB?vartext.rsp", |
| 268 |
) |
| 269 |
|
| 270 |
for file_gen_name in nist_tdes_files: |
| 271 |
for bits in "8", "64": |
| 272 |
file_name = file_gen_name.replace("?", bits) |
| 273 |
def new_func(self, file_name=file_name, bits=bits): |
| 274 |
self._do_tdes_test(file_name, int(bits)) |
| 275 |
setattr(NistCfbVectors, "test_TDES_" + file_name, new_func) |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
class SP800TestVectors(unittest.TestCase): |
| 281 |
"""Class exercising the CFB test vectors found in Section F.3 |
| 282 |
of NIST SP 800-3A""" |
| 283 |
|
| 284 |
def test_aes_128_cfb8(self): |
| 285 |
plaintext = '6bc1bee22e409f96e93d7e117393172aae2d' |
| 286 |
ciphertext = '3b79424c9c0dd436bace9e0ed4586a4f32b9' |
| 287 |
key = '2b7e151628aed2a6abf7158809cf4f3c' |
| 288 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 289 |
|
| 290 |
key = unhexlify(key) |
| 291 |
iv = unhexlify(iv) |
| 292 |
plaintext = unhexlify(plaintext) |
| 293 |
ciphertext = unhexlify(ciphertext) |
| 294 |
|
| 295 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8) |
| 296 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 297 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8) |
| 298 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 299 |
|
| 300 |
def test_aes_192_cfb8(self): |
| 301 |
plaintext = '6bc1bee22e409f96e93d7e117393172aae2d' |
| 302 |
ciphertext = 'cda2521ef0a905ca44cd057cbf0d47a0678a' |
| 303 |
key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b' |
| 304 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 305 |
|
| 306 |
key = unhexlify(key) |
| 307 |
iv = unhexlify(iv) |
| 308 |
plaintext = unhexlify(plaintext) |
| 309 |
ciphertext = unhexlify(ciphertext) |
| 310 |
|
| 311 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8) |
| 312 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 313 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8) |
| 314 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 315 |
|
| 316 |
def test_aes_256_cfb8(self): |
| 317 |
plaintext = '6bc1bee22e409f96e93d7e117393172aae2d' |
| 318 |
ciphertext = 'dc1f1a8520a64db55fcc8ac554844e889700' |
| 319 |
key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4' |
| 320 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 321 |
|
| 322 |
key = unhexlify(key) |
| 323 |
iv = unhexlify(iv) |
| 324 |
plaintext = unhexlify(plaintext) |
| 325 |
ciphertext = unhexlify(ciphertext) |
| 326 |
|
| 327 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8) |
| 328 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 329 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8) |
| 330 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 331 |
|
| 332 |
def test_aes_128_cfb128(self): |
| 333 |
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\ |
| 334 |
'ae2d8a571e03ac9c9eb76fac45af8e51' +\ |
| 335 |
'30c81c46a35ce411e5fbc1191a0a52ef' +\ |
| 336 |
'f69f2445df4f9b17ad2b417be66c3710' |
| 337 |
ciphertext = '3b3fd92eb72dad20333449f8e83cfb4a' +\ |
| 338 |
'c8a64537a0b3a93fcde3cdad9f1ce58b' +\ |
| 339 |
'26751f67a3cbb140b1808cf187a4f4df' +\ |
| 340 |
'c04b05357c5d1c0eeac4c66f9ff7f2e6' |
| 341 |
key = '2b7e151628aed2a6abf7158809cf4f3c' |
| 342 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 343 |
|
| 344 |
key = unhexlify(key) |
| 345 |
iv = unhexlify(iv) |
| 346 |
plaintext = unhexlify(plaintext) |
| 347 |
ciphertext = unhexlify(ciphertext) |
| 348 |
|
| 349 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128) |
| 350 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 351 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128) |
| 352 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 353 |
|
| 354 |
def test_aes_192_cfb128(self): |
| 355 |
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\ |
| 356 |
'ae2d8a571e03ac9c9eb76fac45af8e51' +\ |
| 357 |
'30c81c46a35ce411e5fbc1191a0a52ef' +\ |
| 358 |
'f69f2445df4f9b17ad2b417be66c3710' |
| 359 |
ciphertext = 'cdc80d6fddf18cab34c25909c99a4174' +\ |
| 360 |
'67ce7f7f81173621961a2b70171d3d7a' +\ |
| 361 |
'2e1e8a1dd59b88b1c8e60fed1efac4c9' +\ |
| 362 |
'c05f9f9ca9834fa042ae8fba584b09ff' |
| 363 |
key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b' |
| 364 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 365 |
|
| 366 |
key = unhexlify(key) |
| 367 |
iv = unhexlify(iv) |
| 368 |
plaintext = unhexlify(plaintext) |
| 369 |
ciphertext = unhexlify(ciphertext) |
| 370 |
|
| 371 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128) |
| 372 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 373 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128) |
| 374 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 375 |
|
| 376 |
def test_aes_256_cfb128(self): |
| 377 |
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\ |
| 378 |
'ae2d8a571e03ac9c9eb76fac45af8e51' +\ |
| 379 |
'30c81c46a35ce411e5fbc1191a0a52ef' +\ |
| 380 |
'f69f2445df4f9b17ad2b417be66c3710' |
| 381 |
|
| 382 |
ciphertext = 'dc7e84bfda79164b7ecd8486985d3860' +\ |
| 383 |
'39ffed143b28b1c832113c6331e5407b' +\ |
| 384 |
'df10132415e54b92a13ed0a8267ae2f9' +\ |
| 385 |
'75a385741ab9cef82031623d55b1e471' |
| 386 |
key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4' |
| 387 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 388 |
|
| 389 |
key = unhexlify(key) |
| 390 |
iv = unhexlify(iv) |
| 391 |
plaintext = unhexlify(plaintext) |
| 392 |
ciphertext = unhexlify(ciphertext) |
| 393 |
|
| 394 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128) |
| 395 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 396 |
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128) |
| 397 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 398 |
|
| 399 |
|
| 400 |
def get_tests(config={}): |
| 401 |
tests = [] |
| 402 |
tests += list_test_cases(CfbTests) |
| 403 |
if config.get('slow_tests'): |
| 404 |
tests += list_test_cases(NistCfbVectors) |
| 405 |
tests += list_test_cases(SP800TestVectors) |
| 406 |
return tests |
| 407 |
|
| 408 |
|
| 409 |
if __name__ == '__main__': |
| 410 |
suite = lambda: unittest.TestSuite(get_tests()) |
| 411 |
unittest.main(defaultTest='suite') |