| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/* References |
| 7 |
* ========== |
| 8 |
* |
| 9 |
* - [FIPS140_3_IG] |
| 10 |
* Implementation Guidance for FIPS 140-3 and the Cryptographic Module |
| 11 |
* Validation Program |
| 12 |
* National Institute of Standards and Technology |
| 13 |
* https://csrc.nist.gov/projects/cryptographic-module-validation-program/fips-140-3-ig-announcements |
| 14 |
* |
| 15 |
* - [FIPS204] |
| 16 |
* FIPS 204 Module-Lattice-Based Digital Signature Standard |
| 17 |
* National Institute of Standards and Technology |
| 18 |
* https://csrc.nist.gov/pubs/fips/204/final |
| 19 |
* |
| 20 |
* - [Round3_Spec] |
| 21 |
* CRYSTALS-Dilithium Algorithm Specifications and Supporting Documentation |
| 22 |
* (Version 3.1) |
| 23 |
* Bai, Ducas, Kiltz, Lepoint, Lyubashevsky, Schwabe, Seiler, Stehlé |
| 24 |
* https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdint.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "cbmc.h" |
| 31 |
#include "ct.h" |
| 32 |
#include "debug.h" |
| 33 |
#include "packing.h" |
| 34 |
#include "poly.h" |
| 35 |
#include "poly_kl.h" |
| 36 |
#include "polyvec.h" |
| 37 |
#include "randombytes.h" |
| 38 |
#include "sign.h" |
| 39 |
#include "symmetric.h" |
| 40 |
|
| 41 |
/* Parameter set namespacing |
| 42 |
* This is to facilitate building multiple instances |
| 43 |
* of mldsa-native (e.g. with varying parameter sets) |
| 44 |
* within a single compilation unit. */ |
| 45 |
#define mld_check_pct MLD_ADD_PARAM_SET(mld_check_pct) MLD_CONTEXT_PARAMETERS_2 |
| 46 |
#define mld_sample_s1_s2 MLD_ADD_PARAM_SET(mld_sample_s1_s2) |
| 47 |
#define mld_validate_hash_length MLD_ADD_PARAM_SET(mld_validate_hash_length) |
| 48 |
#define mld_get_hash_oid MLD_ADD_PARAM_SET(mld_get_hash_oid) |
| 49 |
#define mld_H MLD_ADD_PARAM_SET(mld_H) |
| 50 |
#define mld_compute_pack_z MLD_ADD_PARAM_SET(mld_compute_pack_z) |
| 51 |
#define mld_attempt_signature_generation \ |
| 52 |
MLD_ADD_PARAM_SET(mld_attempt_signature_generation) MLD_CONTEXT_PARAMETERS_8 |
| 53 |
#define mld_compute_t0_t1_tr_from_sk_components \ |
| 54 |
MLD_ADD_PARAM_SET(mld_compute_t0_t1_tr_from_sk_components) \ |
| 55 |
MLD_CONTEXT_PARAMETERS_7 |
| 56 |
/* End of parameter set namespacing */ |
| 57 |
|
| 58 |
|
| 59 |
static int mld_check_pct(uint8_t const pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 60 |
uint8_t const sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 61 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 62 |
__contract__( |
| 63 |
requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES)) |
| 64 |
requires(memory_no_alias(sk, MLDSA_CRYPTO_SECRETKEYBYTES)) |
| 65 |
ensures(return_value == 0 |
| 66 |
|| return_value == MLD_ERR_FAIL |
| 67 |
|| return_value == MLD_ERR_OUT_OF_MEMORY |
| 68 |
|| return_value == MLD_ERR_RNG_FAIL) |
| 69 |
); |
| 70 |
|
| 71 |
#if defined(MLD_CONFIG_KEYGEN_PCT) |
| 72 |
/************************************************* |
| 73 |
* @[FIPS140_3_IG] |
| 74 |
* (https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf) |
| 75 |
* |
| 76 |
* TE10.35.02: Pair-wise Consistency Test (PCT) for DSA keypairs |
| 77 |
* |
| 78 |
* Purpose: Validates that a generated public/private key pair can correctly |
| 79 |
* sign and verify data. Test performs signature generation using the private |
| 80 |
* key (sk), followed by signature verification using the public key (pk). |
| 81 |
* Returns 0 if the signature was successfully verified, non-zero if it cannot. |
| 82 |
* |
| 83 |
* Note: @[FIPS204] requires that public/private key pairs are to be used only |
| 84 |
* for the calculation and/of verification of digital signatures. |
| 85 |
**************************************************/ |
| 86 |
static int mld_check_pct(uint8_t const pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 87 |
uint8_t const sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 88 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 89 |
{ |
| 90 |
MLD_ALIGN uint8_t message[1] = {0}; |
| 91 |
size_t siglen; |
| 92 |
int ret; |
| 93 |
MLD_ALLOC(signature, uint8_t, MLDSA_CRYPTO_BYTES, context); |
| 94 |
MLD_ALLOC(pk_test, uint8_t, MLDSA_CRYPTO_PUBLICKEYBYTES, context); |
| 95 |
|
| 96 |
if (signature == NULL || pk_test == NULL) |
| 97 |
{ |
| 98 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 99 |
goto cleanup; |
| 100 |
} |
| 101 |
|
| 102 |
/* Copy public key for testing */ |
| 103 |
mld_memcpy(pk_test, pk, MLDSA_CRYPTO_PUBLICKEYBYTES); |
| 104 |
|
| 105 |
/* Sign a test message using the original secret key */ |
| 106 |
ret = mld_sign_signature(signature, &siglen, message, sizeof(message), NULL, |
| 107 |
0, sk, context); |
| 108 |
if (ret != 0) |
| 109 |
{ |
| 110 |
goto cleanup; |
| 111 |
} |
| 112 |
|
| 113 |
#if defined(MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST) |
| 114 |
/* Deliberately break public key for testing purposes */ |
| 115 |
if (mld_break_pct()) |
| 116 |
{ |
| 117 |
pk_test[0] = ~pk_test[0]; |
| 118 |
} |
| 119 |
#endif /* MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST */ |
| 120 |
|
| 121 |
/* Verify the signature using the (potentially corrupted) public key */ |
| 122 |
ret = mld_sign_verify(signature, siglen, message, sizeof(message), NULL, 0, |
| 123 |
pk_test, context); |
| 124 |
|
| 125 |
cleanup: |
| 126 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 127 |
MLD_FREE(pk_test, uint8_t, MLDSA_CRYPTO_PUBLICKEYBYTES, context); |
| 128 |
MLD_FREE(signature, uint8_t, MLDSA_CRYPTO_BYTES, context); |
| 129 |
|
| 130 |
return ret; |
| 131 |
} |
| 132 |
#else /* MLD_CONFIG_KEYGEN_PCT */ |
| 133 |
static int mld_check_pct(uint8_t const pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 134 |
uint8_t const sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 135 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 136 |
{ |
| 137 |
/* Skip PCT */ |
| 138 |
((void)pk); |
| 139 |
((void)sk); |
| 140 |
#if defined(MLD_CONFIG_CONTEXT_PARAMETER) |
| 141 |
((void)context); |
| 142 |
#endif |
| 143 |
return 0; |
| 144 |
} |
| 145 |
#endif /* !MLD_CONFIG_KEYGEN_PCT */ |
| 146 |
|
| 147 |
static void mld_sample_s1_s2(mld_polyvecl *s1, mld_polyveck *s2, |
| 148 |
const uint8_t seed[MLDSA_CRHBYTES]) |
| 149 |
__contract__( |
| 150 |
requires(memory_no_alias(s1, sizeof(mld_polyvecl))) |
| 151 |
requires(memory_no_alias(s2, sizeof(mld_polyveck))) |
| 152 |
requires(memory_no_alias(seed, MLDSA_CRHBYTES)) |
| 153 |
assigns(object_whole(s1), object_whole(s2)) |
| 154 |
ensures(forall(l0, 0, MLDSA_L, array_abs_bound(s1->vec[l0].coeffs, 0, MLDSA_N, MLDSA_ETA + 1))) |
| 155 |
ensures(forall(k0, 0, MLDSA_K, array_abs_bound(s2->vec[k0].coeffs, 0, MLDSA_N, MLDSA_ETA + 1))) |
| 156 |
) |
| 157 |
{ |
| 158 |
/* Sample short vectors s1 and s2 */ |
| 159 |
#if defined(MLD_CONFIG_SERIAL_FIPS202_ONLY) |
| 160 |
int i; |
| 161 |
uint16_t nonce = 0; |
| 162 |
/* Safety: The nonces are at most 14 (MLDSA_L + MLDSA_K - 1), and, hence, the |
| 163 |
* casts are safe. */ |
| 164 |
for (i = 0; i < MLDSA_L; i++) |
| 165 |
{ |
| 166 |
mld_poly_uniform_eta(&s1->vec[i], seed, (uint8_t)(nonce + i)); |
| 167 |
} |
| 168 |
for (i = 0; i < MLDSA_K; i++) |
| 169 |
{ |
| 170 |
mld_poly_uniform_eta(&s2->vec[i], seed, (uint8_t)(nonce + MLDSA_L + i)); |
| 171 |
} |
| 172 |
#else /* MLD_CONFIG_SERIAL_FIPS202_ONLY */ |
| 173 |
#if MLD_CONFIG_PARAMETER_SET == 44 |
| 174 |
mld_poly_uniform_eta_4x(&s1->vec[0], &s1->vec[1], &s1->vec[2], &s1->vec[3], |
| 175 |
seed, 0, 1, 2, 3); |
| 176 |
mld_poly_uniform_eta_4x(&s2->vec[0], &s2->vec[1], &s2->vec[2], &s2->vec[3], |
| 177 |
seed, 4, 5, 6, 7); |
| 178 |
#elif MLD_CONFIG_PARAMETER_SET == 65 |
| 179 |
mld_poly_uniform_eta_4x(&s1->vec[0], &s1->vec[1], &s1->vec[2], &s1->vec[3], |
| 180 |
seed, 0, 1, 2, 3); |
| 181 |
mld_poly_uniform_eta_4x(&s1->vec[4], &s2->vec[0], &s2->vec[1], |
| 182 |
&s2->vec[2] /* irrelevant */, seed, 4, 5, 6, |
| 183 |
0xFF /* irrelevant */); |
| 184 |
mld_poly_uniform_eta_4x(&s2->vec[2], &s2->vec[3], &s2->vec[4], &s2->vec[5], |
| 185 |
seed, 7, 8, 9, 10); |
| 186 |
#elif MLD_CONFIG_PARAMETER_SET == 87 |
| 187 |
mld_poly_uniform_eta_4x(&s1->vec[0], &s1->vec[1], &s1->vec[2], &s1->vec[3], |
| 188 |
seed, 0, 1, 2, 3); |
| 189 |
mld_poly_uniform_eta_4x(&s1->vec[4], &s1->vec[5], &s1->vec[6], |
| 190 |
&s2->vec[0] /* irrelevant */, seed, 4, 5, 6, |
| 191 |
0xFF /* irrelevant */); |
| 192 |
mld_poly_uniform_eta_4x(&s2->vec[0], &s2->vec[1], &s2->vec[2], &s2->vec[3], |
| 193 |
seed, 7, 8, 9, 10); |
| 194 |
mld_poly_uniform_eta_4x(&s2->vec[4], &s2->vec[5], &s2->vec[6], &s2->vec[7], |
| 195 |
seed, 11, 12, 13, 14); |
| 196 |
#endif /* MLD_CONFIG_PARAMETER_SET == 87 */ |
| 197 |
#endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY */ |
| 198 |
} |
| 199 |
|
| 200 |
/************************************************* |
| 201 |
* Name: mld_compute_t0_t1_tr_from_sk_components |
| 202 |
* |
| 203 |
* Description: Computes t0, t1, tr, and pk from secret key components |
| 204 |
* rho, s1, s2. This is the shared computation used by |
| 205 |
* both keygen and generating the public key from the |
| 206 |
* secret key. |
| 207 |
* |
| 208 |
* Arguments: - mld_polyveck *t0: output t0 |
| 209 |
* - mld_polyveck *t1: output t1 |
| 210 |
* - uint8_t tr[MLDSA_TRBYTES]: output tr |
| 211 |
* - uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]: output public key |
| 212 |
* - const uint8_t rho[MLDSA_SEEDBYTES]: input rho |
| 213 |
* - const mld_polyvecl *s1: input s1 |
| 214 |
* - const mld_polyveck *s2: input s2 |
| 215 |
**************************************************/ |
| 216 |
MLD_MUST_CHECK_RETURN_VALUE |
| 217 |
static int mld_compute_t0_t1_tr_from_sk_components( |
| 218 |
mld_polyveck *t0, mld_polyveck *t1, uint8_t tr[MLDSA_TRBYTES], |
| 219 |
uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], const uint8_t rho[MLDSA_SEEDBYTES], |
| 220 |
const mld_polyvecl *s1, const mld_polyveck *s2, |
| 221 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 222 |
__contract__( |
| 223 |
requires(memory_no_alias(t0, sizeof(mld_polyveck))) |
| 224 |
requires(memory_no_alias(t1, sizeof(mld_polyveck))) |
| 225 |
requires(memory_no_alias(tr, MLDSA_TRBYTES)) |
| 226 |
requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES)) |
| 227 |
requires(memory_no_alias(rho, MLDSA_SEEDBYTES)) |
| 228 |
requires(memory_no_alias(s1, sizeof(mld_polyvecl))) |
| 229 |
requires(memory_no_alias(s2, sizeof(mld_polyveck))) |
| 230 |
requires(forall(l0, 0, MLDSA_L, array_bound(s1->vec[l0].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1))) |
| 231 |
requires(forall(k0, 0, MLDSA_K, array_bound(s2->vec[k0].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1))) |
| 232 |
assigns(memory_slice(t0, sizeof(mld_polyveck))) |
| 233 |
assigns(memory_slice(t1, sizeof(mld_polyveck))) |
| 234 |
assigns(memory_slice(tr, MLDSA_TRBYTES)) |
| 235 |
assigns(memory_slice(pk, MLDSA_CRYPTO_PUBLICKEYBYTES)) |
| 236 |
ensures(forall(k1, 0, MLDSA_K, array_bound(t0->vec[k1].coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))) |
| 237 |
ensures(forall(k2, 0, MLDSA_K, array_bound(t1->vec[k2].coeffs, 0, MLDSA_N, 0, 1 << 10))) |
| 238 |
ensures(return_value == 0 || return_value == MLD_ERR_OUT_OF_MEMORY)) |
| 239 |
{ |
| 240 |
int ret; |
| 241 |
MLD_ALLOC(mat, mld_polymat, 1, context); |
| 242 |
MLD_ALLOC(s1hat, mld_polyvecl, 1, context); |
| 243 |
MLD_ALLOC(t, mld_polyveck, 1, context); |
| 244 |
|
| 245 |
if (mat == NULL || s1hat == NULL || t == NULL) |
| 246 |
{ |
| 247 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 248 |
goto cleanup; |
| 249 |
} |
| 250 |
|
| 251 |
/* Expand matrix */ |
| 252 |
mld_polyvec_matrix_expand(mat, rho); |
| 253 |
|
| 254 |
/* Matrix-vector multiplication */ |
| 255 |
*s1hat = *s1; |
| 256 |
mld_polyvecl_ntt(s1hat); |
| 257 |
mld_polyvec_matrix_pointwise_montgomery(t, mat, s1hat); |
| 258 |
mld_polyveck_invntt_tomont(t); |
| 259 |
|
| 260 |
/* Add error vector s2 */ |
| 261 |
mld_polyveck_add(t, s2); |
| 262 |
|
| 263 |
/* Reference: The following reduction is not present in the reference |
| 264 |
* implementation. Omitting this reduction requires the output of |
| 265 |
* the invntt to be small enough such that the addition of s2 does |
| 266 |
* not result in absolute values >= MLDSA_Q. While our C, x86_64, |
| 267 |
* and AArch64 invntt implementations produce small enough |
| 268 |
* values for this to work out, it complicates the bounds |
| 269 |
* reasoning. We instead add an additional reduction, and can |
| 270 |
* consequently, relax the bounds requirements for the invntt. |
| 271 |
*/ |
| 272 |
mld_polyveck_reduce(t); |
| 273 |
|
| 274 |
/* Decompose to get t1, t0 */ |
| 275 |
mld_polyveck_caddq(t); |
| 276 |
mld_polyveck_power2round(t1, t0, t); |
| 277 |
|
| 278 |
/* Pack public key and compute tr */ |
| 279 |
mld_pack_pk(pk, rho, t1); |
| 280 |
mld_shake256(tr, MLDSA_TRBYTES, pk, MLDSA_CRYPTO_PUBLICKEYBYTES); |
| 281 |
|
| 282 |
ret = 0; |
| 283 |
|
| 284 |
cleanup: |
| 285 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 286 |
MLD_FREE(t, mld_polyveck, 1, context); |
| 287 |
MLD_FREE(s1hat, mld_polyvecl, 1, context); |
| 288 |
MLD_FREE(mat, mld_polymat, 1, context); |
| 289 |
return ret; |
| 290 |
} |
| 291 |
|
| 292 |
MLD_MUST_CHECK_RETURN_VALUE |
| 293 |
MLD_EXTERNAL_API |
| 294 |
int mld_sign_keypair_internal(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 295 |
uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 296 |
const uint8_t seed[MLDSA_SEEDBYTES], |
| 297 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 298 |
{ |
| 299 |
int ret; |
| 300 |
const uint8_t *rho, *rhoprime, *key; |
| 301 |
MLD_ALLOC(seedbuf, uint8_t, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, context); |
| 302 |
MLD_ALLOC(inbuf, uint8_t, MLDSA_SEEDBYTES + 2, context); |
| 303 |
MLD_ALLOC(tr, uint8_t, MLDSA_TRBYTES, context); |
| 304 |
MLD_ALLOC(s1, mld_polyvecl, 1, context); |
| 305 |
MLD_ALLOC(s2, mld_polyveck, 1, context); |
| 306 |
MLD_ALLOC(t1, mld_polyveck, 1, context); |
| 307 |
MLD_ALLOC(t0, mld_polyveck, 1, context); |
| 308 |
|
| 309 |
if (seedbuf == NULL || inbuf == NULL || tr == NULL || s1 == NULL || |
| 310 |
s2 == NULL || t1 == NULL || t0 == NULL) |
| 311 |
{ |
| 312 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 313 |
goto cleanup; |
| 314 |
} |
| 315 |
|
| 316 |
/* Get randomness for rho, rhoprime and key */ |
| 317 |
mld_memcpy(inbuf, seed, MLDSA_SEEDBYTES); |
| 318 |
inbuf[MLDSA_SEEDBYTES + 0] = MLDSA_K; |
| 319 |
inbuf[MLDSA_SEEDBYTES + 1] = MLDSA_L; |
| 320 |
mld_shake256(seedbuf, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, inbuf, |
| 321 |
MLDSA_SEEDBYTES + 2); |
| 322 |
rho = seedbuf; |
| 323 |
rhoprime = rho + MLDSA_SEEDBYTES; |
| 324 |
key = rhoprime + MLDSA_CRHBYTES; |
| 325 |
|
| 326 |
/* Constant time: rho is part of the public key and, hence, public. */ |
| 327 |
MLD_CT_TESTING_DECLASSIFY(rho, MLDSA_SEEDBYTES); |
| 328 |
|
| 329 |
/* Sample s1 and s2 */ |
| 330 |
mld_sample_s1_s2(s1, s2, rhoprime); |
| 331 |
|
| 332 |
/* Compute t0, t1, tr, and pk from rho, s1, s2 */ |
| 333 |
ret = mld_compute_t0_t1_tr_from_sk_components(t0, t1, tr, pk, rho, s1, s2, |
| 334 |
context); |
| 335 |
if (ret != 0) |
| 336 |
{ |
| 337 |
goto cleanup; |
| 338 |
} |
| 339 |
|
| 340 |
/* Pack secret key */ |
| 341 |
mld_pack_sk(sk, rho, tr, key, t0, s1, s2); |
| 342 |
|
| 343 |
/* Constant time: pk is the public key, inherently public data */ |
| 344 |
MLD_CT_TESTING_DECLASSIFY(pk, MLDSA_CRYPTO_PUBLICKEYBYTES); |
| 345 |
|
| 346 |
cleanup: |
| 347 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 348 |
MLD_FREE(t0, mld_polyveck, 1, context); |
| 349 |
MLD_FREE(t1, mld_polyveck, 1, context); |
| 350 |
MLD_FREE(s2, mld_polyveck, 1, context); |
| 351 |
MLD_FREE(s1, mld_polyvecl, 1, context); |
| 352 |
MLD_FREE(tr, uint8_t, MLDSA_TRBYTES, context); |
| 353 |
MLD_FREE(inbuf, uint8_t, MLDSA_SEEDBYTES + 2, context); |
| 354 |
MLD_FREE(seedbuf, uint8_t, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, context); |
| 355 |
|
| 356 |
if (ret != 0) |
| 357 |
{ |
| 358 |
return ret; |
| 359 |
} |
| 360 |
|
| 361 |
/* Pairwise Consistency Test (PCT) @[FIPS140_3_IG, p.87] */ |
| 362 |
/* Do this after freeing all temporaries. */ |
| 363 |
return mld_check_pct(pk, sk, context); |
| 364 |
} |
| 365 |
|
| 366 |
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API) |
| 367 |
MLD_MUST_CHECK_RETURN_VALUE |
| 368 |
MLD_EXTERNAL_API |
| 369 |
int mld_sign_keypair(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 370 |
uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 371 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 372 |
{ |
| 373 |
MLD_ALIGN uint8_t seed[MLDSA_SEEDBYTES]; |
| 374 |
int ret; |
| 375 |
if (mld_randombytes(seed, MLDSA_SEEDBYTES) != 0) |
| 376 |
{ |
| 377 |
ret = MLD_ERR_RNG_FAIL; |
| 378 |
goto cleanup; |
| 379 |
} |
| 380 |
MLD_CT_TESTING_SECRET(seed, sizeof(seed)); |
| 381 |
ret = mld_sign_keypair_internal(pk, sk, seed, context); |
| 382 |
|
| 383 |
cleanup: |
| 384 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 385 |
mld_zeroize(seed, sizeof(seed)); |
| 386 |
return ret; |
| 387 |
} |
| 388 |
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */ |
| 389 |
|
| 390 |
/************************************************* |
| 391 |
* Name: mld_H |
| 392 |
* |
| 393 |
* Description: Abstracts application of SHAKE256 to |
| 394 |
* one, two or three blocks of data, |
| 395 |
* yielding a user-requested size of |
| 396 |
* output. |
| 397 |
* |
| 398 |
* Arguments: - uint8_t *out: pointer to output |
| 399 |
* - size_t outlen: requested output length in bytes |
| 400 |
* - const uint8_t *in1: pointer to input block 1 |
| 401 |
* Must NOT be NULL |
| 402 |
* - size_t in1len: length of input in1 bytes |
| 403 |
* - const uint8_t *in2: pointer to input block 2 |
| 404 |
* May be NULL if in2len=0, in which case |
| 405 |
* this block is ignored |
| 406 |
* - size_t in2len: length of input in2 bytes |
| 407 |
* - const uint8_t *in3: pointer to input block 3 |
| 408 |
* May be NULL if in3len=0, in which case |
| 409 |
* this block is ignored |
| 410 |
* - size_t in3len: length of input in3 bytes |
| 411 |
**************************************************/ |
| 412 |
static void mld_H(uint8_t *out, size_t outlen, const uint8_t *in1, |
| 413 |
size_t in1len, const uint8_t *in2, size_t in2len, |
| 414 |
const uint8_t *in3, size_t in3len) |
| 415 |
__contract__( |
| 416 |
requires(in1len <= MLD_MAX_BUFFER_SIZE) |
| 417 |
requires(in2len <= MLD_MAX_BUFFER_SIZE) |
| 418 |
requires(in3len <= MLD_MAX_BUFFER_SIZE) |
| 419 |
requires(outlen <= 8 * SHAKE256_RATE /* somewhat arbitrary bound */) |
| 420 |
requires(memory_no_alias(in1, in1len)) |
| 421 |
requires(in2len == 0 || memory_no_alias(in2, in2len)) |
| 422 |
requires(in3len == 0 || memory_no_alias(in3, in3len)) |
| 423 |
requires(memory_no_alias(out, outlen)) |
| 424 |
assigns(memory_slice(out, outlen)) |
| 425 |
) |
| 426 |
{ |
| 427 |
mld_shake256ctx state; |
| 428 |
mld_shake256_init(&state); |
| 429 |
mld_shake256_absorb(&state, in1, in1len); |
| 430 |
if (in2len != 0) |
| 431 |
{ |
| 432 |
mld_shake256_absorb(&state, in2, in2len); |
| 433 |
} |
| 434 |
if (in3len != 0) |
| 435 |
{ |
| 436 |
mld_shake256_absorb(&state, in3, in3len); |
| 437 |
} |
| 438 |
mld_shake256_finalize(&state); |
| 439 |
mld_shake256_squeeze(out, outlen, &state); |
| 440 |
mld_shake256_release(&state); |
| 441 |
|
| 442 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 443 |
mld_zeroize(&state, sizeof(state)); |
| 444 |
} |
| 445 |
|
| 446 |
/************************************************* |
| 447 |
* Name: mld_compute_pack_z |
| 448 |
* |
| 449 |
* Description: Computes z = y + s1*c, checks that z has coefficients smaller |
| 450 |
* than MLDSA_GAMMA1 - MLDSA_BETA, and packs z into the |
| 451 |
* signature buffer. |
| 452 |
* |
| 453 |
* Arguments: - uint8_t *sig: output signature |
| 454 |
* - const mld_poly *cp: challenge polynomial |
| 455 |
* - const polyvecl *s1: secret vector s1 |
| 456 |
* - const polyvecl *y: masking vector y |
| 457 |
* |
| 458 |
* Returns: - 0: Success (z has coefficients smaller than |
| 459 |
* MLDSA_GAMMA1 - MLDSA_BETA,) |
| 460 |
* - MLD_ERR_FAIL: z rejected (norm check failed) |
| 461 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 462 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 463 |
* |
| 464 |
* Reference: This function is inlined into mld_sign_signature in the |
| 465 |
* reference implementation. |
| 466 |
**************************************************/ |
| 467 |
MLD_MUST_CHECK_RETURN_VALUE |
| 468 |
static int mld_compute_pack_z(uint8_t sig[MLDSA_CRYPTO_BYTES], |
| 469 |
const mld_poly *cp, const mld_polyvecl *s1, |
| 470 |
const mld_polyvecl *y, mld_poly *z) |
| 471 |
__contract__( |
| 472 |
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES)) |
| 473 |
requires(memory_no_alias(cp, sizeof(mld_poly))) |
| 474 |
requires(memory_no_alias(s1, sizeof(mld_polyvecl))) |
| 475 |
requires(memory_no_alias(y, sizeof(mld_polyvecl))) |
| 476 |
requires(memory_no_alias(z, sizeof(mld_poly))) |
| 477 |
requires(array_abs_bound(cp->coeffs, 0, MLDSA_N, MLD_NTT_BOUND)) |
| 478 |
requires(forall(k0, 0, MLDSA_L, |
| 479 |
array_bound(y->vec[k0].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))) |
| 480 |
requires(forall(k1, 0, MLDSA_L, array_abs_bound(s1->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 481 |
assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES)) |
| 482 |
assigns(memory_slice(z, sizeof(mld_poly))) |
| 483 |
ensures(return_value == 0 || return_value == MLD_ERR_FAIL || |
| 484 |
return_value == MLD_ERR_OUT_OF_MEMORY) |
| 485 |
) |
| 486 |
{ |
| 487 |
unsigned int i; |
| 488 |
uint32_t z_invalid; |
| 489 |
for (i = 0; i < MLDSA_L; i++) |
| 490 |
__loop__( |
| 491 |
assigns(i, memory_slice(z, sizeof(mld_poly)), memory_slice(sig, MLDSA_CRYPTO_BYTES)) |
| 492 |
invariant(i <= MLDSA_L) |
| 493 |
) |
| 494 |
{ |
| 495 |
mld_poly_pointwise_montgomery(z, cp, &s1->vec[i]); |
| 496 |
mld_poly_invntt_tomont(z); |
| 497 |
mld_poly_add(z, &y->vec[i]); |
| 498 |
mld_poly_reduce(z); |
| 499 |
|
| 500 |
z_invalid = mld_poly_chknorm(z, MLDSA_GAMMA1 - MLDSA_BETA); |
| 501 |
/* Constant time: It is fine (and prohibitively expensive to avoid) |
| 502 |
* to leak the result of the norm check and which polynomial in z caused a |
| 503 |
* rejection. It would even be okay to leak which coefficient led to |
| 504 |
* rejection as the candidate signature will be discarded anyway. |
| 505 |
* See Section 5.5 of @[Round3_Spec]. */ |
| 506 |
MLD_CT_TESTING_DECLASSIFY(&z_invalid, sizeof(uint32_t)); |
| 507 |
if (z_invalid) |
| 508 |
{ |
| 509 |
return MLD_ERR_FAIL; /* reject */ |
| 510 |
} |
| 511 |
/* If z is valid, then its coefficients are bounded by |
| 512 |
* MLDSA_GAMMA1 - MLDSA_BETA. This will be needed below |
| 513 |
* to prove the pre-condition of pack_sig_z() */ |
| 514 |
mld_assert_abs_bound(z, MLDSA_N, (MLDSA_GAMMA1 - MLDSA_BETA)); |
| 515 |
|
| 516 |
/* After the norm check, the distribution of each coefficient of z is |
| 517 |
* independent of the secret key and it can, hence, be considered |
| 518 |
* public. It is, hence, okay to immediately pack it into the user-provided |
| 519 |
* signature buffer. */ |
| 520 |
mld_pack_sig_z(sig, z, i); |
| 521 |
} |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
/* Reference: The reference implementation does not explicitly check the |
| 526 |
* maximum nonce value, but instead loops indefinitely (even when the nonce |
| 527 |
* would overflow). Internally, sampling of y uses |
| 528 |
* (nonceL), (nonceL+1), ... (nonce*L+L-1). |
| 529 |
* Hence, there are no overflows if nonce < (UINT16_MAX - L)/L. |
| 530 |
* Explicitly checking for this explicitly allows us to prove type-safety. |
| 531 |
* Note that FIPS204 explicitly allows an upper-bound this loop of |
| 532 |
* 814 (< (UINT16_MAX - L)/L) - see @[FIPS204, Appendix C]. */ |
| 533 |
#define MLD_NONCE_UB ((UINT16_MAX - MLDSA_L) / MLDSA_L) |
| 534 |
|
| 535 |
/************************************************* |
| 536 |
* Name: attempt_signature_generation |
| 537 |
* |
| 538 |
* Description: Attempts to generate a single signature. |
| 539 |
* |
| 540 |
* Arguments: - uint8_t *sig: pointer to output signature |
| 541 |
* - const uint8_t *mu: pointer to message or hash |
| 542 |
* of exactly MLDSA_CRHBYTES bytes |
| 543 |
* - const uint8_t *rhoprime: pointer to randomness seed |
| 544 |
* - uint16_t nonce: current nonce value |
| 545 |
* - const mld_polymat *mat: expanded matrix |
| 546 |
* - const polyvecl *s1: secret vector s1 |
| 547 |
* - const polyveck *s2: secret vector s2 |
| 548 |
* - const polyveck *t0: vector t0 |
| 549 |
* |
| 550 |
* Returns: - 0: Signature generation succeeded |
| 551 |
* - MLD_ERR_FAIL: Signature rejected (norm check failed) |
| 552 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 553 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 554 |
* |
| 555 |
* Reference: This code differs from the reference implementation |
| 556 |
* in that it factors out the core signature generation |
| 557 |
* step into a distinct function here in order to improve |
| 558 |
* efficiency of CBMC proof. |
| 559 |
**************************************************/ |
| 560 |
MLD_MUST_CHECK_RETURN_VALUE |
| 561 |
static int mld_attempt_signature_generation( |
| 562 |
uint8_t sig[MLDSA_CRYPTO_BYTES], const uint8_t *mu, |
| 563 |
const uint8_t rhoprime[MLDSA_CRHBYTES], uint16_t nonce, mld_polymat *mat, |
| 564 |
const mld_polyvecl *s1, const mld_polyveck *s2, const mld_polyveck *t0, |
| 565 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 566 |
__contract__( |
| 567 |
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES)) |
| 568 |
requires(memory_no_alias(mu, MLDSA_CRHBYTES)) |
| 569 |
requires(memory_no_alias(rhoprime, MLDSA_CRHBYTES)) |
| 570 |
requires(memory_no_alias(mat, sizeof(mld_polymat))) |
| 571 |
requires(memory_no_alias(s1, sizeof(mld_polyvecl))) |
| 572 |
requires(memory_no_alias(s2, sizeof(mld_polyveck))) |
| 573 |
requires(memory_no_alias(t0, sizeof(mld_polyveck))) |
| 574 |
requires(nonce <= MLD_NONCE_UB) |
| 575 |
requires(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L, |
| 576 |
array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))) |
| 577 |
requires(forall(k2, 0, MLDSA_K, array_abs_bound(t0->vec[k2].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 578 |
requires(forall(k3, 0, MLDSA_L, array_abs_bound(s1->vec[k3].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 579 |
requires(forall(k4, 0, MLDSA_K, array_abs_bound(s2->vec[k4].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 580 |
assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES)) |
| 581 |
ensures(return_value == 0 || return_value == MLD_ERR_FAIL || |
| 582 |
return_value == MLD_ERR_OUT_OF_MEMORY) |
| 583 |
) |
| 584 |
{ |
| 585 |
unsigned int n; |
| 586 |
uint32_t w0_invalid, h_invalid; |
| 587 |
int ret; |
| 588 |
/* TODO: Remove the following workaround for |
| 589 |
* https://github.com/diffblue/cbmc/issues/8813 */ |
| 590 |
typedef MLD_UNION_OR_STRUCT |
| 591 |
{ |
| 592 |
mld_polyvecl y; |
| 593 |
mld_polyveck h; |
| 594 |
} |
| 595 |
yh_u; |
| 596 |
mld_polyvecl *y; |
| 597 |
mld_polyveck *h; |
| 598 |
|
| 599 |
/* TODO: Remove the following workaround for |
| 600 |
* https://github.com/diffblue/cbmc/issues/8813 */ |
| 601 |
typedef MLD_UNION_OR_STRUCT |
| 602 |
{ |
| 603 |
mld_polyveck w1; |
| 604 |
mld_polyvecl tmp; |
| 605 |
} |
| 606 |
w1tmp_u; |
| 607 |
mld_polyveck *w1; |
| 608 |
mld_polyvecl *tmp; |
| 609 |
|
| 610 |
MLD_ALLOC(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES, context); |
| 611 |
MLD_ALLOC(yh, yh_u, 1, context); |
| 612 |
MLD_ALLOC(z, mld_poly, 1, context); |
| 613 |
MLD_ALLOC(w1tmp, w1tmp_u, 1, context); |
| 614 |
MLD_ALLOC(w0, mld_polyveck, 1, context); |
| 615 |
MLD_ALLOC(cp, mld_poly, 1, context); |
| 616 |
MLD_ALLOC(t, mld_poly, 1, context); |
| 617 |
|
| 618 |
if (challenge_bytes == NULL || yh == NULL || z == NULL || w1tmp == NULL || |
| 619 |
w0 == NULL || cp == NULL || t == NULL) |
| 620 |
{ |
| 621 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 622 |
goto cleanup; |
| 623 |
} |
| 624 |
y = &yh->y; |
| 625 |
h = &yh->h; |
| 626 |
w1 = &w1tmp->w1; |
| 627 |
tmp = &w1tmp->tmp; |
| 628 |
|
| 629 |
/* Sample intermediate vector y */ |
| 630 |
mld_polyvecl_uniform_gamma1(y, rhoprime, nonce); |
| 631 |
|
| 632 |
/* Matrix-vector multiplication */ |
| 633 |
*tmp = *y; |
| 634 |
mld_polyvecl_ntt(tmp); |
| 635 |
mld_polyvec_matrix_pointwise_montgomery(w0, mat, tmp); |
| 636 |
mld_polyveck_invntt_tomont(w0); |
| 637 |
|
| 638 |
/* Decompose w and call the random oracle */ |
| 639 |
mld_polyveck_caddq(w0); |
| 640 |
mld_polyveck_decompose(w1, w0); |
| 641 |
mld_polyveck_pack_w1(sig, w1); |
| 642 |
|
| 643 |
mld_H(challenge_bytes, MLDSA_CTILDEBYTES, mu, MLDSA_CRHBYTES, sig, |
| 644 |
MLDSA_K * MLDSA_POLYW1_PACKEDBYTES, NULL, 0); |
| 645 |
/* Constant time: Leaking challenge_bytes does not reveal any information |
| 646 |
* about the secret key as H() is modelled as random oracle. |
| 647 |
* This also applies to challenges for rejected signatures. |
| 648 |
* See Section 5.5 of @[Round3_Spec]. */ |
| 649 |
MLD_CT_TESTING_DECLASSIFY(challenge_bytes, MLDSA_CTILDEBYTES); |
| 650 |
mld_poly_challenge(cp, challenge_bytes); |
| 651 |
mld_poly_ntt(cp); |
| 652 |
|
| 653 |
/* Compute z, reject if it reveals secret */ |
| 654 |
ret = mld_compute_pack_z(sig, cp, s1, y, t); |
| 655 |
if (ret) |
| 656 |
{ |
| 657 |
goto cleanup; |
| 658 |
} |
| 659 |
|
| 660 |
/* Check that subtracting cs2 does not change high bits of w and low bits |
| 661 |
* do not reveal secret information */ |
| 662 |
mld_polyveck_pointwise_poly_montgomery(h, cp, s2); |
| 663 |
mld_polyveck_invntt_tomont(h); |
| 664 |
mld_polyveck_sub(w0, h); |
| 665 |
mld_polyveck_reduce(w0); |
| 666 |
|
| 667 |
w0_invalid = mld_polyveck_chknorm(w0, MLDSA_GAMMA2 - MLDSA_BETA); |
| 668 |
/* Constant time: w0_invalid may be leaked - see comment for z_invalid. */ |
| 669 |
MLD_CT_TESTING_DECLASSIFY(&w0_invalid, sizeof(uint32_t)); |
| 670 |
if (w0_invalid) |
| 671 |
{ |
| 672 |
ret = MLD_ERR_FAIL; /* reject */ |
| 673 |
goto cleanup; |
| 674 |
} |
| 675 |
|
| 676 |
/* Compute hints for w1 */ |
| 677 |
mld_polyveck_pointwise_poly_montgomery(h, cp, t0); |
| 678 |
mld_polyveck_invntt_tomont(h); |
| 679 |
mld_polyveck_reduce(h); |
| 680 |
|
| 681 |
h_invalid = mld_polyveck_chknorm(h, MLDSA_GAMMA2); |
| 682 |
/* Constant time: h_invalid may be leaked - see comment for z_invalid. */ |
| 683 |
MLD_CT_TESTING_DECLASSIFY(&h_invalid, sizeof(uint32_t)); |
| 684 |
if (h_invalid) |
| 685 |
{ |
| 686 |
ret = MLD_ERR_FAIL; /* reject */ |
| 687 |
goto cleanup; |
| 688 |
} |
| 689 |
|
| 690 |
mld_polyveck_add(w0, h); |
| 691 |
|
| 692 |
/* Constant time: At this point all norm checks have passed and we, hence, |
| 693 |
* know that the signature does not leak any secret information. |
| 694 |
* Consequently, any value that can be computed from the signature and public |
| 695 |
* key is considered public. |
| 696 |
* w0 and w1 are public as they can be computed from Az - ct = \alpha w1 + w0. |
| 697 |
* h=c*t0 is public as both c and t0 are public. |
| 698 |
* For a more detailed discussion, refer to https://eprint.iacr.org/2022/1406. |
| 699 |
*/ |
| 700 |
MLD_CT_TESTING_DECLASSIFY(w0, sizeof(*w0)); |
| 701 |
MLD_CT_TESTING_DECLASSIFY(w1, sizeof(*w1)); |
| 702 |
n = mld_polyveck_make_hint(h, w0, w1); |
| 703 |
if (n > MLDSA_OMEGA) |
| 704 |
{ |
| 705 |
ret = MLD_ERR_FAIL; /* reject */ |
| 706 |
goto cleanup; |
| 707 |
} |
| 708 |
|
| 709 |
/* All is well - write signature */ |
| 710 |
mld_pack_sig_c_h(sig, challenge_bytes, h, n); |
| 711 |
/* Constant time: At this point it is clear that the signature is valid - it |
| 712 |
* can, hence, be considered public. */ |
| 713 |
MLD_CT_TESTING_DECLASSIFY(sig, MLDSA_CRYPTO_BYTES); |
| 714 |
ret = 0; /* success */ |
| 715 |
|
| 716 |
cleanup: |
| 717 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 718 |
MLD_FREE(t, mld_poly, 1, context); |
| 719 |
MLD_FREE(cp, mld_poly, 1, context); |
| 720 |
MLD_FREE(w0, mld_polyveck, 1, context); |
| 721 |
MLD_FREE(w1tmp, w1tmp_u, 1, context); |
| 722 |
MLD_FREE(z, mld_poly, 1, context); |
| 723 |
MLD_FREE(yh, yh_u, 1, context); |
| 724 |
MLD_FREE(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES, context); |
| 725 |
|
| 726 |
return ret; |
| 727 |
} |
| 728 |
MLD_MUST_CHECK_RETURN_VALUE |
| 729 |
MLD_EXTERNAL_API |
| 730 |
int mld_sign_signature_internal(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen, |
| 731 |
const uint8_t *m, size_t mlen, |
| 732 |
const uint8_t *pre, size_t prelen, |
| 733 |
const uint8_t rnd[MLDSA_RNDBYTES], |
| 734 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 735 |
int externalmu, |
| 736 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 737 |
{ |
| 738 |
int ret; |
| 739 |
uint8_t *rho, *tr, *key, *mu, *rhoprime; |
| 740 |
uint16_t nonce = 0; |
| 741 |
MLD_ALLOC(seedbuf, uint8_t, |
| 742 |
2 * MLDSA_SEEDBYTES + MLDSA_TRBYTES + 2 * MLDSA_CRHBYTES, context); |
| 743 |
MLD_ALLOC(mat, mld_polymat, 1, context); |
| 744 |
MLD_ALLOC(s1, mld_polyvecl, 1, context); |
| 745 |
MLD_ALLOC(t0, mld_polyveck, 1, context); |
| 746 |
MLD_ALLOC(s2, mld_polyveck, 1, context); |
| 747 |
|
| 748 |
if (seedbuf == NULL || mat == NULL || s1 == NULL || t0 == NULL || s2 == NULL) |
| 749 |
{ |
| 750 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 751 |
goto cleanup; |
| 752 |
} |
| 753 |
|
| 754 |
rho = seedbuf; |
| 755 |
tr = rho + MLDSA_SEEDBYTES; |
| 756 |
key = tr + MLDSA_TRBYTES; |
| 757 |
mu = key + MLDSA_SEEDBYTES; |
| 758 |
rhoprime = mu + MLDSA_CRHBYTES; |
| 759 |
mld_unpack_sk(rho, tr, key, t0, s1, s2, sk); |
| 760 |
|
| 761 |
if (!externalmu) |
| 762 |
{ |
| 763 |
/* Compute mu = CRH(tr, pre, msg) */ |
| 764 |
mld_H(mu, MLDSA_CRHBYTES, tr, MLDSA_TRBYTES, pre, prelen, m, mlen); |
| 765 |
} |
| 766 |
else |
| 767 |
{ |
| 768 |
/* mu has been provided directly */ |
| 769 |
mld_memcpy(mu, m, MLDSA_CRHBYTES); |
| 770 |
} |
| 771 |
|
| 772 |
/* Compute rhoprime = CRH(key, rnd, mu) */ |
| 773 |
mld_H(rhoprime, MLDSA_CRHBYTES, key, MLDSA_SEEDBYTES, rnd, MLDSA_RNDBYTES, mu, |
| 774 |
MLDSA_CRHBYTES); |
| 775 |
|
| 776 |
/* Constant time: rho is part of the public key and, hence, public. */ |
| 777 |
MLD_CT_TESTING_DECLASSIFY(rho, MLDSA_SEEDBYTES); |
| 778 |
/* Expand matrix and transform vectors */ |
| 779 |
mld_polyvec_matrix_expand(mat, rho); |
| 780 |
mld_polyvecl_ntt(s1); |
| 781 |
mld_polyveck_ntt(s2); |
| 782 |
mld_polyveck_ntt(t0); |
| 783 |
|
| 784 |
/* By default, return failure. Flip to success and write output |
| 785 |
* once signature generation succeeds. */ |
| 786 |
ret = MLD_ERR_FAIL; |
| 787 |
|
| 788 |
/* Reference: This code is re-structured using a while(1), */ |
| 789 |
/* with explicit "continue" statements (rather than "goto") */ |
| 790 |
/* to implement rejection of invalid signatures. */ |
| 791 |
while (1) |
| 792 |
__loop__( |
| 793 |
assigns(nonce, ret, object_whole(siglen), memory_slice(sig, MLDSA_CRYPTO_BYTES)) |
| 794 |
invariant(nonce <= MLD_NONCE_UB) |
| 795 |
|
| 796 |
/* t0, s1, s2, and mat are initialized above and are NOT changed by this */ |
| 797 |
/* loop. We can therefore re-assert their bounds here as part of the */ |
| 798 |
/* loop invariant. This makes proof noticeably faster with CBMC */ |
| 799 |
invariant(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L, |
| 800 |
array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))) |
| 801 |
invariant(forall(k2, 0, MLDSA_K, array_abs_bound(t0->vec[k2].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 802 |
invariant(forall(k3, 0, MLDSA_L, array_abs_bound(s1->vec[k3].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 803 |
invariant(forall(k4, 0, MLDSA_K, array_abs_bound(s2->vec[k4].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))) |
| 804 |
invariant(ret == MLD_ERR_FAIL) |
| 805 |
) |
| 806 |
{ |
| 807 |
/* Reference: this code explicitly checks for exhaustion of nonce */ |
| 808 |
/* values to provide predictable termination and results in that case */ |
| 809 |
/* Checking here also means that incrementing nonce below can also */ |
| 810 |
/* be proven to be type-safe. */ |
| 811 |
if (nonce == MLD_NONCE_UB) |
| 812 |
{ |
| 813 |
/* Note that ret == MLD_ERR_FAIL by default, so we |
| 814 |
* don't need to set it here. */ |
| 815 |
break; |
| 816 |
} |
| 817 |
|
| 818 |
ret = mld_attempt_signature_generation(sig, mu, rhoprime, nonce, mat, s1, |
| 819 |
s2, t0, context); |
| 820 |
nonce++; |
| 821 |
if (ret == 0) |
| 822 |
{ |
| 823 |
*siglen = MLDSA_CRYPTO_BYTES; |
| 824 |
break; |
| 825 |
} |
| 826 |
else if (ret != MLD_ERR_FAIL) |
| 827 |
{ |
| 828 |
/* For failures such as out-of-memory, propagate and exit immediately. */ |
| 829 |
break; |
| 830 |
} |
| 831 |
|
| 832 |
/* Otherwise, try again. */ |
| 833 |
} |
| 834 |
|
| 835 |
cleanup: |
| 836 |
|
| 837 |
if (ret != 0) |
| 838 |
{ |
| 839 |
/* To be on the safe-side, we zeroize the signature buffer. */ |
| 840 |
*siglen = 0; |
| 841 |
mld_memset(sig, 0, MLDSA_CRYPTO_BYTES); |
| 842 |
} |
| 843 |
|
| 844 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 845 |
MLD_FREE(s2, mld_polyveck, 1, context); |
| 846 |
MLD_FREE(t0, mld_polyveck, 1, context); |
| 847 |
MLD_FREE(s1, mld_polyvecl, 1, context); |
| 848 |
MLD_FREE(mat, mld_polymat, 1, context); |
| 849 |
MLD_FREE(seedbuf, uint8_t, |
| 850 |
2 * MLDSA_SEEDBYTES + MLDSA_TRBYTES + 2 * MLDSA_CRHBYTES, context); |
| 851 |
return ret; |
| 852 |
} |
| 853 |
|
| 854 |
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API) |
| 855 |
MLD_MUST_CHECK_RETURN_VALUE |
| 856 |
MLD_EXTERNAL_API |
| 857 |
int mld_sign_signature(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen, |
| 858 |
const uint8_t *m, size_t mlen, const uint8_t *ctx, |
| 859 |
size_t ctxlen, |
| 860 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 861 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 862 |
{ |
| 863 |
size_t pre_len; |
| 864 |
int ret; |
| 865 |
MLD_ALLOC(pre, uint8_t, MLD_DOMAIN_SEPARATION_MAX_BYTES, context); |
| 866 |
MLD_ALLOC(rnd, uint8_t, MLDSA_RNDBYTES, context); |
| 867 |
|
| 868 |
if (pre == NULL || rnd == NULL) |
| 869 |
{ |
| 870 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 871 |
goto cleanup; |
| 872 |
} |
| 873 |
|
| 874 |
/* Prepare domain separation prefix for pure ML-DSA */ |
| 875 |
pre_len = mld_prepare_domain_separation_prefix(pre, NULL, 0, ctx, ctxlen, |
| 876 |
MLD_PREHASH_NONE); |
| 877 |
if (pre_len == 0) |
| 878 |
{ |
| 879 |
ret = MLD_ERR_FAIL; |
| 880 |
goto cleanup; |
| 881 |
} |
| 882 |
|
| 883 |
/* Randomized variant of ML-DSA. If you need the deterministic variant, |
| 884 |
* call mld_sign_signature_internal directly with all-zero rnd. */ |
| 885 |
if (mld_randombytes(rnd, MLDSA_RNDBYTES) != 0) |
| 886 |
{ |
| 887 |
ret = MLD_ERR_RNG_FAIL; |
| 888 |
goto cleanup; |
| 889 |
} |
| 890 |
MLD_CT_TESTING_SECRET(rnd, sizeof(rnd)); |
| 891 |
|
| 892 |
ret = mld_sign_signature_internal(sig, siglen, m, mlen, pre, pre_len, rnd, sk, |
| 893 |
0, context); |
| 894 |
|
| 895 |
cleanup: |
| 896 |
if (ret != 0) |
| 897 |
{ |
| 898 |
/* To be on the safe-side, make sure *siglen and sig have a well-defined |
| 899 |
* value, even in the case of error. |
| 900 |
* |
| 901 |
* If we come from mld_sign_signature_internal, both are redundant, |
| 902 |
* but the error case should not be the norm, and the added cost of the |
| 903 |
* memset insignificant. */ |
| 904 |
*siglen = 0; |
| 905 |
mld_memset(sig, 0, MLDSA_CRYPTO_BYTES); |
| 906 |
} |
| 907 |
|
| 908 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 909 |
MLD_FREE(rnd, uint8_t, MLDSA_RNDBYTES, context); |
| 910 |
MLD_FREE(pre, uint8_t, MLD_DOMAIN_SEPARATION_MAX_BYTES, context); |
| 911 |
|
| 912 |
return ret; |
| 913 |
} |
| 914 |
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */ |
| 915 |
|
| 916 |
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API) |
| 917 |
MLD_MUST_CHECK_RETURN_VALUE |
| 918 |
MLD_EXTERNAL_API |
| 919 |
int mld_sign_signature_extmu(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen, |
| 920 |
const uint8_t mu[MLDSA_CRHBYTES], |
| 921 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 922 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 923 |
{ |
| 924 |
MLD_ALIGN uint8_t rnd[MLDSA_RNDBYTES]; |
| 925 |
int ret; |
| 926 |
|
| 927 |
/* Randomized variant of ML-DSA. If you need the deterministic variant, |
| 928 |
* call mld_sign_signature_internal directly with all-zero rnd. */ |
| 929 |
if (mld_randombytes(rnd, MLDSA_RNDBYTES) != 0) |
| 930 |
{ |
| 931 |
*siglen = 0; |
| 932 |
ret = MLD_ERR_RNG_FAIL; |
| 933 |
goto cleanup; |
| 934 |
} |
| 935 |
MLD_CT_TESTING_SECRET(rnd, sizeof(rnd)); |
| 936 |
|
| 937 |
ret = mld_sign_signature_internal(sig, siglen, mu, MLDSA_CRHBYTES, NULL, 0, |
| 938 |
rnd, sk, 1, context); |
| 939 |
|
| 940 |
cleanup: |
| 941 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 942 |
mld_zeroize(rnd, sizeof(rnd)); |
| 943 |
|
| 944 |
return ret; |
| 945 |
} |
| 946 |
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */ |
| 947 |
|
| 948 |
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API) |
| 949 |
MLD_MUST_CHECK_RETURN_VALUE |
| 950 |
MLD_EXTERNAL_API |
| 951 |
int mld_sign(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen, |
| 952 |
const uint8_t *ctx, size_t ctxlen, |
| 953 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 954 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 955 |
{ |
| 956 |
int ret; |
| 957 |
size_t i; |
| 958 |
|
| 959 |
for (i = 0; i < mlen; ++i) |
| 960 |
__loop__( |
| 961 |
assigns(i, object_whole(sm)) |
| 962 |
invariant(i <= mlen) |
| 963 |
) |
| 964 |
{ |
| 965 |
sm[MLDSA_CRYPTO_BYTES + mlen - 1 - i] = m[mlen - 1 - i]; |
| 966 |
} |
| 967 |
ret = mld_sign_signature(sm, smlen, sm + MLDSA_CRYPTO_BYTES, mlen, ctx, |
| 968 |
ctxlen, sk, context); |
| 969 |
*smlen += mlen; |
| 970 |
return ret; |
| 971 |
} |
| 972 |
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */ |
| 973 |
|
| 974 |
MLD_MUST_CHECK_RETURN_VALUE |
| 975 |
MLD_EXTERNAL_API |
| 976 |
int mld_sign_verify_internal(const uint8_t *sig, size_t siglen, |
| 977 |
const uint8_t *m, size_t mlen, const uint8_t *pre, |
| 978 |
size_t prelen, |
| 979 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 980 |
int externalmu, |
| 981 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 982 |
{ |
| 983 |
int ret, cmp; |
| 984 |
|
| 985 |
/* TODO: Remove the following workaround for |
| 986 |
* https://github.com/diffblue/cbmc/issues/8813 */ |
| 987 |
typedef MLD_UNION_OR_STRUCT |
| 988 |
{ |
| 989 |
mld_polyveck t1; |
| 990 |
mld_polyveck w1; |
| 991 |
} |
| 992 |
t1w1_u; |
| 993 |
mld_polyveck *t1; |
| 994 |
mld_polyveck *w1; |
| 995 |
|
| 996 |
MLD_ALLOC(buf, uint8_t, (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES), context); |
| 997 |
MLD_ALLOC(rho, uint8_t, MLDSA_SEEDBYTES, context); |
| 998 |
MLD_ALLOC(mu, uint8_t, MLDSA_CRHBYTES, context); |
| 999 |
MLD_ALLOC(c, uint8_t, MLDSA_CTILDEBYTES, context); |
| 1000 |
MLD_ALLOC(c2, uint8_t, MLDSA_CTILDEBYTES, context); |
| 1001 |
MLD_ALLOC(cp, mld_poly, 1, context); |
| 1002 |
MLD_ALLOC(mat, mld_polymat, 1, context); |
| 1003 |
MLD_ALLOC(z, mld_polyvecl, 1, context); |
| 1004 |
MLD_ALLOC(t1w1, t1w1_u, 1, context); |
| 1005 |
MLD_ALLOC(tmp, mld_polyveck, 1, context); |
| 1006 |
MLD_ALLOC(h, mld_polyveck, 1, context); |
| 1007 |
|
| 1008 |
if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL || |
| 1009 |
cp == NULL || mat == NULL || z == NULL || t1w1 == NULL || tmp == NULL || |
| 1010 |
h == NULL) |
| 1011 |
{ |
| 1012 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 1013 |
goto cleanup; |
| 1014 |
} |
| 1015 |
t1 = &t1w1->t1; |
| 1016 |
w1 = &t1w1->w1; |
| 1017 |
|
| 1018 |
if (siglen != MLDSA_CRYPTO_BYTES) |
| 1019 |
{ |
| 1020 |
ret = MLD_ERR_FAIL; |
| 1021 |
goto cleanup; |
| 1022 |
} |
| 1023 |
|
| 1024 |
mld_unpack_pk(rho, t1, pk); |
| 1025 |
|
| 1026 |
/* mld_unpack_sig and mld_polyvecl_chknorm signal failure through a |
| 1027 |
* single non-zero error code that's not yet aligned with MLD_ERR_XXX. |
| 1028 |
* Map it to MLD_ERR_FAIL explicitly. */ |
| 1029 |
if (mld_unpack_sig(c, z, h, sig)) |
| 1030 |
{ |
| 1031 |
ret = MLD_ERR_FAIL; |
| 1032 |
goto cleanup; |
| 1033 |
} |
| 1034 |
if (mld_polyvecl_chknorm(z, MLDSA_GAMMA1 - MLDSA_BETA)) |
| 1035 |
{ |
| 1036 |
ret = MLD_ERR_FAIL; |
| 1037 |
goto cleanup; |
| 1038 |
} |
| 1039 |
|
| 1040 |
if (!externalmu) |
| 1041 |
{ |
| 1042 |
/* Compute CRH(H(rho, t1), pre, msg) */ |
| 1043 |
MLD_ALIGN uint8_t hpk[MLDSA_CRHBYTES]; |
| 1044 |
mld_H(hpk, MLDSA_TRBYTES, pk, MLDSA_CRYPTO_PUBLICKEYBYTES, NULL, 0, NULL, |
| 1045 |
0); |
| 1046 |
mld_H(mu, MLDSA_CRHBYTES, hpk, MLDSA_TRBYTES, pre, prelen, m, mlen); |
| 1047 |
|
| 1048 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1049 |
mld_zeroize(hpk, sizeof(hpk)); |
| 1050 |
} |
| 1051 |
else |
| 1052 |
{ |
| 1053 |
/* mu has been provided directly */ |
| 1054 |
mld_memcpy(mu, m, MLDSA_CRHBYTES); |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* Matrix-vector multiplication; compute Az - c2^dt1 */ |
| 1058 |
mld_poly_challenge(cp, c); |
| 1059 |
mld_poly_ntt(cp); |
| 1060 |
mld_polyveck_shiftl(t1); |
| 1061 |
mld_polyveck_ntt(t1); |
| 1062 |
mld_polyveck_pointwise_poly_montgomery(tmp, cp, t1); |
| 1063 |
|
| 1064 |
mld_polyvec_matrix_expand(mat, rho); |
| 1065 |
mld_polyvecl_ntt(z); |
| 1066 |
mld_polyvec_matrix_pointwise_montgomery(w1, mat, z); |
| 1067 |
mld_polyveck_sub(w1, tmp); |
| 1068 |
mld_polyveck_reduce(w1); |
| 1069 |
mld_polyveck_invntt_tomont(w1); |
| 1070 |
|
| 1071 |
/* Reconstruct w1 */ |
| 1072 |
mld_polyveck_caddq(w1); |
| 1073 |
mld_polyveck_use_hint(tmp, w1, h); |
| 1074 |
mld_polyveck_pack_w1(buf, tmp); |
| 1075 |
/* Call random oracle and verify challenge */ |
| 1076 |
mld_H(c2, MLDSA_CTILDEBYTES, mu, MLDSA_CRHBYTES, buf, |
| 1077 |
MLDSA_K * MLDSA_POLYW1_PACKEDBYTES, NULL, 0); |
| 1078 |
|
| 1079 |
cmp = mld_ct_memcmp(c, c2, MLDSA_CTILDEBYTES); |
| 1080 |
|
| 1081 |
/* Declassify the result of the verification. */ |
| 1082 |
MLD_CT_TESTING_DECLASSIFY(&cmp, sizeof(cmp)); |
| 1083 |
|
| 1084 |
ret = cmp == 0 ? 0 : MLD_ERR_FAIL; |
| 1085 |
|
| 1086 |
cleanup: |
| 1087 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1088 |
MLD_FREE(h, mld_polyveck, 1, context); |
| 1089 |
MLD_FREE(tmp, mld_polyveck, 1, context); |
| 1090 |
MLD_FREE(t1w1, t1w1_u, 1, context); |
| 1091 |
MLD_FREE(z, mld_polyvecl, 1, context); |
| 1092 |
MLD_FREE(mat, mld_polymat, 1, context); |
| 1093 |
MLD_FREE(cp, mld_poly, 1, context); |
| 1094 |
MLD_FREE(c2, uint8_t, MLDSA_CTILDEBYTES, context); |
| 1095 |
MLD_FREE(c, uint8_t, MLDSA_CTILDEBYTES, context); |
| 1096 |
MLD_FREE(mu, uint8_t, MLDSA_CRHBYTES, context); |
| 1097 |
MLD_FREE(rho, uint8_t, MLDSA_SEEDBYTES, context); |
| 1098 |
MLD_FREE(buf, uint8_t, (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES), context); |
| 1099 |
return ret; |
| 1100 |
} |
| 1101 |
|
| 1102 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1103 |
MLD_EXTERNAL_API |
| 1104 |
int mld_sign_verify(const uint8_t *sig, size_t siglen, const uint8_t *m, |
| 1105 |
size_t mlen, const uint8_t *ctx, size_t ctxlen, |
| 1106 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 1107 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1108 |
{ |
| 1109 |
MLD_ALIGN uint8_t pre[MLD_DOMAIN_SEPARATION_MAX_BYTES]; |
| 1110 |
size_t pre_len; |
| 1111 |
int ret; |
| 1112 |
|
| 1113 |
pre_len = mld_prepare_domain_separation_prefix(pre, NULL, 0, ctx, ctxlen, |
| 1114 |
MLD_PREHASH_NONE); |
| 1115 |
if (pre_len == 0) |
| 1116 |
{ |
| 1117 |
ret = MLD_ERR_FAIL; |
| 1118 |
goto cleanup; |
| 1119 |
} |
| 1120 |
|
| 1121 |
ret = mld_sign_verify_internal(sig, siglen, m, mlen, pre, pre_len, pk, 0, |
| 1122 |
context); |
| 1123 |
|
| 1124 |
cleanup: |
| 1125 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1126 |
mld_zeroize(pre, sizeof(pre)); |
| 1127 |
|
| 1128 |
return ret; |
| 1129 |
} |
| 1130 |
|
| 1131 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1132 |
MLD_EXTERNAL_API |
| 1133 |
int mld_sign_verify_extmu(const uint8_t *sig, size_t siglen, |
| 1134 |
const uint8_t mu[MLDSA_CRHBYTES], |
| 1135 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 1136 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1137 |
{ |
| 1138 |
return mld_sign_verify_internal(sig, siglen, mu, MLDSA_CRHBYTES, NULL, 0, pk, |
| 1139 |
1, context); |
| 1140 |
} |
| 1141 |
|
| 1142 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1143 |
MLD_EXTERNAL_API |
| 1144 |
int mld_sign_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, |
| 1145 |
const uint8_t *ctx, size_t ctxlen, |
| 1146 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 1147 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1148 |
{ |
| 1149 |
int ret; |
| 1150 |
size_t i; |
| 1151 |
|
| 1152 |
if (smlen < MLDSA_CRYPTO_BYTES) |
| 1153 |
{ |
| 1154 |
ret = MLD_ERR_FAIL; |
| 1155 |
goto cleanup; |
| 1156 |
} |
| 1157 |
|
| 1158 |
*mlen = smlen - MLDSA_CRYPTO_BYTES; |
| 1159 |
ret = mld_sign_verify(sm, MLDSA_CRYPTO_BYTES, sm + MLDSA_CRYPTO_BYTES, *mlen, |
| 1160 |
ctx, ctxlen, pk, context); |
| 1161 |
if (ret == 0) |
| 1162 |
{ |
| 1163 |
/* All good, copy msg, return 0 */ |
| 1164 |
for (i = 0; i < *mlen; ++i) |
| 1165 |
__loop__( |
| 1166 |
assigns(i, memory_slice(m, *mlen)) |
| 1167 |
invariant(i <= *mlen) |
| 1168 |
) |
| 1169 |
{ |
| 1170 |
m[i] = sm[MLDSA_CRYPTO_BYTES + i]; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
cleanup: |
| 1175 |
|
| 1176 |
if (ret != 0) |
| 1177 |
{ |
| 1178 |
/* To be on the safe-side, we zeroize the message buffer. */ |
| 1179 |
*mlen = 0; |
| 1180 |
mld_memset(m, 0, smlen); |
| 1181 |
} |
| 1182 |
|
| 1183 |
return ret; |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1188 |
MLD_EXTERNAL_API |
| 1189 |
int mld_sign_signature_pre_hash_internal( |
| 1190 |
uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen, const uint8_t *ph, |
| 1191 |
size_t phlen, const uint8_t *ctx, size_t ctxlen, |
| 1192 |
const uint8_t rnd[MLDSA_RNDBYTES], |
| 1193 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], int hashalg, |
| 1194 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1195 |
{ |
| 1196 |
MLD_ALIGN uint8_t pre[MLD_DOMAIN_SEPARATION_MAX_BYTES]; |
| 1197 |
size_t pre_len; |
| 1198 |
int ret; |
| 1199 |
|
| 1200 |
pre_len = mld_prepare_domain_separation_prefix(pre, ph, phlen, ctx, ctxlen, |
| 1201 |
hashalg); |
| 1202 |
if (pre_len == 0) |
| 1203 |
{ |
| 1204 |
ret = MLD_ERR_FAIL; |
| 1205 |
goto cleanup; |
| 1206 |
} |
| 1207 |
|
| 1208 |
ret = mld_sign_signature_internal(sig, siglen, pre, pre_len, NULL, 0, rnd, sk, |
| 1209 |
0, context); |
| 1210 |
cleanup: |
| 1211 |
if (ret != 0) |
| 1212 |
{ |
| 1213 |
/* To be on the safe-side, make sure *siglen and sig have a well-defined |
| 1214 |
* value, even in the case of error. |
| 1215 |
* |
| 1216 |
* If we come from mld_sign_signature_internal, both are redundant, |
| 1217 |
* but the error case should not be the norm, and the added cost of the |
| 1218 |
* memset insignificant. */ |
| 1219 |
*siglen = 0; |
| 1220 |
mld_memset(sig, 0, MLDSA_CRYPTO_BYTES); |
| 1221 |
} |
| 1222 |
|
| 1223 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1224 |
mld_zeroize(pre, sizeof(pre)); |
| 1225 |
return ret; |
| 1226 |
} |
| 1227 |
|
| 1228 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1229 |
MLD_EXTERNAL_API |
| 1230 |
int mld_sign_verify_pre_hash_internal( |
| 1231 |
const uint8_t *sig, size_t siglen, const uint8_t *ph, size_t phlen, |
| 1232 |
const uint8_t *ctx, size_t ctxlen, |
| 1233 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], int hashalg, |
| 1234 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1235 |
{ |
| 1236 |
MLD_ALIGN uint8_t pre[MLD_DOMAIN_SEPARATION_MAX_BYTES]; |
| 1237 |
size_t pre_len; |
| 1238 |
int ret; |
| 1239 |
|
| 1240 |
pre_len = mld_prepare_domain_separation_prefix(pre, ph, phlen, ctx, ctxlen, |
| 1241 |
hashalg); |
| 1242 |
if (pre_len == 0) |
| 1243 |
{ |
| 1244 |
ret = MLD_ERR_FAIL; |
| 1245 |
goto cleanup; |
| 1246 |
} |
| 1247 |
|
| 1248 |
ret = mld_sign_verify_internal(sig, siglen, pre, pre_len, NULL, 0, pk, 0, |
| 1249 |
context); |
| 1250 |
|
| 1251 |
cleanup: |
| 1252 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1253 |
mld_zeroize(pre, sizeof(pre)); |
| 1254 |
return ret; |
| 1255 |
} |
| 1256 |
|
| 1257 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1258 |
MLD_EXTERNAL_API |
| 1259 |
int mld_sign_signature_pre_hash_shake256( |
| 1260 |
uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen, const uint8_t *m, |
| 1261 |
size_t mlen, const uint8_t *ctx, size_t ctxlen, |
| 1262 |
const uint8_t rnd[MLDSA_RNDBYTES], |
| 1263 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 1264 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1265 |
{ |
| 1266 |
MLD_ALIGN uint8_t ph[64]; |
| 1267 |
int ret; |
| 1268 |
mld_shake256(ph, sizeof(ph), m, mlen); |
| 1269 |
ret = mld_sign_signature_pre_hash_internal(sig, siglen, ph, sizeof(ph), ctx, |
| 1270 |
ctxlen, rnd, sk, |
| 1271 |
MLD_PREHASH_SHAKE_256, context); |
| 1272 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1273 |
mld_zeroize(ph, sizeof(ph)); |
| 1274 |
return ret; |
| 1275 |
} |
| 1276 |
|
| 1277 |
MLD_MUST_CHECK_RETURN_VALUE |
| 1278 |
MLD_EXTERNAL_API |
| 1279 |
int mld_sign_verify_pre_hash_shake256( |
| 1280 |
const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, |
| 1281 |
const uint8_t *ctx, size_t ctxlen, |
| 1282 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 1283 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1284 |
{ |
| 1285 |
MLD_ALIGN uint8_t ph[64]; |
| 1286 |
int ret; |
| 1287 |
mld_shake256(ph, sizeof(ph), m, mlen); |
| 1288 |
ret = mld_sign_verify_pre_hash_internal(sig, siglen, ph, sizeof(ph), ctx, |
| 1289 |
ctxlen, pk, MLD_PREHASH_SHAKE_256, |
| 1290 |
context); |
| 1291 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1292 |
mld_zeroize(ph, sizeof(ph)); |
| 1293 |
return ret; |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
#define MLD_PRE_HASH_OID_LEN 11 |
| 1298 |
|
| 1299 |
/************************************************* |
| 1300 |
* Name: mld_get_hash_oid |
| 1301 |
* |
| 1302 |
* Description: Returns the OID of a given SHA-2/SHA-3 hash function. |
| 1303 |
* |
| 1304 |
* Arguments: - uint8_t oid[11]: pointer to output oid |
| 1305 |
* - int hashalg: hash algorithm constant (MLD_PREHASH_*) |
| 1306 |
* |
| 1307 |
***************************************************/ |
| 1308 |
static void mld_get_hash_oid(uint8_t oid[MLD_PRE_HASH_OID_LEN], int hashalg) |
| 1309 |
{ |
| 1310 |
unsigned int i; |
| 1311 |
static const struct |
| 1312 |
{ |
| 1313 |
int alg; |
| 1314 |
uint8_t oid[MLD_PRE_HASH_OID_LEN]; |
| 1315 |
} oid_map[] = { |
| 1316 |
{MLD_PREHASH_SHA2_224, |
| 1317 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}}, |
| 1318 |
{MLD_PREHASH_SHA2_256, |
| 1319 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}}, |
| 1320 |
{MLD_PREHASH_SHA2_384, |
| 1321 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}}, |
| 1322 |
{MLD_PREHASH_SHA2_512, |
| 1323 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}}, |
| 1324 |
{MLD_PREHASH_SHA2_512_224, |
| 1325 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05}}, |
| 1326 |
{MLD_PREHASH_SHA2_512_256, |
| 1327 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06}}, |
| 1328 |
{MLD_PREHASH_SHA3_224, |
| 1329 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07}}, |
| 1330 |
{MLD_PREHASH_SHA3_256, |
| 1331 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08}}, |
| 1332 |
{MLD_PREHASH_SHA3_384, |
| 1333 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09}}, |
| 1334 |
{MLD_PREHASH_SHA3_512, |
| 1335 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0A}}, |
| 1336 |
{MLD_PREHASH_SHAKE_128, |
| 1337 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0B}}, |
| 1338 |
{MLD_PREHASH_SHAKE_256, |
| 1339 |
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0C}}}; |
| 1340 |
|
| 1341 |
for (i = 0; i < sizeof(oid_map) / sizeof(oid_map[0]); i++) |
| 1342 |
__loop__( |
| 1343 |
invariant(i <= sizeof(oid_map) / sizeof(oid_map[0])) |
| 1344 |
) |
| 1345 |
{ |
| 1346 |
if (oid_map[i].alg == hashalg) |
| 1347 |
{ |
| 1348 |
mld_memcpy(oid, oid_map[i].oid, MLD_PRE_HASH_OID_LEN); |
| 1349 |
return; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
static int mld_validate_hash_length(int hashalg, size_t len) |
| 1355 |
{ |
| 1356 |
switch (hashalg) |
| 1357 |
{ |
| 1358 |
case MLD_PREHASH_SHA2_224: |
| 1359 |
return (len == 224 / 8) ? 0 : -1; |
| 1360 |
case MLD_PREHASH_SHA2_256: |
| 1361 |
return (len == 256 / 8) ? 0 : -1; |
| 1362 |
case MLD_PREHASH_SHA2_384: |
| 1363 |
return (len == 384 / 8) ? 0 : -1; |
| 1364 |
case MLD_PREHASH_SHA2_512: |
| 1365 |
return (len == 512 / 8) ? 0 : -1; |
| 1366 |
case MLD_PREHASH_SHA2_512_224: |
| 1367 |
return (len == 224 / 8) ? 0 : -1; |
| 1368 |
case MLD_PREHASH_SHA2_512_256: |
| 1369 |
return (len == 256 / 8) ? 0 : -1; |
| 1370 |
case MLD_PREHASH_SHA3_224: |
| 1371 |
return (len == 224 / 8) ? 0 : -1; |
| 1372 |
case MLD_PREHASH_SHA3_256: |
| 1373 |
return (len == 256 / 8) ? 0 : -1; |
| 1374 |
case MLD_PREHASH_SHA3_384: |
| 1375 |
return (len == 384 / 8) ? 0 : -1; |
| 1376 |
case MLD_PREHASH_SHA3_512: |
| 1377 |
return (len == 512 / 8) ? 0 : -1; |
| 1378 |
case MLD_PREHASH_SHAKE_128: |
| 1379 |
return (len == 256 / 8) ? 0 : -1; |
| 1380 |
case MLD_PREHASH_SHAKE_256: |
| 1381 |
return (len == 512 / 8) ? 0 : -1; |
| 1382 |
} |
| 1383 |
return -1; |
| 1384 |
} |
| 1385 |
|
| 1386 |
size_t mld_prepare_domain_separation_prefix( |
| 1387 |
uint8_t prefix[MLD_DOMAIN_SEPARATION_MAX_BYTES], const uint8_t *ph, |
| 1388 |
size_t phlen, const uint8_t *ctx, size_t ctxlen, int hashalg) |
| 1389 |
{ |
| 1390 |
if (ctxlen > 255) |
| 1391 |
{ |
| 1392 |
return 0; |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (hashalg != MLD_PREHASH_NONE) |
| 1396 |
{ |
| 1397 |
if (ph == NULL || mld_validate_hash_length(hashalg, phlen) != 0) |
| 1398 |
{ |
| 1399 |
return 0; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
/* Common prefix: 0x00/0x01 || ctxlen || ctx */ |
| 1404 |
prefix[0] = (hashalg == MLD_PREHASH_NONE) ? 0 : 1; |
| 1405 |
prefix[1] = (uint8_t)ctxlen; |
| 1406 |
if (ctxlen > 0) |
| 1407 |
{ |
| 1408 |
mld_memcpy(prefix + 2, ctx, ctxlen); |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (hashalg == MLD_PREHASH_NONE) |
| 1412 |
{ |
| 1413 |
return 2 + ctxlen; |
| 1414 |
} |
| 1415 |
|
| 1416 |
/* HashML-DSA: append oid || ph */ |
| 1417 |
mld_get_hash_oid(prefix + 2 + ctxlen, hashalg); |
| 1418 |
mld_memcpy(prefix + 2 + ctxlen + MLD_PRE_HASH_OID_LEN, ph, phlen); |
| 1419 |
return 2 + ctxlen + MLD_PRE_HASH_OID_LEN + phlen; |
| 1420 |
} |
| 1421 |
|
| 1422 |
MLD_EXTERNAL_API |
| 1423 |
int mld_sign_pk_from_sk(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 1424 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 1425 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context) |
| 1426 |
{ |
| 1427 |
uint8_t check, cmp0, cmp1, chk1, chk2; |
| 1428 |
int ret; |
| 1429 |
MLD_ALLOC(rho, uint8_t, MLDSA_SEEDBYTES, context); |
| 1430 |
MLD_ALLOC(tr, uint8_t, MLDSA_TRBYTES, context); |
| 1431 |
MLD_ALLOC(tr_computed, uint8_t, MLDSA_TRBYTES, context); |
| 1432 |
MLD_ALLOC(key, uint8_t, MLDSA_SEEDBYTES, context); |
| 1433 |
MLD_ALLOC(s1, mld_polyvecl, 1, context); |
| 1434 |
MLD_ALLOC(s2, mld_polyveck, 1, context); |
| 1435 |
MLD_ALLOC(t0, mld_polyveck, 1, context); |
| 1436 |
MLD_ALLOC(t0_computed, mld_polyveck, 1, context); |
| 1437 |
MLD_ALLOC(t1, mld_polyveck, 1, context); |
| 1438 |
|
| 1439 |
if (rho == NULL || tr == NULL || tr_computed == NULL || key == NULL || |
| 1440 |
s1 == NULL || s2 == NULL || t0 == NULL || t0_computed == NULL || |
| 1441 |
t1 == NULL) |
| 1442 |
{ |
| 1443 |
ret = MLD_ERR_OUT_OF_MEMORY; |
| 1444 |
goto cleanup; |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* Unpack secret key */ |
| 1448 |
mld_unpack_sk(rho, tr, key, t0, s1, s2, sk); |
| 1449 |
|
| 1450 |
/* Validate s1 and s2 coefficients are within [-MLDSA_ETA, MLDSA_ETA] */ |
| 1451 |
chk1 = mld_polyvecl_chknorm(s1, MLDSA_ETA + 1) & 0xFF; |
| 1452 |
chk2 = mld_polyveck_chknorm(s2, MLDSA_ETA + 1) & 0xFF; |
| 1453 |
|
| 1454 |
/* Recompute t0, t1, tr, and pk from rho, s1, s2 */ |
| 1455 |
ret = mld_compute_t0_t1_tr_from_sk_components(t0_computed, t1, tr_computed, |
| 1456 |
pk, rho, s1, s2, context); |
| 1457 |
if (ret != 0) |
| 1458 |
{ |
| 1459 |
goto cleanup; |
| 1460 |
} |
| 1461 |
|
| 1462 |
/* Validate t0 and tr using constant-time comparisons */ |
| 1463 |
cmp0 = mld_ct_memcmp((const uint8_t *)t0, (const uint8_t *)t0_computed, |
| 1464 |
sizeof(mld_polyveck)); |
| 1465 |
cmp1 = mld_ct_memcmp((const uint8_t *)tr, (const uint8_t *)tr_computed, |
| 1466 |
MLDSA_TRBYTES); |
| 1467 |
check = mld_value_barrier_u8(cmp0 | cmp1 | chk1 | chk2); |
| 1468 |
|
| 1469 |
/* Declassify the final result of the validity check. */ |
| 1470 |
MLD_CT_TESTING_DECLASSIFY(&check, sizeof(check)); |
| 1471 |
ret = (check != 0) ? MLD_ERR_FAIL : 0; |
| 1472 |
|
| 1473 |
cleanup: |
| 1474 |
|
| 1475 |
if (ret != 0) |
| 1476 |
{ |
| 1477 |
mld_zeroize(pk, MLDSA_CRYPTO_PUBLICKEYBYTES); |
| 1478 |
} |
| 1479 |
|
| 1480 |
/* Constant time: pk is either the valid public key or zeroed on error */ |
| 1481 |
MLD_CT_TESTING_DECLASSIFY(pk, MLDSA_CRYPTO_PUBLICKEYBYTES); |
| 1482 |
|
| 1483 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 1484 |
MLD_FREE(t1, mld_polyveck, 1, context); |
| 1485 |
MLD_FREE(t0_computed, mld_polyveck, 1, context); |
| 1486 |
MLD_FREE(t0, mld_polyveck, 1, context); |
| 1487 |
MLD_FREE(s2, mld_polyveck, 1, context); |
| 1488 |
MLD_FREE(s1, mld_polyvecl, 1, context); |
| 1489 |
MLD_FREE(key, uint8_t, MLDSA_SEEDBYTES, context); |
| 1490 |
MLD_FREE(tr_computed, uint8_t, MLDSA_TRBYTES, context); |
| 1491 |
MLD_FREE(tr, uint8_t, MLDSA_TRBYTES, context); |
| 1492 |
MLD_FREE(rho, uint8_t, MLDSA_SEEDBYTES, context); |
| 1493 |
|
| 1494 |
return ret; |
| 1495 |
} |
| 1496 |
|
| 1497 |
/* To facilitate single-compilation-unit (SCU) builds, undefine all macros. |
| 1498 |
* Don't modify by hand -- this is auto-generated by scripts/autogen. */ |
| 1499 |
#undef mld_check_pct |
| 1500 |
#undef mld_sample_s1_s2 |
| 1501 |
#undef mld_validate_hash_length |
| 1502 |
#undef mld_get_hash_oid |
| 1503 |
#undef mld_H |
| 1504 |
#undef mld_compute_pack_z |
| 1505 |
#undef mld_attempt_signature_generation |
| 1506 |
#undef mld_compute_t0_t1_tr_from_sk_components |
| 1507 |
#undef MLD_NONCE_UB |
| 1508 |
#undef MLD_PRE_HASH_OID_LEN |