From f089daf2d17bfd14a2450d489653c5ef7bfd3161 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Sun, 12 Nov 2023 18:35:09 -0500 Subject: [PATCH] feat: test compatibility between askar and authlib And add a failing test for further debugging Signed-off-by: Daniel Bluhm --- didcomm_messaging/crypto/backend/authlib.py | 3 +- tests/{authlib => crypto}/__init__.py | 0 .../test_crypto.py => crypto/test_askar.py} | 0 tests/crypto/test_askar_x_authlib.py | 87 +++++++++++++++++++ tests/{authlib => crypto}/test_authlib.py | 0 5 files changed, 89 insertions(+), 1 deletion(-) rename tests/{authlib => crypto}/__init__.py (100%) rename tests/{askar/test_crypto.py => crypto/test_askar.py} (100%) create mode 100644 tests/crypto/test_askar_x_authlib.py rename tests/{authlib => crypto}/test_authlib.py (100%) diff --git a/didcomm_messaging/crypto/backend/authlib.py b/didcomm_messaging/crypto/backend/authlib.py index 9fe28b5..871165f 100644 --- a/didcomm_messaging/crypto/backend/authlib.py +++ b/didcomm_messaging/crypto/backend/authlib.py @@ -1,6 +1,7 @@ """Authlib implementation of DIDComm crypto.""" import hashlib +import json from typing import Mapping, Optional, Sequence, Tuple, Union from pydid import VerificationMethod @@ -180,7 +181,7 @@ async def ecdh_1pu_encrypt( res = jwe.serialize_json( header, message, [value.key for value in to_keys], sender_key=sender_key.key ) - return res + return json.dumps(res).encode() async def ecdh_1pu_decrypt( self, diff --git a/tests/authlib/__init__.py b/tests/crypto/__init__.py similarity index 100% rename from tests/authlib/__init__.py rename to tests/crypto/__init__.py diff --git a/tests/askar/test_crypto.py b/tests/crypto/test_askar.py similarity index 100% rename from tests/askar/test_crypto.py rename to tests/crypto/test_askar.py diff --git a/tests/crypto/test_askar_x_authlib.py b/tests/crypto/test_askar_x_authlib.py new file mode 100644 index 0000000..344fae4 --- /dev/null +++ b/tests/crypto/test_askar_x_authlib.py @@ -0,0 +1,87 @@ +"""Test compabibility between Askar and Authlib.""" +import json +from aries_askar import Key, KeyAlg +from authlib.jose import OKPKey +import pytest + +from didcomm_messaging.crypto.backend.askar import ( + AskarCryptoService, + AskarKey, + AskarSecretKey, +) +from didcomm_messaging.crypto.backend.authlib import ( + AuthlibCryptoService, + AuthlibKey, + AuthlibSecretKey, +) + + +ALICE_KID = "did:example:alice#key-1" +BOB_KID = "did:example:bob#key-1" + + +@pytest.fixture +def alice_askar_key(): + yield Key.generate(KeyAlg.X25519) + + +@pytest.fixture +def bob_askar_key(): + yield Key.generate(KeyAlg.X25519) + + +@pytest.fixture +def alice_authlib_key(alice_askar_key: Key): + yield OKPKey.import_key(json.loads(alice_askar_key.get_jwk_public())) + + +@pytest.fixture +def bob_authlib_key(bob_askar_key: Key): + yield OKPKey.import_key(json.loads(bob_askar_key.get_jwk_secret())) + + +@pytest.fixture +def alice(alice_askar_key: Key, alice_authlib_key: OKPKey): + yield AskarSecretKey(alice_askar_key, ALICE_KID), AuthlibKey( + alice_authlib_key, ALICE_KID + ) + + +@pytest.fixture +def bob(bob_askar_key: Key, bob_authlib_key: OKPKey): + yield AuthlibSecretKey(bob_authlib_key, BOB_KID), AskarKey(bob_askar_key, BOB_KID) + + +@pytest.fixture +def askar(): + yield AskarCryptoService() + + +@pytest.fixture +def authlib(): + yield AuthlibCryptoService() + + +@pytest.mark.asyncio +async def test_compat( + askar: AskarCryptoService, + authlib: AuthlibCryptoService, + alice: tuple[AskarSecretKey, AuthlibKey], + bob: tuple[AuthlibSecretKey, AskarKey], +): + """Test compabibility between Askar and Authlib. + + Alice uses Askar, Bob uses Authlib. + """ + alice_sk, alice_pk = alice + bob_sk, bob_pk = bob + + to_alice = b"Dear alice, please decrypt this" + enc_message = await authlib.ecdh_1pu_encrypt([alice_pk], bob_sk, to_alice) + plaintext = await askar.ecdh_1pu_decrypt(enc_message, alice_sk, bob_pk) + assert plaintext == to_alice + + to_bob = b"Dear bob, please decrypt this" + enc_message = await askar.ecdh_1pu_encrypt([bob_pk], alice_sk, to_bob) + plaintext = await authlib.ecdh_1pu_decrypt(enc_message, bob_sk, alice_pk) + assert plaintext == to_bob diff --git a/tests/authlib/test_authlib.py b/tests/crypto/test_authlib.py similarity index 100% rename from tests/authlib/test_authlib.py rename to tests/crypto/test_authlib.py