Fix NXP MMCAU when data pointer is not aligned.

This commit is contained in:
David Garske
2019-10-29 13:41:28 -07:00
parent 6bc16a4acb
commit 7f42b71571
3 changed files with 74 additions and 2 deletions

View File

@ -115,6 +115,10 @@ int wc_Md5Final(wc_Md5* md5, byte* hash)
#define XTRANSFORM(S,B) Transform((S), (B))
#define XTRANSFORM_LEN(S,B,L) Transform_Len((S), (B), (L))
#if !defined(WC_HASH_DATA_ALIGNMENT) && defined(WOLFSSL_MMCAU_ALIGNMENT)
#define WC_HASH_DATA_ALIGNMENT WOLFSSL_MMCAU_ALIGNMENT
#endif
static int Transform(wc_Md5* md5, const byte* data)
{
int ret = wolfSSL_CryptHwMutexLock();
@ -133,6 +137,25 @@ static int Transform_Len(wc_Md5* md5, const byte* data, word32 len)
{
int ret = wolfSSL_CryptHwMutexLock();
if (ret == 0) {
#if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
if ((size_t)data % WC_HASH_DATA_ALIGNMENT) {
/* data pointer is NOT aligned,
* so copy and perform one block at a time */
byte* local = (byte*)md5->buffer;
while (len >= WC_MD5_BLOCK_SIZE) {
XMEMCPY(local, data, WC_MD5_BLOCK_SIZE);
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_md5_hash_n(local, 1, (unsigned char*)md5->digest);
#else
MMCAU_MD5_HashN(local, 1, (uint32_t*)md5->digest);
#endif
data += WC_MD5_BLOCK_SIZE;
len -= WC_MD5_BLOCK_SIZE;
}
}
else
#endif
{
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_md5_hash_n((byte*)data, len / WC_MD5_BLOCK_SIZE,
(unsigned char*)md5->digest);
@ -140,6 +163,7 @@ static int Transform_Len(wc_Md5* md5, const byte* data, word32 len)
MMCAU_MD5_HashN((byte*)data, len / WC_MD5_BLOCK_SIZE,
(uint32_t*)md5->digest);
#endif
}
wolfSSL_CryptHwMutexUnLock();
}
return ret;

View File

@ -209,6 +209,10 @@
#define XTRANSFORM(S,B) Transform((S),(B))
#define XTRANSFORM_LEN(S,B,L) Transform_Len((S),(B),(L))
#if !defined(WC_HASH_DATA_ALIGNMENT) && defined(WOLFSSL_MMCAU_ALIGNMENT)
#define WC_HASH_DATA_ALIGNMENT WOLFSSL_MMCAU_ALIGNMENT
#endif
static int InitSha(wc_Sha* sha)
{
int ret = 0;
@ -233,7 +237,7 @@
static int Transform(wc_Sha* sha, const byte* data)
{
int ret = wolfSSL_CryptHwMutexLock();
if(ret == 0) {
if (ret == 0) {
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_sha1_hash_n((byte*)data, 1, sha->digest);
#else
@ -247,13 +251,33 @@
static int Transform_Len(wc_Sha* sha, const byte* data, word32 len)
{
int ret = wolfSSL_CryptHwMutexLock();
if(ret == 0) {
if (ret == 0) {
#if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
if ((size_t)data % WC_HASH_DATA_ALIGNMENT) {
/* data pointer is NOT aligned,
* so copy and perform one block at a time */
byte* local = (byte*)sha->buffer;
while (len >= WC_SHA_BLOCK_SIZE) {
XMEMCPY(local, data, WC_SHA_BLOCK_SIZE);
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_sha1_hash_n(local, 1, sha->digest);
#else
MMCAU_SHA1_HashN(local, 1, sha->digest);
#endif
data += WC_SHA_BLOCK_SIZE;
len -= WC_SHA_BLOCK_SIZE;
}
}
else
#endif
{
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_sha1_hash_n((byte*)data, len/WC_SHA_BLOCK_SIZE, sha->digest);
#else
MMCAU_SHA1_HashN((byte*)data, len/WC_SHA_BLOCK_SIZE,
(uint32_t*)sha->digest);
#endif
}
wolfSSL_CryptHwMutexUnLock();
}
return ret;

View File

@ -394,6 +394,10 @@ static int InitSha256(wc_Sha256* sha256)
#define XTRANSFORM(S, D) Transform_Sha256((S),(D))
#define XTRANSFORM_LEN(S, D, L) Transform_Sha256_Len((S),(D),(L))
#if !defined(WC_HASH_DATA_ALIGNMENT) && defined(WOLFSSL_MMCAU_ALIGNMENT)
#define WC_HASH_DATA_ALIGNMENT WOLFSSL_MMCAU_ALIGNMENT
#endif
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
{
int ret = 0;
@ -438,6 +442,25 @@ static int InitSha256(wc_Sha256* sha256)
{
int ret = wolfSSL_CryptHwMutexLock();
if (ret == 0) {
#if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
if ((size_t)data % WC_HASH_DATA_ALIGNMENT) {
/* data pointer is NOT aligned,
* so copy and perform one block at a time */
byte* local = (byte*)sha256->buffer;
while (len >= WC_SHA256_BLOCK_SIZE) {
XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_sha256_hash_n(local, 1, sha256->digest);
#else
MMCAU_SHA256_HashN(local, 1, sha256->digest);
#endif
data += WC_SHA256_BLOCK_SIZE;
len -= WC_SHA256_BLOCK_SIZE;
}
}
else
#endif
{
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
sha256->digest);
@ -445,6 +468,7 @@ static int InitSha256(wc_Sha256* sha256)
MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
sha256->digest);
#endif
}
wolfSSL_CryptHwMutexUnLock();
}
return ret;