Skip to content

Commit

Permalink
feat: test compatibility between askar and authlib
Browse files Browse the repository at this point in the history
And add a failing test for further debugging

Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Nov 12, 2023
1 parent 9ca2e6a commit f089daf
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion didcomm_messaging/crypto/backend/authlib.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
87 changes: 87 additions & 0 deletions tests/crypto/test_askar_x_authlib.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.

0 comments on commit f089daf

Please sign in to comment.