Debian Codegraph @

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