-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement signature verification (#232)
- Loading branch information
Showing
12 changed files
with
239 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import typing as t | ||
|
||
from .p256 import P256 | ||
from .secp256k1 import Secp256k1 | ||
|
||
__all__ = ['P256', 'Secp256k1'] | ||
_ANY_ALG_TYPE = t.Union[t.Type[P256], t.Type[Secp256k1]] | ||
|
||
AVAILABLE_ALGORITHMS: t.List[_ANY_ALG_TYPE] = [P256, Secp256k1] | ||
ALGORITHM_TO_CLASS: t.Dict[str, _ANY_ALG_TYPE] = {alg.NAME: alg for alg in AVAILABLE_ALGORITHMS} | ||
|
||
AVAILABLE_ALGORITHMS = [P256, Secp256k1] | ||
__all__ = ['P256', 'Secp256k1', 'ALGORITHM_TO_CLASS'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
from cryptography.hazmat.primitives.asymmetric.ec import SECP256R1 | ||
|
||
from atproto_crypto.algs.base_alg import AlgBase | ||
from atproto_crypto.consts import P256_CURVE_ORDER, P256_JWT_ALG | ||
|
||
|
||
class P256(AlgBase): | ||
NAME = P256_JWT_ALG | ||
|
||
def __init__(self) -> None: | ||
super().__init__(SECP256R1()) | ||
super().__init__(SECP256R1(), P256_CURVE_ORDER) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 | ||
|
||
from atproto_crypto.algs.base_alg import AlgBase | ||
from atproto_crypto.consts import SECP256K1_CURVE_ORDER, SECP256K1_JWT_ALG | ||
|
||
|
||
class Secp256k1(AlgBase): | ||
NAME = SECP256K1_JWT_ALG | ||
|
||
def __init__(self) -> None: | ||
super().__init__(SECP256K1()) | ||
super().__init__(SECP256K1(), SECP256K1_CURVE_ORDER) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
import typing as t | ||
import warnings | ||
|
||
from atproto_crypto.algs import ALGORITHM_TO_CLASS | ||
from atproto_crypto.did import parse_did_key | ||
from atproto_crypto.exceptions import UnsupportedSignatureAlgorithmError | ||
|
||
|
||
def verify_signature(did_key: str, signing_input: t.Union[str, bytes], signature: t.Union[str, bytes]) -> bool: | ||
# TODO(MarshalX): implement | ||
warnings.warn( | ||
'verify_signature is not implemented yet. Do not trust to this signing_input', | ||
RuntimeWarning, | ||
stacklevel=0, | ||
) | ||
|
||
return True | ||
"""Verify signature. | ||
Args: | ||
did_key: DID key. | ||
signing_input: Signing input (data). | ||
signature: Signature. | ||
Returns: | ||
bool: True if signature is valid, False otherwise. | ||
""" | ||
parsed_did_key = parse_did_key(did_key) | ||
if parsed_did_key.jwt_alg not in ALGORITHM_TO_CLASS: | ||
raise UnsupportedSignatureAlgorithmError('Unsupported signature alg') | ||
|
||
algorithm_class = ALGORITHM_TO_CLASS[parsed_did_key.jwt_alg] | ||
return algorithm_class().verify_signature(parsed_did_key.key_bytes, signing_input, signature) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
[ | ||
{ | ||
"comment": "valid P-256 key and signature, with low-S signature", | ||
"messageBase64": "oWVoZWxsb2V3b3JsZA", | ||
"algorithm": "ES256", | ||
"didDocSuite": "EcdsaSecp256r1VerificationKey2019", | ||
"publicKeyDid": "did:key:zDnaembgSGUhZULN2Caob4HLJPaxBh92N7rtH21TErzqf8HQo", | ||
"publicKeyMultibase": "zxdM8dSstjrpZaRUwBmDvjGXweKuEMVN95A9oJBFjkWMh", | ||
"signatureBase64": "2vZNsG3UKvvO/CDlrdvyZRISOFylinBh0Jupc6KcWoJWExHptCfduPleDbG3rko3YZnn9Lw0IjpixVmexJDegg", | ||
"validSignature": true, | ||
"tags": [] | ||
}, | ||
{ | ||
"comment": "valid K-256 key and signature, with low-S signature", | ||
"messageBase64": "oWVoZWxsb2V3b3JsZA", | ||
"algorithm": "ES256K", | ||
"didDocSuite": "EcdsaSecp256k1VerificationKey2019", | ||
"publicKeyDid": "did:key:zQ3shqwJEJyMBsBXCWyCBpUBMqxcon9oHB7mCvx4sSpMdLJwc", | ||
"publicKeyMultibase": "z25z9DTpsiYYJKGsWmSPJK2NFN8PcJtZig12K59UgW7q5t", | ||
"signatureBase64": "5WpdIuEUUfVUYaozsi8G0B3cWO09cgZbIIwg1t2YKdUn/FEznOndsz/qgiYb89zwxYCbB71f7yQK5Lr7NasfoA", | ||
"validSignature": true, | ||
"tags": [] | ||
}, | ||
{ | ||
"comment": "P-256 key and signature, with non-low-S signature which is invalid in atproto", | ||
"messageBase64": "oWVoZWxsb2V3b3JsZA", | ||
"algorithm": "ES256", | ||
"didDocSuite": "EcdsaSecp256r1VerificationKey2019", | ||
"publicKeyDid": "did:key:zDnaembgSGUhZULN2Caob4HLJPaxBh92N7rtH21TErzqf8HQo", | ||
"publicKeyMultibase": "zxdM8dSstjrpZaRUwBmDvjGXweKuEMVN95A9oJBFjkWMh", | ||
"signatureBase64": "2vZNsG3UKvvO/CDlrdvyZRISOFylinBh0Jupc6KcWoKp7O4VS9giSAah8k5IUbXIW00SuOrjfEqQ9HEkN9JGzw", | ||
"validSignature": false, | ||
"tags": ["high-s"] | ||
}, | ||
{ | ||
"comment": "K-256 key and signature, with non-low-S signature which is invalid in atproto", | ||
"messageBase64": "oWVoZWxsb2V3b3JsZA", | ||
"algorithm": "ES256K", | ||
"didDocSuite": "EcdsaSecp256k1VerificationKey2019", | ||
"publicKeyDid": "did:key:zQ3shqwJEJyMBsBXCWyCBpUBMqxcon9oHB7mCvx4sSpMdLJwc", | ||
"publicKeyMultibase": "z25z9DTpsiYYJKGsWmSPJK2NFN8PcJtZig12K59UgW7q5t", | ||
"signatureBase64": "5WpdIuEUUfVUYaozsi8G0B3cWO09cgZbIIwg1t2YKdXYA67MYxYiTMAVfdnkDCMN9S5B3vHosRe07aORmoshoQ", | ||
"validSignature": false, | ||
"tags": ["high-s"] | ||
}, | ||
{ | ||
"comment": "P-256 key and signature, with DER-encoded signature which is invalid in atproto", | ||
"messageBase64": "oWVoZWxsb2V3b3JsZA", | ||
"algorithm": "ES256", | ||
"didDocSuite": "EcdsaSecp256r1VerificationKey2019", | ||
"publicKeyDid": "did:key:zDnaeT6hL2RnTdUhAPLij1QBkhYZnmuKyM7puQLW1tkF4Zkt8", | ||
"publicKeyMultibase": "ze8N2PPxnu19hmBQ58t5P3E9Yj6CqakJmTVCaKvf9Byq2", | ||
"signatureBase64": "MEQCIFxYelWJ9lNcAVt+jK0y/T+DC/X4ohFZ+m8f9SEItkY1AiACX7eXz5sgtaRrz/SdPR8kprnbHMQVde0T2R8yOTBweA", | ||
"validSignature": false, | ||
"tags": ["der-encoded"] | ||
}, | ||
{ | ||
"comment": "K-256 key and signature, with DER-encoded signature which is invalid in atproto", | ||
"messageBase64": "oWVoZWxsb2V3b3JsZA", | ||
"algorithm": "ES256K", | ||
"didDocSuite": "EcdsaSecp256k1VerificationKey2019", | ||
"publicKeyDid": "did:key:zQ3shnriYMXc8wvkbJqfNWh5GXn2bVAeqTC92YuNbek4npqGF", | ||
"publicKeyMultibase": "z22uZXWP8fdHXi4jyx8cCDiBf9qQTsAe6VcycoMQPfcMQX", | ||
"signatureBase64": "MEUCIQCWumUqJqOCqInXF7AzhIRg2MhwRz2rWZcOEsOjPmNItgIgXJH7RnqfYY6M0eg33wU0sFYDlprwdOcpRn78Sz5ePgk", | ||
"validSignature": false, | ||
"tags": ["der-encoded"] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import base64 | ||
import json | ||
import os | ||
|
||
import pytest | ||
from atproto_crypto.verify import verify_signature | ||
|
||
# Ref: https://github.com/bluesky-social/atproto/blob/main/interop-test-files/crypto/signature-fixtures.json | ||
_FIXTURES_FILE_PATH = os.path.join(os.path.dirname(__file__), 'signature-fixtures.json') | ||
|
||
|
||
def _load_test_cases() -> list: | ||
with open(_FIXTURES_FILE_PATH, encoding='UTF-8') as file: | ||
return json.load(file) | ||
|
||
|
||
def _fix_base64_padding(data: str) -> str: | ||
return data + '==' | ||
|
||
|
||
def _decode_b64(data: str) -> bytes: | ||
return base64.b64decode(_fix_base64_padding(data)) | ||
|
||
|
||
@pytest.mark.parametrize('test_case', _load_test_cases(), ids=lambda x: x['comment']) | ||
def test_verify_signature(test_case: dict) -> None: | ||
did_key = test_case['publicKeyDid'] | ||
data = _decode_b64(test_case['messageBase64']) | ||
signature = _decode_b64(test_case['signatureBase64']) | ||
expected_valid = test_case['validSignature'] | ||
|
||
assert verify_signature(did_key, data, signature) == expected_valid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters