Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/monolithic_build_multilevel_native/main.c

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