Skip to content

Commit 8a5a467

Browse files
committed
Patch to support NXP Kinetis MMCAU SHA2-256 (FREESCALE_MMCAU_CLASSIC_SHA) with --enable-armasm.
1 parent 0c9555b commit 8a5a467

1 file changed

Lines changed: 71 additions & 2 deletions

File tree

wolfcrypt/src/port/arm/armv8-sha256.c

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
#include <wolfcrypt/src/misc.c>
4545
#endif
4646

47+
#if defined(FREESCALE_MMCAU_SHA)
48+
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
49+
#include "cau_api.h"
50+
#else
51+
#include "fsl_mmcau.h"
52+
#endif
53+
#endif
4754

4855
#ifndef WOLFSSL_ARMASM_NO_HW_CRYPTO
4956
static const ALIGN32 word32 K[64] = {
@@ -72,6 +79,17 @@ static int InitSha256(wc_Sha256* sha256)
7279
return BAD_FUNC_ARG;
7380
}
7481

82+
#ifdef FREESCALE_MMCAU_SHA
83+
ret = wolfSSL_CryptHwMutexLock();
84+
if (ret == 0) {
85+
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
86+
cau_sha256_initialize_output(sha256->digest);
87+
#else
88+
MMCAU_SHA256_InitializeOutput((uint32_t*)sha256->digest);
89+
#endif
90+
wolfSSL_CryptHwMutexUnLock();
91+
}
92+
#else
7593
sha256->digest[0] = 0x6A09E667L;
7694
sha256->digest[1] = 0xBB67AE85L;
7795
sha256->digest[2] = 0x3C6EF372L;
@@ -80,6 +98,7 @@ static int InitSha256(wc_Sha256* sha256)
8098
sha256->digest[5] = 0x9B05688CL;
8199
sha256->digest[6] = 0x1F83D9ABL;
82100
sha256->digest[7] = 0x5BE0CD19L;
101+
#endif
83102

84103
sha256->buffLen = 0;
85104
sha256->loLen = 0;
@@ -1317,11 +1336,59 @@ static WC_INLINE int Sha256Final(wc_Sha256* sha256, byte* hash)
13171336

13181337
#endif /* __aarch64__ */
13191338

1320-
#else
1339+
#else /* WOLFSSL_ARMASM_NO_HW_CRYPTO */
1340+
1341+
#if defined(FREESCALE_MMCAU_SHA)
1342+
1343+
#ifndef WC_HASH_DATA_ALIGNMENT
1344+
/* these hardware API's require 4 byte (word32) alignment */
1345+
#define WC_HASH_DATA_ALIGNMENT 4
1346+
#endif
1347+
1348+
static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1349+
word32 len)
1350+
{
1351+
int ret = wolfSSL_CryptHwMutexLock();
1352+
if (ret == 0) {
1353+
#if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
1354+
if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
1355+
/* data pointer is NOT aligned,
1356+
* so copy and perform one block at a time */
1357+
byte* local = (byte*)sha256->buffer;
1358+
while (len >= WC_SHA256_BLOCK_SIZE) {
1359+
XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
1360+
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
1361+
cau_sha256_hash_n(local, 1, sha256->digest);
1362+
#else
1363+
MMCAU_SHA256_HashN(local, 1, (uint32_t*)sha256->digest);
1364+
#endif
1365+
data += WC_SHA256_BLOCK_SIZE;
1366+
len -= WC_SHA256_BLOCK_SIZE;
1367+
}
1368+
}
1369+
else
1370+
#endif
1371+
{
1372+
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
1373+
cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
1374+
sha256->digest);
1375+
#else
1376+
MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
1377+
(uint32_t*)sha256->digest);
1378+
#endif
1379+
}
1380+
wolfSSL_CryptHwMutexUnLock();
1381+
}
1382+
return ret;
1383+
}
1384+
1385+
#else /* */
13211386

13221387
extern void Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
13231388
word32 len);
13241389

1390+
#endif
1391+
13251392
/* ARMv8 hardware acceleration Aarch32 and Thumb2 */
13261393
static WC_INLINE int Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
13271394
{
@@ -1429,7 +1496,9 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
14291496
return BAD_FUNC_ARG;
14301497

14311498
sha256->heap = heap;
1432-
(void)devId;
1499+
#ifdef WOLF_CRYPTO_CB
1500+
sha256->devId = devId;
1501+
#endif
14331502

14341503
return InitSha256(sha256);
14351504
}

0 commit comments

Comments
 (0)