Skip to content

Commit 9bc259a

Browse files
Enable hardware acceleration for AES on PSoC6.
- Implemented AES ECB, CBC, CFB, and GCM modes with hardware acceleration. - Ensured proper mutex locking for concurrent access to hardware resources during - Adjusted the aes.h header to include PSoC6 specific definitions and structures. - Updated README for PSoC6 port.
1 parent 10a60fc commit 9bc259a

File tree

6 files changed

+1296
-43
lines changed

6 files changed

+1296
-43
lines changed

wolfcrypt/src/aes.c

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
9393
#include <wolfcrypt/src/port/ti/ti-aes.c>
9494
#else
9595

96+
97+
#if defined(WOLFSSL_PSOC6_CRYPTO)
98+
#include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
99+
#endif /* WOLFSSL_PSOC6_CRYPTO */
100+
96101
#ifdef NO_INLINE
97102
#include <wolfssl/wolfcrypt/misc.h>
98103
#else
@@ -1118,6 +1123,24 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(Aes* aes, const byte* inBlock,
11181123
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
11191124
/* implemented in wolfcrypt/src/port/silabs/silabs_aes.c */
11201125

1126+
#elif defined(WOLFSSL_PSOC6_CRYPTO)
1127+
1128+
#if (defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT))
1129+
static WARN_UNUSED_RESULT int wc_AesEncrypt(
1130+
Aes* aes, const byte* inBlock, byte* outBlock)
1131+
{
1132+
return wc_Psoc6_Aes_Encrypt(aes, inBlock, outBlock);
1133+
}
1134+
#endif
1135+
1136+
#if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT)
1137+
static WARN_UNUSED_RESULT int wc_AesDecrypt(
1138+
Aes* aes, const byte* inBlock, byte* outBlock)
1139+
{
1140+
return wc_Psoc6_Aes_Decrypt(aes, inBlock, outBlock);
1141+
}
1142+
1143+
#endif
11211144
#else
11221145

11231146
/* using wolfCrypt software implementation */
@@ -4405,6 +4428,22 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
44054428
return AesSetKey(aes, userKey, keylen, iv, dir);
44064429
}
44074430
#endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER */
4431+
4432+
#elif defined(WOLFSSL_PSOC6_CRYPTO)
4433+
4434+
int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
4435+
const byte* iv, int dir)
4436+
{
4437+
return wc_Psoc6_Aes_SetKey(aes, userKey, keylen, iv, dir);
4438+
}
4439+
4440+
#if defined(WOLFSSL_AES_DIRECT)
4441+
int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
4442+
const byte* iv, int dir)
4443+
{
4444+
return wc_AesSetKey(aes, userKey, keylen, iv, dir);
4445+
}
4446+
#endif /* WOLFSSL_AES_DIRECT */
44084447
#else
44094448
#define NEED_SOFTWARE_AES_SETKEY
44104449
#endif
@@ -6108,6 +6147,20 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
61086147
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_AES)
61096148
/* implemented in wolfcrypt/src/port/psa/psa_aes.c */
61106149

6150+
#elif defined(WOLFSSL_PSOC6_CRYPTO)
6151+
6152+
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
6153+
{
6154+
return wc_Psoc6_Aes_CbcEncrypt(aes, out, in, sz);
6155+
}
6156+
6157+
#if defined(HAVE_AES_DECRYPT)
6158+
int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
6159+
{
6160+
return wc_Psoc6_Aes_CbcDecrypt(aes, out, in, sz);
6161+
}
6162+
#endif /* HAVE_AES_DECRYPT */
6163+
61116164
#else
61126165
/* Reminder: Some HW implementations may also define this as needed.
61136166
* (e.g. for unsupported key length fallback) */
@@ -7237,7 +7290,7 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
72377290
}
72387291
else
72397292
#endif
7240-
#if !defined(FREESCALE_LTC_AES_GCM)
7293+
#if !defined(FREESCALE_LTC_AES_GCM) && !defined(WOLFSSL_PSOC6_CRYPTO)
72417294
if (ret == 0) {
72427295
VECTOR_REGISTERS_PUSH;
72437296
/* AES-NI code generates its own H value, but generate it here too, to
@@ -7275,7 +7328,7 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
72757328
}
72767329
#endif /* GCM_TABLE || GCM_TABLE_4BIT */
72777330
}
7278-
#endif /* FREESCALE_LTC_AES_GCM */
7331+
#endif /* !FREESCALE_LTC_AES_GCM && !WOLFSSL_PSOC6_CRYPTO */
72797332
#endif
72807333

72817334
#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_AFALG_XILINX_AES)
@@ -9380,6 +9433,11 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
93809433
authTag, authTagSz, authIn, authInSz);
93819434
#endif /* STM32_CRYPTO_AES_GCM */
93829435

