Debian Codegraph @

lib/Crypto/SelfTest/Cipher/test_CAST.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # SelfTest/Cipher/CAST.py: Self-test for the CAST-128 (CAST5) 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.CAST"""
26
27 import unittest
28
29 from Crypto.Util.py3compat import bchr
30
31 from Crypto.Cipher import CAST
32
33 # This is a list of (plaintext, ciphertext, key) tuples.
34 test_data = [
35 # Test vectors from RFC 2144, B.1
36 ('0123456789abcdef', '238b4fe5847e44b2',
37 '0123456712345678234567893456789a',
38 '128-bit key'),
39
40 ('0123456789abcdef', 'eb6a711a2c02271b',
41 '01234567123456782345',
42 '80-bit key'),
43
44 ('0123456789abcdef', '7ac816d16e9b302e',
45 '0123456712',
46 '40-bit key'),
47 ]
48
49
50 class KeyLength(unittest.TestCase):
51
52 def runTest(self):
53 self.assertRaises(ValueError, CAST.new, bchr(0) * 4, CAST.MODE_ECB)
54 self.assertRaises(ValueError, CAST.new, bchr(0) * 17, CAST.MODE_ECB)
55
56
57 class TestOutput(unittest.TestCase):
58
59 def runTest(self):
60 # Encrypt/Decrypt data and test output parameter
61
62 cipher = CAST.new(b'4'*16, CAST.MODE_ECB)
63
64 pt = b'5' * 16
65 ct = cipher.encrypt(pt)
66
67 output = bytearray(16)
68 res = cipher.encrypt(pt, output=output)
69 self.assertEqual(ct, output)
70 self.assertEqual(res, None)
71
72 res = cipher.decrypt(ct, output=output)
73 self.assertEqual(pt, output)
74 self.assertEqual(res, None)
75
76 output = memoryview(bytearray(16))
77 cipher.encrypt(pt, output=output)
78 self.assertEqual(ct, output)
79
80 cipher.decrypt(ct, output=output)
81 self.assertEqual(pt, output)
82
83 self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
84 self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16)
85
86 shorter_output = bytearray(7)
87 self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
88 self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
89
90
91 def get_tests(config={}):
92 from .common import make_block_tests
93
94 tests = make_block_tests(CAST, "CAST", test_data)
95 tests.append(KeyLength())
96 tests.append(TestOutput())
97 return tests
98
99 if __name__ == '__main__':
100 suite = lambda: unittest.TestSuite(get_tests())
101 unittest.main(defaultTest='suite')