Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/basic_deterministic/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
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 int main(void)
39 {
40 const char test_msg[] = TEST_MSG;
41 const char test_ctx[] = TEST_CTX;
42
43 uint8_t pk[CRYPTO_PUBLICKEYBYTES];
44 uint8_t sk[CRYPTO_SECRETKEYBYTES];
45 uint8_t sig[CRYPTO_BYTES];
46 /* For testing, we use the same randomness that would be generated by our
47 * deterministic test rng such that we can produce the same keys and
48 * signatures as in the basic example.
49 */
50 /* # !!! WARNING !!!
51 * This is for TESTING ONLY.
52 * For key generation, you MUST USE cryptographic randomness outside of
53 * testing.
54 * For signing, you SHOULD provide cryptographic randomness. If you require
55 * deterministic signing, set sign_rnd to all zeros.
56 */
57
58 uint8_t keypair_rnd[MLDSA_SEEDBYTES] = {
59 0x93, 0x4d, 0x60, 0xb3, 0x56, 0x24, 0xd7, 0x40, 0xb3, 0x0a, 0x7f,
60 0x22, 0x7a, 0xf2, 0xae, 0x7c, 0x67, 0x8e, 0x4e, 0x04, 0xe1, 0x3c,
61 0x5f, 0x50, 0x9e, 0xad, 0xe2, 0xb7, 0x9a, 0xea, 0x77, 0xe2};
62
63 uint8_t sign_rnd[MLDSA_RNDBYTES] = {
64 0x3e, 0x2a, 0x2e, 0xa6, 0xc9, 0xc4, 0x76, 0xfc, 0x49, 0x37, 0xb0,
65 0x13, 0xc9, 0x93, 0xa7, 0x93, 0xd6, 0xc0, 0xab, 0x99, 0x60, 0x69,
66 0x5b, 0xa8, 0x38, 0xf6, 0x49, 0xda, 0x53, 0x9c, 0xa3, 0xd0};
67 uint8_t
68 pre[TEST_CTX_LEN + 2]; /* prefix string (0, ctxlen, ctx) for signing */
69 size_t siglen;
70
71 printf("ML-DSA-%d Deterministic Example\n", MLD_CONFIG_PARAMETER_SET);
72 printf("======================\n\n");
73
74 printf("Message: %s\n", test_msg);
75 printf("Context: %s\n\n", test_ctx);
76
77 printf("Generating keypair ... ");
78
79 /* Alice generates a public/private key pair */
80 CHECK(mldsa_keypair_internal(pk, sk, keypair_rnd) == 0);
81
82 printf("DONE\n");
83 printf("Signing message... ");
84
85 /* Prepare pre = (0, ctxlen, ctx) */
86 pre[0] = 0;
87 pre[1] = TEST_CTX_LEN;
88 memcpy(pre + 2, test_ctx, TEST_CTX_LEN);
89
90
91 /* Alice signs the message */
92 CHECK(mldsa_signature_internal(sig, &siglen, (const uint8_t *)test_msg,
93 TEST_MSG_LEN, pre, sizeof(pre), sign_rnd, sk,
94 0) == 0);
95
96 printf("DONE\n");
97 printf("Verifying signature... ");
98
99 /* Bob verifies Alice's signature */
100 CHECK(mldsa_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
101 (const uint8_t *)test_ctx, TEST_CTX_LEN, pk) == 0);
102
103 printf("DONE\n");
104
105 printf("Results:\n");
106 printf("--------\n");
107 printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
108 printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
109 printf("Signature size: %d bytes\n", CRYPTO_BYTES);
110 printf("Message length: %lu bytes\n", (unsigned long)TEST_MSG_LEN);
111 printf("Signature length: %lu bytes\n", (unsigned long)siglen);
112
113 /* Check against expected signature to make sure that
114 * we integrated the library correctly */
115 printf("Checking deterministic signature... ");
116 {
117 /* Compare the generated signature directly against the expected signature
118 */
119 CHECK(siglen == sizeof(expected_signature));
120 CHECK(memcmp(sig, expected_signature, siglen) == 0);
121 }
122 printf("DONE\n");
123
124 printf("Signature verification completed successfully!\n");
125
126 printf("\nAll tests passed! ML-DSA signature verification successful.\n");
127 return 0;
128 }