9436+
#if defined(WOLFSSL_PSOC6_CRYPTO)
9437+
return wc_Psoc6_Aes_GcmEncrypt(aes, out, in, sz, iv, ivSz, authTag,
9438+
authTagSz, authIn, authInSz);
9439+
#endif /* WOLFSSL_PSOC6_CRYPTO */
9440+
93839441
VECTOR_REGISTERS_PUSH;
93849442

93859443
#if !defined(__aarch64__) && defined(WOLFSSL_ARMASM)
@@ -10060,6 +10118,11 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1006010118
authTag, authTagSz, authIn, authInSz);
1006110119
#endif /* STM32_CRYPTO_AES_GCM */
1006210120

10121+
#if defined(WOLFSSL_PSOC6_CRYPTO)
10122+
return wc_Psoc6_Aes_GcmDecrypt(aes, out, in, sz, iv, ivSz, authTag,
10123+
authTagSz, authIn, authInSz);
10124+
#endif /* WOLFSSL_PSOC6_CRYPTO */
10125+
1006310126
VECTOR_REGISTERS_PUSH;
1006410127

1006510128
#if !defined(__aarch64__) && defined(WOLFSSL_ARMASM)
@@ -13100,6 +13163,30 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
1310013163
return AES_ECB_decrypt(aes, in, out, sz);
1310113164
}
1310213165

13166+
#elif defined(WOLFSSL_PSOC6_CRYPTO)
13167+
13168+
int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
13169+
{
13170+
if ((in == NULL) || (out == NULL) || (aes == NULL))
13171+
return BAD_FUNC_ARG;
13172+
13173+
return wc_Psoc6_Aes_EcbEncrypt(aes, out, in, sz);
13174+
}
13175+
13176+
#define _AesEcbEncrypt(aes, out, in, sz) wc_AesEcbEncrypt(aes, out, in, sz)
13177+
13178+
#ifdef HAVE_AES_DECRYPT
13179+
int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
13180+
{
13181+
if ((in == NULL) || (out == NULL) || (aes == NULL))
13182+
return BAD_FUNC_ARG;
13183+
13184+
return wc_Psoc6_Aes_EcbDecrypt(aes, out, in, sz);
13185+
}
13186+
13187+
#define _AesEcbDecrypt(aes, out, in, sz) wc_AesEcbDecrypt(aes, out, in, sz)
13188+
#endif /* HAVE_AES_DECRYPT */
13189+
1310313190
#else
1310413191

1310513192
/* Software AES - ECB */
@@ -13264,6 +13351,22 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
1326413351
#endif /* HAVE_AES_ECB */
1326513352

1326613353
#if defined(WOLFSSL_AES_CFB)
13354+
13355+
#if defined(WOLFSSL_PSOC6_CRYPTO)
13356+
13357+
int wc_AesCfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
13358+
{
13359+
return wc_Psoc6_Aes_CfbEncrypt(aes, out, in, sz);
13360+
}
13361+
13362+
#ifdef HAVE_AES_DECRYPT
13363+
int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
13364+
{
13365+
return wc_Psoc6_Aes_CfbDecrypt(aes, out, in, sz);
13366+
}
13367+
#endif /* HAVE_AES_DECRYPT */
13368+
13369+
#else
1326713370
/* Feedback AES mode
1326813371
*
1326913372
* aes structure holding key to use for encryption
@@ -13456,6 +13559,7 @@ int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
1345613559
return AesCfbDecrypt_C(aes, out, in, sz, AES_CFB_MODE);
1345713560
}
1345813561
#endif /* HAVE_AES_DECRYPT */
13562+
#endif /* WOLFSSL_PSOC6_CRYPTO */
1345913563

1346013564
#ifndef WOLFSSL_NO_AES_CFB_1_8
1346113565
/* shift the whole WC_AES_BLOCK_SIZE array left by 8 or 1 bits */

wolfcrypt/src/port/cypress/README.md

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PSoC6 Hardware Crypto Port for wolfSSL
22

3-
This directory provides a hardware-accelerated cryptography port for Cypress PSoC6 devices, integrating the PSoC6 hardware crypto block with the wolfSSL cryptography library. The implementation leverages the PSoC6 hardware to accelerate various cryptographic hash and ECC operations, improving performance and reducing CPU load.
3+
This directory provides a hardware-accelerated cryptography port for Cypress PSoC6 devices, integrating the PSoC6 hardware crypto block with the wolfSSL cryptography library. The implementation leverages the PSoC6 hardware to accelerate various cryptographic operations including hash functions, AES encryption/decryption, and ECC verification, improving performance and reducing CPU load.
44

55
## Implemented Features
66

