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

ca16be
ca16be
#define TEST_NAME "box_easy2"
ca16be
#include "cmptest.h"
ca16be
ca16be
static const unsigned char small_order_p[crypto_box_PUBLICKEYBYTES] = {
ca16be
    0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
ca16be
    0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
ca16be
    0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00
ca16be
};
ca16be
ca16be
int
ca16be
main(void)
ca16be
{
ca16be
    unsigned char *alicepk;
ca16be
    unsigned char *alicesk;
ca16be
    unsigned char *bobpk;
ca16be
    unsigned char *bobsk;
ca16be
    unsigned char *mac;
ca16be
    unsigned char *nonce;
ca16be
    unsigned char *k1;
ca16be
    unsigned char *k2;
ca16be
    unsigned char *m;
ca16be
    unsigned char *m2;
ca16be
    unsigned char *c;
ca16be
    size_t         mlen;
ca16be
    size_t         i;
ca16be
    size_t         m_size;
ca16be
    size_t         m2_size;
ca16be
    size_t         c_size;
ca16be
    int            ret;
ca16be
ca16be
    m2_size = m_size = 7U + randombytes_uniform(1000);
ca16be
    c_size           = crypto_box_MACBYTES + m_size;
ca16be
    m                = (unsigned char *) sodium_malloc(m_size);
ca16be
    m2               = (unsigned char *) sodium_malloc(m2_size);
ca16be
    c                = (unsigned char *) sodium_malloc(c_size);
ca16be
    alicepk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
ca16be
    alicesk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
ca16be
    bobpk   = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
ca16be
    bobsk   = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
ca16be
    mac     = (unsigned char *) sodium_malloc(crypto_box_MACBYTES);
ca16be
    nonce   = (unsigned char *) sodium_malloc(crypto_box_NONCEBYTES);
ca16be
    k1      = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
ca16be
    k2      = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
ca16be
    crypto_box_keypair(alicepk, alicesk);
ca16be
    crypto_box_keypair(bobpk, bobsk);
ca16be
    mlen = (size_t) randombytes_uniform((uint32_t) m_size) + 1U;
ca16be
    randombytes_buf(m, mlen);
ca16be
    randombytes_buf(nonce, crypto_box_NONCEBYTES);
ca16be
    ret = crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
ca16be
    assert(ret == 0);
ca16be
    if (crypto_box_open_easy(m2, c,
ca16be
                             (unsigned long long) mlen + crypto_box_MACBYTES,
ca16be
                             nonce, alicepk, bobsk) != 0) {
ca16be
        printf("open() failed");
ca16be
        return 1;
ca16be
    }
ca16be
    printf("%d\n", memcmp(m, m2, mlen));
ca16be
ca16be
    for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
ca16be
        if (crypto_box_open_easy(m2, c, (unsigned long long) i, nonce, alicepk,
ca16be
                                 bobsk) == 0) {
ca16be
            printf("short open() should have failed");
ca16be
            return 1;
ca16be
        }
ca16be
    }
ca16be
    memcpy(c, m, mlen);
ca16be
    ret =
ca16be
        crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
ca16be
    assert(ret == 0);
ca16be
    printf("%d\n", memcmp(m, c, mlen) == 0);
ca16be
    printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
ca16be
    if (crypto_box_open_easy(c, c,
ca16be
                             (unsigned long long) mlen + crypto_box_MACBYTES,
ca16be
                             nonce, alicepk, bobsk) != 0) {
ca16be
        printf("crypto_box_open_easy() failed\n");
ca16be
    }
ca16be
ca16be
    ret = crypto_box_beforenm(k1, small_order_p, bobsk);
ca16be
    assert(ret == -1);
ca16be
    ret = crypto_box_beforenm(k2, small_order_p, alicesk);
ca16be
    assert(ret == -1);
ca16be
ca16be
    ret = crypto_box_beforenm(k1, alicepk, bobsk);
ca16be
    assert(ret == 0);
ca16be
    ret = crypto_box_beforenm(k2, bobpk, alicesk);
ca16be
    assert(ret == 0);
ca16be
ca16be
    memset(m2, 0, m2_size);
ca16be
ca16be
    if (crypto_box_easy_afternm(c, m, 0, nonce, k1) != 0) {
ca16be
        printf(
ca16be
            "crypto_box_easy_afternm() with a null ciphertext should have "
ca16be
            "worked\n");
ca16be
    }
ca16be
    crypto_box_easy_afternm(c, m, (unsigned long long) mlen, nonce, k1);
ca16be
    if (crypto_box_open_easy_afternm(
ca16be
            m2, c, (unsigned long long) mlen + crypto_box_MACBYTES, nonce,
ca16be
            k2) != 0) {
ca16be
        printf("crypto_box_open_easy_afternm() failed\n");
ca16be
    }
ca16be
    printf("%d\n", memcmp(m, m2, mlen));
ca16be
    if (crypto_box_open_easy_afternm(m2, c, crypto_box_MACBYTES - 1U, nonce,
ca16be
                                     k2) == 0) {
ca16be
        printf(
ca16be
            "crypto_box_open_easy_afternm() with a huge ciphertext should have "
ca16be
            "failed\n");
ca16be
    }
ca16be
    memset(m2, 0, m2_size);
ca16be
    ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen, nonce,
ca16be
                              small_order_p, bobsk);
ca16be
    assert(ret == -1);
ca16be
    ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen, nonce,
ca16be
                              alicepk, bobsk);
ca16be
    assert(ret == 0);
ca16be
    if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen, nonce,
ca16be
                                 small_order_p, alicesk) != -1) {
ca16be
        printf("crypto_box_open_detached() with a weak key passed\n");
ca16be
    }
ca16be
    if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen, nonce,
ca16be
                                 bobpk, alicesk) != 0) {
ca16be
        printf("crypto_box_open_detached() failed\n");
ca16be
    }
ca16be
    printf("%d\n", memcmp(m, m2, mlen));
ca16be
ca16be
    memset(m2, 0, m2_size);
ca16be
    crypto_box_detached_afternm(c, mac, m, (unsigned long long) mlen, nonce,
ca16be
                                k1);
ca16be
    if (crypto_box_open_detached_afternm(m2, c, mac, (unsigned long long) mlen,
ca16be
                                         nonce, k2) != 0) {
ca16be
        printf("crypto_box_open_detached_afternm() failed\n");
ca16be
    }
ca16be
    printf("%d\n", memcmp(m, m2, mlen));
ca16be
ca16be
    sodium_free(alicepk);
ca16be
    sodium_free(alicesk);
ca16be
    sodium_free(bobpk);
ca16be
    sodium_free(bobsk);
ca16be
    sodium_free(mac);
ca16be
    sodium_free(nonce);
ca16be
    sodium_free(k1);
ca16be
    sodium_free(k2);
ca16be
    sodium_free(m);
ca16be
    sodium_free(m2);
ca16be
    sodium_free(c);
ca16be
    printf("OK\n");
ca16be
ca16be
    return 0;
ca16be
}