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

rdobuilder 775784
rdobuilder 775784
#define TEST_NAME "aead_xchacha20poly1305"
rdobuilder 775784
#include "cmptest.h"
rdobuilder 775784
rdobuilder 775784
static int
rdobuilder 775784
tv(void)
rdobuilder 775784
{
rdobuilder 775784
#undef  MLEN
rdobuilder 775784
#define MLEN 114U
rdobuilder 775784
#undef  ADLEN
rdobuilder 775784
#define ADLEN 12U
rdobuilder 775784
#undef  CLEN
rdobuilder 775784
#define CLEN (MLEN + crypto_aead_xchacha20poly1305_ietf_ABYTES)
rdobuilder 775784
    static const unsigned char firstkey[crypto_aead_xchacha20poly1305_ietf_KEYBYTES]
rdobuilder 775784
        = {
rdobuilder 775784
            0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
rdobuilder 775784
            0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
rdobuilder 775784
            0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
rdobuilder 775784
            0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
rdobuilder 775784
        };
rdobuilder 775784
#undef  MESSAGE
rdobuilder 775784
#define MESSAGE "Ladies and Gentlemen of the class of '99: If I could offer you " \
rdobuilder 775784
    "only one tip for the future, sunscreen would be it."
rdobuilder 775784
    unsigned char *m = (unsigned char *) sodium_malloc(MLEN);
rdobuilder 775784
    static const unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES]
rdobuilder 775784
        = { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
rdobuilder 775784
            0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53 };
rdobuilder 775784
    static const unsigned char ad[ADLEN]
rdobuilder 775784
        = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
rdobuilder 775784
    unsigned char *c = (unsigned char *) sodium_malloc(CLEN);
rdobuilder 775784
    unsigned char *detached_c = (unsigned char *) sodium_malloc(MLEN);
rdobuilder 775784
    unsigned char *key2 = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
rdobuilder 775784
    unsigned char *mac = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_ABYTES);
rdobuilder 775784
    unsigned char *m2 = (unsigned char *) sodium_malloc(MLEN);
rdobuilder 775784
    unsigned long long found_clen;
rdobuilder 775784
    unsigned long long found_maclen;
rdobuilder 775784
    unsigned long long m2len;
rdobuilder 775784
    size_t i;
rdobuilder 775784
rdobuilder 775784
    assert(sizeof MESSAGE - 1U == MLEN);
rdobuilder 775784
    memcpy(m, MESSAGE, MLEN);
rdobuilder 775784
    crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
rdobuilder 775784
                                               ad, ADLEN,
rdobuilder 775784
                                               NULL, nonce, firstkey);
rdobuilder 775784
    if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) {
rdobuilder 775784
        printf("found_clen is not properly set\n");
rdobuilder 775784
    }
rdobuilder 775784
    for (i = 0U; i < CLEN; ++i) {
rdobuilder 775784
        printf(",0x%02x", (unsigned int) c[i]);
rdobuilder 775784
        if (i % 8 == 7) {
rdobuilder 775784
            printf("\n");
rdobuilder 775784
        }
rdobuilder 775784
    }
rdobuilder 775784
    printf("\n");
rdobuilder 775784
    crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c,
rdobuilder 775784
                                                        mac, &found_maclen,
rdobuilder 775784
                                                        m, MLEN,
rdobuilder 775784
                                                        ad, ADLEN,
rdobuilder 775784
                                                        NULL, nonce, firstkey);
rdobuilder 775784
    if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) {
rdobuilder 775784
        printf("found_maclen is not properly set\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (memcmp(detached_c, c, MLEN) != 0) {
rdobuilder 775784
        printf("detached ciphertext is bogus\n");
rdobuilder 775784
    }
rdobuilder 775784
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(NULL, 0, NULL, c, CLEN, ad,
rdobuilder 775784
                                                   ADLEN, nonce, firstkey) != 0) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() tag-only verification failed\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, ad,
rdobuilder 775784
                                                   ADLEN, nonce, firstkey) != 0) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (m2len != MLEN) {
rdobuilder 775784
        printf("m2len is not properly set\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (memcmp(m, m2, MLEN) != 0) {
rdobuilder 775784
        printf("m != m2\n");
rdobuilder 775784
    }
rdobuilder 775784
    memset(m2, 0, m2len);
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL,
rdobuilder 775784
                                                            c, MLEN, mac,
rdobuilder 775784
                                                            ad, ADLEN,
rdobuilder 775784
                                                            nonce, firstkey) != 0) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (memcmp(m, m2, MLEN) != 0) {
rdobuilder 775784
        printf("detached m != m2\n");
rdobuilder 775784
    }
rdobuilder 775784
rdobuilder 775784
    for (i = 0U; i < CLEN; i++) {
rdobuilder 775784
        c[i] ^= (i + 1U);
rdobuilder 775784
        if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN,
rdobuilder 775784
                                                       ad, ADLEN, nonce, firstkey)
