Skip to content

Commit 9e28d50

Browse files
committed
Fixes for ARM ASM with Thumb. Fix for AES ECB build with Thumb. Add alignment to thumb2 AES tables. Refactor alignment macros and expose generic alignment macro XALIGNED. The WOLFSSL_USE_ALIGN still controls alignment enablement in wolfSSL/wolfCrypt. ZD 17225 and ZD 17226
1 parent 77818d9 commit 9e28d50

4 files changed

Lines changed: 53 additions & 83 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,7 +2889,7 @@ static WARN_UNUSED_RESULT WC_INLINE word32 PreFetchTd(void)
28892889
}
28902890
return x;
28912891
}
2892-
#endif
2892+
#endif /* !WOLFSSL_AES_SMALL_TABLES */
28932893

28942894
/* load Td Table4 into cache by cache line stride */
28952895
static WARN_UNUSED_RESULT WC_INLINE word32 PreFetchTd4(void)
@@ -2906,7 +2906,7 @@ static WARN_UNUSED_RESULT WC_INLINE word32 PreFetchTd4(void)
29062906
return 0;
29072907
#endif
29082908
}
2909-
#endif
2909+
#endif /* !WC_NO_CACHE_RESISTANT */
29102910

29112911
/* Decrypt a block using AES.
29122912
*
@@ -3161,7 +3161,7 @@ static void AesDecryptBlocks_C(Aes* aes, const byte* in, byte* out, word32 sz)
31613161
}
31623162
#endif
31633163

3164-
#else
3164+
#else /* WC_AES_BITSLICED */
31653165

31663166
/* http://cs-www.cs.yale.edu/homes/peralta/CircuitStuff/Sinv.txt */
31673167
static void bs_inv_sub_bytes(bs_word u[8])
@@ -3501,7 +3501,7 @@ static void AesDecryptBlocks_C(Aes* aes, const byte* in, byte* out, word32 sz)
35013501
}
35023502
#endif
35033503

3504-
#endif
3504+
#endif /* !WC_AES_BITSLICED */
35053505

