in ring/src/rsa/convert_nist_rsa_test_vectors.py [0:0]
def main(fn, test_type, padding_alg):
input_file_digest = hashlib.sha384(open(fn, 'rb').read()).hexdigest()
# File header
print("# RSA %(padding_alg)s Test Vectors for FIPS 186-4 from %(fn)s in" % \
{ "fn": fn, "padding_alg": padding_alg })
print("# http://csrc.nist.gov/groups/STM/cavp/documents/dss/186-3rsatestvectors.zip")
print("# accessible from")
print("# http://csrc.nist.gov/groups/STM/cavp/digital-signatures.html#test-vectors")
print("# with SHA-384 digest %s" % (input_file_digest))
print("# filtered and reformatted using %s." % __file__)
print("#")
print("# Digest = SHAAlg.")
if test_type == "verify":
print("# Key is (n, e) encoded in an ASN.1 (DER) sequence.")
elif test_type == "sign":
print("# Key is an ASN.1 (DER) RSAPrivateKey.")
else:
print("Invalid test_type: %s" % test_type)
quit()
print("# Sig = S.")
print()
num_cases = 0
# Each test type has a different field as the last entry per case
# For verify tests,PKCS "Result" is always the last field.
# Otherwise, for signing tests, it is dependent on the padding used.
if test_type == "verify":
last_field = "Result"
else:
if padding_alg == "PSS":
last_field = "SaltVal"
else:
last_field = "S"
for case in parse(fn, last_field):
if case['SHAAlg'] == 'SHA224':
# SHA224 not supported in *ring*.
debug("Skipping due to use of SHA224", DEBUG)
continue
if padding_alg == "PSS":
if case['SHAAlg'] == 'SHA1':
# SHA-1 with PSS not supported in *ring*.
debug("Skipping due to use of SHA1 and PSS.", DEBUG)
continue
# *ring* only supports PSS where the salt length is equal to the
# output length of the hash algorithm.
if len(case['SaltVal']) * 2 != DIGEST_OUTPUT_LENGTHS[case['SHAAlg']]:
debug("Skipping due to unsupported salt length.", DEBUG)
continue
# Read private key components.
n = int(case['n'], 16)
e = int(case['e'], 16)
d = int(case['d'], 16)
if test_type == 'sign':
if n.bit_length() // 8 < 2048 // 8:
debug("Skipping due to modulus length (too small).", DEBUG)
continue
if n.bit_length() > 4096:
debug("Skipping due to modulus length (too large).", DEBUG)
continue
print_sign_test(case, n, e, d, padding_alg)
else:
legacy = case['SHAAlg'] in ["SHA1", "SHA256", "SHA512"]
if (n.bit_length() // 8 < 2048 // 8 and not legacy) or n.bit_length() // 8 < 1024 // 8:
debug("Skipping due to modulus length (too small).", DEBUG)
continue
print_verify_test(case, n, e)
num_cases += 1
debug("%d test cases output." % num_cases, True)