/* Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
#include <unity.h>

#include "utils.h"
#include "aes_cmac.h"
#include "aes_soft.h"

/* Bluetooth mesh sample data #1 */
static uint8_t m_net_key[] = {0x7d, 0xd7, 0x36, 0x4c, 0xd8, 0x42, 0xad, 0x18, 0xc1, 0x7c, 0x2b, 0x82, 0x0c, 0x84, 0xc3, 0xd6};
static uint8_t m_priv_key[] = {0xe2, 0xe0, 0x0f, 0x4d, 0xc2, 0xe0, 0x81, 0x35, 0xd2, 0x35, 0x05, 0x2e, 0xc7, 0xf6, 0x3a, 0xfa};
static uint8_t m_net_id[] = {0xb9, 0xb9, 0x39, 0x58, 0xce, 0x1f, 0xf2, 0xe3, 0x57, 0x82, 0xe6, 0xea, 0x43, 0xc4, 0x17, 0x4a};

/* RFC sample data */
static uint8_t m_key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};

static uint8_t m_msg1[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
static uint8_t m_msg2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11};
static uint8_t m_msg3[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};

static uint8_t m_cmac0[] = {0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46};
static uint8_t m_cmac1[] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c};
static uint8_t m_cmac2[] = {0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30, 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27};
static uint8_t m_cmac3[] = {0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe};

static uint8_t m_result[16];


void setUp(void)
{
}

void tearDown(void)
{
}

void test_aes_cmac0(void)
{
    aes_cmac(m_key, '\0', 0, m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_cmac0, m_result, 16);
}

void test_aes_cmac1(void)
{
    aes_cmac(m_key, m_msg1, sizeof(m_msg1), m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_cmac1, m_result, 16);
}

void test_aes_cmac2(void)
{
    aes_cmac(m_key, m_msg2, sizeof(m_msg2), m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_cmac2, m_result, 16);
}

void test_aes_cmac3(void)
{
    aes_cmac(m_key, m_msg3, sizeof(m_msg3), m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_cmac3, m_result, 16);

    /* Repeat test to ensure the same result twice! */
    aes_cmac(m_key, m_msg3, sizeof(m_msg3), m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_cmac3, m_result, 16);
}

void test_aes_cmac_netid(void)
{
    /* NetworkID= AES-CMAC (AES-CMAC (NetKey, “smdt”), “smid”) */
    unsigned char msg1[] = "smdt";
    unsigned char msg2[] = "smid";

    uint8_t buf1[16];
    aes_cmac(m_net_key, (const uint8_t *) msg1, 4, buf1);
    aes_cmac(buf1    ,  (const uint8_t *) msg2, 4, m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_net_id, m_result, 16);
}

void test_aes_cmac_priv_key(void)
{
    /* PrivacyKey = AES-CMAC (AES-CMAC (NetKey, “smpt”), “smpk”) */
    unsigned char msg1[] = "smpt";
    unsigned char msg2[] = "smpk";

    uint8_t buf1[16];
    aes_cmac(m_net_key, (const uint8_t *) msg1, 4, buf1);
    aes_cmac(buf1    ,  (const uint8_t *) msg2, 4, m_result);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(m_priv_key, m_result, 16);
}
