Files
apps
core
deps
codemirror
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
tildefriends/deps/libsodium/test/default/metamorphic.c

188 lines
5.5 KiB
C

#define TEST_NAME "metamorphic"
#include "cmptest.h"
#define MAXLEN 512
#define MAX_ITER 1000
static void
mm_generichash(void)
{
crypto_generichash_state st;
unsigned char *h, *h2;
unsigned char *k;
unsigned char *m;
size_t hlen;
size_t klen;
size_t mlen;
size_t l1, l2;
int i;
for (i = 0; i < MAX_ITER; i++) {
mlen = randombytes_uniform(MAXLEN);
m = (unsigned char *) sodium_malloc(mlen);
klen = randombytes_uniform(crypto_generichash_KEYBYTES_MAX -
crypto_generichash_KEYBYTES_MIN + 1U)
+ crypto_generichash_KEYBYTES_MIN;
k = (unsigned char *) sodium_malloc(klen);
hlen = randombytes_uniform(crypto_generichash_BYTES_MAX -
crypto_generichash_BYTES_MIN + 1U)
+ crypto_generichash_BYTES_MIN;
h = (unsigned char *) sodium_malloc(hlen);
h2 = (unsigned char *) sodium_malloc(hlen);
randombytes_buf(k, klen);
randombytes_buf(m, mlen);
crypto_generichash_init(&st, k, klen, hlen);
l1 = randombytes_uniform((uint32_t) mlen);
l2 = randombytes_uniform((uint32_t) (mlen - l1));
crypto_generichash_update(&st, m, l1);
crypto_generichash_update(&st, m + l1, l2);
crypto_generichash_update(&st, m + l1 + l2, mlen - l1 - l2);
crypto_generichash_final(&st, h, hlen);
crypto_generichash(h2, hlen, m, mlen, k, klen);
assert(memcmp(h, h2, hlen) == 0);
sodium_free(h2);
sodium_free(h);
sodium_free(k);
sodium_free(m);
}
}
static void
mm_onetimeauth(void)
{
crypto_onetimeauth_state st;
unsigned char *h, *h2;
unsigned char *k;
unsigned char *m;
size_t mlen;
size_t l1, l2;
int i;
for (i = 0; i < MAX_ITER; i++) {
mlen = randombytes_uniform(MAXLEN);
m = (unsigned char *) sodium_malloc(mlen);
k = (unsigned char *) sodium_malloc(crypto_onetimeauth_KEYBYTES);
h = (unsigned char *) sodium_malloc(crypto_onetimeauth_BYTES);
h2 = (unsigned char *) sodium_malloc(crypto_onetimeauth_BYTES);
crypto_onetimeauth_keygen(k);
randombytes_buf(m, mlen);
crypto_onetimeauth_init(&st, k);
l1 = randombytes_uniform((uint32_t) mlen);
l2 = randombytes_uniform((uint32_t) (mlen - l1));
crypto_onetimeauth_update(&st, m, l1);
crypto_onetimeauth_update(&st, m + l1, l2);
crypto_onetimeauth_update(&st, m + l1 + l2, mlen - l1 - l2);
crypto_onetimeauth_final(&st, h);
crypto_onetimeauth(h2, m, mlen, k);
assert(memcmp(h, h2, crypto_onetimeauth_BYTES) == 0);
sodium_free(h2);
sodium_free(h);
sodium_free(k);
sodium_free(m);
}
}
static void
mm_hmacsha256(void)
{
crypto_auth_hmacsha256_state st;
unsigned char *h, *h2;
unsigned char *k;
unsigned char *m;
size_t mlen;
size_t l1, l2;
int i;
for (i = 0; i < MAX_ITER; i++) {
mlen = randombytes_uniform(MAXLEN);
m = (unsigned char *) sodium_malloc(mlen);
k = (unsigned char *) sodium_malloc(crypto_auth_hmacsha256_KEYBYTES);
h = (unsigned char *) sodium_malloc(crypto_auth_hmacsha256_BYTES);
h2 = (unsigned char *) sodium_malloc(crypto_auth_hmacsha256_BYTES);
crypto_auth_hmacsha256_keygen(k);
randombytes_buf(m, mlen);
crypto_auth_hmacsha256_init(&st, k, crypto_auth_hmacsha256_KEYBYTES);
l1 = randombytes_uniform((uint32_t) mlen);
l2 = randombytes_uniform((uint32_t) (mlen - l1));
crypto_auth_hmacsha256_update(&st, m, l1);
crypto_auth_hmacsha256_update(&st, m + l1, l2);
crypto_auth_hmacsha256_update(&st, m + l1 + l2, mlen - l1 - l2);
crypto_auth_hmacsha256_final(&st, h);
crypto_auth_hmacsha256(h2, m, mlen, k);
assert(memcmp(h, h2, crypto_auth_hmacsha256_BYTES) == 0);
sodium_free(h2);
sodium_free(h);
sodium_free(k);
sodium_free(m);
}
}
static void
mm_hmacsha512(void)
{
crypto_auth_hmacsha512_state st;
unsigned char *h, *h2;
unsigned char *k;
unsigned char *m;
size_t mlen;
size_t l1, l2;
int i;
for (i = 0; i < MAX_ITER; i++) {
mlen = randombytes_uniform(MAXLEN);
m = (unsigned char *) sodium_malloc(mlen);
k = (unsigned char *) sodium_malloc(crypto_auth_hmacsha512_KEYBYTES);
h = (unsigned char *) sodium_malloc(crypto_auth_hmacsha512_BYTES);
h2 = (unsigned char *) sodium_malloc(crypto_auth_hmacsha512_BYTES);
crypto_auth_hmacsha512_keygen(k);
randombytes_buf(m, mlen);
crypto_auth_hmacsha512_init(&st, k, crypto_auth_hmacsha512_KEYBYTES);
l1 = randombytes_uniform((uint32_t) mlen);
l2 = randombytes_uniform((uint32_t) (mlen - l1));
crypto_auth_hmacsha512_update(&st, m, l1);
crypto_auth_hmacsha512_update(&st, m + l1, l2);
crypto_auth_hmacsha512_update(&st, m + l1 + l2, mlen - l1 - l2);
crypto_auth_hmacsha512_final(&st, h);
crypto_auth_hmacsha512(h2, m, mlen, k);
assert(memcmp(h, h2, crypto_auth_hmacsha512_BYTES) == 0);
sodium_free(h2);
sodium_free(h);
sodium_free(k);
sodium_free(m);
}
}
int
main(void)
{
mm_generichash();
mm_onetimeauth();
mm_hmacsha256();
mm_hmacsha512();
printf("OK\n");
return 0;
}