2016-06-01 15:07:50 -04:00
|
|
|
#include "aes.h"
|
|
|
|
#include "block.h"
|
|
|
|
|
|
|
|
#ifndef HOST_BUILD
|
2021-02-06 16:00:57 -05:00
|
|
|
#include <nrf_soc.h>
|
2016-06-01 15:07:50 -04:00
|
|
|
#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>
|
|
|
|
|
2021-02-06 16:00:57 -05:00
|
|
|
static nrf_ecb_hal_data_t g_ecbdata;
|
2016-06-01 15:07:50 -04:00
|
|
|
|
2016-09-01 14:30:15 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2021-02-06 16:00:57 -05:00
|
|
|
memcpy(g_ecbdata.cleartext, in, 16);
|
|
|
|
sd_ecb_block_encrypt(&g_ecbdata);
|
2016-06-01 15:07:50 -04:00
|
|
|
#endif /* HOST_BUILD */
|
2021-02-06 16:00:57 -05:00
|
|
|
return g_ecbdata.ciphertext;
|
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 */
|