]> git.codecow.com Git - Monocypher.git/commitdiff
Fix the most nitpicky Clang warnings, fix C++ compatibility
authorLoup Vaillant <loup@loup-vaillant.fr>
Mon, 27 Feb 2023 14:06:24 +0000 (15:06 +0100)
committerLoup Vaillant <loup@loup-vaillant.fr>
Mon, 27 Feb 2023 14:06:24 +0000 (15:06 +0100)
This is mostly about shadowed variables.

makefile
src/monocypher.h
src/optional/monocypher-ed25519.c
tests/test.c
tests/tis-ci.c

index 5651c24b1cc3b07202383a127a9a5d3bd52b8189..1305e42376972fce64c36273ac31a5b2f4d020d1 100644 (file)
--- a/makefile
+++ b/makefile
@@ -53,7 +53,7 @@
 .SUFFIXES:
 
 CC=gcc -std=c99
-CFLAGS= -pedantic -Wall -Wextra -O3 -march=native
+CFLAGS=-pedantic -Wall -Wextra -O3 -march=native
 DESTDIR=
 PREFIX=usr/local
 LIBDIR=$(PREFIX)/lib
index c52c8ff57fa8c273cd925a8d68bede427ce6bb06..cf635e88e4f5851e1f58a9e2b3e85b8029a73861 100644 (file)
@@ -289,11 +289,11 @@ void crypto_poly1305(uint8_t        mac[16],
 typedef struct {
        // Do not rely on the size or contents of this type,
        // for they may change without notice.
-       uint32_t r[4];   // constant multiplier (from the secret key)
-       uint32_t h[5];   // accumulated hash
        uint8_t  c[16];  // chunk of the message
-       uint32_t pad[4]; // random number added at the end (from the secret key)
        size_t   c_idx;  // How many bytes are there in the chunk.
+       uint32_t r  [4]; // constant multiplier (from the secret key)
+       uint32_t pad[4]; // random number added at the end (from the secret key)
+       uint32_t h  [5]; // accumulated hash
 } crypto_poly1305_ctx;
 
 void crypto_poly1305_init  (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
index 5d370258d5bd7a02e2b79fa2456bb7cf3d12cd28..b76503ade19dd7fd2318f28df212e127746a118e 100644 (file)
@@ -61,8 +61,8 @@ namespace MONOCYPHER_CPP_NAMESPACE {
 /// Utilities ///
 /////////////////
 #define FOR(i, min, max)     for (size_t i = min; i < max; i++)
-#define COPY(dst, src, size) FOR(i, 0, size) (dst)[i] = (src)[i]
-#define ZERO(buf, size)      FOR(i, 0, size) (buf)[i] = 0
+#define COPY(dst, src, size) FOR(_i_, 0, size) (dst)[_i_] = (src)[_i_]
+#define ZERO(buf, size)      FOR(_i_, 0, size) (buf)[_i_] = 0
 #define WIPE_CTX(ctx)        crypto_wipe(ctx   , sizeof(*(ctx)))
 #define WIPE_BUFFER(buffer)  crypto_wipe(buffer, sizeof(buffer))
 #define MIN(a, b)            ((a) <= (b) ? (a) : (b))
@@ -477,7 +477,7 @@ int crypto_ed25519_check(const u8 signature[64], const u8 public_key[32],
        return crypto_eddsa_check_equation(signature, public_key, h_ram);
 }
 
-static const u8 domain[34] = "SigEd25519 no Ed25519 collisions\1\0";
+static const u8 domain[34] = "SigEd25519 no Ed25519 collisions\1";
 
 void crypto_ed25519_ph_sign(uint8_t signature[64], const uint8_t secret_key[32],
                             const uint8_t message_hash[64])
index 142ee88266f9cb8abbc2662eb5da16e712c04152..1725b1c0fcfb429aabded00700057335c120172a 100644 (file)
@@ -62,9 +62,9 @@
 ////////////////////////////////
 /// Constant time comparison ///
 ////////////////////////////////
-static void p_verify(size_t size, int (*compare)(const u8*, const u8*))
+static void p_verify(unsigned size, int (*compare)(const u8*, const u8*))
 {
-       printf("\tcrypto_verify%zu\n", size);
+       printf("\tcrypto_verify%u\n", size);
        u8 a[64]; // size <= 64
        u8 b[64]; // size <= 64
        FOR (i, 0, 2) {
@@ -437,27 +437,28 @@ static void test_blake2b()
        // interface.
 #undef INPUT_SIZE
 #define INPUT_SIZE (BLAKE2B_BLOCK_SIZE * 3) // total input size
+       {
+               RANDOM_INPUT(input, INPUT_SIZE);
 
-       RANDOM_INPUT(input, INPUT_SIZE);
-
-       // hash at once
-       u8 hash_whole[64];
-       crypto_blake2b(hash_whole, 64, input, INPUT_SIZE);
-
-       FOR (j, 0, INPUT_SIZE) {
-               FOR (i, 0, j+1) {
-                       // Hash bit by bit
-                       u8 *mid_input = j - i == 0 ? NULL : input + i; // test NULL update
-                       u8 hash_chunk[64];
-                       crypto_blake2b_ctx ctx;
-                       crypto_blake2b_init  (&ctx, 64);
-                       crypto_blake2b_update(&ctx, input    , i);
-                       crypto_blake2b_update(&ctx, mid_input, j - i);
-                       crypto_blake2b_update(&ctx, input + j, INPUT_SIZE - j);
-                       crypto_blake2b_final (&ctx, hash_chunk);
-
-                       // Compare the results (must be the same)
-                       ASSERT_EQUAL(hash_chunk, hash_whole, 64);
+               // hash at once
+               u8 hash_whole[64];
+               crypto_blake2b(hash_whole, 64, input, INPUT_SIZE);
+
+               FOR (j, 0, INPUT_SIZE) {
+                       FOR (i, 0, j+1) {
+                               // Hash bit by bit
+                               u8 *mid_input = j - i == 0 ? NULL : input + i; // NULL update
+                               u8 hash_chunk[64];
+                               crypto_blake2b_ctx ctx;
+                               crypto_blake2b_init  (&ctx, 64);
+                               crypto_blake2b_update(&ctx, input    , i);
+                               crypto_blake2b_update(&ctx, mid_input, j - i);
+                               crypto_blake2b_update(&ctx, input + j, INPUT_SIZE - j);
+                               crypto_blake2b_final (&ctx, hash_chunk);
+
+                               // Compare the results (must be the same)
+                               ASSERT_EQUAL(hash_chunk, hash_whole, 64);
+                       }
                }
        }
 
@@ -514,27 +515,28 @@ static void test_sha512()
        printf("\tSHA-512 (incremental)\n");
 #undef INPUT_SIZE
 #define INPUT_SIZE (SHA_512_BLOCK_SIZE * 4 - 32) // total input size
+       {
+               RANDOM_INPUT(input, INPUT_SIZE);
 
-       RANDOM_INPUT(input, INPUT_SIZE);
-
-       // hash at once
-       u8 hash_whole[64];
-       crypto_sha512(hash_whole, input, INPUT_SIZE);
-
-       FOR (j, 0, INPUT_SIZE) {
-               FOR (i, 0, j+1) {
-                       // Hash bit by bit
-                       u8 *mid_input = j - i == 0 ? NULL : input + i; // test NULL update
-                       u8 hash_chunk[64];
-                       crypto_sha512_ctx ctx;
-                       crypto_sha512_init  (&ctx);
-                       crypto_sha512_update(&ctx, input    , i);
-                       crypto_sha512_update(&ctx, mid_input, j - i);
-                       crypto_sha512_update(&ctx, input + j, INPUT_SIZE - j);
-                       crypto_sha512_final (&ctx, hash_chunk);
-
-                       // Compare the results (must be the same)
-                       ASSERT_EQUAL(hash_chunk, hash_whole, 64);
+               // hash at once
+               u8 hash_whole[64];
+               crypto_sha512(hash_whole, input, INPUT_SIZE);
+
+               FOR (j, 0, INPUT_SIZE) {
+                       FOR (i, 0, j+1) {
+                               // Hash bit by bit
+                               u8 *mid_input = j - i == 0 ? NULL : input + i; // NULL update
+                               u8 hash_chunk[64];
+                               crypto_sha512_ctx ctx;
+                               crypto_sha512_init  (&ctx);
+                               crypto_sha512_update(&ctx, input    , i);
+                               crypto_sha512_update(&ctx, mid_input, j - i);
+                               crypto_sha512_update(&ctx, input + j, INPUT_SIZE - j);
+                               crypto_sha512_final (&ctx, hash_chunk);
+
+                               // Compare the results (must be the same)
+                               ASSERT_EQUAL(hash_chunk, hash_whole, 64);
+                       }
                }
        }
 
@@ -609,16 +611,16 @@ static void argon2(vector_reader *reader)
        crypto_argon2_inputs inputs;
        inputs.pass      = pass.buf;
        inputs.salt      = salt.buf;
-       inputs.pass_size = pass.size;
-       inputs.salt_size = salt.size;
+       inputs.pass_size = (u32)pass.size;
+       inputs.salt_size = (u32)salt.size;
 
        crypto_argon2_extras extras;
        extras.key       = key.buf;
        extras.ad        = ad.buf;
-       extras.key_size  = key.size;
-       extras.ad_size   = ad.size;
+       extras.key_size  = (u32)key.size;
+       extras.ad_size   = (u32)ad.size;
 
-       crypto_argon2(out.buf, out.size, work_area, config, inputs, extras);
+       crypto_argon2(out.buf, (u32)out.size, work_area, config, inputs, extras);
        free(work_area);
 }
 
@@ -1102,8 +1104,7 @@ static void test_elligator()
        }
 
        printf("\telligator x25519\n");
-       int i = 0;
-       while (i < 64) {
+       FOR (i, 0, 64) {
                RANDOM_INPUT(sk1, 32);
                RANDOM_INPUT(sk2, 32);
                u8 skc [32];  memcpy(skc, sk1, 32);  skc[0] &= 248;
@@ -1130,7 +1131,8 @@ static void test_elligator()
                u8 tweak = (u8)((i & 1) + (i << 5));
                u8 r[32];
                if (crypto_elligator_rev(r, pkf, tweak)) {
-                       continue; // retry untill success (doesn't increment the tweak)
+                       i--;      // Cancel tweak increment
+                       continue; // retry untill success
                }
                // Verify that the tweak's msb are copied to the representative
                ASSERT((tweak >> 6) == (r[31] >> 6));
@@ -1143,7 +1145,6 @@ static void test_elligator()
                u8 e1 [32];  crypto_x25519(e1, sk2, pk1);
                u8 e2 [32];  crypto_x25519(e2, sk2, pkr);
                ASSERT_EQUAL(e1, e2, 32);
-               i++;
        }
 
        printf("\telligator key pair\n");
index 09fe36be94b0879310d969b15402610fa1d6e51c..d1544a7923b6886f5ac2db12afc0137dddca06fa 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#define ARRAY(name, size)      \
-       u8 name[size]; \
-       for(size_t i = 0; i < size; i++) name[i] = i;
-
 static void chacha20(vector_reader *reader)
 {
        vector key       = next_input(reader);
@@ -85,7 +81,7 @@ static void ietf_chacha20(vector_reader *reader)
        vector out       = next_output(reader);
        u32    nb_blocks = (u32)(plain.size / 64 + (plain.size % 64 != 0));
        u32    new_ctr   = crypto_chacha20_ietf(out.buf, plain.buf, plain.size,
-                                               key.buf, nonce.buf, ctr);
+                                               key.buf, nonce.buf, (u32)ctr);
        if (new_ctr - ctr != nb_blocks) {
                printf("FAILURE: IETF Chacha20 returned counter not correct: ");
        }
@@ -188,16 +184,16 @@ static void argon2(vector_reader *reader)
        crypto_argon2_inputs inputs;
        inputs.pass      = pass.buf;
        inputs.salt      = salt.buf;
-       inputs.pass_size = pass.size;
-       inputs.salt_size = salt.size;
+       inputs.pass_size = (u32)pass.size;
+       inputs.salt_size = (u32)salt.size;
 
        crypto_argon2_extras extras;
        extras.key       = key.buf;
        extras.ad        = ad.buf;
-       extras.key_size  = key.size;
-       extras.ad_size   = ad.size;
+       extras.key_size  = (u32)key.size;
+       extras.ad_size   = (u32)ad.size;
 
-       crypto_argon2(out.buf, out.size, work_area, config, inputs, extras);
+       crypto_argon2(out.buf, (u32)out.size, work_area, config, inputs, extras);
        free(work_area);
 }