@@ -21,68 +21,126 @@ The following hash algorithms are implemented using the PSoC6 hardware crypto bl
2121

2222
All hash operations are offloaded to the PSoC6 hardware, with mutex protection for thread safety.
2323

24-
### 2. Hardware-Accelerated ECDSA Verification
24+
### 2. Hardware-Accelerated AES Functions
25+
26+
The following AES cipher modes are implemented using the PSoC6 hardware crypto block:
27+
28+
- **AES Block Operations**
29+
- Single-block encryption/decryption: `wc_Psoc6_Aes_Encrypt`, `wc_Psoc6_Aes_Decrypt`
30+
- Direct AES operations: `wc_Psoc6_Aes_EncryptDirect`, `wc_Psoc6_Aes_DecryptDirect` (enabled with `WOLFSSL_AES_DIRECT`)
31+
- Supports AES-128, AES-192, and AES-256 key sizes
32+
33+
- **AES-ECB (Electronic Codebook) Mode**
34+
- Multi-block encryption: `wc_Psoc6_Aes_EcbEncrypt`
35+
- Multi-block decryption: `wc_Psoc6_Aes_EcbDecrypt`
36+
- Enabled with `HAVE_AES_ECB`
37+
38+
- **AES-CBC (Cipher Block Chaining) Mode**
39+
- Multi-block encryption with IV chaining: `wc_Psoc6_Aes_CbcEncrypt`
40+
- Multi-block decryption with IV chaining: `wc_Psoc6_Aes_CbcDecrypt`
41+
- Automatically enabled with `HAVE_AES_CBC`
42+
43+
- **AES-CFB (Cipher Feedback) Mode**
44+
- Stream encryption: `wc_Psoc6_Aes_CfbEncrypt`
45+
- Stream decryption: `wc_Psoc6_Aes_CfbDecrypt`
46+
- Enabled with `WOLFSSL_AES_CFB`
47+
48+
- **AES-GCM (Galois/Counter Mode)**
49+
- Authenticated encryption: `wc_Psoc6_Aes_GcmEncrypt`
50+
- Authenticated decryption with tag verification: `wc_Psoc6_Aes_GcmDecrypt`
51+
- Provides both confidentiality and authenticity
52+
- Enabled with `HAVE_AESGCM`
53+
54+
All AES operations are offloaded to the PSoC6 hardware with mutex protection for thread safety.
55+
### 3. Hardware-Accelerated ECDSA Verification
2556

2657
- **ECDSA Signature Verification**
2758
- Function: `psoc6_ecc_verify_hash_ex`
2859
- Uses PSoC6 hardware to verify ECDSA signatures for supported curves (up to secp521r1).
2960
- Enabled when `HAVE_ECC` is defined.
3061

31-
### 3. Crypto Block Initialization and Resource Management
62+
### 4. Crypto Block Initialization and Resource Management
3263

3364
- **Initialization**
3465
- Function: `psoc6_crypto_port_init`
3566
- Enables the PSoC6 crypto hardware block.
3667
- **Resource Cleanup**
37-
- Function: `wc_Psoc6_Sha_Free`
38-
- Clears and synchronizes the hardware register buffer.
68+
- Hash functions: `wc_Psoc6_Sha_Free` — Clears and synchronizes the hardware register buffer
69+
- AES functions: `wc_Psoc6_Aes_Free` — Frees internal AES buffers and state
3970

4071
## Enable Hardware Acceleration
4172

42-
To enable PSoC6 hardware crypto acceleration for hash and ECC algorithms, ensure the following macros are defined:
73+
To enable PSoC6 hardware crypto acceleration, ensure the following macros are defined:
74+
75+
### Core Macro
76+
- `WOLFSSL_PSOC6_CRYPTO` — Enables the PSoC6 hardware crypto port (required for all features)
4377