35063506
#if !defined(WC_AES_BITSLICED) || defined(WOLFSSL_AES_DIRECT)
35073507
/* Software AES - ECB Decrypt */
@@ -11410,7 +11410,7 @@ static WARN_UNUSED_RESULT int _AesEcbEncrypt(
1141011410
else
1141111411
#endif
1141211412
{
11413-
#ifndef WOLFSSL_ARMASM
11413+
#ifdef NEED_AES_TABLES
1141411414
AesEncryptBlocks_C(aes, in, out, sz);
1141511415
#else
1141611416
word32 i;
@@ -11461,7 +11461,7 @@ static WARN_UNUSED_RESULT int _AesEcbDecrypt(
1146111461
else
1146211462
#endif
1146311463
{
11464-
#ifndef WOLFSSL_ARMASM
11464+
#ifdef NEED_AES_TABLES
1146511465
AesDecryptBlocks_C(aes, in, out, sz);
1146611466
#else
1146711467
word32 i;

wolfcrypt/src/port/arm/thumb2-aes-asm_c.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#include <wolfssl/wolfcrypt/aes.h>
5555

5656
#ifdef HAVE_AES_DECRYPT
57-
static const uint32_t L_AES_Thumb2_td_data[] = {
57+
XALIGNED(16) static const uint32_t L_AES_Thumb2_td_data[] = {
5858
0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e,
5959
0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
6060
0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c,
@@ -123,7 +123,7 @@ static const uint32_t L_AES_Thumb2_td_data[] = {
123123

124124
#endif /* HAVE_AES_DECRYPT */
125125
#if defined(HAVE_AES_DECRYPT) || defined(HAVE_AES_CBC) || defined(HAVE_AESCCM) || defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
126-
static const uint32_t L_AES_Thumb2_te_data[] = {
126+
XALIGNED(16) static const uint32_t L_AES_Thumb2_te_data[] = {
127127
0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b,
128128
0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
129129
0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b,
@@ -319,7 +319,7 @@ void AES_invert_key(unsigned char* ks, word32 rounds)
319319
}
320320

321321
#endif /* HAVE_AES_DECRYPT */
322-
static const uint32_t L_AES_Thumb2_rcon[] = {
322+
XALIGNED(16) static const uint32_t L_AES_Thumb2_rcon[] = {
323323
0x01000000, 0x02000000, 0x04000000, 0x08000000,
324324
0x10000000, 0x20000000, 0x40000000, 0x80000000,
325325
0x1b000000, 0x36000000
@@ -2205,7 +2205,7 @@ void AES_CBC_decrypt(const unsigned char* in, unsigned char* out, unsigned long
22052205
#endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER || HAVE_AES_CBC */
22062206
#endif /* HAVE_AES_DECRYPT */
22072207
#ifdef HAVE_AESGCM
2208-
static const uint32_t L_GCM_gmult_len_r[] = {
2208+
XALIGNED(16) static const uint32_t L_GCM_gmult_len_r[] = {
22092209
0x00000000, 0x1c200000, 0x38400000, 0x24600000,
22102210
0x70800000, 0x6ca00000, 0x48c00000, 0x54e00000,
22112211
0xe1000000, 0xfd200000, 0xd9400000, 0xc5600000,

wolfcrypt/src/port/arm/thumb2-sha256-asm_c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#include <wolfssl/wolfcrypt/sha256.h>
5555

5656
#ifdef WOLFSSL_ARMASM_NO_NEON
57-
static const uint32_t L_SHA256_transform_len_k[] = {
57+
XALIGNED(16) static const uint32_t L_SHA256_transform_len_k[] = {
5858
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
5959
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
6060
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,

wolfssl/wolfcrypt/types.h

Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,87 +1237,57 @@ typedef struct w64wrapper {
12371237
#ifndef WOLFSSL_USE_ALIGN
12381238
#define WOLFSSL_USE_ALIGN
12391239
#endif
1240-
#endif /* WOLFSSL_AESNI || WOLFSSL_ARMASM || USE_INTEL_SPEEDUP || WOLFSSL_AFALG_XILINX */
1241-
1242-
#ifdef WOLFSSL_USE_ALIGN
1243-
#if !defined(ALIGN16)
1244-
#if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__) || \
1245-
defined(__llvm__)
1246-
#define ALIGN16 __attribute__ ( (aligned (16)))
1247-
#elif defined(_MSC_VER)
1248-
/* disable align warning, we want alignment ! */
1249-
#pragma warning(disable: 4324)
1250-
#define ALIGN16 __declspec (align (16))
1251-
#else
1252-
#define ALIGN16
1253-
#endif
1254-
#endif /* !ALIGN16 */
1255-
1256-
#if !defined (ALIGN32)
1257-
#if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__) || \
1258-
defined(__llvm__)
1259-
#define ALIGN32 __attribute__ ( (aligned (32)))
1260-
#elif defined(_MSC_VER)
1261-
/* disable align warning, we want alignment ! */
1262-
#pragma warning(disable: 4324)
1263-
#define ALIGN32 __declspec (align (32))
1264-
#else
1265-
#define ALIGN32
1266-
#endif
1267-
#endif /* !ALIGN32 */
1268-
1269-
#if !defined(ALIGN64)
1270-
#if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__) || \
1271-
defined(__llvm__)
1272-
#define ALIGN64 __attribute__ ( (aligned (64)))
1273-
#elif defined(_MSC_VER)
1274-
/* disable align warning, we want alignment ! */
1275-
#pragma warning(disable: 4324)
1276-
#define ALIGN64 __declspec (align (64))
1277-
#else
1278-
#define ALIGN64
1279-
#endif
1280-
#endif /* !ALIGN64 */
1281-
1282-
#if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__) || \
1283-
defined(__llvm__)
1284-
#define ALIGN128 __attribute__ ( (aligned (128)))
1240+
#endif /* WOLFSSL_AESNI || WOLFSSL_ARMASM || USE_INTEL_SPEEDUP || \
1241+
* WOLFSSL_AFALG_XILINX */
1242+
1243+
/* Helpers for memory alignment */
1244+
#ifndef XALIGNED
1245+
#if defined(__GNUC__) || defined(__llvm__) || \
1246+
defined(__IAR_SYSTEMS_ICC__)
1247+
#define XALIGNED(x) __attribute__ ( (aligned (x)))
1248+
#elif defined(__KEIL__)
1249+
#define XALIGNED(x) __align(x)
12851250
#elif defined(_MSC_VER)
12861251
/* disable align warning, we want alignment ! */
12871252
#pragma warning(disable: 4324)
1288-
#define ALIGN128 __declspec (align (128))
1253+
#define XALIGNED(x) __declspec (align (x))
12891254
#else
1290-
#define ALIGN128
1255+
#define XALIGNED(x) /* null expansion */
12911256
#endif
1257+
#endif
12921258

1293-
#if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__) || \
1294-
defined(__llvm__)
1295-
#define ALIGN256 __attribute__ ( (aligned (256)))
1296-
#elif defined(_MSC_VER)
1297-
/* disable align warning, we want alignment ! */
1298-
#pragma warning(disable: 4324)
1299-
#define ALIGN256 __declspec (align (256))
1259+
/* Only use alignment in wolfSSL/wolfCrypt if WOLFSSL_USE_ALIGN is set */
1260+
#ifdef WOLFSSL_USE_ALIGN
1261+
/* For IAR ARM the maximum variable alignment on stack is 8-bytes.
1262+
* Variables declared outside stack (like static globals) can have
1263+
* higher alignment. */
1264+
#if defined(__ICCARM__)
1265+
#define WOLFSSL_ALIGN(x) XALIGNED(8)
13001266
#else
1301-
#define ALIGN256
1267+
#define WOLFSSL_ALIGN(x) XALIGNED(x)
13021268
#endif
1303-
13041269
#else
1305-
#ifndef ALIGN16
1306-
#define ALIGN16
1307-
#endif
1308-
#ifndef ALIGN32
1309-
#define ALIGN32
1310-
#endif
1311-
#ifndef ALIGN64
1312-
#define ALIGN64
1313-
#endif
1314-
#ifndef ALIGN128
1315-
#define ALIGN128
1316-
#endif
1317-
#ifndef ALIGN256
1318-
#define ALIGN256
1319-
#endif
1320-
#endif /* WOLFSSL_USE_ALIGN */
1270+
#define WOLFSSL_ALIGN(x) /* null expansion */
1271+
#endif
1272+
1273+
#ifndef ALIGN8
1274+
#define ALIGN8 WOLFSSL_ALIGN(8)
1275+
#endif
1276+
#ifndef ALIGN16
1277+
#define ALIGN16 WOLFSSL_ALIGN(16)
1278+
#endif
1279+
#ifndef ALIGN32
1280+
#define ALIGN32 WOLFSSL_ALIGN(32)
1281+
#endif
1282+
#ifndef ALIGN64
1283+
#define ALIGN64 WOLFSSL_ALIGN(64)
1284+
#endif
1285+
#ifndef ALIGN128
1286+
#define ALIGN128 WOLFSSL_ALIGN(128)
1287+
#endif
1288+
#ifndef ALIGN256
1289+
#define ALIGN256 WOLFSSL_ALIGN(256)
1290+
#endif
13211291

13221292
#if !defined(PEDANTIC_EXTENSION)
13231293
#if defined(__GNUC__)

0 commit comments

Comments
 (0)