test_Padding.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #
  2. # SelfTest/Util/test_Padding.py: Self-test for padding functions
  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 as uh
  35. from Cryptodome.Util.py3compat import *
  36. from Cryptodome.SelfTest.st_common import list_test_cases
  37. from Cryptodome.Util.Padding import pad, unpad
  38. class PKCS7_Tests(unittest.TestCase):
  39. def test1(self):
  40. padded = pad(b(""), 4)
  41. self.failUnless(padded == uh(b("04040404")))
  42. padded = pad(b(""), 4, 'pkcs7')
  43. self.failUnless(padded == uh(b("04040404")))
  44. back = unpad(padded, 4)
  45. self.failUnless(back == b(""))
  46. def test2(self):
  47. padded = pad(uh(b("12345678")), 4)
  48. self.failUnless(padded == uh(b("1234567804040404")))
  49. back = unpad(padded, 4)
  50. self.failUnless(back == uh(b("12345678")))
  51. def test3(self):
  52. padded = pad(uh(b("123456")), 4)
  53. self.failUnless(padded == uh(b("12345601")))
  54. back = unpad(padded, 4)
  55. self.failUnless(back == uh(b("123456")))
  56. def test4(self):
  57. padded = pad(uh(b("1234567890")), 4)
  58. self.failUnless(padded == uh(b("1234567890030303")))
  59. back = unpad(padded, 4)
  60. self.failUnless(back == uh(b("1234567890")))
  61. def testn1(self):
  62. self.assertRaises(ValueError, pad, uh(b("12")), 4, 'pkcs8')
  63. def testn2(self):
  64. self.assertRaises(ValueError, unpad, b("\0\0\0"), 4)
  65. self.assertRaises(ValueError, unpad, b(""), 4)
  66. def testn3(self):
  67. self.assertRaises(ValueError, unpad, b("123456\x02"), 4)
  68. self.assertRaises(ValueError, unpad, b("123456\x00"), 4)
  69. self.assertRaises(ValueError, unpad, b("123456\x05\x05\x05\x05\x05"), 4)
  70. class X923_Tests(unittest.TestCase):
  71. def test1(self):
  72. padded = pad(b(""), 4, 'x923')
  73. self.failUnless(padded == uh(b("00000004")))
  74. back = unpad(padded, 4, 'x923')
  75. self.failUnless(back == b(""))
  76. def test2(self):
  77. padded = pad(uh(b("12345678")), 4, 'x923')
  78. self.failUnless(padded == uh(b("1234567800000004")))
  79. back = unpad(padded, 4, 'x923')
  80. self.failUnless(back == uh(b("12345678")))
  81. def test3(self):
  82. padded = pad(uh(b("123456")), 4, 'x923')
  83. self.failUnless(padded == uh(b("12345601")))
  84. back = unpad(padded, 4, 'x923')
  85. self.failUnless(back == uh(b("123456")))
  86. def test4(self):
  87. padded = pad(uh(b("1234567890")), 4, 'x923')
  88. self.failUnless(padded == uh(b("1234567890000003")))
  89. back = unpad(padded, 4, 'x923')
  90. self.failUnless(back == uh(b("1234567890")))
  91. def testn1(self):
  92. self.assertRaises(ValueError, unpad, b("123456\x02"), 4, 'x923')
  93. self.assertRaises(ValueError, unpad, b("123456\x00"), 4, 'x923')
  94. self.assertRaises(ValueError, unpad, b("123456\x00\x00\x00\x00\x05"), 4, 'x923')
  95. self.assertRaises(ValueError, unpad, b(""), 4, 'x923')
  96. class ISO7816_Tests(unittest.TestCase):
  97. def test1(self):
  98. padded = pad(b(""), 4, 'iso7816')
  99. self.failUnless(padded == uh(b("80000000")))
  100. back = unpad(padded, 4, 'iso7816')
  101. self.failUnless(back == b(""))
  102. def test2(self):
  103. padded = pad(uh(b("12345678")), 4, 'iso7816')
  104. self.failUnless(padded == uh(b("1234567880000000")))
  105. back = unpad(padded, 4, 'iso7816')
  106. self.failUnless(back == uh(b("12345678")))
  107. def test3(self):
  108. padded = pad(uh(b("123456")), 4, 'iso7816')
  109. self.failUnless(padded == uh(b("12345680")))
  110. #import pdb; pdb.set_trace()
  111. back = unpad(padded, 4, 'iso7816')
  112. self.failUnless(back == uh(b("123456")))
  113. def test4(self):
  114. padded = pad(uh(b("1234567890")), 4, 'iso7816')
  115. self.failUnless(padded == uh(b("1234567890800000")))
  116. back = unpad(padded, 4, 'iso7816')
  117. self.failUnless(back == uh(b("1234567890")))
  118. def testn1(self):
  119. self.assertRaises(ValueError, unpad, b("123456\x81"), 4, 'iso7816')
  120. self.assertRaises(ValueError, unpad, b(""), 4, 'iso7816')
  121. def get_tests(config={}):
  122. tests = []
  123. tests += list_test_cases(PKCS7_Tests)
  124. tests += list_test_cases(X923_Tests)
  125. tests += list_test_cases(ISO7816_Tests)
  126. return tests
  127. if __name__ == '__main__':
  128. suite = lambda: unittest.TestSuite(get_tests())
  129. unittest.main(defaultTest='suite')