44-
- `WOLFSSL_PSOC6_CRYPTO` — Enables the PSoC6 hardware crypto port.
78+
### Hash Function Macros
4579
- The following are defined in `psoc6_crypto.h` and control which hardware hash accelerations are available:
46-
- `PSOC6_HASH_SHA1` — Enables SHA-1 hardware acceleration.
47-
- `PSOC6_HASH_SHA2` — Enables SHA-2 family (SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256) hardware acceleration.
48-
- `PSOC6_HASH_SHA3` — Enables SHA-3 family hardware acceleration.
49-
- To enable the corresponding algorithms in wolfSSL, define the following macros as needed (typically in your `wolfssl/wolfcrypt/settings.h` or build system):
50-
- `WOLFSSL_SHA224` — Enable SHA-224 support.
51-
- `WOLFSSL_SHA384` — Enable SHA-384 support.
52-
- `WOLFSSL_SHA512` — Enable SHA-512, SHA-512/224, SHA-512/256 support.
53-
- `WOLFSSL_SHA3` — Enable SHA-3 support.
54-
- `WOLFSSL_SHAKE128`, `WOLFSSL_SHAKE256` — Enable SHAKE support.
55-
- `HAVE_ECC` — Enable ECC and ECDSA support.
56-
57-
**Example: Enabling SHA-1, SHA-2, and SHA-3 Hardware Acceleration**
80+
- `PSOC6_HASH_SHA1` — Enables SHA-1 hardware acceleration
81+
- `PSOC6_HASH_SHA2` — Enables SHA-2 family hardware acceleration
82+
- `PSOC6_HASH_SHA3` — Enables SHA-3 family hardware acceleration
83+
- To enable the corresponding algorithms in wolfSSL, define these macros (typically in your `wolfssl/wolfcrypt/settings.h` or build system):
84+
- `WOLFSSL_SHA224` — Enable SHA-224 support
85+
- `WOLFSSL_SHA384` — Enable SHA-384 support
86+
- `WOLFSSL_SHA512` — Enable SHA-512, SHA-512/224, SHA-512/256 support
87+
- `WOLFSSL_SHA3` — Enable SHA-3 support
88+
- `WOLFSSL_SHAKE128`, `WOLFSSL_SHAKE256` — Enable SHAKE support
89+
90+
### AES Function Macros
91+
- AES hardware acceleration is automatically enabled when `NO_AES` is not defined
92+
- To enable specific AES modes, define:
93+
- `HAVE_AES_ECB` — Enable AES-ECB mode
94+
- `HAVE_AES_CBC` — Enable AES-CBC mode (typically enabled by default)
95+
- `HAVE_AES_DECRYPT` — Enable AES decryption functions
96+
- `WOLFSSL_AES_DIRECT` — Enable direct AES block operations
97+
- `WOLFSSL_AES_CFB` — Enable AES-CFB mode
98+
- `HAVE_AESGCM` — Enable AES-GCM authenticated encryption
99+
100+
### ECC Function Macros
101+
- `HAVE_ECC` — Enable ECC and ECDSA support
102+
103+
**Example: Enabling Full Hardware Acceleration**
58104

59105
In your build configuration or `wolfssl/wolfcrypt/settings.h`:
60106
```c
61107
#define WOLFSSL_PSOC6_CRYPTO
108+
109+
/* Hash functions */
62110
#define WOLFSSL_SHA224
63111
#define WOLFSSL_SHA384
64112
#define WOLFSSL_SHA512
65113
#define WOLFSSL_SHA3
66114
#define WOLFSSL_SHAKE128
67115
#define WOLFSSL_SHAKE256
116+
117+
/* AES cipher modes */
118+
#define HAVE_AES_ECB
119+
#define HAVE_AES_CBC
120+
#define HAVE_AES_DECRYPT
121+
#define WOLFSSL_AES_DIRECT
122+
#define WOLFSSL_AES_CFB
123+
#define HAVE_AESGCM
124+
125+
/* ECC */
68126
#define HAVE_ECC
69127
```
70-
- No need to define `PSOC6_HASH_SHA1`, `PSOC6_HASH_SHA2`, or `PSOC6_HASH_SHA3` yourself; they are defined in `psoc6_crypto.h`.
128+
- Note: `PSOC6_HASH_SHA1`, `PSOC6_HASH_SHA2`, and `PSOC6_HASH_SHA3` are automatically defined in `psoc6_crypto.h`; you don't need to define them explicitly.
71129

72130
## File Overview
73131

74132
- `psoc6_crypto.h`
75133
Header file declaring the hardware crypto interface and configuration macros.
76134
- `psoc6_crypto.c`
77-
Implementation of the hardware-accelerated hash and ECC functions for PSoC6.
135+
Implementation of the hardware-accelerated hash, AES, and ECC functions for PSoC6.
78136

79137
## Integration Notes
80138

81139
- The port expects the PSoC6 PDL (Peripheral Driver Library) to be available and included in your project.
82140
- The hardware crypto block is initialized on first use; no manual initialization is required unless you wish to call `psoc6_crypto_port_init` directly.
83-
- Hash operations are mutex-protected for thread safety.
141+
- Hash and AES operations are mutex-protected for thread safety.
84142
- ECC hardware operations are not mutex-protected; if you use ECC functions from multiple threads, you must provide your own synchronization.
85-
- The implementation is designed to be compatible with the wolfSSL API, so existing code using wolfSSL hash/ECC functions will automatically benefit from hardware acceleration when enabled.
143+
- The implementation is designed to be compatible with the wolfSSL API, so existing code using wolfSSL hash/AES/ECC functions will automatically benefit from hardware acceleration when enabled.
86144

87145
---
88146

0 commit comments

Comments
 (0)