| 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 |
* - [FIPS204] |
| 10 |
* FIPS 204 Module-Lattice-Based Digital Signature Standard |
| 11 |
* National Institute of Standards and Technology |
| 12 |
* https://csrc.nist.gov/pubs/fips/204/final |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef MLD_H |
| 16 |
#define MLD_H |
| 17 |
|
| 18 |
/* |
| 19 |
* Public API for mldsa-native |
| 20 |
* |
| 21 |
* This header defines the public API of a single build of mldsa-native. |
| 22 |
* |
| 23 |
* Make sure the configuration file is in the include path |
| 24 |
* (this is "mldsa_native_config.h" by default, or MLD_CONFIG_FILE if defined). |
| 25 |
* |
| 26 |
* # Multi-level builds |
| 27 |
* |
| 28 |
* This header specifies a build of mldsa-native for a fixed security level. |
| 29 |
* If you need multiple security levels, leave the security level unspecified |
| 30 |
* in the configuration file and include this header multiple times, setting |
| 31 |
* MLD_CONFIG_PARAMETER_SET accordingly for each, and #undef'ing the MLD_H |
| 32 |
* guard to allow multiple inclusions. |
| 33 |
* |
| 34 |
* # Legacy configuration (deprecated) |
| 35 |
* |
| 36 |
* Instead of providing the config file used for the build, you can |
| 37 |
* alternatively set the following configuration options prior to |
| 38 |
* including this header. |
| 39 |
* |
| 40 |
* This method of configuration is deprecated. |
| 41 |
* It will be removed in mldsa-native-v2. |
| 42 |
* |
| 43 |
* - MLD_CONFIG_API_PARAMETER_SET [required] |
| 44 |
* |
| 45 |
* The parameter set used for the build; 44, 65, or 87. |
| 46 |
* |
| 47 |
* - MLD_CONFIG_API_NAMESPACE_PREFIX [required] |
| 48 |
* |
| 49 |
* The namespace prefix used for the build. |
| 50 |
* |
| 51 |
* NOTE: |
| 52 |
* For a multi-level build, you must include the 44/65/87 suffixes |
| 53 |
* in MLD_CONFIG_API_NAMESPACE_PREFIX. |
| 54 |
* |
| 55 |
* - MLD_CONFIG_API_NO_SUPERCOP [optional] |
| 56 |
* |
| 57 |
* By default, this header will also expose the mldsa-native API in the |
| 58 |
* SUPERCOP naming convention crypto_sign_xxx. If you don't want/need this, |
| 59 |
* set MLD_CONFIG_API_NO_SUPERCOP. You must set this for a multi-level build. |
| 60 |
* |
| 61 |
* - MLD_CONFIG_API_CONSTANTS_ONLY [optional] |
| 62 |
* |
| 63 |
* If you don't want this header to expose any function declarations, |
| 64 |
* but only constants for the sizes of key material, set |
| 65 |
* MLD_CONFIG_API_CONSTANTS_ONLY. In this case, you don't need to set |
| 66 |
* MLD_CONFIG_API_PARAMETER_SET or MLD_CONFIG_API_NAMESPACE_PREFIX, |
| 67 |
* nor include a configuration. |
| 68 |
* |
| 69 |
* - MLD_CONFIG_API_QUALIFIER [optional] |
| 70 |
* |
| 71 |
* Qualifier to apply to external API. |
| 72 |
* |
| 73 |
******************************************************************************/ |
| 74 |
|
| 75 |
/******************************* Key sizes ************************************/ |
| 76 |
|
| 77 |
/* Sizes of cryptographic material, per parameter set */ |
| 78 |
/* See mldsa/src/params.h for the arithmetic expressions giving rise to these */ |
| 79 |
/* check-magic: off */ |
| 80 |
#define MLDSA44_SECRETKEYBYTES 2560 |
| 81 |
#define MLDSA44_PUBLICKEYBYTES 1312 |
| 82 |
#define MLDSA44_BYTES 2420 |
| 83 |
|
| 84 |
#define MLDSA65_SECRETKEYBYTES 4032 |
| 85 |
#define MLDSA65_PUBLICKEYBYTES 1952 |
| 86 |
#define MLDSA65_BYTES 3309 |
| 87 |
|
| 88 |
#define MLDSA87_SECRETKEYBYTES 4896 |
| 89 |
#define MLDSA87_PUBLICKEYBYTES 2592 |
| 90 |
#define MLDSA87_BYTES 4627 |
| 91 |
/* check-magic: on */ |
| 92 |
|
| 93 |
/* Size of seed and randomness in bytes (level-independent) */ |
| 94 |
#define MLDSA_SEEDBYTES 32 |
| 95 |
#define MLDSA44_SEEDBYTES MLDSA_SEEDBYTES |
| 96 |
#define MLDSA65_SEEDBYTES MLDSA_SEEDBYTES |
| 97 |
#define MLDSA87_SEEDBYTES MLDSA_SEEDBYTES |
| 98 |
|
| 99 |
/* Size of CRH output in bytes (level-independent) */ |
| 100 |
#define MLDSA_CRHBYTES 64 |
| 101 |
#define MLDSA44_CRHBYTES MLDSA_CRHBYTES |
| 102 |
#define MLDSA65_CRHBYTES MLDSA_CRHBYTES |
| 103 |
#define MLDSA87_CRHBYTES MLDSA_CRHBYTES |
| 104 |
|
| 105 |
/* Size of TR output in bytes (level-independent) */ |
| 106 |
#define MLDSA_TRBYTES 64 |
| 107 |
#define MLDSA44_TRBYTES MLDSA_TRBYTES |
| 108 |
#define MLDSA65_TRBYTES MLDSA_TRBYTES |
| 109 |
#define MLDSA87_TRBYTES MLDSA_TRBYTES |
| 110 |
|
| 111 |
/* Size of randomness for signing in bytes (level-independent) */ |
| 112 |
#define MLDSA_RNDBYTES 32 |
| 113 |
#define MLDSA44_RNDBYTES MLDSA_RNDBYTES |
| 114 |
#define MLDSA65_RNDBYTES MLDSA_RNDBYTES |
| 115 |
#define MLDSA87_RNDBYTES MLDSA_RNDBYTES |
| 116 |
|
| 117 |
/* Sizes of cryptographic material, as a function of LVL=44,65,87 */ |
| 118 |
#define MLDSA_SECRETKEYBYTES_(LVL) MLDSA##LVL##_SECRETKEYBYTES |
| 119 |
#define MLDSA_PUBLICKEYBYTES_(LVL) MLDSA##LVL##_PUBLICKEYBYTES |
| 120 |
#define MLDSA_BYTES_(LVL) MLDSA##LVL##_BYTES |
| 121 |
#define MLDSA_SECRETKEYBYTES(LVL) MLDSA_SECRETKEYBYTES_(LVL) |
| 122 |
#define MLDSA_PUBLICKEYBYTES(LVL) MLDSA_PUBLICKEYBYTES_(LVL) |
| 123 |
#define MLDSA_BYTES(LVL) MLDSA_BYTES_(LVL) |
| 124 |
|
| 125 |
/****************************** Error codes ***********************************/ |
| 126 |
|
| 127 |
/* Generic failure condition */ |
| 128 |
#define MLD_ERR_FAIL -1 |
| 129 |
/* An allocation failed. This can only happen if MLD_CONFIG_CUSTOM_ALLOC_FREE |
| 130 |
* is defined and the provided MLD_CUSTOM_ALLOC can fail. */ |
| 131 |
#define MLD_ERR_OUT_OF_MEMORY -2 |
| 132 |
/* An rng failure occured. Might be due to insufficient entropy or |
| 133 |
* system misconfiguration. */ |
| 134 |
#define MLD_ERR_RNG_FAIL -3 |
| 135 |
|
| 136 |
/****************************** Function API **********************************/ |
| 137 |
|
| 138 |
#define MLD_API_CONCAT_(x, y) x##y |
| 139 |
#define MLD_API_CONCAT(x, y) MLD_API_CONCAT_(x, y) |
| 140 |
#define MLD_API_CONCAT_UNDERSCORE(x, y) MLD_API_CONCAT(MLD_API_CONCAT(x, _), y) |
| 141 |
|
| 142 |
#if !defined(MLD_CONFIG_API_PARAMETER_SET) |
| 143 |
/* Recommended configuration via same config file as used for the build. */ |
| 144 |
|
| 145 |
/* For now, we derive the legacy API configuration MLD_CONFIG_API_XXX from |
| 146 |
* the config file. In mldsa-native-v2, this will be removed and we will |
| 147 |
* exclusively work with MLD_CONFIG_XXX. */ |
| 148 |
|
| 149 |
/* You need to make sure the config file is in the include path. */ |
| 150 |
#if defined(MLD_CONFIG_FILE) |
| 151 |
#include MLD_CONFIG_FILE |
| 152 |
#else |
| 153 |
#include "mldsa_native_config.h" |
| 154 |
#endif |
| 155 |
|
| 156 |
#define MLD_CONFIG_API_PARAMETER_SET MLD_CONFIG_PARAMETER_SET |
| 157 |
|
| 158 |
#if defined(MLD_CONFIG_MULTILEVEL_BUILD) |
| 159 |
#define MLD_CONFIG_API_NAMESPACE_PREFIX \ |
| 160 |
MLD_API_CONCAT(MLD_CONFIG_NAMESPACE_PREFIX, MLD_CONFIG_PARAMETER_SET) |
| 161 |
#else |
| 162 |
#define MLD_CONFIG_API_NAMESPACE_PREFIX MLD_CONFIG_NAMESPACE_PREFIX |
| 163 |
#endif |
| 164 |
|
| 165 |
#if defined(MLD_CONFIG_NO_SUPERCOP) |
| 166 |
#define MLD_CONFIG_API_NO_SUPERCOP |
| 167 |
#endif |
| 168 |
|
| 169 |
#if defined(MLD_CONFIG_CONSTANTS_ONLY) |
| 170 |
#define MLD_CONFIG_API_CONSTANTS_ONLY |
| 171 |
#endif |
| 172 |
|
| 173 |
#if defined(MLD_CONFIG_EXTERNAL_API_QUALIFIER) |
| 174 |
#define MLD_CONFIG_API_QUALIFIER MLD_CONFIG_EXTERNAL_API_QUALIFIER |
| 175 |
#endif |
| 176 |
|
| 177 |
#else /* !MLD_CONFIG_API_PARAMETER_SET */ |
| 178 |
#define MLD_API_LEGACY_CONFIG |
| 179 |
|
| 180 |
#endif /* MLD_CONFIG_API_PARAMETER_SET */ |
| 181 |
|
| 182 |
#define MLD_API_NAMESPACE(sym) \ |
| 183 |
MLD_API_CONCAT_UNDERSCORE(MLD_CONFIG_API_NAMESPACE_PREFIX, sym) |
| 184 |
|
| 185 |
#if defined(__GNUC__) || defined(clang) |
| 186 |
#define MLD_API_MUST_CHECK_RETURN_VALUE __attribute__((warn_unused_result)) |
| 187 |
#else |
| 188 |
#define MLD_API_MUST_CHECK_RETURN_VALUE |
| 189 |
#endif |
| 190 |
|
| 191 |
#if defined(MLD_CONFIG_API_QUALIFIER) |
| 192 |
#define MLD_API_QUALIFIER MLD_CONFIG_API_QUALIFIER |
| 193 |
#else |
| 194 |
#define MLD_API_QUALIFIER |
| 195 |
#endif |
| 196 |
|
| 197 |
#if !defined(MLD_CONFIG_API_CONSTANTS_ONLY) |
| 198 |
|
| 199 |
#include <stddef.h> |
| 200 |
#include <stdint.h> |
| 201 |
|
| 202 |
|
| 203 |
#ifdef __cplusplus |
| 204 |
extern "C" |
| 205 |
{ |
| 206 |
#endif |
| 207 |
|
| 208 |
/************************************************* |
| 209 |
* Name: crypto_sign_keypair_internal |
| 210 |
* |
| 211 |
* Description: Generates public and private key. Internal API. |
| 212 |
* When MLD_CONFIG_KEYGEN_PCT is set, performs a Pairwise |
| 213 |
* Consistency Test (PCT) as required by FIPS 140-3 IG. |
| 214 |
* |
| 215 |
* Arguments: |
| 216 |
* - uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 217 |
* output public key |
| 218 |
* - uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 219 |
* output private key |
| 220 |
* - const uint8_t seed[MLDSA_SEEDBYTES]: |
| 221 |
* input random seed |
| 222 |
* |
| 223 |
* Returns: |
| 224 |
* - 0: Success |
| 225 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 226 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 227 |
* - MLD_ERR_RNG_FAIL: Random number generation failed. |
| 228 |
* - MLD_ERR_FAIL: Other kinds of failure, incl. PCT failure |
| 229 |
* if MLD_CONFIG_KEYGEN_PCT is enabled. |
| 230 |
* |
| 231 |
* Specification: Implements @[FIPS204 Algorithm 6 (ML-DSA.KeyGen_internal)] |
| 232 |
* |
| 233 |
**************************************************/ |
| 234 |
MLD_API_QUALIFIER |
| 235 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 236 |
int MLD_API_NAMESPACE(keypair_internal)( |
| 237 |
uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 238 |
uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 239 |
const uint8_t seed[MLDSA_SEEDBYTES] |
| 240 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 241 |
, |
| 242 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 243 |
#endif |
| 244 |
); |
| 245 |
|
| 246 |
/************************************************* |
| 247 |
* Name: crypto_sign_keypair |
| 248 |
* |
| 249 |
* Description: Generates public and private key. |
| 250 |
* When MLD_CONFIG_KEYGEN_PCT is set, performs a Pairwise |
| 251 |
* Consistency Test (PCT) as required by FIPS 140-3 IG. |
| 252 |
* |
| 253 |
* Arguments: |
| 254 |
* - uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 255 |
* output public key |
| 256 |
* - uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 257 |
* output private key |
| 258 |
* |
| 259 |
* Returns: - 0: Success |
| 260 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 261 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 262 |
* - MLD_ERR_RNG_FAIL: Random number generation failed. |
| 263 |
* - MLD_ERR_FAIL: If MLD_CONFIG_KEYGEN_PCT is enabled and the |
| 264 |
* PCT check failed. |
| 265 |
* |
| 266 |
* Specification: Implements @[FIPS204 Algorithm 1 (ML-DSA.KeyGen)] |
| 267 |
* |
| 268 |
**************************************************/ |
| 269 |
MLD_API_QUALIFIER |
| 270 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 271 |
int MLD_API_NAMESPACE(keypair)( |
| 272 |
uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 273 |
uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 274 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 275 |
, |
| 276 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 277 |
#endif |
| 278 |
); |
| 279 |
|
| 280 |
/************************************************* |
| 281 |
* Name: crypto_sign_signature_internal |
| 282 |
* |
| 283 |
* Description: Computes signature. Internal API. |
| 284 |
* |
| 285 |
* Arguments: |
| 286 |
* - uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 287 |
* output signature |
| 288 |
* - size_t *siglen: pointer to output length of signature |
| 289 |
* - const uint8_t *m: pointer to message to be signed |
| 290 |
* - size_t mlen: length of message |
| 291 |
* - const uint8_t *pre: pointer to prefix string |
| 292 |
* - size_t prelen: length of prefix string |
| 293 |
* - const uint8_t rnd[MLDSA_RNDBYTES]: |
| 294 |
* random seed |
| 295 |
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 296 |
* bit-packed secret key |
| 297 |
* - int externalmu: indicates input message m is processed as mu |
| 298 |
* |
| 299 |
* Returns: |
| 300 |
* - 0: Success |
| 301 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 302 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 303 |
* - MLD_ERR_FAIL: Other kinds of failure |
| 304 |
* |
| 305 |
* If the returned value is non-zero, then the values of *sig and |
| 306 |
* *siglen should not be referenced. |
| 307 |
* |
| 308 |
* Reference: This code differs from the reference implementation |
| 309 |
* in that it adds an explicit check for nonce exhaustion |
| 310 |
* and can return -1 in that case. |
| 311 |
**************************************************/ |
| 312 |
MLD_API_QUALIFIER |
| 313 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 314 |
int MLD_API_NAMESPACE(signature_internal)( |
| 315 |
uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)], size_t *siglen, |
| 316 |
const uint8_t *m, size_t mlen, const uint8_t *pre, size_t prelen, |
| 317 |
const uint8_t rnd[MLDSA_RNDBYTES], |
| 318 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 319 |
int externalmu |
| 320 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 321 |
, |
| 322 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 323 |
#endif |
| 324 |
); |
| 325 |
|
| 326 |
/************************************************* |
| 327 |
* Name: crypto_sign_signature |
| 328 |
* |
| 329 |
* Description: Computes signature. This function implements the randomized |
| 330 |
* variant of ML-DSA. If you require the deterministic variant, |
| 331 |
* use crypto_sign_signature_internal directly. |
| 332 |
* |
| 333 |
* Arguments: |
| 334 |
* - uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 335 |
* output signature |
| 336 |
* - size_t *siglen: pointer to output length of signature |
| 337 |
* - const uint8_t *m: pointer to message to be signed |
| 338 |
* - size_t mlen: length of message |
| 339 |
* - const uint8_t *ctx: pointer to context string. |
| 340 |
* May be NULL if ctxlen == 0. |
| 341 |
* - size_t ctxlen: length of context string. |
| 342 |
* Should be <= 255. |
| 343 |
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 344 |
* bit-packed secret key |
| 345 |
* |
| 346 |
* Returns: |
| 347 |
* - 0: Success |
| 348 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 349 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 350 |
* - MLD_ERR_RNG_FAIL: Random number generation failed. |
| 351 |
* - MLD_ERR_FAIL: Other kinds of failure. |
| 352 |
* |
| 353 |
* Specification: Implements @[FIPS204 Algorithm 2 (ML-DSA.Sign)] |
| 354 |
* |
| 355 |
**************************************************/ |
| 356 |
MLD_API_QUALIFIER |
| 357 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 358 |
int MLD_API_NAMESPACE(signature)( |
| 359 |
uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)], size_t *siglen, |
| 360 |
const uint8_t *m, size_t mlen, const uint8_t *ctx, size_t ctxlen, |
| 361 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 362 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 363 |
, |
| 364 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 365 |
#endif |
| 366 |
); |
| 367 |
|
| 368 |
/************************************************* |
| 369 |
* Name: crypto_sign_signature_extmu |
| 370 |
* |
| 371 |
* Description: Computes signature. |
| 372 |
* |
| 373 |
* Arguments: |
| 374 |
* - uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 375 |
* output signature |
| 376 |
* - size_t *siglen: pointer to output length of signature |
| 377 |
* - const uint8_t mu[MLDSA_CRHBYTES]: |
| 378 |
* input mu to be signed |
| 379 |
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 380 |
* bit-packed secret key |
| 381 |
* |
| 382 |
* Returns: |
| 383 |
* - 0: Success |
| 384 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 385 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 386 |
* - MLD_ERR_RNG_FAIL: Random number generation failed. |
| 387 |
* - MLD_ERR_FAIL: Other kinds of failure. |
| 388 |
* |
| 389 |
* Specification: Implements @[FIPS204 Algorithm 2 (ML-DSA.Sign external mu |
| 390 |
* variant)] |
| 391 |
* |
| 392 |
**************************************************/ |
| 393 |
MLD_API_QUALIFIER |
| 394 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 395 |
int MLD_API_NAMESPACE(signature_extmu)( |
| 396 |
uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)], size_t *siglen, |
| 397 |
const uint8_t mu[MLDSA_CRHBYTES], |
| 398 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 399 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 400 |
, |
| 401 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 402 |
#endif |
| 403 |
); |
| 404 |
|
| 405 |
/************************************************* |
| 406 |
* Name: crypto_sign |
| 407 |
* |
| 408 |
* Description: Computes signature. This function implements the randomized |
| 409 |
* variant of ML-DSA. If you require the deterministic variant, |
| 410 |
* use crypto_sign_signature_internal directly. |
| 411 |
* |
| 412 |
* Arguments: |
| 413 |
* - uint8_t *sm: pointer to output signed message (allocated array |
| 414 |
* with MLDSA{44,65,87}_BYTES + mlen bytes), can be |
| 415 |
* equal to m |
| 416 |
* - size_t *smlen: pointer to output length of signed message |
| 417 |
* - const uint8_t *m: pointer to message to be signed |
| 418 |
* - size_t mlen: length of message |
| 419 |
* - const uint8_t *ctx: pointer to context string |
| 420 |
* - size_t ctxlen: length of context string |
| 421 |
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 422 |
* bit-packed secret key |
| 423 |
* |
| 424 |
* Returns: |
| 425 |
* - 0: Success |
| 426 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 427 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 428 |
* - MLD_ERR_FAIL: Other kinds of failure |
| 429 |
**************************************************/ |
| 430 |
MLD_API_QUALIFIER |
| 431 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 432 |
int MLD_API_NAMESPACE(sign)( |
| 433 |
uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen, |
| 434 |
const uint8_t *ctx, size_t ctxlen, |
| 435 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 436 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 437 |
, |
| 438 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 439 |
#endif |
| 440 |
); |
| 441 |
|
| 442 |
/************************************************* |
| 443 |
* Name: crypto_sign_verify_internal |
| 444 |
* |
| 445 |
* Description: Verifies signature. Internal API. |
| 446 |
* |
| 447 |
* Arguments: |
| 448 |
* - const uint8_t *sig: pointer to input signature |
| 449 |
* - size_t siglen: length of signature |
| 450 |
* - const uint8_t *m: pointer to message |
| 451 |
* - size_t mlen: length of message |
| 452 |
* - const uint8_t *pre: pointer to prefix string |
| 453 |
* - size_t prelen: length of prefix string |
| 454 |
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 455 |
* bit-packed public key |
| 456 |
* - int externalmu: indicates input message m is processed as mu |
| 457 |
* |
| 458 |
* Returns: |
| 459 |
* - 0: Success |
| 460 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 461 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 462 |
* - MLD_ERR_FAIL: Signature verification failed |
| 463 |
* |
| 464 |
* Specification: Implements @[FIPS204 Algorithm 8 (ML-DSA.Verify_internal)] |
| 465 |
* |
| 466 |
**************************************************/ |
| 467 |
MLD_API_QUALIFIER |
| 468 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 469 |
int MLD_API_NAMESPACE(verify_internal)( |
| 470 |
const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, |
| 471 |
const uint8_t *pre, size_t prelen, |
| 472 |
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 473 |
int externalmu |
| 474 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 475 |
, |
| 476 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 477 |
#endif |
| 478 |
); |
| 479 |
|
| 480 |
/************************************************* |
| 481 |
* Name: crypto_sign_verify |
| 482 |
* |
| 483 |
* Description: Verifies signature. |
| 484 |
* |
| 485 |
* Arguments: |
| 486 |
* - const uint8_t *sig: pointer to input signature |
| 487 |
* - size_t siglen: length of signature |
| 488 |
* - const uint8_t *m: pointer to message |
| 489 |
* - size_t mlen: length of message |
| 490 |
* - const uint8_t *ctx: pointer to context string. |
| 491 |
* May be NULL if ctxlen == 0. |
| 492 |
* - size_t ctxlen: length of context string |
| 493 |
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 494 |
* bit-packed public key |
| 495 |
* |
| 496 |
* Returns: |
| 497 |
* - 0: Success |
| 498 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 499 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 500 |
* - MLD_ERR_FAIL: Signature verification failed |
| 501 |
* |
| 502 |
* Specification: Implements @[FIPS204 Algorithm 3 (ML-DSA.Verify)] |
| 503 |
* |
| 504 |
**************************************************/ |
| 505 |
MLD_API_QUALIFIER |
| 506 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 507 |
int MLD_API_NAMESPACE(verify)( |
| 508 |
const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, |
| 509 |
const uint8_t *ctx, size_t ctxlen, |
| 510 |
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 511 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 512 |
, |
| 513 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 514 |
#endif |
| 515 |
); |
| 516 |
|
| 517 |
/************************************************* |
| 518 |
* Name: crypto_sign_verify_extmu |
| 519 |
* |
| 520 |
* Description: Verifies signature. |
| 521 |
* |
| 522 |
* Arguments: |
| 523 |
* - const uint8_t *sig: pointer to input signature |
| 524 |
* - size_t siglen: length of signature |
| 525 |
* - const uint8_t mu[MLDSA_CRHBYTES]: |
| 526 |
* input mu |
| 527 |
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 528 |
* bit-packed public key |
| 529 |
* |
| 530 |
* Returns: |
| 531 |
* - 0: Success |
| 532 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 533 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 534 |
* - MLD_ERR_FAIL: Signature verification failed |
| 535 |
* |
| 536 |
* Specification: Implements @[FIPS204 Algorithm 3 (ML-DSA.Verify external mu |
| 537 |
* variant)] |
| 538 |
* |
| 539 |
**************************************************/ |
| 540 |
MLD_API_QUALIFIER |
| 541 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 542 |
int MLD_API_NAMESPACE(verify_extmu)( |
| 543 |
const uint8_t *sig, size_t siglen, const uint8_t mu[MLDSA_CRHBYTES], |
| 544 |
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 545 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 546 |
, |
| 547 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 548 |
#endif |
| 549 |
); |
| 550 |
|
| 551 |
/************************************************* |
| 552 |
* Name: crypto_sign_open |
| 553 |
* |
| 554 |
* Description: Verify signed message. |
| 555 |
* |
| 556 |
* Arguments: |
| 557 |
* - uint8_t *m: pointer to output message (allocated array with |
| 558 |
* smlen bytes), can be equal to sm |
| 559 |
* - size_t *mlen: pointer to output length of message |
| 560 |
* - const uint8_t *sm: pointer to signed message |
| 561 |
* - size_t smlen: length of signed message |
| 562 |
* - const uint8_t *ctx: pointer to context string |
| 563 |
* - size_t ctxlen: length of context string |
| 564 |
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 565 |
* bit-packed public key |
| 566 |
* |
| 567 |
* Returns: |
| 568 |
* - 0: Success |
| 569 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 570 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 571 |
* - MLD_ERR_FAIL: Signature verification failed |
| 572 |
**************************************************/ |
| 573 |
MLD_API_QUALIFIER |
| 574 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 575 |
int MLD_API_NAMESPACE(open)( |
| 576 |
uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, |
| 577 |
const uint8_t *ctx, size_t ctxlen, |
| 578 |
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 579 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 580 |
, |
| 581 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 582 |
#endif |
| 583 |
); |
| 584 |
|
| 585 |
/************************************************* |
| 586 |
* Hash algorithm constants for domain separation |
| 587 |
**************************************************/ |
| 588 |
#define MLD_PREHASH_NONE 0 |
| 589 |
#define MLD_PREHASH_SHA2_224 1 |
| 590 |
#define MLD_PREHASH_SHA2_256 2 |
| 591 |
#define MLD_PREHASH_SHA2_384 3 |
| 592 |
#define MLD_PREHASH_SHA2_512 4 |
| 593 |
#define MLD_PREHASH_SHA2_512_224 5 |
| 594 |
#define MLD_PREHASH_SHA2_512_256 6 |
| 595 |
#define MLD_PREHASH_SHA3_224 7 |
| 596 |
#define MLD_PREHASH_SHA3_256 8 |
| 597 |
#define MLD_PREHASH_SHA3_384 9 |
| 598 |
#define MLD_PREHASH_SHA3_512 10 |
| 599 |
#define MLD_PREHASH_SHAKE_128 11 |
| 600 |
#define MLD_PREHASH_SHAKE_256 12 |
| 601 |
|
| 602 |
/************************************************* |
| 603 |
* Name: crypto_sign_signature_pre_hash_internal |
| 604 |
* |
| 605 |
* Description: FIPS 204: Algorithm 4 HashML-DSA.Sign. |
| 606 |
* Computes signature with pre-hashed message. |
| 607 |
* |
| 608 |
* Arguments: |
| 609 |
* - uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 610 |
* output signature |
| 611 |
* - size_t *siglen: pointer to output length of signature |
| 612 |
* - const uint8_t *ph: pointer to pre-hashed message |
| 613 |
* - size_t phlen: length of pre-hashed message |
| 614 |
* - const uint8_t *ctx: pointer to context string |
| 615 |
* - size_t ctxlen: length of context string |
| 616 |
* - const uint8_t rnd[MLDSA_RNDBYTES]: |
| 617 |
* random seed |
| 618 |
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 619 |
* bit-packed secret key |
| 620 |
* - int hashalg: hash algorithm constant (one of MLD_PREHASH_*) |
| 621 |
* |
| 622 |
* Returns: |
| 623 |
* - 0: Success |
| 624 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 625 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 626 |
* - MLD_ERR_FAIL: Other kinds of failure |
| 627 |
* |
| 628 |
* Supported hash algorithm constants: |
| 629 |
* MLD_PREHASH_SHA2_224, MLD_PREHASH_SHA2_256, MLD_PREHASH_SHA2_384, |
| 630 |
* MLD_PREHASH_SHA2_512, MLD_PREHASH_SHA2_512_224, MLD_PREHASH_SHA2_512_256, |
| 631 |
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384, |
| 632 |
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256 |
| 633 |
* |
| 634 |
* Warning: This is an unstable API that may change in the future. If you need |
| 635 |
* a stable API use crypto_sign_signature_pre_hash_shake256. |
| 636 |
**************************************************/ |
| 637 |
MLD_API_QUALIFIER |
| 638 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 639 |
int MLD_API_NAMESPACE(signature_pre_hash_internal)( |
| 640 |
uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)], size_t *siglen, |
| 641 |
const uint8_t *ph, size_t phlen, const uint8_t *ctx, size_t ctxlen, |
| 642 |
const uint8_t rnd[MLDSA_RNDBYTES], |
| 643 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 644 |
int hashalg |
| 645 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 646 |
, |
| 647 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 648 |
#endif |
| 649 |
); |
| 650 |
|
| 651 |
/************************************************* |
| 652 |
* Name: crypto_sign_verify_pre_hash_internal |
| 653 |
* |
| 654 |
* Description: FIPS 204: Algorithm 5 HashML-DSA.Verify. |
| 655 |
* Verifies signature with pre-hashed message. |
| 656 |
* |
| 657 |
* Arguments: |
| 658 |
* - const uint8_t *sig: pointer to input signature |
| 659 |
* - size_t siglen: length of signature |
| 660 |
* - const uint8_t *ph: pointer to pre-hashed message |
| 661 |
* - size_t phlen: length of pre-hashed message |
| 662 |
* - const uint8_t *ctx: pointer to context string |
| 663 |
* - size_t ctxlen: length of context string |
| 664 |
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 665 |
* bit-packed public key |
| 666 |
* - int hashalg: hash algorithm constant (one of MLD_PREHASH_*) |
| 667 |
* |
| 668 |
* Returns: - 0: Success |
| 669 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 670 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 671 |
* - MLD_ERR_FAIL: Signature verification failed |
| 672 |
* |
| 673 |
* Supported hash algorithm constants: |
| 674 |
* MLD_PREHASH_SHA2_224, MLD_PREHASH_SHA2_256, MLD_PREHASH_SHA2_384, |
| 675 |
* MLD_PREHASH_SHA2_512, MLD_PREHASH_SHA2_512_224, MLD_PREHASH_SHA2_512_256, |
| 676 |
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384, |
| 677 |
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256 |
| 678 |
* |
| 679 |
* Warning: This is an unstable API that may change in the future. If you need |
| 680 |
* a stable API use crypto_sign_verify_pre_hash_shake256. |
| 681 |
**************************************************/ |
| 682 |
MLD_API_QUALIFIER |
| 683 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 684 |
int MLD_API_NAMESPACE(verify_pre_hash_internal)( |
| 685 |
const uint8_t *sig, size_t siglen, const uint8_t *ph, size_t phlen, |
| 686 |
const uint8_t *ctx, size_t ctxlen, |
| 687 |
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 688 |
int hashalg |
| 689 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 690 |
, |
| 691 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 692 |
#endif |
| 693 |
); |
| 694 |
|
| 695 |
/************************************************* |
| 696 |
* Name: crypto_sign_signature_pre_hash_shake256 |
| 697 |
* |
| 698 |
* Description: FIPS 204: Algorithm 4 HashML-DSA.Sign with SHAKE256. |
| 699 |
* Computes signature with pre-hashed message using SHAKE256. |
| 700 |
* This function computes the SHAKE256 hash of the message |
| 701 |
* internally. |
| 702 |
* |
| 703 |
* Arguments: |
| 704 |
* - uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 705 |
* output signature |
| 706 |
* - size_t *siglen: pointer to output length of signature |
| 707 |
* - const uint8_t *m: pointer to message to be hashed and signed |
| 708 |
* - size_t mlen: length of message |
| 709 |
* - const uint8_t *ctx: pointer to context string |
| 710 |
* - size_t ctxlen: length of context string |
| 711 |
* - const uint8_t rnd[MLDSA_RNDBYTES]: |
| 712 |
* random seed |
| 713 |
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 714 |
* bit-packed secret key |
| 715 |
* |
| 716 |
* Returns: |
| 717 |
* - 0: Success |
| 718 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 719 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 720 |
* - MLD_ERR_FAIL: Other kinds of failure |
| 721 |
**************************************************/ |
| 722 |
MLD_API_QUALIFIER |
| 723 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 724 |
int MLD_API_NAMESPACE(signature_pre_hash_shake256)( |
| 725 |
uint8_t sig[MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)], size_t *siglen, |
| 726 |
const uint8_t *m, size_t mlen, const uint8_t *ctx, size_t ctxlen, |
| 727 |
const uint8_t rnd[MLDSA_RNDBYTES], |
| 728 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 729 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 730 |
, |
| 731 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 732 |
#endif |
| 733 |
); |
| 734 |
|
| 735 |
/************************************************* |
| 736 |
* Name: crypto_sign_verify_pre_hash_shake256 |
| 737 |
* |
| 738 |
* Description: FIPS 204: Algorithm 5 HashML-DSA.Verify with SHAKE256. |
| 739 |
* Verifies signature with pre-hashed message using SHAKE256. |
| 740 |
* This function computes the SHAKE256 hash of the message |
| 741 |
*internally. |
| 742 |
* |
| 743 |
* Arguments: |
| 744 |
* - const uint8_t *sig: pointer to input signature |
| 745 |
* - size_t siglen: length of signature |
| 746 |
* - const uint8_t *m: pointer to message to be hashed and verified |
| 747 |
* - size_t mlen: length of message |
| 748 |
* - const uint8_t *ctx: pointer to context string |
| 749 |
* - size_t ctxlen: length of context string |
| 750 |
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]: |
| 751 |
* bit-packed public key |
| 752 |
* |
| 753 |
* Returns: |
| 754 |
* - 0: Success |
| 755 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 756 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 757 |
* - MLD_ERR_FAIL: Signature verification failed |
| 758 |
**************************************************/ |
| 759 |
MLD_API_QUALIFIER |
| 760 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 761 |
int MLD_API_NAMESPACE(verify_pre_hash_shake256)( |
| 762 |
const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, |
| 763 |
const uint8_t *ctx, size_t ctxlen, |
| 764 |
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 765 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 766 |
, |
| 767 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 768 |
#endif |
| 769 |
); |
| 770 |
|
| 771 |
/* Maximum formatted domain separation message length */ |
| 772 |
#define MLD_DOMAIN_SEPARATION_MAX_BYTES (2 + 255 + 11 + 64) |
| 773 |
|
| 774 |
/************************************************* |
| 775 |
* Name: mld_prepare_domain_separation_prefix |
| 776 |
* |
| 777 |
* Description: Prepares domain separation prefix for ML-DSA signing. |
| 778 |
* For pure ML-DSA (hashalg == MLD_PREHASH_NONE): |
| 779 |
* Format: 0x00 || ctxlen (1 byte) || ctx |
| 780 |
* For HashML-DSA (hashalg != MLD_PREHASH_NONE): |
| 781 |
* Format: 0x01 || ctxlen (1 byte) || ctx || oid (11 bytes) || ph |
| 782 |
* |
| 783 |
* Arguments: - uint8_t prefix[MLD_DOMAIN_SEPARATION_MAX_BYTES]: |
| 784 |
* output domain separation prefix buffer |
| 785 |
* - const uint8_t *ph: pointer to pre-hashed message |
| 786 |
* (ignored for pure ML-DSA) |
| 787 |
* - size_t phlen: length of pre-hashed message |
| 788 |
* (ignored for pure ML-DSA) |
| 789 |
* - const uint8_t *ctx: pointer to context string (may be NULL) |
| 790 |
* - size_t ctxlen: length of context string |
| 791 |
* - int hashalg: hash algorithm constant |
| 792 |
* (MLD_PREHASH_NONE for pure ML-DSA, or MLD_PREHASH_* for |
| 793 |
* HashML-DSA) |
| 794 |
* |
| 795 |
* Returns the total length of the formatted prefix, or 0 on error. |
| 796 |
* |
| 797 |
* This function is useful for building incremental signing APIs. |
| 798 |
* |
| 799 |
* Specification: |
| 800 |
* - For HashML-DSA (hashalg != MLD_PREHASH_NONE), implements |
| 801 |
* @[FIPS204, Algorithm 4, L23] |
| 802 |
* - For Pure ML-DSA (hashalg == MLD_PREHASH_NONE), implements |
| 803 |
* ``` |
| 804 |
* M' <- BytesToBits(IntegerToBytes(0, 1) |
| 805 |
* || IntegerToBytes(|ctx|, 1) |
| 806 |
* || ctx |
| 807 |
* ``` |
| 808 |
* which is part of @[FIPS204, Algorithm 2 (ML-DSA.Sign), L10] and |
| 809 |
* @[FIPS204, Algorithm 3 (ML-DSA.Verify), L5]. |
| 810 |
* |
| 811 |
**************************************************/ |
| 812 |
MLD_API_QUALIFIER |
| 813 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 814 |
size_t MLD_API_NAMESPACE(prepare_domain_separation_prefix)( |
| 815 |
uint8_t prefix[MLD_DOMAIN_SEPARATION_MAX_BYTES], const uint8_t *ph, |
| 816 |
size_t phlen, const uint8_t *ctx, size_t ctxlen, int hashalg); |
| 817 |
|
| 818 |
/************************************************* |
| 819 |
* Name: crypto_sign_pk_from_sk |
| 820 |
* |
| 821 |
* Description: Performs basic validity checks on secret key, and derives |
| 822 |
* public key. |
| 823 |
* |
| 824 |
* Referring to the decoding of the secret key |
| 825 |
* `sk=(rho, K, tr, s1, s2, t0)` |
| 826 |
* (cf. [@FIPS204, Algorithm 25 skDecode]), |
| 827 |
* the following checks are performed: |
| 828 |
* - Check that s1 and s2 have coefficients in |
| 829 |
* [-MLDSA_ETA, MLDSA_ETA] |
| 830 |
* - Check that t0 and tr stored in sk match recomputed values. |
| 831 |
* |
| 832 |
* Arguments: - uint8_t pk[CRYPTO_PUBLICKEYBYTES]: output public key |
| 833 |
* - const uint8_t sk[CRYPTO_SECRETKEYBYTES]: input secret key |
| 834 |
* |
| 835 |
* Returns: - 0: Success |
| 836 |
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is |
| 837 |
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL. |
| 838 |
* - MLD_ERR_FAIL: Secret key validation failed |
| 839 |
* |
| 840 |
* Note: This function leaks whether the secret key is valid or invalid |
| 841 |
* through its return value and timing. |
| 842 |
**************************************************/ |
| 843 |
MLD_API_QUALIFIER |
| 844 |
MLD_API_MUST_CHECK_RETURN_VALUE |
| 845 |
int MLD_API_NAMESPACE(pk_from_sk)( |
| 846 |
uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)], |
| 847 |
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)] |
| 848 |
#ifdef MLD_CONFIG_CONTEXT_PARAMETER |
| 849 |
, |
| 850 |
MLD_CONFIG_CONTEXT_PARAMETER_TYPE context |
| 851 |
#endif |
| 852 |
); |
| 853 |
|
| 854 |
#ifdef __cplusplus |
| 855 |
} |
| 856 |
#endif |
| 857 |
|
| 858 |
/****************************** SUPERCOP API *********************************/ |
| 859 |
|
| 860 |
#if !defined(MLD_CONFIG_API_NO_SUPERCOP) |
| 861 |
/* Export API in SUPERCOP naming scheme CRYPTO_xxx / crypto_sign_xxx */ |
| 862 |
#define CRYPTO_SECRETKEYBYTES MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET) |
| 863 |
#define CRYPTO_PUBLICKEYBYTES MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET) |
| 864 |
#define CRYPTO_BYTES MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET) |
| 865 |
|
| 866 |
#define crypto_sign_keypair MLD_API_NAMESPACE(keypair) |
| 867 |
#define crypto_sign_signature MLD_API_NAMESPACE(signature) |
| 868 |
#define crypto_sign MLD_API_NAMESPACE(sign) |
| 869 |
#define crypto_sign_verify MLD_API_NAMESPACE(verify) |
| 870 |
#define crypto_sign_open MLD_API_NAMESPACE(open) |
| 871 |
|
| 872 |
#else /* !MLD_CONFIG_API_NO_SUPERCOP */ |
| 873 |
|
| 874 |
/* If the SUPERCOP API is not needed, we can undefine the various helper macros |
| 875 |
* above. Otherwise, they are needed for lazy evaluation of crypto_sign_xxx. */ |
| 876 |
#if !defined(MLD_API_LEGACY_CONFIG) |
| 877 |
#undef MLD_CONFIG_API_PARAMETER_SET |
| 878 |
#undef MLD_CONFIG_API_NAMESPACE_PREFIX |
| 879 |
#undef MLD_CONFIG_API_NO_SUPERCOP |
| 880 |
#undef MLD_CONFIG_API_CONSTANTS_ONLY |
| 881 |
#undef MLD_CONFIG_API_QUALIFIER |
| 882 |
#endif /* !MLD_API_LEGACY_CONFIG */ |
| 883 |
|
| 884 |
#undef MLD_API_CONCAT |
| 885 |
#undef MLD_API_CONCAT_ |
| 886 |
#undef MLD_API_CONCAT_UNDERSCORE |
| 887 |
#undef MLD_API_NAMESPACE |
| 888 |
#undef MLD_API_MUST_CHECK_RETURN_VALUE |
| 889 |
#undef MLD_API_QUALIFIER |
| 890 |
#undef MLD_API_LEGACY_CONFIG |
| 891 |
|
| 892 |
#endif /* MLD_CONFIG_API_NO_SUPERCOP */ |
| 893 |
#endif /* !MLD_CONFIG_API_CONSTANTS_ONLY */ |
| 894 |
|
| 895 |
|
| 896 |
/***************************** Memory Usage **********************************/ |
| 897 |
|
| 898 |
/* |
| 899 |
* By default mldsa-native performs all memory allocations on the stack. |
| 900 |
* Alternatively, mldsa-native supports custom allocation of large structures |
| 901 |
* through the `MLD_CONFIG_CUSTOM_ALLOC_FREE` configuration option. |
| 902 |
* See mldsa_native_config.h for details. |
| 903 |
* |
| 904 |
* `MLD_TOTAL_ALLOC_{44,65,87}_{KEYPAIR,SIGN,VERIFY}` indicates the maximum |
| 905 |
* (accumulative) allocation via MLD_ALLOC for each parameter set and operation. |
| 906 |
* Note that some stack allocation remains even |
| 907 |
* when using custom allocators, so these values are lower than total stack |
| 908 |
* usage with the default stack-only allocation. |
| 909 |
* |
| 910 |
* These constants may be used to implement custom allocations using a |
| 911 |
* fixed-sized buffer and a simple allocator (e.g., bump allocator). |
| 912 |
*/ |
| 913 |
/* check-magic: off */ |
| 914 |
#if defined(MLD_API_LEGACY_CONFIG) || !defined(MLD_CONFIG_REDUCE_RAM) |
| 915 |
#define MLD_TOTAL_ALLOC_44_KEYPAIR_NO_PCT 45248 |
| 916 |
#define MLD_TOTAL_ALLOC_44_KEYPAIR_PCT 56640 |
| 917 |
#define MLD_TOTAL_ALLOC_44_SIGN 52896 |
| 918 |
#define MLD_TOTAL_ALLOC_44_VERIFY 38816 |
| 919 |
#define MLD_TOTAL_ALLOC_65_KEYPAIR_NO_PCT 71872 |
| 920 |
#define MLD_TOTAL_ALLOC_65_KEYPAIR_PCT 85856 |
| 921 |
#define MLD_TOTAL_ALLOC_65_SIGN 80576 |
| 922 |
#define MLD_TOTAL_ALLOC_65_VERIFY 62432 |
| 923 |
#define MLD_TOTAL_ALLOC_87_KEYPAIR_NO_PCT 112832 |
| 924 |
#define MLD_TOTAL_ALLOC_87_KEYPAIR_PCT 130816 |
| 925 |
#define MLD_TOTAL_ALLOC_87_SIGN 123584 |
| 926 |
#define MLD_TOTAL_ALLOC_87_VERIFY 99552 |
| 927 |
#else /* MLD_API_LEGACY_CONFIG || !MLD_CONFIG_REDUCE_RAM */ |
| 928 |
#define MLD_TOTAL_ALLOC_44_KEYPAIR_NO_PCT 32992 |
| 929 |
#define MLD_TOTAL_ALLOC_44_KEYPAIR_PCT 36192 |
| 930 |
#define MLD_TOTAL_ALLOC_44_SIGN 32448 |
| 931 |
#define MLD_TOTAL_ALLOC_44_VERIFY 22464 |
| 932 |
#define MLD_TOTAL_ALLOC_65_KEYPAIR_NO_PCT 46304 |
| 933 |
#define MLD_TOTAL_ALLOC_65_KEYPAIR_PCT 50048 |
| 934 |
#define MLD_TOTAL_ALLOC_65_SIGN 44768 |
| 935 |
#define MLD_TOTAL_ALLOC_65_VERIFY 30720 |
| 936 |
#define MLD_TOTAL_ALLOC_87_KEYPAIR_NO_PCT 62688 |
| 937 |
#define MLD_TOTAL_ALLOC_87_KEYPAIR_PCT 66336 |
| 938 |
#define MLD_TOTAL_ALLOC_87_SIGN 59104 |
| 939 |
#define MLD_TOTAL_ALLOC_87_VERIFY 41216 |
| 940 |
#endif /* !(MLD_API_LEGACY_CONFIG || !MLD_CONFIG_REDUCE_RAM) */ |
| 941 |
/* check-magic: on */ |
| 942 |
|
| 943 |
/* |
| 944 |
* MLD_TOTAL_ALLOC_*_KEYPAIR adapts based on MLD_CONFIG_KEYGEN_PCT. |
| 945 |
* For legacy config, we don't know which options are used, so assume |
| 946 |
* the worst case (PCT enabled). |
| 947 |
*/ |
| 948 |
#if defined(MLD_API_LEGACY_CONFIG) || defined(MLD_CONFIG_KEYGEN_PCT) |
| 949 |
#define MLD_TOTAL_ALLOC_44_KEYPAIR MLD_TOTAL_ALLOC_44_KEYPAIR_PCT |
| 950 |
#define MLD_TOTAL_ALLOC_65_KEYPAIR MLD_TOTAL_ALLOC_65_KEYPAIR_PCT |
| 951 |
#define MLD_TOTAL_ALLOC_87_KEYPAIR MLD_TOTAL_ALLOC_87_KEYPAIR_PCT |
| 952 |
#else |
| 953 |
#define MLD_TOTAL_ALLOC_44_KEYPAIR MLD_TOTAL_ALLOC_44_KEYPAIR_NO_PCT |
| 954 |
#define MLD_TOTAL_ALLOC_65_KEYPAIR MLD_TOTAL_ALLOC_65_KEYPAIR_NO_PCT |
| 955 |
#define MLD_TOTAL_ALLOC_87_KEYPAIR MLD_TOTAL_ALLOC_87_KEYPAIR_NO_PCT |
| 956 |
#endif |
| 957 |
|
| 958 |
#define MLD_MAX3_(a, b, c) \ |
| 959 |
((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c))) |
| 960 |
|
| 961 |
/* |
| 962 |
* `MLD_TOTAL_ALLOC_{44,65,87}` is the maximum across all operations for each |
| 963 |
* parameter set. |
| 964 |
*/ |
| 965 |
#define MLD_TOTAL_ALLOC_44 \ |
| 966 |
MLD_MAX3_(MLD_TOTAL_ALLOC_44_KEYPAIR, MLD_TOTAL_ALLOC_44_SIGN, \ |
| 967 |
MLD_TOTAL_ALLOC_44_VERIFY) |
| 968 |
#define MLD_TOTAL_ALLOC_65 \ |
| 969 |
MLD_MAX3_(MLD_TOTAL_ALLOC_65_KEYPAIR, MLD_TOTAL_ALLOC_65_SIGN, \ |
| 970 |
MLD_TOTAL_ALLOC_65_VERIFY) |
| 971 |
#define MLD_TOTAL_ALLOC_87 \ |
| 972 |
MLD_MAX3_(MLD_TOTAL_ALLOC_87_KEYPAIR, MLD_TOTAL_ALLOC_87_SIGN, \ |
| 973 |
MLD_TOTAL_ALLOC_87_VERIFY) |
| 974 |
|
| 975 |
#endif /* !MLD_H */ |