Update WolfSSL library to the latest version.

This commit is contained in:
Richard Barry 2015-08-28 13:46:22 +00:00
parent 8af1ad9bac
commit 5a6242fbd0
443 changed files with 70230 additions and 45414 deletions

View file

@ -0,0 +1,371 @@
/* pic32mz-hash.c
*
* Copyright (C) 2006-2015 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifdef WOLFSSL_PIC32MZ_HASH
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/md5.h>
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
#if !defined(NO_MD5) && !defined(NO_SHA) && !defined(NO_SHA256)
static uint8_t dataBuffer[PIC32MZ_MAX_BD][PIC32_BLOCK_SIZE] __attribute__((aligned (4), coherent));
static void reset_engine(pic32mz_desc *desc, int algo)
{
int i;
pic32mz_desc* uc_desc = KVA0_TO_KVA1(desc);
CECON = 1 << 6;
while (CECON);
/* Make sure everything is clear first before we make settings. */
XMEMSET((void *)&uc_desc->sa, 0, sizeof(uc_desc->sa));
/* Set up the security association */
uc_desc->sa.SA_CTRL.ALGO = algo ;
uc_desc->sa.SA_CTRL.LNC = 1;
uc_desc->sa.SA_CTRL.FB = 1;
uc_desc->sa.SA_CTRL.ENCTYPE = 1;
uc_desc->sa.SA_CTRL.LOADIV = 1;
/* Set up the buffer descriptor */
uc_desc->err = 0 ;
for (i = 0; i < PIC32MZ_MAX_BD; i++)
{
XMEMSET((void *)&uc_desc->bd[i], 0, sizeof(uc_desc->bd[i]));
uc_desc->bd[i].BD_CTRL.LAST_BD = 1;
uc_desc->bd[i].BD_CTRL.LIFM = 1;
uc_desc->bd[i].BD_CTRL.PKT_INT_EN = 1;
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]));
CEPOLLCON = 10;
CECON = 0x27;
}
#define PIC32MZ_IF_RAM(addr) (KVA_TO_PA(addr) < 0x80000)
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)
{
int total ;
pic32mz_desc *uc_desc = KVA0_TO_KVA1(desc);
uc_desc->bd[desc->currBd].UPDPTR = KVA_TO_PA(hash);
// Add the data to the current buffer. If the buffer fills, start processing it
// and fill the next one.
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;
}
}
}
}
static void start_engine(pic32mz_desc *desc) {
// Wrap up the last buffer descriptor and enable it
int i ;
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) {
unsigned int i;
unsigned int *intptr;
pic32mz_desc *uc_desc = KVA0_TO_KVA1(desc);
enum {true = 1, false = 0} engineRunning = true;
while (engineRunning)
{
engineRunning = false;
for (i = 0; i < PIC32MZ_MAX_BD; i++)
engineRunning = engineRunning || uc_desc->bd[i].BD_CTRL.DESC_EN;
}
XMEMCPY(hash, KVA0_TO_KVA1(hash), hash_sz) ;
#ifdef DEBUG_CYASSL
print_mem(KVA0_TO_KVA1(hash), hash_sz) ;
print_mem( hash , hash_sz) ;
#endif
for (i = 0, intptr = (unsigned int *)hash; i < hash_sz/sizeof(unsigned int);
i++, intptr++)
{
*intptr = ntohl(*intptr);
}
}
static int fillBuff(char *buff, int *bufflen, const char *data, int len, int blocksz)
{
int room, copysz ;
room = blocksz - *bufflen ;
copysz = (len <= room) ? len : room ;
XMEMCPY(buff, data, copysz) ;
*bufflen += copysz ;
return (*bufflen == blocksz) ? 1 : 0 ;
}
#endif
#ifndef NO_MD5
void wc_InitMd5(Md5* md5)
{
WOLFSSL_ENTER("InitMd5\n") ;
XMEMSET((void *)md5, 0xcc, sizeof(Md5)) ;
XMEMSET((void *)KVA0_TO_KVA1(md5), 0xcc, sizeof(Md5)) ;
reset_engine(&(md5->desc), PIC32_ALGO_MD5) ;
}
void wc_Md5Update(Md5* md5, const byte* data, word32 len)
{
WOLFSSL_ENTER("Md5Update\n") ;
update_engine(&(md5->desc), data, len, md5->digest) ;
}
void wc_Md5Final(Md5* md5, byte* hash)
{
WOLFSSL_ENTER("Md5Final\n") ;
start_engine(&(md5->desc)) ;
wait_engine(&(md5->desc), (char *)md5->digest, MD5_HASH_SIZE) ;
XMEMCPY(hash, md5->digest, MD5_HASH_SIZE) ;
wc_InitMd5(md5); /* reset state */
}
void Md5SizeSet(Md5* md5, word32 len)
{
WOLFSSL_ENTER("Md5SizeSet\n");
md5->desc.msgSize = len;
}
#endif
#ifndef NO_SHA
int wc_InitSha(Sha* sha)
{
WOLFSSL_ENTER("InitSha\n") ;
XMEMSET((void *)sha, 0xcc, sizeof(Sha)) ;
XMEMSET((void *)KVA0_TO_KVA1(sha), 0xcc, sizeof(Sha)) ;
reset_engine(&(sha->desc), PIC32_ALGO_SHA1) ;
return 0;
}
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
{
WOLFSSL_ENTER("ShaUpdate\n") ;
update_engine(&(sha->desc), data, len, sha->digest) ;
return 0;
}
int wc_ShaFinal(Sha* sha, byte* hash)
{
WOLFSSL_ENTER("ShaFinal\n") ;
start_engine(&(sha->desc)) ;
wait_engine(&(sha->desc), (char *)sha->digest, SHA1_HASH_SIZE) ;
XMEMCPY(hash, sha->digest, SHA1_HASH_SIZE) ;
wc_InitSha(sha); /* reset state */
return 0;
}
void ShaSizeSet(Sha* sha, word32 len)
{
sha->desc.msgSize = len;
}
#endif /* NO_SHA */
#ifndef NO_SHA256
int wc_InitSha256(Sha256* sha256)
{
WOLFSSL_ENTER("InitSha256\n") ;
XMEMSET((void *)sha256, 0xcc, sizeof(Sha256)) ;
XMEMSET((void *)KVA0_TO_KVA1(sha256), 0xcc, sizeof(Sha256)) ;
reset_engine(&(sha256->desc), PIC32_ALGO_SHA256) ;
return 0;
}
int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len)
{
WOLFSSL_ENTER("Sha256Update\n") ;
update_engine(&(sha256->desc), data, len, sha256->digest) ;
return 0;
}
int wc_Sha256Final(Sha256* sha256, byte* hash)
{
WOLFSSL_ENTER("Sha256Final\n") ;
start_engine(&(sha256->desc)) ;
wait_engine(&(sha256->desc), (char *)sha256->digest, SHA256_HASH_SIZE) ;
XMEMCPY(hash, sha256->digest, SHA256_HASH_SIZE) ;
wc_InitSha256(sha256); /* reset state */
return 0;
}
void Sha256SizeSet(Sha256* sha256, word32 len)
{
WOLFSSL_ENTER("Sha256SizeSet\n");
sha256->desc.msgSize = len;
}
#endif /* NO_SHA256 */
#endif

