/*
 * Copyright (C) 2018 Silke Hofstra
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup     tests
 * @{
 *
 * @file
 * @brief       Test the libb2 package
 *
 * @author      Silke Hofstra <silke@slxh.eu>
 *
 * @}
 */

#include <string.h>
#include "embUnit.h"
#include "blake2.h"

const uint8_t msg[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0a, 0x0b, 0x0c };
const uint8_t key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
                        0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
                        0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
                        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
                        0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f };
const uint8_t b2s[] = { 0xf5, 0xc4, 0xbe, 0xc6, 0xd6, 0x2f, 0xc6, 0x08,
                        0xbf, 0x41, 0xcc, 0x11, 0x5f, 0x16, 0xd6, 0x1c,
                        0x7e, 0xfd, 0x3f, 0xf6, 0xc6, 0x56, 0x92, 0xbb,
                        0xe0, 0xaf, 0xff, 0xb1, 0xfe, 0xde, 0x74, 0x75 };
const uint8_t b2b[] = { 0x43, 0xd4, 0x4b, 0xfa, 0x18, 0x76, 0x8c, 0x59,
                        0x89, 0x6b, 0xf7, 0xed, 0x17, 0x65, 0xcb, 0x2d,
                        0x14, 0xaf, 0x8c, 0x26, 0x02, 0x66, 0x03, 0x90,
                        0x99, 0xb2, 0x5a, 0x60, 0x3e, 0x4d, 0xdc, 0x50,
                        0x39, 0xd6, 0xef, 0x3a, 0x91, 0x84, 0x7d, 0x10,
                        0x88, 0xd4, 0x01, 0xc0, 0xc7, 0xe8, 0x47, 0x78,
                        0x1a, 0x8a, 0x59, 0x0d, 0x33, 0xa3, 0xc6, 0xcb,
                        0x4d, 0xf0, 0xfa, 0xb1, 0xc2, 0xf2, 0x23, 0x55 };

/* Test BLAKE2s */
static void test_blake2s(void)
{
    uint8_t hash[BLAKE2S_OUTBYTES];
    int res = blake2s(hash, msg, key,
                      sizeof hash, sizeof msg, BLAKE2S_KEYBYTES);

    TEST_ASSERT(res == 0);
    TEST_ASSERT(memcmp(b2s, hash, sizeof hash) == 0);
}

/* Test BLAKE2b */
static void test_blake2b(void)
{
    uint8_t hash[BLAKE2B_OUTBYTES];
    int res = blake2b(hash, msg, key,
                      sizeof hash, sizeof msg, BLAKE2B_KEYBYTES);

    TEST_ASSERT(res == 0);
    TEST_ASSERT(memcmp(b2b, hash, sizeof hash) == 0);
}

Test *tests_blake2(void)
{
    EMB_UNIT_TESTFIXTURES(fixtures) {
        new_TestFixture(test_blake2s),
        new_TestFixture(test_blake2b),
    };
    EMB_UNIT_TESTCALLER(blake2_tests, NULL, NULL, fixtures);
    return (Test *)&blake2_tests;
}

int main(void)
{
    TESTS_START();
    TESTS_RUN(tests_blake2());
    TESTS_END();
    return 0;
}
