From 2ebbe6eb870313331bba48884112675107a6b866 Mon Sep 17 00:00:00 2001 From: areviu Date: Thu, 30 Nov 2017 21:12:58 +0000 Subject: update sha and trng lld git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11091 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c | 16 ++- os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c | 42 +----- os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h | 7 +- os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c | 79 +++++++---- os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h | 3 +- os/hal/ports/SAMA/LLD/CRYPTOv1/sama_tdes_lld.c | 44 ++++--- os/hal/ports/SAMA/LLD/TRNGv1/driver.mk | 5 - os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c | 53 -------- os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h | 36 ----- os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c | 161 ++++++++++++++++++++++- os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h | 35 +++-- 11 files changed, 277 insertions(+), 204 deletions(-) delete mode 100644 os/hal/ports/SAMA/LLD/TRNGv1/driver.mk delete mode 100644 os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c delete mode 100644 os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h (limited to 'os') 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 0efed73b0..da66978fc 100644 --- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c +++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c @@ -125,7 +125,11 @@ cryerror_t sama_aes_lld_process_polling(CRYDriver *cryp, aesparams *params, AES->AES_MR |= (AES_MR_CFBS(cryp->config->cfbs) | AES_MR_CKEY_PASSWD); - sama_aes_lld_write_key(key0_buffer,( const uint32_t *) params->iv, cryp->key0_size); + osalMutexLock(&cryp->mutex); + + sama_aes_lld_write_key(cryp->key0_buffer,( const uint32_t *) params->iv, cryp->key0_size); + + osalMutexUnlock(&cryp->mutex); if (params->encrypt) AES->AES_MR |= AES_MR_CIPHER; @@ -202,13 +206,13 @@ cryerror_t sama_aes_lld_process_dma(CRYDriver *cryp, aesparams *params, dmaChannelSetMode(cryp->dmarx, cryp->rxdmamode); dmaChannelSetMode(cryp->dmatx, cryp->txdmamode); - /* Writing channel */ + // Writing channel dmaChannelSetSource(cryp->dmatx, in); dmaChannelSetDestination(cryp->dmatx, AES->AES_IDATAR); dmaChannelSetTransactionSize(cryp->dmatx, ( indata_len / DMA_DATA_WIDTH_TO_BYTE(cryp->dmawith))); - /* Reading channel */ + // Reading channel dmaChannelSetSource(cryp->dmarx, AES->AES_ODATAR); dmaChannelSetDestination(cryp->dmarx, out); dmaChannelSetTransactionSize(cryp->dmarx, ( indata_len / DMA_DATA_WIDTH_TO_BYTE(cryp->dmawith))); @@ -226,7 +230,11 @@ cryerror_t sama_aes_lld_process_dma(CRYDriver *cryp, aesparams *params, AES->AES_MR |= (AES_MR_CFBS(cryp->config->cfbs) | AES_MR_CKEY_PASSWD); - sama_aes_lld_write_key(key0_buffer,( const uint32_t *) params->iv, cryp->key0_size); + osalMutexLock(&cryp->mutex); + + sama_aes_lld_write_key(cryp->key0_buffer,( const uint32_t *) params->iv, cryp->key0_size); + + osalMutexUnlock(&cryp->mutex); if (params->encrypt) AES->AES_MR |= AES_MR_CIPHER; diff --git a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c index 0ee83e861..f06ef34ed 100644 --- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c +++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c @@ -15,13 +15,11 @@ */ #include "hal.h" -#if HAL_USE_CRY || defined(__DOXYGEN__) +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) #include "sama_crypto_lld.h" -#define KEY0_BUFFER_SIZE_W HAL_CRY_MAX_KEY_SIZE/4 -uint32_t key0_buffer[KEY0_BUFFER_SIZE_W]; #if defined(SAMA_DMA_REQUIRED) static void crypto_lld_serve_read_interrupt(CRYDriver *cryp, uint32_t flags); @@ -63,13 +61,12 @@ static void crypto_lld_serve_write_interrupt(CRYDriver *cryp, uint32_t flags); void samaCryptoDriverInit(CRYDriver *cryp) { cryp->enabledPer = 0; cryp->thread = NULL; + osalMutexObjectInit(&cryp->mutex); } void samaCryptoDriverStart(CRYDriver *cryp) { - samaClearKeyBuffer(); - if (cryp->config->transfer_mode == TRANSFER_DMA) { #if defined(SAMA_DMA_REQUIRED) @@ -96,40 +93,6 @@ void samaCryptoDriverStop(CRYDriver *cryp) { samaCryptoDriverDisable(cryp); } - -/** - * write key into internal buffer - */ -cryerror_t samaCryptoDriverWriteTransientKey(const uint8_t *keyp,size_t size) -{ - uint8_t *p = (uint8_t *)key0_buffer; - - if (size <= HAL_CRY_MAX_KEY_SIZE) - { - samaClearKeyBuffer(); - - for (size_t i=0;ienabledPer & AES_PER)) { @@ -146,6 +109,7 @@ void samaCryptoDriverDisable(CRYDriver *cryp) } if ((cryp->enabledPer & TRNG_PER)) { cryp->enabledPer &= ~TRNG_PER; + TRNG->TRNG_CR = TRNG_CR_KEY_PASSWD; pmcDisableTRNG(); } } 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 9134f189c..a496fc201 100644 --- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h +++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h @@ -21,9 +21,8 @@ extern void samaCryptoDriverInit(CRYDriver *cryp); extern void samaCryptoDriverStart(CRYDriver *cryp); extern void samaCryptoDriverStop(CRYDriver *cryp); -extern cryerror_t samaCryptoDriverWriteTransientKey(const uint8_t *keyp,size_t size); extern void samaCryptoDriverDisable(CRYDriver *cryp); -extern void samaClearKeyBuffer(void); + #define AES_PER 0x01 #define TRNG_PER 0x02 @@ -45,10 +44,6 @@ extern void samaClearKeyBuffer(void); -extern uint32_t key0_buffer[HAL_CRY_MAX_KEY_SIZE/4]; - - - #include "sama_aes_lld.h" #include "sama_tdes_lld.h" #include "sama_sha_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 7fa89d2f5..9806541fd 100644 --- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c +++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c @@ -19,44 +19,67 @@ #include "sama_crypto_lld.h" -#define SHA_MAX_PADDING_LEN (2 * 128) -#define SHA_UPDATE_LEN (128 * 1024) +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 uint8_t sha_buffer[SHA_MAX_PADDING_LEN]; static uint32_t shaOutputSize(shadalgo_t algo); static uint32_t shadPaddedMessSize(uint8_t mode, uint32_t len); -static uint8_t shaBlockSize(shadalgo_t algo); +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 processBlock(const uint8_t* data, uint32_t len, uint32_t block_size); -static void updatePollingMode(struct sha_data *shadata,const uint8_t* data, uint32_t data_size); -static void updateDMA(struct sha_data *shadata,const uint8_t* data, uint32_t data_size); +static void updatePollingMode(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data, uint32_t data_size); +static void updateDMA(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 inline uint32_t min_u32(uint32_t a, uint32_t b) -{ - return a < b ? a : b; -} +uint8_t shaDigestSize(shadalgo_t algo); + +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; + } +} -int sha_finish(struct sha_data *shadata,const uint8_t* buffer,uint32_t buffer_size) +int sha_finish(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* buffer,uint32_t buffer_size) { - uint32_t padding_len; + uint32_t padding_len=0; if (buffer_size < shadata->output_size) return -1; @@ -64,10 +87,10 @@ int sha_finish(struct sha_data *shadata,const uint8_t* buffer,uint32_t buffer_si //pad data for the end of the buffer padding_len = fillPadding(shadata, shadata->processed + shadata->remaining, - &sha_buffer[shadata->remaining] + &cryp->sha_buffer[shadata->remaining] ); - processBlock(sha_buffer, shadata->remaining + padding_len, shadata->block_size); + processBlock(cryp->sha_buffer, shadata->remaining + padding_len, shadata->block_size); readData(buffer, buffer_size); @@ -78,18 +101,17 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp, shaparams_t *params, const uint8_t *in, uint8_t *out, - size_t indata_len, - size_t out_size + size_t indata_len ) { uint32_t algoregval; struct sha_data shadata; - //uint8_t *data_in; + if (!(cryp->enabledPer & SHA_PER)) { cryp->enabledPer |= SHA_PER; pmcEnableSHA(); - } + } shadata.processed = 0; shadata.remaining = 0; @@ -127,6 +149,8 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp, //soft reset SHA->SHA_CR = SHA_CR_SWRST; + + //configure SHA->SHA_MR = algoregval | SHA_MR_SMOD_MANUAL_START | SHA_MR_PROCDLY_LONGEST; @@ -145,15 +169,15 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp, } if (cryp->config->transfer_mode == TRANSFER_POLLING) - updatePollingMode(&shadata, in, buf_in_size); + updatePollingMode(cryp,&shadata, in, buf_in_size); else - updateDMA(&shadata, in, buf_in_size); + updateDMA(cryp,&shadata, in, buf_in_size); p += buf_in_size; indata_len -= buf_in_size; } - sha_finish(&shadata, out, out_size); + sha_finish(cryp, &shadata,out, shadata.output_size); @@ -199,7 +223,7 @@ static uint32_t shadPaddedMessSize(uint8_t mode, uint32_t len) return len; } -static uint8_t shaBlockSize(shadalgo_t algo) +uint8_t shaBlockSize(shadalgo_t algo) { if ( (algo == CRY_SHA_384) || (algo == CRY_SHA_512) ) { return 128; @@ -254,13 +278,14 @@ static uint32_t processBlock(const uint8_t* data, uint32_t len, uint32_t block_s return processed; } -static void updateDMA(struct sha_data *shadata,const uint8_t* data, uint32_t data_size) +static void updateDMA(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data, uint32_t data_size) { (void)shadata; (void)data; (void)data_size; + (void)cryp; } -static void updatePollingMode(struct sha_data *shadata,const uint8_t* data, uint32_t data_size) +static void updatePollingMode(CRYDriver *cryp,struct sha_data *shadata,const uint8_t* data, uint32_t data_size) { uint32_t i; uint32_t processed; @@ -269,14 +294,14 @@ static void updatePollingMode(struct sha_data *shadata,const uint8_t* data, uint if (shadata->remaining) { //complete previous data uint32_t complement = min_u32(data_size, shadata->block_size - shadata->remaining); - memcpy(&sha_buffer[shadata->remaining], data, complement); + memcpy(&cryp->sha_buffer[shadata->remaining], data, complement); shadata->remaining += complement; data += complement; data_size -= complement; //if data is complete process the block if (shadata->remaining == shadata->block_size) { - processBlock(sha_buffer, shadata->remaining, shadata->block_size); + processBlock(cryp->sha_buffer, shadata->remaining, shadata->block_size); shadata->processed += shadata->block_size; shadata->remaining = 0; @@ -295,7 +320,7 @@ static void updatePollingMode(struct sha_data *shadata,const uint8_t* data, uint if (shadata->remaining) { for (i=0;iremaining;i++) - sha_buffer[i] = data[processed+i]; + cryp->sha_buffer[i] = data[processed+i]; } } 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 f2702fe97..c27d33cac 100644 --- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h +++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h @@ -22,8 +22,7 @@ cryerror_t sama_sha_lld_process(CRYDriver *cryp, shaparams_t *params, const uint8_t *in, uint8_t *out, - size_t indata_len, - size_t out_size + size_t indata_len ); diff --git a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_tdes_lld.c b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_tdes_lld.c index f72cb2324..a2dcd5cd3 100644 --- a/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_tdes_lld.c +++ b/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_tdes_lld.c @@ -78,24 +78,22 @@ cryerror_t sama_tdes_lld_polling(CRYDriver *cryp, tdes_config_t *params, } TDES->TDES_MR = mode; - + osalMutexLock(&cryp->mutex); //write keys - /* Write the 64-bit key(s) in the different Key Word Registers, - * depending on whether one, two or three keys are required. */ - TDES->TDES_KEY1WR[0] = key0_buffer[0]; - TDES->TDES_KEY1WR[1] = key0_buffer[1]; + TDES->TDES_KEY1WR[0] = cryp->key0_buffer[0]; + TDES->TDES_KEY1WR[1] = cryp->key0_buffer[1]; if (cryp->key0_size > 8) { - TDES->TDES_KEY2WR[0] = key0_buffer[2]; - TDES->TDES_KEY2WR[1] = key0_buffer[3]; + TDES->TDES_KEY2WR[0] = cryp->key0_buffer[2]; + TDES->TDES_KEY2WR[1] = cryp->key0_buffer[3]; } else { TDES->TDES_KEY2WR[0] = 0x0; TDES->TDES_KEY2WR[1] = 0x0; } if (cryp->key0_size > 16) { - TDES->TDES_KEY3WR[0] = key0_buffer[4]; - TDES->TDES_KEY3WR[1] = key0_buffer[5]; + TDES->TDES_KEY3WR[0] = cryp->key0_buffer[4]; + TDES->TDES_KEY3WR[1] = cryp->key0_buffer[5]; } else { TDES->TDES_KEY3WR[0] = 0x0; TDES->TDES_KEY3WR[1] = 0x0; @@ -108,8 +106,8 @@ cryerror_t sama_tdes_lld_polling(CRYDriver *cryp, tdes_config_t *params, if (params->algo == TDES_ALGO_XTEA) { TDES->TDES_XTEA_RNDR = TDES_XTEA_RNDR_XTEA_RNDS(32); } - - /* Iterate per 64-bit data block */ + osalMutexUnlock(&cryp->mutex); + //load 64 bit data size in tdes registers for (i = 0; i < data_len; i += size) { if (size == 8) tdes_set_input((uint32_t *) ((data) + i), @@ -177,15 +175,14 @@ cryerror_t sama_tdes_lld_dma(CRYDriver *cryp, tdes_config_t *params, dmaChannelSetMode(cryp->dmarx, cryp->rxdmamode); dmaChannelSetMode(cryp->dmatx, cryp->txdmamode); - /* Writing channel */ + // Writing channel dmaChannelSetSource(cryp->dmatx, data); dmaChannelSetDestination(cryp->dmatx, TDES->TDES_IDATAR); dmaChannelSetTransactionSize(cryp->dmatx, (data_len / DMA_DATA_WIDTH_TO_BYTE(cryp->dmawith))); - // ( data_len / DMA_DATA_WIDTH_TO_BYTE(cryp->dmawith)) - /* Reading channel */ + // Reading channel dmaChannelSetSource(cryp->dmarx, TDES->TDES_ODATAR); dmaChannelSetDestination(cryp->dmarx, out); dmaChannelSetTransactionSize(cryp->dmarx, @@ -217,29 +214,34 @@ cryerror_t sama_tdes_lld_dma(CRYDriver *cryp, tdes_config_t *params, TDES->TDES_MR = mode; + osalMutexLock(&cryp->mutex); + //write keys - TDES->TDES_KEY1WR[0] = key0_buffer[0]; - TDES->TDES_KEY1WR[1] = key0_buffer[1]; + TDES->TDES_KEY1WR[0] = cryp->key0_buffer[0]; + TDES->TDES_KEY1WR[1] = cryp->key0_buffer[1]; if (cryp->key0_size > 8) { - TDES->TDES_KEY2WR[0] = key0_buffer[2]; - TDES->TDES_KEY2WR[1] = key0_buffer[3]; + TDES->TDES_KEY2WR[0] = cryp->key0_buffer[2]; + TDES->TDES_KEY2WR[1] = cryp->key0_buffer[3]; } else { TDES->TDES_KEY2WR[0] = 0x0; TDES->TDES_KEY2WR[1] = 0x0; } if (cryp->key0_size > 16) { - TDES->TDES_KEY3WR[0] = key0_buffer[4]; - TDES->TDES_KEY3WR[1] = key0_buffer[5]; + TDES->TDES_KEY3WR[0] = cryp->key0_buffer[4]; + TDES->TDES_KEY3WR[1] = cryp->key0_buffer[5]; } else { TDES->TDES_KEY3WR[0] = 0x0; TDES->TDES_KEY3WR[1] = 0x0; } - /* The Initialization Vector Registers apply to all modes except ECB. */ + //initialize vectors registers ( except ECB mode) if (params->mode != TDES_MODE_ECB && vectors != NULL) { TDES->TDES_IVR[0] = vectors[0]; TDES->TDES_IVR[1] = vectors[1]; } + + osalMutexUnlock(&cryp->mutex); + if (params->algo == TDES_ALGO_XTEA) { TDES->TDES_XTEA_RNDR = TDES_XTEA_RNDR_XTEA_RNDS(32); } diff --git a/os/hal/ports/SAMA/LLD/TRNGv1/driver.mk b/os/hal/ports/SAMA/LLD/TRNGv1/driver.mk deleted file mode 100644 index 92d59e051..000000000 --- a/os/hal/ports/SAMA/LLD/TRNGv1/driver.mk +++ /dev/null @@ -1,5 +0,0 @@ -PLATFORMSRC +=$(CHIBIOS)/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c - - - -PLATFORMINC +=$(CHIBIOS)/os/hal/ports/SAMA/LLD/TRNGv1 \ No newline at end of file diff --git a/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c b/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c deleted file mode 100644 index 2c416b6ad..000000000 --- a/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - 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 "sama_trng_lld.h" - - -void trng_lld_init(void) { - pmcEnableTRNG(); -} - -void trng_lld_start(void) { - - TRNG->TRNG_CR = TRNG_CR_ENABLE | TRNG_CR_KEY_PASSWD; - //enable interrupt - // TRNG->TRNG_IER = TRNG_IER_DATRDY; - - -} - -void trng_lld_getrandom(uint32_t * random) { - - //generate a random every 84 clock cycles. - while (!(TRNG->TRNG_ISR & TRNG_ISR_DATRDY)); - - *random = TRNG->TRNG_ODATA; - -} - -void trng_lld_stop(void) { - //disable interrupt - //TRNG->TRNG_IDR = TRNG_IDR_DATRDY; - TRNG->TRNG_CR = TRNG_CR_KEY_PASSWD; - pmcDisableTRNG(); -} - - - - - - diff --git a/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h b/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h deleted file mode 100644 index 83dd36174..000000000 --- a/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - 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. -*/ -#ifndef HAL_LLD_SAMA_TRNG_H_ -#define HAL_LLD_SAMA_TRNG_H_ - - - -#ifdef __cplusplus -extern "C" { -#endif - -extern void trng_lld_init(void); -extern void trng_lld_start(void); -extern void trng_lld_stop(void); -extern void trng_lld_getrandom(uint32_t * random); - -#ifdef __cplusplus -} -#endif - - - -#endif /* HAL_LLD_SAMA_TRNG_H_ */ diff --git a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c index 79c4f3a3f..2e841cca7 100644 --- a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c +++ b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c @@ -83,6 +83,12 @@ void cry_lld_init(void) { void cry_lld_start(CRYDriver *cryp) { if (cryp->state == CRY_STOP) { + //clear key + for (size_t i=0;ikey0_buffer[i] = 0; + } + #if PLATFORM_CRY_USE_CRY1 if (&CRYD1 == cryp) { samaCryptoDriverStart(&CRYD1); @@ -126,9 +132,33 @@ void cry_lld_stop(CRYDriver *cryp) { cryerror_t cry_lld_loadkey(CRYDriver *cryp, cryalgorithm_t algorithm, size_t size, const uint8_t *keyp) { - (void)(cryp); - (void)(algorithm); - return samaCryptoDriverWriteTransientKey(keyp, size); + + uint8_t *p = (uint8_t *)cryp->key0_buffer; + + (void)algorithm; + + + if (size <= HAL_CRY_MAX_KEY_SIZE) + { + osalMutexLock(&cryp->mutex); + //clear key + for (size_t i=0;ikey0_buffer[i] = 0; + } + + for (size_t i=0;imutex); + } + else + { + return CRY_ERR_INV_KEY_SIZE; + } + + return CRY_NOERROR; } @@ -1096,6 +1126,131 @@ cryerror_t cry_lld_decrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id, return ret; } + + +/** + * @brief Hash 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. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * + * @api + */ +cryerror_t cry_lld_SHA1(CRYDriver *cryp, size_t size, + const uint8_t *in, uint8_t *out) { + + cryerror_t ret; + + shaparams_t params = {CRY_SHA_1}; + + ret = sama_sha_lld_process(cryp, + ¶ms, + in, + out, + size + ); + + return ret; +} + +/** + * @brief Hash using SHA256. + * + * @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. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * + * @api + */ +cryerror_t cry_lld_SHA256(CRYDriver *cryp, size_t size, + const uint8_t *in, uint8_t *out) { + + cryerror_t ret; + + shaparams_t params = {CRY_SHA_256}; + + ret = sama_sha_lld_process(cryp, + ¶ms, + in, + out, + size + ); + + return ret; +} + +/** + * @brief Hash 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. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * + * @api + */ +cryerror_t cry_lld_SHA512(CRYDriver *cryp, size_t size, + const uint8_t *in, uint8_t *out) { + + cryerror_t ret; + + shaparams_t params = {CRY_SHA_512}; + + ret = sama_sha_lld_process(cryp, + ¶ms, + in, + out, + size + ); + + return ret; +} + +/** + * @brief True random numbers generator. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @param[out] out 128 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 + */ +cryerror_t cry_lld_TRNG(CRYDriver *cryp, uint8_t *out) { + + if (!(cryp->enabledPer & TRNG_PER)) { + cryp->enabledPer |= TRNG_PER; + pmcEnableTRNG(); + + //start trng + TRNG->TRNG_CR = TRNG_CR_ENABLE | TRNG_CR_KEY_PASSWD; + } + + while (!(TRNG->TRNG_ISR & TRNG_ISR_DATRDY)); + + *((uint32_t*) out) = TRNG->TRNG_ODATA; + + return (cryerror_t)CRY_NOERROR; +} + #endif /* HAL_USE_CRY */ /** @} */ diff --git a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h index 029b4d83c..4521bb57b 100644 --- a/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h +++ b/os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h @@ -25,7 +25,7 @@ #ifndef HAL_CRYPTO_LLD_H #define HAL_CRYPTO_LLD_H -#if HAL_USE_CRY || defined(__DOXYGEN__) +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) /*===========================================================================*/ /* Driver constants. */ @@ -40,10 +40,14 @@ #define CRY_LLD_SUPPORTS_AES_CBC TRUE #define CRY_LLD_SUPPORTS_AES_CFB TRUE #define CRY_LLD_SUPPORTS_AES_CTR TRUE -#define CRY_LLD_SUPPORTS_AES_GCM TRUE +#define CRY_LLD_SUPPORTS_AES_GCM FALSE #define CRY_LLD_SUPPORTS_DES TRUE #define CRY_LLD_SUPPORTS_DES_ECB TRUE #define CRY_LLD_SUPPORTS_DES_CBC TRUE +#define CRY_LLD_SUPPORTS_SHA1 TRUE +#define CRY_LLD_SUPPORTS_SHA256 TRUE +#define CRY_LLD_SUPPORTS_SHA512 TRUE +#define CRY_LLD_SUPPORTS_TRNG TRUE /** @{ */ /*===========================================================================*/ @@ -99,13 +103,13 @@ typedef enum { CRY_SHA_224, CRY_SHA_256, CRY_SHA_384, - CRY_SHA_512, + CRY_SHA_512 + }shadalgo_t; typedef struct { shadalgo_t algo; - }shaparams_t; /** @@ -138,14 +142,22 @@ typedef struct { } 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; \ sama_dma_channel_t *dmatx; \ uint32_t rxdmamode; \ uint32_t txdmamode; \ - uint8_t dmawith; \ - uint8_t dmachunksize; \ - uint8_t enabledPer; + uint8_t dmawith; \ + 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]; + /** * @brief Structure representing an CRY driver. */ @@ -302,11 +314,18 @@ 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_TRNG(CRYDriver *cryp, uint8_t *out); #ifdef __cplusplus } #endif -#endif /* HAL_USE_CRY */ +#endif /* HAL_USE_CRY == TRUE */ #endif /* HAL_CRYPTO_LLD_H */ -- cgit v1.2.3