Skip to main content

SHA256 on Texas Instrument's Tiva C Series of Cortex M4 Microcontrollers: Software vs. Hardware

So I wrote code to perform SHA256 hashing on a single block on data using the TM4C129DNCPDT running off a 16 MHz Crystal. I performed the hashing first using software C code and then using the Tivaware Library function which make use of TM4C129x's "Hardware Accelerated Hash (SHA/MD5) advanced hash engine that supports SHA-1, SHA-2 or MD5 Hash computation".

I assigned 2 GPIOs for measuring the time taken for each method (the software one and the hardware one).
I toggled the GPIO before and after hashing for each of the two methods.
The Oscilloscope shows the pulses observed on the two GPIOs.

The results are as below:
Software SHA256: 1.216 milliseconds or 1216 microseconds.
SHA256 using Hardware Acceleration: 28 microseconds (4200% faster)

Amount of time take to perform SHA256 in Software (Channel 2) and
using Hardware Acceleration (Channel 1).



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/shamd5.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "sha256.h"

#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//Output observable on UART0 115200 8-N-1
//SHA256 Software vs. Hardware comparison on TM4C129DNCPDT
//SHA256 Software Code taken from: http://www.spale.com/download/scrypt/scrypt1.0/
//Input Message (64 bytes): {0x72616e41, 0x696c616b, 0x20616b20, 0x6e6f6870, 0x61682065}
//Software SHA-256 Result: {0x568e2d32, 0x7113821c, 0xa5b5025a, 0xe3335ddd, 0x779bc58c, 0x5d0fc8ed, 0x98f48aef, 0xc0e6db2e}
//Hardware SHA-256 Result: {0x568e2d32, 0x7113821c, 0xa5b5025a, 0xe3335ddd, 0x779bc58c, 0x5d0fc8ed, 0x98f48aef, 0xc0e6db2e}

void ConfigureUART(void) {
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000L);
}

int main(void) {
 uint8_t stringMessage[64] = "Anarkali ka phone hai. Anarkali ka phone hai. Anarkali ka phone.";
 uint32_t * g_ui32Message = (uint32_t *) stringMessage;

 sha256_context SW_ctx;
 unsigned char SW_sha256sumSW[32];
 uint32_t * SW_ui32SHA256Result = (uint32_t *)SW_sha256sumSW;

 uint32_t HW_ui32SHA256Result[8];

 FPULazyStackingEnable();
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |SYSCTL_OSC_MAIN);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_2);
    GPIOPinWrite( GPIO_PORTD_BASE,GPIO_PIN_3,0);
    GPIOPinWrite( GPIO_PORTD_BASE,GPIO_PIN_2,0);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_CCM0); //Enable the CCM module.
 while(!SysCtlPeripheralReady(SYSCTL_PERIPH_CCM0)) {
    } //Wait for the CCM module to be ready.

    ConfigureUART();
    UARTprintf("\nSHA256 Software vs. Hardware comparison on TM4C129DNCPDT\n");
    UARTprintf("SHA256 Software Code taken from: http://www.spale.com/download/scrypt/scrypt1.0/\n");

    UARTprintf("Input Message (64 bytes): {0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x}\n",
          g_ui32Message[0],
          g_ui32Message[1],
          g_ui32Message[2],
          g_ui32Message[3],
          g_ui32Message[4]);

    GPIOPinWrite( GPIO_PORTD_BASE,GPIO_PIN_3,GPIO_PIN_3);
    sha256_starts( &SW_ctx );
    sha256_update( &SW_ctx, (uint8 *) stringMessage, 64);
    sha256_finish( &SW_ctx, SW_sha256sumSW );
    GPIOPinWrite( GPIO_PORTD_BASE,GPIO_PIN_3,0);

    GPIOPinWrite( GPIO_PORTD_BASE,GPIO_PIN_2,GPIO_PIN_2);
 SHAMD5Reset(SHAMD5_BASE); //Reset the SHA/MD5 module before use.
    SHAMD5ConfigSet(SHAMD5_BASE, SHAMD5_ALGO_SHA256); //Configure the SHA/MD5 module.
    SHAMD5DataProcess(SHAMD5_BASE, g_ui32Message, 64, HW_ui32SHA256Result); //Generate the hash from the data.
    GPIOPinWrite( GPIO_PORTD_BASE,GPIO_PIN_2,0);

    UARTprintf("Software SHA-256 Result: {0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x}\n",
      SW_ui32SHA256Result[0],
      SW_ui32SHA256Result[1],
      SW_ui32SHA256Result[2],
      SW_ui32SHA256Result[3],
      SW_ui32SHA256Result[4],
      SW_ui32SHA256Result[5],
      SW_ui32SHA256Result[6],
      SW_ui32SHA256Result[7]);

    UARTprintf("Hardware SHA-256 Result: {0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x}\n",
      HW_ui32SHA256Result[0],
      HW_ui32SHA256Result[1],
      HW_ui32SHA256Result[2],
      HW_ui32SHA256Result[3],
      HW_ui32SHA256Result[4],
      HW_ui32SHA256Result[5],
      HW_ui32SHA256Result[6],
      HW_ui32SHA256Result[7]);
    while(1) {
    }
}


The complete Eclipse Workspace Project (Code Composer Studio 5.5) for this experiment can be downloaded from here.

Comments