]> git.codecow.com Git - Monocypher.git/commitdiff
Use ASSERT everywhere
authorLoup Vaillant <loup@loup-vaillant.fr>
Wed, 28 Dec 2022 07:50:25 +0000 (08:50 +0100)
committerLoup Vaillant <loup@loup-vaillant.fr>
Wed, 28 Dec 2022 08:18:02 +0000 (09:18 +0100)
tests/test.c
tests/tis-ci.c
tests/utils.c

index 53c578c1544340e3180314dd14fd2a9f8729ec98..bce5d50d5a746b805f2eb5aa676092334bcdc2cb 100644 (file)
@@ -115,10 +115,7 @@ static void chacha20(vector_reader *reader)
        u64    nb_blocks = plain.size / 64 + (plain.size % 64 != 0);
        u64    new_ctr   = crypto_chacha20_ctr(out.buf, plain.buf, plain.size,
                                               key.buf, nonce.buf, ctr);
-       if (new_ctr - ctr != nb_blocks) {
-               printf("FAILURE: Chacha20 returned counter not correct: ");
-               exit(1);
-       }
+       ASSERT(new_ctr - ctr == nb_blocks);
 }
 
 static void ietf_chacha20(vector_reader *reader)
@@ -131,10 +128,7 @@ static void ietf_chacha20(vector_reader *reader)
        u32    nb_blocks = (u32)(plain.size / 64 + (plain.size % 64 != 0));
        u32    new_ctr   = crypto_ietf_chacha20_ctr(out.buf, plain.buf, plain.size,
                                                    key.buf, nonce.buf, ctr);
-       if (new_ctr - ctr != nb_blocks) {
-               printf("FAILURE: IETF Chacha20 returned counter not correct: ");
-               exit(1);
-       }
+       ASSERT(new_ctr - ctr == nb_blocks);
 }
 
 static void xchacha20(vector_reader *reader)
@@ -147,10 +141,7 @@ static void xchacha20(vector_reader *reader)
        u64    nb_blocks = plain.size / 64 + (plain.size % 64 != 0);
        u64    new_ctr   = crypto_xchacha20_ctr(out.buf, plain.buf, plain.size,
                                                key.buf, nonce.buf, ctr);
-       if (new_ctr - ctr != nb_blocks) {
-               printf("FAILURE: XChacha20 returned counter not correct: ");
-               exit(1);
-       }
+       ASSERT(new_ctr - ctr == nb_blocks);
 }
 
 static void hchacha20(vector_reader *reader)
@@ -754,18 +745,9 @@ static void edDSA_pk(vector_reader *reader)
        memcpy(out.buf, public_key, 32);
 
        u8 zeroes[32] = {0};
-       if (memcmp(seed, zeroes, 32)) {
-               printf("FAILURE: seed has not been wiped\n");
-               exit(1);
-       }
-       if (memcmp(secret_key, in.buf, 32)) {
-               printf("FAILURE: first half of secret key is not the seed\n");
-               exit(1);
-       }
-       if (memcmp(secret_key + 32, public_key, 32)) {
-               printf("FAILURE: second half of secret key is not the public key\n");
-               exit(1);
-       }
+       ASSERT(memcmp(seed           , zeroes    , 32) == 0);
+       ASSERT(memcmp(secret_key     , in.buf    , 32) == 0);
+       ASSERT(memcmp(secret_key + 32, public_key, 32) == 0);
 }
 
 static void test_edDSA()
@@ -864,18 +846,9 @@ static void ed_25519_pk(vector_reader *reader)
        memcpy(out.buf, public_key, 32);
 
        u8 zeroes[32] = {0};
-       if (memcmp(seed, zeroes, 32)) {
-               printf("FAILURE: seed has not been wiped\n");
-               exit(1);
-       }
-       if (memcmp(secret_key, in.buf, 32)) {
-               printf("FAILURE: first half of secret key is not the seed\n");
-               exit(1);
-       }
-       if (memcmp(secret_key + 32, public_key, 32)) {
-               printf("FAILURE: second half of secret key is not the public key\n");
-               exit(1);
-       }
+       ASSERT(memcmp(seed           , zeroes    , 32) == 0);
+       ASSERT(memcmp(secret_key     , in.buf    , 32) == 0);
+       ASSERT(memcmp(secret_key + 32, public_key, 32) == 0);
 }
 
 static void ed_25519_check(vector_reader *reader)
@@ -912,10 +885,7 @@ static void elligator_inv(vector_reader *reader)
        u8     failure = next_input(reader).buf[0];
        vector out     = next_output(reader);
        int    check   = crypto_curve_to_hidden(out.buf, point.buf, tweak);
