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

rdobuilder 775784
rdobuilder 775784
#define TEST_NAME "chacha20"
rdobuilder 775784
#include "cmptest.h"
rdobuilder 775784
rdobuilder 775784
static
rdobuilder 775784
void tv(void)
rdobuilder 775784
{
rdobuilder 775784
    static struct {
rdobuilder 775784
        const char *key_hex;
rdobuilder 775784
        const char *nonce_hex;
rdobuilder 775784
    } tests[]
rdobuilder 775784
      = { { "0000000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "0000000000000000" },
rdobuilder 775784
          { "0000000000000000000000000000000000000000000000000000000000000001",
rdobuilder 775784
            "0000000000000000" },
rdobuilder 775784
          { "0000000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "0000000000000001" },
rdobuilder 775784
          { "0000000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "0100000000000000" },
rdobuilder 775784
          { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
rdobuilder 775784
            "0001020304050607" } };
rdobuilder 775784
    unsigned char  key[crypto_stream_chacha20_KEYBYTES];
rdobuilder 775784
    unsigned char  nonce[crypto_stream_chacha20_NONCEBYTES];
rdobuilder 775784
    unsigned char *part;
rdobuilder 775784
    unsigned char  out[160];
rdobuilder 775784
    unsigned char  zero[160];
rdobuilder 775784
    char           out_hex[160 * 2 + 1];
rdobuilder 775784
    size_t         i = 0U;
rdobuilder 775784
    size_t         plen;
rdobuilder 775784
rdobuilder 775784
    memset(zero, 0, sizeof zero);
rdobuilder 775784
    do {
rdobuilder 775784
        sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
rdobuilder 775784
                       strlen(tests[i].key_hex), NULL, NULL, NULL);
rdobuilder 775784
        sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
rdobuilder 775784
                       strlen(tests[i].nonce_hex), NULL, NULL, NULL);
rdobuilder 775784
        crypto_stream_chacha20(out, sizeof out, nonce, key);
rdobuilder 775784
        sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
        printf("[%s]\n", out_hex);
rdobuilder 775784
        for (plen = 1U; plen < sizeof out; plen++) {
rdobuilder 775784
            part = (unsigned char *) sodium_malloc(plen);
rdobuilder 775784
            crypto_stream_chacha20_xor(part, out, plen, nonce, key);
rdobuilder 775784
            if (memcmp(part, zero, plen) != 0) {
rdobuilder 775784
                printf("Failed with length %lu\n", (unsigned long) plen);
rdobuilder 775784
            }
rdobuilder 775784
            sodium_free(part);
rdobuilder 775784
        }
rdobuilder 775784
    } while (++i < (sizeof tests) / (sizeof tests[0]));
rdobuilder 775784
    assert(66 <= sizeof out);
rdobuilder 775784
    for (plen = 1U; plen < 66; plen += 3) {
rdobuilder 775784
        memset(out, (int) (plen & 0xff), sizeof out);
rdobuilder 775784
        crypto_stream_chacha20(out, plen, nonce, key);
rdobuilder 775784
        sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
        printf("[%s]\n", out_hex);
rdobuilder 775784
    }
rdobuilder 775784
    randombytes_buf(out, sizeof out);
rdobuilder 775784
    crypto_stream_chacha20(out, sizeof out, nonce, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
rdobuilder 775784
    assert(crypto_stream_chacha20(out, 0U, nonce, key) == 0);
rdobuilder 775784
    assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
rdobuilder 775784
    assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
rdobuilder 775784
    assert(crypto_stream_chacha20_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
rdobuilder 775784
rdobuilder 775784
    memset(out, 0x42, sizeof out);
rdobuilder 775784
    crypto_stream_chacha20_xor(out, out, sizeof out, nonce, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
rdobuilder 775784
    crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 0U, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
rdobuilder 775784
    crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 1U, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
}
rdobuilder 775784
rdobuilder 775784
static
rdobuilder 775784
void tv_ietf(void)
rdobuilder 775784
{
rdobuilder 775784
    static struct {
rdobuilder 775784
        const char *key_hex;
rdobuilder 775784
        const char *nonce_hex;
rdobuilder 775784
        uint32_t    ic;
rdobuilder 775784
    } tests[]
rdobuilder 775784
      = { { "0000000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "000000000000000000000000",
rdobuilder 775784
            0U },
rdobuilder 775784
          { "0000000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "000000000000000000000000",
rdobuilder 775784
            1U },
rdobuilder 775784
          { "0000000000000000000000000000000000000000000000000000000000000001",
rdobuilder 775784
            "000000000000000000000000",
rdobuilder 775784
            1U },
rdobuilder 775784
          { "00ff000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "000000000000000000000000",
rdobuilder 775784
            2U },
rdobuilder 775784
          { "0000000000000000000000000000000000000000000000000000000000000000",
rdobuilder 775784
            "000000000000000000000002",
rdobuilder 775784
            0U },
rdobuilder 775784
          { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
rdobuilder 775784
            "000000090000004a00000000",
rdobuilder 775784
            1U },
rdobuilder 775784
          { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
rdobuilder 775784
            "000000090000004a00000000",
rdobuilder 775784
            0xfeffffff }};
rdobuilder 775784
    unsigned char  key[crypto_stream_chacha20_KEYBYTES];
rdobuilder 775784
    unsigned char  nonce[crypto_stream_chacha20_IETF_NONCEBYTES];
rdobuilder 775784
    unsigned char *part;
rdobuilder 775784
    unsigned char  out[160];
rdobuilder 775784
    unsigned char  zero[160];
rdobuilder 775784
    char           out_hex[160 * 2 + 1];
rdobuilder 775784
    size_t         i = 0U;
rdobuilder 775784
    size_t         plen;
rdobuilder 775784
rdobuilder 775784
    memset(zero, 0, sizeof zero);
rdobuilder 775784
    do {
rdobuilder 775784
        sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
rdobuilder 775784
                       strlen(tests[i].key_hex), ": ", NULL, NULL);
rdobuilder 775784
        sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
rdobuilder 775784
                       strlen(tests[i].nonce_hex), ": ", NULL, NULL);
rdobuilder 775784
        memset(out, 0, sizeof out);
rdobuilder 775784
        crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, tests[i].ic, key);
rdobuilder 775784
        sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
        printf("[%s]\n", out_hex);
rdobuilder 775784
        for (plen = 1U; plen < sizeof out; plen++) {
rdobuilder 775784
            part = (unsigned char *) sodium_malloc(plen);
rdobuilder 775784
            crypto_stream_chacha20_ietf_xor_ic(part, out, plen, nonce, tests[i].ic, key);
rdobuilder 775784
            if (memcmp(part, zero, plen) != 0) {
rdobuilder 775784
                printf("Failed with length %lu\n", (unsigned long) plen);
rdobuilder 775784
            }
rdobuilder 775784
            sodium_free(part);
rdobuilder 775784
        }
rdobuilder 775784
    } while (++i < (sizeof tests) / (sizeof tests[0]));
rdobuilder 775784
    assert(66 <= sizeof out);
rdobuilder 775784
    for (plen = 1U; plen < 66; plen += 3) {
rdobuilder 775784
        memset(out, (int) (plen & 0xff), sizeof out);
rdobuilder 775784
        crypto_stream_chacha20(out, plen, nonce, key);
rdobuilder 775784
        sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
        printf("[%s]\n", out_hex);
rdobuilder 775784
    }
rdobuilder 775784
    randombytes_buf(out, sizeof out);
rdobuilder 775784
    crypto_stream_chacha20_ietf(out, sizeof out, nonce, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf(out, 0U, nonce, key) == 0);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
rdobuilder 775784
rdobuilder 775784
    memset(out, 0x42, sizeof out);
rdobuilder 775784
    crypto_stream_chacha20_ietf_xor(out, out, sizeof out, nonce, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
rdobuilder 775784
    crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 0U, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
rdobuilder 775784
    crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 1U, key);
rdobuilder 775784
    sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
rdobuilder 775784
    printf("[%s]\n", out_hex);
rdobuilder 775784
}
rdobuilder 775784
rdobuilder 775784
int
rdobuilder 775784
main(void)
rdobuilder 775784
{
rdobuilder 775784
    tv();
rdobuilder 775784
    tv_ietf();
rdobuilder 775784
rdobuilder 775784
    assert(crypto_stream_chacha20_keybytes() > 0U);
rdobuilder 775784
    assert(crypto_stream_chacha20_keybytes() == crypto_stream_chacha20_KEYBYTES);
rdobuilder 775784
    assert(crypto_stream_chacha20_noncebytes() > 0U);
rdobuilder 775784
    assert(crypto_stream_chacha20_noncebytes() == crypto_stream_chacha20_NONCEBYTES);
rdobuilder 775784
    assert(crypto_stream_chacha20_messagebytes_max() == crypto_stream_chacha20_MESSAGEBYTES_MAX);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_keybytes() > 0U);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_keybytes() == crypto_stream_chacha20_ietf_KEYBYTES);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_noncebytes() > 0U);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_noncebytes() == crypto_stream_chacha20_ietf_NONCEBYTES);
rdobuilder 775784
    assert(crypto_stream_chacha20_ietf_messagebytes_max() == crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX);
rdobuilder 775784
rdobuilder 775784
    return 0;
rdobuilder 775784
}