#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_uart.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/shamd5.h"
#include "utils/uartstdio.h"

//*****************************************************************************
//
// Number of bytes to send and receive.
//
//*****************************************************************************
#if 0
uint32_t g_ui32HMACKey[16] 		=  {0x6566654a, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0
								   };
uint32_t g_ui32RandomData[16] 	=  {0x74616877, 0x206f6420, 0x77206179, 0x20746e61,
									0x20726f66, 0x68746f6e, 0x3f676e69, 0x0,
									0x0, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0
	   	   	   	   	   	   	   	   };
#define STRINGLENGTH 28
#endif
#if 0
uint8_t g_ui8HMACKey[64] = "Jefe";
uint8_t g_ui8RandomData[64] = "what do ya want for nothing?";
#define STRINGLENGTH 28
#endif
#if 1
uint8_t g_ui8HMACKey[64] = "ABC123";
uint8_t g_ui8RandomData[64] = "helloworld";
#define STRINGLENGTH 10
#endif
#if 0
uint32_t g_ui32HMACKey[16] 		=  {0x0b0b0b0b, 0x0b0b0b0b, 0x0b0b0b0b, 0x0b0b0b0b,
									0x0b0b0b0b, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0
								   };
uint32_t g_ui32RandomData[16] 	=  {0x54206948, 0x65726568, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0,
									0x0, 0x0, 0x0, 0x0
	   	   	   	   	   	   	   	   };
#endif
//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
void
InitConsole(void)
{
    //
    // Enable GPIO port A which is used for UART0 pins.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Configure the pin muxing for UART0 functions on port A0 and A1.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);

    //
    // Enable UART0 so that we can configure the clock.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Use the internal 16MHz oscillator as the UART clock source.
    //
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    //
    // Select the alternate (UART) function for these pins.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, 16000000);
}

//*****************************************************************************
//
// Configure SSI3 in master Freescale (SPI) mode.  This example will send out
// 3 bytes of data, then wait for 3 bytes of data to come in.  This will all be
// done using the polling method.
//
//*****************************************************************************
int
main(void)
{
	uint32_t ui32Counter;
	uint32_t pui32HashResult[32];
	uint8_t  *ptr;

	InitConsole();

	SysCtlPeripheralDisable(SYSCTL_PERIPH_CCM0);
	SysCtlPeripheralReset(SYSCTL_PERIPH_CCM0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_CCM0);

	while(!SysCtlPeripheralReady(SYSCTL_PERIPH_CCM0))
	{
	}

	SHAMD5Reset(SHAMD5_BASE);

	SHAMD5ConfigSet(SHAMD5_BASE, SHAMD5_ALGO_HMAC_SHA256);

	//SHAMD5HMACKeySet(SHAMD5_BASE, g_ui32HMACKey);
	//SHAMD5HMACProcess(SHAMD5_BASE, g_ui32RandomData, 28, pui32HashResult); // orig -> 8

	SHAMD5HMACKeySet(SHAMD5_BASE, (uint32_t *) g_ui8HMACKey);
	SHAMD5HMACProcess(SHAMD5_BASE, (uint32_t *) g_ui8RandomData, STRINGLENGTH, pui32HashResult); // orig -> 8

	ptr = (uint8_t *)&pui32HashResult[0];

	for(ui32Counter=0;ui32Counter<32;ui32Counter++)
	{
		UARTprintf("%02x",*ptr++);
	}

    //
    // Loop Here
    //
    while(1);
}
