Debian Codegraph @

lib/Crypto/SelfTest/Cipher/test_DES3.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # SelfTest/Cipher/DES3.py: Self-test for the Triple-DES cipher
4 #
5 # Written in 2008 by Dwayne C. Litzenberger <[email protected]>
6 #
7 # ===================================================================
8 # The contents of this file are dedicated to the public domain. To
9 # the extent that dedication to the public domain is not available,
10 # everyone is granted a worldwide, perpetual, royalty-free,
11 # non-exclusive license to exercise all rights associated with the
12 # contents of this file for any purpose whatsoever.
13 # No rights are reserved.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 # SOFTWARE.
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 # This is a list of (plaintext, ciphertext, key, description) tuples.
38 test_data = [
39 # Test vector from Appendix B of NIST SP 800-67
40 # "Recommendation for the Triple Data Encryption Algorithm (TDEA) Block
41 # Cipher"
42 # http://csrc.nist.gov/publications/nistpubs/800-67/SP800-67.pdf
43 ('54686520717566636b2062726f776e20666f78206a756d70',
44 'a826fd8ce53b855fcce21c8112256fe668d5c05dd9b6b900',
45 '0123456789abcdef23456789abcdef01456789abcdef0123',
46 'NIST SP800-67 B.1'),
47
48 # This test is designed to test the DES3 API, not the correctness of the
49 # output.
50 ('21e81b7ade88a259', '5c577d4d9b20c0f8',
51 '9b397ebf81b1181e282f4bb8adbadc6b', 'Two-key 3DES'),
52 ]
53
54 # NIST CAVP test vectors
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 # The test vector file contains some directive lines
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 # K1 == K2
99 self.assertRaises(ValueError, DES3.adjust_key_parity,
100 sub_key1 * 2 + sub_key2)
101
102 # K2 == K3
103 self.assertRaises(ValueError, DES3.adjust_key_parity,
104 sub_key1 + sub_key2 * 2)
105
106 # K1 == K2 == K3
107 self.assertRaises(ValueError, DES3.adjust_key_parity,
108 sub_key1 * 3)
109
110 # K1 == K2 (with different parity)
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 # K1 == K2
122 self.assertRaises(ValueError, DES3.new,
123 sub_key1 * 2 + sub_key2,
124 DES3.MODE_ECB)
125
126 # K2 == K3
127 self.assertRaises(ValueError, DES3.new,
128 sub_key1 + sub_key2 * 2,
129 DES3.MODE_ECB)
130
131 # K1 == K2 == K3
132 self.assertRaises(ValueError, DES3.new,
133 sub_key1 * 3,
134 DES3.MODE_ECB)
135
136 # K2 == K3 (parity is ignored)
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 # Encrypt/Decrypt data and test output parameter
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 # vim:set ts=4 sw=4 sts=4 expandtab: