Skip to content

Commit dc0d194

Browse files
committed
refactor: format long lines and improve type hints in COSE/HPKE code
1 parent 4cc17bf commit dc0d194

5 files changed

Lines changed: 39 additions & 11 deletions

File tree

cwt/algs/ec2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def __init__(self, params: Dict[int, Any]):
8383
self._key_ops = [2]
8484
if self._alg:
8585
# Validate alg for EC2 curve.
86-
if self._crv == 1 and self._alg not in ([-7, -9, 35, 45, 46, 53] + list(COSE_ALGORITHMS_CKDM_KEY_AGREEMENT.values())):
86+
if self._crv == 1 and self._alg not in (
87+
[-7, -9, 35, 45, 46, 53] + list(COSE_ALGORITHMS_CKDM_KEY_AGREEMENT.values())
88+
):
8789
raise ValueError(f"Unsupported or unknown alg used with P-256: {self._alg}.")
8890
elif self._crv == 2 and self._alg not in ([-35, -51, 37, 47] + list(COSE_ALGORITHMS_CKDM_KEY_AGREEMENT.values())):
8991
raise ValueError(f"Unsupported or unknown alg used with P-384: {self._alg}.")

cwt/cose.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ def decode(
350350
DecodeError: Failed to decode data.
351351
VerifyError: Failed to verify data.
352352
"""
353-
_, _, res = self.decode_with_headers(data, keys, context, external_aad, detached_payload, enable_non_aead, hpke_psk, hpke_info, hpke_aad, extra_info)
353+
_, _, res = self.decode_with_headers(
354+
data, keys, context, external_aad, detached_payload, enable_non_aead, hpke_psk, hpke_info, hpke_aad, extra_info
355+
)
354356
return res
355357

356358
def decode_with_headers(
@@ -365,7 +367,7 @@ def decode_with_headers(
365367
hpke_info: bytes = b"",
366368
hpke_aad: bytes = b"",
367369
extra_info: bytes = b"",
368-
) -> Tuple[Dict[int, Any], Dict[int, Any], bytes]:
370+
) -> Tuple[Dict[Union[str, int], Any], Dict[Union[str, int], Any], bytes]:
369371
"""
370372
Verifies and decodes COSE data, and returns protected headers, unprotected headers and payload.
371373
@@ -490,7 +492,9 @@ def decode_with_headers(
490492

491493
# Encrypt
492494
if data.tag == 96:
493-
rs = Recipients.from_list(data.value[3], self._verify_kid, context, hpke_psk=hpke_psk, extra_info=extra_info, hpke_aad=hpke_aad)
495+
rs = Recipients.from_list(
496+
data.value[3], self._verify_kid, context, hpke_psk=hpke_psk, extra_info=extra_info, hpke_aad=hpke_aad
497+
)
494498
nonce = u.get(5, b"")
495499
enc_key = rs.derive_key(keys, alg, external_aad, "Enc_Recipient")
496500
aad = self._dumps(["Encrypt", data.value[0], external_aad])

cwt/recipient.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,31 @@ def from_list(
135135
if not isinstance(recipient[2], bytes):
136136
raise ValueError("ciphertext should be bytes.")
137137
if len(recipient) == 3:
138-
rec = cls.new(protected, recipient[1], recipient[2], hpke_psk=hpke_psk, context=context, extra_info=extra_info, hpke_aad=hpke_aad)
138+
rec = cls.new(
139+
protected,
140+
recipient[1],
141+
recipient[2],
142+
hpke_psk=hpke_psk,
143+
context=context,
144+
extra_info=extra_info,
145+
hpke_aad=hpke_aad,
146+
)
139147
rec._set_b_protected(recipient[0])
140148
return rec
141149
if not isinstance(recipient[3], list):
142150
raise ValueError("recipients should be list.")
143151
recipients: List[RecipientInterface] = []
144152
for r in recipient[3]:
145153
recipients.append(cls.from_list(r, hpke_psk=hpke_psk, extra_info=extra_info, hpke_aad=hpke_aad))
146-
rec = cls.new(protected, recipient[1], recipient[2], recipients, hpke_psk=hpke_psk, context=context, extra_info=extra_info, hpke_aad=hpke_aad)
154+
rec = cls.new(
155+
protected,
156+
recipient[1],
157+
recipient[2],
158+
recipients,
159+
hpke_psk=hpke_psk,
160+
context=context,
161+
extra_info=extra_info,
162+
hpke_aad=hpke_aad,
163+
)
147164
rec._set_b_protected(recipient[0])
148165
return rec

tests/test_cose_hpke.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
import cbor2
1111
import pytest
1212

13-
from cwt import COSE, COSEAlgs, COSEHeaders, COSEKey, DecodeError, EncodeError, Recipient
13+
from cwt import (
14+
COSE,
15+
COSEAlgs,
16+
COSEHeaders,
17+
COSEKey,
18+
DecodeError,
19+
EncodeError,
20+
Recipient,
21+
)
1422

1523

1624
class TestCOSE_HPKE:

tests/test_cose_hpke_vectors.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from cwt import COSE, COSEKey
77

8-
98
# --- KE vectors (COSE_Encrypt, tag 96) ---
109

1110
# HPKE-0-KE private key
@@ -1178,9 +1177,7 @@ def test_ke_vector(self, key_hex, ct_hex, external_aad, extra_info, hpke_aad, re
11781177
pytest.xfail("draft-ietf-cose-hpke-23 X448 (HPKE-5/6) KE test vectors appear incorrect")
11791178
key = COSEKey.new(cbor2.loads(bytes.fromhex(key_hex)))
11801179
ct = bytes.fromhex(ct_hex)
1181-
result = COSE.new().decode(
1182-
ct, key, external_aad=external_aad, extra_info=extra_info, hpke_aad=hpke_aad
1183-
)
1180+
result = COSE.new().decode(ct, key, external_aad=external_aad, extra_info=extra_info, hpke_aad=hpke_aad)
11841181
assert result == b"hpke test payload"
11851182

11861183

0 commit comments

Comments
 (0)