Skip to content

enable ES256K for ECDSA signing scheme #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion jose/backends/cryptography_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(self, key, algorithm, cryptography_backend=default_backend):
self.hash_alg = {
ALGORITHMS.ES256: self.SHA256,
ALGORITHMS.ES384: self.SHA384,
ALGORITHMS.ES512: self.SHA512
ALGORITHMS.ES512: self.SHA512,
ALGORITHMS.ES256K: self.SHA256
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I'm a huge fan of trailing commas in lists to minimize the size of diffs (and also makes git blames a lot more easier). Please add a trailing comma to the ALGORITHMS.E256K line.

}.get(algorithm)
self._algorithm = algorithm

Expand Down Expand Up @@ -87,6 +88,7 @@ def _process_jwk(self, jwk_dict):
'P-256': ec.SECP256R1,
'P-384': ec.SECP384R1,
'P-521': ec.SECP521R1,
'P-256K': ec.SECP256K1,
}[jwk_dict['crv']]

public = ec.EllipticCurvePublicNumbers(x, y, curve())
Expand Down Expand Up @@ -172,6 +174,7 @@ def to_dict(self):
'secp256r1': 'P-256',
'secp384r1': 'P-384',
'secp521r1': 'P-521',
'secp256k1': 'P-256K',
}[self.prepared_key.curve.name]

# Calculate the key size in bytes. Section 6.2.1.2 and 6.2.1.3 of
Expand Down
5 changes: 4 additions & 1 deletion jose/backends/ecdsa_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ECDSAECKey(Key):
SHA256: ecdsa.curves.NIST256p,
SHA384: ecdsa.curves.NIST384p,
SHA512: ecdsa.curves.NIST521p,
SHA256: ecdsa.curves.SECP256k1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key in line 29 is same as key in line 26, that's a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pohutukawa Please fix.

}

def __init__(self, key, algorithm):
Expand All @@ -35,7 +36,8 @@ def __init__(self, key, algorithm):
self.hash_alg = {
ALGORITHMS.ES256: self.SHA256,
ALGORITHMS.ES384: self.SHA384,
ALGORITHMS.ES512: self.SHA512
ALGORITHMS.ES512: self.SHA512,
ALGORITHMS.ES256K: self.SHA256
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing comma here as well.

}.get(algorithm)
self._algorithm = algorithm

Expand Down Expand Up @@ -120,6 +122,7 @@ def to_dict(self):
ecdsa.curves.NIST256p: 'P-256',
ecdsa.curves.NIST384p: 'P-384',
ecdsa.curves.NIST521p: 'P-521',
ecdsa.curves.SECP256k1: 'P-256K',
}[self.prepared_key.curve]

# Calculate the key size in bytes. Section 6.2.1.2 and 6.2.1.3 of
Expand Down
4 changes: 3 additions & 1 deletion jose/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class Algorithms(object):
ES256 = 'ES256'
ES384 = 'ES384'
ES512 = 'ES512'
ES256K = 'ES256K'

HMAC = {HS256, HS384, HS512}
RSA = {RS256, RS384, RS512}
EC = {ES256, ES384, ES512}
EC = {ES256, ES384, ES512, ES256K}

SUPPORTED = HMAC.union(RSA).union(EC)

Expand All @@ -31,6 +32,7 @@ class Algorithms(object):
ES256: hashlib.sha256,
ES384: hashlib.sha384,
ES512: hashlib.sha512,
ES256K: hashlib.sha256,
}

KEYS = {}
Expand Down
1 change: 1 addition & 0 deletions jose/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def get_algorithm_object(algorithm):
ALGORITHMS.ES256: 'SHA256',
ALGORITHMS.ES384: 'SHA384',
ALGORITHMS.ES512: 'SHA512',
ALGORITHMS.ES256K: 'SHA256',
}
key = get_key(algorithm)
attr = algorithms.get(algorithm, None)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ def test_EC512(self, payload):
token = jws.sign(payload, ec_private_key, algorithm=ALGORITHMS.ES512)
assert jws.verify(token, ec_public_key, ALGORITHMS.ES512) == payload

def test_EC256K(self, payload):
token = jws.sign(payload, ec_private_key, algorithm=ALGORITHMS.ES256K)
assert jws.verify(token, ec_public_key, ALGORITHMS.ES256K) == payload

def test_wrong_alg(self, payload):
token = jws.sign(payload, ec_private_key, algorithm=ALGORITHMS.ES256)
with pytest.raises(JWSError):
Expand Down