Skip to content

Commit a87dddf

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 a87dddf

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

test/wycheproof/wycheproof_client.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@ 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 (
115+
False
116+
), f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
117117
info("ok")
118118
count += 1
119119
info(f" {count} keygen_seed tests passed")
@@ -132,21 +132,21 @@ def run_encaps_test(data_file):
132132
for tc in tg["tests"]:
133133
info(f" tcId={tc['tcId']} ... ", end="")
134134
out = run_binary([binary, "encaps", f"ek={tc['ek']}", f"m={tc['m']}"])
135-
if "_error" in out or "decode_error" in out:
135+
if tc["result"] == "invalid":
136136
assert (
137-
tc["result"] == "invalid"
138-
), f"binary error on non-invalid tcId={tc['tcId']}"
139-
elif tc["result"] in ("valid", "acceptable"):
137+
"_error" in out or "decode_error" in out
138+
), f"binary success on invalid tcId={tc['tcId']}"
139+
elif tc["result"] == "valid":
140140
assert (
141141
out["c"].upper() == tc["c"].upper()
142142
), f"c mismatch tcId={tc['tcId']}"
143143
assert (
144144
out["K"].upper() == tc["K"].upper()
145145
), f"K mismatch tcId={tc['tcId']}"
146-
elif tc["result"] == "invalid":
146+
else:
147147
assert (
148-
out["K"].upper() != tc["K"].upper()
149-
), f"K should not match for invalid tcId={tc['tcId']}"
148+
False
149+
), f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
150150
info("ok")
151151
count += 1
152152
info(f" {count} encaps tests passed")
@@ -165,9 +165,16 @@ def run_semi_expanded_decaps_test(data_file):
165165
for tc in tg["tests"]:
166166
info(f" tcId={tc['tcId']} ... ", end="")
167167
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:
168+
if tc["result"] == "invalid":
169+
assert (
170+
"_error" in out or "decode_error" in out
171+
), f"binary success on invalid tcId={tc['tcId']}"
172+
elif tc["result"] == "valid":
170173
assert "K" in out, f"missing K in output tcId={tc['tcId']}"
174+
else:
175+
assert (
176+
False
177+
), f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
171178
info("ok")
172179
count += 1
173180
info(f" {count} semi_expanded_decaps tests passed")
@@ -187,33 +194,33 @@ def run_combined_test(data_file):
187194
info(f" tcId={tc['tcId']} ... ", end="")
188195
# Generate keypair from seed
189196
keygen_out = run_binary([binary, "keygen_seed", f"seed={tc['seed']}"])
190-
if "_error" in keygen_out or "decode_error" in keygen_out:
197+
if "decode_error" in keygen_out:
191198
assert (
192199
tc["result"] == "invalid"
193-
), f"keygen error on non-invalid tcId={tc['tcId']}"
200+
), f"keygen decode error on valid tcId={tc['tcId']}"
194201
info("ok")
195202
count += 1
196203
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']}"
204+
# Keygen succeeded — check ek
205+
assert "ek" in tc, f"missing ek in test vector tcId={tc['tcId']}"
206+
assert (
207+
keygen_out["ek"].upper() == tc["ek"].upper()
208+
), f"ek mismatch tcId={tc['tcId']}"
202209
# Decapsulate
203210
dk = keygen_out["dk"]
204211
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:
212+
if tc["result"] == "invalid":
206213
assert (
207-
tc["result"] == "invalid"
208-
), f"decaps error on non-invalid tcId={tc['tcId']}"
209-
elif tc["result"] in ("valid", "acceptable"):
214+
"_error" in decaps_out or "decode_error" in decaps_out
215+
), f"binary success on invalid tcId={tc['tcId']}"
216+
elif tc["result"] == "valid":
210217
assert (
211218
decaps_out["K"].upper() == tc["K"].upper()
212219
), f"K mismatch tcId={tc['tcId']}"
213-
elif tc["result"] == "invalid":
220+
else:
214221
assert (
215-
decaps_out["K"].upper() != tc["K"].upper()
216-
), f"K should not match for invalid tcId={tc['tcId']}"
222+
False
223+
), f"Unexpected result '{tc['result']}' for tcId={tc['tcId']}"
217224
info("ok")
218225
count += 1
219226
info(f" {count} combined tests passed")

0 commit comments

Comments
 (0)