Skip to content

Commit 095f519

Browse files
committed
Check that ML-KEM derives the same shared secret on both sides
1 parent 1ae8569 commit 095f519

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

pq/ml_kem/ml_kem.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ int main(void)
6262
byte bob_ct[WC_ML_KEM_512_CIPHER_TEXT_SIZE];
6363
byte alice_ss[WC_ML_KEM_SS_SZ];
6464
byte bob_ss[WC_ML_KEM_SS_SZ];
65+
byte diff = 0;
66+
int i;
6567

6668
printf("Alice creates an ML-KEM-512 key pair\n\n");
6769

@@ -133,6 +135,21 @@ int main(void)
133135
printf("An error occurred\n");
134136
}
135137

138+
if (ret == 0) {
139+
/* the whole point of the KEM: both sides must derive the same secret.
140+
* Accumulate rather than break early, the secrets are secret data. */
141+
for (i = 0; i < WC_ML_KEM_SS_SZ; i++)
142+
diff |= (byte)(alice_ss[i] ^ bob_ss[i]);
143+
144+
if (diff != 0) {
145+
printf("Shared secrets do not match\n");
146+
ret = -1;
147+
}
148+
else {
149+
printf("Shared secrets match\n");
150+
}
151+
}
152+
136153
wc_ForceZero(alice_ss, sizeof(alice_ss));
137154
wc_ForceZero(bob_ss, sizeof(bob_ss));
138155

@@ -142,6 +159,7 @@ int main(void)
142159
wc_MlKemKey_Free(&BobKey);
143160
if (rngInit)
144161
wc_FreeRng(&rng);
145-
return ret;
162+
/* not ret: an exit code is masked to 8 bits, so -256 would read as 0 */
163+
return (ret == 0) ? 0 : 1;
146164
}
147165

0 commit comments

Comments
 (0)