| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <limits.h> |
| 6 |
#include <stddef.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include "src/common.h" |
| 11 |
|
| 12 |
#include "mldsa_native.h" |
| 13 |
|
| 14 |
|
| 15 |
#define crypto_sign_keypair_internal MLD_API_NAMESPACE(keypair_internal) |
| 16 |
#define crypto_sign_signature_internal MLD_API_NAMESPACE(signature_internal) |
| 17 |
#define crypto_sign_verify_internal MLD_API_NAMESPACE(verify_internal) |
| 18 |
#define crypto_sign_verify_extmu MLD_API_NAMESPACE(verify_extmu) |
| 19 |
#define crypto_sign_signature_pre_hash_internal \ |
| 20 |
MLD_API_NAMESPACE(signature_pre_hash_internal) |
| 21 |
#define crypto_sign_verify_pre_hash_internal \ |
| 22 |
MLD_API_NAMESPACE(verify_pre_hash_internal) |
| 23 |
#define crypto_sign_signature_pre_hash_shake256 \ |
| 24 |
MLD_API_NAMESPACE(signature_pre_hash_shake256) |
| 25 |
#define crypto_sign_verify_pre_hash_shake256 \ |
| 26 |
MLD_API_NAMESPACE(verify_pre_hash_shake256) |
| 27 |
|
| 28 |
#define USAGE "acvp_mldsa{lvl} [keyGen|sigGen|sigVer] {test specific arguments}" |
| 29 |
#define KEYGEN_USAGE "acvp_mldsa{lvl} keyGen seed=HEX" |
| 30 |
#define SIGGEN_USAGE \ |
| 31 |
"acvp_mldsa{lvl} sigGen message=HEX sk=HEX context=HEX rnd=HEX" |
| 32 |
#define SIGGEN_INTERNAL_USAGE \ |
| 33 |
"acvp_mldsa{lvl} sigGenInternal message=HEX sk=HEX externalMu=0/1 rnd=HEX" |
| 34 |
#define SIGGEN_DETERMINISTIC_USAGE \ |
| 35 |
"acvp_mldsa{lvl} sigGenDeterministic message=HEX sk=HEX context=HEX" |
| 36 |
#define SIGGEN_INTERNAL_DETERMINISTIC_USAGE \ |
| 37 |
"acvp_mldsa{lvl} sigGenInternalDeterministic message=HEX sk=HEX " \ |
| 38 |
"externalMu=0/1" |
| 39 |
#define SIGGEN_PREHASH_DETERMINISTIC_USAGE \ |
| 40 |
"acvp_mldsa{lvl} sigGenPreHashDeterministic ph=HEX context=HEX sk=HEX " \ |
| 41 |
"hashAlg=STRING" |
| 42 |
#define SIGGEN_PREHASH_SHAKE256_DETERMINISTIC_USAGE \ |
| 43 |
"acvp_mldsa{lvl} sigGenPreHashShake256Deterministic message=HEX " \ |
| 44 |
"context=HEX sk=HEX" |
| 45 |
#define SIGVER_USAGE \ |
| 46 |
"acvp_mldsa{lvl} sigVer message=HEX context=HEX signature=HEX pk=HEX" |
| 47 |
#define SIGVER_INTERNAL_USAGE \ |
| 48 |
"acvp_mldsa{lvl} sigVerInternal message=HEX signature=HEX pk=HEX " \ |
| 49 |
"externalMu=0/1" |
| 50 |
#define SIGGEN_PREHASH_USAGE \ |
| 51 |
"acvp_mldsa{lvl} sigGenPreHash ph=HEX context=HEX sk=HEX " \ |
| 52 |
"hashAlg=STRING rnd=HEX" |
| 53 |
#define SIGVER_PREHASH_USAGE \ |
| 54 |
"acvp_mldsa{lvl} sigVerPreHash ph=HEX context=HEX signature=HEX pk=HEX " \ |
| 55 |
"hashAlg=STRING" |
| 56 |
#define SIGGEN_PREHASH_SHAKE256_USAGE \ |
| 57 |
"acvp_mldsa{lvl} sigGenPreHashShake256 message=HEX context=HEX " \ |
| 58 |
"sk=HEX rnd=HEX" |
| 59 |
#define SIGVER_PREHASH_SHAKE256_USAGE \ |
| 60 |
"acvp_mldsa{lvl} sigVerPreHashShake256 message=HEX context=HEX " \ |
| 61 |
"signature=HEX pk=HEX" |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define MAX_MSG_LENGTH 65536 |
| 66 |
|
| 67 |
#define MAX_CTX_LENGTH 255 |
| 68 |
|
| 69 |
#define CHECK(x) \ |
| 70 |
do \ |
| 71 |
{ \ |
| 72 |
int rc; \ |
| 73 |
rc = (x); \ |
| 74 |
if (!rc) \ |
| 75 |
{ \ |
| 76 |
fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \ |
| 77 |
exit(1); \ |
| 78 |
} \ |
| 79 |
} while (0) |
| 80 |
|
| 81 |
typedef enum |
| 82 |
{ |
| 83 |
keyGen, |
| 84 |
sigGen, |
| 85 |
sigGenInternal, |
| 86 |
sigGenDeterministic, |
| 87 |
sigGenInternalDeterministic, |
| 88 |
sigGenPreHashDeterministic, |
| 89 |
sigGenPreHashShake256Deterministic, |
| 90 |
sigVer, |
| 91 |
sigVerInternal, |
| 92 |
sigGenPreHash, |
| 93 |
sigVerPreHash, |
| 94 |
sigGenPreHashShake256, |
| 95 |
sigVerPreHashShake256 |
| 96 |
} acvp_mode; |
| 97 |
|
| 98 |
|
| 99 |
static unsigned char decode_hex_char(char hex) |
| 100 |
{ |
| 101 |
if (hex >= '0' && hex <= '9') |
| 102 |
{ |
| 103 |
return (unsigned char)(hex - '0'); |
| 104 |
} |
| 105 |
else if (hex >= 'A' && hex <= 'F') |
| 106 |
{ |
| 107 |
return (unsigned char)(10 + (unsigned char)(hex - 'A')); |
| 108 |
} |
| 109 |
else if (hex >= 'a' && hex <= 'f') |
| 110 |
{ |
| 111 |
return (unsigned char)(10 + (unsigned char)(hex - 'a')); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
return 0xFF; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
static int decode_hex(const char *prefix, unsigned char *out, size_t out_len, |
| 120 |
const char *hex) |
| 121 |
{ |
| 122 |
size_t i; |
| 123 |
size_t hex_len = strlen(hex); |
| 124 |
size_t prefix_len = strlen(prefix); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if (hex_len < prefix_len + 1 || memcmp(prefix, hex, prefix_len) != 0 || |
| 131 |
hex[prefix_len] != '=') |
| 132 |
{ |
| 133 |
goto hex_usage; |
| 134 |
} |
| 135 |
|
| 136 |
hex += prefix_len + 1; |
| 137 |
hex_len -= prefix_len + 1; |
| 138 |
|
| 139 |
if (hex_len != 2 * out_len) |
| 140 |
{ |
| 141 |
goto hex_usage; |
| 142 |
} |
| 143 |
|
| 144 |
for (i = 0; i < out_len; i++, hex += 2, out++) |
| 145 |
{ |
| 146 |
unsigned hex0 = decode_hex_char(hex[0]); |
| 147 |
unsigned hex1 = decode_hex_char(hex[1]); |
| 148 |
if (hex0 == 0xFF || hex1 == 0xFF) |
| 149 |
{ |
| 150 |
goto hex_usage; |
| 151 |
} |
| 152 |
|
| 153 |
*out = ((hex0 << 4) | hex1) & 0XFF; |
| 154 |
} |
| 155 |
|
| 156 |
return 0; |
| 157 |
|
| 158 |
hex_usage: |
| 159 |
fprintf(stderr, |
| 160 |
"Argument %s invalid: Expected argument of the form '%s=HEX' with " |
| 161 |
"HEX being a hex encoding of %u bytes\n", |
| 162 |
hex, prefix, (unsigned)out_len); |
| 163 |
return 1; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
static int decode_keyed_int(const char *prefix_string, int *out, |
| 168 |
const char *str) |
| 169 |
{ |
| 170 |
size_t str_len = strlen(str); |
| 171 |
size_t prefix_len = strlen(prefix_string); |
| 172 |
char *endptr; |
| 173 |
long val; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (str_len < prefix_len + 1 || memcmp(prefix_string, str, prefix_len) != 0 || |
| 180 |
str[prefix_len] != '=') |
| 181 |
{ |
| 182 |
goto int_usage; |
| 183 |
} |
| 184 |
|
| 185 |
str += prefix_len + 1; |
| 186 |
|
| 187 |
|
| 188 |
val = strtol(str, &endptr, 10); |
| 189 |
|
| 190 |
|
| 191 |
if (*endptr != '\0' || endptr == str) |
| 192 |
{ |
| 193 |
goto int_usage; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
if (val > INT_MAX || val < INT_MIN) |
| 198 |
{ |
| 199 |
goto int_usage; |
| 200 |
} |
| 201 |
|
| 202 |
*out = (int)val; |
| 203 |
return 0; |
| 204 |
|
| 205 |
int_usage: |
| 206 |
fprintf(stderr, |
| 207 |
"Argument %s invalid: Expected argument of the form '%s=INT' with " |
| 208 |
"INT being a decimal integer\n", |
| 209 |
str, prefix_string); |
| 210 |
return 1; |
| 211 |
} |
| 212 |
|
| 213 |
static int parse_str(const char *prefix, char *out, size_t out_max_len, |
| 214 |
const char *str) |
| 215 |
{ |
| 216 |
size_t str_len = strlen(str); |
| 217 |
size_t prefix_len = strlen(prefix); |
| 218 |
size_t value_len; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if (str_len < prefix_len + 1 || memcmp(prefix, str, prefix_len) != 0 || |
| 225 |
str[prefix_len] != '=') |
| 226 |
{ |
| 227 |
goto str_usage; |
| 228 |
} |
| 229 |
|
| 230 |
str += prefix_len + 1; |
| 231 |
value_len = strlen(str); |
| 232 |
|
| 233 |
if (value_len >= out_max_len) |
| 234 |
{ |
| 235 |
fprintf(stderr, |
| 236 |
"Argument %s invalid: String value too long (max %u characters)\n", |
| 237 |
str - prefix_len - 1, (unsigned)(out_max_len - 1)); |
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
strncpy(out, str, out_max_len - 1); |
| 242 |
out[out_max_len - 1] = '\0'; |
| 243 |
return 0; |
| 244 |
|
| 245 |
str_usage: |
| 246 |
fprintf(stderr, |
| 247 |
"Argument %s invalid: Expected argument of the form '%s=STRING'\n", |
| 248 |
str, prefix); |
| 249 |
return 1; |
| 250 |
} |
| 251 |
|
| 252 |
static void print_hex(const char *name, const unsigned char *raw, size_t len) |
| 253 |
{ |
| 254 |
if (name != NULL) |
| 255 |
{ |
| 256 |
printf("%s=", name); |
| 257 |
} |
| 258 |
for (; len > 0; len--, raw++) |
| 259 |
{ |
| 260 |
printf("%02X", *raw); |
| 261 |
} |
| 262 |
printf("\n"); |
| 263 |
} |
| 264 |
|
| 265 |
static void acvp_mldsa_keyGen_AFT(const unsigned char seed[MLDSA_RNDBYTES]) |
| 266 |
{ |
| 267 |
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; |
| 268 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 269 |
|
| 270 |
CHECK(crypto_sign_keypair_internal(pk, sk, seed) == 0); |
| 271 |
|
| 272 |
print_hex("pk", pk, sizeof(pk)); |
| 273 |
print_hex("sk", sk, sizeof(sk)); |
| 274 |
} |
| 275 |
|
| 276 |
static void acvp_mldsa_sigGen_AFT(const unsigned char *message, size_t mlen, |
| 277 |
const unsigned char rnd[MLDSA_SEEDBYTES], |
| 278 |
const unsigned char sk[CRYPTO_SECRETKEYBYTES], |
| 279 |
const unsigned char *context, size_t ctxlen) |
| 280 |
{ |
| 281 |
unsigned char sig[CRYPTO_BYTES]; |
| 282 |
size_t siglen; |
| 283 |
unsigned char pre[MAX_CTX_LENGTH + 2]; |
| 284 |
|
| 285 |
CHECK(ctxlen <= 255); |
| 286 |
pre[0] = 0; |
| 287 |
|
| 288 |
pre[1] = (uint8_t)ctxlen; |
| 289 |
memcpy(pre + 2, context, ctxlen); |
| 290 |
|
| 291 |
CHECK(crypto_sign_signature_internal(sig, &siglen, message, mlen, pre, |
| 292 |
ctxlen + 2, rnd, sk, 0) == 0); |
| 293 |
print_hex("signature", sig, sizeof(sig)); |
| 294 |
} |
| 295 |
|
| 296 |
static void acvp_mldsa_sigGenInternal_AFT( |
| 297 |
const unsigned char *message, size_t mlen, |
| 298 |
const unsigned char rnd[MLDSA_SEEDBYTES], |
| 299 |
const unsigned char sk[CRYPTO_SECRETKEYBYTES], int externalMu) |
| 300 |
{ |
| 301 |
unsigned char sig[CRYPTO_BYTES]; |
| 302 |
size_t siglen; |
| 303 |
CHECK(crypto_sign_signature_internal(sig, &siglen, message, mlen, NULL, 0, |
| 304 |
rnd, sk, externalMu) == 0); |
| 305 |
print_hex("signature", sig, sizeof(sig)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
static void acvp_mldsa_sigGenDeterministic_AFT( |
| 311 |
const unsigned char *message, size_t mlen, |
| 312 |
const unsigned char sk[CRYPTO_SECRETKEYBYTES], const unsigned char *context, |
| 313 |
size_t ctxlen) |
| 314 |
{ |
| 315 |
unsigned char sig[CRYPTO_BYTES]; |
| 316 |
size_t siglen; |
| 317 |
unsigned char rnd[MLDSA_SEEDBYTES] = {0}; |
| 318 |
|
| 319 |
unsigned char pre[MAX_CTX_LENGTH + 2]; |
| 320 |
|
| 321 |
CHECK(ctxlen <= 255); |
| 322 |
pre[0] = 0; |
| 323 |
|
| 324 |
pre[1] = (uint8_t)ctxlen; |
| 325 |
memcpy(pre + 2, context, ctxlen); |
| 326 |
|
| 327 |
CHECK(crypto_sign_signature_internal(sig, &siglen, message, mlen, pre, |
| 328 |
ctxlen + 2, rnd, sk, 0) == 0); |
| 329 |
print_hex("signature", sig, sizeof(sig)); |
| 330 |
} |
| 331 |
|
| 332 |
static void acvp_mldsa_sigGenInternalDeterministic_AFT( |
| 333 |
const unsigned char *message, size_t mlen, |
| 334 |
const unsigned char sk[CRYPTO_SECRETKEYBYTES], int externalMu) |
| 335 |
{ |
| 336 |
unsigned char sig[CRYPTO_BYTES]; |
| 337 |
size_t siglen; |
| 338 |
unsigned char rnd[MLDSA_SEEDBYTES] = {0}; |
| 339 |
|
| 340 |
CHECK(crypto_sign_signature_internal(sig, &siglen, message, mlen, NULL, 0, |
| 341 |
rnd, sk, externalMu) == 0); |
| 342 |
print_hex("signature", sig, sizeof(sig)); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
static int acvp_mldsa_sigVer_AFT(const unsigned char *message, size_t mlen, |
| 347 |
const unsigned char *context, size_t ctxlen, |
| 348 |
const unsigned char signature[CRYPTO_BYTES], |
| 349 |
const unsigned char pk[CRYPTO_PUBLICKEYBYTES]) |
| 350 |
{ |
| 351 |
return crypto_sign_verify(signature, CRYPTO_BYTES, message, mlen, context, |
| 352 |
ctxlen, pk); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
static int acvp_mldsa_sigVerInternal_AFT( |
| 357 |
const unsigned char *message, size_t mlen, |
| 358 |
const unsigned char signature[CRYPTO_BYTES], |
| 359 |
const unsigned char pk[CRYPTO_PUBLICKEYBYTES], int externalMu) |
| 360 |
{ |
| 361 |
if (externalMu) |
| 362 |
{ |
| 363 |
return crypto_sign_verify_extmu(signature, CRYPTO_BYTES, message, pk); |
| 364 |
} |
| 365 |
else |
| 366 |
{ |
| 367 |
return crypto_sign_verify_internal(signature, CRYPTO_BYTES, message, mlen, |
| 368 |
NULL, 0, pk, 0); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
static int str_to_hash_alg(const char *hashAlg) |
| 373 |
{ |
| 374 |
if (strcmp(hashAlg, "SHA2-224") == 0) |
| 375 |
{ |
| 376 |
return MLD_PREHASH_SHA2_224; |
| 377 |
} |
| 378 |
if (strcmp(hashAlg, "SHA2-256") == 0) |
| 379 |
{ |
| 380 |
return MLD_PREHASH_SHA2_256; |
| 381 |
} |
| 382 |
if (strcmp(hashAlg, "SHA2-384") == 0) |
| 383 |
{ |
| 384 |
return MLD_PREHASH_SHA2_384; |
| 385 |
} |
| 386 |
if (strcmp(hashAlg, "SHA2-512") == 0) |
| 387 |
{ |
| 388 |
return MLD_PREHASH_SHA2_512; |
| 389 |
} |
| 390 |
if (strcmp(hashAlg, "SHA2-512/224") == 0) |
| 391 |
{ |
| 392 |
return MLD_PREHASH_SHA2_512_224; |
| 393 |
} |
| 394 |
if (strcmp(hashAlg, "SHA2-512/256") == 0) |
| 395 |
{ |
| 396 |
return MLD_PREHASH_SHA2_512_256; |
| 397 |
} |
| 398 |
if (strcmp(hashAlg, "SHA3-224") == 0) |
| 399 |
{ |
| 400 |
return MLD_PREHASH_SHA3_224; |
| 401 |
} |
| 402 |
if (strcmp(hashAlg, "SHA3-256") == 0) |
| 403 |
{ |
| 404 |
return MLD_PREHASH_SHA3_256; |
| 405 |
} |
| 406 |
if (strcmp(hashAlg, "SHA3-384") == 0) |
| 407 |
{ |
| 408 |
return MLD_PREHASH_SHA3_384; |
| 409 |
} |
| 410 |
if (strcmp(hashAlg, "SHA3-512") == 0) |
| 411 |
{ |
| 412 |
return MLD_PREHASH_SHA3_512; |
| 413 |
} |
| 414 |
if (strcmp(hashAlg, "SHAKE-128") == 0) |
| 415 |
{ |
| 416 |
return MLD_PREHASH_SHAKE_128; |
| 417 |
} |
| 418 |
if (strcmp(hashAlg, "SHAKE-256") == 0) |
| 419 |
{ |
| 420 |
return MLD_PREHASH_SHAKE_256; |
| 421 |
} |
| 422 |
|
| 423 |
fprintf(stderr, "Error: Unsupported hash algorithm: %s\n", hashAlg); |
| 424 |
exit(1); |
| 425 |
} |
| 426 |
|
| 427 |
static int acvp_mldsa_sigGenPreHash_AFT( |
| 428 |
const unsigned char *ph, size_t phlen, const unsigned char *context, |
| 429 |
size_t ctxlen, const unsigned char rng[MLDSA_RNDBYTES], |
| 430 |
const unsigned char sk[CRYPTO_SECRETKEYBYTES], const char *hashAlg) |
| 431 |
{ |
| 432 |
unsigned char signature[CRYPTO_BYTES]; |
| 433 |
size_t siglen; |
| 434 |
|
| 435 |
if (crypto_sign_signature_pre_hash_internal(signature, &siglen, ph, phlen, |
| 436 |
context, ctxlen, rng, sk, |
| 437 |
str_to_hash_alg(hashAlg)) != 0) |
| 438 |
{ |
| 439 |
return 1; |
| 440 |
} |
| 441 |
|
| 442 |
print_hex("signature", signature, siglen); |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
static int acvp_mldsa_sigVerPreHash_AFT( |
| 447 |
const unsigned char *ph, size_t phlen, const unsigned char *context, |
| 448 |
size_t ctxlen, const unsigned char signature[CRYPTO_BYTES], |
| 449 |
const unsigned char pk[CRYPTO_PUBLICKEYBYTES], const char *hashAlg) |
| 450 |
{ |
| 451 |
return crypto_sign_verify_pre_hash_internal(signature, CRYPTO_BYTES, ph, |
| 452 |
phlen, context, ctxlen, pk, |
| 453 |
str_to_hash_alg(hashAlg)); |
| 454 |
} |
| 455 |
|
| 456 |
static int acvp_mldsa_sigGenPreHashShake256_AFT( |
| 457 |
const unsigned char *message, size_t mlen, const unsigned char *context, |
| 458 |
size_t ctxlen, const unsigned char rnd[MLDSA_RNDBYTES], |
| 459 |
const unsigned char sk[CRYPTO_SECRETKEYBYTES]) |
| 460 |
{ |
| 461 |
unsigned char signature[CRYPTO_BYTES]; |
| 462 |
size_t siglen; |
| 463 |
|
| 464 |
if (crypto_sign_signature_pre_hash_shake256(signature, &siglen, message, mlen, |
| 465 |
context, ctxlen, rnd, sk) != 0) |
| 466 |
{ |
| 467 |
return 1; |
| 468 |
} |
| 469 |
|
| 470 |
print_hex("signature", signature, siglen); |
| 471 |
return 0; |
| 472 |
} |
| 473 |
|
| 474 |
static int acvp_mldsa_sigVerPreHashShake256_AFT( |
| 475 |
const unsigned char *message, size_t mlen, const unsigned char *context, |
| 476 |
size_t ctxlen, const unsigned char signature[CRYPTO_BYTES], |
| 477 |
const unsigned char pk[CRYPTO_PUBLICKEYBYTES]) |
| 478 |
{ |
| 479 |
return crypto_sign_verify_pre_hash_shake256(signature, CRYPTO_BYTES, message, |
| 480 |
mlen, context, ctxlen, pk); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
static int acvp_mldsa_sigGenPreHashDeterministic_AFT( |
| 485 |
const unsigned char *ph, size_t phlen, const unsigned char *context, |
| 486 |
size_t ctxlen, const unsigned char sk[CRYPTO_SECRETKEYBYTES], |
| 487 |
const char *hashAlg) |
| 488 |
{ |
| 489 |
unsigned char signature[CRYPTO_BYTES]; |
| 490 |
size_t siglen; |
| 491 |
unsigned char rnd[MLDSA_RNDBYTES] = {0}; |
| 492 |
|
| 493 |
if (crypto_sign_signature_pre_hash_internal(signature, &siglen, ph, phlen, |
| 494 |
context, ctxlen, rnd, sk, |
| 495 |
str_to_hash_alg(hashAlg)) != 0) |
| 496 |
{ |
| 497 |
return 1; |
| 498 |
} |
| 499 |
|
| 500 |
print_hex("signature", signature, siglen); |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
static int acvp_mldsa_sigGenPreHashShake256Deterministic_AFT( |
| 505 |
const unsigned char *message, size_t mlen, const unsigned char *context, |
| 506 |
size_t ctxlen, const unsigned char sk[CRYPTO_SECRETKEYBYTES]) |
| 507 |
{ |
| 508 |
unsigned char signature[CRYPTO_BYTES]; |
| 509 |
size_t siglen; |
| 510 |
unsigned char rnd[MLDSA_RNDBYTES] = {0}; |
| 511 |
|
| 512 |
if (crypto_sign_signature_pre_hash_shake256(signature, &siglen, message, mlen, |
| 513 |
context, ctxlen, rnd, sk) != 0) |
| 514 |
{ |
| 515 |
return 1; |
| 516 |
} |
| 517 |
|
| 518 |
print_hex("signature", signature, siglen); |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
int main(int argc, char *argv[]) |
| 524 |
{ |
| 525 |
acvp_mode mode; |
| 526 |
|
| 527 |
if (argc == 0) |
| 528 |
{ |
| 529 |
goto usage; |
| 530 |
} |
| 531 |
argc--, argv++; |
| 532 |
|
| 533 |
if (argc == 0) |
| 534 |
{ |
| 535 |
goto usage; |
| 536 |
} |
| 537 |
|
| 538 |
if (strcmp(*argv, "keyGen") == 0) |
| 539 |
{ |
| 540 |
mode = keyGen; |
| 541 |
} |
| 542 |
else if (strcmp(*argv, "sigGen") == 0) |
| 543 |
{ |
| 544 |
mode = sigGen; |
| 545 |
} |
| 546 |
else if (strcmp(*argv, "sigGenInternal") == 0) |
| 547 |
{ |
| 548 |
mode = sigGenInternal; |
| 549 |
} |
| 550 |
else if (strcmp(*argv, "sigGenDeterministic") == 0) |
| 551 |
{ |
| 552 |
mode = sigGenDeterministic; |
| 553 |
} |
| 554 |
else if (strcmp(*argv, "sigGenInternalDeterministic") == 0) |
| 555 |
{ |
| 556 |
mode = sigGenInternalDeterministic; |
| 557 |
} |
| 558 |
else if (strcmp(*argv, "sigGenPreHashDeterministic") == 0) |
| 559 |
{ |
| 560 |
mode = sigGenPreHashDeterministic; |
| 561 |
} |
| 562 |
else if (strcmp(*argv, "sigGenPreHashShake256Deterministic") == 0) |
| 563 |
{ |
| 564 |
mode = sigGenPreHashShake256Deterministic; |
| 565 |
} |
| 566 |
else if (strcmp(*argv, "sigVer") == 0) |
| 567 |
{ |
| 568 |
mode = sigVer; |
| 569 |
} |
| 570 |
else if (strcmp(*argv, "sigVerInternal") == 0) |
| 571 |
{ |
| 572 |
mode = sigVerInternal; |
| 573 |
} |
| 574 |
else if (strcmp(*argv, "sigGenPreHash") == 0) |
| 575 |
{ |
| 576 |
mode = sigGenPreHash; |
| 577 |
} |
| 578 |
else if (strcmp(*argv, "sigVerPreHash") == 0) |
| 579 |
{ |
| 580 |
mode = sigVerPreHash; |
| 581 |
} |
| 582 |
else if (strcmp(*argv, "sigGenPreHashShake256") == 0) |
| 583 |
{ |
| 584 |
mode = sigGenPreHashShake256; |
| 585 |
} |
| 586 |
else if (strcmp(*argv, "sigVerPreHashShake256") == 0) |
| 587 |
{ |
| 588 |
mode = sigVerPreHashShake256; |
| 589 |
} |
| 590 |
else |
| 591 |
{ |
| 592 |
goto usage; |
| 593 |
} |
| 594 |
argc--, argv++; |
| 595 |
|
| 596 |
switch (mode) |
| 597 |
{ |
| 598 |
case keyGen: |
| 599 |
{ |
| 600 |
unsigned char seed[MLDSA_SEEDBYTES]; |
| 601 |
|
| 602 |
if (argc == 0 || decode_hex("seed", seed, sizeof(seed), *argv) != 0) |
| 603 |
{ |
| 604 |
goto keygen_usage; |
| 605 |
} |
| 606 |
argc--, argv++; |
| 607 |
|
| 608 |
|
| 609 |
acvp_mldsa_keyGen_AFT(seed); |
| 610 |
break; |
| 611 |
} |
| 612 |
|
| 613 |
case sigGen: |
| 614 |
{ |
| 615 |
unsigned char message[MAX_MSG_LENGTH]; |
| 616 |
unsigned char rnd[MLDSA_RNDBYTES]; |
| 617 |
unsigned char context[MAX_CTX_LENGTH]; |
| 618 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 619 |
size_t mlen, ctxlen; |
| 620 |
|
| 621 |
|
| 622 |
if (argc == 0) |
| 623 |
{ |
| 624 |
goto siggen_usage; |
| 625 |
} |
| 626 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 627 |
if (mlen > MAX_MSG_LENGTH || |
| 628 |
decode_hex("message", message, mlen, *argv) != 0) |
| 629 |
{ |
| 630 |
goto siggen_usage; |
| 631 |
} |
| 632 |
argc--, argv++; |
| 633 |
|
| 634 |
|
| 635 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 636 |
{ |
| 637 |
goto siggen_usage; |
| 638 |
} |
| 639 |
argc--, argv++; |
| 640 |
|
| 641 |
|
| 642 |
if (argc == 0) |
| 643 |
{ |
| 644 |
goto siggen_usage; |
| 645 |
} |
| 646 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 647 |
if (mlen > MAX_MSG_LENGTH || |
| 648 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 649 |
{ |
| 650 |
goto siggen_usage; |
| 651 |
} |
| 652 |
argc--, argv++; |
| 653 |
|
| 654 |
|
| 655 |
if (argc == 0 || decode_hex("rnd", rnd, sizeof(rnd), *argv) != 0) |
| 656 |
{ |
| 657 |
goto siggen_usage; |
| 658 |
} |
| 659 |
argc--, argv++; |
| 660 |
|
| 661 |
|
| 662 |
acvp_mldsa_sigGen_AFT(message, mlen, rnd, sk, context, ctxlen); |
| 663 |
break; |
| 664 |
} |
| 665 |
case sigGenInternal: |
| 666 |
{ |
| 667 |
unsigned char message[MAX_MSG_LENGTH + MAX_CTX_LENGTH + 2]; |
| 668 |
unsigned char rnd[MLDSA_RNDBYTES]; |
| 669 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 670 |
int externalMu; |
| 671 |
size_t mlen; |
| 672 |
|
| 673 |
|
| 674 |
if (argc == 0) |
| 675 |
{ |
| 676 |
goto siggen_internal_usage; |
| 677 |
} |
| 678 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 679 |
if (mlen > sizeof(message) || |
| 680 |
decode_hex("message", message, mlen, *argv) != 0) |
| 681 |
{ |
| 682 |
goto siggen_internal_usage; |
| 683 |
} |
| 684 |
argc--, argv++; |
| 685 |
|
| 686 |
|
| 687 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 688 |
{ |
| 689 |
goto siggen_internal_usage; |
| 690 |
} |
| 691 |
argc--, argv++; |
| 692 |
|
| 693 |
|
| 694 |
if (argc == 0 || |
| 695 |
decode_keyed_int("externalMu", &externalMu, *argv) != 0 || |
| 696 |
externalMu > 1 || externalMu < 0) |
| 697 |
{ |
| 698 |
goto siggen_internal_usage; |
| 699 |
} |
| 700 |
argc--, argv++; |
| 701 |
|
| 702 |
|
| 703 |
if (argc == 0 || decode_hex("rnd", rnd, sizeof(rnd), *argv) != 0) |
| 704 |
{ |
| 705 |
goto siggen_internal_usage; |
| 706 |
} |
| 707 |
argc--, argv++; |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
acvp_mldsa_sigGenInternal_AFT(message, mlen, rnd, sk, externalMu); |
| 712 |
break; |
| 713 |
} |
| 714 |
|
| 715 |
case sigGenDeterministic: |
| 716 |
{ |
| 717 |
unsigned char message[MAX_MSG_LENGTH]; |
| 718 |
unsigned char context[MAX_CTX_LENGTH]; |
| 719 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 720 |
size_t mlen, ctxlen; |
| 721 |
|
| 722 |
|
| 723 |
if (argc == 0) |
| 724 |
{ |
| 725 |
goto siggen_deterministic_usage; |
| 726 |
} |
| 727 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 728 |
if (mlen > MAX_MSG_LENGTH || |
| 729 |
decode_hex("message", message, mlen, *argv) != 0) |
| 730 |
{ |
| 731 |
goto siggen_deterministic_usage; |
| 732 |
} |
| 733 |
argc--, argv++; |
| 734 |
|
| 735 |
|
| 736 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 737 |
{ |
| 738 |
goto siggen_deterministic_usage; |
| 739 |
} |
| 740 |
argc--, argv++; |
| 741 |
|
| 742 |
|
| 743 |
if (argc == 0) |
| 744 |
{ |
| 745 |
goto siggen_deterministic_usage; |
| 746 |
} |
| 747 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 748 |
if (mlen > MAX_MSG_LENGTH || |
| 749 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 750 |
{ |
| 751 |
goto siggen_deterministic_usage; |
| 752 |
} |
| 753 |
argc--, argv++; |
| 754 |
|
| 755 |
|
| 756 |
acvp_mldsa_sigGenDeterministic_AFT(message, mlen, sk, context, ctxlen); |
| 757 |
break; |
| 758 |
} |
| 759 |
|
| 760 |
case sigGenInternalDeterministic: |
| 761 |
{ |
| 762 |
unsigned char message[MAX_MSG_LENGTH + MAX_CTX_LENGTH + 2]; |
| 763 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 764 |
int externalMu; |
| 765 |
size_t mlen; |
| 766 |
|
| 767 |
|
| 768 |
if (argc == 0) |
| 769 |
{ |
| 770 |
goto siggen_internal_deterministic_usage; |
| 771 |
} |
| 772 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 773 |
if (mlen > sizeof(message) || |
| 774 |
decode_hex("message", message, mlen, *argv) != 0) |
| 775 |
{ |
| 776 |
goto siggen_internal_deterministic_usage; |
| 777 |
} |
| 778 |
argc--, argv++; |
| 779 |
|
| 780 |
|
| 781 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 782 |
{ |
| 783 |
goto siggen_internal_deterministic_usage; |
| 784 |
} |
| 785 |
argc--, argv++; |
| 786 |
|
| 787 |
|
| 788 |
if (argc == 0 || |
| 789 |
decode_keyed_int("externalMu", &externalMu, *argv) != 0 || |
| 790 |
externalMu > 1 || externalMu < 0) |
| 791 |
{ |
| 792 |
goto siggen_internal_deterministic_usage; |
| 793 |
} |
| 794 |
argc--, argv++; |
| 795 |
|
| 796 |
|
| 797 |
acvp_mldsa_sigGenInternalDeterministic_AFT(message, mlen, sk, externalMu); |
| 798 |
break; |
| 799 |
} |
| 800 |
|
| 801 |
case sigVer: |
| 802 |
{ |
| 803 |
unsigned char message[MAX_MSG_LENGTH]; |
| 804 |
unsigned char context[MAX_CTX_LENGTH]; |
| 805 |
unsigned char signature[CRYPTO_BYTES]; |
| 806 |
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; |
| 807 |
size_t mlen, ctxlen; |
| 808 |
|
| 809 |
|
| 810 |
if (argc == 0) |
| 811 |
{ |
| 812 |
goto sigver_usage; |
| 813 |
} |
| 814 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 815 |
if (mlen > MAX_MSG_LENGTH || |
| 816 |
decode_hex("message", message, mlen, *argv) != 0) |
| 817 |
{ |
| 818 |
goto sigver_usage; |
| 819 |
} |
| 820 |
argc--, argv++; |
| 821 |
|
| 822 |
|
| 823 |
if (argc == 0) |
| 824 |
{ |
| 825 |
goto sigver_usage; |
| 826 |
} |
| 827 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 828 |
if (mlen > MAX_MSG_LENGTH || |
| 829 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 830 |
{ |
| 831 |
goto sigver_usage; |
| 832 |
} |
| 833 |
argc--, argv++; |
| 834 |
|
| 835 |
|
| 836 |
if (argc == 0 || |
| 837 |
decode_hex("signature", signature, sizeof(signature), *argv) != 0) |
| 838 |
{ |
| 839 |
goto sigver_usage; |
| 840 |
} |
| 841 |
argc--, argv++; |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
if (argc == 0 || decode_hex("pk", pk, sizeof(pk), *argv) != 0) |
| 846 |
{ |
| 847 |
goto sigver_usage; |
| 848 |
} |
| 849 |
argc--, argv++; |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
return acvp_mldsa_sigVer_AFT(message, mlen, context, ctxlen, signature, |
| 854 |
pk); |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
case sigVerInternal: |
| 859 |
{ |
| 860 |
unsigned char message[MAX_MSG_LENGTH]; |
| 861 |
unsigned char signature[CRYPTO_BYTES]; |
| 862 |
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; |
| 863 |
size_t mlen; |
| 864 |
int externalMu; |
| 865 |
|
| 866 |
|
| 867 |
if (argc == 0) |
| 868 |
{ |
| 869 |
goto sigver_internal_usage; |
| 870 |
} |
| 871 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 872 |
if (mlen > MAX_MSG_LENGTH || |
| 873 |
decode_hex("message", message, mlen, *argv) != 0) |
| 874 |
{ |
| 875 |
goto sigver_internal_usage; |
| 876 |
} |
| 877 |
argc--, argv++; |
| 878 |
|
| 879 |
|
| 880 |
if (argc == 0 || |
| 881 |
decode_hex("signature", signature, sizeof(signature), *argv) != 0) |
| 882 |
{ |
| 883 |
goto sigver_internal_usage; |
| 884 |
} |
| 885 |
argc--, argv++; |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
if (argc == 0 || decode_hex("pk", pk, sizeof(pk), *argv) != 0) |
| 890 |
{ |
| 891 |
goto sigver_internal_usage; |
| 892 |
} |
| 893 |
argc--, argv++; |
| 894 |
|
| 895 |
|
| 896 |
if (argc == 0 || |
| 897 |
decode_keyed_int("externalMu", &externalMu, *argv) != 0 || |
| 898 |
externalMu > 1 || externalMu < 0) |
| 899 |
{ |
| 900 |
goto sigver_internal_usage; |
| 901 |
} |
| 902 |
argc--, argv++; |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
return acvp_mldsa_sigVerInternal_AFT(message, mlen, signature, pk, |
| 908 |
externalMu); |
| 909 |
} |
| 910 |
|
| 911 |
case sigGenPreHash: |
| 912 |
{ |
| 913 |
unsigned char ph[64]; |
| 914 |
unsigned char context[MAX_CTX_LENGTH]; |
| 915 |
unsigned char rnd[MLDSA_RNDBYTES]; |
| 916 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 917 |
char hashAlg[100]; |
| 918 |
size_t phlen; |
| 919 |
size_t ctxlen; |
| 920 |
|
| 921 |
|
| 922 |
if (argc == 0) |
| 923 |
{ |
| 924 |
goto siggen_prehash_usage; |
| 925 |
} |
| 926 |
phlen = (strlen(*argv) - strlen("ph=")) / 2; |
| 927 |
if (phlen > 64 || decode_hex("ph", ph, phlen, *argv) != 0) |
| 928 |
{ |
| 929 |
goto siggen_prehash_usage; |
| 930 |
} |
| 931 |
argc--, argv++; |
| 932 |
|
| 933 |
|
| 934 |
if (argc == 0) |
| 935 |
{ |
| 936 |
goto siggen_prehash_usage; |
| 937 |
} |
| 938 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 939 |
if (ctxlen > MAX_CTX_LENGTH || |
| 940 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 941 |
{ |
| 942 |
goto siggen_prehash_usage; |
| 943 |
} |
| 944 |
argc--, argv++; |
| 945 |
|
| 946 |
|
| 947 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 948 |
{ |
| 949 |
goto siggen_prehash_usage; |
| 950 |
} |
| 951 |
argc--, argv++; |
| 952 |
|
| 953 |
|
| 954 |
if (argc == 0 || |
| 955 |
parse_str("hashAlg", hashAlg, sizeof(hashAlg), *argv) != 0) |
| 956 |
{ |
| 957 |
goto siggen_prehash_usage; |
| 958 |
} |
| 959 |
argc--, argv++; |
| 960 |
|
| 961 |
|
| 962 |
if (argc == 0 || decode_hex("rnd", rnd, sizeof(rnd), *argv) != 0) |
| 963 |
{ |
| 964 |
goto siggen_prehash_usage; |
| 965 |
} |
| 966 |
argc--, argv++; |
| 967 |
|
| 968 |
|
| 969 |
return acvp_mldsa_sigGenPreHash_AFT(ph, phlen, context, ctxlen, rnd, sk, |
| 970 |
hashAlg); |
| 971 |
} |
| 972 |
|
| 973 |
case sigVerPreHash: |
| 974 |
{ |
| 975 |
unsigned char ph[64]; |
| 976 |
unsigned char context[MAX_CTX_LENGTH]; |
| 977 |
unsigned char signature[CRYPTO_BYTES]; |
| 978 |
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; |
| 979 |
char hashAlg[100]; |
| 980 |
size_t phlen; |
| 981 |
size_t ctxlen; |
| 982 |
|
| 983 |
|
| 984 |
if (argc == 0) |
| 985 |
{ |
| 986 |
goto sigver_prehash_usage; |
| 987 |
} |
| 988 |
phlen = (strlen(*argv) - strlen("ph=")) / 2; |
| 989 |
if (phlen > 64 || decode_hex("ph", ph, phlen, *argv) != 0) |
| 990 |
{ |
| 991 |
goto sigver_prehash_usage; |
| 992 |
} |
| 993 |
argc--, argv++; |
| 994 |
|
| 995 |
|
| 996 |
if (argc == 0) |
| 997 |
{ |
| 998 |
goto sigver_prehash_usage; |
| 999 |
} |
| 1000 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 1001 |
if (ctxlen > MAX_CTX_LENGTH || |
| 1002 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 1003 |
{ |
| 1004 |
goto sigver_prehash_usage; |
| 1005 |
} |
| 1006 |
argc--, argv++; |
| 1007 |
|
| 1008 |
|
| 1009 |
if (argc == 0 || |
| 1010 |
decode_hex("signature", signature, sizeof(signature), *argv) != 0) |
| 1011 |
{ |
| 1012 |
goto sigver_prehash_usage; |
| 1013 |
} |
| 1014 |
argc--, argv++; |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
if (argc == 0 || decode_hex("pk", pk, sizeof(pk), *argv) != 0) |
| 1019 |
{ |
| 1020 |
goto sigver_prehash_usage; |
| 1021 |
} |
| 1022 |
argc--, argv++; |
| 1023 |
|
| 1024 |
|
| 1025 |
if (argc == 0 || |
| 1026 |
parse_str("hashAlg", hashAlg, sizeof(hashAlg), *argv) != 0) |
| 1027 |
{ |
| 1028 |
goto sigver_prehash_usage; |
| 1029 |
} |
| 1030 |
argc--, argv++; |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
return acvp_mldsa_sigVerPreHash_AFT(ph, phlen, context, ctxlen, signature, |
| 1036 |
pk, hashAlg); |
| 1037 |
} |
| 1038 |
|
| 1039 |
case sigGenPreHashShake256: |
| 1040 |
{ |
| 1041 |
unsigned char message[MAX_MSG_LENGTH]; |
| 1042 |
unsigned char context[MAX_CTX_LENGTH]; |
| 1043 |
unsigned char rnd[MLDSA_RNDBYTES]; |
| 1044 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 1045 |
size_t mlen; |
| 1046 |
size_t ctxlen; |
| 1047 |
|
| 1048 |
|
| 1049 |
if (argc == 0) |
| 1050 |
{ |
| 1051 |
goto siggen_prehash_shake256_usage; |
| 1052 |
} |
| 1053 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 1054 |
if (mlen > MAX_MSG_LENGTH || |
| 1055 |
decode_hex("message", message, mlen, *argv) != 0) |
| 1056 |
{ |
| 1057 |
goto siggen_prehash_shake256_usage; |
| 1058 |
} |
| 1059 |
argc--, argv++; |
| 1060 |
|
| 1061 |
|
| 1062 |
if (argc == 0) |
| 1063 |
{ |
| 1064 |
goto siggen_prehash_shake256_usage; |
| 1065 |
} |
| 1066 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 1067 |
if (ctxlen > MAX_CTX_LENGTH || |
| 1068 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 1069 |
{ |
| 1070 |
goto siggen_prehash_shake256_usage; |
| 1071 |
} |
| 1072 |
argc--, argv++; |
| 1073 |
|
| 1074 |
|
| 1075 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 1076 |
{ |
| 1077 |
goto siggen_prehash_shake256_usage; |
| 1078 |
} |
| 1079 |
argc--, argv++; |
| 1080 |
|
| 1081 |
|
| 1082 |
if (argc == 0 || decode_hex("rnd", rnd, sizeof(rnd), *argv) != 0) |
| 1083 |
{ |
| 1084 |
goto siggen_prehash_shake256_usage; |
| 1085 |
} |
| 1086 |
argc--, argv++; |
| 1087 |
|
| 1088 |
|
| 1089 |
return acvp_mldsa_sigGenPreHashShake256_AFT(message, mlen, context, |
| 1090 |
ctxlen, rnd, sk); |
| 1091 |
} |
| 1092 |
|
| 1093 |
case sigGenPreHashDeterministic: |
| 1094 |
{ |
| 1095 |
unsigned char ph[64]; |
| 1096 |
unsigned char context[MAX_CTX_LENGTH]; |
| 1097 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 1098 |
char hashAlg[100]; |
| 1099 |
size_t phlen; |
| 1100 |
size_t ctxlen; |
| 1101 |
|
| 1102 |
|
| 1103 |
if (argc == 0) |
| 1104 |
{ |
| 1105 |
goto siggen_prehash_deterministic_usage; |
| 1106 |
} |
| 1107 |
phlen = (strlen(*argv) - strlen("ph=")) / 2; |
| 1108 |
if (phlen > 64 || decode_hex("ph", ph, phlen, *argv) != 0) |
| 1109 |
{ |
| 1110 |
goto siggen_prehash_deterministic_usage; |
| 1111 |
} |
| 1112 |
argc--, argv++; |
| 1113 |
|
| 1114 |
|
| 1115 |
if (argc == 0) |
| 1116 |
{ |
| 1117 |
goto siggen_prehash_deterministic_usage; |
| 1118 |
} |
| 1119 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 1120 |
if (ctxlen > MAX_CTX_LENGTH || |
| 1121 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 1122 |
{ |
| 1123 |
goto siggen_prehash_deterministic_usage; |
| 1124 |
} |
| 1125 |
argc--, argv++; |
| 1126 |
|
| 1127 |
|
| 1128 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 1129 |
{ |
| 1130 |
goto siggen_prehash_deterministic_usage; |
| 1131 |
} |
| 1132 |
argc--, argv++; |
| 1133 |
|
| 1134 |
|
| 1135 |
if (argc == 0 || |
| 1136 |
parse_str("hashAlg", hashAlg, sizeof(hashAlg), *argv) != 0) |
| 1137 |
{ |
| 1138 |
goto siggen_prehash_deterministic_usage; |
| 1139 |
} |
| 1140 |
argc--, argv++; |
| 1141 |
|
| 1142 |
|
| 1143 |
return acvp_mldsa_sigGenPreHashDeterministic_AFT(ph, phlen, context, |
| 1144 |
ctxlen, sk, hashAlg); |
| 1145 |
} |
| 1146 |
|
| 1147 |
case sigGenPreHashShake256Deterministic: |
| 1148 |
{ |
| 1149 |
unsigned char message[MAX_MSG_LENGTH]; |
| 1150 |
unsigned char context[MAX_CTX_LENGTH]; |
| 1151 |
unsigned char sk[CRYPTO_SECRETKEYBYTES]; |
| 1152 |
size_t mlen; |
| 1153 |
size_t ctxlen; |
| 1154 |
|
| 1155 |
|
| 1156 |
if (argc == 0) |
| 1157 |
{ |
| 1158 |
goto siggen_prehash_shake256_deterministic_usage; |
| 1159 |
} |
| 1160 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 1161 |
if (mlen > MAX_MSG_LENGTH || |
| 1162 |
decode_hex("message", message, mlen, *argv) != 0) |
| 1163 |
{ |
| 1164 |
goto siggen_prehash_shake256_deterministic_usage; |
| 1165 |
} |
| 1166 |
argc--, argv++; |
| 1167 |
|
| 1168 |
|
| 1169 |
if (argc == 0) |
| 1170 |
{ |
| 1171 |
goto siggen_prehash_shake256_deterministic_usage; |
| 1172 |
} |
| 1173 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 1174 |
if (ctxlen > MAX_CTX_LENGTH || |
| 1175 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 1176 |
{ |
| 1177 |
goto siggen_prehash_shake256_deterministic_usage; |
| 1178 |
} |
| 1179 |
argc--, argv++; |
| 1180 |
|
| 1181 |
|
| 1182 |
if (argc == 0 || decode_hex("sk", sk, sizeof(sk), *argv) != 0) |
| 1183 |
{ |
| 1184 |
goto siggen_prehash_shake256_deterministic_usage; |
| 1185 |
} |
| 1186 |
argc--, argv++; |
| 1187 |
|
| 1188 |
|
| 1189 |
return acvp_mldsa_sigGenPreHashShake256Deterministic_AFT( |
| 1190 |
message, mlen, context, ctxlen, sk); |
| 1191 |
} |
| 1192 |
|
| 1193 |
case sigVerPreHashShake256: |
| 1194 |
{ |
| 1195 |
unsigned char message[MAX_MSG_LENGTH]; |
| 1196 |
unsigned char context[MAX_CTX_LENGTH]; |
| 1197 |
unsigned char signature[CRYPTO_BYTES]; |
| 1198 |
unsigned char pk[CRYPTO_PUBLICKEYBYTES]; |
| 1199 |
size_t mlen; |
| 1200 |
size_t ctxlen; |
| 1201 |
|
| 1202 |
|
| 1203 |
if (argc == 0) |
| 1204 |
{ |
| 1205 |
goto sigver_prehash_shake256_usage; |
| 1206 |
} |
| 1207 |
mlen = (strlen(*argv) - strlen("message=")) / 2; |
| 1208 |
if (mlen > MAX_MSG_LENGTH || |
| 1209 |
decode_hex("message", message, mlen, *argv) != 0) |
| 1210 |
{ |
| 1211 |
goto sigver_prehash_shake256_usage; |
| 1212 |
} |
| 1213 |
argc--, argv++; |
| 1214 |
|
| 1215 |
|
| 1216 |
if (argc == 0) |
| 1217 |
{ |
| 1218 |
goto sigver_prehash_shake256_usage; |
| 1219 |
} |
| 1220 |
ctxlen = (strlen(*argv) - strlen("context=")) / 2; |
| 1221 |
if (ctxlen > MAX_CTX_LENGTH || |
| 1222 |
decode_hex("context", context, ctxlen, *argv) != 0) |
| 1223 |
{ |
| 1224 |
goto sigver_prehash_shake256_usage; |
| 1225 |
} |
| 1226 |
argc--, argv++; |
| 1227 |
|
| 1228 |
|
| 1229 |
if (argc == 0 || |
| 1230 |
decode_hex("signature", signature, sizeof(signature), *argv) != 0) |
| 1231 |
{ |
| 1232 |
goto sigver_prehash_shake256_usage; |
| 1233 |
} |
| 1234 |
argc--, argv++; |
| 1235 |
|
| 1236 |
|
| 1237 |
if (argc == 0 || decode_hex("pk", pk, sizeof(pk), *argv) != 0) |
| 1238 |
{ |
| 1239 |
goto sigver_prehash_shake256_usage; |
| 1240 |
} |
| 1241 |
argc--, argv++; |
| 1242 |
|
| 1243 |
|
| 1244 |
return acvp_mldsa_sigVerPreHashShake256_AFT(message, mlen, context, |
| 1245 |
ctxlen, signature, pk); |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
return (0); |
| 1250 |
|
| 1251 |
usage: |
| 1252 |
fprintf(stderr, USAGE "\n"); |
| 1253 |
return (1); |
| 1254 |
|
| 1255 |
keygen_usage: |
| 1256 |
fprintf(stderr, KEYGEN_USAGE "\n"); |
| 1257 |
return (1); |
| 1258 |
|
| 1259 |
siggen_usage: |
| 1260 |
fprintf(stderr, SIGGEN_USAGE "\n"); |
| 1261 |
return (1); |
| 1262 |
|
| 1263 |
siggen_internal_usage: |
| 1264 |
fprintf(stderr, SIGGEN_INTERNAL_USAGE "\n"); |
| 1265 |
return (1); |
| 1266 |
|
| 1267 |
siggen_deterministic_usage: |
| 1268 |
fprintf(stderr, SIGGEN_DETERMINISTIC_USAGE "\n"); |
| 1269 |
return (1); |
| 1270 |
|
| 1271 |
siggen_internal_deterministic_usage: |
| 1272 |
fprintf(stderr, SIGGEN_INTERNAL_DETERMINISTIC_USAGE "\n"); |
| 1273 |
return (1); |
| 1274 |
|
| 1275 |
siggen_prehash_deterministic_usage: |
| 1276 |
fprintf(stderr, SIGGEN_PREHASH_DETERMINISTIC_USAGE "\n"); |
| 1277 |
return (1); |
| 1278 |
|
| 1279 |
siggen_prehash_shake256_deterministic_usage: |
| 1280 |
fprintf(stderr, SIGGEN_PREHASH_SHAKE256_DETERMINISTIC_USAGE "\n"); |
| 1281 |
return (1); |
| 1282 |
|
| 1283 |
sigver_usage: |
| 1284 |
fprintf(stderr, SIGVER_USAGE "\n"); |
| 1285 |
return (1); |
| 1286 |
|
| 1287 |
sigver_internal_usage: |
| 1288 |
fprintf(stderr, SIGVER_INTERNAL_USAGE "\n"); |
| 1289 |
return (1); |
| 1290 |
|
| 1291 |
siggen_prehash_usage: |
| 1292 |
fprintf(stderr, SIGGEN_PREHASH_USAGE "\n"); |
| 1293 |
return (1); |
| 1294 |
|
| 1295 |
sigver_prehash_usage: |
| 1296 |
fprintf(stderr, SIGVER_PREHASH_USAGE "\n"); |
| 1297 |
return (1); |
| 1298 |
|
| 1299 |
siggen_prehash_shake256_usage: |
| 1300 |
fprintf(stderr, SIGGEN_PREHASH_SHAKE256_USAGE "\n"); |
| 1301 |
return (1); |
| 1302 |
|
| 1303 |
sigver_prehash_shake256_usage: |
| 1304 |
fprintf(stderr, SIGVER_PREHASH_SHAKE256_USAGE "\n"); |
| 1305 |
return (1); |
| 1306 |
} |