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