Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4 */
5
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 /* Import public mldsa-native API
12 *
13 * This requires specifying the parameter set and namespace prefix
14 * used for the build.
15 */
16 #include <mldsa_native.h>
17 #include "expected_signatures.h"
18 #include "test_only_rng/notrandombytes.h"
19
20 #define CHECK(x) \
21 do \
22 { \
23 int rc; \
24 rc = (x); \
25 if (!rc) \
26 { \
27 fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \
28 return 1; \
29 } \
30 } while (0)
31
32 #define TEST_MSG \
33 "This is a test message for ML-DSA digital signature algorithm!"
34 #define TEST_MSG_LEN (sizeof(TEST_MSG) - 1)
35
36 #define TEST_CTX "test_context_123"
37 #define TEST_CTX_LEN (sizeof(TEST_CTX) - 1)
38
39 int main(void)
40 {
41 const char test_msg[] = TEST_MSG;
42 const char test_ctx[] = TEST_CTX;
43
44 uint8_t pk[CRYPTO_PUBLICKEYBYTES];
45 uint8_t sk[CRYPTO_SECRETKEYBYTES];
46 uint8_t sig[CRYPTO_BYTES];
47 uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
48 uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
49 size_t siglen;
50 size_t smlen;
51 size_t mlen;
52
53 /* WARNING: Test-only
54 * Normally, you would want to seed a PRNG with trustworthy entropy here. */
55 randombytes_reset();
56
57 printf("ML-DSA-%d Bring Your Own FIPS-202 Example\n",
58 MLD_CONFIG_PARAMETER_SET);
59 printf("======================\n\n");
60
61 printf("Message: %s\n", test_msg);
62 printf("Context: %s\n\n", test_ctx);
63
64 printf("Generating keypair ... ");
65
66 /* Alice generates a public/private key pair */
67 CHECK(crypto_sign_keypair(pk, sk) == 0);
68
69 printf("DONE\n");
70 printf("Signing message... ");
71
72 /* Alice signs the message */
73 CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg,
74 TEST_MSG_LEN, (const uint8_t *)test_ctx,
75 TEST_CTX_LEN, sk) == 0);
76
77 printf("DONE\n");
78 printf("Verifying signature... ");
79
80 /* Bob verifies Alice's signature */
81 CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
82 (const uint8_t *)test_ctx, TEST_CTX_LEN, pk) == 0);
83
84 printf("DONE\n");
85 printf("Creating signed message... ");
86
87 /* Alternative API: Create a signed message (signature + message combined) */
88 CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN,
89 (const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0);
90
91 printf("DONE\n");
92 printf("Opening signed message... ");
93
94 /* Bob opens the signed message to recover the original message */
95 CHECK(crypto_sign_open(m2, &mlen, sm, smlen, (const uint8_t *)test_ctx,
96 TEST_CTX_LEN, pk) == 0);
97
98 printf("DONE\n");
99 printf("Compare messages... ");
100
101 /* Verify the recovered message matches the original */
102 CHECK(mlen == TEST_MSG_LEN);
103 CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0);
104
105 printf("DONE\n\n");
106
107 printf("Results:\n");
108 printf("--------\n");
109 printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
110 printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
111 printf("Signature size: %d bytes\n", CRYPTO_BYTES);
112 printf("Message length: %lu bytes\n", (unsigned long)TEST_MSG_LEN);
113 printf("Signature length: %lu bytes\n", (unsigned long)siglen);
114 printf("Signed msg length: %lu bytes\n", (unsigned long)smlen);
115
116 #if !defined(MLD_CONFIG_KEYGEN_PCT)
117 /* Check against expected signature to make sure that
118 * we integrated the library correctly */
119 printf("Checking deterministic signature... ");
120 {
121 /* Compare the generated signature directly against the expected signature
122 */
123 CHECK(siglen == sizeof(expected_signature));
124 CHECK(memcmp(sig, expected_signature, siglen) == 0);
125 }
126 printf("DONE\n");
127 #else /* !MLD_CONFIG_KEYGEN_PCT */
128 printf(
129 "[WARNING] Skipping KAT test since PCT is enabled and modifies PRNG\n");
130 #endif /* MLD_CONFIG_KEYGEN_PCT */
131
132 printf("Signature verification completed successfully!\n");
133 printf("Custom FIPS-202 implementation is working correctly.\n");
134
135 printf("\nAll tests passed! ML-DSA signature verification successful.\n");
136 return 0;
137 }