aboutsummaryrefslogtreecommitdiff
path: root/cc3200/util/hash.c
blob: a7afdf9f2f531630c4bae9f5f07b3d6a9f1a4a69 (plain)
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
#include "std.h"
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_nvic.h"
#include "inc/hw_shamd5.h"
#include "inc/hw_dthe.h"
#include "hw_memmap.h"
#include "rom_map.h"
#include "prcm.h"
#include "shamd5.h"
#include "hash.h"

#ifdef USE_FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#endif


#ifdef USE_FREERTOS
static SemaphoreHandle_t xShamd5Semaphore = NULL;
#endif

void HASH_Init (void) {
    // Enable the Data Hashing and Transform Engine
    MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
#ifdef USE_FREERTOS
    vSemaphoreCreateBinary(xShamd5Semaphore);
#endif
}



void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {

#ifdef USE_FREERTOS
    xSemaphoreTake (xShamd5Semaphore, portMAX_DELAY);
#endif

    MAP_PRCMPeripheralReset(PRCM_DTHE);

    // wait until the context is ready
    while ((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_CONTEXT_READY) == 0);

    // Configure the SHA/MD5 module algorithm
    MAP_SHAMD5ConfigSet(SHAMD5_BASE, algo);

    // if not a multiple of 64 bytes, close the hash
    if (blocklen % 64) {
        HWREG(SHAMD5_BASE + SHAMD5_O_MODE) |= SHAMD5_MODE_CLOSE_HASH;
    }

    // set the lenght
    HWREG(SHAMD5_BASE + SHAMD5_O_LENGTH) = blocklen;
}

void HASH_SHAMD5Update (uint8_t *data, uint32_t datalen) {
    // write the data
    SHAMD5DataWriteMultiple(data, datalen);
}

void HASH_SHAMD5Read (uint8_t *hash) {
    // wait for the output to be ready.
    while((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_OUTPUT_READY) == 0);
    // read the result.
    MAP_SHAMD5ResultRead(SHAMD5_BASE, hash);
#ifdef USE_FREERTOS
    xSemaphoreGive (xShamd5Semaphore);
#endif
}