2 #include <twr_system.h>
6 #define _TWR_AES_DATATYPE AES_CR_DATATYPE_1
10 static bool _twr_aes_process(
void *buffer_out,
const void *buffer_in,
size_t length);
14 RCC->AHBENR |= RCC_AHBENR_CRYPEN_Msk;
21 twr_system_pll_enable();
23 AES->CR = AES_CR_MODE_0;
25 _twr_aes_set_key(key);
31 while ((AES->SR & AES_SR_CCF) == 0)
35 twr_system_pll_disable();
40 decryption_key[0] = AES->KEYR0;
41 decryption_key[1] = AES->KEYR1;
42 decryption_key[2] = AES->KEYR2;
43 decryption_key[3] = AES->KEYR3;
45 AES->CR |= AES_CR_CCFC;
47 twr_system_pll_disable();
54 if ((length % 16) != 0)
59 AES->CR = _TWR_AES_DATATYPE;
61 _twr_aes_set_key(key);
63 return _twr_aes_process(buffer_out, buffer_in, length);
68 if ((length % 16) != 0)
73 AES->CR = _TWR_AES_DATATYPE | AES_CR_MODE_1;
75 _twr_aes_set_key(key);
77 return _twr_aes_process(buffer_out, buffer_in, length);
82 if ((length % 16) != 0)
87 AES->CR = AES_CR_CHMOD_0 | _TWR_AES_DATATYPE;
89 _twr_aes_set_key(key);
93 return _twr_aes_process(buffer_out, buffer_in, length);
98 if ((length % 16) != 0)
103 AES->CR = AES_CR_CHMOD_0 | _TWR_AES_DATATYPE | AES_CR_MODE_1;
105 _twr_aes_set_key(key);
109 return _twr_aes_process(buffer_out, buffer_in, length);
114 uint8_t *tmp = (uint8_t *) key;
116 for (
int i = 0; i < 16; i++)
118 tmp[15 - i] = buffer[i];
124 uint8_t *tmp = (uint8_t *) iv;
126 for (
int i = 0; i < 16; i++)
128 tmp[15 - i] = buffer[i];
148 static bool _twr_aes_process(
void *buffer_out,
const void *buffer_in,
size_t length)
152 uint32_t *inputaddr = (uint32_t *) buffer_in;
153 uint32_t *outputaddr = (uint32_t *) buffer_out;
155 twr_system_pll_enable();
157 bool inputaddr_aligned = ((uint32_t) inputaddr & (uint32_t) 0x00000003U) == 0U;
158 bool outputaddr_aligned = ((uint32_t) outputaddr & (uint32_t) 0x00000003U) == 0U;
160 if (!outputaddr_aligned)
162 outputaddr = (uint32_t *) buffer;
165 AES->CR |= AES_CR_EN;
167 for (
size_t i = 0; i < length; i += 16)
169 if (!inputaddr_aligned)
171 memcpy(buffer, (uint8_t *) buffer_in + i, 16);
173 inputaddr = (uint32_t *) buffer;
176 AES->DINR = *(inputaddr++);
177 AES->DINR = *(inputaddr++);
178 AES->DINR = *(inputaddr++);
179 AES->DINR = *(inputaddr++);
183 while ((AES->SR & AES_SR_CCF) == 0)
187 twr_system_pll_disable();
192 AES->CR |= AES_CR_CCFC;
194 *(outputaddr++) = AES->DOUTR;
195 *(outputaddr++) = AES->DOUTR;
196 *(outputaddr++) = AES->DOUTR;
197 *(outputaddr++) = AES->DOUTR;
199 if (!outputaddr_aligned)
201 memcpy((uint8_t *) buffer_out + i, buffer, 16);
202 outputaddr = (uint32_t *) buffer;
206 AES->CR &= ~AES_CR_EN;
208 twr_system_pll_disable();
uint32_t twr_aes_iv_t[TWR_AES_IVLEN/8/4]
AES 128-bit Initialization vector.
bool twr_aes_ctwr_decrypt(void *buffer_out, const void *buffer_in, size_t length, twr_aes_key_t key, twr_aes_iv_t iv)
AES Cipher block chaining (CBC)
void twr_aes_key_from_uint8(twr_aes_key_t key, const uint8_t *buffer)
Create key from uint8 array.
uint32_t twr_aes_key_t[TWR_AES_KEYLEN/8/4]
AES 128-bit Key.
void twr_aes_init(void)
Initialize AES.
void twr_aes_iv_from_uint8(twr_aes_iv_t iv, const uint8_t *buffer)
Create Initialization vector from uint8 array.
bool twr_aes_ecb_decrypt(void *buffer_out, const void *buffer_in, size_t length, twr_aes_key_t key)
AES decryption Electronic CodeBook (ECB)
bool twr_aes_ecb_encrypt(void *buffer_out, const void *buffer_in, const size_t length, const twr_aes_key_t key)
AES encryption Electronic CodeBook (ECB)
bool twr_aes_key_derivation(twr_aes_key_t decryption_key, const twr_aes_key_t key)
AES derivation decryption key from encryption key.
bool twr_aes_ctwr_encrypt(void *buffer_out, const void *buffer_in, size_t length, twr_aes_key_t key, twr_aes_iv_t iv)
AES Cipher block chaining (CBC)
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.