aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA
diff options
context:
space:
mode:
Diffstat (limited to 'os/hal/ports/SAMA')
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c66
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h15
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c216
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h10
-rw-r--r--os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c1078
-rw-r--r--os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h70
6 files changed, 857 insertions, 598 deletions
diff --git a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c
index b490b05bc..0ed4af4a2 100644
--- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c
+++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c
@@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-*/
+ */
#include "hal.h"
#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)
@@ -83,11 +83,9 @@ void sama_aes_lld_set_input(uint32_t* data) {
uint8_t size = 4;
if ((AES->AES_MR & AES_MR_OPMOD_Msk) == AES_MR_OPMOD_CFB) {
- if ((AES->AES_MR & AES_MR_CFBS_Msk) ==
- AES_MR_CFBS_SIZE_128BIT)
+ if ((AES->AES_MR & AES_MR_CFBS_Msk) == AES_MR_CFBS_SIZE_128BIT)
size = 4;
- else if ((AES->AES_MR & AES_MR_CFBS_Msk) ==
- AES_MR_CFBS_SIZE_64BIT)
+ else if ((AES->AES_MR & AES_MR_CFBS_Msk) == AES_MR_CFBS_SIZE_64BIT)
size = 2;
else
size = 1;
@@ -113,13 +111,13 @@ cryerror_t sama_aes_lld_process_polling(CRYDriver *cryp, aesparams *params,
cryerror_t ret;
osalMutexLock(&cryp->mutex);
-//AES soft reset
+ //AES soft reset
AES->AES_CR = AES_CR_SWRST;
-//AES set op mode
+ //AES set op mode
AES->AES_MR |= ((AES_MR_OPMOD_Msk & (params->mode)) | AES_MR_CKEY_PASSWD);
-//AES set key size
+ //AES set key size
ret = sama_aes_lld_set_key_size(cryp->key0_size);
if (ret == CRY_NOERROR) {
@@ -137,10 +135,9 @@ cryerror_t sama_aes_lld_process_polling(CRYDriver *cryp, aesparams *params,
else
AES->AES_MR &= ~AES_MR_CIPHER;
- AES->AES_MR |= (((AES_MR_SMOD_Msk & (AES_MR_SMOD_MANUAL_START)))
- | AES_MR_CKEY_PASSWD);
+ AES->AES_MR |= (((AES_MR_SMOD_Msk & (AES_MR_SMOD_MANUAL_START))) | AES_MR_CKEY_PASSWD);
-//Enable aes interrupt
+ //Enable aes interrupt
AES->AES_IER = AES_IER_DATRDY;
for (i = 0; i < indata_len; i += params->block_size) {
@@ -149,8 +146,7 @@ cryerror_t sama_aes_lld_process_polling(CRYDriver *cryp, aesparams *params,
AES->AES_CR = AES_CR_START;
- while ((AES->AES_ISR & AES_ISR_DATRDY) != AES_ISR_DATRDY)
- ;
+ while ((AES->AES_ISR & AES_ISR_DATRDY) != AES_ISR_DATRDY);
sama_aes_lld_get_output((uint32_t *) ((out) + i));
}
@@ -165,7 +161,7 @@ cryerror_t sama_aes_lld_process_polling(CRYDriver *cryp, aesparams *params,
cryerror_t sama_aes_lld_process_dma(CRYDriver *cryp, aesparams *params,
const uint8_t *in, uint8_t *out, size_t indata_len) {
-
+#if defined(SAMA_DMA_REQUIRED)
cryerror_t ret;
osalDbgAssert(cryp->thread == NULL, "already waiting");
@@ -187,26 +183,26 @@ cryerror_t sama_aes_lld_process_dma(CRYDriver *cryp, aesparams *params,
cryp->dmawith = DMA_DATA_WIDTH_BYTE;
cryp->rxdmamode = XDMAC_CC_TYPE_PER_TRAN |
- XDMAC_CC_PROT_SEC |
- XDMAC_CC_MBSIZE_SINGLE |
- XDMAC_CC_DSYNC_PER2MEM | XDMAC_CC_CSIZE(cryp->dmachunksize) |
- XDMAC_CC_DWIDTH(cryp->dmawith) |
- XDMAC_CC_SIF_AHB_IF1 |
- XDMAC_CC_DIF_AHB_IF0 |
- XDMAC_CC_SAM_FIXED_AM |
- XDMAC_CC_DAM_INCREMENTED_AM |
- XDMAC_CC_PERID(PERID_AES_RX);
+ XDMAC_CC_PROT_SEC |
+ XDMAC_CC_MBSIZE_SINGLE |
+ XDMAC_CC_DSYNC_PER2MEM | XDMAC_CC_CSIZE(cryp->dmachunksize) |
+ XDMAC_CC_DWIDTH(cryp->dmawith) |
+ XDMAC_CC_SIF_AHB_IF1 |
+ XDMAC_CC_DIF_AHB_IF0 |
+ XDMAC_CC_SAM_FIXED_AM |
+ XDMAC_CC_DAM_INCREMENTED_AM |
+ XDMAC_CC_PERID(PERID_AES_RX);
cryp->txdmamode = XDMAC_CC_TYPE_PER_TRAN |
- XDMAC_CC_PROT_SEC |
- XDMAC_CC_MBSIZE_SINGLE |
- XDMAC_CC_DSYNC_MEM2PER | XDMAC_CC_CSIZE(cryp->dmachunksize) |
- XDMAC_CC_DWIDTH(cryp->dmawith) |
- XDMAC_CC_SIF_AHB_IF0 |
- XDMAC_CC_DIF_AHB_IF1 |
- XDMAC_CC_SAM_INCREMENTED_AM |
- XDMAC_CC_DAM_FIXED_AM |
- XDMAC_CC_PERID(PERID_AES_TX);
+ XDMAC_CC_PROT_SEC |
+ XDMAC_CC_MBSIZE_SINGLE |
+ XDMAC_CC_DSYNC_MEM2PER | XDMAC_CC_CSIZE(cryp->dmachunksize) |
+ XDMAC_CC_DWIDTH(cryp->dmawith) |
+ XDMAC_CC_SIF_AHB_IF0 |
+ XDMAC_CC_DIF_AHB_IF1 |
+ XDMAC_CC_SAM_INCREMENTED_AM |
+ XDMAC_CC_DAM_FIXED_AM |
+ XDMAC_CC_PERID(PERID_AES_TX);
dmaChannelSetMode(cryp->dmarx, cryp->rxdmamode);
dmaChannelSetMode(cryp->dmatx, cryp->txdmamode);
@@ -223,10 +219,10 @@ cryerror_t sama_aes_lld_process_dma(CRYDriver *cryp, aesparams *params,
dmaChannelSetTransactionSize(cryp->dmarx, ( indata_len / DMA_DATA_WIDTH_TO_BYTE(cryp->dmawith)));
//AES soft reset
- AES->AES_CR = AES_CR_SWRST;
+ AES->AES_CR = AES_CR_SWRST;
//AES set op mode
- AES->AES_MR |= ((AES_MR_OPMOD_Msk & (params->mode)) | AES_MR_CKEY_PASSWD);
+ AES->AES_MR |= ((AES_MR_OPMOD_Msk & (params->mode)) | AES_MR_CKEY_PASSWD);
//AES set key size
ret = sama_aes_lld_set_key_size(cryp->key0_size);
@@ -260,7 +256,7 @@ cryerror_t sama_aes_lld_process_dma(CRYDriver *cryp, aesparams *params,
osalSysUnlock();
osalMutexUnlock(&cryp->mutex);
-
+#endif //#if defined(SAMA_DMA_REQUIRED)
return CRY_NOERROR;
}
diff --git a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h
index b7a1a9fae..029a209ac 100644
--- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h
+++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h
@@ -42,6 +42,21 @@ extern void samaCryptoDriverDisable(CRYDriver *cryp);
#define DMA_DATA_WIDTH_TO_BYTE(w) (1 << w)
+#ifndef SAMA_CRY_CRYD1_DMA_IRQ_PRIORITY
+#define SAMA_CRY_CRYD1_DMA_IRQ_PRIORITY 4
+#endif
+
+#ifndef SAMA_CRY_CRYD1_IRQ_PRIORITY
+#define SAMA_CRY_CRYD1_IRQ_PRIORITY 4
+#endif
+
+#ifndef SAMA_CRY_DMA_ERROR_HOOK
+#define SAMA_CRY_DMA_ERROR_HOOK(cryp) osalSysHalt("DMA failure")
+#endif
+
+#ifndef SAMA_CRY_SHA_UPDATE_LEN_MAX
+#define SAMA_CRY_SHA_UPDATE_LEN_MAX 128*1024
+#endif
#include "sama_aes_lld.h"
diff --git a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c
index 79432e008..a2c925cd5 100644
--- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c
+++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c
@@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-*/
+ */
#include "hal.h"
#include <string.h>
#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)
@@ -24,14 +24,7 @@ static inline uint32_t min_u32(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
-struct sha_data {
- uint32_t remaining;
- uint32_t processed;
- uint32_t block_size;
- uint32_t output_size;
- shadalgo_t algo;
- uint8_t hmac;
-};
+
static uint32_t shaOutputSize(shadalgo_t algo);
@@ -39,11 +32,11 @@ static uint32_t shadPaddedMessSize(uint8_t mode, uint32_t len);
uint8_t shaBlockSize(shadalgo_t algo);
static void loadData(const uint8_t* data, int len);
static void readData(const uint8_t* data, int len);
-static uint32_t processBlockPolling(CRYDriver *cryp, uint32_t len, uint32_t block_size);
-static uint32_t processBlockDMA(CRYDriver *cryp, uint32_t len, uint32_t block_size);
+static uint32_t processBlockPolling(const uint8_t *data,uint32_t len, uint32_t block_size);
+static uint32_t processBlockDMA(CRYDriver *cryp,const uint8_t *data, uint32_t len, uint32_t block_size);
static void update(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data, uint32_t data_size);
-static uint32_t fillPadding(struct sha_data *shadata, uint32_t len, uint8_t* buffer);
+static uint32_t fillPadding(struct sha_data *shadata, uint32_t len, uint8_t* buffer,size_t buffer_size);
@@ -53,62 +46,99 @@ uint8_t shaDigestSize(shadalgo_t algo)
{
switch(algo)
{
- case CRY_SHA_1:
- return 20;
- break;
- case CRY_SHA_224:
- return 28;
- break;
- case CRY_SHA_256:
- return 32;
- break;
- case CRY_SHA_384:
- return 48;
- break;
- case CRY_SHA_512:
- return 64;
- break;
- default:
- return 0;
+ case CRY_SHA_1:
+ return 20;
+ break;
+ case CRY_SHA_224:
+ return 28;
+ break;
+ case CRY_SHA_256:
+ return 32;
+ break;
+ case CRY_SHA_384:
+ return 48;
+ break;
+ case CRY_SHA_512:
+ return 64;
+ break;
+ default:
+ return 0;
}
}
-int sha_finish(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* buffer,uint32_t buffer_size)
+static cryerror_t sha_finish(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* buffer,uint32_t buffer_size)
{
uint32_t padding_len=0;
+
if (buffer_size < shadata->output_size)
- return -1;
+ return CRY_ERR_INV_ALGO;
//pad data for the end of the buffer
- padding_len = fillPadding(shadata,
- shadata->processed + shadata->remaining,
- &cryp->sha_buffer[shadata->remaining]
- );
+ padding_len = fillPadding(shadata,shadata->processed + shadata->remaining,&shadata->sha_buffer[shadata->remaining],shadata->sha_buffer_size - shadata->remaining);
if (cryp->config->transfer_mode == TRANSFER_POLLING)
- processBlockPolling(cryp, shadata->remaining + padding_len, shadata->block_size);
+ processBlockPolling(shadata->sha_buffer,shadata->remaining + padding_len, shadata->block_size);
else
- processBlockDMA(cryp, shadata->remaining + padding_len, shadata->block_size);
+ processBlockDMA(cryp,shadata->sha_buffer,shadata->remaining + padding_len, shadata->block_size);
readData(buffer, buffer_size);
+ return CRY_NOERROR;
- return 0;
}
-cryerror_t sama_sha_lld_process(CRYDriver *cryp,
- shaparams_t *params,
- const uint8_t *in,
- uint8_t *out,
- size_t indata_len
- )
+cryerror_t sama_sha_lld_update(CRYDriver *cryp, struct sha_data *sha)
+{
+ uint32_t buf_in_size;
+ uint8_t* p;
+ osalMutexLock(&cryp->mutex);
+
+ p = (uint8_t*)sha->in;
+
+ while (sha->indata_len) {
+ buf_in_size = min_u32(sha->indata_len, SAMA_CRY_SHA_UPDATE_LEN_MAX);
+
+ //First block
+ if (!sha->processed) {
+ SHA->SHA_CR = SHA_CR_FIRST;
+ }
+
+ update(cryp, sha, p, buf_in_size);
+
+ sha->indata_len -= buf_in_size;
+ p += buf_in_size;
+ }
+ osalMutexUnlock(&cryp->mutex);
+
+ return CRY_NOERROR;
+}
+
+cryerror_t sama_sha_lld_final(CRYDriver *cryp, struct sha_data *sha)
+{
+ cryerror_t err = CRY_NOERROR;
+ osalMutexLock(&cryp->mutex);
+
+ if (!sha->processed) {
+ SHA->SHA_CR = SHA_CR_FIRST;
+ }
+
+ err = sha_finish(cryp, sha, sha->out, sha->output_size);
+
+ osalMutexUnlock(&cryp->mutex);
+
+ return err;
+}
+
+
+
+cryerror_t sama_sha_lld_init(CRYDriver *cryp, struct sha_data *sha)
{
uint32_t algoregval;
- struct sha_data shadata;
+ cryerror_t cryerr = CRY_NOERROR;
if (!(cryp->enabledPer & SHA_PER)) {
cryp->enabledPer |= SHA_PER;
@@ -117,17 +147,19 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp,
osalMutexLock(&cryp->mutex);
- shadata.processed = 0;
- shadata.remaining = 0;
- shadata.output_size = shaOutputSize(params->algo);
- shadata.block_size = shaBlockSize(params->algo);
- shadata.algo = params->algo;
- if (shadata.output_size == 0) {
+ sha->processed = 0;
+ sha->remaining = 0;
+ sha->output_size = shaOutputSize(sha->algo);
+ sha->block_size = shaBlockSize(sha->algo);
+
+
+ if (sha->output_size == 0) {
+ osalMutexUnlock(&cryp->mutex);
return CRY_ERR_INV_ALGO;
}
- switch (params->algo) {
+ switch (sha->algo) {
case CRY_SHA_1:
algoregval = SHA_MR_ALGO_SHA1;
break;
@@ -148,12 +180,15 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp,
break;
#endif
default:
+ osalMutexUnlock(&cryp->mutex);
return CRY_ERR_INV_ALGO;
}
+
//soft reset
SHA->SHA_CR = SHA_CR_SWRST;
+
if (cryp->config->transfer_mode == TRANSFER_POLLING) {
algoregval |= SHA_MR_SMOD_MANUAL_START;
} else {
@@ -163,15 +198,15 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp,
cryp->dmachunksize = DMA_CHUNK_SIZE_16;
cryp->txdmamode = XDMAC_CC_TYPE_PER_TRAN |
- XDMAC_CC_PROT_SEC |
- XDMAC_CC_MBSIZE_SINGLE |
- XDMAC_CC_DSYNC_MEM2PER | XDMAC_CC_CSIZE(cryp->dmachunksize) |
- XDMAC_CC_DWIDTH(cryp->dmawith) |
- XDMAC_CC_SIF_AHB_IF0 |
- XDMAC_CC_DIF_AHB_IF1 |
- XDMAC_CC_SAM_INCREMENTED_AM |
- XDMAC_CC_DAM_FIXED_AM |
- XDMAC_CC_PERID(PERID_SHA_TX);
+ XDMAC_CC_PROT_SEC |
+ XDMAC_CC_MBSIZE_SINGLE |
+ XDMAC_CC_DSYNC_MEM2PER | XDMAC_CC_CSIZE(cryp->dmachunksize) |
+ XDMAC_CC_DWIDTH(cryp->dmawith) |
+ XDMAC_CC_SIF_AHB_IF0 |
+ XDMAC_CC_DIF_AHB_IF1 |
+ XDMAC_CC_SAM_INCREMENTED_AM |
+ XDMAC_CC_DAM_FIXED_AM |
+ XDMAC_CC_PERID(PERID_SHA_TX);
cryp->rxdmamode = 0xFFFFFFFF;
@@ -184,31 +219,15 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp,
//enable interrupt
SHA->SHA_IER = SHA_IER_DATRDY;
- uint32_t buf_in_size;
- const uint8_t *p = in;
-
- while (indata_len) {
- buf_in_size = min_u32(indata_len, SHA_UPDATE_LEN);
-
- //First block
- if (!shadata.processed) {
- SHA->SHA_CR = SHA_CR_FIRST;
- }
-
- update(cryp, &shadata, in, buf_in_size);
-
- p += buf_in_size;
- indata_len -= buf_in_size;
- }
- sha_finish(cryp, &shadata, out, shadata.output_size);
osalMutexUnlock(&cryp->mutex);
- return CRY_NOERROR;
+ return cryerr;
}
+
static uint32_t shaOutputSize(shadalgo_t algo)
{
switch (algo) {
@@ -253,7 +272,7 @@ uint8_t shaBlockSize(shadalgo_t algo)
return 128;
}
- return 64;
+ return 64;
}
@@ -270,6 +289,7 @@ static void loadData(const uint8_t* data, int len)
SHA->SHA_IODATAR[i - 16] = value;
}
}
+
static void readData(const uint8_t* data, int len)
{
int i;
@@ -277,19 +297,20 @@ static void readData(const uint8_t* data, int len)
for (i = 0; i < (len / 4) && i < 16; i++) {
value = SHA->SHA_IODATAR[i];
- memcpy(&data[i * 4], &value, 4);
+ memcpy((uint8_t*)&data[i * 4], &value, 4);
}
}
-static uint32_t processBlockPolling(CRYDriver *cryp,uint32_t len, uint32_t block_size)
+static uint32_t processBlockPolling(const uint8_t *data,uint32_t len, uint32_t block_size)
{
uint32_t processed = 0;
+
while ((len - processed) >= block_size) {
// load data in the sha input registers
- loadData(&cryp->sha_buffer[processed], block_size);
+ loadData(&data[processed], block_size);
SHA->SHA_CR = SHA_CR_START;
@@ -302,7 +323,7 @@ static uint32_t processBlockPolling(CRYDriver *cryp,uint32_t len, uint32_t block
return processed;
}
-static uint32_t processBlockDMA(CRYDriver *cryp, uint32_t len, uint32_t block_size)
+static uint32_t processBlockDMA(CRYDriver *cryp, const uint8_t *data,uint32_t len, uint32_t block_size)
{
uint32_t processed = 0;
@@ -310,7 +331,7 @@ static uint32_t processBlockDMA(CRYDriver *cryp, uint32_t len, uint32_t block_si
// load data in the sha input registers
// Writing channel
- dmaChannelSetSource(cryp->dmatx, &cryp->sha_buffer[processed]);
+ dmaChannelSetSource(cryp->dmatx, &data[processed]);
dmaChannelSetDestination(cryp->dmatx, SHA->SHA_IDATAR);
dmaChannelSetTransactionSize(cryp->dmatx,
(block_size / DMA_DATA_WIDTH_TO_BYTE(cryp->dmawith)));
@@ -338,7 +359,7 @@ static void update(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data,
if (shadata->remaining) {
//complete previous data
uint32_t complement = min_u32(data_size, shadata->block_size - shadata->remaining);
- memcpy(&cryp->sha_buffer[shadata->remaining], data, complement);
+ memcpy(&shadata->sha_buffer[shadata->remaining], data, complement);
shadata->remaining += complement;
data += complement;
data_size -= complement;
@@ -346,9 +367,9 @@ static void update(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data,
//if data is complete process the block
if (shadata->remaining == shadata->block_size) {
if (cryp->config->transfer_mode == TRANSFER_POLLING )
- processBlockPolling(cryp,shadata->remaining, shadata->block_size);
+ processBlockPolling(shadata->sha_buffer,shadata->remaining, shadata->block_size);
else
- processBlockDMA(cryp, shadata->remaining, shadata->block_size);
+ processBlockDMA(cryp, shadata->sha_buffer,shadata->remaining, shadata->block_size);
shadata->processed += shadata->block_size;
shadata->remaining = 0;
@@ -360,9 +381,9 @@ static void update(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data,
// Process blocks
if (cryp->config->transfer_mode == TRANSFER_POLLING )
- processed = processBlockPolling(cryp, data_size, shadata->block_size);
+ processed = processBlockPolling(data,data_size, shadata->block_size);
else
- processed = processBlockDMA(cryp, data_size, shadata->block_size);
+ processed = processBlockDMA(cryp, data,data_size, shadata->block_size);
shadata->processed += processed;
@@ -371,17 +392,20 @@ static void update(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data,
if (shadata->remaining)
{
for (i=0;i<shadata->remaining;i++)
- cryp->sha_buffer[i] = data[processed+i];
+ shadata->sha_buffer[i] = data[processed+i];
}
}
-static uint32_t fillPadding(struct sha_data *shadata, uint32_t len, uint8_t* buffer)
+static uint32_t fillPadding(struct sha_data *shadata, uint32_t len, uint8_t* buffer,size_t buffer_size)
{
- uint32_t padding_len = shadPaddedMessSize(shadata->algo, len) - len;
- uint32_t k = padding_len - 9;
+ uint32_t padding_len,k;
+
+
+ padding_len = shadPaddedMessSize(shadata->algo, len);
+ padding_len -= len;
+ k = padding_len - 9;
- osalDbgAssert( padding_len <= (SHA_MAX_PADDING_LEN - shadata->remaining),
- "invalid buffer size");
+ osalDbgAssert( padding_len <= buffer_size,"invalid buffer size");
// Append "1" bit and seven "0" bits to the end of the message
diff --git a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h
index bc883aeed..e112e64e9 100644
--- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h
+++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h
@@ -17,13 +17,9 @@
#define CRYPTOLIB_LLD_SAMA_SHA_H_
-
-cryerror_t sama_sha_lld_process(CRYDriver *cryp,
- shaparams_t *params,
- const uint8_t *in,
- uint8_t *out,
- size_t indata_len
- );
+cryerror_t sama_sha_lld_init(CRYDriver *cryp, struct sha_data *sha);
+cryerror_t sama_sha_lld_update(CRYDriver *cryp, struct sha_data *sha);
+cryerror_t sama_sha_lld_final(CRYDriver *cryp, struct sha_data *sha);
diff --git a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c
index 2e8a67309..5f1c62339 100644
--- a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c
+++ b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c
@@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-*/
+ */
/**
* @file hal_crypto_lld.c
@@ -73,10 +73,11 @@ void cry_lld_init(void) {
#endif
}
+
/**
* @brief Configures and activates the crypto peripheral.
*
- * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] cryp pointer to the @p CRYDriver object
*
* @notapi
*/
@@ -85,9 +86,9 @@ void cry_lld_start(CRYDriver *cryp) {
if (cryp->state == CRY_STOP) {
//clear key
for (size_t i=0;i<KEY0_BUFFER_SIZE_W;i++)
- {
+ {
cryp->key0_buffer[i] = 0;
- }
+ }
#if PLATFORM_CRY_USE_CRY1
if (&CRYD1 == cryp) {
@@ -119,9 +120,11 @@ void cry_lld_stop(CRYDriver *cryp) {
/**
* @brief Initializes the transient key for a specific algorithm.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] keyp pointer to the key data
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] algorithm the algorithm identifier
+ * @param[in] size key size in bytes
+ * @param[in] keyp pointer to the key data
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the specified algorithm is unknown or
* unsupported.
@@ -129,8 +132,11 @@ void cry_lld_stop(CRYDriver *cryp) {
*
* @notapi
*/
-cryerror_t cry_lld_loadkey(CRYDriver *cryp, cryalgorithm_t algorithm,
- size_t size, const uint8_t *keyp) {
+cryerror_t cry_lld_loadkey(CRYDriver *cryp,
+ cryalgorithm_t algorithm,
+ size_t size,
+ const uint8_t *keyp) {
+
uint8_t *p = (uint8_t *)cryp->key0_buffer;
@@ -138,27 +144,27 @@ cryerror_t cry_lld_loadkey(CRYDriver *cryp, cryalgorithm_t algorithm,
(void)algorithm;
- if (size <= HAL_CRY_MAX_KEY_SIZE)
+ if (size <= HAL_CRY_MAX_KEY_SIZE)
+ {
+ osalMutexLock(&cryp->mutex);
+ //clear key
+ for (size_t i=0;i<KEY0_BUFFER_SIZE_W;i++)
{
- osalMutexLock(&cryp->mutex);
- //clear key
- for (size_t i=0;i<KEY0_BUFFER_SIZE_W;i++)
- {
- cryp->key0_buffer[i] = 0;
- }
-
- for (size_t i=0;i<size;i++)
- {
- p[i] = keyp[i];
- }
- osalMutexUnlock(&cryp->mutex);
+ cryp->key0_buffer[i] = 0;
}
- else
+
+ for (size_t i=0;i<size;i++)
{
- return CRY_ERR_INV_KEY_SIZE;
+ p[i] = keyp[i];
}
+ osalMutexUnlock(&cryp->mutex);
+ }
+ else
+ {
+ return CRY_ERR_INV_KEY_SIZE;
+ }
- return CRY_NOERROR;
+ return CRY_NOERROR;
}
@@ -167,13 +173,13 @@ cryerror_t cry_lld_loadkey(CRYDriver *cryp, cryalgorithm_t algorithm,
* @note The implementation of this function must guarantee that it can
* be called from any context.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -183,30 +189,33 @@ cryerror_t cry_lld_loadkey(CRYDriver *cryp, cryalgorithm_t algorithm,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_AES(CRYDriver *cryp, crykey_t key_id,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_encrypt_AES(CRYDriver *cryp,
+ crykey_t key_id,
+ const uint8_t *in,
+ uint8_t *out) {
+
cryerror_t ret = CRY_NOERROR;
- aesparams params;
+ aesparams params;
- if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ if(key_id != 0 )
+ return CRY_ERR_INV_KEY_ID;
- if (!(cryp->enabledPer & AES_PER)) {
- cryp->enabledPer |= AES_PER;
- pmcEnableAES();
- }
+ if (!(cryp->enabledPer & AES_PER)) {
+ cryp->enabledPer |= AES_PER;
+ pmcEnableAES();
+ }
- params.encrypt = 1;
- params.block_size = 16;
- params.mode = 0;
- params.iv = NULL;
+ params.encrypt = 1;
+ params.block_size = 16;
+ params.mode = 0;
+ params.iv = NULL;
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, 16);
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, 16);
- return ret;
+ return ret;
}
/**
@@ -214,13 +223,13 @@ cryerror_t cry_lld_encrypt_AES(CRYDriver *cryp, crykey_t key_id,
* @note The implementation of this function must guarantee that it can
* be called from any context.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] in buffer containing the input cyphertext
+ * @param[out] out buffer for the output plaintext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -230,30 +239,33 @@ cryerror_t cry_lld_encrypt_AES(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_AES(CRYDriver *cryp, crykey_t key_id,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_decrypt_AES(CRYDriver *cryp,
+ crykey_t key_id,
+ const uint8_t *in,
+ uint8_t *out) {
+
cryerror_t ret = CRY_NOERROR;
- aesparams params;
+ aesparams params;
- if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ if(key_id != 0 )
+ return CRY_ERR_INV_KEY_ID;
- if (!(cryp->enabledPer & AES_PER)) {
- cryp->enabledPer |= AES_PER;
- pmcEnableAES();
- }
+ if (!(cryp->enabledPer & AES_PER)) {
+ cryp->enabledPer |= AES_PER;
+ pmcEnableAES();
+ }
- params.encrypt = 0;
- params.block_size = 16;
- params.mode = 0;
- params.iv = NULL;
+ params.encrypt = 0;
+ params.block_size = 16;
+ params.mode = 0;
+ params.iv = NULL;
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, 16);
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, 16);
- return ret;
+ return ret;
}
/**
@@ -262,15 +274,15 @@ cryerror_t cry_lld_decrypt_AES(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of the selected key size
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of the selected key size
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -280,8 +292,11 @@ cryerror_t cry_lld_decrypt_AES(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
@@ -315,15 +330,15 @@ cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of the selected key size
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of the selected key size
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -333,13 +348,16 @@ cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ return CRY_ERR_INV_KEY_ID;
if (!(cryp->enabledPer & AES_PER)) {
cryp->enabledPer |= AES_PER;
@@ -347,15 +365,15 @@ cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
}
params.encrypt = 0;
- params.block_size = 16;
- params.mode = AES_MR_OPMOD_ECB;
+ params.block_size = 16;
+ params.mode = AES_MR_OPMOD_ECB;
- if (cryp->config->transfer_mode == TRANSFER_POLLING)
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
- else
- ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
+ if (cryp->config->transfer_mode == TRANSFER_POLLING)
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
+ else
+ ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
@@ -368,16 +386,16 @@ cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of the selected key size
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits initial vector
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of the selected key size
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 128 bits initial vector
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -387,13 +405,17 @@ cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ return CRY_ERR_INV_KEY_ID;
if (!(cryp->enabledPer & AES_PER)) {
cryp->enabledPer |= AES_PER;
@@ -405,10 +427,10 @@ cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
params.mode = AES_MR_OPMOD_CBC;
params.iv = iv;
- if (cryp->config->transfer_mode == TRANSFER_POLLING)
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
- else
- ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
+ if (cryp->config->transfer_mode == TRANSFER_POLLING)
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
+ else
+ ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
@@ -421,16 +443,16 @@ cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of the selected key size
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits initial vector
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of the selected key size
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 128 bits initial vector
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -440,13 +462,17 @@ cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ return CRY_ERR_INV_KEY_ID;
if (!(cryp->enabledPer & AES_PER)) {
cryp->enabledPer |= AES_PER;
@@ -455,13 +481,13 @@ cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
}
params.encrypt = 0;
- params.block_size = 16;
- params.mode = AES_MR_OPMOD_CBC;
- params.iv = iv;
- if (cryp->config->transfer_mode == TRANSFER_POLLING)
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
- else
- ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
+ params.block_size = 16;
+ params.mode = AES_MR_OPMOD_CBC;
+ params.iv = iv;
+ if (cryp->config->transfer_mode == TRANSFER_POLLING)
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
+ else
+ ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
@@ -474,16 +500,16 @@ cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of the selected key size
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits initial vector
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of the selected key size
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 128 bits initial vector
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -493,13 +519,17 @@ cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ return CRY_ERR_INV_KEY_ID;
if (!(cryp->enabledPer & AES_PER)) {
cryp->enabledPer |= AES_PER;
@@ -507,31 +537,31 @@ cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
;
}
params.encrypt = 1;
- params.block_size = 16;
- switch (cryp->config->cfbs) {
- case AES_CFBS_128:
- params.block_size = 16;
- break;
- case AES_CFBS_64:
- params.block_size = 8;
- break;
- case AES_CFBS_32:
- params.block_size = 4;
- break;
- case AES_CFBS_16:
- params.block_size = 2;
- break;
- case AES_CFBS_8:
- params.block_size = 1;
- break;
- }
- params.mode = AES_MR_OPMOD_CFB;
- params.iv = iv;
-
- if (cryp->config->transfer_mode == TRANSFER_POLLING)
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
- else
- ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
+ params.block_size = 16;
+ switch (cryp->config->cfbs) {
+ case AES_CFBS_128:
+ params.block_size = 16;
+ break;
+ case AES_CFBS_64:
+ params.block_size = 8;
+ break;
+ case AES_CFBS_32:
+ params.block_size = 4;
+ break;
+ case AES_CFBS_16:
+ params.block_size = 2;
+ break;
+ case AES_CFBS_8:
+ params.block_size = 1;
+ break;
+ }
+ params.mode = AES_MR_OPMOD_CFB;
+ params.iv = iv;
+
+ if (cryp->config->transfer_mode == TRANSFER_POLLING)
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
+ else
+ ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
return ret;
@@ -543,16 +573,16 @@ cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of the selected key size
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits initial vector
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of the selected key size
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 128 bits initial vector
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -562,13 +592,17 @@ cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ return CRY_ERR_INV_KEY_ID;
if (!(cryp->enabledPer & AES_PER)) {
cryp->enabledPer |= AES_PER;
@@ -577,31 +611,31 @@ cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
}
params.encrypt = 0;
- params.block_size = 16;
- switch (cryp->config->cfbs) {
- case AES_CFBS_128:
- params.block_size = 16;
- break;
- case AES_CFBS_64:
- params.block_size = 8;
- break;
- case AES_CFBS_32:
- params.block_size = 4;
- break;
- case AES_CFBS_16:
- params.block_size = 2;
- break;
- case AES_CFBS_8:
- params.block_size = 1;
- break;
- }
- params.mode = AES_MR_OPMOD_CFB;
- params.iv = iv;
-
- if (cryp->config->transfer_mode == TRANSFER_POLLING)
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
- else
- ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
+ params.block_size = 16;
+ switch (cryp->config->cfbs) {
+ case AES_CFBS_128:
+ params.block_size = 16;
+ break;
+ case AES_CFBS_64:
+ params.block_size = 8;
+ break;
+ case AES_CFBS_32:
+ params.block_size = 4;
+ break;
+ case AES_CFBS_16:
+ params.block_size = 2;
+ break;
+ case AES_CFBS_8:
+ params.block_size = 1;
+ break;
+ }
+ params.mode = AES_MR_OPMOD_CFB;
+ params.iv = iv;
+
+ if (cryp->config->transfer_mode == TRANSFER_POLLING)
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
+ else
+ ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
@@ -614,17 +648,17 @@ cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits initial vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of 16
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 128 bits initial vector + counter, it contains
+ * a 96 bits IV and a 32 bits counter
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -634,13 +668,17 @@ cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
if(key_id != 0 )
- return CRY_ERR_INV_KEY_ID;
+ return CRY_ERR_INV_KEY_ID;
if (!(cryp->enabledPer & AES_PER)) {
cryp->enabledPer |= AES_PER;
@@ -649,13 +687,13 @@ cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id,
}
params.encrypt = 1;
- params.block_size = 16;
- params.mode = AES_MR_OPMOD_CTR;
- params.iv = iv;
- if (cryp->config->transfer_mode == TRANSFER_POLLING)
- ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
- else
- ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
+ params.block_size = 16;
+ params.mode = AES_MR_OPMOD_CTR;
+ params.iv = iv;
+ if (cryp->config->transfer_mode == TRANSFER_POLLING)
+ ret = sama_aes_lld_process_polling(cryp, &params, in, out, size);
+ else
+ ret = sama_aes_lld_process_dma(cryp, &params, in, out, size);
@@ -668,17 +706,17 @@ cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @param[in] iv 128 bits initial vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of 16
+ * @param[in] in buffer containing the input cyphertext
+ * @param[out] out buffer for the output plaintext
+ * @param[in] iv 128 bits initial vector + counter, it contains
+ * a 96 bits IV and a 32 bits counter
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -688,8 +726,12 @@ cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_decrypt_AES_CTR(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
aesparams params;
@@ -720,21 +762,22 @@ cryerror_t cry_lld_decrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the text buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits initial vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @param[in] aadsize size of the authentication data, this number must be a
- * multiple of 16
- * @param[in] aad buffer containing the authentication data
- * @param[in] authtag 128 bits buffer for the generated authentication tag
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the text buffers, this number must be a
+ * multiple of 16
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 128 bits initial vector + counter, it contains
+ * a 96 bits IV and a 32 bits counter
+ * @param[in] aadsize size of the authentication data, this number
+ * must be a multiple of 16
+ * @param[in] aad buffer containing the authentication data
+ * @param[in] authtag 128 bits buffer for the generated authentication
+ * tag
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -754,17 +797,17 @@ cryerror_t cry_lld_encrypt_AES_GCM(CRYDriver *cryp,
const uint8_t *aad,
uint8_t *authtag) {
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- (void)aadsize;
- (void)aad;
- (void)authtag;
-
- return CRY_ERR_INV_ALGO;
+ (void)cryp;
+ (void)key_id;
+ (void)size;
+ (void)in;
+ (void)out;
+ (void)iv;
+ (void)aadsize;
+ (void)aad;
+ (void)authtag;
+
+ return CRY_ERR_INV_ALGO;
}
/**
@@ -773,21 +816,22 @@ cryerror_t cry_lld_encrypt_AES_GCM(CRYDriver *cryp,
* of an AES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the text buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer for the output cyphertext
- * @param[out] out buffer containing the input plaintext
- * @param[in] iv 128 bits initial vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @param[in] aadsize size of the authentication data, this number must be a
- * multiple of 16
- * @param[in] aad buffer containing the authentication data
- * @param[in] authtag 128 bits buffer for the generated authentication tag
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the text buffers, this number must be a
+ * multiple of 16
+ * @param[in] in buffer for the output cyphertext
+ * @param[out] out buffer containing the input plaintext
+ * @param[in] iv 128 bits initial vector + counter, it contains
+ * a 96 bits IV and a 32 bits counter
+ * @param[in] aadsize size of the authentication data, this number
+ * must be a multiple of 16
+ * @param[in] aad buffer containing the authentication data
+ * @param[in] authtag 128 bits buffer for the generated authentication
+ * tag
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -807,17 +851,17 @@ cryerror_t cry_lld_decrypt_AES_GCM(CRYDriver *cryp,
const uint8_t *aad,
uint8_t *authtag) {
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- (void)aadsize;
- (void)aad;
- (void)authtag;
-
- return CRY_ERR_INV_ALGO;
+ (void)cryp;
+ (void)key_id;
+ (void)size;
+ (void)in;
+ (void)out;
+ (void)iv;
+ (void)aadsize;
+ (void)aad;
+ (void)authtag;
+
+ return CRY_ERR_INV_ALGO;
}
@@ -826,13 +870,13 @@ cryerror_t cry_lld_decrypt_AES_GCM(CRYDriver *cryp,
* @note The implementation of this function must guarantee that it can
* be called from any context.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -842,20 +886,28 @@ cryerror_t cry_lld_decrypt_AES_GCM(CRYDriver *cryp,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_DES(CRYDriver *cryp, crykey_t key_id,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_encrypt_DES(CRYDriver *cryp,
+ crykey_t key_id,
+ const uint8_t *in,
+ uint8_t *out) {
cryerror_t ret = CRY_NOERROR;
+ tdes_config_t params = { 0, 0 };
if (key_id != 0)
return CRY_ERR_INV_KEY_ID;
- if ((cryp->config->tdes_algo == TDES_ALGO_SINGLE && cryp->key0_size != 8)
- || (cryp->config->tdes_algo == TDES_ALGO_TRIPLE
- && !(cryp->key0_size == 16 || cryp->key0_size == 24)))
+ if (cryp->key0_size == 8)
+ {
+ params.algo = TDES_ALGO_SINGLE;
+ }
+ else if (cryp->key0_size == 16 || cryp->key0_size == 24)
+ {
+ params.algo = TDES_ALGO_TRIPLE;
+ }
+ else
return CRY_ERR_INV_KEY_SIZE;
- tdes_config_t params = { cryp->config->tdes_algo, 0 };
if (!(cryp->enabledPer & TDES_PER)) {
cryp->enabledPer |= TDES_PER;
@@ -873,13 +925,13 @@ cryerror_t cry_lld_encrypt_DES(CRYDriver *cryp, crykey_t key_id,
* be called from any context.
*
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] in buffer containing the input cyphertext
+ * @param[out] out buffer for the output plaintext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -889,17 +941,26 @@ cryerror_t cry_lld_encrypt_DES(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_DES(CRYDriver *cryp, crykey_t key_id,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_decrypt_DES(CRYDriver *cryp,
+ crykey_t key_id,
+ const uint8_t *in,
+ uint8_t *out) {
cryerror_t ret = CRY_NOERROR;
- tdes_config_t params = { cryp->config->tdes_algo, 0 };
+ tdes_config_t params = { 0, 0 };
+
if (key_id != 0)
return CRY_ERR_INV_KEY_ID;
- if ((cryp->config->tdes_algo == TDES_ALGO_SINGLE && cryp->key0_size != 8)
- || (cryp->config->tdes_algo == TDES_ALGO_TRIPLE
- && !(cryp->key0_size == 16 || cryp->key0_size == 24)))
+ if (cryp->key0_size == 8)
+ {
+ params.algo = TDES_ALGO_SINGLE;
+ }
+ else if (cryp->key0_size == 16 || cryp->key0_size == 24)
+ {
+ params.algo = TDES_ALGO_TRIPLE;
+ }
+ else
return CRY_ERR_INV_KEY_SIZE;
if (!(cryp->enabledPer & TDES_PER)) {
@@ -918,15 +979,15 @@ cryerror_t cry_lld_decrypt_DES(CRYDriver *cryp, crykey_t key_id,
* of an DES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of 8
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -936,21 +997,27 @@ cryerror_t cry_lld_decrypt_DES(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_encrypt_DES_ECB(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out) {
cryerror_t ret = CRY_NOERROR;
- tdes_config_t params = { cryp->config->tdes_algo, TDES_MODE_ECB };
-
+ tdes_config_t params = { 0, TDES_MODE_ECB };
if (key_id != 0)
return CRY_ERR_INV_KEY_ID;
- if (cryp->config->tdes_algo == TDES_ALGO_SINGLE && cryp->key0_size != 8) {
- return CRY_ERR_INV_KEY_SIZE;
+ if (cryp->key0_size == 8)
+ {
+ params.algo = TDES_ALGO_SINGLE;
}
- if (cryp->config->tdes_algo == TDES_ALGO_TRIPLE && !(cryp->key0_size == 16 || cryp->key0_size == 24) ) {
- return CRY_ERR_INV_KEY_SIZE;
+ else if (cryp->key0_size == 16 || cryp->key0_size == 24)
+ {
+ params.algo = TDES_ALGO_TRIPLE;
}
+ else
+ return CRY_ERR_INV_KEY_SIZE;
if (!(cryp->enabledPer & TDES_PER)) {
cryp->enabledPer |= TDES_PER;
@@ -970,15 +1037,15 @@ cryerror_t cry_lld_encrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id,
* of an DES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of 8
+ * @param[in] in buffer containing the input cyphertext
+ * @param[out] out buffer for the output plaintext
+ * @return T he operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -988,20 +1055,28 @@ cryerror_t cry_lld_encrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_decrypt_DES_ECB(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out) {
cryerror_t ret = CRY_NOERROR;
- tdes_config_t params = { cryp->config->tdes_algo, TDES_MODE_ECB };
+ tdes_config_t params = { 0, TDES_MODE_ECB };
if (key_id != 0)
return CRY_ERR_INV_KEY_ID;
- if (cryp->config->tdes_algo == TDES_ALGO_SINGLE && cryp->key0_size != 8) {
- return CRY_ERR_INV_KEY_SIZE;
+ if (cryp->key0_size == 8)
+ {
+ params.algo = TDES_ALGO_SINGLE;
}
- if (cryp->config->tdes_algo == TDES_ALGO_TRIPLE && !(cryp->key0_size == 16 || cryp->key0_size == 24) ) {
- return CRY_ERR_INV_KEY_SIZE;
+ else if (cryp->key0_size == 16 || cryp->key0_size == 24)
+ {
+ params.algo = TDES_ALGO_TRIPLE;
}
+ else
+ return CRY_ERR_INV_KEY_SIZE;
+
if (!(cryp->enabledPer & TDES_PER)) {
cryp->enabledPer |= TDES_PER;
pmcEnableDES()
@@ -1022,16 +1097,16 @@ cryerror_t cry_lld_decrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id,
* of an DES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 64 bits input vector
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of 8
+ * @param[in] in buffer containing the input plaintext
+ * @param[out] out buffer for the output cyphertext
+ * @param[in] iv 64 bits input vector
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -1041,22 +1116,30 @@ cryerror_t cry_lld_decrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_encrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_encrypt_DES_CBC(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
- tdes_config_t params = { cryp->config->tdes_algo, TDES_MODE_CBC };
+ tdes_config_t params = { 0, TDES_MODE_CBC };
if (key_id != 0)
return CRY_ERR_INV_KEY_ID;
- if (cryp->config->tdes_algo == TDES_ALGO_SINGLE)
- return CRY_ERR_INV_ALGO;
-
- if (cryp->key0_size != 16 && cryp->key0_size != 24) {
- return CRY_ERR_INV_KEY_SIZE;
- }
+ if (cryp->key0_size == 8)
+ {
+ params.algo = TDES_ALGO_SINGLE;
+ }
+ else if (cryp->key0_size == 16 || cryp->key0_size == 24)
+ {
+ params.algo = TDES_ALGO_TRIPLE;
+ }
+ else
+ return CRY_ERR_INV_KEY_SIZE;
if (!(cryp->enabledPer & TDES_PER)) {
cryp->enabledPer |= TDES_PER;
@@ -1077,16 +1160,16 @@ cryerror_t cry_lld_encrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id,
* of an DES block, this means that padding must be done by the
* caller.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] key_id the key to be used for the operation, zero is the
- * transient key, other values are keys stored in an
- * unspecified way
- * @param[in] size size of the plaintext buffer, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @param[in] iv 64 bits input vector
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] key_id the key to be used for the operation, zero is
+ * the transient key, other values are keys stored
+ * in an unspecified way
+ * @param[in] size size of the plaintext buffer, this number must
+ * be a multiple of 8
+ * @param[in] in buffer containing the input cyphertext
+ * @param[out] out buffer for the output plaintext
+ * @param[in] iv 64 bits input vector
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
@@ -1096,21 +1179,29 @@ cryerror_t cry_lld_encrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id,
*
* @notapi
*/
-cryerror_t cry_lld_decrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id,
- size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv) {
+cryerror_t cry_lld_decrypt_DES_CBC(CRYDriver *cryp,
+ crykey_t key_id,
+ size_t size,
+ const uint8_t *in,
+ uint8_t *out,
+ const uint8_t *iv) {
cryerror_t ret = CRY_NOERROR;
- tdes_config_t params = { cryp->config->tdes_algo, TDES_MODE_CBC };
+ tdes_config_t params = { 0, TDES_MODE_CBC };
if (key_id != 0)
return CRY_ERR_INV_KEY_ID;
- if (cryp->config->tdes_algo == TDES_ALGO_SINGLE)
- return CRY_ERR_INV_ALGO;
-
- if (cryp->key0_size != 16 && cryp->key0_size != 24) {
- return CRY_ERR_INV_KEY_SIZE;
+ if (cryp->key0_size == 8)
+ {
+ params.algo = TDES_ALGO_SINGLE;
+ }
+ else if (cryp->key0_size == 16 || cryp->key0_size == 24)
+ {
+ params.algo = TDES_ALGO_TRIPLE;
}
+ else
+ return CRY_ERR_INV_KEY_SIZE;
if (!(cryp->enabledPer & TDES_PER)) {
cryp->enabledPer |= TDES_PER;
@@ -1129,97 +1220,188 @@ cryerror_t cry_lld_decrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id,
/**
- * @brief Hash using SHA1.
- * @NOTE Use of this algorithm is not recommended because proven weak.
+ * @brief Hash initialization using SHA1.
+ * @note Use of this algorithm is not recommended because proven weak.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @param[out] out 160 bits output buffer
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[out] sha1ctxp pointer to a SHA1 context to be initialized
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
*
- * @api
+ * @notapi
*/
-cryerror_t cry_lld_SHA1(CRYDriver *cryp, size_t size,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_SHA1_init(CRYDriver *cryp, SHA1Context *sha1ctxp) {
- cryerror_t ret;
+ sha1ctxp->sha.algo = CRY_SHA_1;
- shaparams_t params = {CRY_SHA_1};
+ return sama_sha_lld_init(cryp, &sha1ctxp->sha);
+}
- ret = sama_sha_lld_process(cryp,
- &params,
- in,
- out,
- size
- );
+/**
+ * @brief Hash update using SHA1.
+ * @note Use of this algorithm is not recommended because proven weak.
+ *
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] sha1ctxp pointer to a SHA1 context
+ * @param[in] size size of input buffer
+ * @param[in] in buffer containing the input text
+ * @return The operation status.
+ * @retval CRY_NOERROR if the operation succeeded.
+ * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
+ * device instance.
+ *
+ * @notapi
+ */
+cryerror_t cry_lld_SHA1_update(CRYDriver *cryp, SHA1Context *sha1ctxp,
+ size_t size, const uint8_t *in) {
- return ret;
+ sha1ctxp->sha.in = in;
+ sha1ctxp->sha.indata_len = size;
+
+ return sama_sha_lld_update(cryp, &sha1ctxp->sha);
}
/**
- * @brief Hash using SHA256.
+ * @brief Hash finalization using SHA1.
+ * @note Use of this algorithm is not recommended because proven weak.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @param[out] out 256 bits output buffer
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] sha1ctxp pointer to a SHA1 context
+ * @param[out] out 160 bits output buffer
+ * @return The operation status.
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
*
- * @api
+ * @notapi
+ */
+cryerror_t cry_lld_SHA1_final(CRYDriver *cryp,
+ SHA1Context *sha1ctxp,
+ uint8_t *out)
+{
+ sha1ctxp->sha.out = out;
+ return sama_sha_lld_final(cryp, &sha1ctxp->sha);
+}
+
+/**
+ * @brief Hash initialization using SHA256.
+ *
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[out] sha256ctxp pointer to a SHA256 context to be initialized
+ * @retval CRY_NOERROR if the operation succeeded.
+ * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
+ * device instance.
+ *
+ * @notapi
+ */
+cryerror_t cry_lld_SHA256_init(CRYDriver *cryp, SHA256Context *sha256ctxp) {
+
+ sha256ctxp->sha.algo = CRY_SHA_256;
+
+ return sama_sha_lld_init(cryp, &sha256ctxp->sha);
+}
+
+/**
+ * @brief Hash update using SHA256.
+ *
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] sha256ctxp pointer to a SHA256 context
+ * @param[in] size size of input buffer
+ * @param[in] in buffer containing the input text
+ * @return The operation status.
+ * @retval CRY_NOERROR if the operation succeeded.
+ * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
+ * device instance.
+ *
+ * @notapi
*/
-cryerror_t cry_lld_SHA256(CRYDriver *cryp, size_t size,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_SHA256_update(CRYDriver *cryp, SHA256Context *sha256ctxp,
+ size_t size, const uint8_t *in) {
- cryerror_t ret;
+ sha256ctxp->sha.in = in;
+ sha256ctxp->sha.indata_len = size;
- shaparams_t params = {CRY_SHA_256};
+ return sama_sha_lld_update(cryp, &sha256ctxp->sha);
+}
- ret = sama_sha_lld_process(cryp,
- &params,
- in,
- out,
- size
- );
+/**
+ * @brief Hash finalization using SHA256.
+ *
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] sha256ctxp pointer to a SHA256 context
+ * @param[out] out 256 bits output buffer
+ * @return The operation status.
+ * @retval CRY_NOERROR if the operation succeeded.
+ * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
+ * device instance.
+ *
+ * @notapi
+ */
+cryerror_t cry_lld_SHA256_final(CRYDriver *cryp, SHA256Context *sha256ctxp,
+ uint8_t *out) {
- return ret;
+ sha256ctxp->sha.out = out;
+ return sama_sha_lld_final(cryp, &sha256ctxp->sha);
}
/**
- * @brief Hash using SHA512.
+ * @brief Hash initialization using SHA512.
*
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @param[out] out 512 bits output buffer
- * @return The operation status.
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[out] sha512ctxp pointer to a SHA512 context to be initialized
* @retval CRY_NOERROR if the operation succeeded.
* @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
* device instance.
*
- * @api
+ * @notapi
+ */
+cryerror_t cry_lld_SHA512_init(CRYDriver *cryp, SHA512Context *sha512ctxp) {
+ sha512ctxp->sha.algo = CRY_SHA_512;
+ return sama_sha_lld_init(cryp, &sha512ctxp->sha);
+}
+
+/**
+ * @brief Hash update using SHA512.
+ *
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] sha512ctxp pointer to a SHA512 context
+ * @param[in] size size of input buffer
+ * @param[in] in buffer containing the input text
+ * @return The operation status.
+ * @retval CRY_NOERROR if the operation succeeded.
+ * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
+ * device instance.
+ *
+ * @notapi
*/
-cryerror_t cry_lld_SHA512(CRYDriver *cryp, size_t size,
- const uint8_t *in, uint8_t *out) {
+cryerror_t cry_lld_SHA512_update(CRYDriver *cryp, SHA512Context *sha512ctxp,
+ size_t size, const uint8_t *in) {
- cryerror_t ret;
+ sha512ctxp->sha.in = in;
+ sha512ctxp->sha.indata_len = size;
- shaparams_t params = {CRY_SHA_512};
+ return sama_sha_lld_update(cryp, &sha512ctxp->sha);
+}
- ret = sama_sha_lld_process(cryp,
- &params,
- in,
- out,
- size
- );
+/**
+ * @brief Hash finalization using SHA512.
+ *
+ * @param[in] cryp pointer to the @p CRYDriver object
+ * @param[in] sha512ctxp pointer to a SHA512 context
+ * @param[out] out 512 bits output buffer
+ * @return The operation status.
+ * @retval CRY_NOERROR if the operation succeeded.
+ * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
+ * device instance.
+ *
+ * @notapi
+ */
+cryerror_t cry_lld_SHA512_final(CRYDriver *cryp, SHA512Context *sha512ctxp,
+ uint8_t *out) {
- return ret;
+ sha512ctxp->sha.out = out;
+ return sama_sha_lld_final(cryp, &sha512ctxp->sha);
}
/**
diff --git a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h
index af3985aa2..7087e1ead 100644
--- a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h
+++ b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h
@@ -134,17 +134,28 @@ typedef enum {
TDES_ALGO_XTEA
}tdes_algo_t;
+struct sha_data {
+ uint32_t remaining;
+ uint32_t processed;
+ uint32_t block_size;
+ uint32_t output_size;
+ uint32_t sha_buffer_size;
+ const uint8_t *in;
+ uint8_t *out;
+ size_t indata_len;
+ uint8_t *sha_buffer;
+
+ shadalgo_t algo;
+};
+
typedef struct {
crytransfermode_t transfer_mode;
uint32_t cfbs;
- tdes_algo_t tdes_algo;
-
} CRYConfig;
#define KEY0_BUFFER_SIZE_W HAL_CRY_MAX_KEY_SIZE/4
-#define SHA_MAX_PADDING_LEN (2 * 128)
-#define SHA_UPDATE_LEN (128 * 1024)
+
#define CRY_DRIVER_EXT_FIELDS thread_reference_t thread; \
sama_dma_channel_t *dmarx; \
@@ -155,8 +166,7 @@ typedef struct {
uint8_t dmachunksize; \
uint8_t enabledPer; \
mutex_t mutex; \
- uint32_t key0_buffer[KEY0_BUFFER_SIZE_W]; \
- uint8_t sha_buffer[SHA_MAX_PADDING_LEN];
+ uint32_t key0_buffer[KEY0_BUFFER_SIZE_W];
/**
* @brief Structure representing an CRY driver.
@@ -190,6 +200,33 @@ struct CRYDriver {
/* End of the mandatory fields.*/
};
+#if (CRY_LLD_SUPPORTS_SHA1 == TRUE) || defined(__DOXYGEN__)
+/**
+ * @brief Type of a SHA1 context.
+ */
+typedef struct {
+ struct sha_data sha;
+} SHA1Context;
+#endif
+
+#if (CRY_LLD_SUPPORTS_SHA256 == TRUE) || defined(__DOXYGEN__)
+/**
+ * @brief Type of a SHA256 context.
+ */
+typedef struct {
+ struct sha_data sha;
+} SHA256Context;
+#endif
+
+#if (CRY_LLD_SUPPORTS_SHA512 == TRUE) || defined(__DOXYGEN__)
+/**
+ * @brief Type of a SHA512 context.
+ */
+typedef struct {
+ struct sha_data sha;
+} SHA512Context;
+#endif
+
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
@@ -314,12 +351,21 @@ extern "C" {
const uint8_t *in,
uint8_t *out,
const uint8_t *iv);
- cryerror_t cry_lld_SHA1(CRYDriver *cryp, size_t size,
- const uint8_t *in, uint8_t *out);
- cryerror_t cry_lld_SHA256(CRYDriver *cryp, size_t size,
- const uint8_t *in, uint8_t *out);
- cryerror_t cry_lld_SHA512(CRYDriver *cryp, size_t size,
- const uint8_t *in, uint8_t *out);
+ cryerror_t cry_lld_SHA1_init(CRYDriver *cryp, SHA1Context *sha1ctxp);
+ cryerror_t cry_lld_SHA1_update(CRYDriver *cryp, SHA1Context *sha1ctxp,
+ size_t size, const uint8_t *in);
+ cryerror_t cry_lld_SHA1_final(CRYDriver *cryp, SHA1Context *sha1ctxp,
+ uint8_t *out);
+ cryerror_t cry_lld_SHA256_init(CRYDriver *cryp, SHA256Context *sha256ctxp);
+ cryerror_t cry_lld_SHA256_update(CRYDriver *cryp, SHA256Context *sha256ctxp,
+ size_t size, const uint8_t *in);
+ cryerror_t cry_lld_SHA256_final(CRYDriver *cryp, SHA256Context *sha256ctxp,
+ uint8_t *out);
+ cryerror_t cry_lld_SHA512_init(CRYDriver *cryp, SHA512Context *sha512ctxp);
+ cryerror_t cry_lld_SHA512_update(CRYDriver *cryp, SHA512Context *sha512ctxp,
+ size_t size, const uint8_t *in);
+ cryerror_t cry_lld_SHA512_final(CRYDriver *cryp, SHA512Context *sha512ctxp,
+ uint8_t *out);
cryerror_t cry_lld_TRNG(CRYDriver *cryp, uint8_t *out);
#ifdef __cplusplus
}