test_KW.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import unittest
  2. from Crypto.SelfTest.st_common import list_test_cases
  3. from Crypto.SelfTest.loader import load_test_vectors_wycheproof
  4. from Crypto.Cipher import AES
  5. class KW_Tests(unittest.TestCase):
  6. # From RFC3394
  7. tvs = [
  8. ("000102030405060708090A0B0C0D0E0F",
  9. "00112233445566778899AABBCCDDEEFF",
  10. "1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5"),
  11. ("000102030405060708090A0B0C0D0E0F1011121314151617",
  12. "00112233445566778899AABBCCDDEEFF",
  13. "96778B25AE6CA435F92B5B97C050AED2468AB8A17AD84E5D"),
  14. ("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
  15. "00112233445566778899AABBCCDDEEFF",
  16. "64E8C3F9CE0F5BA263E9777905818A2A93C8191E7D6E8AE7"),
  17. ("000102030405060708090A0B0C0D0E0F1011121314151617",
  18. "00112233445566778899AABBCCDDEEFF0001020304050607",
  19. "031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2"),
  20. ("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
  21. "00112233445566778899AABBCCDDEEFF0001020304050607",
  22. "A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1"),
  23. ("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
  24. "00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F",
  25. "28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21"),
  26. ]
  27. def test_rfc3394(self):
  28. for tv in self.tvs:
  29. kek, pt, ct = [bytes.fromhex(x) for x in tv]
  30. cipher = AES.new(kek, AES.MODE_KW)
  31. ct2 = cipher.seal(pt)
  32. self.assertEqual(ct, ct2)
  33. cipher = AES.new(kek, AES.MODE_KW)
  34. pt2 = cipher.unseal(ct)
  35. self.assertEqual(pt, pt2)
  36. def test_neg1(self):
  37. cipher = AES.new(b'-' * 16, AES.MODE_KW)
  38. with self.assertRaises(ValueError):
  39. cipher.seal(b'')
  40. with self.assertRaises(ValueError):
  41. cipher.seal(b'8' * 17)
  42. def test_neg2(self):
  43. cipher = AES.new(b'-' * 16, AES.MODE_KW)
  44. ct = bytearray(cipher.seal(b'7' * 16))
  45. cipher = AES.new(b'-' * 16, AES.MODE_KW)
  46. cipher.unseal(ct)
  47. cipher = AES.new(b'-' * 16, AES.MODE_KW)
  48. ct[0] ^= 0xFF
  49. with self.assertRaises(ValueError):
  50. cipher.unseal(ct)
  51. class KW_Wycheproof(unittest.TestCase):
  52. def setUp(self):
  53. self.vectors = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
  54. "kw_test.json",
  55. "Wycheproof tests for KW")
  56. def test_wycheproof(self):
  57. if not self.vectors:
  58. self.skipTest("No test vectors available")
  59. for vector in self.vectors:
  60. with self.subTest(testId=vector.id):
  61. cipher = AES.new(vector.key, AES.MODE_KW)
  62. try:
  63. cipher.seal(vector.msg)
  64. except ValueError:
  65. if vector.valid:
  66. raise
  67. continue
  68. cipher = AES.new(vector.key, AES.MODE_KW)
  69. try:
  70. pt = cipher.unseal(vector.ct)
  71. except ValueError:
  72. if vector.valid:
  73. raise
  74. continue
  75. self.assertEqual(pt, vector.msg)
  76. class KWP_Tests(unittest.TestCase):
  77. tvs = [
  78. ("5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8",
  79. "c37b7e6492584340bed12207808941155068f738",
  80. "138bdeaa9b8fa7fc61f97742e72248ee5ae6ae5360d1ae6a5f54f373fa543b6a"),
  81. ("5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8",
  82. "466f7250617369",
  83. "afbeb0f07dfbf5419200f2ccb50bb24f"),
  84. ]
  85. def test_rfc5649(self):
  86. for tv in self.tvs:
  87. kek, pt, ct = [bytes.fromhex(x) for x in tv]
  88. cipher = AES.new(kek, AES.MODE_KWP)
  89. ct2 = cipher.seal(pt)
  90. self.assertEqual(ct, ct2)
  91. cipher = AES.new(kek, AES.MODE_KWP)
  92. pt2 = cipher.unseal(ct)
  93. self.assertEqual(pt, pt2)
  94. class KWP_Wycheproof(unittest.TestCase):
  95. def setUp(self):
  96. self.vectors = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
  97. "kwp_test.json",
  98. "Wycheproof tests for KWP")
  99. def test_wycheproof(self):
  100. if not self.vectors:
  101. self.skipTest("No test vectors available")
  102. for vector in self.vectors:
  103. with self.subTest(testId=vector.id):
  104. cipher = AES.new(vector.key, AES.MODE_KWP)
  105. try:
  106. cipher.seal(vector.msg)
  107. except ValueError:
  108. if vector.valid and not vector.warning:
  109. raise
  110. continue
  111. cipher = AES.new(vector.key, AES.MODE_KWP)
  112. try:
  113. pt = cipher.unseal(vector.ct)
  114. except ValueError:
  115. if vector.valid and not vector.warning:
  116. raise
  117. continue
  118. self.assertEqual(pt, vector.msg)
  119. def get_tests(config={}):
  120. tests = []
  121. tests += list_test_cases(KW_Tests)
  122. tests += list_test_cases(KWP_Tests)
  123. tests += list_test_cases(KW_Wycheproof)
  124. tests += list_test_cases(KWP_Wycheproof)
  125. return tests
  126. if __name__ == '__main__':
  127. def suite():
  128. return unittest.TestSuite(get_tests())
  129. unittest.main(defaultTest='suite')