Skip to content

Commit 5cc2776

Browse files
authored
test(hpke): add PSK vectors from testvectors.txt (#662)
* test(hpke): add PSK vectors from testvectors.txt * Fix formatter/linter error.
1 parent 45937d5 commit 5cc2776

2 files changed

Lines changed: 181 additions & 137 deletions

File tree

tests/test_cose_hpke_vectors.py

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -176,48 +176,93 @@ def test_encrypt0_vector(self, key_hex, ct_hex, external_aad, hpke_info):
176176
assert result == b"hpke test payload"
177177

178178

179-
@pytest.mark.skip(
180-
reason="PSK test vectors do not include psk_id in the protected header "
181-
"as required by the updated draft-ietf-cose-hpke. "
182-
"Vectors need to be regenerated with psk_id in the protected header."
183-
)
179+
# --- PSK vectors loaded from testvectors.txt ---
180+
181+
VECTORS_PATH = os.path.join(os.path.dirname(__file__), "vectors", "testvectors.txt")
182+
183+
PLAINTEXT = b"hpke test payload"
184+
185+
PSK = bytes.fromhex("0247fd33b913760fa1fa51e1892d9f307fbe65eb171e8132c2af18555a738b82")
186+
187+
EXT_AAD = b"external-aad"
188+
EXT_INFO = b"external-info"
189+
EXT_HPKE_AAD = b"external-hpke-aad"
190+
191+
192+
def _parse_psk_vectors():
193+
"""Parse testvectors.txt and return KE+PSK and Encrypt0+PSK vectors."""
194+
with open(VECTORS_PATH) as f:
195+
lines = f.readlines()
196+
197+
ke_psk = []
198+
e0_psk = []
199+
200+
current_key = None
201+
i = 0
202+
while i < len(lines):
203+
line = lines[i].rstrip("\n")
204+
i += 1
205+
206+
if "COSE_Key" in line:
207+
idx = line.rfind(": ")
208+
if idx >= 0:
209+
current_key = line[idx + 2 :].strip()
210+
continue
211+
212+
if "KE+PSK with" in line:
213+
desc = line
214+
while i < len(lines):
215+
ct_line = lines[i].rstrip("\n")
216+
i += 1
217+
if ct_line.startswith("Ciphertext: "):
218+
ct_hex = ct_line[len("Ciphertext: ") :]
219+
break
220+
ext_aad = EXT_AAD if "external aad" in desc else b""
221+
extra_info = EXT_INFO if "external info" in desc else b""
222+
hpke_aad = EXT_HPKE_AAD if "external hpke aad" in desc else b""
223+
ke_psk.append((current_key, ct_hex, ext_aad, extra_info, hpke_aad))
224+
225+
elif "Encrypt0+PSK with" in line:
226+
desc = line
227+
while i < len(lines):
228+
ct_line = lines[i].rstrip("\n")
229+
i += 1
230+
if ct_line.startswith("Ciphertext: "):
231+
ct_hex = ct_line[len("Ciphertext: ") :]
232+
break
233+
ext_aad = EXT_AAD if "external aad" in desc else b""
234+
hpke_info = EXT_INFO if "external info" in desc else b""
235+
e0_psk.append((current_key, ct_hex, ext_aad, hpke_info))
236+
237+
return ke_psk, e0_psk
238+
239+
240+
_KE_PSK_VECTORS, _E0_PSK_VECTORS = _parse_psk_vectors()
241+
242+
184243
class TestCOSEHPKEKEPSKVectors:
185244
"""Test vectors for COSE-HPKE Key Encryption with PSK (COSE_Encrypt)."""
186245

187246
@pytest.mark.parametrize(
188247
"key_hex, ct_hex, external_aad, extra_info, hpke_aad",
189-
[v[:5] for v in _KE_PSK],
190-
ids=[v[5] for v in _KE_PSK],
248+
_KE_PSK_VECTORS,
191249
)
192250
def test_ke_psk_vector(self, key_hex, ct_hex, external_aad, extra_info, hpke_aad):
193251
key = COSEKey.new(cbor2.loads(bytes.fromhex(key_hex)))
194252
ct = bytes.fromhex(ct_hex)
195-
result = COSE.new().decode(
196-
ct,
197-
key,
198-
external_aad=external_aad,
199-
extra_info=extra_info,
200-
hpke_aad=hpke_aad,
201-
hpke_psk=PSK,
202-
)
203-
assert result == b"hpke test payload"
253+
result = COSE.new().decode(ct, key, external_aad=external_aad, extra_info=extra_info, hpke_aad=hpke_aad, hpke_psk=PSK)
254+
assert result == PLAINTEXT
204255

205256

206-
@pytest.mark.skip(
207-
reason="PSK test vectors do not include psk_id in the protected header "
208-
"as required by the updated draft-ietf-cose-hpke. "
209-
"Vectors need to be regenerated with psk_id in the protected header."
210-
)
211257
class TestCOSEHPKEEncrypt0PSKVectors:
212258
"""Test vectors for COSE-HPKE Integrated Encryption with PSK (COSE_Encrypt0)."""
213259

214260
@pytest.mark.parametrize(
215261
"key_hex, ct_hex, external_aad, hpke_info",
216-
[v[:4] for v in _E0_PSK],
217-
ids=[v[4] for v in _E0_PSK],
262+
_E0_PSK_VECTORS,
218263
)
219264
def test_encrypt0_psk_vector(self, key_hex, ct_hex, external_aad, hpke_info):
220265
key = COSEKey.new(cbor2.loads(bytes.fromhex(key_hex)))
221266
ct = bytes.fromhex(ct_hex)
222267
result = COSE.new().decode(ct, key, external_aad=external_aad, hpke_info=hpke_info, hpke_psk=PSK)
223-
assert result == b"hpke test payload"
268+
assert result == PLAINTEXT

0 commit comments

Comments
 (0)