]> git.codecow.com Git - Monocypher.git/commitdiff
more tests for crypto_chacha20_set_ctr()
authorLoup Vaillant <loup@loup-vaillant.fr>
Mon, 17 Jul 2017 16:15:02 +0000 (18:15 +0200)
committerLoup Vaillant <loup@loup-vaillant.fr>
Mon, 17 Jul 2017 16:15:02 +0000 (18:15 +0200)
tests/properties.c
tests/sodium.c

index d5ffdf5200bc487def3d000b2396190385594951..9803ca62630af986dc8366138eea9de55dd7b357 100644 (file)
@@ -26,21 +26,17 @@ void p_random(u8 *stream, size_t size)
     crypto_chacha20_stream(&ctx, stream, size);
 }
 
-static u32 load32_le(const u8 s[4])
+// Random 64 bit number
+u64 rand64()
 {
-    return (u32)s[0]
-        | ((u32)s[1] <<  8)
-        | ((u32)s[2] << 16)
-        | ((u32)s[3] << 24);
-}
-
-// Random number between 0 and max.
-// Unbalanced if max is not a power of 2.
-u32 rand_mod(u32 max)
-{
-    u8 n[4];
-    p_random(n, 4);
-    return load32_le(n) % max;
+    u8  tmp;
+    u64 result = 0;
+    FOR (i, 0, 8) {
+        p_random(&tmp, 1);
+        result <<= 8;
+        result  += tmp;
+    }
+    return result;
 }
 
 // Tests that encrypting in chunks yields the same result than
@@ -65,7 +61,7 @@ static int chacha20()
         crypto_chacha_ctx ctx;
         crypto_chacha20_init(&ctx, key, nonce);
         while (1) {
-            size_t chunk_size = rand_mod(c_max_size);
+            size_t chunk_size = rand64() % c_max_size;
             if (offset + chunk_size > input_size) { break; }
             u8 *out = output_chunk + offset;
             u8 *in  = input        + offset;
@@ -90,11 +86,12 @@ static int chacha20_set_ctr()
     static const size_t stream_size = block_size * nb_blocks;
     int status = 0;
     FOR (i, 0, 1000) {
-        u8 output_part[stream_size];
-        u8 output_all [stream_size];
+        u8 output_part[stream_size    ];
+        u8 output_all [stream_size    ];
+        u8 output_more[stream_size * 2];
         u8 key        [32];          p_random(key  , 32);
         u8 nonce      [8];           p_random(nonce, 8 );
-        size_t ctr   = rand_mod(nb_blocks);
+        size_t ctr   = rand64() % nb_blocks;
         size_t limit = ctr * block_size;
         // Encrypt all at once
         crypto_chacha_ctx ctx;
@@ -108,6 +105,16 @@ static int chacha20_set_ctr()
         crypto_chacha20_stream(&ctx, output_part, limit);
         // Compare the results (must be the same)
         status |= crypto_memcmp(output_part, output_all, stream_size);
+
+        // Encrypt before the begining
+        crypto_chacha20_set_ctr(&ctx, -ctr);
+        crypto_chacha20_stream(&ctx,
+                               output_more + stream_size - limit,
+                               stream_size + limit);
+        // Compare the results (must be the same)
+        status |= crypto_memcmp(output_more + stream_size,
+                                output_all,
+                                stream_size);
     }
     printf("%s: Chacha20 (set counter)\n", status != 0 ? "FAILED" : "OK");
     return status;
@@ -134,7 +141,7 @@ static int poly1305()
         crypto_poly1305_ctx ctx;
         crypto_poly1305_init(&ctx, key);
         while (1) {
-            size_t chunk_size = rand_mod(c_max_size);
+            size_t chunk_size = rand64() % c_max_size;
             if (offset + chunk_size > input_size) { break; }
             crypto_poly1305_update(&ctx, input + offset, chunk_size);
             offset += chunk_size;
@@ -173,7 +180,7 @@ static int blake2b()
         crypto_blake2b_ctx ctx;
         crypto_blake2b_init(&ctx);
         while (1) {
-            size_t chunk_size = rand_mod(c_max_size);
+            size_t chunk_size = rand64() % c_max_size;
             if (offset + chunk_size > input_size) { break; }
             crypto_blake2b_update(&ctx, input + offset, chunk_size);
             offset += chunk_size;
index d2819fac8b43c4f4c7264c90168e91c27ad16eb0..ce377ff0abede06e95dd68a0cfe362f2d0ceedbd 100644 (file)
@@ -6,7 +6,8 @@
 #include <sodium.h>
 
 #define FOR(i, start, end) for (size_t (i) = (start); (i) < (end); (i)++)
-typedef uint8_t u8;
+typedef uint8_t  u8;
+typedef uint64_t u64;
 
 // Deterministic "random" number generator, so we can make "random", yet
 // reproducible tests.  To change the random stream, change the seed.
@@ -23,6 +24,19 @@ void p_random(u8 *stream, u8 size)
     rename_chacha20_stream(&ctx, stream, size);
 }
 
+// Random 64 bit number
+u64 rand64()
+{
+    u8  tmp;
+    u64 result = 0;
+    FOR (i, 0, 8) {
+        p_random(&tmp, 1);
+        result <<= 8;
+        result  += tmp;
+    }
+    return result;
+}
+
 static int chacha20(void)
 {
     u8  key[32], nonce[8], in[256], mono[256], sodium[256];
@@ -31,10 +45,12 @@ static int chacha20(void)
         p_random(key, 32);
         p_random(nonce, 8);
         p_random(in, size);
+        u64 ctr = rand64();
         rename_chacha_ctx ctx;
-        rename_chacha20_init(&ctx, key, nonce);
+        rename_chacha20_init   (&ctx, key, nonce);
+        rename_chacha20_set_ctr(&ctx, ctr);
         rename_chacha20_encrypt(&ctx, mono, in, size);
-        crypto_stream_chacha20_xor(sodium, in, size, nonce, key);
+        crypto_stream_chacha20_xor_ic(sodium, in, size, nonce, ctr, key);
         status |= rename_memcmp(mono, sodium, size);
     }
     printf("%s: Chacha20\n", status != 0 ? "FAILED" : "OK");
@@ -49,10 +65,12 @@ static int xchacha20(void)
         p_random(key, 32);
         p_random(nonce, 24);
         p_random(in, size);
+        u64 ctr = rand64();
         rename_chacha_ctx ctx;
-        rename_chacha20_x_init(&ctx, key, nonce);
+        rename_chacha20_x_init (&ctx, key, nonce);
+        rename_chacha20_set_ctr(&ctx, ctr);
         rename_chacha20_encrypt(&ctx, mono, in, size);
-        crypto_stream_xchacha20_xor(sodium, in, size, nonce, key);
+        crypto_stream_xchacha20_xor_ic(sodium, in, size, nonce, ctr, key);
         status |= rename_memcmp(mono, sodium, size);
     }
     printf("%s: XChacha20\n", status != 0 ? "FAILED" : "OK");