Skip to content

Commit 19d1e75

Browse files
committed
Fix C90 issues in example code
The C90 tests were not functional before as make clean would not clean the example code. The previous commit fixed that and revealed some C90 issues in the example code. This commit fixes the C90 issues. Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 11514b2 commit 19d1e75

File tree

4 files changed

+88
-81
lines changed

4 files changed

+88
-81
lines changed

examples/basic/main.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@
3131
} \
3232
} while (0)
3333

34+
#define TEST_MSG \
35+
"This is a test message for ML-DSA digital signature algorithm!"
36+
#define TEST_MSG_LEN (sizeof(TEST_MSG) - 1)
37+
3438
int main(void)
3539
{
36-
const char *test_msg =
37-
"This is a test message for ML-DSA digital signature algorithm!";
40+
const char test_msg[] = TEST_MSG;
3841
const char *test_ctx = "test_context_123";
39-
size_t msglen = strlen(test_msg);
4042
size_t ctxlen = strlen(test_ctx);
4143

4244
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4345
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4446
uint8_t sig[CRYPTO_BYTES];
45-
uint8_t sm[msglen + CRYPTO_BYTES]; /* signed message buffer */
46-
uint8_t m2[msglen]; /* recovered message buffer */
47+
uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
48+
uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
4749
size_t siglen;
4850
size_t smlen;
4951
size_t mlen;
@@ -67,21 +69,22 @@ int main(void)
6769
printf("Signing message... ");
6870

6971
/* Alice signs the message */
70-
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg, msglen,
71-
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
72+
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg,
73+
TEST_MSG_LEN, (const uint8_t *)test_ctx, ctxlen,
74+
sk) == 0);
7275

7376
printf("DONE\n");
7477
printf("Verifying signature... ");
7578

7679
/* Bob verifies Alice's signature */
77-
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, msglen,
80+
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
7881
(const uint8_t *)test_ctx, ctxlen, pk) == 0);
7982

8083
printf("DONE\n");
8184
printf("Creating signed message... ");
8285

8386
/* Alternative API: Create a signed message (signature + message combined) */
84-
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, msglen,
87+
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN,
8588
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
8689

8790
printf("DONE\n");
@@ -95,8 +98,8 @@ int main(void)
9598
printf("Compare messages... ");
9699

97100
/* Verify the recovered message matches the original */
98-
CHECK(mlen == msglen);
99-
CHECK(memcmp(test_msg, m2, msglen) == 0);
101+
CHECK(mlen == TEST_MSG_LEN);
102+
CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0);
100103

101104
printf("DONE\n\n");
102105

@@ -105,9 +108,9 @@ int main(void)
105108
printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
106109
printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
107110
printf("Signature size: %d bytes\n", CRYPTO_BYTES);
108-
printf("Message length: %zu bytes\n", msglen);
109-
printf("Signature length: %zu bytes\n", siglen);
110-
printf("Signed msg length: %zu bytes\n", smlen);
111+
printf("Message length: %lu bytes\n", TEST_MSG_LEN);
112+
printf("Signature length: %lu bytes\n", (unsigned long)siglen);
113+
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen);
111114

112115
#if !defined(MLD_CONFIG_KEYGEN_PCT)
113116
/* Check against expected signature to make sure that

examples/bring_your_own_fips202/main.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@
3131
} \
3232
} while (0)
3333

34+
#define TEST_MSG \
35+
"This is a test message for ML-DSA digital signature algorithm!"
36+
#define TEST_MSG_LEN (sizeof(TEST_MSG) - 1)
37+
3438
int main(void)
3539
{
36-
const char *test_msg =
37-
"This is a test message for ML-DSA digital signature algorithm!";
40+
const char test_msg[] = TEST_MSG;
3841
const char *test_ctx = "test_context_123";
39-
size_t msglen = strlen(test_msg);
4042
size_t ctxlen = strlen(test_ctx);
4143

4244
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4345
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4446
uint8_t sig[CRYPTO_BYTES];
45-
uint8_t sm[msglen + CRYPTO_BYTES]; /* signed message buffer */
46-
uint8_t m2[msglen]; /* recovered message buffer */
47+
uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
48+
uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
4749
size_t siglen;
4850
size_t smlen;
4951
size_t mlen;
@@ -68,21 +70,22 @@ int main(void)
6870
printf("Signing message... ");
6971

7072
/* Alice signs the message */
71-
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg, msglen,
72-
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
73+
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg,
74+
TEST_MSG_LEN, (const uint8_t *)test_ctx, ctxlen,
75+
sk) == 0);
7376

7477
printf("DONE\n");
7578
printf("Verifying signature... ");
7679

