Skip to content

Commit f2e623f

Browse files
committed
Wycheproof: Fix incorrect assertion for invalid encaps/decaps tests
The encaps and combined test functions asserted that K should NOT match the expected value for invalid test cases where the binary succeeded. This is wrong: for invalid inputs, the binary should reject the input via input validation, not succeed and produce a different K. Fix this by restructuring all test functions to branch on tc["result"] first, then verify the binary's behavior: - "invalid": assert the binary rejected the input (_error or decode_error) - "valid": assert outputs match expected values - anything else: fail with unexpected result Additional changes: - Remove "acceptable" as a result type (not used in any test vectors) - In keygen_seed tests, only "valid" results exist; simplify accordingly - In semi_expanded_decaps tests, assert that invalid tests are rejected rather than silently ignoring errors on valid tests - In combined tests, distinguish keygen decode errors from runtime errors, and always verify ek when keygen succeeds Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
1 parent e920542 commit f2e623f

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

test/wycheproof/wycheproof_client.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,15 @@ def run_keygen_seed_test(data_file):
103103
for tc in tg["tests"]:
104104
info(f" tcId={tc['tcId']} ... ", end="")
105105
out = run_binary([binary, "keygen_seed", f"seed={tc['seed']}"])
106-
if "_error" in out or "decode_error" in out:
107-
assert (
108-
tc["result"] == "invalid"
109-
), f"binary error on non-invalid tcId={tc['tcId']}"
110-
elif tc["result"] in ("valid", "acceptable"):
106+
if tc["result"] == "valid":
111107
assert (
112108
out["ek"].upper() == tc["ek"].upper()
113109
), f"ek mismatch tcId={tc['tcId']}"
114110
assert (
115111
out["dk"].upper() == tc["dk"].upper()
116112
), f"dk mismatch tcId={tc['tcId']}"
113+
else:
114+
assert False, f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
117115
info("ok")
118116
count += 1
119117
info(f" {count} keygen_seed tests passed")
@@ -132,21 +130,19 @@ def run_encaps_test(data_file):
132130
for tc in tg["tests"]:
133131
info(f" tcId={tc['tcId']} ... ", end="")
134132
out = run_binary([binary, "encaps", f"ek={tc['ek']}", f"m={tc['m']}"])
135-
if "_error" in out or "decode_error" in out:
133+
if tc["result"] == "invalid":
136134
assert (
137-
tc["result"] == "invalid"
138-
), f"binary error on non-invalid tcId={tc['tcId']}"
139-
elif tc["result"] in ("valid", "acceptable"):
135+
"_error" in out or "decode_error" in out
136+
), f"binary success on invalid tcId={tc['tcId']}"
137+
elif tc["result"] == "valid":
140138
assert (
141139
out["c"].upper() == tc["c"].upper()
142140
), f"c mismatch tcId={tc['tcId']}"
143141
assert (
144142
out["K"].upper() == tc["K"].upper()
145143
), f"K mismatch tcId={tc['tcId']}"
146-
elif tc["result"] == "invalid":
147-
assert (
148-
out["K"].upper() != tc["K"].upper()
149-
), f"K should not match for invalid tcId={tc['tcId']}"
144+
else:
145+
assert False, f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
150146
info("ok")
151147
count += 1
152148
info(f" {count} encaps tests passed")
@@ -165,9 +161,14 @@ def run_semi_expanded_decaps_test(data_file):
165161
for tc in tg["tests"]:
166162
info(f" tcId={tc['tcId']} ... ", end="")
167163
out = run_binary([binary, "decaps", f"dk={tc['dk']}", f"c={tc['c']}"])
168-
# For errors (wrong length dk/c, or runtime failure), just check it didn't crash unexpectedly
169-
if "_error" not in out and "decode_error" not in out:
164+
if tc["result"] == "invalid":
165+
assert (
166+
"_error" in out or "decode_error" in out
167+
), f"binary success on invalid tcId={tc['tcId']}"
168+
elif tc["result"] == "valid":
170169
assert "K" in out, f"missing K in output tcId={tc['tcId']}"
170+
else:
171+
assert False, f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
171172
info("ok")
172173
count += 1
173174
info(f" {count} semi_expanded_decaps tests passed")
@@ -187,33 +188,31 @@ def run_combined_test(data_file):
187188
info(f" tcId={tc['tcId']} ... ", end="")
188189
# Generate keypair from seed
189190
keygen_out = run_binary([binary, "keygen_seed", f"seed={tc['seed']}"])
190-
if "_error" in keygen_out or "decode_error" in keygen_out:
191+
if "decode_error" in keygen_out:
191192
assert (
192193
tc["result"] == "invalid"
193-
), f"keygen error on non-invalid tcId={tc['tcId']}"
194+
), f"keygen decode error on valid tcId={tc['tcId']}"
194195
info("ok")
195196
count += 1
196197
continue
197-
# Check ek matches if present
198-
if "ek" in tc and tc["result"] in ("valid", "acceptable"):
199-
assert (
200-
keygen_out["ek"].upper() == tc["ek"].upper()
201-
), f"ek mismatch tcId={tc['tcId']}"
198+
# Keygen succeeded — check ek
199+
assert "ek" in tc, f"missing ek in test vector tcId={tc['tcId']}"
200+
assert (
201+
keygen_out["ek"].upper() == tc["ek"].upper()
202+
), f"ek mismatch tcId={tc['tcId']}"
202203
# Decapsulate
203204
dk = keygen_out["dk"]
204205
decaps_out = run_binary([binary, "decaps", f"dk={dk}", f"c={tc['c']}"])
205-
if "_error" in decaps_out or "decode_error" in decaps_out:
206+
if tc["result"] == "invalid":
206207
assert (
207-
tc["result"] == "invalid"
208-
), f"decaps error on non-invalid tcId={tc['tcId']}"
209-
elif tc["result"] in ("valid", "acceptable"):
208+
"_error" in decaps_out or "decode_error" in decaps_out
209+
), f"binary success on invalid tcId={tc['tcId']}"
210+
elif tc["result"] == "valid":
210211
assert (
211212
decaps_out["K"].upper() == tc["K"].upper()
212213
), f"K mismatch tcId={tc['tcId']}"
213-
elif tc["result"] == "invalid":
214-
assert (
215-
decaps_out["K"].upper() != tc["K"].upper()
216-
), f"K should not match for invalid tcId={tc['tcId']}"
214+
else:
215+
assert False, f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
217216
info("ok")
218217
count += 1
219218
info(f" {count} combined tests passed")

0 commit comments

Comments
 (0)