/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/


#include "cmac.h"
#include "string.h"

// Removing of compiler warnings and errors because of type re-declaration between hal_stdtypes.h and Std_Types.h
#define _BOOLEAN_DECLARED
#define _UINT8_DECLARED
#define _UINT16_DECLARED
#define _UINT32_DECLARED
#define _UINT64_DECLARED
#define _SINT8_DECLARED
#define _SINT16_DECLARED
#define _SINT32_DECLARED
#define _SINT64_DECLARED
#define _FLOAT32_DECLARED
#define _FLOAT64_DECLARED
#define _STD_RETURN_TYPE_DECLARED
#define _STDVERSION_DECLARED

#include "Ios_SecurityAccess.h"
#include "Ios_IntrTimerManagement.h"
#include "Ios_Std.h"


#define USE_SECRETKEY_FROM_NVM

// ZIST-2197 Seed is 128bits long
uint8 SecurityAccessSeed[SEEDLENGTH]={0};

uint8 SecurityAccessCalcClientKey[SEEDLENGTH]={0};

uint8 SecurityAccessServerSecretKey[SEEDLENGTH]={0x01,0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                                                 0x09,0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,0x10};



void fnIosCalcClientKey(uint8 * Seed, uint8 SeedLength)
{
    //todo: get Stored Secret key from NVM
    fnIosResetCalcClientKey();

#ifdef USE_SECRETKEY_FROM_NVM
    Fbl_DataM_NvmSecretKeyReadFunc(SecurityAccessServerSecretKey);
#endif
    aes_cmac(SecurityAccessSeed, SeedLength, SecurityAccessCalcClientKey, SecurityAccessServerSecretKey);

}

void fnIosResetSeed(void)
{
    memset(SecurityAccessSeed, 0x00, SEEDLENGTH);
}

void fnIosResetCalcClientKey(void)
{
    memset(SecurityAccessCalcClientKey, 0x00, SEEDLENGTH);
}

void fnIosGetCalcClientKey(uint8 * Key, uint8 Length)
{
    uint8 i = 0;


    if(SEEDLENGTH >= Length)
    {
        //wcu_printf("Calc client key: ");
        for(i=0; i<Length; i++)
        {
            Key[i] = SecurityAccessCalcClientKey[i];
            //wcu_printf("0x%x ", Key[i]);
        }
        //wcu_printf("\n");
    }
}

void fnIosGenerateSeed(uint8* bpSeedArray, uint8 bSeedLength)
{
    uint64 u64SeedPart1 = fnIosGetFreeRunningCounterValue64Bits(rtiCOUNTER_BLOCK0);
    uint64 u64SeedPart2 = fnIosGetFreeRunningCounterValue64Bits(rtiCOUNTER_BLOCK1);
    uint8 i;

    if((NULL != bpSeedArray) && (SEEDLENGTH >= bSeedLength))
    {
        // Generate seed
        SecurityAccessSeed[0] = (uint8)(u64SeedPart1);
        SecurityAccessSeed[1] = (uint8)(u64SeedPart2);
        SecurityAccessSeed[2] = (uint8)(u64SeedPart1  >> 8);
        SecurityAccessSeed[3] = (uint8)(u64SeedPart2  >> 8);
        SecurityAccessSeed[4] = (uint8)(u64SeedPart1  >> 16);
        SecurityAccessSeed[5] = (uint8)(u64SeedPart2  >> 16);
        SecurityAccessSeed[6] = (uint8)(u64SeedPart1  >> 24);
        SecurityAccessSeed[7] = (uint8)(u64SeedPart2  >> 24);
        SecurityAccessSeed[8] = (uint8)(u64SeedPart1  >> 32);
        SecurityAccessSeed[9] = (uint8)(u64SeedPart2  >> 32);
        SecurityAccessSeed[10] = (uint8)(u64SeedPart1 >> 40);
        SecurityAccessSeed[11] = (uint8)(u64SeedPart2 >> 40);
        SecurityAccessSeed[12] = (uint8)(u64SeedPart1 >> 48);
        SecurityAccessSeed[13] = (uint8)(u64SeedPart2 >> 48);
        SecurityAccessSeed[14] = (uint8)(u64SeedPart1 >> 56);
        SecurityAccessSeed[15] = (uint8)(u64SeedPart2 >> 56);

        //wcu_printf("SA seed: ");
        // copy seed to recipient
        for(i = 0; i< bSeedLength; i++)
        {
            bpSeedArray[i] = SecurityAccessSeed[i];
            //wcu_printf("0x%x ", bpSeedArray[i]);
        }
        //wcu_printf("\n");

    }

}


