Skip to content

Commit 6d579fa

Browse files
mkannwischerhanno-becker
authored andcommitted
Keypair: zeroize pk and sk in case of failure
If the pair-wise consistency test fails (e.g., due to OOM), we currently do still return the pk and sk. This commit changes that to zeroize the buffers instead which lowers the risk of API mis-use. Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu>
1 parent e29da58 commit 6d579fa

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

mlkem/src/kem.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ int mlk_kem_keypair_derand(uint8_t pk[MLKEM_INDCCA_PUBLICKEYBYTES],
217217
ret = mlk_indcpa_keypair_derand(pk, sk, coins, context);
218218
if (ret != 0)
219219
{
220-
return ret;
220+
goto cleanup;
221221
}
222222

223223
mlk_memcpy(sk + MLKEM_INDCPA_SECRETKEYBYTES, pk, MLKEM_INDCCA_PUBLICKEYBYTES);
@@ -234,10 +234,17 @@ int mlk_kem_keypair_derand(uint8_t pk[MLKEM_INDCCA_PUBLICKEYBYTES],
234234
ret = mlk_check_pct(pk, sk, context);
235235
if (ret != 0)
236236
{
237-
return ret;
237+
goto cleanup;
238238
}
239239

240-
return 0;
240+
cleanup:
241+
if (ret != 0)
242+
{
243+
mlk_zeroize(pk, MLKEM_INDCCA_PUBLICKEYBYTES);
244+
mlk_zeroize(sk, MLKEM_INDCCA_SECRETKEYBYTES);
245+
}
246+
247+
return ret;
241248
}
242249

243250
#if !defined(MLK_CONFIG_NO_RANDOMIZED_API)

0 commit comments

Comments
 (0)