From 06e4a485c4e73c623ee276f53151be870c66ea4d Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 6 May 2019 11:40:57 +0200 Subject: [PATCH] More tests --- .../crypto_core/salsa/ref/core_salsa_ref.c | 4 +- .../crypto_pwhash/argon2/argon2-core.c | 4 +- src/libsodium/sodium/runtime.c | 2 + test/default/box_seal.c | 58 +++++++++++++++++-- test/default/box_seal.exp | 4 ++ 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/libsodium/crypto_core/salsa/ref/core_salsa_ref.c b/src/libsodium/crypto_core/salsa/ref/core_salsa_ref.c index a077d7f4..c023378c 100644 --- a/src/libsodium/crypto_core/salsa/ref/core_salsa_ref.c +++ b/src/libsodium/crypto_core/salsa/ref/core_salsa_ref.c @@ -127,7 +127,7 @@ crypto_core_salsa20_constbytes(void) } #ifndef MINIMAL - +/* LCOV_EXCL_START */ int crypto_core_salsa2012(unsigned char *out, const unsigned char *in, const unsigned char *k, const unsigned char *c) @@ -191,5 +191,5 @@ crypto_core_salsa208_constbytes(void) { return crypto_core_salsa208_CONSTBYTES; } - +/* LCOV_EXCL_END */ #endif diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-core.c b/src/libsodium/crypto_pwhash/argon2/argon2-core.c index bfe3fbbf..c4e4d842 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-core.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-core.c @@ -113,9 +113,11 @@ allocate_memory(block_region **region, uint32_t m_cost) } #endif if (base == NULL) { + /* LCOV_EXCL_START */ free(*region); *region = NULL; - return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */ + return ARGON2_MEMORY_ALLOCATION_ERROR; + /* LCOV_EXCL_STOP */ } (*region)->base = base; (*region)->memory = memory; diff --git a/src/libsodium/sodium/runtime.c b/src/libsodium/sodium/runtime.c index a5a89bca..9dfe54f8 100644 --- a/src/libsodium/sodium/runtime.c +++ b/src/libsodium/sodium/runtime.c @@ -203,11 +203,13 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) unsigned int cpu_info7[4]; _cpuid(cpu_info7, 0x00000007); + /* LCOV_EXCL_START */ if ((cpu_info7[1] & CPUID_EBX_AVX512F) == CPUID_EBX_AVX512F && (xcr0 & (XCR0_OPMASK | XCR0_ZMM_HI256 | XCR0_HI16_ZMM)) == (XCR0_OPMASK | XCR0_ZMM_HI256 | XCR0_HI16_ZMM)) { cpu_features->has_avx512f = 1; } + /* LCOV_EXCL_STOP */ } #endif diff --git a/test/default/box_seal.c b/test/default/box_seal.c index f9c970bc..e19ddd62 100644 --- a/test/default/box_seal.c +++ b/test/default/box_seal.c @@ -2,8 +2,8 @@ #define TEST_NAME "box_seal" #include "cmptest.h" -int -main(void) +static +void tv1(void) { unsigned char pk[crypto_box_PUBLICKEYBYTES]; unsigned char sk[crypto_box_SECRETKEYBYTES]; @@ -22,11 +22,11 @@ main(void) randombytes_buf(m, m_len); if (crypto_box_seal(c, m, m_len, pk) != 0) { printf("crypto_box_seal() failure\n"); - return 1; + return; } if (crypto_box_seal_open(m2, c, c_len, pk, sk) != 0) { printf("crypto_box_seal_open() failure\n"); - return 1; + return; } printf("%d\n", memcmp(m, m2, m_len)); @@ -39,6 +39,56 @@ main(void) sodium_free(m2); assert(crypto_box_sealbytes() == crypto_box_SEALBYTES); +} +#ifndef SODIUM_LIBRARY_MINIMAL +static +void tv2(void) +{ + unsigned char pk[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES]; + unsigned char sk[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES]; + unsigned char *c; + unsigned char *m; + unsigned char *m2; + size_t m_len; + size_t c_len; + + crypto_box_curve25519xchacha20poly1305_keypair(pk, sk); + m_len = (size_t) randombytes_uniform(1000); + c_len = crypto_box_curve25519xchacha20poly1305_SEALBYTES + m_len; + m = (unsigned char *) sodium_malloc(m_len); + m2 = (unsigned char *) sodium_malloc(m_len); + c = (unsigned char *) sodium_malloc(c_len); + randombytes_buf(m, m_len); + if (crypto_box_curve25519xchacha20poly1305_seal(c, m, m_len, pk) != 0) { + printf("crypto_box_curve25519xchacha20poly1305_seal() failure\n"); + return; + } + if (crypto_box_curve25519xchacha20poly1305_seal_open(m2, c, c_len, pk, sk) != 0) { + printf("crypto_box_curve25519xchacha20poly1305_seal_open() failure\n"); + return; + } + printf("%d\n", memcmp(m, m2, m_len)); + + printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, 0U, pk, sk)); + printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, c_len - 1U, pk, sk)); + printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, c_len, sk, pk)); + + sodium_free(c); + sodium_free(m); + sodium_free(m2); + + assert(crypto_box_curve25519xchacha20poly1305_sealbytes() == + crypto_box_curve25519xchacha20poly1305_SEALBYTES); +} +#endif + +int +main(void) +{ + tv1(); +#ifndef SODIUM_LIBRARY_MINIMAL + tv2(); +#endif return 0; } diff --git a/test/default/box_seal.exp b/test/default/box_seal.exp index 78ea705a..ded7a43c 100644 --- a/test/default/box_seal.exp +++ b/test/default/box_seal.exp @@ -2,3 +2,7 @@ -1 -1 -1 +0 +-1 +-1 +-1