| 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.CAST""" |
| 26 |
|
| 27 |
import unittest |
| 28 |
|
| 29 |
from Crypto.Util.py3compat import bchr |
| 30 |
|
| 31 |
from Crypto.Cipher import CAST |
| 32 |
|
| 33 |
|
| 34 |
test_data = [ |
| 35 |
|
| 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 |
|
| 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') |