test_pss.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. import unittest
  31. from Cryptodome.Util.py3compat import b, bchr
  32. from Cryptodome.Util.number import bytes_to_long
  33. from Cryptodome.Util.strxor import strxor
  34. from Cryptodome.SelfTest.st_common import list_test_cases
  35. from Cryptodome.SelfTest.loader import load_tests
  36. from Cryptodome.Hash import SHA1
  37. from Cryptodome.PublicKey import RSA
  38. from Cryptodome.Signature import pss
  39. from Cryptodome.Signature import PKCS1_PSS
  40. def load_hash_by_name(hash_name):
  41. return __import__("Cryptodome.Hash." + hash_name, globals(), locals(), ["new"])
  42. class PRNG(object):
  43. def __init__(self, stream):
  44. self.stream = stream
  45. self.idx = 0
  46. def __call__(self, rnd_size):
  47. result = self.stream[self.idx:self.idx + rnd_size]
  48. self.idx += rnd_size
  49. return result
  50. class FIPS_PKCS1_Verify_Tests(unittest.TestCase):
  51. def shortDescription(self):
  52. return "FIPS PKCS1 Tests (Verify)"
  53. def verify_positive(self, hashmod, message, public_key, salt, signature):
  54. prng = PRNG(salt)
  55. hashed = hashmod.new(message)
  56. verifier = pss.new(public_key, salt_bytes=len(salt), rand_func=prng)
  57. verifier.verify(hashed, signature)
  58. def verify_negative(self, hashmod, message, public_key, salt, signature):
  59. prng = PRNG(salt)
  60. hashed = hashmod.new(message)
  61. verifier = pss.new(public_key, salt_bytes=len(salt), rand_func=prng)
  62. self.assertRaises(ValueError, verifier.verify, hashed, signature)
  63. def test_can_sign(self):
  64. test_public_key = RSA.generate(1024).publickey()
  65. verifier = pss.new(test_public_key)
  66. self.assertEqual(verifier.can_sign(), False)
  67. class FIPS_PKCS1_Verify_Tests_KAT(unittest.TestCase):
  68. pass
  69. test_vectors_verify = load_tests(("Cryptodome", "SelfTest", "Signature", "test_vectors", "PKCS1-PSS"),
  70. "SigVerPSS_186-3.rsp",
  71. "Signature Verification 186-3",
  72. { 'shaalg' : lambda x: x,
  73. 'result' : lambda x: x })
  74. for count, tv in enumerate(test_vectors_verify):
  75. if isinstance(tv, str):
  76. continue
  77. if hasattr(tv, "n"):
  78. modulus = tv.n
  79. continue
  80. if hasattr(tv, "p"):
  81. continue
  82. hash_module = load_hash_by_name(tv.shaalg.upper())
  83. hash_obj = hash_module.new(tv.msg)
  84. public_key = RSA.construct([bytes_to_long(x) for x in (modulus, tv.e)]) # type: ignore
  85. if tv.saltval != b("\x00"):
  86. prng = PRNG(tv.saltval)
  87. verifier = pss.new(public_key, salt_bytes=len(tv.saltval), rand_func=prng)
  88. else:
  89. verifier = pss.new(public_key, salt_bytes=0)
  90. def positive_test(self, hash_obj=hash_obj, verifier=verifier, signature=tv.s):
  91. verifier.verify(hash_obj, signature)
  92. def negative_test(self, hash_obj=hash_obj, verifier=verifier, signature=tv.s):
  93. self.assertRaises(ValueError, verifier.verify, hash_obj, signature)
  94. if tv.result == 'p':
  95. setattr(FIPS_PKCS1_Verify_Tests_KAT, "test_positive_%d" % count, positive_test)
  96. else:
  97. setattr(FIPS_PKCS1_Verify_Tests_KAT, "test_negative_%d" % count, negative_test)
  98. class FIPS_PKCS1_Sign_Tests(unittest.TestCase):
  99. def shortDescription(self):
  100. return "FIPS PKCS1 Tests (Sign)"
  101. def test_can_sign(self):
  102. test_private_key = RSA.generate(1024)
  103. signer = pss.new(test_private_key)
  104. self.assertEqual(signer.can_sign(), True)
  105. class FIPS_PKCS1_Sign_Tests_KAT(unittest.TestCase):
  106. pass
  107. test_vectors_sign = load_tests(("Cryptodome", "SelfTest", "Signature", "test_vectors", "PKCS1-PSS"),
  108. "SigGenPSS_186-2.txt",
  109. "Signature Generation 186-2",
  110. { 'shaalg' : lambda x: x })
  111. test_vectors_sign += load_tests(("Cryptodome", "SelfTest", "Signature", "test_vectors", "PKCS1-PSS"),
  112. "SigGenPSS_186-3.txt",
  113. "Signature Generation 186-3",
  114. { 'shaalg' : lambda x: x })
  115. for count, tv in enumerate(test_vectors_sign):
  116. if isinstance(tv, str):
  117. continue
  118. if hasattr(tv, "n"):
  119. modulus = tv.n
  120. continue
  121. if hasattr(tv, "e"):
  122. private_key = RSA.construct([bytes_to_long(x) for x in (modulus, tv.e, tv.d)]) # type: ignore
  123. continue
  124. hash_module = load_hash_by_name(tv.shaalg.upper())
  125. hash_obj = hash_module.new(tv.msg)
  126. if tv.saltval != b("\x00"):
  127. prng = PRNG(tv.saltval)
  128. signer = pss.new(private_key, salt_bytes=len(tv.saltval), rand_func=prng)
  129. else:
  130. signer = pss.new(private_key, salt_bytes=0)
  131. def new_test(self, hash_obj=hash_obj, signer=signer, result=tv.s):
  132. signature = signer.sign(hash_obj)
  133. self.assertEqual(signature, result)
  134. setattr(FIPS_PKCS1_Sign_Tests_KAT, "test_%d" % count, new_test)
  135. class PKCS1_Legacy_Module_Tests(unittest.TestCase):
  136. """Verify that the legacy module Cryptodome.Signature.PKCS1_PSS
  137. behaves as expected. The only difference is that the verify()
  138. method returns True/False and does not raise exceptions."""
  139. def shortDescription(self):
  140. return "Test legacy Cryptodome.Signature.PKCS1_PSS"
  141. def runTest(self):
  142. key = RSA.generate(1024)
  143. hashed = SHA1.new(b("Test"))
  144. good_signature = PKCS1_PSS.new(key).sign(hashed)
  145. verifier = PKCS1_PSS.new(key.publickey())
  146. self.assertEqual(verifier.verify(hashed, good_signature), True)
  147. # Flip a few bits in the signature
  148. bad_signature = strxor(good_signature, bchr(1) * len(good_signature))
  149. self.assertEqual(verifier.verify(hashed, bad_signature), False)
  150. class PKCS1_All_Hashes_Tests(unittest.TestCase):
  151. def shortDescription(self):
  152. return "Test PKCS#1 PSS signature in combination with all hashes"
  153. def runTest(self):
  154. key = RSA.generate(1280)
  155. signer = pss.new(key)
  156. hash_names = ("MD2", "MD4", "MD5", "RIPEMD160", "SHA1",
  157. "SHA224", "SHA256", "SHA384", "SHA512",
  158. "SHA3_224", "SHA3_256", "SHA3_384", "SHA3_512")
  159. for name in hash_names:
  160. hashed = load_hash_by_name(name).new(b("Test"))
  161. signer.sign(hashed)
  162. from Cryptodome.Hash import BLAKE2b, BLAKE2s
  163. for hash_size in (20, 32, 48, 64):
  164. hashed_b = BLAKE2b.new(digest_bytes=hash_size, data=b("Test"))
  165. signer.sign(hashed_b)
  166. for hash_size in (16, 20, 28, 32):
  167. hashed_s = BLAKE2s.new(digest_bytes=hash_size, data=b("Test"))
  168. signer.sign(hashed_s)
  169. def get_tests(config={}):
  170. tests = []
  171. tests += list_test_cases(FIPS_PKCS1_Verify_Tests)
  172. tests += list_test_cases(FIPS_PKCS1_Sign_Tests)
  173. tests += list_test_cases(PKCS1_Legacy_Module_Tests)
  174. tests += list_test_cases(PKCS1_All_Hashes_Tests)
  175. if config.get('slow_tests'):
  176. tests += list_test_cases(FIPS_PKCS1_Verify_Tests_KAT)
  177. tests += list_test_cases(FIPS_PKCS1_Sign_Tests_KAT)
  178. return tests
  179. if __name__ == '__main__':
  180. suite = lambda: unittest.TestSuite(get_tests())
  181. unittest.main(defaultTest='suite')