Skip to content

Commit

Permalink
CBMC: Add proof for crypto_kem_enc
Browse files Browse the repository at this point in the history
Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker authored and rod-chapman committed Nov 13, 2024
1 parent 5eea727 commit 11131b9
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 35 deletions.
54 changes: 54 additions & 0 deletions cbmc/proofs/crypto_kem_dec/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# SPDX-License-Identifier: Apache-2.0

include ../Makefile_params.common

HARNESS_ENTRY = harness
HARNESS_FILE = crypto_kem_dec_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_dec

DEFINES +=
INCLUDES +=

REMOVE_FUNCTION_BODY +=
UNWINDSET +=

PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
PROJECT_SOURCES += $(SRCDIR)/mlkem/kem.c

CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)dec
USE_FUNCTION_CONTRACTS= $(FIPS202_NAMESPACE)sha3_512 $(FIPS202_NAMESPACE)sha3_256 $(MLKEM_NAMESPACE)indcpa_enc $(MLKEM_NAMESPACE)indcpa_dec $(MLKEM_NAMESPACE)mlkem_shake256_rkprf $(MLKEM_NAMESPACE)verify $(MLKEM_NAMESPACE)cmov memcmp
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)dec

# 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. Hdece, _SOURCE,
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.

include ../Makefile.common
3 changes: 3 additions & 0 deletions cbmc/proofs/crypto_kem_dec/cbmc-proof.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: Apache-2.0

# This file marks this directory as containing a CBMC proof.
27 changes: 27 additions & 0 deletions cbmc/proofs/crypto_kem_dec/crypto_kem_dec_harness.c
Original file line number Diff line number Diff line change
@@ -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_dec_harness.c
* @brief Implements the proof harness for crypto_kem_dec function.
*/

/*
* Insert project header files that
* - include the declaration of the function
* - include the types needed to declare function arguments
*/
#include <kem.h>

/**
* @brief Starting point for formal analysis
*
*/
void harness(void) {
uint8_t *a, *b, *c;
crypto_kem_dec(a, b, c);
}
18 changes: 0 additions & 18 deletions mlkem/kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,6 @@ int crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk) {
return crypto_kem_enc_derand(ct, ss, pk, coins);
}

/*************************************************
* Name: crypto_kem_dec
*
* Description: Generates shared secret for given
* cipher text and private key
*
* Arguments: - uint8_t *ss: pointer to output shared secret
* (an already allocated array of MLKEM_SSBYTES bytes)
* - const uint8_t *ct: pointer to input cipher text
* (an already allocated array of MLKEM_CIPHERTEXTBYTES bytes)
* - const uint8_t *sk: pointer to input private key
* (an already allocated array of MLKEM_SECRETKEYBYTES bytes)
*
* Returns 0 on success, and -1 if the secret key hash check (see Section 7.3 of
* FIPS203) fails.
*
* On failure, ss will contain a pseudo-random value.
**************************************************/
int crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk) {
int fail;
uint8_t buf[2 * MLKEM_SYMBYTES] ALIGN;
Expand Down
26 changes: 25 additions & 1 deletion mlkem/kem.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ int crypto_kem_enc(uint8_t *ct, uint8_t *ss,
// clang-format on

#define crypto_kem_dec MLKEM_NAMESPACE(dec)
int crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
/*************************************************
* Name: crypto_kem_dec
*
* Description: Generates shared secret for given
* cipher text and private key
*
* Arguments: - uint8_t *ss: pointer to output shared secret
* (an already allocated array of MLKEM_SSBYTES bytes)
* - const uint8_t *ct: pointer to input cipher text
* (an already allocated array of MLKEM_CIPHERTEXTBYTES bytes)
* - const uint8_t *sk: pointer to input private key
* (an already allocated array of MLKEM_SECRETKEYBYTES bytes)
*
* Returns 0 on success, and -1 if the secret key hash check (see Section 7.3 of
* FIPS203) fails.
*
* On failure, ss will contain a pseudo-random value.
**************************************************/
int crypto_kem_dec(uint8_t *ss, const uint8_t *ct,
const uint8_t *sk) // clang-format off
REQUIRES(IS_FRESH(ss, MLKEM_SSBYTES))
REQUIRES(IS_FRESH(ct, MLKEM_CIPHERTEXTBYTES))
REQUIRES(IS_FRESH(sk, MLKEM_SECRETKEYBYTES))
ASSIGNS(OBJECT_WHOLE(ss));
// clang-format on

#endif
13 changes: 0 additions & 13 deletions mlkem/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ int verify(const uint8_t *a, const uint8_t *b, const size_t len) {
return (int)u;
}

/*************************************************
* Name: cmov
*
* Description: Copy len bytes from x to r if b is 1;
* don't modify x if b is 0. Requires b to be in {0,1};
* assumes two's complement representation of negative integers.
* Runs in constant time.
*
* Arguments: uint8_t *r: pointer to output byte array
* const uint8_t *x: pointer to input byte array
* size_t len: Amount of bytes to be copied
* uint8_t b: Condition bit; has to be in {0,1}
**************************************************/
void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b) {
size_t i;

Expand Down
22 changes: 19 additions & 3 deletions mlkem/verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* Arguments: const uint8_t *a: pointer to first byte array
* const uint8_t *b: pointer to second byte array
* int len: length of the byte arrays
* size_t len: length of the byte arrays
*
* Returns 0 if the byte arrays are equal, 1 otherwise
**************************************************/
Expand All @@ -28,9 +28,25 @@ REQUIRES(len <= INT_MAX)
ENSURES(RETURN_VALUE == (1 - FORALL(int, i, 0, ((int)len - 1), (a[i] == b[i]))));
// clang-format on


#define cmov MLKEM_NAMESPACE(cmov)
void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b);
/*************************************************
* Name: cmov
*
* Description: Copy len bytes from x to r if b is 1;
* don't modify x if b is 0. Requires b to be in {0,1};
* assumes two's complement representation of negative integers.
* Runs in constant time.
*
* Arguments: uint8_t *r: pointer to output byte array
* const uint8_t *x: pointer to input byte array
* size_t len: Amount of bytes to be copied
* uint8_t b: Condition bit; has to be in {0,1}
**************************************************/
void cmov(uint8_t *r, const uint8_t *x, size_t len,
uint8_t b) // clang-format on
REQUIRES(IS_FRESH(r, len)) REQUIRES(IS_FRESH(x, len))
REQUIRES(b == 0 || b == 1) ASSIGNS(OBJECT_UPTO(r, len));
// clang-format off

#define cmov_int16 MLKEM_NAMESPACE(cmov_int16)
/*************************************************
Expand Down

0 comments on commit 11131b9

Please sign in to comment.