]> git.codecow.com Git - Monocypher.git/commitdiff
Corrected clang warnings
authorLoup Vaillant <loup@loup-vaillant.fr>
Sat, 28 Sep 2019 09:30:47 +0000 (11:30 +0200)
committerLoup Vaillant <loup@loup-vaillant.fr>
Sat, 28 Sep 2019 09:30:47 +0000 (11:30 +0200)
Those are easily visible through the QtCreator IDE intellisense, but
somehow never showed up when compiling at the command line.  This should
help silence MSVC warnings as well.

src/monocypher.c
tests/speed.h
tests/test.c
tests/utils.h
tests/vector_to_header.c

index 777d00daac76a25ff64abd76af6eb6a21457ecf2..20b4ff4d8c8d7828f0f96bfc13a0ff28f97ec102 100644 (file)
@@ -19,7 +19,7 @@
 #define HASH_UPDATE COMBINE2(HASH, _update)
 #define HASH_FINAL  COMBINE2(HASH, _final)
 
-#define FOR(i, start, end)   for (size_t (i) = (start); (i) < (end); (i)++)
+#define FOR(i, start, end)   for (size_t i = (start); i < (end); i++)
 #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))
@@ -492,7 +492,7 @@ static void blake2b_compress(crypto_blake2b_ctx *ctx, int is_last_block)
     u64 v3 = ctx->hash[3];  u64 v11 = iv[3];
     u64 v4 = ctx->hash[4];  u64 v12 = iv[4] ^ ctx->input_offset[0];
     u64 v5 = ctx->hash[5];  u64 v13 = iv[5] ^ ctx->input_offset[1];
-    u64 v6 = ctx->hash[6];  u64 v14 = iv[6] ^ is_last_block;
+    u64 v6 = ctx->hash[6];  u64 v14 = iv[6] ^ (u64)~(is_last_block - 1);
     u64 v7 = ctx->hash[7];  u64 v15 = iv[7];
 
     // mangle work vector
@@ -501,7 +501,7 @@ static void blake2b_compress(crypto_blake2b_ctx *ctx, int is_last_block)
     v##a += v##b + x;  v##d = rotr64(v##d ^ v##a, 32); \
     v##c += v##d;      v##b = rotr64(v##b ^ v##c, 24); \
     v##a += v##b + y;  v##d = rotr64(v##d ^ v##a, 16); \
-    v##c += v##d;      v##b = rotr64(v##b ^ v##c, 63);
+    v##c += v##d;      v##b = rotr64(v##b ^ v##c, 63)
 #define BLAKE2_ROUND(i)                                                 \
     BLAKE2_G(v, 0, 4,  8, 12, input[sigma[i][ 0]], input[sigma[i][ 1]]);\
     BLAKE2_G(v, 1, 5,  9, 13, input[sigma[i][ 2]], input[sigma[i][ 3]]);\
@@ -621,8 +621,8 @@ void crypto_blake2b_final(crypto_blake2b_ctx *ctx, u8 *hash)
     FOR (i, ctx->input_idx, 128) {
         blake2b_set_input(ctx, 0, i);
     }
