Skip to content

Commit c5c7577

Browse files
committed
add: {ECDH-ES+AES-KW}x{CTR, CBC} test
1 parent 8106acb commit c5c7577

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

tests/test_recipient.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,3 +958,72 @@ def test_recipients_a256ctr_hpke(self, rsk1, rsk2):
958958
)
959959
recipient = COSE.new()
960960
assert b"This is the content." == recipient.decode(encoded, [rsk1, rsk2])
961+
962+
@pytest.mark.parametrize(
963+
"key_agreement_alg, key_agreement_alg_id, kw_alg, enc_alg",
964+
[
965+
("ECDH-ES+A128KW", -29, "A128KW", "A128CTR"),
966+
("ECDH-ES+A192KW", -30, "A192KW", "A192CTR"),
967+
("ECDH-ES+A256KW", -31, "A256KW", "A256CTR"),
968+
("ECDH-ES+A128KW", -29, "A128KW", "A128CBC"),
969+
("ECDH-ES+A192KW", -30, "A192KW", "A192CBC"),
970+
("ECDH-ES+A256KW", -31, "A256KW", "A256CBC"),
971+
],
972+
)
973+
def test_recipients_ecdh_es(self, key_agreement_alg, key_agreement_alg_id, kw_alg, enc_alg):
974+
enc_key = COSEKey.from_symmetric_key(alg=enc_alg)
975+
context = {
976+
"alg": kw_alg,
977+
"supp_pub": {
978+
"key_data_length": len(enc_key.key) * 8,
979+
"protected": {1: key_agreement_alg_id},
980+
},
981+
}
982+
983+
# The sender side:
984+
rsk1 = COSEKey.from_jwk(
985+
{
986+
"kty": "EC",
987+
"kid": "01",
988+
"crv": "P-256",
989+
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
990+
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
991+
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM",
992+
"alg": key_agreement_alg,
993+
}
994+
)
995+
rpk2 = COSEKey.from_jwk(
996+
{
997+
"kty": "EC",
998+
# "kid": "02",
999+
"crv": "P-256",
1000+
"x": "-eZXC6nV-xgthy8zZMCN8pcYSeE2XfWWqckA2fsxHPc",
1001+
"y": "BGU5soLgsu_y7GN2I3EPUXS9EZ7Sw0qif-V70JtInFI",
1002+
}
1003+
)
1004+
r = Recipient.new(protected={"alg": key_agreement_alg}, sender_key=rsk1, recipient_key=rpk2, context=context)
1005+
1006+
nonce = enc_key.generate_nonce()
1007+
sender = COSE.new()
1008+
encoded = sender.encode(
1009+
b"Hello world!",
1010+
enc_key,
1011+
protected={"alg": enc_alg},
1012+
unprotected={"iv": nonce},
1013+
recipients=[r],
1014+
)
1015+
1016+
# The recipient side:
1017+
rsk2 = COSEKey.from_jwk(
1018+
{
1019+
"kty": "EC",
1020+
"kid": "02",
1021+
"crv": "P-256",
1022+
"x": "-eZXC6nV-xgthy8zZMCN8pcYSeE2XfWWqckA2fsxHPc",
1023+
"y": "BGU5soLgsu_y7GN2I3EPUXS9EZ7Sw0qif-V70JtInFI",
1024+
"d": "kwibx3gas6Kz1V2fyQHKSnr-ybflddSjN0eOnbmLmyo",
1025+
"alg": key_agreement_alg,
1026+
}
1027+
)
1028+
recipient = COSE.new()
1029+
assert b"Hello world!" == recipient.decode(encoded, rsk2, context)

0 commit comments

Comments
 (0)