test_CAST.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <dlitz@dlitz.net>
  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. """Self-test suite for Crypto.Cipher.CAST"""
  25. import unittest
  26. from tls.Crypto.Util.py3compat import bchr
  27. from tls.Crypto.Cipher import CAST
  28. # This is a list of (plaintext, ciphertext, key) tuples.
  29. test_data = [
  30. # Test vectors from RFC 2144, B.1
  31. ('0123456789abcdef', '238b4fe5847e44b2',
  32. '0123456712345678234567893456789a',
  33. '128-bit key'),
  34. ('0123456789abcdef', 'eb6a711a2c02271b',
  35. '01234567123456782345',
  36. '80-bit key'),
  37. ('0123456789abcdef', '7ac816d16e9b302e',
  38. '0123456712',
  39. '40-bit key'),
  40. ]
  41. class KeyLength(unittest.TestCase):
  42. def runTest(self):
  43. self.assertRaises(ValueError, CAST.new, bchr(0) * 4, CAST.MODE_ECB)
  44. self.assertRaises(ValueError, CAST.new, bchr(0) * 17, CAST.MODE_ECB)
  45. class TestOutput(unittest.TestCase):
  46. def runTest(self):
  47. # Encrypt/Decrypt data and test output parameter
  48. cipher = CAST.new(b'4'*16, CAST.MODE_ECB)
  49. pt = b'5' * 16
  50. ct = cipher.encrypt(pt)
  51. output = bytearray(16)
  52. res = cipher.encrypt(pt, output=output)
  53. self.assertEqual(ct, output)
  54. self.assertEqual(res, None)
  55. res = cipher.decrypt(ct, output=output)
  56. self.assertEqual(pt, output)
  57. self.assertEqual(res, None)
  58. import sys
  59. if sys.version[:3] != '2.6':
  60. output = memoryview(bytearray(16))
  61. cipher.encrypt(pt, output=output)
  62. self.assertEqual(ct, output)
  63. cipher.decrypt(ct, output=output)
  64. self.assertEqual(pt, output)
  65. self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
  66. self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16)
  67. shorter_output = bytearray(7)
  68. self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
  69. self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
  70. def get_tests(config={}):
  71. from .common import make_block_tests
  72. tests = make_block_tests(CAST, "CAST", test_data)
  73. tests.append(KeyLength())
  74. tests.append(TestOutput())
  75. return tests
  76. if __name__ == '__main__':
  77. suite = lambda: unittest.TestSuite(get_tests())
  78. unittest.main(defaultTest='suite')