Skip to content

Commit e500b36

Browse files
committed
Bli explicit about salt length when doing PSS padding.
1 parent 2b735fe commit e500b36

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/cryptojwt/jws/pss.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ class PSSSigner(Signer):
1414
def __init__(self, algorithm="SHA256"):
1515
if algorithm == "SHA256":
1616
self.hash_algorithm = hashes.SHA256
17+
self.salt_length = 32
1718
elif algorithm == "SHA384":
1819
self.hash_algorithm = hashes.SHA384
20+
self.salt_length = 48
1921
elif algorithm == "SHA512":
2022
self.hash_algorithm = hashes.SHA512
23+
self.salt_length = 64
2124
else:
2225
raise Unsupported(f"algorithm: {algorithm}")
2326

@@ -36,7 +39,7 @@ def sign(self, msg, key):
3639
digest,
3740
padding.PSS(
3841
mgf=padding.MGF1(self.hash_algorithm()),
39-
salt_length=padding.PSS.MAX_LENGTH,
42+
salt_length=self.salt_length,
4043
),
4144
utils.Prehashed(self.hash_algorithm()),
4245
)
@@ -48,7 +51,7 @@ def verify(self, msg, signature, key):
4851
4952
:param msg: The message
5053
:param sig: A signature
51-
:param key: A ec.EllipticCurvePublicKey to use for the verification.
54+
:param key: A rsa._RSAPublicKey to use for the verification.
5255
:raises: BadSignature if the signature can't be verified.
5356
:return: True
5457
"""
@@ -58,7 +61,7 @@ def verify(self, msg, signature, key):
5861
msg,
5962
padding.PSS(
6063
mgf=padding.MGF1(self.hash_algorithm()),
61-
salt_length=padding.PSS.MAX_LENGTH,
64+
salt_length=self.salt_length,
6265
),
6366
self.hash_algorithm(),
6467
)

tests/test_21_pss.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
3+
import pytest
4+
5+
from cryptojwt.jwk.jwk import key_from_jwk_dict
6+
from cryptojwt.jws.jws import JWS
7+
import test_vector
8+
9+
10+
@pytest.mark.parametrize(
11+
"alg",
12+
["RS256", "RS384", "RS512", "PS256", "PS384", "PS512"]
13+
)
14+
def test_jws_rsa_signer_and_verifier(alg):
15+
_jwk_dict = json.loads(test_vector.json_rsa_priv_key)
16+
_key = key_from_jwk_dict(_jwk_dict)
17+
_key.alg = alg
18+
_key.add_kid()
19+
20+
json_header_rsa = json.loads(test_vector.test_header_rsa)
21+
json_header_rsa["alg"] = alg
22+
23+
# Sign
24+
jws = JWS(msg=test_vector.test_payload, **json_header_rsa)
25+
signed_token = jws.sign_compact([_key])
26+
27+
# Verify
28+
verifier = JWS(alg=[alg])
29+
assert verifier.verify_compact(signed_token, [_key])

0 commit comments

Comments
 (0)