]> git.codecow.com Git - Monocypher.git/commitdiff
added test vector generators
authorLoup Vaillant <loup@loup-vaillant.fr>
Tue, 5 Sep 2017 18:06:28 +0000 (20:06 +0200)
committerLoup Vaillant <loup@loup-vaillant.fr>
Wed, 6 Sep 2017 06:53:41 +0000 (08:53 +0200)
tests/gen/argon2i.c [new file with mode: 0644]
tests/gen/blake2b.c [new file with mode: 0644]
tests/gen/chacha20.c [new file with mode: 0644]
tests/gen/edDSA.c [new file with mode: 0644]
tests/gen/makefile [new file with mode: 0644]
tests/gen/poly1305.c [new file with mode: 0644]
tests/gen/sha512.c [new file with mode: 0644]
tests/gen/x25519.c [new file with mode: 0644]
tests/gen/xchacha20.c [new file with mode: 0644]

diff --git a/tests/gen/argon2i.c b/tests/gen/argon2i.c
new file mode 100644 (file)
index 0000000..ffdf115
--- /dev/null
@@ -0,0 +1,31 @@
+#include <sodium.h>
+#include "utils.h"
+
+void test(size_t nb_blocks, size_t hash_size)
+{
+    const size_t salt_size = crypto_pwhash_SALTBYTES;
+    RANDOM_INPUT(password, 16       );
+    RANDOM_INPUT(salt    , salt_size);
+    u8 hash[256];
+
+    if (crypto_pwhash(hash, hash_size, (char*)password, 16, salt,
+                      3, nb_blocks * 1024, crypto_pwhash_ALG_DEFAULT)) {
+        fprintf(stderr, "Argon2i failed.  nb_blocks = %lu, hash_size =%lu\n",
+                nb_blocks, hash_size);
+        printf(":deadbeef:\n"); // prints a canary to fail subsequent tests
+    }
+
+    print_vector(password, 16);
+    print_vector(salt, salt_size);
+    print_number(nb_blocks);
+    print_vector(hash, hash_size);
+    printf("\n");
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    FOR (nb_blocks,  8, 1024) { test(nb_blocks, 32       ); }
+    FOR (hash_size, 16,  256) { test(8        , hash_size); }
+    return 0;
+}
diff --git a/tests/gen/blake2b.c b/tests/gen/blake2b.c
new file mode 100644 (file)
index 0000000..6565b72
--- /dev/null
@@ -0,0 +1,25 @@
+#include <sodium.h>
+#include "utils.h"
+
+void test(size_t size, size_t key_size, size_t hash_size)
+{
+    RANDOM_INPUT(in  , 256);
+    RANDOM_INPUT(key , 32);
+    u8 hash[64];
+
+    crypto_generichash(hash, hash_size, in, size, key, key_size);
+
+    print_vector(in , size);
+    print_vector(key, key_size);
+    print_vector(hash, hash_size);
+    printf("\n");
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    FOR(size     , 0, 256) { test(size, 0       , 64       ); }
+    FOR(key_size , 0,  32) { test(128 , key_size, 64       ); }
+    FOR(hash_size, 1,  64) { test(128 , 0       , hash_size); }
+    return 0;
+}
diff --git a/tests/gen/chacha20.c b/tests/gen/chacha20.c
new file mode 100644 (file)
index 0000000..96b4c81
--- /dev/null
@@ -0,0 +1,31 @@
+#include <sodium.h>
+#include "utils.h"
+
+static void test(size_t size, u64 ctr)
+{
+    RANDOM_INPUT(key  ,  32);
+    RANDOM_INPUT(nonce,   8);
+    RANDOM_INPUT(in   , 256); // size <= 256
+    u8 out  [256];            // size <= 256
+
+    crypto_stream_chacha20_xor_ic(out, in, size, nonce, ctr, key);
+
+    print_vector(key   ,   32);
+    print_vector(nonce ,    8);
+    print_vector(in    , size);
+    print_number(ctr         );
+    print_vector(out, size);
+    printf("\n");
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    // regular tests
+    FOR (size, 0, 256) { test(size, rand64()); }
+    // counter overflow (should wrap around)
+    test(256, -1);
+    test(256, -2);
+    test(256, -3);
+    return 0;
+}
diff --git a/tests/gen/edDSA.c b/tests/gen/edDSA.c
new file mode 100644 (file)
index 0000000..32ddd97
--- /dev/null
@@ -0,0 +1,24 @@
+#include <sodium.h>
+#include "utils.h"
+#include "ed25519.h"
+
+void test(size_t size)
+{
+    RANDOM_INPUT(sk ,  32);
+    RANDOM_INPUT(msg, 256);
+    u8 pk[32], sig[64];
+
+    ed25519_publickey(sk, pk);
+    ed25519_sign(msg, size, sk, pk, sig);
+
+    print_vector(sk , 32  );
+    print_vector(msg, size);
+    print_vector(sig, 64  );
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    FOR (i, 0, 256) { test(i); }
+    return 0;
+}
diff --git a/tests/gen/makefile b/tests/gen/makefile
new file mode 100644 (file)
index 0000000..e953a69
--- /dev/null
@@ -0,0 +1,52 @@
+CC=gcc -std=c99
+#CC = clang -std=c99 -fsanitize=address
+#CC = clang -std=c99 -fsanitize=memory
+#CC = clang -std=c99 -fsanitize=undefined
+CFLAGS= -pedantic -Wall -Wextra -O3 -march=native
+HASH=
+#HASH=-DED25519_SHA512
+
+.PHONY: all clean
+
+all: chacha20.vec \
+     xchacha20.vec \
+     poly1305.vec \
+     blake2b.vec \
+     sha512.vec \
+     argon2i.vec \
+     edDSA.vec \
+     x25519.vec
+
+clean:
+       rm -f *.out
+       rm -f *.vec
+       rm -f *.o
+
+%.vec: %.out
+       ./$< > $@
+
+%.out: %.c donna.o ../utils.c monocypher.o
+       $(CC) $(CFLAGS) -o $@ $^              \
+            -I ../                            \
+            -I ../ed25519-donna               \
+            -I ../../src                      \
+            $$(pkg-config --cflags libsodium) \
+            $$(pkg-config --libs   libsodium)
+
+# Needed for EdDSA
+donna.o: ../ed25519-donna/ed25519.c
+       $(CC) $(CFLAGS) -o $@ -c $^ \
+            -I ../../src            \
+            $(HASH)                 \
+            -DED25519_CUSTOMHASH    \
+            -DED25519_TEST          \
+            -DED25519_NO_INLINE_ASM \
+            -DED25519_FORCE_32BIT
+
+# Needed for Blake2b in EdDSA
+monocypher.o: ../../src/monocypher.c
+       $(CC) $(CFLAGS) -o $@ -c $^ \
+            -I ../../src
+
+# Needed for Ed25519
+sha512.o: ../../src/monocypher.c
diff --git a/tests/gen/poly1305.c b/tests/gen/poly1305.c
new file mode 100644 (file)
index 0000000..ab41886
--- /dev/null
@@ -0,0 +1,23 @@
+#include <sodium.h>
+#include "utils.h"
+
+void test(size_t size)
+{
+    RANDOM_INPUT(key,  32);
+    RANDOM_INPUT(in , 256);
+    u8 tag[ 16];
+
+    crypto_onetimeauth(tag, in, size, key);
+
+    print_vector(key, 32);
+    print_vector(in , 32);
+    print_vector(tag, 16);
+    printf("\n");
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    FOR (size, 0, 256) { test(size); }
+    return 0;
+}
diff --git a/tests/gen/sha512.c b/tests/gen/sha512.c
new file mode 100644 (file)
index 0000000..72fb194
--- /dev/null
@@ -0,0 +1,22 @@
+#include <sodium.h>
+#include "utils.h"
+
+int main(void)
+{
+    if (sodium_init() == -1) {
+        printf("Libsodium init failed.  Abort.  No test performed\n");
+        return 1;
+    }
+
+    u8  key[32], nonce[8], in[256], sodium[256];
+    FOR (size, 0, 256) {
+        FOR(i, 0, 10) {
+            p_random(key  ,   32);  print_vector(key  ,   32);
+            p_random(nonce,    8);  print_vector(nonce,    8);
+            p_random(in   , size);  print_vector(in   , size);
+            u64 ctr = rand64();
+            crypto_stream_chacha20_xor_ic(sodium, in, size, nonce, ctr, key);
+        }
+    }
+    return 0;
+}
diff --git a/tests/gen/x25519.c b/tests/gen/x25519.c
new file mode 100644 (file)
index 0000000..a44c002
--- /dev/null
@@ -0,0 +1,32 @@
+#include <sodium.h>
+#include "utils.h"
+
+void test()
+{
+    RANDOM_INPUT(sk1, 32);
+    RANDOM_INPUT(sk2, 32);
+    u8 pk1[32], pk2[32], shared[32];
+
+    crypto_scalarmult_base(pk1, sk1);
+    crypto_scalarmult_base(pk2, sk2);
+    if (crypto_scalarmult(shared, sk1, pk2)) {
+        fprintf(stderr, "Libsodium rejected the public key\n");
+        printf(":deadbeef:\n"); // prints a canary to fail subsequent tests
+    }
+
+    print_vector(sk1   , 32);
+    print_vector(pk2   , 32);
+    print_vector(shared, 32);
+    printf("\n");
+    print_vector(sk2   , 32);
+    print_vector(pk1   , 32);
+    print_vector(shared, 32);
+    printf("\n");
+}
+
+int main(void)
+{
+    SODIUM_INIT;
+    FOR (i, 0, 100) { test(); }
+    return 0;
+}
diff --git a/tests/gen/xchacha20.c b/tests/gen/xchacha20.c
new file mode 100644 (file)
index 0000000..7a3a449
--- /dev/null
@@ -0,0 +1,2 @@
+#define crypto_stream_chacha20_xor_ic crypto_stream_xchacha20_xor_ic
+#include "chacha20.c"