44#include < rcon.hpp>
55#include < hexascii.hpp>
66
7- uint8_t * aesKey::asBytes () {
8- return key.asByte ;
9- }
10-
11- uint32_t * aesKey::asWords () {
12- return key.asUint32 ;
13- }
14-
157void aesKey::setFromByteArray (const uint8_t bytes[lengthInBytes]) {
16- (void )memcpy (key.asByte , bytes, lengthInBytes);
8+ (void )memcpy (keyAsBytes, bytes, lengthInBytes);
9+ syncWordsFromBytes ();
10+ syncHexStringFromBytes ();
11+ #ifndef HARDWARE_AES
1712 expandKey ();
18- }
19-
20- void aesKey::setFromWordArray (const uint32_t wordsIn[lengthInWords]) {
21- for (uint32_t wordIndex = 0 ; wordIndex < 4 ; wordIndex++) {
22- key.asUint32 [wordIndex] = wordsIn[wordIndex];
23- }
13+ #endif
2414}
2515
2616void aesKey::setFromHexString (const char * string) {
27- uint8_t tmpBytes[lengthInBytes];
28- hexAscii::hexStringToByteArray (tmpBytes, string, 32U );
29- (void )memcpy (key.asByte , tmpBytes, lengthInBytes);
17+ hexAscii::hexStringToByteArray (keyAsBytes, string, 32U );
18+ syncWordsFromBytes ();
19+ syncHexStringFromBytes ();
20+ #ifndef HARDWARE_AES
3021 expandKey ();
22+ #endif
3123}
3224
33- uint32_t aesKey::swapLittleBigEndian ( uint32_t wordIn) {
25+ uint32_t aesKey::swapLittleBigEndian (uint32_t wordIn) {
3426 // ARM Cortex-M4 stores uin32_t in little endian format, but STM32WLE5 AES peripheral expects big endian format. This function swaps the bytes in a word.
3527 uint32_t wordOut;
3628 wordOut = (wordIn & 0xFF000000 ) >> 24 | (wordIn & 0x00FF0000 ) >> 8 | (wordIn & 0x0000FF00 ) << 8 | (wordIn & 0x000000FF ) << 24 ;
3729 return wordOut;
3830}
3931
32+ #ifndef HARDWARE_AES
4033void aesKey::expandKey () {
41- (void )memcpy (expandedKey, key.asByte , 16 );
34+ for (uint32_t index = 0 ; index < lengthInBytes; index++) {
35+ expandedKey[index] = keyAsBytes[index];
36+ }
4237 for (uint8_t round = 1 ; round <= 10 ; round++) {
4338 (void )memcpy (expandedKey + (round * 16 ), expandedKey + ((round - 1 ) * 16 ), 16 );
4439 calculateRoundKey (round);
4540 }
4641}
42+ #endif
4743
44+ #ifndef HARDWARE_AES
4845void aesKey::calculateRoundKey (uint8_t round) {
4946 unsigned char temp[4 ];
5047 uint8_t * expandedKeyPtr = expandedKey + (round * 16 );
@@ -61,3 +58,11 @@ void aesKey::calculateRoundKey(uint8_t round) {
6158 temp[index % 4 ] = expandedKeyPtr[index];
6259 }
6360}
61+ #endif
62+
63+ void aesKey::syncWordsFromBytes () {
64+ (void )memcpy (keyAsWords, keyAsBytes, lengthInBytes);
65+ }
66+ void aesKey::syncHexStringFromBytes () {
67+ hexAscii::byteArrayToHexString (keyAsHexString, keyAsBytes, lengthInBytes);
68+ }
0 commit comments