Skip to content

Commit

Permalink
simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
trishankatdatadog committed Sep 2, 2022
1 parent 3f3ed97 commit 2b94e8e
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions tests/test_rsa_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,38 +160,25 @@ def test_verify_rsa_signature(self):

def test_verify_rsa_pss_sha256(self):
rsa_scheme = 'rsassa-pss-sha256'
data = 'Some say the bigger the salt, the more provable the security'.encode('utf-8')
data = 'The ancients say the longer the salt, the more provable the security'.encode('utf-8')

private_key_object = load_pem_private_key(private_rsa.encode('utf-8'),
private_key = load_pem_private_key(private_rsa.encode('utf-8'),
password=None, backend=default_backend())
public_key_object = load_pem_public_key(public_rsa.encode('utf-8'),
backend=default_backend())
digest_obj = securesystemslib.hash.digest_from_rsa_scheme(rsa_scheme,
'pyca_crypto')
max_salt_length = padding.calculate_max_pss_salt_length(private_key_object, digest_obj)

# Check that every valid salt length value can be automatically verified.
for salt_length in range(max_salt_length+1):
signature = private_key_object.sign(
data, padding.PSS(mgf=padding.MGF1(digest_obj.algorithm),
salt_length=salt_length), digest_obj.algorithm)

# Current behaviour: automatic inferring of salt length.
self.assertTrue(securesystemslib.rsa_keys.verify_rsa_signature(signature,
rsa_scheme, public_rsa, data))

# Previous behaviour: generally wrong assumption about salt length.
if salt_length == digest_obj.digest_size:
self.assertIsNone(public_key_object.verify(signature, data,
padding.PSS(mgf=padding.MGF1(digest_obj.algorithm),
salt_length=padding.PSS.DIGEST_LENGTH),
digest_obj.algorithm))
else:
with self.assertRaises(InvalidSignature):
public_key_object.verify(signature, data,
padding.PSS(mgf=padding.MGF1(digest_obj.algorithm),
salt_length=padding.PSS.DIGEST_LENGTH),
digest_obj.algorithm)
digest = securesystemslib.hash.digest_from_rsa_scheme(rsa_scheme, 'pyca_crypto')

# Old-style signature: use the hash length as the salt length.
old_signature = private_key.sign(data,
padding.PSS(mgf=padding.MGF1(digest.algorithm), salt_length=padding.PSS.DIGEST_LENGTH),
digest.algorithm)

# New-style signature: use the automatic salt length.
new_signature, _ = securesystemslib.rsa_keys.create_rsa_signature(private_rsa, data)

# Verify both old-style and new-style signatures.
for signature in (old_signature, new_signature):
verified = securesystemslib.rsa_keys.verify_rsa_signature(signature, rsa_scheme,
public_rsa, data)
self.assertTrue(verified)


def test_create_rsa_encrypted_pem(self):
Expand Down

0 comments on commit 2b94e8e

Please sign in to comment.