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