From 7b4816194e550a5cb07e9557e7ec19923c1219e8 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Sat, 9 Nov 2024 06:10:29 +0000 Subject: [PATCH] CBMC: Prove type+memory-safety of crypto_kem_keypair_derand This commit brings together the numerous low-level proofs to a proof of type-safety and memory-safety of the top-level MLKEM key generation API `crypto_kem_keypair_derand()`. Signed-off-by: Hanno Becker --- .../proofs/crypto_kem_keypair_derand/Makefile | 54 +++++++++++++++++++ .../crypto_kem_keypair_derand/cbmc-proof.txt | 3 ++ .../crypto_kem_keypair_derand_harness.c | 27 ++++++++++ mlkem/kem.h | 10 +++- 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 cbmc/proofs/crypto_kem_keypair_derand/Makefile create mode 100644 cbmc/proofs/crypto_kem_keypair_derand/cbmc-proof.txt create mode 100644 cbmc/proofs/crypto_kem_keypair_derand/crypto_kem_keypair_derand_harness.c diff --git a/cbmc/proofs/crypto_kem_keypair_derand/Makefile b/cbmc/proofs/crypto_kem_keypair_derand/Makefile new file mode 100644 index 000000000..c89fc716f --- /dev/null +++ b/cbmc/proofs/crypto_kem_keypair_derand/Makefile @@ -0,0 +1,54 @@ +# SPDX-License-Identifier: Apache-2.0 + +include ../Makefile_params.common + +HARNESS_ENTRY = harness +HARNESS_FILE = crypto_kem_keypair_derand_harness + +# This should be a unique identifier for this proof, and will appear on the +# Litani dashboard. It can be human-readable and contain spaces if you wish. +PROOF_UID = crypto_kem_keypair_derand + +DEFINES += +INCLUDES += + +REMOVE_FUNCTION_BODY += +UNWINDSET += + +PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c +PROJECT_SOURCES += $(SRCDIR)/mlkem/kem.c + +CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)keypair_derand +USE_FUNCTION_CONTRACTS=$(FIPS202_NAMESPACE)sha3_256 $(MLKEM_NAMESPACE)indcpa_keypair_derand +APPLY_LOOP_CONTRACTS=on +USE_DYNAMIC_FRAMES=1 + +# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead +EXTERNAL_SAT_SOLVER= +CBMCFLAGS=--smt2 + +FUNCTION_NAME = $(MLKEM_NAMESPACE)keypair_derand + +# If this proof is found to consume huge amounts of RAM, you can set the +# EXPENSIVE variable. With new enough versions of the proof tools, this will +# restrict the number of EXPENSIVE CBMC jobs running at once. See the +# documentation in Makefile.common under the "Job Pools" heading for details. +# EXPENSIVE = true + +# This function is large enough to need... +CBMC_OBJECT_BITS = 10 + +# If you require access to a file-local ("static") function or object to conduct +# your proof, set the following (and do not include the original source file +# ("mlkem/poly.c") in PROJECT_SOURCES). +# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i +# include ../Makefile.common +# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mlkem/poly.c +# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar +# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz +# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must +# be set before including Makefile.common, but any use of variables on the +# left-hand side requires those variables to be defined. Hence, _SOURCE, +# _FUNCTIONS, _OBJECTS is set after including Makefile.common. + +include ../Makefile.common diff --git a/cbmc/proofs/crypto_kem_keypair_derand/cbmc-proof.txt b/cbmc/proofs/crypto_kem_keypair_derand/cbmc-proof.txt new file mode 100644 index 000000000..3d1913ebf --- /dev/null +++ b/cbmc/proofs/crypto_kem_keypair_derand/cbmc-proof.txt @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: Apache-2.0 + +# This file marks this directory as containing a CBMC proof. diff --git a/cbmc/proofs/crypto_kem_keypair_derand/crypto_kem_keypair_derand_harness.c b/cbmc/proofs/crypto_kem_keypair_derand/crypto_kem_keypair_derand_harness.c new file mode 100644 index 000000000..be09d867f --- /dev/null +++ b/cbmc/proofs/crypto_kem_keypair_derand/crypto_kem_keypair_derand_harness.c @@ -0,0 +1,27 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT-0 + +/* + * Insert copyright notice + */ + +/** + * @file crypto_kem_keypair_derand_harness.c + * @brief Implements the proof harness for crypto_kem_keypair_derand function. + */ + +/* + * Insert project header files that + * - include the declaration of the function + * - include the types needed to declare function arguments + */ +#include + +/** + * @brief Starting point for formal analysis + * + */ +void harness(void) { + uint8_t *a, *b, *c; + crypto_kem_keypair_derand(a, b, c); +} diff --git a/mlkem/kem.h b/mlkem/kem.h index e79e9a539..039f9e27e 100644 --- a/mlkem/kem.h +++ b/mlkem/kem.h @@ -3,6 +3,7 @@ #define KEM_H #include +#include "cbmc.h" #include "params.h" #define CRYPTO_SECRETKEYBYTES MLKEM_SECRETKEYBYTES @@ -19,7 +20,14 @@ #endif #define crypto_kem_keypair_derand MLKEM_NAMESPACE(keypair_derand) -int crypto_kem_keypair_derand(uint8_t *pk, uint8_t *sk, const uint8_t *coins); +int crypto_kem_keypair_derand(uint8_t *pk, uint8_t *sk, const uint8_t *coins) + // clang-format off + REQUIRES(IS_FRESH(pk, MLKEM_PUBLICKEYBYTES)) + REQUIRES(IS_FRESH(sk, MLKEM_SECRETKEYBYTES)) + REQUIRES(IS_FRESH(coins, 2 * MLKEM_SYMBYTES)) + ASSIGNS(OBJECT_WHOLE(pk)) + ASSIGNS(OBJECT_WHOLE(sk)); +// clang-format on #define crypto_kem_keypair MLKEM_NAMESPACE(keypair) int crypto_kem_keypair(uint8_t *pk, uint8_t *sk);