Debian Codegraph @

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