test_strxor.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #
  2. # SelfTest/Util/test_strxor.py: Self-test for XORing
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. import unittest
  34. from binascii import unhexlify, hexlify
  35. from Crypto.Util.py3compat import *
  36. from Crypto.SelfTest.st_common import list_test_cases
  37. from Crypto.Util.strxor import strxor, strxor_c
  38. class StrxorTests(unittest.TestCase):
  39. def test1(self):
  40. term1 = unhexlify(b("ff339a83e5cd4cdf5649"))
  41. term2 = unhexlify(b("383d4ba020573314395b"))
  42. result = unhexlify(b("c70ed123c59a7fcb6f12"))
  43. self.assertEqual(strxor(term1, term2), result)
  44. self.assertEqual(strxor(term2, term1), result)
  45. def test2(self):
  46. es = b("")
  47. self.assertEqual(strxor(es, es), es)
  48. def test3(self):
  49. term1 = unhexlify(b("ff339a83e5cd4cdf5649"))
  50. all_zeros = bchr(0) * len(term1)
  51. self.assertEqual(strxor(term1, term1), all_zeros)
  52. def test_wrong_length(self):
  53. term1 = unhexlify(b("ff339a83e5cd4cdf5649"))
  54. term2 = unhexlify(b("ff339a83e5cd4cdf564990"))
  55. self.assertRaises(ValueError, strxor, term1, term2)
  56. class Strxor_cTests(unittest.TestCase):
  57. def test1(self):
  58. term1 = unhexlify(b("ff339a83e5cd4cdf5649"))
  59. result = unhexlify(b("be72dbc2a48c0d9e1708"))
  60. self.assertEqual(strxor_c(term1, 65), result)
  61. def test2(self):
  62. term1 = unhexlify(b("ff339a83e5cd4cdf5649"))
  63. self.assertEqual(strxor_c(term1, 0), term1)
  64. def test3(self):
  65. self.assertEqual(strxor_c(b(""), 90), b(""))
  66. def test_wrong_range(self):
  67. term1 = unhexlify(b("ff339a83e5cd4cdf5649"))
  68. self.assertRaises(ValueError, strxor_c, term1, -1)
  69. self.assertRaises(ValueError, strxor_c, term1, 256)
  70. def get_tests(config={}):
  71. tests = []
  72. tests += list_test_cases(StrxorTests)
  73. tests += list_test_cases(Strxor_cTests)
  74. return tests
  75. if __name__ == '__main__':
  76. suite = lambda: unittest.TestSuite(get_tests())
  77. unittest.main(defaultTest='suite')