]> git.codecow.com Git - Monocypher.git/commitdiff
test vectors for the test suite
authorLoup Vaillant <loup@loup-vaillant.fr>
Wed, 11 Jan 2017 17:46:10 +0000 (18:46 +0100)
committerLoup Vaillant <loup@loup-vaillant.fr>
Wed, 11 Jan 2017 17:46:10 +0000 (18:46 +0100)
test.c
vectors_test_diff.txt [new file with mode: 0644]
vectors_test_equal.txt [new file with mode: 0644]

diff --git a/test.c b/test.c
index 429cc1cc1930c56d9c68d0c9100c2a30219e98cd..c540535df723298ab9f3a0550a00436d36890842 100644 (file)
--- a/test.c
+++ b/test.c
@@ -94,6 +94,14 @@ vec_push_back(vector *v, uint8_t e)
     v->size++;
 }
 
+static int
+vec_cmp(const vector *u, const vector *v)
+{
+    if (u->size != v-> size)
+        return -1;
+    return memcmp(u->buffer, v->buffer, u->size);
+}
+
 // Read a line into a vector
 // Free the vector's memory with vec_del()
 static vector
@@ -113,6 +121,43 @@ read_hex_line(FILE *input_file)
     return v;
 }
 
+///////////////////////////
+/// Test the test suite ///
+///////////////////////////
+static int
+test_test_equal(char* filename)
+{
+    int   status = 0;
+    FILE *file   = file_open(filename);
+    while (getc(file) != EOF) {
+        vector a = read_hex_line(file);
+        vector b = read_hex_line(file);
+        status |= vec_cmp(&a, &b);
+        vec_del(&b);
+        vec_del(&a);
+    }
+    printf("%s: test_equal\n", status != 0 ? "FAILED" : "OK");
+    fclose(file);
+    return status;
+}
+
+static int
+test_test_diff(char* filename)
+{
+    int   status = 0;
+    FILE *file   = file_open(filename);
+    while (getc(file) != EOF) {
+        vector a = read_hex_line(file);
+        vector b = read_hex_line(file);
+        status |= !vec_cmp(&a, &b);
+        vec_del(&b);
+        vec_del(&a);
+    }
+    printf("%s: test_diff\n", status != 0 ? "FAILED" : "OK");
+    fclose(file);
+    return status;
+}
+
 ////////////////////////
 /// The tests proper ///
 ////////////////////////
@@ -130,7 +175,7 @@ test_chacha20(char* filename)
         crypto_chacha_ctx ctx;
         crypto_init_chacha20(&ctx, key.buffer, nonce.buffer);
         crypto_encrypt_chacha20(&ctx, 0, out.buffer, out.size);
-        status |= memcmp(out.buffer, stream.buffer, out.size);
+        status |= vec_cmp(&out, &stream);
 
         vec_del(&out);
         vec_del(&stream);
@@ -142,6 +187,36 @@ test_chacha20(char* filename)
     return status;
 }
 
+static int
+test_ietf_chacha20(char* filename)
+{
+    int   status = 0;
+    FILE *file   = file_open(filename);
+    while (getc(file) != EOF) {
+        vector key    = read_hex_line(file);
+        vector nonce  = read_hex_line(file);
+        vector plain  = read_hex_line(file);
+        vector cipher = read_hex_line(file);
+        vector out    = vec_uninitialized(plain.size);
+
+        uint8_t dump[64];
+        crypto_chacha_ctx ctx;
+        crypto_init_ietf_chacha20(&ctx, key.buffer, nonce.buffer);
+        crypto_encrypt_chacha20(&ctx, 0, dump, 64);
+        crypto_encrypt_chacha20(&ctx, plain.buffer, out.buffer, out.size);
+        status |= vec_cmp(&out, &cipher);
+
+        vec_del(&out);
+        vec_del(&cipher);
+        vec_del(&plain);
+        vec_del(&nonce);
+        vec_del(&key);
+    }
+    printf("%s: ietf_chacha20\n", status != 0 ? "FAILED" : "OK");
+    fclose(file);
+    return status;
+}
+
 static int
 test_blake2b(char* filename)
 {
@@ -157,7 +232,7 @@ test_blake2b(char* filename)
                                key.buffer, key .size,
                                in .buffer, in  .size);
 
-        status |= memcmp(out.buffer, hash.buffer, out.size);
+        status |= vec_cmp(&out, &hash);
 
         vec_del(&out);
         vec_del(&hash);
@@ -181,7 +256,7 @@ test_poly1305(char *filename)
         vector out = vec_uninitialized(tag.size);
 
         crypto_poly1305_auth(out.buffer, msg.buffer, msg.size, key.buffer);
-        status |= memcmp(out.buffer, tag.buffer, out.size);
+        status |= vec_cmp(&out, &tag);
 
         vec_del(&out);
         vec_del(&tag);
@@ -219,7 +294,7 @@ test_argon2i(char *filename)
                             nb_blocks    .buffer[0],
                             nb_iterations.buffer[0]);
 
-        status |= memcmp(out.buffer, tag.buffer, out.size);
+        status |= vec_cmp(&out, &tag);
 
         free(work_area);
         vec_del(&nb_blocks    );
@@ -239,9 +314,13 @@ test_argon2i(char *filename)
 int main(void)
 {
     int status = 0;
-    status |= test_chacha20("vectors_chacha20.txt");
-    status |= test_blake2b ("vectors_blake2b.txt" );
-    status |= test_poly1305("vectors_poly1305.txt");
-    status |= test_argon2i ("vectors_argon2i.txt" );
+    status |= test_test_equal   ("vectors_test_equal.txt"   );
+    status |= test_test_diff    ("vectors_test_diff.txt"    );
+    status |= test_chacha20     ("vectors_chacha20.txt"     );
+    status |= test_ietf_chacha20("vectors_ietf_chacha20.txt");
+    status |= test_blake2b      ("vectors_blake2b.txt"      );
+    status |= test_poly1305     ("vectors_poly1305.txt"     );
+    status |= test_argon2i      ("vectors_argon2i.txt"      );
+    printf(status ? "TESTS FAILED\n" : "ALL TESTS OK\n");
     return status;
 }
diff --git a/vectors_test_diff.txt b/vectors_test_diff.txt
new file mode 100644 (file)
index 0000000..e6437da
--- /dev/null
@@ -0,0 +1,26 @@
+a:     
+b:     00
+
+a:     00
+b:     
+
+a:     00
+b:     01
+
+a:     01
+b:     00
+
+a:     0000
+b:     000000
+
+a:     000000
+b:     0000
+
+a:     0012
+b:     0011
+
+a:     0011
+b:     0211
+
+a:     001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788001122334455667388
+b:     001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788
diff --git a/vectors_test_equal.txt b/vectors_test_equal.txt
new file mode 100644 (file)
index 0000000..a8618e8
--- /dev/null
@@ -0,0 +1,17 @@
+a:     
+b:     
+
+a:     00
+b:     00
+
+a:     0000
+b:     0000
+
+a:     0011
+b:     0011
+
+a:     001122
+b:     001122
+
+a:     001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788
+b:     001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788001122334455667788