Skip to content

Commit 7a69c58

Browse files
Fix AES encryption on 16-bit platforms
The AES_S array contains 16-bit integers, which are shifted upwards during processing. On platforms where int is 32-bit or wider, these integers are integer-promoted to 32-bit before shifting, making them work as expected. On smaller platforms, this does not happen, causing the upper 16 bits to be shifted off before the value is converted to a 32-bit integer. By manually casting these values to 32-bit before shifting, this issue is prevented.
1 parent 4245276 commit 7a69c58

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/lmic/aes.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ static const u4_t AES_E4[256] = {
194194
r0 ^= AES_E1[ (i>>24)]
195195

196196
#define AES_expr(a,r0,r1,r2,r3,i) a = ki[i]; \
197-
a ^= (AES_S[ r0>>24 ]<<24); \
198-
a ^= (AES_S[u1(r1>>16)]<<16); \
199-
a ^= (AES_S[u1(r2>> 8)]<< 8); \
200-
a ^= AES_S[u1(r3) ]
197+
a ^= ((u4_t)AES_S[ r0>>24 ]<<24); \
198+
a ^= ((u4_t)AES_S[u1(r1>>16)]<<16); \
199+
a ^= ((u4_t)AES_S[u1(r2>> 8)]<< 8); \
200+
a ^= (u4_t)AES_S[u1(r3) ]
201201

202202
// global area for passing parameters (aux, key) and for storing round keys
203203
u4_t AESAUX[16/sizeof(u4_t)];
@@ -217,10 +217,10 @@ static void aesroundkeys () {
217217
for( ; i<44; i++ ) {
218218
if( i%4==0 ) {
219219
// b = SubWord(RotWord(b)) xor Rcon[i/4]
220-
b = (AES_S[u1(b >> 16)] << 24) ^
221-
(AES_S[u1(b >> 8)] << 16) ^
222-
(AES_S[u1(b) ] << 8) ^
223-
(AES_S[ b >> 24 ] ) ^
220+
b = ((u4_t)AES_S[u1(b >> 16)] << 24) ^
221+
((u4_t)AES_S[u1(b >> 8)] << 16) ^
222+
((u4_t)AES_S[u1(b) ] << 8) ^
223+
((u4_t)AES_S[ b >> 24 ] ) ^
224224
AES_RCON[(i-4)/4];
225225
}
226226
AESKEY[i] = b ^= AESKEY[i-4];

0 commit comments

Comments
 (0)