Skip to content

Commit ae1870d

Browse files
committed
CBMC: Add proof and contract for crypto_sign_keypair_internal
Signed-off-by: Jake Massimo <[email protected]>
1 parent 16143f7 commit ae1870d

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

mldsa/sign.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ int crypto_sign_keypair_internal(uint8_t *pk, uint8_t *sk,
1818
const uint8_t seed[MLDSA_SEEDBYTES])
1919
{
2020
uint8_t seedbuf[2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES];
21+
uint8_t inbuf[MLDSA_SEEDBYTES + 2];
2122
uint8_t tr[MLDSA_TRBYTES];
2223
const uint8_t *rho, *rhoprime, *key;
2324
polyvecl mat[MLDSA_K];
2425
polyvecl s1, s1hat;
2526
polyveck s2, t1, t0;
2627

2728
/* Get randomness for rho, rhoprime and key */
28-
memcpy(seedbuf, seed, MLDSA_SEEDBYTES);
29-
seedbuf[MLDSA_SEEDBYTES + 0] = MLDSA_K;
30-
seedbuf[MLDSA_SEEDBYTES + 1] = MLDSA_L;
31-
shake256(seedbuf, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, seedbuf,
29+
memcpy(inbuf, seed, MLDSA_SEEDBYTES);
30+
inbuf[MLDSA_SEEDBYTES + 0] = MLDSA_K;
31+
inbuf[MLDSA_SEEDBYTES + 1] = MLDSA_L;
32+
shake256(seedbuf, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, inbuf,
3233
MLDSA_SEEDBYTES + 2);
3334
rho = seedbuf;
3435
rhoprime = rho + MLDSA_SEEDBYTES;

mldsa/sign.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
#include <stddef.h>
99
#include <stdint.h>
10+
#include "cbmc.h"
1011
#include "common.h"
1112
#include "poly.h"
1213
#include "polyvec.h"
1314

14-
#define crypto_sign_keypair_internal MLD_NAMESPACE(keypair_internal)
15+
#define crypto_sign_keypair_internal MLD_NAMESPACE(crypto_sign_keypair_internal)
1516
/*************************************************
1617
* Name: crypto_sign_keypair_internal
1718
*
@@ -28,7 +29,14 @@
2829
* Returns 0 (success)
2930
**************************************************/
3031
int crypto_sign_keypair_internal(uint8_t *pk, uint8_t *sk,
31-
const uint8_t seed[MLDSA_SEEDBYTES]);
32+
const uint8_t seed[MLDSA_SEEDBYTES])
33+
__contract__(
34+
requires(memory_no_alias(pk, CRYPTO_PUBLICKEYBYTES))
35+
requires(memory_no_alias(sk, CRYPTO_SECRETKEYBYTES))
36+
requires(memory_no_alias(seed, MLDSA_SEEDBYTES))
37+
assigns(object_whole(pk))
38+
assigns(object_whole(sk))
39+
);
3240

3341
#define crypto_sign_keypair MLD_NAMESPACE(keypair)
3442
/*************************************************
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) The mldsa-native project authors
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
include ../Makefile_params.common
5+
6+
HARNESS_ENTRY = harness
7+
HARNESS_FILE = crypto_sign_keypair_internal_harness
8+
9+
# This should be a unique identifier for this proof, and will appear on the
10+
# Litani dashboard. It can be human-readable and contain spaces if you wish.
11+
PROOF_UID = crypto_sign_keypair_internal
12+
13+
DEFINES +=
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mldsa/sign.c
21+
22+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)crypto_sign_keypair_internal
23+
USE_FUNCTION_CONTRACTS=$(FIPS202_NAMESPACE)shake256 $(MLD_NAMESPACE)polyvec_matrix_expand \
24+
$(MLD_NAMESPACE)poly_uniform_eta_4x $(MLD_NAMESPACE)polyvecl_ntt \
25+
$(MLD_NAMESPACE)polyvec_matrix_pointwise_montgomery \
26+
$(MLD_NAMESPACE)polyveck_reduce $(MLD_NAMESPACE)polyveck_invntt_tomont \
27+
$(MLD_NAMESPACE)polyveck_add $(MLD_NAMESPACE)polyveck_caddq \
28+
$(MLD_NAMESPACE)polyveck_power2round $(MLD_NAMESPACE)pack_pk \
29+
$(MLD_NAMESPACE)pack_sk
30+
APPLY_LOOP_CONTRACTS=on
31+
USE_DYNAMIC_FRAMES=1
32+
33+
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
34+
EXTERNAL_SAT_SOLVER=
35+
CBMCFLAGS=--smt2
36+
37+
FUNCTION_NAME = crypto_sign_keypair_internal
38+
39+
# If this proof is found to consume huge amounts of RAM, you can set the
40+
# EXPENSIVE variable. With new enough versions of the proof tools, this will
41+
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
42+
# documentation in Makefile.common under the "Job Pools" heading for details.
43+
# EXPENSIVE = true
44+
45+
# This function is large enough to need...
46+
CBMC_OBJECT_BITS = 9
47+
48+
# If you require access to a file-local ("static") function or object to conduct
49+
# your proof, set the following (and do not include the original source file
50+
# ("mldsa/poly.c") in PROJECT_SOURCES).
51+
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
52+
# include ../Makefile.common
53+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mldsa/poly.c
54+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
55+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
56+
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
57+
# be set before including Makefile.common, but any use of variables on the
58+
# left-hand side requires those variables to be defined. Hence, _SOURCE,
59+
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.
60+
61+
include ../Makefile.common
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include "sign.h"
5+
6+
void harness(void)
7+
{
8+
uint8_t *a, *b, *c;
9+
crypto_sign_keypair_internal(a, b, c);
10+
}

0 commit comments

Comments
 (0)