View file

@ -0,0 +1,548 @@
/* port/ti/ti-aes.c
*
* Copyright (C) 2006-2015 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifndef NO_AES
#if defined(WOLFSSL_TI_CRYPT)
#include <stdbool.h>
#include <stdint.h>
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/port/ti/ti-ccm.h>
#include "inc/hw_aes.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/aes.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
static int AesSetIV(Aes* aes, const byte* iv)
{
if (aes == NULL)
return BAD_FUNC_ARG;
if (iv)
XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
else
XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
return 0;
}
WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
int dir)
{
if(!wolfSSL_TI_CCMInit())return 1 ;
if ((aes == NULL) || (key == NULL) || (iv == NULL))
return BAD_FUNC_ARG;
if(!((dir == AES_ENCRYPTION) || (dir == AES_DECRYPTION)))
return BAD_FUNC_ARG;
switch(len) {
case 16: aes->keylen = AES_CFG_KEY_SIZE_128BIT ; break ;
case 24: aes->keylen = AES_CFG_KEY_SIZE_192BIT ; break ;
case 32: aes->keylen = AES_CFG_KEY_SIZE_256BIT ; break ;
default: return BAD_FUNC_ARG;
}
XMEMCPY(aes->key, key, len) ;
#ifdef WOLFSSL_AES_COUNTER
aes->left = 0;
#endif /* WOLFSSL_AES_COUNTER */
return AesSetIV(aes, iv);
}
#define AES_CFG_MODE_CTR_NOCTR AES_CFG_MODE_CTR+100
#define IS_ALIGN16(p) (((unsigned int)(p)&0xf) == 0)
static int AesAlign16(Aes* aes, byte* out, const byte* in, word32 sz, word32 dir, word32 mode)
{
wolfSSL_TI_lockCCM() ;
ROM_AESReset(AES_BASE);
ROM_AESConfigSet(AES_BASE, (aes->keylen | dir |
(mode==AES_CFG_MODE_CTR_NOCTR ? AES_CFG_MODE_CTR : mode)));
ROM_AESIVSet(AES_BASE, aes->reg);
ROM_AESKey1Set(AES_BASE, aes->key, aes->keylen);
if((dir == AES_CFG_DIR_DECRYPT)&& (mode == AES_CFG_MODE_CBC))
/* if input and output same will overwrite input iv */
XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
ROM_AESDataProcess(AES_BASE, (uint32_t *)in, (uint32_t *)out, sz);
wolfSSL_TI_unlockCCM() ;
/* store iv for next call */
if(mode == AES_CFG_MODE_CBC){
if(dir == AES_CFG_DIR_ENCRYPT)
XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
else
XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
}
if(mode == AES_CFG_MODE_CTR) {
do {
int i ;
for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
if (++((byte *)aes->reg)[i])
break ;
}
sz -= AES_BLOCK_SIZE ;
} while((int)sz > 0) ;
}
return 0 ;
}
static int AesProcess(Aes* aes, byte* out, const byte* in, word32 sz, word32 dir, word32 mode)
{
const byte * in_p ; byte * out_p ;
word32 size ;
#define TI_BUFFSIZE 1024
byte buff[TI_BUFFSIZE] ;
if ((aes == NULL) || (in == NULL) || (out == NULL))
return BAD_FUNC_ARG;
if(sz % AES_BLOCK_SIZE)
return BAD_FUNC_ARG;
while(sz > 0) {
size = sz ; in_p = in ; out_p = out ;
if(!IS_ALIGN16(in)){
size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
XMEMCPY(buff, in, size) ;
in_p = (const byte *)buff ;
}
if(!IS_ALIGN16(out)){
size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
out_p = buff ;
}
AesAlign16(aes, out_p, in_p, size, dir, mode) ;
if(!IS_ALIGN16(out)){
XMEMCPY(out, buff, size) ;
}
sz -= size ; in += size ; out += size ;
}
return 0 ;
}
WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
return AesProcess(aes, out, in, sz, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CBC) ;
}
WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
return AesProcess(aes, out, in, sz, AES_CFG_DIR_DECRYPT, AES_CFG_MODE_CBC) ;
}
#ifdef WOLFSSL_AES_COUNTER
WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
char out_block[AES_BLOCK_SIZE] ;
int odd ;
int even ;
char *tmp ; /* (char *)aes->tmp, for short */
tmp = (char *)aes->tmp ;
if(aes->left) {
if((aes->left + sz) >= AES_BLOCK_SIZE){
odd = AES_BLOCK_SIZE - aes->left ;
} else {
odd = sz ;
}
XMEMCPY(tmp+aes->left, in, odd) ;
if((odd+aes->left) == AES_BLOCK_SIZE){
AesProcess(aes, (byte *)out_block, (byte const *)tmp, AES_BLOCK_SIZE,
AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CTR) ;
XMEMCPY(out, out_block+aes->left, odd) ;
aes->left = 0 ;
XMEMSET(tmp, 0x0, AES_BLOCK_SIZE) ;
}
in += odd ;
out+= odd ;
sz -= odd ;
}
odd = sz % AES_BLOCK_SIZE ; /* if there is tail flagment */
if(sz / AES_BLOCK_SIZE) {
even = (sz/AES_BLOCK_SIZE)*AES_BLOCK_SIZE ;
AesProcess(aes, out, in, even, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CTR);
out += even ;
in += even ;
}
if(odd) {
XMEMSET(tmp+aes->left, 0x0, AES_BLOCK_SIZE - aes->left) ;
XMEMCPY(tmp+aes->left, in, odd) ;
AesProcess(aes, (byte *)out_block, (byte const *)tmp, AES_BLOCK_SIZE,
AES_CFG_DIR_ENCRYPT,
AES_CFG_MODE_CTR_NOCTR /* Counter mode without counting IV */
);
XMEMCPY(out, out_block+aes->left,odd) ;
aes->left += odd ;
}
}
#endif
/* AES-DIRECT */
#if defined(WOLFSSL_AES_DIRECT)
WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
{
AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CBC) ;
}
WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
{
AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_DECRYPT, AES_CFG_MODE_CBC) ;
}
WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
const byte* iv, int dir)
{
return(wc_AesSetKey(aes, key, len, iv, dir)) ;
}
#endif
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
static int AesAuthSetKey(Aes* aes, const byte* key, word32 keySz)
{
byte nonce[AES_BLOCK_SIZE];
if ((aes == NULL) || (key == NULL))
return BAD_FUNC_ARG ;
if (!((keySz == 16) || (keySz == 24) || (keySz == 32)))
return BAD_FUNC_ARG ;
XMEMSET(nonce, 0, sizeof(nonce));
return wc_AesSetKey(aes, key, keySz, nonce, AES_ENCRYPTION);
}
static int AesAuthArgCheck(Aes* aes, byte* out, const byte* in, word32 inSz,
const byte* nonce, word32 nonceSz,
const byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz, word32 *M, word32 *L)
{
if((aes == NULL)||(nonce == NULL)||(authTag== NULL)||(authIn == NULL))
return BAD_FUNC_ARG;
if((inSz != 0) && ((out == NULL)||(in == NULL)))
return BAD_FUNC_ARG;
switch(authTagSz){
case 4:
*M = AES_CFG_CCM_M_4; break ;
case 6:
*M = AES_CFG_CCM_M_6; break ;
case 8:
*M = AES_CFG_CCM_M_8; break ;
case 10:
*M = AES_CFG_CCM_M_10; break ;
case 12:
*M = AES_CFG_CCM_M_12; break ;
case 14:
*M = AES_CFG_CCM_M_14; break ;
case 16:
*M = AES_CFG_CCM_M_16; break ;
default:
return 1 ;
}
switch(nonceSz){
case 7:
*L = AES_CFG_CCM_L_8; break ;
case 8:
*L = AES_CFG_CCM_L_7; break ;
case 9:
*L = AES_CFG_CCM_L_6; break ;
case 10:
*L = AES_CFG_CCM_L_5; break ;
case 11:
*L = AES_CFG_CCM_L_4; break ;
case 12:
*L = AES_CFG_CCM_L_3; break ;
case 13:
*L = AES_CFG_CCM_L_2; break ;
case 14:
*L = AES_CFG_CCM_L_1; break ;
default:
return 1;
}
return 0 ;
}
static void AesAuthSetIv(Aes *aes, const byte *nonce, word32 len, word32 L, int mode) {
if(mode == AES_CFG_MODE_CCM){
XMEMSET(aes->reg, 0, 16) ;
switch(L){
case AES_CFG_CCM_L_8:
aes->reg[0] = 0x7; break ;
case AES_CFG_CCM_L_7:
aes->reg[0] = 0x6; break ;
case AES_CFG_CCM_L_6:
aes->reg[0] = 0x5; break ;
case AES_CFG_CCM_L_5:
aes->reg[0] = 0x4; break ;
case AES_CFG_CCM_L_4:
aes->reg[0] = 0x3; break ;
case AES_CFG_CCM_L_3:
aes->reg[0] = 0x2; break ;
case AES_CFG_CCM_L_2:
aes->reg[0] = 0x1; break ;
case AES_CFG_CCM_L_1:
aes->reg[0] = 0x0; break ;
}
XMEMCPY(((byte *)aes->reg)+1, nonce, len) ;
} else {
byte *b = (byte *)aes->reg ;
XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
XMEMCPY(aes->reg, nonce, len);
b[AES_BLOCK_SIZE-4] = 0 ;
b[AES_BLOCK_SIZE-3] = 0 ;
b[AES_BLOCK_SIZE-2] = 0 ;
b[AES_BLOCK_SIZE-1] = 1 ;
}
}
#define RoundUp16(n) ((n+15)&0xfffffff0)
#define FREE_ALL \
if(in_save) XFREE(in_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);\
if(out_save) XFREE(out_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);\
if(authIn_save)XFREE(authIn_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);\
if(nonce_save) XFREE(nonce_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);
static int AesAuthEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
const byte* nonce, word32 nonceSz,
byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz, int mode)
{
word32 M, L ;
byte *in_a, *in_save ;
byte *out_a, *out_save ;
byte *authIn_a, *authIn_save ;
byte *nonce_a, *nonce_save ;
word32 tmpTag[4] ;
int ret ;
if(AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz, authIn, authInSz, &M, &L)
== BAD_FUNC_ARG)return BAD_FUNC_ARG ;
/* 16 byte padding */
in_save = NULL ; out_save = NULL ; authIn_save = NULL ; nonce_save = NULL ;
if((inSz%16)==0){
in_save = NULL ; in_a = (byte *)in ;
out_save = NULL ; out_a = out ;
} else {
if((in_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E ; }
in_a = in_save ; XMEMSET(in_a, 0, RoundUp16(inSz)) ; XMEMCPY(in_a, in, inSz) ;
if((out_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E ; }
out_a = out_save ;
}
if((authInSz%16)==0){
authIn_save = NULL ; authIn_a = (byte *)authIn ;
} else {
if((authIn_save = XMALLOC(RoundUp16(authInSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E ; }
authIn_a = authIn_save ; XMEMSET(authIn_a, 0, RoundUp16(authInSz)) ; XMEMCPY(authIn_a, authIn, authInSz) ;
}
if((nonceSz%16)==0){
nonce_save = NULL ; nonce_a = (byte *)nonce ;
} else {
if((nonce_save = XMALLOC(RoundUp16(nonceSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E; }
nonce_a = nonce_save ; XMEMSET(nonce_a, 0, RoundUp16(nonceSz)) ; XMEMCPY(nonce_a, nonce, nonceSz) ;
}
/* do aes-ccm */
AesAuthSetIv(aes, nonce, nonceSz, L, mode) ;
ROM_AESReset(AES_BASE);
ROM_AESConfigSet(AES_BASE, (aes->keylen | AES_CFG_DIR_ENCRYPT |
AES_CFG_CTR_WIDTH_128 |
mode | ((mode== AES_CFG_MODE_CCM) ? (L | M) : 0 ))) ;
ROM_AESIVSet(AES_BASE, aes->reg);
ROM_AESKey1Set(AES_BASE, aes->key, aes->keylen);
ret = ROM_AESDataProcessAuth(AES_BASE, (unsigned int*)in_a, (unsigned int *)out_a, inSz,
(unsigned int*)authIn_a, authInSz, (unsigned int *)tmpTag);
if(ret == false){
XMEMSET(out, 0, inSz) ;
XMEMSET(authTag, 0, authTagSz) ;
} else {
XMEMCPY(out, out_a, inSz) ;
XMEMCPY(authTag, tmpTag, authTagSz) ;
}
FREE_ALL;
return 0 ;
}
static int AesAuthDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
const byte* nonce, word32 nonceSz,
const byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz, int mode)
{
word32 M, L ;
byte *in_a, *in_save ;
byte *out_a, *out_save ;
byte *authIn_a, *authIn_save ;
byte *nonce_a, *nonce_save ;
word32 tmpTag[4] ;
bool ret ;
if(AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz, authIn, authInSz, &M, &L)
== BAD_FUNC_ARG)return BAD_FUNC_ARG ;
/* 16 byte padding */
in_save = NULL ; out_save = NULL ; authIn_save = NULL ; nonce_save = NULL ;
if((inSz%16)==0){
in_save = NULL ; in_a = (byte *)in ;
out_save = NULL ; out_a = out ;
} else {
if((in_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E;}
in_a = in_save ; XMEMSET(in_a, 0, RoundUp16(inSz)) ; XMEMCPY(in_a, in, inSz) ;
if((out_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E;}
out_a = out_save ;
}
if((authInSz%16)==0){
authIn_save = NULL ; authIn_a = (byte *)authIn ;
} else {
if((authIn_save = XMALLOC(RoundUp16(authInSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E; }
authIn_a = authIn_save ; XMEMSET(authIn_a, 0, RoundUp16(authInSz)) ; XMEMCPY(authIn_a, authIn, authInSz) ;
}
if((nonceSz%16)==0){
nonce_save = NULL ; nonce_a = (byte *)nonce ;
} else {
if((nonce_save = XMALLOC(RoundUp16(nonceSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E; }
nonce_a = nonce_save ; XMEMSET(nonce_a, 0, RoundUp16(nonceSz)) ; XMEMCPY(nonce_a, nonce, nonceSz) ;
}
/* do aes-ccm */
AesAuthSetIv(aes, nonce, nonceSz, L, mode) ;
ROM_AESReset(AES_BASE);
ROM_AESConfigSet(AES_BASE, (aes->keylen | AES_CFG_DIR_DECRYPT |
AES_CFG_CTR_WIDTH_128 |
mode | ((mode== AES_CFG_MODE_CCM) ? (L | M) : 0 ))) ;
ROM_AESIVSet(AES_BASE, aes->reg);
ROM_AESKey1Set(AES_BASE, aes->key, aes->keylen);
ret = ROM_AESDataProcessAuth(AES_BASE, (unsigned int*)in_a, (unsigned int *)out_a, inSz,
(unsigned int*)authIn_a, authInSz, (unsigned int *)tmpTag);
if((ret == false) || (XMEMCMP(authTag, tmpTag, authTagSz) != 0)){
XMEMSET(out, 0, inSz) ;
ret = false ;
} else {
XMEMCPY(out, out_a, inSz) ;
}
FREE_ALL ;
return ret==true ? 0 : 1 ;
}
#endif
#ifdef HAVE_AESGCM
WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
{
return AesAuthSetKey(aes, key, len) ;
}
WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
const byte* iv, word32 ivSz,
byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz)
{
return AesAuthEncrypt(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC) ;
}
WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
const byte* iv, word32 ivSz,
const byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz)
{
return AesAuthDecrypt(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC) ;
}
WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
{
return AesAuthSetKey(&gmac->aes, key, len) ;
}
WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
const byte* authIn, word32 authInSz,
byte* authTag, word32 authTagSz)
{
return AesAuthEncrypt(&gmac->aes, NULL, NULL, 0, iv, ivSz, authTag, authTagSz,
authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC) ;
}
#endif /* HAVE_AESGCM */
#ifdef HAVE_AESCCM
WOLFSSL_API void wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
{
AesAuthSetKey(aes, key, keySz) ;
}
WOLFSSL_API void wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
const byte* nonce, word32 nonceSz,
byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz)
{
AesAuthEncrypt(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz,
authIn, authInSz, AES_CFG_MODE_CCM) ;
}
WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
const byte* nonce, word32 nonceSz,
const byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz)
{
return AesAuthDecrypt(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz,
authIn, authInSz, AES_CFG_MODE_CCM) ;
}
#endif /* HAVE_AESCCM */
#endif /* WOLFSSL_TI_CRYPT */
#endif /* NO_AES */

View file

@ -0,0 +1,82 @@
/* port/ti/ti_ccm.c
*
* Copyright (C) 2006-2015 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
#include <stdbool.h>
#include <stdint.h>
#include "driverlib/sysctl.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
#ifndef SINGLE_THREADED
#include <wolfssl/wolfcrypt/wc_port.h>
static wolfSSL_Mutex TI_CCM_Mutex ;
#endif
#define TIMEOUT 500000
#define WAIT(stat) { volatile int i ; for(i=0; i<TIMEOUT; i++)if(stat)break ; if(i==TIMEOUT)return(false) ; }
static bool ccm_init = false ;
bool wolfSSL_TI_CCMInit(void)
{
if(ccm_init)return true ;
ccm_init = true ;
SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
if(!ROM_SysCtlPeripheralPresent(SYSCTL_PERIPH_CCM0))
return false ;
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_CCM0);
WAIT(ROM_SysCtlPeripheralReady(SYSCTL_PERIPH_CCM0)) ;
ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_CCM0);
WAIT(ROM_SysCtlPeripheralReady(SYSCTL_PERIPH_CCM0)) ;
#ifndef SINGLE_THREADED
InitMutex(&TI_CCM_Mutex) ;
#endif
return true ;
}
#ifndef SINGLE_THREADED
void wolfSSL_TI_lockCCM() {
LockMutex(&TI_CCM_Mutex) ;
}
void wolfSSL_TI_unlockCCM() {
UnLockMutex(&TI_CCM_Mutex) ;
}
#endif
#endif

View file

@ -0,0 +1,181 @@
/* port/ti/ti-des.c
*
* Copyright (C) 2006-2015 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifndef NO_DES
#if defined(WOLFSSL_TI_CRYPT)
#include <stdbool.h>
#include <stdint.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/port/ti/ti-ccm.h>
#include "inc/hw_des.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/des.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
static int DesSetIV(Des* des, const byte* iv, int tri)
{
if (des == NULL)
return BAD_FUNC_ARG;
if (iv)
XMEMCPY(des->reg, iv, tri == DES_CFG_TRIPLE ? DES3_IVLEN : DES_IVLEN);
else
XMEMSET(des->reg, 0, tri == DES_CFG_TRIPLE ? DES3_IVLEN : DES_IVLEN);
return 0;
}
static int DesSetKey(Des* des, const byte* key, const byte* iv,int dir, int tri)
{
if(!wolfSSL_TI_CCMInit())return 1 ;
if ((des == NULL) || (key == NULL) || (iv == NULL))
return BAD_FUNC_ARG;
if(!((dir == DES_ENCRYPTION) || (dir == DES_DECRYPTION)))
return BAD_FUNC_ARG;
XMEMCPY(des->key, key, tri == DES_CFG_SINGLE ? DES_KEYLEN : DES3_KEYLEN) ;
return DesSetIV(des, iv, tri);
}
static int DesCbcAlign16(Des* des, byte* out, const byte* in, word32 sz, word32 dir, word32 tri)
{
wolfSSL_TI_lockCCM() ;
ROM_DESReset(DES_BASE);
ROM_DESConfigSet(DES_BASE, (dir | DES_CFG_MODE_CBC | tri));
ROM_DESIVSet(DES_BASE, des->reg);
ROM_DESKeySet(DES_BASE, des->key);
if(dir == DES_CFG_DIR_DECRYPT)
/* if input and output same will overwrite input iv */
XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
ROM_DESDataProcess(DES_BASE, (uint32_t *)in, (uint32_t *)out, sz);
wolfSSL_TI_unlockCCM() ;
/* store iv for next call */
if(dir == DES_CFG_DIR_ENCRYPT)
XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
else
XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
return 0 ;
}
#define IS_ALIGN16(p) (((unsigned int)(p)&0xf) == 0)
static int DesCbc(Des* des, byte* out, const byte* in, word32 sz, word32 dir, word32 tri)
{
const byte * in_p ; byte * out_p ;
word32 size ;
#define TI_BUFFSIZE 1024
byte buff[TI_BUFFSIZE] ;
if ((des == NULL) || (in == NULL) || (out == NULL))
return BAD_FUNC_ARG;
if(sz % DES_BLOCK_SIZE)
return BAD_FUNC_ARG;
while(sz > 0) {
size = sz ; in_p = in ; out_p = out ;
if(!IS_ALIGN16(in)){
size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
XMEMCPY(buff, in, size) ;
in_p = (const byte *)buff ;
}
if(!IS_ALIGN16(out)){
size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
out_p = (byte *)buff ;
}
DesCbcAlign16(des, out_p, in_p, size, dir, tri) ;
if(!IS_ALIGN16(out)){
XMEMCPY(out, buff, size) ;
}
sz -= size ; in += size ; out += size ;
}
return 0 ;
}
WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key, const byte* iv,int dir)
{
return DesSetKey(des, key, iv, dir, DES_CFG_SINGLE) ;
}
WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv)
{
DesSetIV(des, iv, DES_CFG_SINGLE) ;
}
WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv,int dir)
{
return DesSetKey((Des *)des, key, iv, dir, DES_CFG_TRIPLE) ;
}
WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv)
{
return DesSetIV((Des *)des, iv, DES_CFG_TRIPLE) ;
}
WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
{
return DesCbc(des, out, in, sz, DES_CFG_DIR_ENCRYPT, DES_CFG_SINGLE) ;
}
WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
{
return DesCbc(des, out, in, sz, DES_CFG_DIR_DECRYPT, DES_CFG_SINGLE) ;
}
WOLFSSL_API int wc_Des_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
const byte* key, const byte* iv)
{ return 0 ;}
WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
{
return DesCbc((Des *)des, out, in, sz, DES_CFG_DIR_ENCRYPT, DES_CFG_TRIPLE) ;
}
WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
{
return DesCbc((Des *)des, out, in, sz, DES_CFG_DIR_DECRYPT, DES_CFG_TRIPLE) ;
}
WOLFSSL_API int wc_Des3_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
const byte* key, const byte* iv)
{ return 0 ; }
#endif /* WOLFSSL_TI_CRYPT */
#endif /* NO_DES */

View file

@ -0,0 +1,291 @@
/* port/ti/ti-hash.c
*
* Copyright (C) 2006-2015 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/types.h>
#if defined(WOLFSSL_TI_HASH)
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/md5.h>
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/port/ti/ti-hash.h>
#include <wolfssl/wolfcrypt/port/ti/ti-ccm.h>
#include <wolfssl/wolfcrypt/logging.h>
#ifndef TI_DUMMY_BUILD
#include "inc/hw_memmap.h"
#include "inc/hw_shamd5.h"
#include "inc/hw_ints.h"
#include "driverlib/shamd5.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
#else
#define SHAMD5_ALGO_MD5 1
#define SHAMD5_ALGO_SHA1 2
#define SHAMD5_ALGO_SHA256 3
bool wolfSSL_TI_CCMInit(void) { return true ; }
#endif
static int hashInit(wolfssl_TI_Hash *hash) {
hash->used = 0 ;
hash->msg = 0 ;
hash->len = 0 ;
return 0 ;
}
static int hashUpdate(wolfssl_TI_Hash *hash, const byte* data, word32 len)
{
void *p ;
if((hash== NULL) || (data == NULL))return BAD_FUNC_ARG;
if(hash->len < hash->used+len) {
if(hash->msg == NULL) {
p = XMALLOC(hash->used+len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
} else {
p = XREALLOC(hash->msg, hash->used+len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if(p == 0)return 1 ;
hash->msg = p ;
hash->len = hash->used+len ;
}
XMEMCPY(hash->msg+hash->used, data, len) ;
hash->used += len ;
return 0 ;
}
static int hashGetHash(wolfssl_TI_Hash *hash, byte* result, word32 algo, word32 hsize)
{
uint32_t h[16] ;
#ifndef TI_DUMMY_BUILD
wolfSSL_TI_lockCCM() ;
ROM_SHAMD5Reset(SHAMD5_BASE);
ROM_SHAMD5ConfigSet(SHAMD5_BASE, algo);
ROM_SHAMD5DataProcess(SHAMD5_BASE,
(uint32_t *)hash->msg, hash->used, h);
wolfSSL_TI_unlockCCM() ;
#else
(void) hash ;
(void) algo ;
#endif
XMEMCPY(result, h, hsize) ;
return 0 ;
}
static void hashRestorePos(wolfssl_TI_Hash *h1, wolfssl_TI_Hash *h2) {
h1->used = h2->used ;
}
static int hashFinal(wolfssl_TI_Hash *hash, byte* result, word32 algo, word32 hsize)
{
hashGetHash(hash, result, algo, hsize) ;
XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER);
hashInit(hash) ;
return 0 ;
}
static int hashHash(const byte* data, word32 len, byte* hash, word32 algo, word32 hsize)
{
int ret = 0;
#ifdef WOLFSSL_SMALL_STACK
wolfssl_TI_Hash* hash_desc;
#else
wolfssl_TI_Hash hash_desc[1];
#endif
#ifdef WOLFSSL_SMALL_STACK
hash_desc = (wolfssl_TI_Hash*)XMALLOC(sizeof(wolfssl_TI_Hash), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (hash_desc == NULL)
return MEMORY_E;
#endif
if ((ret = hashInit(hash_desc)) != 0) {
WOLFSSL_MSG("Hash Init failed");
}
else {
hashUpdate(hash_desc, data, len);
hashFinal(hash_desc, hash, algo, hsize);
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}
#if !defined(NO_MD5)
WOLFSSL_API void wc_InitMd5(Md5* md5)
{
if (md5 == NULL)
return ;
if(!wolfSSL_TI_CCMInit())return ;
hashInit((wolfssl_TI_Hash *)md5) ;
}
WOLFSSL_API void wc_Md5Update(Md5* md5, const byte* data, word32 len)
{
hashUpdate((wolfssl_TI_Hash *)md5, data, len) ;
}
WOLFSSL_API void wc_Md5Final(Md5* md5, byte* hash)
{
hashFinal((wolfssl_TI_Hash *)md5, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
}
WOLFSSL_API void wc_Md5GetHash(Md5* md5, byte* hash)
{
hashGetHash((wolfssl_TI_Hash *)md5, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
}
WOLFSSL_API void wc_Md5RestorePos(Md5* m1, Md5* m2) {
hashRestorePos((wolfssl_TI_Hash *)m1, (wolfssl_TI_Hash *)m2) ;
}
WOLFSSL_API int wc_Md5Hash(const byte*data, word32 len, byte*hash)
{
return hashHash(data, len, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
}
#endif /* NO_MD5 */
#if !defined(NO_SHA)
WOLFSSL_API int wc_InitSha(Sha* sha)
{
if (sha == NULL)
return 1 ;
if(!wolfSSL_TI_CCMInit())return 1 ;
return hashInit((wolfssl_TI_Hash *)sha) ;
}
WOLFSSL_API int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
{
return hashUpdate((wolfssl_TI_Hash *)sha, data, len) ;
}
WOLFSSL_API int wc_ShaFinal(Sha* sha, byte* hash)
{
return hashFinal((wolfssl_TI_Hash *)sha, hash, SHAMD5_ALGO_SHA1, SHA_DIGEST_SIZE) ;
}
WOLFSSL_API int wc_ShaGetHash(Sha* sha, byte* hash)
{
return hashGetHash(sha, hash, SHAMD5_ALGO_SHA1, SHA_DIGEST_SIZE) ;
}
WOLFSSL_API void wc_ShaRestorePos(Sha* s1, Sha* s2) {
hashRestorePos((wolfssl_TI_Hash *)s1, (wolfssl_TI_Hash *)s2) ;
}
WOLFSSL_API int wc_ShaHash(const byte*data, word32 len, byte*hash)
{
return hashHash(data, len, hash, SHAMD5_ALGO_SHA1, SHA_DIGEST_SIZE) ;
}
#endif /* NO_SHA */
#if defined(HAVE_SHA224)
WOLFSSL_API int wc_InitSha224(Sha224* sha224)
{
if (sha224 == NULL)
return 1 ;
if(!wolfSSL_TI_CCMInit())return 1 ;
return hashInit((wolfssl_TI_Hash *)sha224) ;
}
WOLFSSL_API int wc_Sha224Update(Sha224* sha224, const byte* data, word32 len)
{
return hashUpdate((wolfssl_TI_Hash *)sha224, data, len) ;
}
WOLFSSL_API int wc_Sha224Final(Sha224* sha224, byte* hash)
{
return hashFinal((wolfssl_TI_Hash *)sha224, hash, SHAMD5_ALGO_SHA224, SHA224_DIGEST_SIZE) ;
}
WOLFSSL_API int wc_Sha224GetHash(Sha224* sha224, byte* hash)
{
return hashGetHash(sha224, hash, SHAMD5_ALGO_SHA224, SHA224_DIGEST_SIZE) ;
}
WOLFSSL_API void wc_Sha224RestorePos(Sha224* s1, Sha224* s2) {
hashRestorePos((wolfssl_TI_Hash *)s1, (wolfssl_TI_Hash *)s2) ;
}
WOLFSSL_API int wc_Sha224Hash(const byte* data, word32 len, byte*hash)
{
return hashHash(data, len, hash, SHAMD5_ALGO_SHA224, SHA224_DIGEST_SIZE) ;
}
#endif /* HAVE_SHA224 */
#if !defined(NO_SHA256)
WOLFSSL_API int wc_InitSha256(Sha256* sha256)
{
if (sha256 == NULL)
return 1 ;
if(!wolfSSL_TI_CCMInit())return 1 ;
return hashInit((wolfssl_TI_Hash *)sha256) ;
}
WOLFSSL_API int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len)
{
return hashUpdate((wolfssl_TI_Hash *)sha256, data, len) ;
}
WOLFSSL_API int wc_Sha256Final(Sha256* sha256, byte* hash)
{
return hashFinal((wolfssl_TI_Hash *)sha256, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ;
}
WOLFSSL_API int wc_Sha256GetHash(Sha256* sha256, byte* hash)
{
return hashGetHash(sha256, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ;
}
WOLFSSL_API void wc_Sha256RestorePos(Sha256* s1, Sha256* s2) {
hashRestorePos((wolfssl_TI_Hash *)s1, (wolfssl_TI_Hash *)s2) ;
}
WOLFSSL_API int wc_Sha256Hash(const byte* data, word32 len, byte*hash)
{
return hashHash(data, len, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ;
}
#endif
#endif