forked from pq-code-package/mldsa-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
156 lines (138 loc) · 4.78 KB
/
Copy pathmain.c
File metadata and controls
156 lines (138 loc) · 4.78 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* 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_test_vectors.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)
#if !defined(MLD_CONFIG_NO_KEYPAIR_API)
static int example_keygen(void)
{
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
uint8_t sk[CRYPTO_SECRETKEYBYTES];
printf("Generating keypair... ");
CHECK(crypto_sign_keypair(pk, sk) == 0);
CHECK(memcmp(pk, test_vector_pk, CRYPTO_PUBLICKEYBYTES) == 0);
CHECK(memcmp(sk, test_vector_sk, CRYPTO_SECRETKEYBYTES) == 0);
printf("DONE\n");
return 0;
}
#else /* !MLD_CONFIG_NO_KEYPAIR_API */
static int example_keygen(void)
{
printf("Generating keypair... SKIPPED (keygen API disabled)\n");
return 0;
}
#endif /* MLD_CONFIG_NO_KEYPAIR_API */
#if !defined(MLD_CONFIG_NO_SIGN_API)
static int example_sign(void)
{
uint8_t sig[CRYPTO_BYTES];
size_t siglen;
printf("Signing message... ");
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)TEST_VECTOR_MSG,
TEST_VECTOR_MSG_LEN,
(const uint8_t *)TEST_VECTOR_CTX,
TEST_VECTOR_CTX_LEN, test_vector_sk) == 0);
CHECK(siglen == sizeof(test_vector_sig));
CHECK(memcmp(sig, test_vector_sig, siglen) == 0);
printf("DONE\n");
return 0;
}
#else /* !MLD_CONFIG_NO_SIGN_API */
static int example_sign(void)
{
printf("Signing message... SKIPPED (sign API disabled)\n");
return 0;
}
#endif /* MLD_CONFIG_NO_SIGN_API */
#if !defined(MLD_CONFIG_NO_VERIFY_API)
static int example_verify(void)
{
printf("Verifying signature... ");
CHECK(crypto_sign_verify(test_vector_sig, sizeof(test_vector_sig),
(const uint8_t *)TEST_VECTOR_MSG,
TEST_VECTOR_MSG_LEN,
(const uint8_t *)TEST_VECTOR_CTX,
TEST_VECTOR_CTX_LEN, test_vector_pk) == 0);
printf("DONE\n");
return 0;
}
#else /* !MLD_CONFIG_NO_VERIFY_API */
static int example_verify(void)
{
printf("Verifying signature... SKIPPED (verify API disabled)\n");
return 0;
}
#endif /* MLD_CONFIG_NO_VERIFY_API */
#if !defined(MLD_CONFIG_NO_SIGN_API) && !defined(MLD_CONFIG_NO_VERIFY_API)
static int example_sign_message(void)
{
uint8_t sm[TEST_VECTOR_MSG_LEN + CRYPTO_BYTES];
uint8_t m2[TEST_VECTOR_MSG_LEN + CRYPTO_BYTES];
size_t smlen;
size_t mlen;
printf("Sign and open message... ");
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)TEST_VECTOR_MSG,
TEST_VECTOR_MSG_LEN, (const uint8_t *)TEST_VECTOR_CTX,
TEST_VECTOR_CTX_LEN, test_vector_sk) == 0);
CHECK(crypto_sign_open(m2, &mlen, sm, smlen, (const uint8_t *)TEST_VECTOR_CTX,
TEST_VECTOR_CTX_LEN, test_vector_pk) == 0);
CHECK(mlen == TEST_VECTOR_MSG_LEN);
CHECK(memcmp(TEST_VECTOR_MSG, m2, TEST_VECTOR_MSG_LEN) == 0);
printf("DONE\n");
return 0;
}
#else /* !MLD_CONFIG_NO_SIGN_API && !MLD_CONFIG_NO_VERIFY_API */
static int example_sign_message(void)
{
printf("Sign and open message... SKIPPED (requires sign+verify APIs)\n");
return 0;
}
#endif /* !(!MLD_CONFIG_NO_SIGN_API && !MLD_CONFIG_NO_VERIFY_API) */
int main(void)
{
int r = 0;
printf("ML-DSA-%d Basic Example\n", MLD_CONFIG_PARAMETER_SET);
printf("======================\n\n");
printf("Message: %s\n", TEST_VECTOR_MSG);
printf("Context: %s\n\n", TEST_VECTOR_CTX);
/* WARNING: Test-only
* Normally, you would want to seed a PRNG with trustworthy entropy here. */
randombytes_reset();
r |= example_keygen();
/* WARNING: Test-only
* Normally, you would seed a PRNG _once_ with trustworthy entropy
* and not reseed it afterwards. Here, we reseed to make tests
* independent and reproducible. */
randombytes_reset();
r |= example_sign();
r |= example_verify();
r |= example_sign_message();
if (r)
{
return 1;
}
printf("\nAll tests passed! ML-DSA signature verification successful.\n");
return 0;
}