#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#if defined(WOLFSSL_CAAM) && defined(WOLFSSL_CAAM_HASH) \
&& !defined(WOLFSSL_IMXRT1170_CAAM)
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#if defined(__INTEGRITY) || defined(INTEGRITY)
#include <INTEGRITY.h>
#endif
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#include <wolfssl/wolfcrypt/port/caam/wolfcaam_hash.h>
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
#include <stdio.h>
#endif
#ifndef NO_SHA
#include <wolfssl/wolfcrypt/sha.h>
#endif
#if !defined(NO_SHA256) || defined(WOLFSSL_SHA224)
#include <wolfssl/wolfcrypt/sha256.h>
#endif
#if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
#include <wolfssl/wolfcrypt/sha512.h>
#endif
#ifndef NO_MD5
#include <wolfssl/wolfcrypt/md5.h>
#endif
#ifndef WC_CAAM_CTXLEN
#define WC_CAAM_CTXLEN 0
#endif
#ifndef WOLFSSL_HASH_KEEP
static int _ShaUpdate(byte* buffer, word32* buffLen, const byte* ctx,
const byte* data, word32 len, word32 digestSz, word32 type)
{
CAAM_BUFFER buf[2];
word32 arg[4];
int ret;
int idx = 0;
if (buffer == NULL || ctx == NULL || (data == NULL && len > 0)) {
return BAD_FUNC_ARG;
}
if (len == 0) return 0;
if (*buffLen > 0) {
word32 add = min(len, WC_CAAM_HASH_BLOCK - *buffLen);
XMEMCPY(&buffer[*buffLen], data, add);
*buffLen += add;
data += add;
len -= add;
if (*buffLen == WC_CAAM_HASH_BLOCK) {
buf[idx].BufferType = DataBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[idx].Length = digestSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[idx].Transferred = 0;
#endif
idx++;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)sha->buffer;
buf[idx].Length = *buffLen;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_UPDATE;
arg[1] = digestSz + WC_CAAM_CTXLEN;
if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA update");
return ret;
}
*buffLen = 0;
}
}
if (len >= WC_CAAM_HASH_BLOCK) {
word32 sz = len / WC_CAAM_HASH_BLOCK;
sz = sz * WC_CAAM_HASH_BLOCK;
idx = 0;
buf[idx].BufferType = DataBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[idx].Length = digestSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[idx].Transferred = 0;
#endif
idx++;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)data;
buf[idx].Length = sz;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_UPDATE;
arg[1] = digestSz + WC_CAAM_CTXLEN;
if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA update");
return ret;
}
len -= sz;
data += sz;
}
if (len > 0) {
word32 add = min(len, WC_CAAM_HASH_BLOCK - *buffLen);
XMEMCPY(&buffer[*buffLen], data, add);
*buffLen += add;
}
return 0;
}
#endif
static int _ShaFinal(byte* ctx, word32 ctxSz, byte* in, word32 inSz, byte* out,
word32 type)
{
CAAM_BUFFER buf[2];
word32 arg[4];
int ret, idx = 0;
if (ctx == NULL || out == NULL) {
return BAD_FUNC_ARG;
}
buf[idx].BufferType = DataBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)ctx;
buf[idx].Length = ctxSz;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[idx].Transferred = 0;
#endif
idx++;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)in;
buf[idx].Length = inSz;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_FINAL;
arg[1] = ctxSz + WC_CAAM_CTXLEN;
if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA Final");
return ret;
}
XMEMCPY(out, ctx, ctxSz);
return 0;
}
#if !defined(NO_SHA)
int wc_CAAM_ShaHash(wc_Sha* sha, const byte* in, word32 inSz, byte* digest)
{
int ret = 0;
if (in != NULL) {
#ifdef WOLFSSL_HASH_KEEP
ret = _wc_Hash_Grow(&(sha->msg), &(sha->used), &(sha->len), in,
inSz, sha->heap);
#else
ret = _ShaUpdate((byte*)sha->buffer, &sha->buffLen, (byte*)sha->digest,
in, inSz, SHA_DIGEST_SIZE, CAAM_SHA);
#endif
}
if (digest != NULL) {
#ifdef WOLFSSL_HASH_KEEP
int devId = sha->devId;
void* heap = sha->heap;
ret = _ShaFinal((byte*)sha->digest, SHA_DIGEST_SIZE, sha->msg,
sha->used, digest, CAAM_SHA);
wc_ShaFree(sha);
wc_InitSha_ex(sha, heap, devId);
#else
ret = _ShaFinal((byte*)sha->digest, SHA_DIGEST_SIZE,
(byte*)sha->buffer, sha->buffLen, digest, CAAM_SHA);
#endif
}
return ret;
}
#endif
#ifdef WOLFSSL_SHA224
int wc_CAAM_Sha224Hash(wc_Sha224* sha224, const byte* in, word32 inSz,
byte* digest)
{
int ret = 0;
if (in != NULL) {
#ifdef WOLFSSL_HASH_KEEP
ret = wc_Sha224_Grow(sha224, in, inSz);
#else
ret = _ShaUpdate(sha224->buffer, &sha224->bufferLen,
(byte*)sha224->digest, in, inSz, SHA224_DIGEST_SIZE, CAAM_SHA224);
#endif
}
if (digest != NULL) {
#ifdef WOLFSSL_HASH_KEEP
int devId = sha224->devId;
void* heap = sha224->heap;
ret = _ShaFinal((byte*)sha224->digest, SHA224_DIGEST_SIZE, sha224->msg,
sha224->used, digest, CAAM_SHA224);
wc_Sha224Free(sha224);
wc_InitSha224_ex(sha224, heap, devId);
#else
ret = _ShaFinal((byte*)sha224->digest, SHA224_DIGEST_SIZE,
(byte*)sha224->buffer, sha224->bufferLen, digest, CAAM_SHA224);
#endif
}
return ret;
}
#endif
#if !defined(NO_SHA256)
int wc_CAAM_Sha256Hash(wc_Sha256* sha256, const byte* in, word32 inSz,
byte* digest)
{
int ret = 0;
if (in != NULL) {
#ifdef WOLFSSL_HASH_KEEP
ret = wc_Sha256_Grow(sha256, in, inSz);
#else
ret = _ShaUpdate((byte*)sha256->buffer, &sha256->buffLen,
(byte*)sha256->digest, in, inSz, SHA256_DIGEST_SIZE, CAAM_SHA256);
#endif
}
if (digest != NULL) {
#ifdef WOLFSSL_HASH_KEEP
int devId = sha256->devId;
void* heap = sha256->heap;
ret = _ShaFinal((byte*)sha256->digest, SHA256_DIGEST_SIZE, sha256->msg,
sha256->used, digest, CAAM_SHA256);
wc_Sha256Free(sha256);
wc_InitSha256_ex(sha256, heap, devId);
#else
ret = _ShaFinal((byte*)sha256->digest, SHA256_DIGEST_SIZE,
(byte*)sha256->buffer, sha256->buffLen, digest, CAAM_SHA256);
#endif
}
return ret;
}
#endif
#ifdef WOLFSSL_SHA384
int wc_CAAM_Sha384Hash(wc_Sha384* sha384, const byte* in, word32 inSz,
byte* digest)
{
int ret = 0;
if (in != NULL) {
#ifdef WOLFSSL_HASH_KEEP
ret = wc_Sha384_Grow(sha384, in, inSz);
#else
ret = _ShaUpdate((byte*)sha384->buffer, &sha384->buffLen,
(byte*)sha384->digest, in, inSz, SHA384_DIGEST_SIZE, CAAM_SHA384);
#endif
}
if (digest != NULL) {
#ifdef WOLFSSL_HASH_KEEP
int devId = sha384->devId;
void* heap = sha384->heap;
ret = _ShaFinal((byte*)sha384->digest, SHA384_DIGEST_SIZE, sha384->msg,
sha384->used, digest, CAAM_SHA384);
wc_Sha384Free(sha384);
wc_InitSha384_ex(sha384, heap, devId);
#else
ret = _ShaFinal((byte*)sha384->digest, SHA384_DIGEST_SIZE,
(byte*)sha384->buffer, sha384->buffLen, digest, CAAM_SHA384);
#endif
}
return ret;
}
#endif
#ifdef WOLFSSL_SHA512
int wc_CAAM_Sha512Hash(wc_Sha512* sha512, const byte* in, word32 inSz,
byte* digest)
{
int ret = 0;
if (in != NULL) {
#ifdef WOLFSSL_HASH_KEEP
ret = wc_Sha512_Grow(sha512, in, inSz);
#else
ret = _ShaUpdate((byte*)sha512->buffer, &sha512->buffLen,
(byte*)sha512->digest, in, inSz, SHA512_DIGEST_SIZE, CAAM_SHA512);
#endif
}
if (digest != NULL) {
#ifdef WOLFSSL_HASH_KEEP
int devId = sha512->devId;
void* heap = sha512->heap;
ret = _ShaFinal((byte*)sha512->digest, SHA512_DIGEST_SIZE, sha512->msg,
sha512->used, digest, CAAM_SHA512);
wc_Sha512Free(sha512);
wc_InitSha512_ex(sha512, heap, devId);
#else
ret = _ShaFinal((byte*)sha512->digest, SHA512_DIGEST_SIZE,
(byte*)sha512->buffer, sha512->buffLen, digest, CAAM_SHA512);
#endif
}
return ret;
}
#endif
#endif