]> git.codecow.com Git - Monocypher.git/commitdiff
Added tests for IETF Chacha20
authorLoup Vaillant <loup@loup-vaillant.fr>
Mon, 9 Dec 2019 10:49:34 +0000 (11:49 +0100)
committerLoup Vaillant <loup@loup-vaillant.fr>
Mon, 9 Dec 2019 10:49:34 +0000 (11:49 +0100)
tests/gen/ietf_chacha20.c [new file with mode: 0644]
tests/gen/makefile
tests/test.c
tests/utils.c
tests/utils.h

diff --git a/tests/gen/ietf_chacha20.c b/tests/gen/ietf_chacha20.c
new file mode 100644 (file)
index 0000000..7256020
--- /dev/null
@@ -0,0 +1,26 @@
+#include <sodium.h>
+#include "utils.h"
+
+static void test(size_t size, u32 ctr)
+{
+    RANDOM_INPUT(key  ,  32);
+    RANDOM_INPUT(nonce,  12);
+    RANDOM_INPUT(in   , 128); // size <= 128
+    u8 out[128];              // size <= 128
+
+    crypto_stream_chacha20_ietf_xor_ic(out, in, size, nonce, ctr, key);
+
+    print_vector(key  ,   32);
+    print_vector(nonce,   12);
+    print_vector(in   , size);
+    print_number(ctr        );
+    print_vector(out  , size);
+    printf("\n");
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    FOR (size, 0, 128) { test(size, (u32)rand64()); }
+    return 0;
+}
index d567cf3094354f0079490cd53193391e984c0cc5..d1f127de7eb9528076d55c545bc27cc8214e717e 100644 (file)
@@ -3,10 +3,10 @@ CFLAGS = -pedantic -Wall -Wextra
 
 .PHONY: all clean
 
-VEC     = chacha20    hchacha20  xchacha20     aead_ietf poly1305 \
-          blake2b     sha512     hmac_sha512   argon2i            \
-          edDSA       edDSA_pk   ed_25519      ed_25519_check     \
-          x25519      x25519_pk  key_exchange
+VEC     = chacha20  hchacha20  xchacha20    ietf_chacha20 aead_ietf  \
+          poly1305  blake2b    sha512       hmac_sha512   argon2i    \
+          edDSA     edDSA_pk   ed_25519     ed_25519_check           \
+          x25519    x25519_pk  key_exchange
 #          monokex_xk1 monokex_x
 VEC2    = $(patsubst %, %.all.vec, $(VEC))
 HEADERS = $(patsubst %, %.h.vec  , $(VEC))
@@ -56,6 +56,7 @@ x25519.all.vec        : x25519.vec      vectors/x25519
 x25519_pk.all.vec     : x25519_pk.vec
 hchacha20.all.vec     : hchacha20.vec
 xchacha20.all.vec     : xchacha20.vec
+ietf_chacha20.all.vec : ietf_chacha20.vec
 aead_ietf.all.vec     : aead_ietf.vec
 blake2b.all.vec       : blake2b.vec
 sha512.all.vec        : sha512.vec
index 3f28c233f0adfef3324e7b366a4706c9f951c7d9..fd53b29b991000d0682228acd5538b1a2c068a8b 100644 (file)
@@ -29,6 +29,20 @@ static void chacha20(const vector in[], vector *out)
     }
 }
 
+static void ietf_chacha20(const vector in[], vector *out)
+{
+    const vector *key   = in;
+    const vector *nonce = in + 1;
+    const vector *plain = in + 2;
+    u32 ctr       = load32_le(in[3].buf);
+    u32 new_ctr   = crypto_ietf_chacha20_ctr(out->buf, plain->buf, plain->size,
+                                             key->buf, nonce->buf, ctr);
+    u32 nb_blocks = plain->size / 64 + (plain->size % 64 != 0);
+    if (new_ctr - ctr != nb_blocks) {
+        printf("FAILURE: IETF Chacha20 returned counter not correct: ");
+    }
+}
+
 static void hchacha20(const vector in[], vector *out)
 {
     const vector *key   = in;
@@ -46,7 +60,7 @@ static void xchacha20(const vector in[], vector *out)
                                          key->buf, nonce->buf, ctr);
     u64 nb_blocks = plain->size / 64 + (plain->size % 64 != 0);
     if (new_ctr - ctr != nb_blocks) {
-        printf("FAILURE: Chacha20 returned counter not correct: ");
+        printf("FAILURE: XChacha20 returned counter not correct: ");
     }
 }
 
@@ -712,6 +726,7 @@ int main(int argc, char *argv[])
     printf("\nTest against vectors");
     printf("\n--------------------\n");
     status |= TEST(chacha20      , 4);
+    status |= TEST(ietf_chacha20 , 4);
     status |= TEST(hchacha20     , 2);
     status |= TEST(xchacha20     , 4);
     status |= TEST(poly1305      , 2);
index fc71ade3384255c6c62a63b973bd52d6f236e26f..ddc4d7b2ab4feaf9168581f8fffd868b3b8e598e 100644 (file)
@@ -16,16 +16,17 @@ void store64_le(u8 out[8], u64 in)
     out[7] = (in >> 56) & 0xff;
 }
 
-u64 load64_le(const u8 s[8])
+u32 load32_le(const u8 s[4])
 {
     return (u64)s[0]
         | ((u64)s[1] <<  8)
         | ((u64)s[2] << 16)
-        | ((u64)s[3] << 24)
-        | ((u64)s[4] << 32)
-        | ((u64)s[5] << 40)
-        | ((u64)s[6] << 48)
-        | ((u64)s[7] << 56);
+        | ((u64)s[3] << 24);
+}
+
+u64 load64_le(const u8 s[8])
+{
+    return load32_le(s) | ((u64)load32_le(s+4) << 32);
 }
 
 // Must be seeded with a nonzero value.
index c3151519ce2067e55179ea65d78af088db6bfe68..380519bbd10a28451cfc40d1d003207ec64b237d 100644 (file)
@@ -30,6 +30,7 @@ typedef struct {
 
 void store64_le(u8 out[8], u64 in);
 u64  load64_le(const u8 s[8]);
+u32  load32_le(const u8 s[8]);
 u64 rand64(); // Pseudo-random 64 bit number, based on xorshift*
 void p_random(u8 *stream, size_t size);
 void print_vector(const u8 *buf, size_t size);