| 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.st_common import list_test_cases |
| 35 |
from Crypto.Util.py3compat import tobytes |
| 36 |
from Crypto.Cipher import AES, DES3, DES |
| 37 |
from Crypto.Hash import SHAKE128 |
| 38 |
from Crypto.SelfTest.loader import load_test_vectors_wycheproof |
| 39 |
|
| 40 |
def get_tag_random(tag, length): |
| 41 |
return SHAKE128.new(data=tobytes(tag)).read(length) |
| 42 |
|
| 43 |
from Crypto.SelfTest.Cipher.test_CBC import BlockChainingTests |
| 44 |
|
| 45 |
class OfbTests(BlockChainingTests): |
| 46 |
|
| 47 |
aes_mode = AES.MODE_OFB |
| 48 |
des3_mode = DES3.MODE_OFB |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
def test_unaligned_data_128(self): |
| 53 |
plaintexts = [ b"7777777" ] * 100 |
| 54 |
|
| 55 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=8) |
| 56 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 57 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=8) |
| 58 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 59 |
|
| 60 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=128) |
| 61 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 62 |
cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=128) |
| 63 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 64 |
|
| 65 |
def test_unaligned_data_64(self): |
| 66 |
plaintexts = [ b"7777777" ] * 100 |
| 67 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=8) |
| 68 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 69 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=8) |
| 70 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 71 |
|
| 72 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=64) |
| 73 |
ciphertexts = [ cipher.encrypt(x) for x in plaintexts ] |
| 74 |
cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=64) |
| 75 |
self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts))) |
| 76 |
|
| 77 |
|
| 78 |
from Crypto.SelfTest.Cipher.test_CBC import NistBlockChainingVectors |
| 79 |
|
| 80 |
class NistOfbVectors(NistBlockChainingVectors): |
| 81 |
aes_mode = AES.MODE_OFB |
| 82 |
des_mode = DES.MODE_OFB |
| 83 |
des3_mode = DES3.MODE_OFB |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
nist_aes_kat_mmt_files = ( |
| 88 |
|
| 89 |
"OFBGFSbox128.rsp", |
| 90 |
"OFBGFSbox192.rsp", |
| 91 |
"OFBGFSbox256.rsp", |
| 92 |
"OFBKeySbox128.rsp", |
| 93 |
"OFBKeySbox192.rsp", |
| 94 |
"OFBKeySbox256.rsp", |
| 95 |
"OFBVarKey128.rsp", |
| 96 |
"OFBVarKey192.rsp", |
| 97 |
"OFBVarKey256.rsp", |
| 98 |
"OFBVarTxt128.rsp", |
| 99 |
"OFBVarTxt192.rsp", |
| 100 |
"OFBVarTxt256.rsp", |
| 101 |
|
| 102 |
"OFBMMT128.rsp", |
| 103 |
"OFBMMT192.rsp", |
| 104 |
"OFBMMT256.rsp", |
| 105 |
) |
| 106 |
nist_aes_mct_files = ( |
| 107 |
"OFBMCT128.rsp", |
| 108 |
"OFBMCT192.rsp", |
| 109 |
"OFBMCT256.rsp", |
| 110 |
) |
| 111 |
|
| 112 |
for file_name in nist_aes_kat_mmt_files: |
| 113 |
def new_func(self, file_name=file_name): |
| 114 |
self._do_kat_aes_test(file_name) |
| 115 |
setattr(NistOfbVectors, "test_AES_" + file_name, new_func) |
| 116 |
|
| 117 |
for file_name in nist_aes_mct_files: |
| 118 |
def new_func(self, file_name=file_name): |
| 119 |
self._do_mct_aes_test(file_name) |
| 120 |
setattr(NistOfbVectors, "test_AES_" + file_name, new_func) |
| 121 |
del file_name, new_func |
| 122 |
|
| 123 |
nist_tdes_files = ( |
| 124 |
"TOFBMMT2.rsp", |
| 125 |
"TOFBMMT3.rsp", |
| 126 |
"TOFBinvperm.rsp", |
| 127 |
"TOFBpermop.rsp", |
| 128 |
"TOFBsubtab.rsp", |
| 129 |
"TOFBvarkey.rsp", |
| 130 |
"TOFBvartext.rsp", |
| 131 |
) |
| 132 |
|
| 133 |
for file_name in nist_tdes_files: |
| 134 |
def new_func(self, file_name=file_name): |
| 135 |
self._do_tdes_test(file_name) |
| 136 |
setattr(NistOfbVectors, "test_TDES_" + file_name, new_func) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class SP800TestVectors(unittest.TestCase): |
| 142 |
"""Class exercising the OFB test vectors found in Section F.4 |
| 143 |
of NIST SP 800-3A""" |
| 144 |
|
| 145 |
def test_aes_128(self): |
| 146 |
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\ |
| 147 |
'ae2d8a571e03ac9c9eb76fac45af8e51' +\ |
| 148 |
'30c81c46a35ce411e5fbc1191a0a52ef' +\ |
| 149 |
'f69f2445df4f9b17ad2b417be66c3710' |
| 150 |
ciphertext = '3b3fd92eb72dad20333449f8e83cfb4a' +\ |
| 151 |
'7789508d16918f03f53c52dac54ed825' +\ |
| 152 |
'9740051e9c5fecf64344f7a82260edcc' +\ |
| 153 |
'304c6528f659c77866a510d9c1d6ae5e' |
| 154 |
key = '2b7e151628aed2a6abf7158809cf4f3c' |
| 155 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 156 |
|
| 157 |
key = unhexlify(key) |
| 158 |
iv = unhexlify(iv) |
| 159 |
plaintext = unhexlify(plaintext) |
| 160 |
ciphertext = unhexlify(ciphertext) |
| 161 |
|
| 162 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 163 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 164 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 165 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 166 |
|
| 167 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 168 |
self.assertEqual(cipher.encrypt(plaintext[:-8]), ciphertext[:-8]) |
| 169 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 170 |
self.assertEqual(cipher.decrypt(ciphertext[:-8]), plaintext[:-8]) |
| 171 |
|
| 172 |
def test_aes_192(self): |
| 173 |
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\ |
| 174 |
'ae2d8a571e03ac9c9eb76fac45af8e51' +\ |
| 175 |
'30c81c46a35ce411e5fbc1191a0a52ef' +\ |
| 176 |
'f69f2445df4f9b17ad2b417be66c3710' |
| 177 |
ciphertext = 'cdc80d6fddf18cab34c25909c99a4174' +\ |
| 178 |
'fcc28b8d4c63837c09e81700c1100401' +\ |
| 179 |
'8d9a9aeac0f6596f559c6d4daf59a5f2' +\ |
| 180 |
'6d9f200857ca6c3e9cac524bd9acc92a' |
| 181 |
key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b' |
| 182 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 183 |
|
| 184 |
key = unhexlify(key) |
| 185 |
iv = unhexlify(iv) |
| 186 |
plaintext = unhexlify(plaintext) |
| 187 |
ciphertext = unhexlify(ciphertext) |
| 188 |
|
| 189 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 190 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 191 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 192 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 193 |
|
| 194 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 195 |
self.assertEqual(cipher.encrypt(plaintext[:-8]), ciphertext[:-8]) |
| 196 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 197 |
self.assertEqual(cipher.decrypt(ciphertext[:-8]), plaintext[:-8]) |
| 198 |
|
| 199 |
def test_aes_256(self): |
| 200 |
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\ |
| 201 |
'ae2d8a571e03ac9c9eb76fac45af8e51' +\ |
| 202 |
'30c81c46a35ce411e5fbc1191a0a52ef' +\ |
| 203 |
'f69f2445df4f9b17ad2b417be66c3710' |
| 204 |
ciphertext = 'dc7e84bfda79164b7ecd8486985d3860' +\ |
| 205 |
'4febdc6740d20b3ac88f6ad82a4fb08d' +\ |
| 206 |
'71ab47a086e86eedf39d1c5bba97c408' +\ |
| 207 |
'0126141d67f37be8538f5a8be740e484' |
| 208 |
key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4' |
| 209 |
iv = '000102030405060708090a0b0c0d0e0f' |
| 210 |
|
| 211 |
key = unhexlify(key) |
| 212 |
iv = unhexlify(iv) |
| 213 |
plaintext = unhexlify(plaintext) |
| 214 |
ciphertext = unhexlify(ciphertext) |
| 215 |
|
| 216 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 217 |
self.assertEqual(cipher.encrypt(plaintext), ciphertext) |
| 218 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 219 |
self.assertEqual(cipher.decrypt(ciphertext), plaintext) |
| 220 |
|
| 221 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 222 |
self.assertEqual(cipher.encrypt(plaintext[:-8]), ciphertext[:-8]) |
| 223 |
cipher = AES.new(key, AES.MODE_OFB, iv) |
| 224 |
self.assertEqual(cipher.decrypt(ciphertext[:-8]), plaintext[:-8]) |
| 225 |
|
| 226 |
|
| 227 |
def get_tests(config={}): |
| 228 |
tests = [] |
| 229 |
tests += list_test_cases(OfbTests) |
| 230 |
if config.get('slow_tests'): |
| 231 |
tests += list_test_cases(NistOfbVectors) |
| 232 |
tests += list_test_cases(SP800TestVectors) |
| 233 |
return tests |
| 234 |
|
| 235 |
|
| 236 |
if __name__ == '__main__': |
| 237 |
suite = lambda: unittest.TestSuite(get_tests()) |
| 238 |
unittest.main(defaultTest='suite') |