Add a couple more tests

This commit is contained in:
Frank Denis
2026-04-09 23:00:04 +02:00
parent b0d658a856
commit 8490eeb0d5
12 changed files with 347 additions and 1 deletions
+5
View File
@@ -109,6 +109,7 @@ test/default/browser
test/default/chacha20
test/default/codecs
test/default/codecs2
test/default/codecs3
test/default/core_ed25519
test/default/core_keccak1600
test/default/core_ristretto255
@@ -129,6 +130,7 @@ test/default/kdf
test/default/kdf_hkdf
test/default/kdf_hkdf2
test/default/kdf_hkdf3
test/default/kdf_hkdf4
test/default/keygen
test/default/kx
test/default/metamorphic
@@ -138,6 +140,7 @@ test/default/onetimeauth2
test/default/onetimeauth7
test/default/pwhash_argon2i
test/default/pwhash_argon2id
test/default/pwhash_argon2id2
test/default/pwhash_scrypt
test/default/pwhash_scrypt_ll
test/default/randombytes
@@ -163,12 +166,14 @@ test/default/sodium_core
test/default/sodium_utils
test/default/sodium_utils2
test/default/sodium_utils3
test/default/sodium_utils4
test/default/sodium_version
test/default/stream
test/default/stream2
test/default/stream3
test/default/stream4
test/default/stream5
test/default/stream6
test/default/verify1
test/default/xchacha20
test/default/xof_shake128
+47 -1
View File
@@ -29,6 +29,7 @@ EXTRA_DIST = \
chacha20.exp \
codecs.exp \
codecs2.exp \
codecs3.exp \
core_ed25519.exp \
core_keccak1600.exp \
core_ristretto255.exp \
@@ -49,6 +50,10 @@ EXTRA_DIST = \
kdf_hkdf.exp \
kdf_hkdf2.exp \
kdf_hkdf3.exp \
kdf_hkdf4.exp \
kem.exp \
kem_mlkem768.exp \
kem_xwing.exp \
keygen.exp \
kx.exp \
metamorphic.exp \
@@ -58,6 +63,7 @@ EXTRA_DIST = \
onetimeauth7.exp \
pwhash_argon2i.exp \
pwhash_argon2id.exp \
pwhash_argon2id2.exp \
pwhash_scrypt.exp \
pwhash_scrypt_ll.exp \
randombytes.exp \
@@ -83,12 +89,14 @@ EXTRA_DIST = \
sodium_utils.exp \
sodium_utils2.exp \
sodium_utils3.exp \
sodium_utils4.exp \
sodium_version.exp \
stream.exp \
stream2.exp \
stream3.exp \
stream4.exp \
stream5.exp \
stream6.exp \
verify1.exp \
xchacha20.exp \
xof_shake128.exp \
@@ -122,6 +130,7 @@ DISTCLEANFILES = \
chacha20.res \
codecs.res \
codecs2.res \
codecs3.res \
core_ed25519.res \
core_keccak1600.res \
core_ristretto255.res \
@@ -143,6 +152,10 @@ DISTCLEANFILES = \
kdf_hkdf.res \
kdf_hkdf2.res \
kdf_hkdf3.res \
kdf_hkdf4.res \
kem.res \
kem_mlkem768.res \
kem_xwing.res \
keygen.res \
kx.res \
metamorphic.res \
@@ -152,6 +165,7 @@ DISTCLEANFILES = \
onetimeauth7.res \
pwhash_argon2i.res \
pwhash_argon2id.res \
pwhash_argon2id2.res \
pwhash_scrypt.res \
pwhash_scrypt_ll.res \
randombytes.res \
@@ -177,12 +191,14 @@ DISTCLEANFILES = \
sodium_utils.res \
sodium_utils2.res \
sodium_utils3.res \
sodium_utils4.res \
sodium_version.res \
stream.res \
stream2.res \
stream3.res \
stream4.res \
stream5.res \
stream6.res \
verify1.res \
xchacha20.res \
xof_shake128.res \
@@ -226,6 +242,7 @@ TESTS_TARGETS = \
chacha20 \
codecs \
codecs2 \
codecs3 \
core_keccak1600 \
core1 \
core2 \
@@ -250,6 +267,7 @@ TESTS_TARGETS = \
onetimeauth7 \
pwhash_argon2i \
pwhash_argon2id \
pwhash_argon2id2 \
randombytes \
scalarmult \
scalarmult2 \
@@ -283,7 +301,8 @@ TESTS_TARGETS = \
if !EMSCRIPTEN
TESTS_TARGETS += \
sodium_utils2 \
sodium_utils3
sodium_utils3 \
sodium_utils4
endif
check_PROGRAMS = $(TESTS_TARGETS)
@@ -365,6 +384,9 @@ codecs_LDADD = $(TESTS_LDADD)
codecs2_SOURCE = cmptest.h codecs2.c
codecs2_LDADD = $(TESTS_LDADD)
codecs3_SOURCE = cmptest.h codecs3.c
codecs3_LDADD = $(TESTS_LDADD)
core_ed25519_SOURCE = cmptest.h core_ed25519.c
core_ed25519_LDADD = $(TESTS_LDADD)
@@ -425,6 +447,19 @@ kdf_hkdf2_LDADD = $(TESTS_LDADD)
kdf_hkdf3_SOURCE = cmptest.h kdf_hkdf3.c
kdf_hkdf3_LDADD = $(TESTS_LDADD)
kdf_hkdf4_SOURCE = cmptest.h kdf_hkdf4.c
kdf_hkdf4_LDADD = $(TESTS_LDADD)
kem_SOURCE = cmptest.h kem.c
kem_LDADD = $(TESTS_LDADD)
kem_mlkem768_SOURCE = cmptest.h kem_mlkem768.c
kem_mlkem768_LDADD = $(TESTS_LDADD)
kem_xwing_SOURCE = cmptest.h kem_xwing.c
kem_xwing_LDADD = $(TESTS_LDADD)
keygen_SOURCE = cmptest.h keygen.c
keygen_LDADD = $(TESTS_LDADD)
@@ -452,6 +487,9 @@ pwhash_argon2i_LDADD = $(TESTS_LDADD)
pwhash_argon2id_SOURCE = cmptest.h pwhash_argon2id.c
pwhash_argon2id_LDADD = $(TESTS_LDADD)
pwhash_argon2id2_SOURCE = cmptest.h pwhash_argon2id2.c
pwhash_argon2id2_LDADD = $(TESTS_LDADD)
pwhash_scrypt_SOURCE = cmptest.h pwhash_scrypt.c
pwhash_scrypt_LDADD = $(TESTS_LDADD)
@@ -527,6 +565,9 @@ sodium_utils2_LDADD = $(TESTS_LDADD)
sodium_utils3_SOURCE = cmptest.h sodium_utils3.c
sodium_utils3_LDADD = $(TESTS_LDADD)
sodium_utils4_SOURCE = cmptest.h sodium_utils4.c
sodium_utils4_LDADD = $(TESTS_LDADD)
sodium_version_SOURCE = cmptest.h sodium_version.c
sodium_version_LDADD = $(TESTS_LDADD)
@@ -545,6 +586,9 @@ stream4_LDADD = $(TESTS_LDADD)
stream5_SOURCE = cmptest.h stream5.c
stream5_LDADD = $(TESTS_LDADD)
stream6_SOURCE = cmptest.h stream6.c
stream6_LDADD = $(TESTS_LDADD)
verify1_SOURCE = cmptest.h verify1.c
verify1_LDADD = $(TESTS_LDADD)
@@ -573,8 +617,10 @@ TESTS_TARGETS += \
kdf_hkdf \
kdf_hkdf2 \
kdf_hkdf3 \
kdf_hkdf4 \
pwhash_scrypt \
stream5 \
stream6 \
pwhash_scrypt_ll \
scalarmult_ed25519 \
scalarmult_ristretto255 \
+56
View File
@@ -0,0 +1,56 @@
#define TEST_NAME "codecs3"
#include "cmptest.h"
int
main(void)
{
unsigned char ip_bytes[16];
assert(sodium_ip2bin(ip_bytes, "::1:2:3:4:5:6:7:8",
strlen("::1:2:3:4:5:6:7:8")) == -1);
printf("ipv6 double-colon full address: rejected\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7",
strlen("1:2:3:4:5:6:7")) == -1);
printf("ipv6 too few groups: rejected\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7:8:9",
strlen("1:2:3:4:5:6:7:8:9")) == -1);
printf("ipv6 too many groups: rejected\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7:8:a",
strlen("1:2:3:4:5:6:7:8:a")) == -1);
printf("ipv6 too many groups at end: rejected\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7:192.168.1.1",
strlen("1:2:3:4:5:6:7:192.168.1.1")) == -1);
printf("ipv6 embedded IPv4 too many groups: rejected\n");
assert(sodium_ip2bin(ip_bytes, "::999.1.1.1",
strlen("::999.1.1.1")) == -1);
printf("ipv6 embedded IPv4 invalid: rejected\n");
assert(sodium_ip2bin(ip_bytes, "::192.168.1.1",
strlen("::192.168.1.1")) == 0);
printf("ipv6 embedded IPv4 valid: ok\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7:8:",
strlen("1:2:3:4:5:6:7:8:")) == -1);
printf("ipv6 trailing colon: rejected\n");
assert(sodium_ip2bin(ip_bytes, ":1:2:3:4:5:6:7:8",
strlen(":1:2:3:4:5:6:7:8")) == -1);
printf("ipv6 single leading colon: rejected\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7:8",
strlen("1:2:3:4:5:6:7:8")) == 0);
assert(ip_bytes[0] == 0 && ip_bytes[1] == 1);
assert(ip_bytes[14] == 0 && ip_bytes[15] == 8);
printf("ipv6 full 8 groups: ok\n");
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:1.2.3.4",
strlen("1:2:3:4:5:6:1.2.3.4")) == 0);
printf("ipv6 embedded IPv4 6 groups: ok\n");
return 0;
}
+11
View File
@@ -0,0 +1,11 @@
ipv6 double-colon full address: rejected
ipv6 too few groups: rejected
ipv6 too many groups: rejected
ipv6 too many groups at end: rejected
ipv6 embedded IPv4 too many groups: rejected
ipv6 embedded IPv4 invalid: rejected
ipv6 embedded IPv4 valid: ok
ipv6 trailing colon: rejected
ipv6 single leading colon: rejected
ipv6 full 8 groups: ok
ipv6 embedded IPv4 6 groups: ok
+42
View File
@@ -0,0 +1,42 @@
#define TEST_NAME "kdf_hkdf4"
#include "cmptest.h"
int
main(void)
{
unsigned char prk256[crypto_kdf_hkdf_sha256_KEYBYTES];
unsigned char prk512[crypto_kdf_hkdf_sha512_KEYBYTES];
unsigned char out256[256];
unsigned char out512[256];
char hex[65];
crypto_kdf_hkdf_sha256_extract(prk256, NULL, 0,
(const unsigned char *) "input key material", 18);
assert(crypto_kdf_hkdf_sha256_expand(out256, 100, "context", 7, prk256) == 0);
sodium_bin2hex(hex, sizeof hex, out256, 32);
printf("sha256 expand 100 first 32: %s\n", hex);
sodium_bin2hex(hex, sizeof hex, out256 + 64, 32);
printf("sha256 expand 100 bytes 64-95: %s\n", hex);
crypto_kdf_hkdf_sha512_extract(prk512, NULL, 0,
(const unsigned char *) "input key material", 18);
assert(crypto_kdf_hkdf_sha512_expand(out512, 200, "context", 7, prk512) == 0);
sodium_bin2hex(hex, sizeof hex, out512, 32);
printf("sha512 expand 200 first 32: %s\n", hex);
sodium_bin2hex(hex, sizeof hex, out512 + 128, 32);
printf("sha512 expand 200 bytes 128-159: %s\n", hex);
assert(crypto_kdf_hkdf_sha512_expand(out512, 128, "ctx2", 4, prk512) == 0);
sodium_bin2hex(hex, sizeof hex, out512 + 64, 32);
printf("sha512 expand 128 second block: %s\n", hex);
assert(crypto_kdf_hkdf_sha512_expand(out512, 64, "ctx3", 4, prk512) == 0);
sodium_bin2hex(hex, sizeof hex, out512, 32);
printf("sha512 expand 64: %s\n", hex);
printf("ok\n");
return 0;
}
+7
View File
@@ -0,0 +1,7 @@
sha256 expand 100 first 32: 6c7c112b81dce760ab3e69dc3d908046a8b0eaa96cf0d0193196ddc422190535
sha256 expand 100 bytes 64-95: 242987e3b48af0960bd7fc2f436ce4017f7f5706179b7063204347b1581be20a
sha512 expand 200 first 32: f8f7a3d9d607725ce796b192216c4eb3cb95fc91b946eeaa9cc5d60998123b9a
sha512 expand 200 bytes 128-159: 521f83b9f328f046a1358214abf716e356987c02dceb04ee139f7c120a5c8c55
sha512 expand 128 second block: 5512dbbaf49f8f4351de113f123c8cd2dc93b67b4fd7c1e46d9e89a682489930
sha512 expand 64: b31b0743dc48859226e25ee25337c18a6dbf21b1ec8124b11ac282be30e934b2
ok
+56
View File
@@ -0,0 +1,56 @@
#define TEST_NAME "pwhash_argon2id2"
#include "cmptest.h"
int
main(void)
{
char str_out[crypto_pwhash_STRBYTES];
unsigned char out[32];
int ret;
ret = crypto_pwhash_argon2id_str(str_out, "test", 4, 3, 5000000);
if (ret != 0) {
printf("pwhash_argon2id_str failed\n");
return 1;
}
printf("hash created: ok\n");
assert(crypto_pwhash_argon2id_str_verify(str_out, "test", 4) == 0);
printf("verify: ok\n");
#if SIZE_MAX > 0xFFFFFFFFULL
ret = crypto_pwhash_argon2id_str_needs_rehash(str_out,
(unsigned long long) 0x100000000ULL,
5000000);
assert(ret == -1);
printf("needs_rehash opslimit overflow: rejected\n");
ret = crypto_pwhash_argon2id_str_needs_rehash(str_out, 3,
(size_t) 0x100000000ULL * 1024ULL);
assert(ret == -1);
printf("needs_rehash memlimit overflow: rejected\n");
#else
printf("needs_rehash opslimit overflow: rejected\n");
printf("needs_rehash memlimit overflow: rejected\n");
#endif
ret = crypto_pwhash_argon2id_str_needs_rehash(str_out, 3, 5000000);
assert(ret == 0);
printf("needs_rehash same params: ok\n");
ret = crypto_pwhash_argon2i_str_needs_rehash(str_out, 3, 5000000);
assert(ret == -1);
printf("argon2i needs_rehash on argon2id: rejected\n");
assert(crypto_pwhash_str_verify(str_out, "wrong", 5) == -1);
printf("verify wrong password: rejected\n");
ret = crypto_pwhash(out, sizeof out,
"test", 4,
(const unsigned char *) str_out + 30,
3, 5000000, crypto_pwhash_ALG_ARGON2I13);
assert(ret == 0);
printf("pwhash argon2i alg: ok\n");
return 0;
}
+8
View File
@@ -0,0 +1,8 @@
hash created: ok
verify: ok
needs_rehash opslimit overflow: rejected
needs_rehash memlimit overflow: rejected
needs_rehash same params: ok
argon2i needs_rehash on argon2id: rejected
verify wrong password: rejected
pwhash argon2i alg: ok
+51
View File
@@ -0,0 +1,51 @@
#define TEST_NAME "sodium_utils4"
#include "cmptest.h"
#undef sodium_malloc
#undef sodium_free
#undef sodium_allocarray
int
main(void)
{
void *buf;
buf = sodium_allocarray(10, 100);
assert(buf != NULL);
memset(buf, 0, 1000);
sodium_free(buf);
printf("allocarray: ok\n");
buf = sodium_allocarray(0, 100);
sodium_free(buf);
printf("allocarray zero count: ok\n");
buf = sodium_allocarray(100, 0);
sodium_free(buf);
printf("allocarray zero size: ok\n");
buf = sodium_allocarray(SIZE_MAX / 2 + 1, SIZE_MAX / 2);
assert(buf == NULL);
printf("allocarray overflow: ok\n");
buf = sodium_malloc(SIZE_MAX - 1U);
assert(buf == NULL);
printf("malloc SIZE_MAX-1: ok\n");
sodium_free(NULL);
printf("free NULL: ok\n");
buf = sodium_malloc(100);
assert(buf != NULL);
memset(buf, 0x42, 100);
sodium_mprotect_noaccess(buf);
sodium_mprotect_readwrite(buf);
assert(((unsigned char *) buf)[0] == 0x42);
sodium_mprotect_readonly(buf);
assert(((unsigned char *) buf)[0] == 0x42);
sodium_mprotect_readwrite(buf);
sodium_free(buf);
printf("mprotect cycle: ok\n");
return 0;
}
+7
View File
@@ -0,0 +1,7 @@
allocarray: ok
allocarray zero count: ok
allocarray zero size: ok
allocarray overflow: ok
malloc SIZE_MAX-1: ok
free NULL: ok
mprotect cycle: ok
+52
View File
@@ -0,0 +1,52 @@
#define TEST_NAME "stream6"
#include "cmptest.h"
int
main(void)
{
unsigned char key[crypto_stream_salsa2012_KEYBYTES];
unsigned char nonce[crypto_stream_salsa2012_NONCEBYTES];
unsigned char *buf;
unsigned char *out;
memset(key, 0x01, sizeof key);
memset(nonce, 0x02, sizeof nonce);
buf = (unsigned char *) sodium_malloc(256);
out = (unsigned char *) sodium_malloc(256);
assert(crypto_stream_salsa2012_xor(out, buf, 0, nonce, key) == 0);
printf("xor_zero_len: ok\n");
memset(buf, 0, 100);
crypto_stream_salsa2012_xor(out, buf, 100, nonce, key);
{
unsigned char stream_out[100];
crypto_stream_salsa2012(stream_out, 100, nonce, key);
assert(memcmp(out, stream_out, 100) == 0);
}
printf("xor_tail_block: ok\n");
memset(buf, 0x55, 137);
crypto_stream_salsa2012_xor(out, buf, 137, nonce, key);
crypto_stream_salsa2012_xor(out, out, 137, nonce, key);
assert(memcmp(out, buf, 137) == 0);
printf("xor_roundtrip_137: ok\n");
memset(out, 0xab, 1);
crypto_stream_salsa2012(out, 0, nonce, key);
assert(out[0] == 0xab);
printf("stream_zero_len: ok\n");
memset(buf, 0x42, 256);
crypto_stream_salsa2012_xor(out, buf, 256, nonce, key);
crypto_stream_salsa2012_xor(out, out, 256, nonce, key);
assert(memcmp(out, buf, 256) == 0);
printf("xor_roundtrip_256: ok\n");
sodium_free(buf);
sodium_free(out);
return 0;
}
+5
View File
@@ -0,0 +1,5 @@
xor_zero_len: ok
xor_tail_block: ok
xor_roundtrip_137: ok
stream_zero_len: ok
xor_roundtrip_256: ok