From 1127c43278870a9cde46ab5fbefc0bbcb814c2b9 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 21 May 2020 18:37:50 +0200 Subject: [PATCH] Add extra box_seal() tests --- .../box_seal_curve25519xchacha20poly1305.c | 2 +- test/default/box_seal.c | 68 ++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_seal_curve25519xchacha20poly1305.c b/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_seal_curve25519xchacha20poly1305.c index 9e73a265..0240f036 100644 --- a/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_seal_curve25519xchacha20poly1305.c +++ b/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_seal_curve25519xchacha20poly1305.c @@ -38,11 +38,11 @@ crypto_box_curve25519xchacha20poly1305_seal(unsigned char *c, const unsigned cha if (crypto_box_curve25519xchacha20poly1305_keypair(epk, esk) != 0) { return -1; /* LCOV_EXCL_LINE */ } - memcpy(c, epk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES); _crypto_box_curve25519xchacha20poly1305_seal_nonce(nonce, epk, pk); ret = crypto_box_curve25519xchacha20poly1305_easy( c + crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES, m, mlen, nonce, pk, esk); + memcpy(c, epk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES); sodium_memzero(esk, sizeof esk); sodium_memzero(epk, sizeof epk); sodium_memzero(nonce, sizeof nonce); diff --git a/test/default/box_seal.c b/test/default/box_seal.c index 01765ca8..e0f32b13 100644 --- a/test/default/box_seal.c +++ b/test/default/box_seal.c @@ -41,9 +41,38 @@ void tv1(void) assert(crypto_box_sealbytes() == crypto_box_SEALBYTES); } -#ifndef SODIUM_LIBRARY_MINIMAL static void tv2(void) +{ + unsigned char pk[crypto_box_PUBLICKEYBYTES]; + unsigned char sk[crypto_box_SECRETKEYBYTES]; + unsigned char *cm; + unsigned char *m2; + size_t m_len; + size_t cm_len; + + crypto_box_keypair(pk, sk); + m_len = (size_t) randombytes_uniform(1000); + cm_len = crypto_box_SEALBYTES + m_len; + m2 = (unsigned char *) sodium_malloc(m_len); + cm = (unsigned char *) sodium_malloc(cm_len); + randombytes_buf(cm, m_len); + if (crypto_box_seal(cm, cm, m_len, pk) != 0) { + printf("crypto_box_seal() failure\n"); + return; + } + if (crypto_box_seal_open(m2, cm, cm_len, pk, sk) != 0) { + printf("crypto_box_seal_open() failure\n"); + return; + } + assert(memcmp(cm, m2, m_len) != 0); + sodium_free(cm); + sodium_free(m2); +} + +#ifndef SODIUM_LIBRARY_MINIMAL +static +void tv3(void) { unsigned char pk[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES]; unsigned char sk[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES]; @@ -82,13 +111,46 @@ void tv2(void) crypto_box_curve25519xchacha20poly1305_SEALBYTES); } +static +void tv4(void) +{ + unsigned char pk[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES]; + unsigned char sk[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES]; + unsigned char *cm; + unsigned char *m2; + size_t m_len; + size_t cm_len; + + crypto_box_curve25519xchacha20poly1305_keypair(pk, sk); + m_len = (size_t) randombytes_uniform(1000); + cm_len = crypto_box_curve25519xchacha20poly1305_SEALBYTES + m_len; + m2 = (unsigned char *) sodium_malloc(m_len); + cm = (unsigned char *) sodium_malloc(cm_len); + randombytes_buf(cm, m_len); + if (crypto_box_curve25519xchacha20poly1305_seal(cm, cm, m_len, pk) != 0) { + printf("crypto_box_curve25519xchacha20poly1305_seal() failure\n"); + return; + } + if (crypto_box_curve25519xchacha20poly1305_seal_open(m2, cm, cm_len, pk, sk) != 0) { + printf("crypto_box_curve25519xchacha20poly1305_seal_open() failure\n"); + return; + } + assert(memcmp(cm, m2, m_len) != 0); + sodium_free(cm); + sodium_free(m2); +} + #else static -void tv2(void) +void tv3(void) { printf("0\n-1\n-1\n-1\n"); } + +static +void tv4(void) +{ } #endif int @@ -96,6 +158,8 @@ main(void) { tv1(); tv2(); + tv3(); + tv4(); return 0; }