7780
/* Bob verifies Alice's signature */
78-
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, msglen,
81+
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
7982
(const uint8_t *)test_ctx, ctxlen, pk) == 0);
8083

8184
printf("DONE\n");
8285
printf("Creating signed message... ");
8386

8487
/* Alternative API: Create a signed message (signature + message combined) */
85-
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, msglen,
88+
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN,
8689
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
8790

8891
printf("DONE\n");
@@ -96,8 +99,8 @@ int main(void)
9699
printf("Compare messages... ");
97100

98101
/* Verify the recovered message matches the original */
99-
CHECK(mlen == msglen);
100-
CHECK(memcmp(test_msg, m2, msglen) == 0);
102+
CHECK(mlen == TEST_MSG_LEN);
103+
CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0);
101104

102105
printf("DONE\n\n");
103106

@@ -106,9 +109,9 @@ int main(void)
106109
printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
107110
printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
108111
printf("Signature size: %d bytes\n", CRYPTO_BYTES);
109-
printf("Message length: %zu bytes\n", msglen);
110-
printf("Signature length: %zu bytes\n", siglen);
111-
printf("Signed msg length: %zu bytes\n", smlen);
112+
printf("Message length: %lu bytes\n", TEST_MSG_LEN);
113+
printf("Signature length: %lu bytes\n", (unsigned long)siglen);
114+
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen);
112115

113116
#if !defined(MLD_CONFIG_KEYGEN_PCT)
114117
/* Check against expected signature to make sure that

examples/monolithic_build/main.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@
3333
} \
3434
} while (0)
3535

36+
#define TEST_MSG \
37+
"This is a test message for ML-DSA digital signature algorithm!"
38+
#define TEST_MSG_LEN (sizeof(TEST_MSG) - 1)
39+
3640
int main(void)
3741
{
38-
const char *test_msg =
39-
"This is a test message for ML-DSA digital signature algorithm!";
42+
const char test_msg[] = TEST_MSG;
4043
const char *test_ctx = "test_context_123";
41-
size_t msglen = strlen(test_msg);
4244
size_t ctxlen = strlen(test_ctx);
4345

4446
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4547
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4648
uint8_t sig[CRYPTO_BYTES];
47-
uint8_t sm[msglen + CRYPTO_BYTES]; /* signed message buffer */
48-
uint8_t m2[msglen]; /* recovered message buffer */
49+
uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
50+
uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
4951
size_t siglen;
5052
size_t smlen;
5153
size_t mlen;
@@ -69,21 +71,22 @@ int main(void)
6971
printf("Signing message... ");
7072

7173
/* Alice signs the message */
72-
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg, msglen,
73-
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
74+
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg,
75+
TEST_MSG_LEN, (const uint8_t *)test_ctx, ctxlen,
76+
sk) == 0);
7477

7578
printf("DONE\n");
7679
printf("Verifying signature... ");
7780

7881
/* Bob verifies Alice's signature */
79-
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, msglen,
82+
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
8083
(const uint8_t *)test_ctx, ctxlen, pk) == 0);
8184

8285
printf("DONE\n");
8386
printf("Creating signed message... ");
8487

8588
/* Alternative API: Create a signed message (signature + message combined) */
86-
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, msglen,
89+
CHECK(crypto_sign(sm, &smlen, (const uint8_t *)test_msg, TEST_MSG_LEN,
8790
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
8891

8992
printf("DONE\n");
@@ -97,8 +100,8 @@ int main(void)
97100
printf("Compare messages... ");
98101

99102
/* Verify the recovered message matches the original */
100-
CHECK(mlen == msglen);
101-
CHECK(memcmp(test_msg, m2, msglen) == 0);
103+
CHECK(mlen == TEST_MSG_LEN);
104+
CHECK(memcmp(test_msg, m2, TEST_MSG_LEN) == 0);
102105

103106
printf("DONE\n\n");
104107

@@ -107,9 +110,9 @@ int main(void)
107110
printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
108111
printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
109112
printf("Signature size: %d bytes\n", CRYPTO_BYTES);
110-
printf("Message length: %zu bytes\n", msglen);
111-
printf("Signature length: %zu bytes\n", siglen);
112-
printf("Signed msg length: %zu bytes\n", smlen);
113+
printf("Message length: %lu bytes\n", TEST_MSG_LEN);
114+
printf("Signature length: %lu bytes\n", (unsigned long)siglen);
115+
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen);
113116

114117
#if !defined(MLD_CONFIG_KEYGEN_PCT)
115118
/* Check against expected signature to make sure that

0 commit comments

Comments
 (0)