Add extra box_seal() tests

This commit is contained in:
Frank Denis 2020-05-21 18:37:50 +02:00
parent 81a2655233
commit c35437c2e5
2 changed files with 67 additions and 3 deletions

View File

@ -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);

View File

@ -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;
}