Skip to content

Commit 93a3c0a

Browse files
committed
JOSE ECDSA signature encoding is raw encoding, not ASN1
1 parent e786a0e commit 93a3c0a

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

jose/backends/cryptography_backend.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,20 @@ def _sig_component_length(self):
106106
"""
107107
return math.ceil(self.prepared_key.key_size / 8.0)
108108

109-
def _der_to_asn1(self, der_signature):
110-
"""Convert signature from DER encoding to ASN1 encoding."""
109+
def _der_to_raw(self, der_signature):
110+
"""Convert signature from DER encoding to RAW encoding."""
111111
r, s = decode_dss_signature(der_signature)
112112
component_length = self._sig_component_length()
113113
return int_to_bytes(r, component_length) + int_to_bytes(s, component_length)
114114

115-
def _asn1_to_der(self, asn1_signature):
116-
"""Convert signature from ASN1 encoding to DER encoding."""
115+
def _raw_to_der(self, raw_signature):
116+
"""Convert signature from RAW encoding to DER encoding."""
117117
component_length = self._sig_component_length()
118-
if len(asn1_signature) != int(2 * component_length):
118+
if len(raw_signature) != int(2 * component_length):
119119
raise ValueError("Invalid signature")
120120

121-
r_bytes = asn1_signature[:component_length]
122-
s_bytes = asn1_signature[component_length:]
121+
r_bytes = raw_signature[:component_length]
122+
s_bytes = raw_signature[component_length:]
123123
r = int_from_bytes(r_bytes, "big")
124124
s = int_from_bytes(s_bytes, "big")
125125
return encode_dss_signature(r, s)
@@ -130,11 +130,11 @@ def sign(self, msg):
130130
"for your digest (%d)" % (self.prepared_key.curve.name,
131131
8 * self.hash_alg.digest_size))
132132
signature = self.prepared_key.sign(msg, ec.ECDSA(self.hash_alg()))
133-
return self._der_to_asn1(signature)
133+
return self._der_to_raw(signature)
134134

135135
def verify(self, msg, sig):
136136
try:
137-
signature = self._asn1_to_der(sig)
137+
signature = self._raw_to_der(sig)
138138
self.prepared_key.verify(signature, msg, ec.ECDSA(self.hash_alg()))
139139
return True
140140
except Exception:

tests/algorithms/test_EC.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
b"\x9d\xdf \xd1\xbc\xedK\x01\x87:}\xf2\x02!\x00\xb2shTA\x00\x1a\x13~\xba"
4040
b"J\xdb\xeem\x12\x1e\xfeMO\x04\xb2[\x86A\xbd\xc6hu\x953X\x1e"
4141
)
42-
ASN1_SIGNATURE = (
42+
RAW_SIGNATURE = (
4343
b"\x89yG\x81W\x01\x11\x9b0\x08\xa4\xd0\xe3g([\x07\xb5\x01\xb3\x9d\xdf "
4444
b"\xd1\xbc\xedK\x01\x87:}\xf2\xb2shTA\x00\x1a\x13~\xbaJ\xdb\xeem\x12\x1e"
4545
b"\xfeMO\x04\xb2[\x86A\xbd\xc6hu\x953X\x1e"
@@ -88,16 +88,16 @@ def test_cryptography_sig_component_length(algorithm, expected_length):
8888

8989
@pytest.mark.cryptography
9090
@pytest.mark.skipif(CryptographyECKey is None, reason="pyca/cryptography backend not available")
91-
def test_cryptograhy_der_to_asn1():
91+
def test_cryptograhy_der_to_raw():
9292
key = CryptographyECKey(private_key, ALGORITHMS.ES256)
93-
assert key._der_to_asn1(DER_SIGNATURE) == ASN1_SIGNATURE
93+
assert key._der_to_raw(DER_SIGNATURE) == RAW_SIGNATURE
9494

9595

9696
@pytest.mark.cryptography
9797
@pytest.mark.skipif(CryptographyECKey is None, reason="pyca/cryptography backend not available")
98-
def test_cryptograhy_asn1_to_der():
98+
def test_cryptograhy_raw_to_der():
9999
key = CryptographyECKey(private_key, ALGORITHMS.ES256)
100-
assert key._asn1_to_der(ASN1_SIGNATURE) == DER_SIGNATURE
100+
assert key._raw_to_der(RAW_SIGNATURE) == DER_SIGNATURE
101101

102102

103103
class TestECAlgorithm:

0 commit comments

Comments
 (0)