1
0
Fork 0
nrf51-cryptolib/aes.c

44 lines
876 B
C
Raw Normal View History

2016-06-01 15:07:50 -04:00
#include "aes.h"
#include "block.h"
#ifndef HOST_BUILD
#include "nrf.h"
#else
2016-06-07 12:42:08 -04:00
#include <openssl/aes.h>
2016-06-01 15:07:50 -04:00
#endif /* HOST_BUILD */
#include <string.h>
static ecbdata_t g_ecbdata;
void aes128_init(uint8_t *key) {
2016-06-07 12:42:08 -04:00
if (key != NULL) {
memmove(g_ecbdata.key, key, 16);
2016-06-01 15:07:50 -04:00
#ifndef HOST_BUILD
2016-06-07 12:42:08 -04:00
NRF_ECB->ECBDATAPTR = (uint32_t)&g_ecbdata;
2016-06-01 15:07:50 -04:00
#endif /* HOST_BUILD */
2016-06-07 12:42:08 -04:00
}
2016-06-01 15:07:50 -04:00
return;
}
void aes128_ecb(uint8_t *dest, uint8_t const * const in) {
2016-06-01 15:07:50 -04:00
#ifdef HOST_BUILD
2016-06-07 12:42:08 -04:00
AES_KEY key;
AES_set_encrypt_key(g_ecbdata.key, 128, &key);
AES_encrypt(in, g_ecbdata.out, &key);
2016-06-01 15:07:50 -04:00
#else
memmove(g_ecbdata.in, in, 16);
NRF_ECB->TASKS_STARTECB = 1;
while (!NRF_ECB->EVENTS_ENDECB);
NRF_ECB->EVENTS_ENDECB = 0;
#endif /* HOST_BUILD */
memmove(dest, g_ecbdata.out, 16);
return;
2016-06-01 15:07:50 -04:00
}
2016-06-07 12:42:08 -04:00
#ifdef HOST_BUILD
void aes_dump_state(void) {
block_print("Key", g_ecbdata.key);
}
#endif /* HOST_BUILD */