| 1 |
/* |
| 2 |
* Copyright (c) The mlkem-native project authors |
| 3 |
* Copyright (c) The mldsa-native project authors |
| 4 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "expected_signatures.h" |
| 12 |
#include "mldsa_native_all.h" |
| 13 |
#include "test_only_rng/notrandombytes.h" |
| 14 |
|
| 15 |
#define CHECK(x) \ |
| 16 |
do \ |
| 17 |
{ \ |
| 18 |
int rc; \ |
| 19 |
rc = (x); \ |
| 20 |
if (!rc) \ |
| 21 |
{ \ |
| 22 |
fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \ |
| 23 |
return 1; \ |
| 24 |
} \ |
| 25 |
} while (0) |
| 26 |
|
| 27 |
#define TEST_MSG \ |
| 28 |
"This is a test message for ML-DSA digital signature algorithm!" |
| 29 |
#define TEST_MSG_LEN (sizeof(TEST_MSG) - 1) |
| 30 |
|
| 31 |
#define TEST_CTX "test_context_123" |
| 32 |
#define TEST_CTX_LEN (sizeof(TEST_CTX) - 1) |
| 33 |
|
| 34 |
static int test_mldsa44(void) |
| 35 |
{ |
| 36 |
const char test_msg[] = TEST_MSG; |
| 37 |
const char test_ctx[] = TEST_CTX; |
| 38 |
|
| 39 |
uint8_t pk[MLDSA44_PUBLICKEYBYTES]; |
| 40 |
uint8_t sk[MLDSA44_SECRETKEYBYTES]; |
| 41 |
uint8_t sig[MLDSA44_BYTES]; |
| 42 |
uint8_t sm[TEST_MSG_LEN + MLDSA44_BYTES]; /* signed message buffer */ |
| 43 |
uint8_t m2[TEST_MSG_LEN + MLDSA44_BYTES]; /* recovered message buffer */ |
| 44 |
size_t siglen; |
| 45 |
size_t smlen; |
| 46 |
size_t mlen; |
| 47 |
|
| 48 |
/* WARNING: Test-only |
| 49 |
* Normally, you would want to seed a PRNG with trustworthy entropy here. */ |
| 50 |
randombytes_reset(); |
| 51 |
|
| 52 |
printf("ML-DSA-44\n"); |
| 53 |
printf("======================\n\n"); |
| 54 |
|
| 55 |
printf("Message: %s\n", test_msg); |
| 56 |
printf("Context: %s\n\n", test_ctx); |
| 57 |
|
| 58 |
printf("Generating keypair ... "); |
| 59 |
|
| 60 |
/* Alice generates a public/private key pair */ |
| 61 |
CHECK(mldsa44_keypair(pk, sk) == 0); |
| 62 |
|
| 63 |
printf("DONE\n"); |
| 64 |
printf("Signing message... "); |
| 65 |
|
| 66 |
/* Alice signs the message */ |
| 67 |
CHECK(mldsa44_signature(sig, &siglen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 68 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0); |
| 69 |
|
| 70 |
printf("DONE\n"); |
| 71 |
printf("Verifying signature... "); |
| 72 |
|
| 73 |
/* Bob verifies Alice's signature */ |
| 74 |
CHECK(mldsa44_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 75 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, pk) == 0); |
| 76 |
|
| 77 |
printf("DONE\n"); |
| 78 |
printf("Creating signed message... "); |
| 79 |
|
| 80 |
/* Alternative API: Create a signed message (signature + message combined) */ |
| 81 |
CHECK(mldsa44_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 82 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0); |
| 83 |
|
| 84 |
printf("DONE\n"); |
| 85 |
printf("Opening signed message... "); |
| 86 |
|
| 87 |
/* Bob opens the signed message to recover the original message */ |
| 88 |
CHECK(mldsa44_open(m2, &mlen, sm, smlen, (const uint8_t *)test_ctx, |
| 89 |
TEST_CTX_LEN, pk) == 0); |
| 90 |
|
| 91 |
printf("DONE\n"); |
| 92 |
printf("Compare messages... "); |
| 93 |
|
| 94 |
/* Verify the recovered message matches the original */ |
| 95 |
CHECK(mlen == TEST_MSG_LEN); |
| 96 |
CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0); |
| 97 |
|
| 98 |
printf("DONE\n\n"); |
| 99 |
|
| 100 |
printf("Results:\n"); |
| 101 |
printf("--------\n"); |
| 102 |
printf("Public key size: %d bytes\n", MLDSA44_PUBLICKEYBYTES); |
| 103 |
printf("Secret key size: %d bytes\n", MLDSA44_SECRETKEYBYTES); |
| 104 |
printf("Signature size: %d bytes\n", MLDSA44_BYTES); |
| 105 |
printf("Message length: %lu bytes\n", (unsigned long)TEST_MSG_LEN); |
| 106 |
printf("Signature length: %lu bytes\n", (unsigned long)siglen); |
| 107 |
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen); |
| 108 |
|
| 109 |
#if !defined(MLD_CONFIG_KEYGEN_PCT) |
| 110 |
/* Check against expected signature to make sure that |
| 111 |
* we integrated the library correctly */ |
| 112 |
printf("Checking deterministic signature... "); |
| 113 |
{ |
| 114 |
/* Compare the generated signature directly against the expected signature |
| 115 |
*/ |
| 116 |
CHECK(siglen == sizeof(expected_signature_44)); |
| 117 |
CHECK(memcmp(sig, expected_signature_44, siglen) == 0); |
| 118 |
} |
| 119 |
printf("DONE\n"); |
| 120 |
#else /* !MLD_CONFIG_KEYGEN_PCT */ |
| 121 |
printf( |
| 122 |
"[WARNING] Skipping KAT test since PCT is enabled and modifies PRNG\n"); |
| 123 |
#endif /* MLD_CONFIG_KEYGEN_PCT */ |
| 124 |
|
| 125 |
printf("Signature verification completed successfully!\n"); |
| 126 |
|
| 127 |
printf("\nAll tests passed! ML-DSA signature verification successful.\n"); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static int test_mldsa65(void) |
| 132 |
{ |
| 133 |
const char test_msg[] = TEST_MSG; |
| 134 |
const char test_ctx[] = TEST_CTX; |
| 135 |
|
| 136 |
uint8_t pk[MLDSA65_PUBLICKEYBYTES]; |
| 137 |
uint8_t sk[MLDSA65_SECRETKEYBYTES]; |
| 138 |
uint8_t sig[MLDSA65_BYTES]; |
| 139 |
uint8_t sm[TEST_MSG_LEN + MLDSA65_BYTES]; /* signed message buffer */ |
| 140 |
uint8_t m2[TEST_MSG_LEN + MLDSA65_BYTES]; /* recovered message buffer */ |
| 141 |
size_t siglen; |
| 142 |
size_t smlen; |
| 143 |
size_t mlen; |
| 144 |
|
| 145 |
/* WARNING: Test-only |
| 146 |
* Normally, you would want to seed a PRNG with trustworthy entropy here. */ |
| 147 |
randombytes_reset(); |
| 148 |
|
| 149 |
printf("ML-DSA-65\n"); |
| 150 |
printf("======================\n\n"); |
| 151 |
|
| 152 |
printf("Message: %s\n", test_msg); |
| 153 |
printf("Context: %s\n\n", test_ctx); |
| 154 |
|
| 155 |
printf("Generating keypair ... "); |
| 156 |
|
| 157 |
/* Alice generates a public/private key pair */ |
| 158 |
CHECK(mldsa65_keypair(pk, sk) == 0); |
| 159 |
|
| 160 |
printf("DONE\n"); |
| 161 |
printf("Signing message... "); |
| 162 |
|
| 163 |
/* Alice signs the message */ |
| 164 |
CHECK(mldsa65_signature(sig, &siglen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 165 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0); |
| 166 |
|
| 167 |
printf("DONE\n"); |
| 168 |
printf("Verifying signature... "); |
| 169 |
|
| 170 |
/* Bob verifies Alice's signature */ |
| 171 |
CHECK(mldsa65_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 172 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, pk) == 0); |
| 173 |
|
| 174 |
printf("DONE\n"); |
| 175 |
printf("Creating signed message... "); |
| 176 |
|
| 177 |
/* Alternative API: Create a signed message (signature + message combined) */ |
| 178 |
CHECK(mldsa65_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 179 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0); |
| 180 |
|
| 181 |
printf("DONE\n"); |
| 182 |
printf("Opening signed message... "); |
| 183 |
|
| 184 |
/* Bob opens the signed message to recover the original message */ |
| 185 |
CHECK(mldsa65_open(m2, &mlen, sm, smlen, (const uint8_t *)test_ctx, |
| 186 |
TEST_CTX_LEN, pk) == 0); |
| 187 |
|
| 188 |
printf("DONE\n"); |
| 189 |
printf("Compare messages... "); |
| 190 |
|
| 191 |
/* Verify the recovered message matches the original */ |
| 192 |
CHECK(mlen == TEST_MSG_LEN); |
| 193 |
CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0); |
| 194 |
|
| 195 |
printf("DONE\n\n"); |
| 196 |
|
| 197 |
printf("Results:\n"); |
| 198 |
printf("--------\n"); |
| 199 |
printf("Public key size: %d bytes\n", MLDSA65_PUBLICKEYBYTES); |
| 200 |
printf("Secret key size: %d bytes\n", MLDSA65_SECRETKEYBYTES); |
| 201 |
printf("Signature size: %d bytes\n", MLDSA65_BYTES); |
| 202 |
printf("Message length: %lu bytes\n", (unsigned long)TEST_MSG_LEN); |
| 203 |
printf("Signature length: %lu bytes\n", (unsigned long)siglen); |
| 204 |
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen); |
| 205 |
|
| 206 |
#if !defined(MLD_CONFIG_KEYGEN_PCT) |
| 207 |
/* Check against expected signature to make sure that |
| 208 |
* we integrated the library correctly */ |
| 209 |
printf("Checking deterministic signature... "); |
| 210 |
{ |
| 211 |
/* Compare the generated signature directly against the expected signature |
| 212 |
*/ |
| 213 |
CHECK(siglen == sizeof(expected_signature_65)); |
| 214 |
CHECK(memcmp(sig, expected_signature_65, siglen) == 0); |
| 215 |
} |
| 216 |
printf("DONE\n"); |
| 217 |
#else /* !MLD_CONFIG_KEYGEN_PCT */ |
| 218 |
printf( |
| 219 |
"[WARNING] Skipping KAT test since PCT is enabled and modifies PRNG\n"); |
| 220 |
#endif /* MLD_CONFIG_KEYGEN_PCT */ |
| 221 |
|
| 222 |
printf("Signature verification completed successfully!\n"); |
| 223 |
|
| 224 |
printf("\nAll tests passed! ML-DSA signature verification successful.\n"); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
static int test_mldsa87(void) |
| 230 |
{ |
| 231 |
const char test_msg[] = TEST_MSG; |
| 232 |
const char test_ctx[] = TEST_CTX; |
| 233 |
|
| 234 |
uint8_t pk[MLDSA87_PUBLICKEYBYTES]; |
| 235 |
uint8_t sk[MLDSA87_SECRETKEYBYTES]; |
| 236 |
uint8_t sig[MLDSA87_BYTES]; |
| 237 |
uint8_t sm[TEST_MSG_LEN + MLDSA87_BYTES]; /* signed message buffer */ |
| 238 |
uint8_t m2[TEST_MSG_LEN + MLDSA87_BYTES]; /* recovered message buffer */ |
| 239 |
size_t siglen; |
| 240 |
size_t smlen; |
| 241 |
size_t mlen; |
| 242 |
|
| 243 |
/* WARNING: Test-only |
| 244 |
* Normally, you would want to seed a PRNG with trustworthy entropy here. */ |
| 245 |
randombytes_reset(); |
| 246 |
|
| 247 |
printf("ML-DSA-87\n"); |
| 248 |
printf("======================\n\n"); |
| 249 |
|
| 250 |
printf("Message: %s\n", test_msg); |
| 251 |
printf("Context: %s\n\n", test_ctx); |
| 252 |
|
| 253 |
printf("Generating keypair ... "); |
| 254 |
|
| 255 |
/* Alice generates a public/private key pair */ |
| 256 |
CHECK(mldsa87_keypair(pk, sk) == 0); |
| 257 |
|
| 258 |
printf("DONE\n"); |
| 259 |
printf("Signing message... "); |
| 260 |
|
| 261 |
/* Alice signs the message */ |
| 262 |
CHECK(mldsa87_signature(sig, &siglen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 263 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0); |
| 264 |
|
| 265 |
printf("DONE\n"); |
| 266 |
printf("Verifying signature... "); |
| 267 |
|
| 268 |
/* Bob verifies Alice's signature */ |
| 269 |
CHECK(mldsa87_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 270 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, pk) == 0); |
| 271 |
|
| 272 |
printf("DONE\n"); |
| 273 |
printf("Creating signed message... "); |
| 274 |
|
| 275 |
/* Alternative API: Create a signed message (signature + message combined) */ |
| 276 |
CHECK(mldsa87_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN, |
| 277 |
(const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0); |
| 278 |
|
| 279 |
printf("DONE\n"); |
| 280 |
printf("Opening signed message... "); |
| 281 |
|
| 282 |
/* Bob opens the signed message to recover the original message */ |
| 283 |
CHECK(mldsa87_open(m2, &mlen, sm, smlen, (const uint8_t *)test_ctx, |
| 284 |
TEST_CTX_LEN, pk) == 0); |
| 285 |
|
| 286 |
printf("DONE\n"); |
| 287 |
printf("Compare messages... "); |
| 288 |
|
| 289 |
/* Verify the recovered message matches the original */ |
| 290 |
CHECK(mlen == TEST_MSG_LEN); |
| 291 |
CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0); |
| 292 |
|
| 293 |
printf("DONE\n\n"); |
| 294 |
|
| 295 |
printf("Results:\n"); |
| 296 |
printf("--------\n"); |
| 297 |
printf("Public key size: %d bytes\n", MLDSA87_PUBLICKEYBYTES); |
| 298 |
printf("Secret key size: %d bytes\n", MLDSA87_SECRETKEYBYTES); |
| 299 |
printf("Signature size: %d bytes\n", MLDSA87_BYTES); |
| 300 |
printf("Message length: %lu bytes\n", (unsigned long)TEST_MSG_LEN); |
| 301 |
printf("Signature length: %lu bytes\n", (unsigned long)siglen); |
| 302 |
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen); |
| 303 |
|
| 304 |
#if !defined(MLD_CONFIG_KEYGEN_PCT) |
| 305 |
/* Check against expected signature to make sure that |
| 306 |
* we integrated the library correctly */ |
| 307 |
printf("Checking deterministic signature... "); |
| 308 |
{ |
| 309 |
/* Compare the generated signature directly against the expected signature |
| 310 |
*/ |
| 311 |
CHECK(siglen == sizeof(expected_signature_87)); |
| 312 |
CHECK(memcmp(sig, expected_signature_87, siglen) == 0); |
| 313 |
} |
| 314 |
printf("DONE\n"); |
| 315 |
#else /* !MLD_CONFIG_KEYGEN_PCT */ |
| 316 |
printf( |
| 317 |
"[WARNING] Skipping KAT test since PCT is enabled and modifies PRNG\n"); |
| 318 |
#endif /* MLD_CONFIG_KEYGEN_PCT */ |
| 319 |
|
| 320 |
printf("Signature verification completed successfully!\n"); |
| 321 |
|
| 322 |
printf("\nAll tests passed! ML-DSA signature verification successful.\n"); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
int main(void) |
| 327 |
{ |
| 328 |
printf("ML-DSA multilevel_build Example\n"); |
| 329 |
|
| 330 |
if (test_mldsa44() != 0) |
| 331 |
{ |
| 332 |
return 1; |
| 333 |
} |
| 334 |
|
| 335 |
if (test_mldsa65() != 0) |
| 336 |
{ |
| 337 |
return 1; |
| 338 |
} |
| 339 |
|
| 340 |
if (test_mldsa87() != 0) |
| 341 |
{ |
| 342 |
return 1; |
| 343 |
} |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |