Skip to content

Commit 3ad3089

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 170ad1f commit 3ad3089

File tree

4 files changed

+74
-69
lines changed

4 files changed

+74
-69
lines changed

examples/basic/main.c

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

34+
#define TEST_MSG_LEN 62 /* Length of the test message string */
35+
3436
int main(void)
3537
{
3638
const char *test_msg =
3739
"This is a test message for ML-DSA digital signature algorithm!";
3840
const char *test_ctx = "test_context_123";
39-
size_t msglen = strlen(test_msg);
4041
size_t ctxlen = strlen(test_ctx);
4142

4243
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4344
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4445
uint8_t sig[CRYPTO_BYTES];
45-
uint8_t sm[msglen + CRYPTO_BYTES]; /* signed message buffer */
46-
uint8_t m2[msglen]; /* recovered message buffer */
46+
uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
47+
uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
4748
size_t siglen;
4849
size_t smlen;
4950
size_t mlen;
@@ -67,21 +68,22 @@ int main(void)
6768
printf("Signing message... ");
6869

6970
/* 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);
71+
CHECK(crypto_sign_signature(sig, &siglen, (const uint8_t *)test_msg,
72+
TEST_MSG_LEN, (const uint8_t *)test_ctx, ctxlen,
73+
sk) == 0);
7274

7375
printf("DONE\n");
7476
printf("Verifying signature... ");
7577

7678
/* Bob verifies Alice's signature */
77-
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, msglen,
79+
CHECK(crypto_sign_verify(sig, siglen, (const uint8_t *)test_msg, TEST_MSG_LEN,
7880
(const uint8_t *)test_ctx, ctxlen, pk) == 0);
7981

8082
printf("DONE\n");
8183
printf("Creating signed message... ");
8284

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

8789
printf("DONE\n");
@@ -95,8 +97,8 @@ int main(void)
9597
printf("Compare messages... ");
9698

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

101103
printf("DONE\n\n");
102104

@@ -105,9 +107,9 @@ int main(void)
105107
printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
106108
printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
107109
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);
110+
printf("Message length: %d bytes\n", TEST_MSG_LEN);
111+
printf("Signature length: %lu bytes\n", (unsigned long)siglen);
112+
printf("Signed msg length: %lu bytes\n", (unsigned long)smlen);
111113

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

examples/bring_your_own_fips202/main.c

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

34+
#define TEST_MSG_LEN 62 /* Length of the test message string */
35+
3436
int main(void)
3537
{
3638
const char *test_msg =
3739
"This is a test message for ML-DSA digital signature algorithm!";
3840
const char *test_ctx = "test_context_123";
39-
size_t msglen = strlen(test_msg);
4041
size_t ctxlen = strlen(test_ctx);
4142

4243
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4344
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4445
uint8_t sig[CRYPTO_BYTES];
45-
uint8_t sm[msglen + CRYPTO_BYTES]; /* signed message buffer */
46-
uint8_t m2[msglen]; /* recovered message buffer */
46+
uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
47+
uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
4748
size_t siglen;
4849
size_t smlen;
4950
size_t mlen;
@@ -68,21 +69,22 @@ int main(void)
6869
printf("Signing message... ");
6970

7071
/* 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);
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);
7375

7476
printf("DONE\n");
7577
printf("Verifying signature... ");
7678

7779
/* Bob verifies Alice's signature */
78-
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,
7981
(const uint8_t *)test_ctx, ctxlen, pk) == 0);
8082

8183
printf("DONE\n");
8284
printf("Creating signed message... ");
8385

8486
/* Alternative API: Create a signed message (signature + message combined) */
85-
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,
8688
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
8789

8890
printf("DONE\n");
@@ -96,8 +98,8 @@ int main(void)
9698
printf("Compare messages... ");
9799

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

102104
printf("DONE\n\n");
103105

@@ -106,9 +108,9 @@ int main(void)
106108
printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
107109
printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
108110
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);
111+
printf("Message length: %d 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);
112114

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

examples/monolithic_build/main.c

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

36+
#define TEST_MSG_LEN 62 /* Length of the test message string */
37+
3638
int main(void)
3739
{
3840
const char *test_msg =
3941
"This is a test message for ML-DSA digital signature algorithm!";
4042
const char *test_ctx = "test_context_123";
41-
size_t msglen = strlen(test_msg);
4243
size_t ctxlen = strlen(test_ctx);
4344

4445
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4546
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4647
uint8_t sig[CRYPTO_BYTES];
47-
uint8_t sm[msglen + CRYPTO_BYTES]; /* signed message buffer */
48-
uint8_t m2[msglen]; /* recovered message buffer */
48+
uint8_t sm[TEST_MSG_LEN + CRYPTO_BYTES]; /* signed message buffer */
49+
uint8_t m2[TEST_MSG_LEN + CRYPTO_BYTES]; /* recovered message buffer */
4950
size_t siglen;
5051
size_t smlen;
5152
size_t mlen;
@@ -69,21 +70,22 @@ int main(void)
6970
printf("Signing message... ");
7071

7172
/* 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);
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);
7476

7577
printf("DONE\n");
7678
printf("Verifying signature... ");
7779

7880
/* Bob verifies Alice's signature */
79-
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,
8082
(const uint8_t *)test_ctx, ctxlen, pk) == 0);
8183

8284
printf("DONE\n");
8385
printf("Creating signed message... ");
8486

8587
/* Alternative API: Create a signed message (signature + message combined) */
86-
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,
8789
(const uint8_t *)test_ctx, ctxlen, sk) == 0);
8890

8991
printf("DONE\n");
@@ -97,8 +99,8 @@ int main(void)
9799
printf("Compare messages... ");
98100

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

103105
printf("DONE\n\n");
104106

@@ -107,9 +109,9 @@ int main(void)
107109
printf("Public key size: %d bytes\n", CRYPTO_PUBLICKEYBYTES);
108110
printf("Secret key size: %d bytes\n", CRYPTO_SECRETKEYBYTES);
109111
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);
112+
printf("Message length: %d 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);
113115

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

0 commit comments

Comments
 (0)