Blame SOURCES/libsodium-1.0.18/test/default/box_seal.c

rdobuilder 775784
rdobuilder 775784
#define TEST_NAME "box_seal"
rdobuilder 775784
#include "cmptest.h"
rdobuilder 775784
rdobuilder 775784
static
rdobuilder 775784
void tv1(void)
rdobuilder 775784
{
rdobuilder 775784
    unsigned char  pk[crypto_box_PUBLICKEYBYTES];
rdobuilder 775784
    unsigned char  sk[crypto_box_SECRETKEYBYTES];
rdobuilder 775784
    unsigned char *c;
rdobuilder 775784
    unsigned char *m;
rdobuilder 775784
    unsigned char *m2;
rdobuilder 775784
    size_t         m_len;
rdobuilder 775784
    size_t         c_len;
rdobuilder 775784
rdobuilder 775784
    crypto_box_keypair(pk, sk);
rdobuilder 775784
    m_len = (size_t) randombytes_uniform(1000);
rdobuilder 775784
    c_len = crypto_box_SEALBYTES + m_len;
rdobuilder 775784
    m     = (unsigned char *) sodium_malloc(m_len);
rdobuilder 775784
    m2    = (unsigned char *) sodium_malloc(m_len);
rdobuilder 775784
    c     = (unsigned char *) sodium_malloc(c_len);
rdobuilder 775784
    randombytes_buf(m, m_len);
rdobuilder 775784
    if (crypto_box_seal(c, m, m_len, pk) != 0) {
rdobuilder 775784
        printf("crypto_box_seal() failure\n");
rdobuilder 775784
        return;
rdobuilder 775784
    }
rdobuilder 775784
    if (crypto_box_seal_open(m2, c, c_len, pk, sk) != 0) {
rdobuilder 775784
        printf("crypto_box_seal_open() failure\n");
rdobuilder 775784
        return;
rdobuilder 775784
    }
rdobuilder 775784
    printf("%d\n", memcmp(m, m2, m_len));
rdobuilder 775784
rdobuilder 775784
    printf("%d\n", crypto_box_seal_open(m, c, 0U, pk, sk));
rdobuilder 775784
    printf("%d\n", crypto_box_seal_open(m, c, c_len - 1U, pk, sk));
rdobuilder 775784
    printf("%d\n", crypto_box_seal_open(m, c, c_len, sk, pk));
rdobuilder 775784
rdobuilder 775784
    sodium_free(c);
rdobuilder 775784
    sodium_free(m);
rdobuilder 775784
    sodium_free(m2);
rdobuilder 775784
rdobuilder 775784
    assert(crypto_box_sealbytes() == crypto_box_SEALBYTES);
rdobuilder 775784
}
rdobuilder 775784
rdobuilder 775784
#ifndef SODIUM_LIBRARY_MINIMAL
rdobuilder 775784
static
rdobuilder 775784
void tv2(void)
rdobuilder 775784
{
rdobuilder 775784
    unsigned char  pk[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES];
rdobuilder 775784
    unsigned char  sk[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES];
rdobuilder 775784
    unsigned char *c;
rdobuilder 775784
    unsigned char *m;
rdobuilder 775784
    unsigned char *m2;
rdobuilder 775784
    size_t         m_len;
rdobuilder 775784
    size_t         c_len;
rdobuilder 775784
rdobuilder 775784
    crypto_box_curve25519xchacha20poly1305_keypair(pk, sk);
rdobuilder 775784
    m_len = (size_t) randombytes_uniform(1000);
rdobuilder 775784
    c_len = crypto_box_curve25519xchacha20poly1305_SEALBYTES + m_len;
rdobuilder 775784
    m     = (unsigned char *) sodium_malloc(m_len);
rdobuilder 775784
    m2    = (unsigned char *) sodium_malloc(m_len);
rdobuilder 775784
    c     = (unsigned char *) sodium_malloc(c_len);
rdobuilder 775784
    randombytes_buf(m, m_len);
rdobuilder 775784
    if (crypto_box_curve25519xchacha20poly1305_seal(c, m, m_len, pk) != 0) {
rdobuilder 775784
        printf("crypto_box_curve25519xchacha20poly1305_seal() failure\n");
rdobuilder 775784
        return;
rdobuilder 775784
    }
rdobuilder 775784
    if (crypto_box_curve25519xchacha20poly1305_seal_open(m2, c, c_len, pk, sk) != 0) {
rdobuilder 775784
        printf("crypto_box_curve25519xchacha20poly1305_seal_open() failure\n");
rdobuilder 775784
        return;
rdobuilder 775784
    }
rdobuilder 775784
    printf("%d\n", memcmp(m, m2, m_len));
rdobuilder 775784
rdobuilder 775784
    printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, 0U, pk, sk));
rdobuilder 775784
    printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, c_len - 1U, pk, sk));
rdobuilder 775784
    printf("%d\n", crypto_box_curve25519xchacha20poly1305_seal_open(m, c, c_len, sk, pk));
rdobuilder 775784
rdobuilder 775784
    sodium_free(c);
rdobuilder 775784
    sodium_free(m);
rdobuilder 775784
    sodium_free(m2);
rdobuilder 775784
rdobuilder 775784
    assert(crypto_box_curve25519xchacha20poly1305_sealbytes() ==
rdobuilder 775784
           crypto_box_curve25519xchacha20poly1305_SEALBYTES);
rdobuilder 775784
}
rdobuilder 775784
rdobuilder 775784
#else
rdobuilder 775784
rdobuilder 775784
static
rdobuilder 775784
void tv2(void)
rdobuilder 775784
{
rdobuilder 775784
    printf("0\n-1\n-1\n-1\n");
rdobuilder 775784
}
rdobuilder 775784
#endif
rdobuilder 775784
rdobuilder 775784
int
rdobuilder 775784
main(void)
rdobuilder 775784
{
rdobuilder 775784
    tv1();
rdobuilder 775784
    tv2();
rdobuilder 775784
rdobuilder 775784
    return 0;
rdobuilder 775784
}