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

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