2015-04-16 17:46:29 -04:00
|
|
|
|
|
|
|
#define TEST_NAME "box_seal"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2019-05-30 10:04:55 -04:00
|
|
|
static
|
|
|
|
void tv1(void)
|
2015-04-16 17:46:29 -04:00
|
|
|
{
|
2017-02-23 05:27:45 -05:00
|
|
|
unsigned char pk[crypto_box_PUBLICKEYBYTES];
|
|
|
|
unsigned char sk[crypto_box_SECRETKEYBYTES];
|
2015-04-16 17:46:29 -04:00
|
|
|
unsigned char *c;
|
|
|
|
unsigned char *m;
|
|
|
|
unsigned char *m2;
|
|
|
|
size_t m_len;
|
|
|
|
size_t c_len;
|
|
|
|
|
|
|
|
crypto_box_keypair(pk, sk);
|
|
|
|
m_len = (size_t) randombytes_uniform(1000);
|
|
|
|
c_len = crypto_box_SEALBYTES + m_len;
|
2017-02-23 05:27:45 -05:00
|
|
|
m = (unsigned char *) sodium_malloc(m_len);
|
|
|
|
m2 = (unsigned char *) sodium_malloc(m_len);
|
|
|
|
c = (unsigned char *) sodium_malloc(c_len);
|
2015-04-16 17:46:29 -04:00
|
|
|
randombytes_buf(m, m_len);
|
|
|
|
if (crypto_box_seal(c, m, m_len, pk) != 0) {
|
|
|
|
printf("crypto_box_seal() failure\n");
|
2019-05-30 10:04:55 -04:00
|
|
|
return;
|
2015-04-16 17:46:29 -04:00
|
|
|
}
|
|
|
|
if (crypto_box_seal_open(m2, c, c_len, pk, sk) != 0) {
|
|
|
|
printf("crypto_box_seal_open() failure\n");
|
2019-05-30 10:04:55 -04:00
|
|
|
return;
|
2015-04-16 17:46:29 -04:00
|
|
|
}
|
|
|
|
printf("%d\n", memcmp(m, m2, m_len));
|
|
|
|
|
|
|
|
printf("%d\n", crypto_box_seal_open(m, c, 0U, pk, sk));
|
|
|
|
printf("%d\n", crypto_box_seal_open(m, c, c_len - 1U, pk, sk));
|
|
|
|
printf("%d\n", crypto_box_seal_open(m, c, c_len, sk, pk));
|
|
|
|
|
|
|
|
sodium_free(c);
|
|
|
|
sodium_free(m);
|
|
|
|
sodium_free(m2);
|
|
|
|
|
|
|
|
assert(crypto_box_sealbytes() == crypto_box_SEALBYTES);
|
2019-05-30 10:04:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void tv2(void)
|
2020-05-21 12:37:50 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-06-22 12:31:46 -04:00
|
|
|
assert(m_len == 0 || memcmp(cm, m2, m_len) != 0);
|
2020-05-21 12:37:50 -04:00
|
|
|
sodium_free(cm);
|
|
|
|
sodium_free(m2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SODIUM_LIBRARY_MINIMAL
|
|
|
|
static
|
|
|
|
void tv3(void)
|
2019-05-30 10:04:55 -04:00
|
|
|
{
|
|
|
|
unsigned char pk[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES];
|
|
|
|
unsigned char sk[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES];
|
|
|
|
unsigned char *c;
|
|
|
|
unsigned char *m;
|
|
|
|
unsigned char *m2;
|
|
|
|
size_t m_len;
|
|
|
|
size_t c_len;
|
|
|
|
|
|
|
|
crypto_box_curve25519xchacha20poly1305_keypair(pk, sk);
|
|
|
|
m_len = (size_t) randombytes_uniform(1000);
|
|
|
|
c_len = crypto_box_curve25519xchacha20poly1305_SEALBYTES + m_len;
|
|
|
|
m = (unsigned char *) sodium_malloc(m_len);
|
|
|
|
m2 = (unsigned char *) sodium_malloc(m_len);
|
|
|
|
c = (unsigned char *) sodium_malloc(c_len);
|
|
|
|
randombytes_buf(m, m_len);
|
|
|
|
if (crypto_box_curve25519xchacha20poly1305_seal(c, m, m_len, pk) != 0) {
|
|
|
|
printf("crypto_box_curve25519xchacha20poly1305_seal() failure\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (crypto_box_curve25519xchacha20poly1305_seal_open(m2, c, c_len, pk, sk) != 0) {
|
|
|
|
printf("crypto_box_curve25519xchacha20poly1305_seal_open() failure\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("%d\n", memcmp(m, m2, m_len));
|
|
|
|
|
|
|
|
printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, 0U, pk, sk));
|
|
|
|
printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, c_len - 1U, pk, sk));
|
|
|
|
printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, c_len, sk, pk));
|
|
|
|
|
|
|
|
sodium_free(c);
|
|
|
|
sodium_free(m);
|
|
|
|
sodium_free(m2);
|
|
|
|
|
|
|
|
assert(crypto_box_curve25519xchacha20poly1305_sealbytes() ==
|
|
|
|
crypto_box_curve25519xchacha20poly1305_SEALBYTES);
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:37:50 -04:00
|
|
|
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;
|
|
|
|
}
|
2020-06-22 12:31:46 -04:00
|
|
|
assert(m_len == 0 || memcmp(cm, m2, m_len) != 0);
|
2020-05-21 12:37:50 -04:00
|
|
|
sodium_free(cm);
|
|
|
|
sodium_free(m2);
|
|
|
|
}
|
|
|
|
|
2019-05-30 10:04:55 -04:00
|
|
|
#else
|
|
|
|
|
|
|
|
static
|
2020-05-21 12:37:50 -04:00
|
|
|
void tv3(void)
|
2019-05-30 10:04:55 -04:00
|
|
|
{
|
|
|
|
printf("0\n-1\n-1\n-1\n");
|
|
|
|
}
|
2020-05-21 12:37:50 -04:00
|
|
|
|
|
|
|
static
|
|
|
|
void tv4(void)
|
|
|
|
{ }
|
2019-05-30 10:04:55 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
tv1();
|
|
|
|
tv2();
|
2020-05-21 12:37:50 -04:00
|
|
|
tv3();
|
|
|
|
tv4();
|
2015-04-16 17:46:29 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|