Cleanups for the ti-aes.c code to conform with coding standards.

This commit is contained in:
David Garske
2023-11-22 12:45:46 -08:00
parent 0c9555b29e
commit d17955f2d0

View File

@ -26,10 +26,8 @@
#include <wolfssl/wolfcrypt/settings.h>
#ifndef NO_AES
#if !defined(NO_AES) && defined(WOLFSSL_TI_CRYPT)
#if defined(WOLFSSL_TI_CRYPT)
#include <stdbool.h>
#include <stdint.h>
@ -45,6 +43,13 @@
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
#define AES_CFG_MODE_CTR_NOCTR (AES_CFG_MODE_CTR + 100)
#define IS_ALIGN16(p) (((unsigned int)(p) & 0xf) == 0)
#define ROUNDUP_16(n) ((n+15) & 0xfffffff0)
#ifndef TI_BUFFSIZE
#define TI_BUFFSIZE 1024
#endif
static int AesSetIV(Aes* aes, const byte* iv)
{
if (aes == NULL)
@ -58,10 +63,10 @@ static int AesSetIV(Aes* aes, const byte* iv)
return 0;
}
WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
int dir)
int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv, int dir)
{
if(!wolfSSL_TI_CCMInit())return 1 ;
if (!wolfSSL_TI_CCMInit())
return 1;
if ((aes == NULL) || (key == NULL) || (iv == NULL))
return BAD_FUNC_ARG;
if (!((dir == AES_ENCRYPTION) || (dir == AES_DECRYPTION)))
@ -77,24 +82,24 @@ WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte*
XMEMCPY(aes->key, key, len);
#ifdef WOLFSSL_AES_COUNTER
aes->left = 0;
#endif /* WOLFSSL_AES_COUNTER */
#endif
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)
static int AesAlign16(Aes* aes, byte* out, const byte* in, word32 sz,
word32 dir, word32 mode)
{
/* Processed aligned chunk to HW AES */
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, (uint32_t *)aes->reg);
ROM_AESKey1Set(AES_BASE, (uint32_t *)aes->key, aes->keylen);
if((dir == AES_CFG_DIR_DECRYPT)&& (mode == AES_CFG_MODE_CBC))
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();
@ -120,11 +125,11 @@ static int AesAlign16(Aes* aes, byte* out, const byte* in, word32 sz, word32 di
return 0;
}
static int AesProcess(Aes* aes, byte* out, const byte* in, word32 sz, word32 dir, word32 mode)
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))
@ -155,18 +160,18 @@ static int AesProcess(Aes* aes, byte* out, const byte* in, word32 sz, word32 di
return 0;
}
WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
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)
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 int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
char out_block[AES_BLOCK_SIZE];
int odd;
@ -195,7 +200,7 @@ WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
out+= odd;
sz -= odd;
}
odd = sz % AES_BLOCK_SIZE ; /* if there is tail flagment */
odd = sz % AES_BLOCK_SIZE; /* if there is tail fragment */
if (sz / AES_BLOCK_SIZE) {
even = (sz/AES_BLOCK_SIZE)*AES_BLOCK_SIZE;
ret = AesProcess(aes, out, in, even, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CTR);
@ -218,24 +223,26 @@ WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
}
return 0;
}
#endif
#endif /* WOLFSSL_AES_COUNTER */
/* AES-DIRECT */
#if defined(WOLFSSL_AES_DIRECT)
WOLFSSL_API int wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
int wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
{
return AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CBC) ;
return AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_ENCRYPT,
AES_CFG_MODE_CBC);
}
WOLFSSL_API int wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
int wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
{
return AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_DECRYPT, AES_CFG_MODE_CBC) ;
return 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)
int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len, const byte* iv,
int dir)
{
return(wc_AesSetKey(aes, key, len, iv, dir)) ;
return wc_AesSetKey(aes, key, len, iv, dir);
}
#endif
#endif /* WOLFSSL_AES_DIRECT */
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
@ -307,8 +314,9 @@ static int AesAuthArgCheck(Aes* aes, byte* out, const byte* in, word32 inSz,
return 0;
}
static void AesAuthSetIv(Aes *aes, const byte *nonce, word32 len, word32 L, int mode) {
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) {
@ -330,7 +338,8 @@ static void AesAuthSetIv(Aes *aes, const byte *nonce, word32 len, word32 L, int
aes->reg[0] = 0x0; break;
}
XMEMCPY(((byte *)aes->reg)+1, nonce, len);
} else {
}
else {
byte *b = (byte *)aes->reg;
XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
XMEMCPY(aes->reg, nonce, len);
@ -341,58 +350,66 @@ static void AesAuthSetIv(Aes *aes, const byte *nonce, word32 len, word32 L, int
}
}
#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;
word32 M, L;
byte *in_a, *in_save = NULL;
byte *out_a, *out_save = NULL;
byte *authIn_a, *authIn_save = NULL;
byte *nonce_a, *nonce_save = NULL;
word32 tmpTag[4];
if(AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz, authIn, authInSz, &M, &L)
== BAD_FUNC_ARG)return BAD_FUNC_ARG ;
ret = AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag,
authTagSz, authIn, authInSz, &M, &L);
if (ret != 0) {
return ret;
}
/* 16 byte padding */
in_save = NULL; out_save = NULL; authIn_save = NULL; nonce_save = NULL;
if((inSz%16)==0){
if (IS_ALIGN16(inSz)) {
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) ;
}
else {
in_save = XMALLOC(ROUNDUP_16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (in_save == NULL) { ret = MEMORY_E; goto exit; }
in_a = in_save;
XMEMSET(in_a, 0, ROUNDUP_16(inSz));
XMEMCPY(in_a, in, inSz);
if((out_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E ; }
out_save = XMALLOC(ROUNDUP_16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)
if (out_save == NULL) { ret = MEMORY_E; goto exit; }
out_a = out_save;
}
if((authInSz%16)==0){
if (IS_ALIGN16(authInSz)) {
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) ;
}
else {
authIn_save = XMALLOC(ROUNDUP_16(authInSz), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (authIn_save == NULL) { ret = MEMORY_E; goto exit; }
authIn_a = authIn_save;
XMEMSET(authIn_a, 0, ROUNDUP_16(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) ;
if (IS_ALIGN16(nonceSz)) {
nonce_save = NULL;
nonce_a = (byte *)nonce;
}
else {
nonce_save = XMALLOC(ROUNDUP_16(nonceSz), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (nonce_save == NULL) { ret = MEMORY_E; goto exit; }
nonce_a = nonce_save;
XMEMSET(nonce_a, 0, ROUNDUP_16(nonceSz));
XMEMCPY(nonce_a, nonce, nonceSz);
}
/* do aes-ccm */
@ -413,7 +430,11 @@ static int AesAuthEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
XMEMCPY(authTag, tmpTag, authTagSz);
}
FREE_ALL;
exit:
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);
return 0;
}
@ -422,46 +443,61 @@ static int AesAuthDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
const byte* authTag, word32 authTagSz,
const byte* authIn, word32 authInSz, int mode)
{
int ret;
word32 M, L;
byte *in_a, *in_save ;
byte *out_a, *out_save ;
byte *authIn_a, *authIn_save ;
byte *nonce_a, *nonce_save ;
byte *in_a, *in_save = NULL;
byte *out_a, *out_save = NULL;
byte *authIn_a, *authIn_save = NULL;
byte *nonce_a, *nonce_save = NULL;
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 ;
ret = AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag,
authTagSz, authIn, authInSz, &M, &L)
if (ret != 0) {
return ret;
}
/* 16 byte padding */
in_save = NULL; out_save = NULL; authIn_save = NULL; nonce_save = NULL;
if((inSz%16)==0){
if (IS_ALIGN16(inSz)) {
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) ;
}
else {
in_save = XMALLOC(ROUNDUP_16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (in_save == NULL) { ret = MEMORY_E; goto exit; }
in_a = in_save;
XMEMSET(in_a, 0, ROUNDUP_16(inSz));
XMEMCPY(in_a, in, inSz);
if((out_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
FREE_ALL; return MEMORY_E;}
out_save = XMALLOC(ROUNDUP_16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)
if (out_save == NULL) { ret = MEMORY_E; goto exit; }
out_a = out_save;
}
if((authInSz%16)==0){
if (IS_ALIGN16(authInSz)) {
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) ;
}
else {
authIn_save = XMALLOC(ROUNDUP_16(authInSz), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (authIn_save == NULL) { ret = MEMORY_E; goto exit; }
authIn_a = authIn_save;
XMEMSET(authIn_a, 0, ROUNDUP_16(authInSz));
XMEMCPY(authIn_a, authIn, authInSz);
}
if((nonceSz%16)==0){
if (IS_ALIGN16(nonceSz)) {
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) ;
}
else {
nonce_save = XMALLOC(ROUNDUP_16(nonceSz), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (authIn_save == NULL) { ret = MEMORY_E; goto exit; }
nonce_a = nonce_save;
XMEMSET(nonce_a, 0, ROUNDUP_16(nonceSz));
XMEMCPY(nonce_a, nonce, nonceSz);
}
/* do aes-ccm */
@ -481,19 +517,23 @@ static int AesAuthDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
XMEMCPY(out, out_a, inSz);
}
FREE_ALL ;
exit:
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);
return ret==true ? 0 : 1;
}
#endif
#endif /* HAVE_AESGCM || HAVE_AESCCM */
#ifdef HAVE_AESGCM
WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
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,
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)
@ -504,7 +544,7 @@ WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz
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,
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)
@ -513,28 +553,27 @@ WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz
authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC);
}
WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
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,
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 int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
{
return AesAuthSetKey(aes, key, keySz);
}
WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
int 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)
@ -543,7 +582,7 @@ WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inS
authIn, authInSz, AES_CFG_MODE_CCM);
}
WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
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)
@ -553,7 +592,7 @@ WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inS
}
#endif /* HAVE_AESCCM */
WOLFSSL_API int wc_AesInit(Aes* aes, void* heap, int devId)
int wc_AesInit(Aes* aes, void* heap, int devId)
{
if (aes == NULL)
return BAD_FUNC_ARG;
@ -564,14 +603,9 @@ WOLFSSL_API int wc_AesInit(Aes* aes, void* heap, int devId)
return 0;
}
WOLFSSL_API void wc_AesFree(Aes* aes)
void wc_AesFree(Aes* aes)
{
(void)aes;
}
#endif /* WOLFSSL_TI_CRYPT */
#endif /* NO_AES */
#endif /* !NO_AES && WOLFSSL_TI_CRYPT */