Skip to content

Commit 96f9cd2

Browse files
authored
Merge pull request #443 from kentakayama/add-ecdh-es+aes-kw-test
Add ECDH-ES+AES-KW test
2 parents d1cacab + c5c7577 commit 96f9cd2

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

cwt/cose.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ def _validate_cose_message(
662662
return 0 # Encrypt
663663
if recipients[0].context[0] in COSE_ALGORITHMS_HPKE.values():
664664
return 0 # Encrypt
665+
if recipients[0].context[0] in COSE_ALGORITHMS_KEY_WRAP.values():
666+
return 0 # Encrypt
665667
if recipients[0].context[0] in COSE_ALGORITHMS_MAC.values():
666668
return 1 # MAC
667669
raise ValueError(f"Invalid alg in recipients' context information: {recipients[0]._context[0]}.")

tests/test_recipient.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,72 @@ def test_recipients_hpke(self, rsk1, rsk2, enc_alg):
843843
)
844844
recipient = COSE.new()
845845
assert b"This is the content." == recipient.decode(encoded, [rsk1, rsk2])
846+
847+
@pytest.mark.parametrize(
848+
"key_agreement_alg, key_agreement_alg_id, kw_alg, enc_alg",
849+
[
850+
("ECDH-ES+A128KW", -29, "A128KW", "A128CTR"),
851+
("ECDH-ES+A192KW", -30, "A192KW", "A192CTR"),
852+
("ECDH-ES+A256KW", -31, "A256KW", "A256CTR"),
853+
("ECDH-ES+A128KW", -29, "A128KW", "A128CBC"),
854+
("ECDH-ES+A192KW", -30, "A192KW", "A192CBC"),
855+
("ECDH-ES+A256KW", -31, "A256KW", "A256CBC"),
856+
],
857+
)
858+
def test_recipients_ecdh_es(self, key_agreement_alg, key_agreement_alg_id, kw_alg, enc_alg):
859+
enc_key = COSEKey.from_symmetric_key(alg=enc_alg)
860+
context = {
861+
"alg": kw_alg,
862+
"supp_pub": {
863+
"key_data_length": len(enc_key.key) * 8,
864+
"protected": {1: key_agreement_alg_id},
865+
},
866+
}
867+
868+
# The sender side:
869+
rsk1 = COSEKey.from_jwk(
870+
{
871+
"kty": "EC",
872+
"kid": "01",
873+
"crv": "P-256",
874+
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
875+
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
876+
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM",
877+
"alg": key_agreement_alg,
878+
}
879+
)
880+
rpk2 = COSEKey.from_jwk(
881+
{
882+
"kty": "EC",
883+
# "kid": "02",
884+
"crv": "P-256",
885+
"x": "-eZXC6nV-xgthy8zZMCN8pcYSeE2XfWWqckA2fsxHPc",
886+
"y": "BGU5soLgsu_y7GN2I3EPUXS9EZ7Sw0qif-V70JtInFI",
887+
}
888+
)
889+
r = Recipient.new(protected={"alg": key_agreement_alg}, sender_key=rsk1, recipient_key=rpk2, context=context)
890+
891+
nonce = enc_key.generate_nonce()
892+
sender = COSE.new()
893+
encoded = sender.encode(
894+
b"Hello world!",
895+
enc_key,
896+
protected={"alg": enc_alg},
897+
unprotected={"iv": nonce},
898+
recipients=[r],
899+
)
900+
901+
# The recipient side:
902+
rsk2 = COSEKey.from_jwk(
903+
{
904+
"kty": "EC",
905+
"kid": "02",
906+
"crv": "P-256",
907+
"x": "-eZXC6nV-xgthy8zZMCN8pcYSeE2XfWWqckA2fsxHPc",
908+
"y": "BGU5soLgsu_y7GN2I3EPUXS9EZ7Sw0qif-V70JtInFI",
909+
"d": "kwibx3gas6Kz1V2fyQHKSnr-ybflddSjN0eOnbmLmyo",
910+
"alg": key_agreement_alg,
911+
}
912+
)
913+
recipient = COSE.new()
914+
assert b"Hello world!" == recipient.decode(encoded, rsk2, context)

0 commit comments

Comments
 (0)