Skip to content

Commit

Permalink
Merge PR #202.
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Aug 22, 2023
2 parents aa3c5cd + a40850a commit 8c9f3f0
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion fido2/cose.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def parse(cose: Mapping[int, Any]) -> CoseKey:
@staticmethod
def supported_algorithms() -> Sequence[int]:
"""Get a list of all supported algorithm identifiers"""
algs: Sequence[Type[CoseKey]] = [ES256, EdDSA, ES384, ES512, PS256, RS256]
algs: Sequence[Type[CoseKey]] = [ES256, EdDSA, ES384, ES512, PS256, RS256, ES256K]
return [cls.ALGORITHM for cls in algs]


Expand Down Expand Up @@ -271,3 +271,30 @@ def verify(self, message, signature):
def from_cryptography_key(cls, public_key):
pn = public_key.public_numbers()
return cls({1: 3, 3: cls.ALGORITHM, -1: int2bytes(pn.n), -2: int2bytes(pn.e)})


class ES256K(CoseKey):
ALGORITHM = -47
_HASH_ALG = hashes.SHA256()

def verify(self, message, signature):
if self[-1] != 8:
raise ValueError("Unsupported elliptic curve")
ec.EllipticCurvePublicNumbers(
bytes2int(self[-2]), bytes2int(self[-3]), ec.SECP256K1()
).public_key(default_backend()).verify(
signature, message, ec.ECDSA(self._HASH_ALG)
)

@classmethod
def from_cryptography_key(cls, public_key):
pn = public_key.public_numbers()
return cls(
{
1: 2,
3: cls.ALGORITHM,
-1: 8,
-2: int2bytes(pn.x, 32),
-3: int2bytes(pn.y, 32),
}
)

0 comments on commit 8c9f3f0

Please sign in to comment.