rdobuilder 775784
            == 0 || memcmp(m, m2, MLEN) == 0) {
rdobuilder 775784
            printf("message can be forged\n");
rdobuilder 775784
        }
rdobuilder 775784
        c[i] ^= (i + 1U);
rdobuilder 775784
    }
rdobuilder 775784
    crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
rdobuilder 775784
                                               NULL, 0U, NULL, nonce, firstkey);
rdobuilder 775784
    if (found_clen != CLEN) {
rdobuilder 775784
        printf("clen is not properly set (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    for (i = 0U; i < CLEN; ++i) {
rdobuilder 775784
        printf(",0x%02x", (unsigned int) c[i]);
rdobuilder 775784
        if (i % 8 == 7) {
rdobuilder 775784
            printf("\n");
rdobuilder 775784
        }
rdobuilder 775784
    }
rdobuilder 775784
    printf("\n");
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN,
rdobuilder 775784
                                                   NULL, 0U, nonce, firstkey) != 0) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (m2len != MLEN) {
rdobuilder 775784
        printf("m2len is not properly set (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (memcmp(m, m2, MLEN) != 0) {
rdobuilder 775784
        printf("m != m2 (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    m2len = 1;
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(
rdobuilder 775784
            m2, &m2len, NULL, guard_page,
rdobuilder 775784
            randombytes_uniform(crypto_aead_xchacha20poly1305_ietf_ABYTES),
rdobuilder 775784
            NULL, 0U, nonce, firstkey) != -1) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() worked with a short "
rdobuilder 775784
               "ciphertext\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (m2len != 0) {
rdobuilder 775784
        printf("Message length should have been set to zero after a failure\n");
rdobuilder 775784
    }
rdobuilder 775784
    m2len = 1;
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U,
rdobuilder 775784
                                                  nonce, firstkey) != -1) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() worked with an empty "
rdobuilder 775784
               "ciphertext\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (m2len != 0) {
rdobuilder 775784
        printf("Message length should have been set to zero after a failure\n");
rdobuilder 775784
    }
rdobuilder 775784
rdobuilder 775784
    memcpy(c, m, MLEN);
rdobuilder 775784
    crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN,
rdobuilder 775784
                                               NULL, 0U, NULL, nonce, firstkey);
rdobuilder 775784
    if (found_clen != CLEN) {
rdobuilder 775784
        printf("clen is not properly set (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    for (i = 0U; i < CLEN; ++i) {
rdobuilder 775784
        printf(",0x%02x", (unsigned int) c[i]);
rdobuilder 775784
        if (i % 8 == 7) {
rdobuilder 775784
            printf("\n");
rdobuilder 775784
        }
rdobuilder 775784
    }
rdobuilder 775784
    printf("\n");
rdobuilder 775784
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
rdobuilder 775784
                                                   NULL, 0U, nonce, firstkey) != 0) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (m2len != MLEN) {
rdobuilder 775784
        printf("m2len is not properly set (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
    if (memcmp(m, c, MLEN) != 0) {
rdobuilder 775784
        printf("m != c (adlen=0)\n");
rdobuilder 775784
    }
rdobuilder 775784
rdobuilder 775784
    crypto_aead_xchacha20poly1305_ietf_keygen(key2);
rdobuilder 775784
    if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
rdobuilder 775784
                                                   NULL, 0U, nonce, key2) == 0) {
rdobuilder 775784
        printf("crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed\n");
rdobuilder 775784
    }
rdobuilder 775784
rdobuilder 775784
    sodium_free(c);
rdobuilder 775784
    sodium_free(detached_c);
rdobuilder 775784
    sodium_free(key2);
rdobuilder 775784
    sodium_free(mac);
rdobuilder 775784
    sodium_free(m2);
rdobuilder 775784
    sodium_free(m);
rdobuilder 775784
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_ietf_abytes() == crypto_aead_xchacha20poly1305_ietf_ABYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_ietf_keybytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_ietf_npubbytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == 0U);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_ietf_messagebytes_max() == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_IETF_KEYBYTES  == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_IETF_NSECBYTES == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_IETF_NPUBBYTES == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_IETF_ABYTES    == crypto_aead_xchacha20poly1305_ietf_ABYTES);
rdobuilder 775784
    assert(crypto_aead_xchacha20poly1305_IETF_MESSAGEBYTES_MAX == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
rdobuilder 775784
rdobuilder 775784
    return 0;
rdobuilder 775784
}
rdobuilder 775784
rdobuilder 775784
int
rdobuilder 775784
main(void)
rdobuilder 775784
{
rdobuilder 775784
    tv();
rdobuilder 775784
rdobuilder 775784
    return 0;
rdobuilder 775784
}