-    blake2b_incr(ctx);         // update the input offset
-    blake2b_compress(ctx, -1); // compress the last block
+    blake2b_incr(ctx);        // update the input offset
+    blake2b_compress(ctx, 1); // compress the last block
     size_t nb_words = ctx->hash_size >> 3;
     FOR (i, 0, nb_words) {
         store64_le(hash + i*8, ctx->hash[i]);
@@ -708,7 +708,7 @@ static void extended_hash(u8       *digest, u32 digest_size,
     if (digest_size > 64) {
         // the conversion to u64 avoids integer overflow on
         // ludicrously big hash sizes.
-        u32 r   = (((u64)digest_size + 31) >> 5) - 2;
+        u32 r   = (u32)(((u64)digest_size + 31) >> 5) - 2;
         u32 i   =  1;
         u32 in  =  0;
         u32 out = 32;
@@ -1133,7 +1133,7 @@ static void fe_mul(fe h, const fe f, const fe g)
     c9 = (h9 + (i64) (1<<24)) >> 25; h0 += c9 * 19; h9 -= c9 * (1 << 25); \
     c0 = (h0 + (i64) (1<<25)) >> 26; h1 += c0;      h0 -= c0 * (1 << 26); \
     h[0]=(i32)h0;  h[1]=(i32)h1;  h[2]=(i32)h2;  h[3]=(i32)h3;  h[4]=(i32)h4; \
-    h[5]=(i32)h5;  h[6]=(i32)h6;  h[7]=(i32)h7;  h[8]=(i32)h8;  h[9]=(i32)h9; \
+    h[5]=(i32)h5;  h[6]=(i32)h6;  h[7]=(i32)h7;  h[8]=(i32)h8;  h[9]=(i32)h9
 
     CARRY;
 }
@@ -1285,7 +1285,7 @@ static void trim_scalar(u8 s[32])
     s[31] |= 64;
 }
 
-static int scalar_bit(const u8 s[32], size_t i) {return (s[i>>3] >> (i&7)) & 1;}
+static int scalar_bit(const u8 s[32], int i) { return (s[i>>3] >> (i&7)) & 1; }
 
 ///////////////
 /// X-25519 /// Taken from Supercop's ref10 implementation.
@@ -1398,7 +1398,7 @@ static void reduce(u8 r[64])
 {
     i64 x[64];
     FOR (i, 0, 64) {
-        x[i] = (u64) r[i];
+        x[i] = (i64)(u64)r[i]; // preserve unsigned
         r[i] = 0;
     }
     modL(r, x);
@@ -1409,11 +1409,11 @@ static void reduce(u8 r[64])
 static void mul_add(u8 r[32], const u8 a[32], const u8 b[32], const u8 c[32])
 {
     i64 s[64];
-    FOR (i,  0, 32) { s[i] = (u64) c[i]; }
-    FOR (i, 32, 64) { s[i] = 0;          }
+    FOR (i,  0, 32) { s[i] = (i64)(u64)c[i]; } // preserve unsigned
+    FOR (i, 32, 64) { s[i] = 0;              }
     FOR (i,  0, 32) {
         FOR (j, 0, 32) {
-            s[i+j] += a[i] * (u64) b[j];
+            s[i+j] += a[i] * (u64)b[j];
         }
     }
     modL(r, s);
@@ -1654,8 +1654,8 @@ static const fe window_T2[8] = {
 // Compute signed sliding windows (either 0, or odd numbers)
 static void slide(size_t width, i8 *adds, const u8 scalar[32])
 {
-    FOR (i,   0, 256        ) { adds[i] = (i8)scalar_bit(scalar, i); }
-    FOR (i, 256, 253 + width) { adds[i] = 0;                         }
+    FOR (i,   0, 256        ) { adds[i] = (i8)scalar_bit(scalar, (int)i); }
+    FOR (i, 256, 253 + width) { adds[i] = 0;                              }
     FOR (i, 0, 254) {
         if (adds[i] != 0) {
             // base value of the window
@@ -1713,17 +1713,19 @@ static void ge_double_scalarmult_vartime(ge *sum, const ge *P,
 
     // Merged double and add ladder
     fe t1, t2;
-#define CACHED_ADD(i)                                              \
-    if (p_adds[i] > 0) { ge_add(sum, sum, &cP[ p_adds[i] / 2]); }  \
-    if (p_adds[i] < 0) { ge_sub(sum, sum, &cP[-p_adds[i] / 2]); }  \
-    if (b_adds[i] > 0) { ge_madd(sum, sum,                         \
-                                 window_Yp[ b_adds[i] / 2],            \
-                                 window_Ym[ b_adds[i] / 2],            \
-                                 window_T2[ b_adds[i] / 2], t1, t2); } \
-    if (b_adds[i] < 0) { ge_msub(sum, sum,                             \
-                                 window_Yp[-b_adds[i] / 2],            \
-                                 window_Ym[-b_adds[i] / 2],            \
-                                 window_T2[-b_adds[i] / 2], t1, t2); }
+#define CACHED_ADD(i)                                                      \
+    do {                                                                   \
+        if (p_adds[i] > 0) { ge_add(sum, sum, &cP[ p_adds[i] / 2]); }      \
+        if (p_adds[i] < 0) { ge_sub(sum, sum, &cP[-p_adds[i] / 2]); }      \
+        if (b_adds[i] > 0) { ge_madd(sum, sum,                             \
+                                     window_Yp[ b_adds[i] / 2],            \
+                                     window_Ym[ b_adds[i] / 2],            \
+                                     window_T2[ b_adds[i] / 2], t1, t2); } \
+        if (b_adds[i] < 0) { ge_msub(sum, sum,                             \
+                                     window_Yp[-b_adds[i] / 2],            \
+                                     window_Ym[-b_adds[i] / 2],            \
+                                     window_T2[-b_adds[i] / 2], t1, t2); } \
+    } while (0)
     ge_zero(sum);
     CACHED_ADD(i);
     i--;
index b9d0455a3351fe61193fcec594f7b57f1f8c8059..9438d0d0aa1066bda09fb6d40e494a9d770a6744 100644 (file)
@@ -16,9 +16,8 @@ typedef struct timespec timespec;
 // Difference in nanoseconds
 static u64 diff(timespec start, timespec end)
 {
-    return
-        (end.tv_sec  - start.tv_sec ) * BILLION +
-        (end.tv_nsec - start.tv_nsec);
+    return (u64)((end.tv_sec  - start.tv_sec ) * BILLION +
+                 (end.tv_nsec - start.tv_nsec));
 }
 
 static u64 min(u64 a, u64 b)
@@ -44,7 +43,7 @@ static void print(const char *name, u64 duration, const char *unit)
     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t)
 
 #define TIMING_START                            \
-    u64 duration = -1u;                         \
+    u64 duration = (u64)-1;                     \
     FOR (i, 0, 500) {                           \
         TIMESTAMP(start);
 
index f1e289c6019b2051db15d7be89959ed64c6228ed..899a6f4a7a62deddfbcee66f56b069d32da5dd4b 100644 (file)
@@ -12,7 +12,6 @@
 #define POLY1305_BLOCK_SIZE  16
 #define BLAKE2B_BLOCK_SIZE  128
 #define SHA_512_BLOCK_SIZE  128
-#define COMPARISON_DIFF_THRESHOLD 0.04
 
 /////////////////
 /// Utilities ///
@@ -164,12 +163,12 @@ static void argon2i(const vector in[], vector *out)
     const vector *ad       = in + 5;
 
     void *work_area = alloc(nb_blocks * 1024);
-    crypto_argon2i_general(out->buf, out->size,
-                           work_area, nb_blocks, nb_iterations,
-                           password->buf, password->size,
-                           salt    ->buf, salt    ->size,
-                           key     ->buf, key     ->size,
-                           ad      ->buf, ad      ->size);
+    crypto_argon2i_general(out->buf, (u32)out->size,
+                           work_area, (u32)nb_blocks, (u32)nb_iterations,
+                           password->buf, (u32)password->size,
+                           salt    ->buf, (u32)salt    ->size,
+                           key     ->buf, (u32)key     ->size,
+                           ad      ->buf, (u32)ad      ->size);
     free(work_area);
 }
 
@@ -261,11 +260,13 @@ static void monokex_xk1(const vector in[], vector *out)
         fprintf(stderr, "FAILURE: crypto_kex_xk1_4\n");
         return;
     }
-#define COMPARE(local, vector)                      \
-    if (memcmp(local, vector->buf, vector->size)) { \
-        fprintf(stderr, "FAILURE: "#vector"\n");    \
-        return;                                     \
-    }
+#define COMPARE(local, vector)                          \
+    do {                                                \
+        if (memcmp(local, vector->buf, vector->size)) { \
+            fprintf(stderr, "FAILURE: "#vector"\n");    \
+            return;                                     \
+        }                                               \
+    } while (0)
     COMPARE(m1        , msg1);
     COMPARE(m2        , msg2);
     COMPARE(m3        , msg3);
@@ -351,8 +352,8 @@ static int p_verify(size_t size, int (*compare)(const u8*, const u8*))
         FOR (j, 0, 2) {
             // Set every byte to the chosen value, then compare
             FOR (k, 0, size) {
-                a[k] = i;
-                b[k] = j;
+                a[k] = (u8)i;
+                b[k] = (u8)j;
             }
             int cmp = compare(a, b);
             status |= (i == j ? cmp : ~cmp);
@@ -362,8 +363,8 @@ static int p_verify(size_t size, int (*compare)(const u8*, const u8*))
                     a[l] = 0;
                     b[l] = 0;
                 }
-                a[k] = i; a[k + size/2 - 1] = i;
-                b[k] = j; b[k + size/2 - 1] = j;
+                a[k] = (u8)i; a[k + size/2 - 1] = (u8)i;
+                b[k] = (u8)j; b[k + size/2 - 1] = (u8)j;
                 int cmp = compare(a, b);
                 status |= (i == j ? cmp : ~cmp);
             }
index e4b6256fdd0f1758dff54b352762cacf6556507b..9d7bcc1bcd8ea0cb09f4180da18d17a9e236830e 100644 (file)
@@ -12,12 +12,14 @@ typedef int32_t  i32;
 typedef int64_t  i64;
 typedef uint64_t u64;
 
-#define FOR(i, start, end) for (size_t (i) = (start); (i) < (end); (i)++)
-#define SODIUM_INIT                                 \
-    if (sodium_init() == -1) {                      \
-        printf("Libsodium init failed.  Abort.\n"); \
-        return 1;                                   \
-    }
+#define FOR(i, start, end) for (size_t i = (start); i < (end); i++)
+#define SODIUM_INIT                                     \
+    do {                                                \
+        if (sodium_init() == -1) {                      \
+            printf("Libsodium init failed.  Abort.\n"); \
+            return 1;                                   \
+        }                                               \
+    } while (0)
 #define RANDOM_INPUT(name, size) u8 name[size]; p_random(name, size)
 
 static void store64_le(u8 out[8], u64 in)
index 170bd2aa74b5bc02293a9143e6c595bb36f4bd2b..1c2cbdd98447a45226a20eac5e1fc48cc30852f3 100644 (file)
@@ -34,8 +34,8 @@ int main(int argc, char** argv)
         else {
             printf("uint8_t %s_%d[] = { ", prefix, nb_vec);
             while (c != ':') {
-                char msb = c;  c = getchar();
-                char lsb = c;  c = getchar();
+                char msb = (char)c;  c = getchar();
+                char lsb = (char)c;  c = getchar();
                 printf("0x%c%c, ", msb, lsb);
                 size ++;
             }