Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/monolithic_build/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 Example\n", MLD_CONFIG_PARAMETER_SET);
61 printf("======================\n\n");
62
63 printf("Message: %s\n", test_msg);
64 printf("Context: %s\n\n", test_ctx);
65
66 printf("Generating keypair ... ");
67
68 /* Alice generates a public/private key pair */
69 CHECK(crypto_sign_keypair(pk, sk) == 0);
70
71 printf("DONE\n");
72 printf("Signing message... ");
73
74 /* Alice signs the message */
75 CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg,
76 TEST_MSG_LEN, (const uint8_t *)test_ctx,
77 TEST_CTX_LEN, sk) == 0);
78
79 printf("DONE\n");
80 printf("Verifying signature... ");
81
82 /* Bob verifies Alice's signature */
83 CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
84 (const uint8_t *)test_ctx, TEST_CTX_LEN, pk) == 0);
85
86 printf("DONE\n");
87 printf("Creating signed message... ");
88
89 /* Alternative API: Create a signed message (signature + message combined) */
90 CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN,
91 (const uint8_t *)test_ctx, TEST_CTX_LEN, sk) == 0);
92
93 printf("DONE\n");
94 printf("Opening signed message... ");
95
96 /* Bob opens the signed message to recover the original message */
97 CHECK(crypto_sign_open(m2, &mlen, sm, smlen, (const uint8_t *)test_ctx,
98 TEST_CTX_LEN, pk) == 0);
99
100 printf("DONE\n");
101 printf("Compare messages... ");
102
103 /* Verify the recovered message matches the original */
104 CHECK(mlen == TEST_MSG_LEN);
105 CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0);
106
107 printf("DONE\n\n");
108
109 printf("Results:\n");
110 printf("--------\n");
111 printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
112 printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
113 printf("Signature size: %d bytes\n", CRYPTO_BYTES);
114 printf("Message length: %lu bytes\n", (unsigned long)TEST_MSG_LEN);
115 printf("Signature length: %lu bytes\n", (unsigned long)siglen);
116 printf("Signed msg length: %lu bytes\n", (unsigned long)smlen);
117
118 #if !defined(MLD_CONFIG_KEYGEN_PCT)
119 /* Check against expected signature to make sure that
120 * we integrated the library correctly */
121 printf("Checking deterministic signature... ");
122 {
123 /* Compare the generated signature directly against the expected signature
124 */
125 CHECK(siglen == sizeof(expected_signature));
126 CHECK(memcmp(sig, expected_signature, siglen) == 0);
127 }
128 printf("DONE\n");
129 #else /* !MLD_CONFIG_KEYGEN_PCT */
130 printf(
131 "[WARNING] Skipping KAT test since PCT is enabled and modifies PRNG\n");
132 #endif /* MLD_CONFIG_KEYGEN_PCT */
133
134 printf("Signature verification completed successfully!\n");
135
136 printf("\nAll tests passed! ML-DSA signature verification successful.\n");
137 return 0;
138 }