1
0
Fork 0
nrf51-cryptolib/aes.c

45 lines
896 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(const uint8_t *key) {
2016-06-07 12:51:42 -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:51:42 -04:00
NRF_ECB->ECBDATAPTR = (uint32_t)&g_ecbdata;
2016-06-01 15:07:50 -04:00
#endif /* HOST_BUILD */
2016-06-07 12:51:42 -04:00
}
return;
2016-06-01 15:07:50 -04:00
}
2017-08-02 15:59:58 -04:00
uint8_t *aes128_ecb(uint8_t const *const in) {
2016-06-01 15:07:50 -04:00
#ifdef HOST_BUILD
2016-06-07 12:51:42 -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
2017-08-02 15:59:58 -04:00
memcpy(g_ecbdata.in, in, 16);
2016-06-07 12:51:42 -04:00
NRF_ECB->TASKS_STARTECB = 1;
2016-09-29 12:50:08 -04:00
while (!NRF_ECB->EVENTS_ENDECB) {
2016-06-07 12:51:42 -04:00
;
2016-09-29 12:50:08 -04:00
}
2016-06-07 12:51:42 -04:00
NRF_ECB->EVENTS_ENDECB = 0;
2016-06-01 15:07:50 -04:00
#endif /* HOST_BUILD */
2017-08-02 15:59:58 -04:00
return g_ecbdata.out;
2016-06-01 15:07:50 -04:00
}
2016-06-07 12:42:08 -04:00
#ifdef HOST_BUILD
void aes_dump_state(void) {
2016-06-07 12:51:42 -04:00
block_print("Key", g_ecbdata.key);
2016-06-07 12:42:08 -04:00
}
#endif /* HOST_BUILD */