forked from cory/tildefriends
apps
core
deps
codemirror
codemirror_src
crypt_blowfish
libbacktrace
libbacktrace_config
libsodium
.github
build-aux
builds
ci
contrib
dist-build
m4
packaging
regen-msvc
src
test
default
Makefile.am
Makefile.in
aead_aegis128l.c
aead_aegis128l.exp
aead_aegis256.c
aead_aegis256.exp
aead_aes256gcm.c
aead_aes256gcm.exp
aead_aes256gcm2.c
aead_aes256gcm2.exp
aead_chacha20poly1305.c
aead_chacha20poly1305.exp
aead_chacha20poly13052.c
aead_chacha20poly13052.exp
aead_xchacha20poly1305.c
aead_xchacha20poly1305.exp
auth.c
auth.exp
auth2.c
auth2.exp
auth3.c
auth3.exp
auth5.c
auth5.exp
auth6.c
auth6.exp
auth7.c
auth7.exp
box.c
box.exp
box2.c
box2.exp
box7.c
box7.exp
box8.c
box8.exp
box_easy.c
box_easy.exp
box_easy2.c
box_easy2.exp
box_seal.c
box_seal.exp
box_seed.c
box_seed.exp
chacha20.c
chacha20.exp
cmptest.h
codecs.c
codecs.exp
core1.c
core1.exp
core2.c
core2.exp
core3.c
core3.exp
core4.c
core4.exp
core5.c
core5.exp
core6.c
core6.exp
core_ed25519.c
core_ed25519.exp
core_ristretto255.c
core_ristretto255.exp
ed25519_convert.c
ed25519_convert.exp
generichash.c
generichash.exp
generichash2.c
generichash2.exp
generichash3.c
generichash3.exp
hash.c
hash.exp
hash3.c
hash3.exp
index.html.tpl
kdf.c
kdf.exp
kdf_hkdf.c
kdf_hkdf.exp
keygen.c
keygen.exp
kx.c
kx.exp
metamorphic.c
metamorphic.exp
misuse.c
misuse.exp
onetimeauth.c
onetimeauth.exp
onetimeauth2.c
onetimeauth2.exp
onetimeauth7.c
onetimeauth7.exp
pre.js.inc
pwhash_argon2i.c
pwhash_argon2i.exp
pwhash_argon2id.c
pwhash_argon2id.exp
pwhash_scrypt.c
pwhash_scrypt.exp
pwhash_scrypt_ll.c
pwhash_scrypt_ll.exp
randombytes.c
randombytes.exp
run.sh
scalarmult.c
scalarmult.exp
scalarmult2.c
scalarmult2.exp
scalarmult5.c
scalarmult5.exp
scalarmult6.c
scalarmult6.exp
scalarmult7.c
scalarmult7.exp
scalarmult8.c
scalarmult8.exp
scalarmult_ed25519.c
scalarmult_ed25519.exp
scalarmult_ristretto255.c
scalarmult_ristretto255.exp
secretbox.c
secretbox.exp
secretbox2.c
secretbox2.exp
secretbox7.c
secretbox7.exp
secretbox8.c
secretbox8.exp
secretbox_easy.c
secretbox_easy.exp
secretbox_easy2.c
secretbox_easy2.exp
secretstream_xchacha20poly1305.c
secretstream_xchacha20poly1305.exp
shorthash.c
shorthash.exp
sign.c
sign.exp
siphashx24.c
siphashx24.exp
sodium_core.c
sodium_core.exp
sodium_utils.c
sodium_utils.exp
sodium_utils2.c
sodium_utils2.exp
sodium_utils3.c
sodium_utils3.exp
sodium_version.c
sodium_version.exp
stream.c
stream.exp
stream2.c
stream2.exp
stream3.c
stream3.exp
stream4.c
stream4.exp
verify1.c
verify1.exp
wasi-test-wrapper.sh
wintest.bat
xchacha20.c
xchacha20.exp
quirks
Makefile.am
Makefile.in
constcheck.sh
.gitignore
AUTHORS
CITATION.cff
ChangeLog
LICENSE
Makefile.am
Makefile.in
README.markdown
THANKS
aclocal.m4
appveyor.yml
autogen.sh
azure-pipelines.yml
build.zig
configure
configure.ac
lgtm.yml
libsodium-uninstalled.pc.in
libsodium.pc.in
logo.png
libuv
lit
openssl
picohttpparser
quickjs
speedscope
sqlite
xopt
zlib
docs
src
tools
.dockerignore
Dockerfile
GNUmakefile
LICENSE
README.md
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3859 ed5197a5-7fde-0310-b194-c3ffbd925b24
225 lines
8.2 KiB
C
225 lines
8.2 KiB
C
#define TEST_NAME "sodium_utils"
|
|
#include "cmptest.h"
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
unsigned char buf_add[1000];
|
|
unsigned char buf1[1000];
|
|
unsigned char buf2[1000];
|
|
unsigned char buf1_rev[1000];
|
|
unsigned char buf2_rev[1000];
|
|
unsigned char nonce[24];
|
|
char nonce_hex[49];
|
|
unsigned char *bin_padded;
|
|
size_t bin_len, bin_len2;
|
|
size_t bin_padded_len;
|
|
size_t bin_padded_maxlen;
|
|
size_t blocksize;
|
|
unsigned int i;
|
|
unsigned int j;
|
|
|
|
randombytes_buf(buf1, sizeof buf1);
|
|
memcpy(buf2, buf1, sizeof buf2);
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
sodium_memzero(buf1, 0U);
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
sodium_memzero(buf1, sizeof buf1 / 2);
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, 0U));
|
|
sodium_memzero(buf2, sizeof buf2 / 2);
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
printf("%d\n", sodium_memcmp(buf1, guard_page, 0U));
|
|
printf("%d\n", sodium_memcmp(guard_page, buf2, 0U));
|
|
printf("%d\n", sodium_memcmp(guard_page, guard_page, 0U));
|
|
sodium_memzero(guard_page, 0U);
|
|
|
|
memset(nonce, 0, sizeof nonce);
|
|
sodium_increment(nonce, sizeof nonce);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
memset(nonce, 255, sizeof nonce);
|
|
sodium_increment(nonce, sizeof nonce);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
nonce[1] = 1U;
|
|
sodium_increment(nonce, sizeof nonce);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
nonce[1] = 0U;
|
|
sodium_increment(nonce, sizeof nonce);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
nonce[0] = 255U;
|
|
nonce[2] = 255U;
|
|
sodium_increment(nonce, sizeof nonce);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
for (i = 0U; i < 1000U; i++) {
|
|
bin_len = (size_t) randombytes_uniform(sizeof buf1);
|
|
randombytes_buf(buf1, bin_len);
|
|
randombytes_buf(buf2, bin_len);
|
|
for (j = 0U; j < bin_len; j++) {
|
|
buf1_rev[bin_len - 1 - j] = buf1[j];
|
|
buf2_rev[bin_len - 1 - j] = buf2[j];
|
|
}
|
|
if (memcmp(buf1_rev, buf2_rev, bin_len) *
|
|
sodium_compare(buf1, buf2, bin_len) < 0) {
|
|
printf("sodium_compare() failure with length=%u\n",
|
|
(unsigned int) bin_len);
|
|
}
|
|
memcpy(buf1, buf2, bin_len);
|
|
if (sodium_compare(buf1, buf2, bin_len)) {
|
|
printf("sodium_compare() equality failure with length=%u\n",
|
|
(unsigned int) bin_len);
|
|
}
|
|
}
|
|
printf("%d\n", sodium_compare(buf1, NULL, 0U));
|
|
printf("%d\n", sodium_compare(NULL, buf1, 0U));
|
|
memset(buf1, 0, sizeof buf1);
|
|
if (sodium_is_zero(buf1, sizeof buf1) != 1) {
|
|
printf("sodium_is_zero() failed\n");
|
|
}
|
|
for (i = 0U; i < sizeof buf1; i++) {
|
|
buf1[i]++;
|
|
if (sodium_is_zero(buf1, sizeof buf1) != 0) {
|
|
printf("sodium_is_zero() failed\n");
|
|
}
|
|
buf1[i]--;
|
|
}
|
|
bin_len = randombytes_uniform(sizeof buf1);
|
|
randombytes_buf(buf1, bin_len);
|
|
memcpy(buf2, buf1, bin_len);
|
|
memset(buf_add, 0, bin_len);
|
|
j = randombytes_uniform(10000);
|
|
for (i = 0U; i < j; i++) {
|
|
sodium_increment(buf1, bin_len);
|
|
sodium_increment(buf_add, bin_len);
|
|
}
|
|
sodium_add(buf2, buf_add, bin_len);
|
|
if (sodium_compare(buf1, buf2, bin_len) != 0) {
|
|
printf("sodium_add() failed\n");
|
|
}
|
|
bin_len = randombytes_uniform(sizeof buf1);
|
|
randombytes_buf(buf1, bin_len);
|
|
memcpy(buf2, buf1, bin_len);
|
|
memset(buf_add, 0xff, bin_len);
|
|
sodium_increment(buf2, bin_len);
|
|
sodium_increment(buf2, 0U);
|
|
sodium_add(buf2, buf_add, bin_len);
|
|
sodium_add(buf2, buf_add, 0U);
|
|
if (sodium_compare(buf1, buf2, bin_len) != 0) {
|
|
printf("sodium_add() failed\n");
|
|
}
|
|
for (i = 0U; i < 1000U; i++) {
|
|
randombytes_buf(buf1, bin_len);
|
|
randombytes_buf(buf2, bin_len);
|
|
sodium_add(buf1, buf2, bin_len);
|
|
sodium_sub(buf1, buf2, bin_len);
|
|
sodium_sub(buf1, buf2, 0U);
|
|
if (sodium_is_zero(buf1, bin_len) &&
|
|
!sodium_is_zero(buf1, bin_len)) {
|
|
printf("sodium_sub() failed\n");
|
|
}
|
|
sodium_sub(buf1, buf1, bin_len);
|
|
if (!sodium_is_zero(buf1, bin_len)) {
|
|
printf("sodium_sub() failed\n");
|
|
}
|
|
}
|
|
assert(sizeof nonce >= 24U);
|
|
memset(nonce, 0xfe, 24U);
|
|
memset(nonce, 0xff, 6U);
|
|
sodium_increment(nonce, 8U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
memset(nonce, 0xfe, 24U);
|
|
memset(nonce, 0xff, 10U);
|
|
sodium_increment(nonce, 12U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
memset(nonce, 0xff, 22U);
|
|
sodium_increment(nonce, 24U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
|
|
assert(sizeof nonce >= 24U);
|
|
memset(nonce, 0xfe, 24U);
|
|
memset(nonce, 0xff, 6U);
|
|
sodium_add(nonce, nonce, 7U);
|
|
sodium_add(nonce, nonce, 8U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
memset(nonce, 0xfe, 24U);
|
|
memset(nonce, 0xff, 10U);
|
|
sodium_add(nonce, nonce, 11U);
|
|
sodium_add(nonce, nonce, 12U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
memset(nonce, 0xff, 22U);
|
|
sodium_add(nonce, nonce, 23U);
|
|
sodium_add(nonce, nonce, 24U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
sodium_add(nonce, nonce, 0U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
sodium_add(nonce, guard_page, 0U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
sodium_add(guard_page, nonce, 0U);
|
|
|
|
sodium_sub(nonce, nonce, 0U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
sodium_sub(nonce, guard_page, 0U);
|
|
printf("%s\n",
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
|
sodium_sub(guard_page, nonce, 0U);
|
|
|
|
randombytes_buf(buf1, 64U);
|
|
randombytes_buf(buf2, 64U);
|
|
memset(buf_add, 0, 64U);
|
|
sodium_add(buf_add, buf1, 64U);
|
|
assert(!sodium_is_zero(buf_add, 64U));
|
|
sodium_add(buf_add, buf2, 64U);
|
|
assert(!sodium_is_zero(buf_add, 64U));
|
|
sodium_sub(buf_add, buf1, 64U);
|
|
assert(!sodium_is_zero(buf_add, 64U));
|
|
sodium_sub(buf_add, buf2, 64U);
|
|
assert(sodium_is_zero(buf_add, 64U));
|
|
|
|
for (i = 0; i < 2000U; i++) {
|
|
bin_len = randombytes_uniform(200U);
|
|
blocksize = 1U + randombytes_uniform(500U);
|
|
bin_padded_maxlen = bin_len + (blocksize - bin_len % blocksize);
|
|
bin_padded = (unsigned char *) sodium_malloc(bin_padded_maxlen);
|
|
randombytes_buf(bin_padded, bin_padded_maxlen);
|
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
blocksize, bin_padded_maxlen - 1U) == -1);
|
|
assert(sodium_pad(NULL, bin_padded, bin_len,
|
|
blocksize, bin_padded_maxlen + 1U) == 0);
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
blocksize, bin_padded_maxlen + 1U) == 0);
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
0U, bin_padded_maxlen) == -1);
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
blocksize, bin_padded_maxlen) == 0);
|
|
assert(bin_padded_len == bin_padded_maxlen);
|
|
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
bin_padded_len + 1U) == -1);
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
0U) == -1);
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
blocksize) == 0);
|
|
assert(bin_len2 == bin_len);
|
|
|
|
sodium_free(bin_padded);
|
|
}
|
|
|
|
sodium_stackzero(512);
|
|
|
|
return 0;
|
|
}
|