mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 20:24:39 +02:00
buffering hash message, pic32mz-hash.c
This commit is contained in:
@@ -36,89 +36,194 @@
|
|||||||
|
|
||||||
#if !defined(NO_MD5) && !defined(NO_SHA) && !defined(NO_SHA256)
|
#if !defined(NO_MD5) && !defined(NO_SHA) && !defined(NO_SHA256)
|
||||||
|
|
||||||
static void reset_engine(pic32mz_desc *desc_l, int algo)
|
static uint8_t dataBuffer[PIC32MZ_MAX_BD][PIC32_BLOCK_SIZE] __attribute__((aligned (4), coherent));
|
||||||
|
|
||||||
|
static void reset_engine(pic32mz_desc *desc, int algo)
|
||||||
{
|
{
|
||||||
pic32mz_desc *desc ;
|
int i;
|
||||||
desc = KVA0_TO_KVA1(desc_l) ;
|
pic32mz_desc* uc_desc = KVA0_TO_KVA1(desc);
|
||||||
|
|
||||||
CECON = 1 << 6;
|
CECON = 1 << 6;
|
||||||
while (CECON);
|
while (CECON);
|
||||||
|
|
||||||
/* Make sure everything is clear first before we make settings. */
|
/* Make sure everything is clear first before we make settings. */
|
||||||
XMEMSET((void *)KVA0_TO_KVA1(&desc->sa), 0, sizeof(desc->sa));
|
XMEMSET((void *)&uc_desc->sa, 0, sizeof(uc_desc->sa));
|
||||||
XMEMSET((void *)KVA0_TO_KVA1(&desc->bd[0]), 0, sizeof(desc->bd[0]));
|
|
||||||
XMEMSET((void *)KVA0_TO_KVA1(&desc->bd[1]), 0, sizeof(desc->bd[1]));
|
|
||||||
|
|
||||||
/* Set up the security association */
|
/* Set up the security association */
|
||||||
desc->sa.SA_CTRL.ALGO = algo ;
|
uc_desc->sa.SA_CTRL.ALGO = algo ;
|
||||||
desc->sa.SA_CTRL.LNC = 1;
|
uc_desc->sa.SA_CTRL.LNC = 1;
|
||||||
desc->sa.SA_CTRL.FB = 1;
|
uc_desc->sa.SA_CTRL.FB = 1;
|
||||||
desc->sa.SA_CTRL.ENCTYPE = 1;
|
uc_desc->sa.SA_CTRL.ENCTYPE = 1;
|
||||||
desc->sa.SA_CTRL.LOADIV = 1;
|
uc_desc->sa.SA_CTRL.LOADIV = 1;
|
||||||
|
|
||||||
/* Set up the buffer descriptor */
|
/* Set up the buffer descriptor */
|
||||||
desc->err = 0 ;
|
uc_desc->err = 0 ;
|
||||||
desc->bd[0].BD_CTRL.LAST_BD = 1;
|
for (i = 0; i < PIC32MZ_MAX_BD; i++)
|
||||||
desc->bd[0].BD_CTRL.LIFM = 1;
|
{
|
||||||
desc->bd[0].SA_ADDR = KVA_TO_PA(&desc->sa);
|
XMEMSET((void *)&uc_desc->bd[i], 0, sizeof(uc_desc->bd[i]));
|
||||||
desc->bd[1].BD_CTRL.LAST_BD = 1;
|
uc_desc->bd[i].BD_CTRL.LAST_BD = 1;
|
||||||
desc->bd[1].BD_CTRL.LIFM = 1;
|
uc_desc->bd[i].BD_CTRL.LIFM = 1;
|
||||||
desc->bd[1].SA_ADDR = KVA_TO_PA(&desc->sa);
|
uc_desc->bd[i].BD_CTRL.PKT_INT_EN = 1;
|
||||||
desc_l->bdCount = 0 ;
|
uc_desc->bd[i].SA_ADDR = KVA_TO_PA(&uc_desc->sa);
|
||||||
|
uc_desc->bd[i].SRCADDR = KVA_TO_PA(&dataBuffer[i]);
|
||||||
|
if (PIC32MZ_MAX_BD > i+1)
|
||||||
|
uc_desc->bd[i].NXTPTR = KVA_TO_PA(&uc_desc->bd[i+1]);
|
||||||
|
else
|
||||||
|
uc_desc->bd[i].NXTPTR = KVA_TO_PA(&uc_desc->bd[0]);
|
||||||
|
XMEMSET((void *)&dataBuffer[i], 0, PIC32_BLOCK_SIZE);
|
||||||
|
}
|
||||||
|
uc_desc->bd[0].BD_CTRL.SA_FETCH_EN = 1; // Fetch the security association on the first BD
|
||||||
|
desc->dbPtr = 0;
|
||||||
|
desc->currBd = 0;
|
||||||
|
desc->msgSize = 0;
|
||||||
|
desc->processed = 0;
|
||||||
CEBDPADDR = KVA_TO_PA(&(desc->bd[0]));
|
CEBDPADDR = KVA_TO_PA(&(desc->bd[0]));
|
||||||
|
|
||||||
|
CEPOLLCON = 10;
|
||||||
|
|
||||||
CECON = 0x27;
|
CECON = 0x27;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define PIC32MZ_IF_RAM(addr) (KVA_TO_PA(addr) < 0x80000)
|
#define PIC32MZ_IF_RAM(addr) (KVA_TO_PA(addr) < 0x80000)
|
||||||
|
|
||||||
static void update_engine(pic32mz_desc *desc_l, const char *input, word32 len,
|
static void update_data_size(pic32mz_desc *desc, word32 msgSize)
|
||||||
|
{
|
||||||
|
desc->msgSize = msgSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_engine(pic32mz_desc *desc, const char *input, word32 len,
|
||||||
word32 *hash)
|
word32 *hash)
|
||||||
{
|
{
|
||||||
pic32mz_desc *desc ;
|
int total ;
|
||||||
int i ;
|
pic32mz_desc *uc_desc = KVA0_TO_KVA1(desc);
|
||||||
int total ;
|
|
||||||
desc = KVA0_TO_KVA1(desc_l) ;
|
|
||||||
|
|
||||||
i = desc_l->bdCount ;
|
uc_desc->bd[desc->currBd].UPDPTR = KVA_TO_PA(hash);
|
||||||
if(i >= PIC32MZ_MAX_BD) {
|
// Add the data to the current buffer. If the buffer fills, start processing it
|
||||||
desc_l->err = 1 ;
|
// and fill the next one.
|
||||||
return ;
|
while (len)
|
||||||
|
{
|
||||||
|
// If the engine is processing the current BD, spin.
|
||||||
|
// if (uc_desc->bd[desc->currBd].BD_CTRL.DESC_EN)
|
||||||
|
// continue;
|
||||||
|
if (desc->msgSize)
|
||||||
|
{
|
||||||
|
// If we've been given the message size, we can process along the
|
||||||
|
// way.
|
||||||
|
// Enable the current buffer descriptor if it is full.
|
||||||
|
if (desc->dbPtr >= PIC32_BLOCK_SIZE)
|
||||||
|
{
|
||||||
|
// Wrap up the buffer descriptor and enable it so the engine can process
|
||||||
|
uc_desc->bd[desc->currBd].MSGLEN = desc->msgSize;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.BUFLEN = desc->dbPtr;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.LAST_BD = 0;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.LIFM = 0;
|
||||||
|
//SYS_DEVCON_DataCacheClean((word32)desc, sizeof(pic32mz_desc));
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.DESC_EN = 1;
|
||||||
|
// Move to the next buffer descriptor, or wrap around.
|
||||||
|
desc->currBd++;
|
||||||
|
if (desc->currBd >= PIC32MZ_MAX_BD)
|
||||||
|
desc->currBd = 0;
|
||||||
|
// Wait until the engine has processed the new BD.
|
||||||
|
while (uc_desc->bd[desc->currBd].BD_CTRL.DESC_EN);
|
||||||
|
uc_desc->bd[desc->currBd].UPDPTR = KVA_TO_PA(hash);
|
||||||
|
desc->dbPtr = 0;
|
||||||
|
}
|
||||||
|
if (!PIC32MZ_IF_RAM(input)) // If we're inputting from flash, let the BD have the address and max the buffer size
|
||||||
|
{
|
||||||
|
uc_desc->bd[desc->currBd].SRCADDR = KVA_TO_PA(input);
|
||||||
|
total = (len > PIC32MZ_MAX_BLOCK ? PIC32MZ_MAX_BLOCK : len);
|
||||||
|
desc->dbPtr = total;
|
||||||
|
len -= total;
|
||||||
|
input += total;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (len > PIC32_BLOCK_SIZE - desc->dbPtr)
|
||||||
|
{
|
||||||
|
// We have more data than can be put in the buffer. Fill what we can.
|
||||||
|
total = PIC32_BLOCK_SIZE - desc->dbPtr;
|
||||||
|
XMEMCPY(&dataBuffer[desc->currBd][desc->dbPtr], input, total);
|
||||||
|
len -= total;
|
||||||
|
desc->dbPtr = PIC32_BLOCK_SIZE;
|
||||||
|
input += total;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Fill up what we have, but don't turn on the engine.
|
||||||
|
XMEMCPY(&dataBuffer[desc->currBd][desc->dbPtr], input, len);
|
||||||
|
desc->dbPtr += len;
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We have to buffer everything and keep track of how much has been
|
||||||
|
// added in order to get a total size. If the buffer fills, we move
|
||||||
|
// to the next one. If we try to add more when the last buffer is
|
||||||
|
// full, we error out.
|
||||||
|
if (desc->dbPtr == PIC32_BLOCK_SIZE)
|
||||||
|
{
|
||||||
|
// We filled the last BD buffer, so move on to the next one
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.LAST_BD = 0;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.LIFM = 0;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.BUFLEN = PIC32_BLOCK_SIZE;
|
||||||
|
desc->currBd++;
|
||||||
|
uc_desc->bd[desc->currBd].UPDPTR = KVA_TO_PA(hash);
|
||||||
|
desc->dbPtr = 0;
|
||||||
|
if (desc->currBd >= PIC32MZ_MAX_BD)
|
||||||
|
{
|
||||||
|
desc->err = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (len > PIC32_BLOCK_SIZE - desc->dbPtr)
|
||||||
|
{
|
||||||
|
// We have more data than can be put in the buffer. Fill what we can.
|
||||||
|
total = PIC32_BLOCK_SIZE - desc->dbPtr;
|
||||||
|
XMEMCPY(&dataBuffer[desc->currBd][desc->dbPtr], input, total);
|
||||||
|
len -= total;
|
||||||
|
desc->processed += total;
|
||||||
|
desc->dbPtr = PIC32_BLOCK_SIZE;
|
||||||
|
input += total;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Fill up what we have
|
||||||
|
XMEMCPY(&dataBuffer[desc->currBd][desc->dbPtr], input, len);
|
||||||
|
desc->dbPtr += len;
|
||||||
|
desc->processed += len;
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(PIC32MZ_IF_RAM(input))
|
|
||||||
XMEMCPY(KVA0_TO_KVA1(input), input, len) ; /* Sync phys with cache */
|
|
||||||
desc->bd[i].SRCADDR = KVA_TO_PA(input);
|
|
||||||
/* Finally, turn on the buffer descriptor */
|
|
||||||
if (len % 4)
|
|
||||||
desc->bd[i].BD_CTRL.BUFLEN = (len + 4) - (len % 4);
|
|
||||||
else desc->bd[i].BD_CTRL.BUFLEN = len ;
|
|
||||||
|
|
||||||
if(i == 0) {
|
|
||||||
desc->bd[i].MSGLEN = len ;
|
|
||||||
desc->bd[i].BD_CTRL.SA_FETCH_EN = 1;
|
|
||||||
} else {
|
|
||||||
desc->bd[i-1].NXTPTR = KVA_TO_PA(&(desc->bd[i])) ;
|
|
||||||
desc->bd[i].BD_CTRL.DESC_EN = 1;
|
|
||||||
desc->bd[i-1].BD_CTRL.LAST_BD = 0 ;
|
|
||||||
desc->bd[i-1].BD_CTRL.LIFM = 0 ;
|
|
||||||
total = desc->bd[i-1].MSGLEN + len ;
|
|
||||||
desc->bd[i].MSGLEN = total ;
|
|
||||||
desc->bd[i-1].MSGLEN = total ;
|
|
||||||
}
|
|
||||||
desc->bd[i].UPDPTR = KVA_TO_PA(hash);
|
|
||||||
desc_l->bdCount ++ ;
|
|
||||||
|
|
||||||
#ifdef DEBUG_CYASSL
|
|
||||||
printf("Input[bd=%d, len=%d]:%x->\"%s\"\n", desc_l->bdCount, len, input, input) ;
|
|
||||||
print_mem(input, len+4) ;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void start_engine(pic32mz_desc *desc) {
|
static void start_engine(pic32mz_desc *desc) {
|
||||||
bufferDescriptor *hash_bd[2] ;
|
// Wrap up the last buffer descriptor and enable it
|
||||||
hash_bd[0] = (bufferDescriptor *)KVA0_TO_KVA1(&(desc->bd[0])) ;
|
int i ;
|
||||||
hash_bd[0]->BD_CTRL.DESC_EN = 1;
|
int bufferLen ;
|
||||||
|
pic32mz_desc *uc_desc = KVA0_TO_KVA1(desc);
|
||||||
|
|
||||||
|
bufferLen = desc->dbPtr;
|
||||||
|
if (bufferLen % 4)
|
||||||
|
bufferLen = (bufferLen + 4) - (bufferLen % 4);
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.BUFLEN = bufferLen;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.LAST_BD = 1;
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.LIFM = 1;
|
||||||
|
if (desc->msgSize == 0)
|
||||||
|
{
|
||||||
|
// We were not given the size, so now we have to go through every BD
|
||||||
|
// and give it what will be processed, and enable them.
|
||||||
|
for (i = desc->currBd; i >= 0; i--)
|
||||||
|
{
|
||||||
|
uc_desc->bd[i].MSGLEN = desc->processed;
|
||||||
|
uc_desc->bd[i].BD_CTRL.DESC_EN = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uc_desc->bd[desc->currBd].BD_CTRL.DESC_EN = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wait_engine(pic32mz_desc *desc, char *hash, int hash_sz) {
|
void wait_engine(pic32mz_desc *desc, char *hash, int hash_sz) {
|
||||||
@@ -183,6 +288,12 @@ void wc_Md5Final(Md5* md5, byte* hash)
|
|||||||
XMEMCPY(hash, md5->digest, MD5_HASH_SIZE) ;
|
XMEMCPY(hash, md5->digest, MD5_HASH_SIZE) ;
|
||||||
wc_InitMd5(md5); /* reset state */
|
wc_InitMd5(md5); /* reset state */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Md5SizeSet(Md5* md5, word32 len)
|
||||||
|
{
|
||||||
|
WOLFSSL_ENTER("Md5SizeSet\n");
|
||||||
|
md5->desc.msgSize = len;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef NO_SHA
|
#ifndef NO_SHA
|
||||||
@@ -212,6 +323,11 @@ int wc_ShaFinal(Sha* sha, byte* hash)
|
|||||||
wc_InitSha(sha); /* reset state */
|
wc_InitSha(sha); /* reset state */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ShaSizeSet(Sha* sha, word32 len)
|
||||||
|
{
|
||||||
|
sha->desc.msgSize = len;
|
||||||
|
}
|
||||||
#endif /* NO_SHA */
|
#endif /* NO_SHA */
|
||||||
|
|
||||||
#ifndef NO_SHA256
|
#ifndef NO_SHA256
|
||||||
@@ -242,6 +358,13 @@ int wc_Sha256Final(Sha256* sha256, byte* hash)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Sha256SizeSet(Sha256* sha256, word32 len)
|
||||||
|
{
|
||||||
|
WOLFSSL_ENTER("Sha256SizeSet\n");
|
||||||
|
sha256->desc.msgSize = len;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* NO_SHA256 */
|
#endif /* NO_SHA256 */
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user