-       if ((u8)check != failure) {
-               printf("Elligator inverse map: failure mismatch\n");
-               exit(1);
-       }
+       ASSERT((u8)check == failure);
 }
 
 static void test_elligator()
index ce9dc94829b6439f643ee663c95250ba4fb8330c..053d6c2d9c03a3e0af2362bc6ae278b8a737c412 100644 (file)
@@ -239,10 +239,7 @@ static void elligator_inv(vector_reader *reader)
        u8     failure = next_input(reader).buf[0];
        vector out     = next_output(reader);
        int    check   = crypto_curve_to_hidden(out.buf, point.buf, tweak);
-       if ((u8)check != failure) {
-               printf("Elligator inverse map: failure mismatch\n");
-               exit(1);
-       }
+       ASSERT((u8)check == failure);
        if (check) {
                out.buf[0] = 0;
        }
@@ -410,30 +407,29 @@ TEST(elligator_inv)
 
 //@ ensures \result == 0;
 int main(void) {
-       int status = 0;
-       status |= v_chacha20      ();
-       status |= v_ietf_chacha20 ();
-       status |= v_hchacha20     ();
-       status |= v_xchacha20     ();
-       status |= v_poly1305      ();
-       status |= v_aead_ietf     ();
-       status |= v_blake2b       ();
-       status |= v_sha512        ();
-       status |= v_hmac_sha512   ();
-       status |= v_argon2i       ();
-       status |= v_x25519        ();
-       status |= v_edDSA         ();
-       status |= v_ed_25519      ();
-       status |= v_ed_25519_check();
-       status |= v_elligator_dir ();
-       status |= v_elligator_inv ();
-
-       status |= p_from_eddsa    ();
-       status |= p_from_ed25519  ();
-       status |= p_dirty         ();
-       status |= p_x25519_inverse();
-       status |= p_verify16      ();
-       status |= p_verify32      ();
-       status |= p_verify64      ();
-       return status;
+       ASSERT(v_chacha20      () == 0);
+       ASSERT(v_ietf_chacha20 () == 0);
+       ASSERT(v_hchacha20     () == 0);
+       ASSERT(v_xchacha20     () == 0);
+       ASSERT(v_poly1305      () == 0);
+       ASSERT(v_aead_ietf     () == 0);
+       ASSERT(v_blake2b       () == 0);
+       ASSERT(v_sha512        () == 0);
+       ASSERT(v_hmac_sha512   () == 0);
+       ASSERT(v_argon2i       () == 0);
+       ASSERT(v_x25519        () == 0);
+       ASSERT(v_edDSA         () == 0);
+       ASSERT(v_ed_25519      () == 0);
+       ASSERT(v_ed_25519_check() == 0);
+       ASSERT(v_elligator_dir () == 0);
+       ASSERT(v_elligator_inv () == 0);
+
+       ASSERT(p_from_eddsa    () == 0);
+       ASSERT(p_from_ed25519  () == 0);
+       ASSERT(p_dirty         () == 0);
+       ASSERT(p_x25519_inverse() == 0);
+       ASSERT(p_verify16      () == 0);
+       ASSERT(p_verify32      () == 0);
+       ASSERT(p_verify64      () == 0);
+       return 0;
 }
index f7235f0cc0c562fb03be5dad10fca2edfdf7b312..e33ca993fd8ff966026b57322f4fa815bd3a5fcc 100644 (file)
@@ -125,10 +125,7 @@ void* alloc(size_t size)
                return NULL;
        }
        void *buf = malloc(size);
-       if (buf == NULL) {
-               fprintf(stderr, "Allocation failed: 0x%zx bytes\n", size);
-               exit(1);
-       }
+       ASSERT(buf != NULL);
        return buf;
 }
 
@@ -154,10 +151,9 @@ static vector vector_of_string(const char *s)
 
 vector next_input(vector_reader *reader)
 {
-       if (reader->size == 0 || reader->nb_inputs >= 10) {
-               fprintf(stderr, "Input reader overload (no more vectors)\n");
-               exit(1);
-       }
+       ASSERT(reader->size > 0);
+       ASSERT(reader->nb_inputs < 10);
+
        const char *next = *(reader->next);
        vector *input = reader->inputs + reader->nb_inputs;
        reader->next++;
@@ -169,10 +165,9 @@ vector next_input(vector_reader *reader)
 
 vector next_output(vector_reader *reader)
 {
-       if (reader->size == 0 || reader->nb_inputs >= 10) {
-               fprintf(stderr, "Input reader overload (no more vectors)\n");
-               exit(1);
-       }
+       ASSERT(reader->size > 0);
+       ASSERT(reader->nb_inputs < 10);
+
        const char *next = *(reader->next);
        reader->next++;
        reader->size--;