Spaces:
Running
Running
File size: 13,541 Bytes
ba2f5d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# ===================================================================
#
# Copyright (c) 2014, Legrandin <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ===================================================================
import json
import unittest
from binascii import unhexlify
from Crypto.Util.py3compat import bchr
from Crypto.Util.number import bytes_to_long
from Crypto.Util.strxor import strxor
from Crypto.SelfTest.st_common import list_test_cases
from Crypto.SelfTest.loader import load_test_vectors, load_test_vectors_wycheproof
from Crypto.Hash import (SHA1, SHA224, SHA256, SHA384, SHA512, SHA3_384,
SHA3_224, SHA3_256, SHA3_512)
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Signature import PKCS1_v1_5
from Crypto.Util._file_system import pycryptodome_filename
from Crypto.Util.strxor import strxor
def load_hash_by_name(hash_name):
return __import__("Crypto.Hash." + hash_name, globals(), locals(), ["new"])
class FIPS_PKCS1_Verify_Tests(unittest.TestCase):
def shortDescription(self):
return "FIPS PKCS1 Tests (Verify)"
def test_can_sign(self):
test_public_key = RSA.generate(1024).public_key()
verifier = pkcs1_15.new(test_public_key)
self.assertEqual(verifier.can_sign(), False)
class FIPS_PKCS1_Verify_Tests_KAT(unittest.TestCase):
pass
test_vectors_verify = load_test_vectors(("Signature", "PKCS1-v1.5"),
"SigVer15_186-3.rsp",
"Signature Verification 186-3",
{'shaalg': lambda x: x,
'd': lambda x: int(x),
'result': lambda x: x}) or []
for count, tv in enumerate(test_vectors_verify):
if isinstance(tv, str):
continue
if hasattr(tv, "n"):
modulus = tv.n
continue
hash_module = load_hash_by_name(tv.shaalg.upper())
hash_obj = hash_module.new(tv.msg)
public_key = RSA.construct([bytes_to_long(x) for x in (modulus, tv.e)]) # type: ignore
verifier = pkcs1_15.new(public_key)
def positive_test(self, hash_obj=hash_obj, verifier=verifier, signature=tv.s):
verifier.verify(hash_obj, signature)
def negative_test(self, hash_obj=hash_obj, verifier=verifier, signature=tv.s):
self.assertRaises(ValueError, verifier.verify, hash_obj, signature)
if tv.result == 'f':
setattr(FIPS_PKCS1_Verify_Tests_KAT, "test_negative_%d" % count, negative_test)
else:
setattr(FIPS_PKCS1_Verify_Tests_KAT, "test_positive_%d" % count, positive_test)
class FIPS_PKCS1_Sign_Tests(unittest.TestCase):
def shortDescription(self):
return "FIPS PKCS1 Tests (Sign)"
def test_can_sign(self):
test_private_key = RSA.generate(1024)
signer = pkcs1_15.new(test_private_key)
self.assertEqual(signer.can_sign(), True)
class FIPS_PKCS1_Sign_Tests_KAT(unittest.TestCase):
pass
test_vectors_sign = load_test_vectors(("Signature", "PKCS1-v1.5"),
"SigGen15_186-2.txt",
"Signature Generation 186-2",
{'shaalg': lambda x: x}) or []
test_vectors_sign += load_test_vectors(("Signature", "PKCS1-v1.5"),
"SigGen15_186-3.txt",
"Signature Generation 186-3",
{'shaalg': lambda x: x}) or []
for count, tv in enumerate(test_vectors_sign):
if isinstance(tv, str):
continue
if hasattr(tv, "n"):
modulus = tv.n
continue
if hasattr(tv, "e"):
private_key = RSA.construct([bytes_to_long(x) for x in (modulus, tv.e, tv.d)]) # type: ignore
signer = pkcs1_15.new(private_key)
continue
hash_module = load_hash_by_name(tv.shaalg.upper())
hash_obj = hash_module.new(tv.msg)
def new_test(self, hash_obj=hash_obj, signer=signer, result=tv.s):
signature = signer.sign(hash_obj)
self.assertEqual(signature, result)
setattr(FIPS_PKCS1_Sign_Tests_KAT, "test_%d" % count, new_test)
class PKCS1_15_NoParams(unittest.TestCase):
"""Verify that PKCS#1 v1.5 signatures pass even without NULL parameters in
the algorithm identifier (PyCrypto/LP bug #1119552)."""
rsakey = """-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAL8eJ5AKoIsjURpcEoGubZMxLD7+kT+TLr7UkvEtFrRhDDKMtuII
q19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQJACUSDEp8RTe32ftq8IwG8
Wojl5mAd1wFiIOrZ/Uv8b963WJOJiuQcVN29vxU5+My9GPZ7RA3hrDBEAoHUDPrI
OQIhAPIPLz4dphiD9imAkivY31Rc5AfHJiQRA7XixTcjEkojAiEAyh/pJHks/Mlr
+rdPNEpotBjfV4M4BkgGAA/ipcmaAjcCIQCHvhwwKVBLzzTscT2HeUdEeBMoiXXK
JACAr3sJQJGxIQIgarRp+m1WSKV1MciwMaTOnbU7wxFs9DP1pva76lYBzgUCIQC9
n0CnZCJ6IZYqSt0H5N7+Q+2Ro64nuwV/OSQfM6sBwQ==
-----END RSA PRIVATE KEY-----"""
msg = b"This is a test\x0a"
# PKCS1 v1.5 signature of the message computed using SHA-1.
# The digestAlgorithm SEQUENCE does NOT contain the NULL parameter.
sig_str = "a287a13517f716e72fb14eea8e33a8db4a4643314607e7ca3e3e28"\
"1893db74013dda8b855fd99f6fecedcb25fcb7a434f35cd0a101f8"\
"b19348e0bd7b6f152dfc"
signature = unhexlify(sig_str)
def runTest(self):
verifier = pkcs1_15.new(RSA.importKey(self.rsakey))
hashed = SHA1.new(self.msg)
verifier.verify(hashed, self.signature)
class PKCS1_Legacy_Module_Tests(unittest.TestCase):
"""Verify that the legacy module Crypto.Signature.PKCS1_v1_5
behaves as expected. The only difference is that the verify()
method returns True/False and does not raise exceptions."""
def shortDescription(self):
return "Test legacy Crypto.Signature.PKCS1_v1_5"
def runTest(self):
key = RSA.importKey(PKCS1_15_NoParams.rsakey)
hashed = SHA1.new(b"Test")
good_signature = PKCS1_v1_5.new(key).sign(hashed)
verifier = PKCS1_v1_5.new(key.public_key())
self.assertEqual(verifier.verify(hashed, good_signature), True)
# Flip a few bits in the signature
bad_signature = strxor(good_signature, bchr(1) * len(good_signature))
self.assertEqual(verifier.verify(hashed, bad_signature), False)
class PKCS1_All_Hashes_Tests(unittest.TestCase):
def shortDescription(self):
return "Test PKCS#1v1.5 signature in combination with all hashes"
def runTest(self):
key = RSA.generate(1024)
signer = pkcs1_15.new(key)
hash_names = ("MD2", "MD4", "MD5", "RIPEMD160", "SHA1",
"SHA224", "SHA256", "SHA384", "SHA512",
"SHA3_224", "SHA3_256", "SHA3_384", "SHA3_512")
for name in hash_names:
hashed = load_hash_by_name(name).new(b"Test")
signer.sign(hashed)
from Crypto.Hash import BLAKE2b, BLAKE2s
for hash_size in (20, 32, 48, 64):
hashed_b = BLAKE2b.new(digest_bytes=hash_size, data=b"Test")
signer.sign(hashed_b)
for hash_size in (16, 20, 28, 32):
hashed_s = BLAKE2s.new(digest_bytes=hash_size, data=b"Test")
signer.sign(hashed_s)
class TestVectorsWycheproof(unittest.TestCase):
def __init__(self, wycheproof_warnings):
unittest.TestCase.__init__(self)
self._wycheproof_warnings = wycheproof_warnings
self._id = "None"
def setUp(self):
self.tv = []
self.add_tests("rsa_sig_gen_misc_test.json")
self.add_tests("rsa_signature_2048_sha224_test.json")
self.add_tests("rsa_signature_2048_sha256_test.json")
self.add_tests("rsa_signature_2048_sha384_test.json")
self.add_tests("rsa_signature_2048_sha3_224_test.json")
self.add_tests("rsa_signature_2048_sha3_256_test.json")
self.add_tests("rsa_signature_2048_sha3_384_test.json")
self.add_tests("rsa_signature_2048_sha3_512_test.json")
self.add_tests("rsa_signature_2048_sha512_test.json")
self.add_tests("rsa_signature_2048_sha512_224_test.json")
self.add_tests("rsa_signature_2048_sha512_256_test.json")
self.add_tests("rsa_signature_3072_sha256_test.json")
self.add_tests("rsa_signature_3072_sha384_test.json")
self.add_tests("rsa_signature_3072_sha3_256_test.json")
self.add_tests("rsa_signature_3072_sha3_384_test.json")
self.add_tests("rsa_signature_3072_sha3_512_test.json")
self.add_tests("rsa_signature_3072_sha512_test.json")
self.add_tests("rsa_signature_3072_sha512_256_test.json")
self.add_tests("rsa_signature_4096_sha384_test.json")
self.add_tests("rsa_signature_4096_sha512_test.json")
self.add_tests("rsa_signature_4096_sha512_256_test.json")
self.add_tests("rsa_signature_test.json")
def add_tests(self, filename):
def filter_rsa(group):
return RSA.import_key(group['keyPem'])
def filter_sha(group):
hash_name = group['sha']
if hash_name == "SHA-512":
return SHA512
elif hash_name == "SHA-512/224":
return SHA512.new(truncate="224")
elif hash_name == "SHA-512/256":
return SHA512.new(truncate="256")
elif hash_name == "SHA3-512":
return SHA3_512
elif hash_name == "SHA-384":
return SHA384
elif hash_name == "SHA3-384":
return SHA3_384
elif hash_name == "SHA-256":
return SHA256
elif hash_name == "SHA3-256":
return SHA3_256
elif hash_name == "SHA-224":
return SHA224
elif hash_name == "SHA3-224":
return SHA3_224
elif hash_name == "SHA-1":
return SHA1
else:
raise ValueError("Unknown hash algorithm: " + hash_name)
def filter_type(group):
type_name = group['type']
if type_name not in ("RsassaPkcs1Verify", "RsassaPkcs1Generate"):
raise ValueError("Unknown type name " + type_name)
result = load_test_vectors_wycheproof(("Signature", "wycheproof"),
filename,
"Wycheproof PKCS#1v1.5 signature (%s)" % filename,
group_tag={'rsa_key': filter_rsa,
'hash_mod': filter_sha,
'type': filter_type})
return result
def shortDescription(self):
return self._id
def warn(self, tv):
if tv.warning and self._wycheproof_warnings:
import warnings
warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment))
def test_verify(self, tv):
self._id = "Wycheproof RSA PKCS$#1 Test #" + str(tv.id)
hashed_msg = tv.hash_module.new(tv.msg)
signer = pkcs1_15.new(tv.key)
try:
signature = signer.verify(hashed_msg, tv.sig)
except ValueError as e:
if tv.warning:
return
assert not tv.valid
else:
assert tv.valid
self.warn(tv)
def runTest(self):
for tv in self.tv:
self.test_verify(tv)
def get_tests(config={}):
wycheproof_warnings = config.get('wycheproof_warnings')
tests = []
tests += list_test_cases(FIPS_PKCS1_Verify_Tests)
tests += list_test_cases(FIPS_PKCS1_Sign_Tests)
tests += list_test_cases(PKCS1_15_NoParams)
tests += list_test_cases(PKCS1_Legacy_Module_Tests)
tests += list_test_cases(PKCS1_All_Hashes_Tests)
tests += [ TestVectorsWycheproof(wycheproof_warnings) ]
if config.get('slow_tests'):
tests += list_test_cases(FIPS_PKCS1_Verify_Tests_KAT)
tests += list_test_cases(FIPS_PKCS1_Sign_Tests_KAT)
return tests
if __name__ == '__main__':
suite = lambda: unittest.TestSuite(get_tests())
unittest.main(defaultTest='suite')
|