| 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 |
"""Self-test suite for Crypto.Cipher.DES3""" |
| 26 |
|
| 27 |
import unittest |
| 28 |
from binascii import hexlify, unhexlify |
| 29 |
|
| 30 |
from Crypto.Cipher import DES3 |
| 31 |
|
| 32 |
from Crypto.Util.strxor import strxor_c |
| 33 |
from Crypto.Util.py3compat import bchr, tostr |
| 34 |
from Crypto.SelfTest.loader import load_test_vectors |
| 35 |
from Crypto.SelfTest.st_common import list_test_cases |
| 36 |
|
| 37 |
|
| 38 |
test_data = [ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
('54686520717566636b2062726f776e20666f78206a756d70', |
| 44 |
'a826fd8ce53b855fcce21c8112256fe668d5c05dd9b6b900', |
| 45 |
'0123456789abcdef23456789abcdef01456789abcdef0123', |
| 46 |
'NIST SP800-67 B.1'), |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
('21e81b7ade88a259', '5c577d4d9b20c0f8', |
| 51 |
'9b397ebf81b1181e282f4bb8adbadc6b', 'Two-key 3DES'), |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
nist_tdes_mmt_files = ("TECBMMT2.rsp", "TECBMMT3.rsp") |
| 57 |
|
| 58 |
for tdes_file in nist_tdes_mmt_files: |
| 59 |
|
| 60 |
test_vectors = load_test_vectors( |
| 61 |
("Cipher", "TDES"), |
| 62 |
tdes_file, |
| 63 |
"TDES ECB (%s)" % tdes_file, |
| 64 |
{"count": lambda x: int(x)}) or [] |
| 65 |
|
| 66 |
for index, tv in enumerate(test_vectors): |
| 67 |
|
| 68 |
|
| 69 |
if isinstance(tv, str): |
| 70 |
continue |
| 71 |
|
| 72 |
key = tv.key1 + tv.key2 + tv.key3 |
| 73 |
test_data_item = (tostr(hexlify(tv.plaintext)), |
| 74 |
tostr(hexlify(tv.ciphertext)), |
| 75 |
tostr(hexlify(key)), |
| 76 |
"%s (%s)" % (tdes_file, index)) |
| 77 |
test_data.append(test_data_item) |
| 78 |
|
| 79 |
|
| 80 |
class CheckParity(unittest.TestCase): |
| 81 |
|
| 82 |
def test_parity_option2(self): |
| 83 |
before_2k = unhexlify("CABF326FA56734324FFCCABCDEFACABF") |
| 84 |
after_2k = DES3.adjust_key_parity(before_2k) |
| 85 |
self.assertEqual(after_2k, |
| 86 |
unhexlify("CBBF326EA46734324FFDCBBCDFFBCBBF")) |
| 87 |
|
| 88 |
def test_parity_option3(self): |
| 89 |
before_3k = unhexlify("AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC") |
| 90 |
after_3k = DES3.adjust_key_parity(before_3k) |
| 91 |
self.assertEqual(after_3k, |
| 92 |
unhexlify("ABABABABABABABABBABABABABABABABACDCDCDCDCDCDCDCD")) |
| 93 |
|
| 94 |
def test_degradation(self): |
| 95 |
sub_key1 = bchr(1) * 8 |
| 96 |
sub_key2 = bchr(255) * 8 |
| 97 |
|
| 98 |
|
| 99 |
self.assertRaises(ValueError, DES3.adjust_key_parity, |
| 100 |
sub_key1 * 2 + sub_key2) |
| 101 |
|
| 102 |
|
| 103 |
self.assertRaises(ValueError, DES3.adjust_key_parity, |
| 104 |
sub_key1 + sub_key2 * 2) |
| 105 |
|
| 106 |
|
| 107 |
self.assertRaises(ValueError, DES3.adjust_key_parity, |
| 108 |
sub_key1 * 3) |
| 109 |
|
| 110 |
|
| 111 |
self.assertRaises(ValueError, DES3.adjust_key_parity, |
| 112 |
sub_key1 + strxor_c(sub_key1, 1) + sub_key2) |
| 113 |
|
| 114 |
|
| 115 |
class DegenerateToDESTest(unittest.TestCase): |
| 116 |
|
| 117 |
def runTest(self): |
| 118 |
sub_key1 = bchr(1) * 8 |
| 119 |
sub_key2 = bchr(255) * 8 |
| 120 |
|
| 121 |
|
| 122 |
self.assertRaises(ValueError, DES3.new, |
| 123 |
sub_key1 * 2 + sub_key2, |
| 124 |
DES3.MODE_ECB) |
| 125 |
|
| 126 |
|
| 127 |
self.assertRaises(ValueError, DES3.new, |
| 128 |
sub_key1 + sub_key2 * 2, |
| 129 |
DES3.MODE_ECB) |
| 130 |
|
| 131 |
|
| 132 |
self.assertRaises(ValueError, DES3.new, |
| 133 |
sub_key1 * 3, |
| 134 |
DES3.MODE_ECB) |
| 135 |
|
| 136 |
|
| 137 |
self.assertRaises(ValueError, DES3.new, |
| 138 |
sub_key1 + sub_key2 + strxor_c(sub_key2, 0x1), |
| 139 |
DES3.MODE_ECB) |
| 140 |
|
| 141 |
|
| 142 |
class TestOutput(unittest.TestCase): |
| 143 |
|
| 144 |
def runTest(self): |
| 145 |
|
| 146 |
|
| 147 |
cipher = DES3.new(b'4'*8 + b'G'*8 + b'T'*8, DES3.MODE_ECB) |
| 148 |
|
| 149 |
pt = b'5' * 16 |
| 150 |
ct = cipher.encrypt(pt) |
| 151 |
|
| 152 |
output = bytearray(16) |
| 153 |
res = cipher.encrypt(pt, output=output) |
| 154 |
self.assertEqual(ct, output) |
| 155 |
self.assertEqual(res, None) |
| 156 |
|
| 157 |
res = cipher.decrypt(ct, output=output) |
| 158 |
self.assertEqual(pt, output) |
| 159 |
self.assertEqual(res, None) |
| 160 |
|
| 161 |
output = memoryview(bytearray(16)) |
| 162 |
cipher.encrypt(pt, output=output) |
| 163 |
self.assertEqual(ct, output) |
| 164 |
|
| 165 |
cipher.decrypt(ct, output=output) |
| 166 |
self.assertEqual(pt, output) |
| 167 |
|
| 168 |
self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) |
| 169 |
self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) |
| 170 |
|
| 171 |
shorter_output = bytearray(7) |
| 172 |
self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) |
| 173 |
self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) |
| 174 |
|
| 175 |
|
| 176 |
def get_tests(config={}): |
| 177 |
from .common import make_block_tests |
| 178 |
|
| 179 |
tests = [] |
| 180 |
tests = make_block_tests(DES3, "DES3", test_data) |
| 181 |
tests.append(DegenerateToDESTest()) |
| 182 |
tests += list_test_cases(CheckParity) |
| 183 |
tests += [TestOutput()] |
| 184 |
return tests |
| 185 |
|
| 186 |
|
| 187 |
if __name__ == '__main__': |
| 188 |
import unittest |
| 189 |
|
| 190 |
def suite(): |
| 191 |
unittest.TestSuite(get_tests()) |
| 192 |
|
| 193 |
unittest.main(defaultTest='suite') |
| 194 |
|
| 195 |
|