aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorareviu <areviu.info@gmail.com>2017-11-30 21:12:58 +0000
committerareviu <areviu.info@gmail.com>2017-11-30 21:12:58 +0000
commit2ebbe6eb870313331bba48884112675107a6b866 (patch)
treef2eb4b050f057c5ebddb6a48cb1e511296940544
parent5b31410098434a2aff0c0729621fee6c6f2b3519 (diff)
downloadChibiOS-2ebbe6eb870313331bba48884112675107a6b866.tar.gz
ChibiOS-2ebbe6eb870313331bba48884112675107a6b866.tar.bz2
ChibiOS-2ebbe6eb870313331bba48884112675107a6b866.zip
update sha and trng lld
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11091 35acf78f-673a-0410-8e92-d51de3d6d3f4
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_aes_lld.c16
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c42
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.h7
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.c79
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_sha_lld.h3
-rw-r--r--os/hal/ports/SAMA/LLD/CRYPTOv1/sama_tdes_lld.c44
-rw-r--r--os/hal/ports/SAMA/LLD/TRNGv1/driver.mk5
-rw-r--r--os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.c53
-rw-r--r--os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.c161
-rw-r--r--os/hal/ports/SAMA/SAMA5D2x/hal_crypto_lld.h35
-rw-r--r--test/crypto/configuration.xml390
-rw-r--r--test/crypto/crypto_test.mk5
-rw-r--r--test/crypto/ref/gen_cfiles.bat1
-rw-r--r--test/crypto/ref/gen_testref.bat5
-rw-r--r--test/crypto/ref/sha_ref.bat20
-rw-r--r--test/crypto/source/test/cry_test_root.c10
-rw-r--r--test/crypto/source/test/cry_test_root.h9
-rw-r--r--test/crypto/source/test/cry_test_sequence_005.c128
-rw-r--r--test/crypto/source/test/cry_test_sequence_005.h (renamed from os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h)27
-rw-r--r--test/crypto/source/test/cry_test_sequence_006.c295
-rw-r--r--test/crypto/source/test/cry_test_sequence_006.h27
-rw-r--r--test/crypto/source/testref/ref_sha.c58
-rw-r--r--test/crypto/source/testref/ref_sha.h25
23 files changed, 1258 insertions, 187 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 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;i<size;i++)
- {
- p[i] = keyp[i];
- }
- }
- else
- {
- return CRY_ERR_INV_KEY_SIZE;
- }
-
- return CRY_NOERROR;
-}
-
-
-void samaClearKeyBuffer(void)
-{
- for (size_t i=0;i<KEY0_BUFFER_SIZE_W;i++)
- {
- key0_buffer[i] = 0;
- }
-}
-
void samaCryptoDriverDisable(CRYDriver *cryp)
{
if ((cryp->enabledPer & 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;i<shadata->remaining;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/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;i<KEY0_BUFFER_SIZE_W;i++)
+ {
+ cryp->key0_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;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);
+ }
+ 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,
+ &params,
+ 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,
+ &params,
+ 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,
+ &params,
+ 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 */
diff --git a/test/crypto/configuration.xml b/test/crypto/configuration.xml
index 958839811..4d5edd304 100644
--- a/test/crypto/configuration.xml
+++ b/test/crypto/configuration.xml
@@ -44,9 +44,11 @@ extern void cryptoTest_printArray32(bool isLE,const uint32_t *a,size_t len);
#ifdef LOG_CRYPTO_DATA
#define SHOW_ENCRYPDATA(w) cryptoTest_printArray32(true,msg_encrypted,w)
#define SHOW_DECRYPDATA(w) cryptoTest_printArray32(true,msg_decrypted,w)
+#define SHOW_DATA(d,w) cryptoTest_printArray32(true,d,w)
#else
#define SHOW_ENCRYPDATA(w)
#define SHOW_DECRYPDATA(w)
+#define SHOW_DATA(d,w)
#endif
#define TEST_DATA_BYTE_LEN 640
@@ -56,12 +58,17 @@ extern void cryptoTest_printArray32(bool isLE,const uint32_t *a,size_t len);
#define TEST_MSG_DATA_BYTE_LEN 640
#define TEST_MSG_DATA_WORD_LEN (TEST_MSG_DATA_BYTE_LEN / 4)
+#define SHA_LEN_0 3
+#define SHA_LEN_1 56
+
extern const char test_plain_data[TEST_DATA_BYTE_LEN];
extern uint32_t msg_clear[TEST_MSG_DATA_WORD_LEN];
extern uint32_t msg_encrypted[TEST_MSG_DATA_WORD_LEN];
extern uint32_t msg_decrypted[TEST_MSG_DATA_WORD_LEN];
extern const uint32_t test_keys[8];
extern const uint32_t test_vectors[4];
+extern const uint8_t sha_msg0[SHA_LEN_0];
+extern const uint8_t sha_msg1[SHA_LEN_1];
]]></value>
</global_definitions>
@@ -109,6 +116,12 @@ am quis dolor in libero placerat congue. Sed sodales urna sceler\
isque dui faucibus, vitae malesuada dui fermentum. Proin ultrici\
es sit amet justo at ornare. Suspendisse efficitur purus nullam.";
+
+const uint8_t sha_msg0[SHA_LEN_0] = "hi!";
+
+const uint8_t sha_msg1[SHA_LEN_1] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+
ALIGNED_VAR(4) uint32_t msg_clear[TEST_MSG_DATA_WORD_LEN];
ALIGNED_VAR(4) uint32_t msg_encrypted[TEST_MSG_DATA_WORD_LEN];
ALIGNED_VAR(4) uint32_t msg_decrypted[TEST_MSG_DATA_WORD_LEN];
@@ -2287,6 +2300,383 @@ for (int i = 0; i < TEST_DATA_WORD_LEN; i++) {
</cases>
</sequence>
+<sequence>
+ <type index="0">
+ <value>Internal Tests</value>
+ </type>
+ <brief>
+ <value>TRNG</value>
+ </brief>
+ <description>
+ <value>TRNG testing</value>
+ </description>
+ <condition>
+ <value />
+ </condition>
+ <shared_code>
+ <value><![CDATA[
+#include <string.h>
+
+static const CRYConfig configTRNG_Polling=
+{
+ TRANSFER_POLLING,
+ 0,
+ 0
+};
+
+ ]]></value>
+ </shared_code>
+ <cases>
+ <case>
+ <brief>
+ <value>TRNG Polling</value>
+ </brief>
+ <description>
+ <value>testing TRNG in polled mode</value>
+ </description>
+ <condition>
+ <value />
+ </condition>
+ <various_code>
+ <setup_code>
+ <value><![CDATA[
+cryStart(&CRYD1, &configTRNG_Polling);
+
+ ]]></value>
+ </setup_code>
+ <teardown_code>
+ <value><![CDATA[cryStop(&CRYD1);]]></value>
+ </teardown_code>
+ <local_variables>
+ <value><![CDATA[
+ cryerror_t ret;
+]]></value>
+ </local_variables>
+ </various_code>
+ <steps>
+
+ <step>
+ <description>
+ <value>Random generation and test</value>
+ </description>
+ <tags>
+ <value />
+ </tags>
+ <code>
+ <value><![CDATA[
+uint32_t random[10];
+ int i,j;
+ for (i=0;i<10;i++)
+ {
+ ret = cryTRNG(&CRYD1,(uint8_t*)&random[i]);
+
+ test_assert(ret == CRY_NOERROR , "failed random");
+
+ SHOW_DATA(&random[i],1);
+
+ test_assert(random[i] != 0 , "failed random generation (zero)");
+
+ for (j=0;j<i;j++)
+ {
+ test_assert(random[i] != random[j] , "failed random generation");
+ }
+ }
+
+]]></value>
+ </code>
+ </step>
+
+
+
+
+
+ </steps>
+ </case>
+
+
+
+ </cases>
+ </sequence>
+
+<sequence>
+ <type index="0">
+ <value>Internal Tests</value>
+ </type>
+ <brief>
+ <value>SHA</value>
+ </brief>
+ <description>
+ <value>SHA testing</value>
+ </description>
+ <condition>
+ <value />
+ </condition>
+ <shared_code>
+ <value><![CDATA[
+#include <string.h>
+#include "ref_sha.h"
+
+#define MAX_DIGEST_SIZE_INBYTE 64
+#define MAX_DIGEST_SIZE_INWORD (MAX_DIGEST_SIZE_INBYTE/4)
+static uint32_t digest[MAX_DIGEST_SIZE_INWORD];
+
+static const CRYConfig configSHA_Polling=
+{
+ TRANSFER_POLLING,
+ 0,
+ 0
+};
+
+ ]]></value>
+ </shared_code>
+ <cases>
+ <case>
+ <brief>
+ <value>SHA1 Polling</value>
+ </brief>
+ <description>
+ <value>testing SHA1 in polled mode</value>
+ </description>
+ <condition>
+ <value />
+ </condition>
+ <various_code>
+ <setup_code>
+ <value><![CDATA[
+memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+memset(digest, 0, MAX_DIGEST_SIZE_INWORD);
+memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0);
+cryStart(&CRYD1, &configSHA_Polling);
+
+ ]]></value>
+ </setup_code>
+ <teardown_code>
+ <value><![CDATA[cryStop(&CRYD1);]]></value>
+ </teardown_code>
+ <local_variables>
+ <value><![CDATA[
+ cryerror_t ret;
+ uint32_t *ref;
+]]></value>
+ </local_variables>
+ </various_code>
+ <steps>
+
+ <step>
+ <description>
+ <value>Digest</value>
+ </description>
+ <tags>
+ <value />
+ </tags>
+ <code>
+ <value><![CDATA[
+//---- One Block Test
+ret = crySHA1(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+
+test_assert(ret == CRY_NOERROR, "sha1 failed");
+
+
+SHOW_DATA(digest,5);
+
+ref = (uint32_t*)refSHA_SHA1_3;
+for (int i = 0; i < 5; i++) {
+ test_assert(digest[i] == ref[i], "sha1 digest mismatch");
+}
+
+//---- Multi Block Test
+memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1);
+
+ret = crySHA1(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+test_assert(ret == CRY_NOERROR, "sha1 failed");
+
+ SHOW_DATA(digest,5);
+
+
+ref = (uint32_t*)refSHA_SHA1_56;
+for (int i = 0; i < 5; i++) {
+ test_assert(digest[i] == ref[i], "sha1 digest mismatch");
+}
+
+
+]]></value>
+ </code>
+ </step>
+
+
+
+
+
+ </steps>
+ </case>
+
+ <case>
+ <brief>
+ <value>SHA256 Polling</value>
+ </brief>
+ <description>
+ <value>testing SHA256 in polled mode</value>
+ </description>
+ <condition>
+ <value />
+ </condition>
+ <various_code>
+ <setup_code>
+ <value><![CDATA[
+memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+memset(digest, 0, MAX_DIGEST_SIZE_INWORD);
+memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0);
+cryStart(&CRYD1, &configSHA_Polling);
+
+ ]]></value>
+ </setup_code>
+ <teardown_code>
+ <value><![CDATA[cryStop(&CRYD1);]]></value>
+ </teardown_code>
+ <local_variables>
+ <value><![CDATA[
+ cryerror_t ret;
+ uint32_t *ref;
+]]></value>
+ </local_variables>
+ </various_code>
+ <steps>
+
+ <step>
+ <description>
+ <value>Digest</value>
+ </description>
+ <tags>
+ <value />
+ </tags>
+ <code>
+ <value><![CDATA[
+
+//---- One Block Test
+ret = crySHA256(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+test_assert(ret == CRY_NOERROR, "sha256 failed");
+
+SHOW_DATA(digest,8);
+
+ref = (uint32_t*)refSHA_SHA256_3;
+for (int i = 0; i < 8; i++) {
+ test_assert(digest[i] == ref[i], "sha256 digest mismatch");
+}
+
+//---- Multi Block Test
+memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1);
+
+ret = crySHA256(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+test_assert(ret == CRY_NOERROR, "sha256 failed");
+
+
+ SHOW_DATA(digest,8);
+
+ref = (uint32_t*)refSHA_SHA256_56;
+for (int i = 0; i < 8; i++) {
+ test_assert(digest[i] == ref[i], "sha256 digest mismatch");
+}
+
+
+]]></value>
+ </code>
+ </step>
+
+
+
+
+
+ </steps>
+ </case>
+ <case>
+ <brief>
+ <value>SHA512 Polling</value>
+ </brief>
+ <description>
+ <value>testing SHA512 in polled mode</value>
+ </description>
+ <condition>
+ <value />
+ </condition>
+ <various_code>
+ <setup_code>
+ <value><![CDATA[
+memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+memset(digest, 0, MAX_DIGEST_SIZE_INWORD);
+memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0);
+cryStart(&CRYD1, &configSHA_Polling);
+
+ ]]></value>
+ </setup_code>
+ <teardown_code>
+ <value><![CDATA[cryStop(&CRYD1);]]></value>
+ </teardown_code>
+ <local_variables>
+ <value><![CDATA[
+ cryerror_t ret;
+ uint32_t *ref;
+]]></value>
+ </local_variables>
+ </various_code>
+ <steps>
+
+ <step>
+ <description>
+ <value>Digest</value>
+ </description>
+ <tags>
+ <value />
+ </tags>
+ <code>
+ <value><![CDATA[
+//---- One Block Test
+ret = crySHA512(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+test_assert(ret == CRY_NOERROR, "sha512 failed");
+
+SHOW_DATA(digest,16);
+
+ref = (uint32_t*)refSHA_SHA512_3;
+for (int i = 0; i < 16; i++) {
+ test_assert(digest[i] == ref[i], "sha512 digest mismatch");
+}
+
+//---- Multi Block Test
+memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1);
+
+ret = crySHA512(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+test_assert(ret == CRY_NOERROR, "sha512 failed");
+
+
+ SHOW_DATA(digest,16);
+
+ref = (uint32_t*)refSHA_SHA512_56;
+for (int i = 0; i < 16; i++) {
+ test_assert(digest[i] == ref[i], "sha512 digest mismatch");
+}
+
+
+]]></value>
+ </code>
+ </step>
+
+
+
+
+
+ </steps>
+ </case>
+ </cases>
+ </sequence>
+
</sequences>
</instance>
</instances>
diff --git a/test/crypto/crypto_test.mk b/test/crypto/crypto_test.mk
index 039cff349..efd7faf88 100644
--- a/test/crypto/crypto_test.mk
+++ b/test/crypto/crypto_test.mk
@@ -2,10 +2,13 @@
TESTSRC += ${CHIBIOS}/test/crypto/source/test/cry_test_root.c \
${CHIBIOS}/test/crypto/source/testref/ref_aes.c \
${CHIBIOS}/test/crypto/source/testref/ref_des.c \
+ ${CHIBIOS}/test/crypto/source/testref/ref_sha.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_001.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_002.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_003.c \
- ${CHIBIOS}/test/crypto/source/test/cry_test_sequence_004.c
+ ${CHIBIOS}/test/crypto/source/test/cry_test_sequence_004.c \
+ ${CHIBIOS}/test/crypto/source/test/cry_test_sequence_005.c \
+ ${CHIBIOS}/test/crypto/source/test/cry_test_sequence_006.c
# Required include directories
TESTINC += ${CHIBIOS}/test/crypto/source/testref \
${CHIBIOS}/test/crypto/source/test
diff --git a/test/crypto/ref/gen_cfiles.bat b/test/crypto/ref/gen_cfiles.bat
index d0bc19ff9..d63807b38 100644
--- a/test/crypto/ref/gen_cfiles.bat
+++ b/test/crypto/ref/gen_cfiles.bat
@@ -2,3 +2,4 @@
%PYTHON%\python genfile.py -f des_ecb_8,tdes_ecb_16,tdes_ecb_24,tdes_cbc_16,tdes_cbc_24 -o ref_des -p ../source/testref
+%PYTHON%\python genfile.py -f sha_sha1_3,sha_sha1_56,sha_sha256_3,sha_sha256_56,sha_sha512_3,sha_sha512_56 -o ref_sha -p ../source/testref
diff --git a/test/crypto/ref/gen_testref.bat b/test/crypto/ref/gen_testref.bat
index 311376773..38c329606 100644
--- a/test/crypto/ref/gen_testref.bat
+++ b/test/crypto/ref/gen_testref.bat
@@ -6,7 +6,12 @@ del *.enc
%PYTHON%\python -c "print 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent et pellentesque risus. Sed id gravida elit. Proin eget accumsan mi. Aliquam vitae dui porta, euismod velit viverra, elementum lacus. Nunc turpis orci, venenatis vel vulputate nec, luctus sitamet urna. Ut et nunc purus. Aliquam erat volutpat. Vestibulum nulla dolor, cursus vitae cursus eget, dapibus eget sapien. Integer justo eros, commodo ut massa eu, bibendum elementum tellus. Nam quis dolor in libero placerat congue. Sed sodales urna scelerisque dui faucibus, vitae malesuada dui fermentum. Proin ultricies sit amet justo at ornare. Suspendisse efficitur purus nullam.'.decode('ascii')" > plaintext
+echo|set /p="hi!" > plaintext_2
+
+echo|set /p="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" > plaintext_3
+
call aes_ref.bat
call des_ref.bat
+call sha_ref.bat
call gen_cfiles.bat \ No newline at end of file
diff --git a/test/crypto/ref/sha_ref.bat b/test/crypto/ref/sha_ref.bat
new file mode 100644
index 000000000..9eaa96463
--- /dev/null
+++ b/test/crypto/ref/sha_ref.bat
@@ -0,0 +1,20 @@
+
+call %OPENSSL%\openssl dgst -sha1 -c -binary -out sha_sha1_3 plaintext_2
+ren sha_sha1_3 sha_sha1_3.enc
+
+call %OPENSSL%\openssl dgst -sha1 -c -binary -out sha_sha1_56 plaintext_3
+ren sha_sha1_56 sha_sha1_56.enc
+
+call %OPENSSL%\openssl dgst -sha256 -c -binary -out sha_sha256_3 plaintext_2
+ren sha_sha256_3 sha_sha256_3.enc
+
+call %OPENSSL%\openssl dgst -sha256 -c -binary -out sha_sha256_56 plaintext_3
+ren sha_sha256_56 sha_sha256_56.enc
+
+
+call %OPENSSL%\openssl dgst -sha512 -c -binary -out sha_sha512_3 plaintext_2
+ren sha_sha512_3 sha_sha512_3.enc
+
+
+call %OPENSSL%\openssl dgst -sha512 -c -binary -out sha_sha512_56 plaintext_3
+ren sha_sha512_56 sha_sha512_56.enc \ No newline at end of file
diff --git a/test/crypto/source/test/cry_test_root.c b/test/crypto/source/test/cry_test_root.c
index e80e1d3fb..bd176b409 100644
--- a/test/crypto/source/test/cry_test_root.c
+++ b/test/crypto/source/test/cry_test_root.c
@@ -25,6 +25,8 @@
* - @subpage cry_test_sequence_002
* - @subpage cry_test_sequence_003
* - @subpage cry_test_sequence_004
+ * - @subpage cry_test_sequence_005
+ * - @subpage cry_test_sequence_006
* .
*/
@@ -50,6 +52,8 @@ const testsequence_t * const cry_test_suite_array[] = {
&cry_test_sequence_002,
&cry_test_sequence_003,
&cry_test_sequence_004,
+ &cry_test_sequence_005,
+ &cry_test_sequence_006,
NULL
};
@@ -107,6 +111,12 @@ am quis dolor in libero placerat congue. Sed sodales urna sceler\
isque dui faucibus, vitae malesuada dui fermentum. Proin ultrici\
es sit amet justo at ornare. Suspendisse efficitur purus nullam.";
+
+const uint8_t sha_msg0[SHA_LEN_0] = "hi!";
+
+const uint8_t sha_msg1[SHA_LEN_1] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+
ALIGNED_VAR(4) uint32_t msg_clear[TEST_MSG_DATA_WORD_LEN];
ALIGNED_VAR(4) uint32_t msg_encrypted[TEST_MSG_DATA_WORD_LEN];
ALIGNED_VAR(4) uint32_t msg_decrypted[TEST_MSG_DATA_WORD_LEN];
diff --git a/test/crypto/source/test/cry_test_root.h b/test/crypto/source/test/cry_test_root.h
index 2bb5ab029..24842646f 100644
--- a/test/crypto/source/test/cry_test_root.h
+++ b/test/crypto/source/test/cry_test_root.h
@@ -28,6 +28,8 @@
#include "cry_test_sequence_002.h"
#include "cry_test_sequence_003.h"
#include "cry_test_sequence_004.h"
+#include "cry_test_sequence_005.h"
+#include "cry_test_sequence_006.h"
#if !defined(__DOXYGEN__)
@@ -53,9 +55,11 @@ extern void cryptoTest_printArray32(bool isLE,const uint32_t *a,size_t len);
#ifdef LOG_CRYPTO_DATA
#define SHOW_ENCRYPDATA(w) cryptoTest_printArray32(true,msg_encrypted,w)
#define SHOW_DECRYPDATA(w) cryptoTest_printArray32(true,msg_decrypted,w)
+#define SHOW_DATA(d,w) cryptoTest_printArray32(true,d,w)
#else
#define SHOW_ENCRYPDATA(w)
#define SHOW_DECRYPDATA(w)
+#define SHOW_DATA(d,w)
#endif
#define TEST_DATA_BYTE_LEN 640
@@ -65,12 +69,17 @@ extern void cryptoTest_printArray32(bool isLE,const uint32_t *a,size_t len);
#define TEST_MSG_DATA_BYTE_LEN 640
#define TEST_MSG_DATA_WORD_LEN (TEST_MSG_DATA_BYTE_LEN / 4)
+#define SHA_LEN_0 3
+#define SHA_LEN_1 56
+
extern const char test_plain_data[TEST_DATA_BYTE_LEN];
extern uint32_t msg_clear[TEST_MSG_DATA_WORD_LEN];
extern uint32_t msg_encrypted[TEST_MSG_DATA_WORD_LEN];
extern uint32_t msg_decrypted[TEST_MSG_DATA_WORD_LEN];
extern const uint32_t test_keys[8];
extern const uint32_t test_vectors[4];
+extern const uint8_t sha_msg0[SHA_LEN_0];
+extern const uint8_t sha_msg1[SHA_LEN_1];
#endif /* !defined(__DOXYGEN__) */
diff --git a/test/crypto/source/test/cry_test_sequence_005.c b/test/crypto/source/test/cry_test_sequence_005.c
new file mode 100644
index 000000000..fb15c13c1
--- /dev/null
+++ b/test/crypto/source/test/cry_test_sequence_005.c
@@ -0,0 +1,128 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 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 "cry_test_root.h"
+
+/**
+ * @file cry_test_sequence_005.c
+ * @brief Test Sequence 005 code.
+ *
+ * @page cry_test_sequence_005 [5] TRNG
+ *
+ * File: @ref cry_test_sequence_005.c
+ *
+ * <h2>Description</h2>
+ * TRNG testing.
+ *
+ * <h2>Test Cases</h2>
+ * - @subpage cry_test_005_001
+ * .
+ */
+
+/****************************************************************************
+ * Shared code.
+ ****************************************************************************/
+
+#include <string.h>
+
+static const CRYConfig configTRNG_Polling=
+{
+ TRANSFER_POLLING,
+ 0,
+ 0
+};
+
+
+
+/****************************************************************************
+ * Test cases.
+ ****************************************************************************/
+
+/**
+ * @page cry_test_005_001 [5.1] TRNG Polling
+ *
+ * <h2>Description</h2>
+ * testing TRNG in polled mode.
+ *
+ * <h2>Test Steps</h2>
+ * - [5.1.1] Random generation and test.
+ * .
+ */
+
+static void cry_test_005_001_setup(void) {
+ cryStart(&CRYD1, &configTRNG_Polling);
+
+
+}
+
+static void cry_test_005_001_teardown(void) {
+ cryStop(&CRYD1);
+}
+
+static void cry_test_005_001_execute(void) {
+ cryerror_t ret;
+
+ /* [5.1.1] Random generation and test.*/
+ test_set_step(1);
+ {
+ uint32_t random[10];
+ int i,j;
+ for (i=0;i<10;i++)
+ {
+ ret = cryTRNG(&CRYD1,(uint8_t*)&random[i]);
+
+ test_assert(ret == CRY_NOERROR , "failed random");
+
+ SHOW_DATA(&random[i],1);
+
+ test_assert(random[i] != 0 , "failed random generation (zero)");
+
+ for (j=0;j<i;j++)
+ {
+ test_assert(random[i] != random[j] , "failed random generation");
+ }
+ }
+
+ }
+}
+
+static const testcase_t cry_test_005_001 = {
+ "TRNG Polling",
+ cry_test_005_001_setup,
+ cry_test_005_001_teardown,
+ cry_test_005_001_execute
+};
+
+/****************************************************************************
+ * Exported data.
+ ****************************************************************************/
+
+/**
+ * @brief Array of test cases.
+ */
+const testcase_t * const cry_test_sequence_005_array[] = {
+ &cry_test_005_001,
+ NULL
+};
+
+/**
+ * @brief TRNG.
+ */
+const testsequence_t cry_test_sequence_005 = {
+ "TRNG",
+ cry_test_sequence_005_array
+};
diff --git a/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h b/test/crypto/source/test/cry_test_sequence_005.h
index 83dd36174..c0e945365 100644
--- a/os/hal/ports/SAMA/LLD/TRNGv1/sama_trng_lld.h
+++ b/test/crypto/source/test/cry_test_sequence_005.h
@@ -1,5 +1,5 @@
/*
- ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
+ ChibiOS - Copyright (C) 2006..2017 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.
@@ -13,24 +13,15 @@
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_
+/**
+ * @file cry_test_sequence_005.h
+ * @brief Test Sequence 005 header.
+ */
+#ifndef CRY_TEST_SEQUENCE_005_H
+#define CRY_TEST_SEQUENCE_005_H
-#ifdef __cplusplus
-extern "C" {
-#endif
+extern const testsequence_t cry_test_sequence_005;
-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_ */
+#endif /* CRY_TEST_SEQUENCE_005_H */
diff --git a/test/crypto/source/test/cry_test_sequence_006.c b/test/crypto/source/test/cry_test_sequence_006.c
new file mode 100644
index 000000000..b2c54facb
--- /dev/null
+++ b/test/crypto/source/test/cry_test_sequence_006.c
@@ -0,0 +1,295 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 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 "cry_test_root.h"
+
+/**
+ * @file cry_test_sequence_006.c
+ * @brief Test Sequence 006 code.
+ *
+ * @page cry_test_sequence_006 [6] SHA
+ *
+ * File: @ref cry_test_sequence_006.c
+ *
+ * <h2>Description</h2>
+ * SHA testing.
+ *
+ * <h2>Test Cases</h2>
+ * - @subpage cry_test_006_001
+ * - @subpage cry_test_006_002
+ * - @subpage cry_test_006_003
+ * .
+ */
+
+/****************************************************************************
+ * Shared code.
+ ****************************************************************************/
+
+#include <string.h>
+#include "ref_sha.h"
+
+#define MAX_DIGEST_SIZE_INBYTE 64
+#define MAX_DIGEST_SIZE_INWORD (MAX_DIGEST_SIZE_INBYTE/4)
+static uint32_t digest[MAX_DIGEST_SIZE_INWORD];
+
+static const CRYConfig configSHA_Polling=
+{
+ TRANSFER_POLLING,
+ 0,
+ 0
+};
+
+
+
+/****************************************************************************
+ * Test cases.
+ ****************************************************************************/
+
+/**
+ * @page cry_test_006_001 [6.1] SHA1 Polling
+ *
+ * <h2>Description</h2>
+ * testing SHA1 in polled mode.
+ *
+ * <h2>Test Steps</h2>
+ * - [6.1.1] Digest.
+ * .
+ */
+
+static void cry_test_006_001_setup(void) {
+ memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+ memset(digest, 0, MAX_DIGEST_SIZE_INWORD);
+ memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0);
+ cryStart(&CRYD1, &configSHA_Polling);
+
+
+}
+
+static void cry_test_006_001_teardown(void) {
+ cryStop(&CRYD1);
+}
+
+static void cry_test_006_001_execute(void) {
+ cryerror_t ret;
+ uint32_t *ref;
+
+ /* [6.1.1] Digest.*/
+ test_set_step(1);
+ {
+ //---- One Block Test
+ ret = crySHA1(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+
+ test_assert(ret == CRY_NOERROR, "sha1 failed");
+
+
+ SHOW_DATA(digest,5);
+
+ ref = (uint32_t*)refSHA_SHA1_3;
+ for (int i = 0; i < 5; i++) {
+ test_assert(digest[i] == ref[i], "sha1 digest mismatch");
+ }
+
+ //---- Multi Block Test
+ memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+ memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1);
+
+ ret = crySHA1(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+ test_assert(ret == CRY_NOERROR, "sha1 failed");
+
+ SHOW_DATA(digest,5);
+
+
+ ref = (uint32_t*)refSHA_SHA1_56;
+ for (int i = 0; i < 5; i++) {
+ test_assert(digest[i] == ref[i], "sha1 digest mismatch");
+ }
+
+
+ }
+}
+
+static const testcase_t cry_test_006_001 = {
+ "SHA1 Polling",
+ cry_test_006_001_setup,
+ cry_test_006_001_teardown,
+ cry_test_006_001_execute
+};
+
+/**
+ * @page cry_test_006_002 [6.2] SHA256 Polling
+ *
+ * <h2>Description</h2>
+ * testing SHA256 in polled mode.
+ *
+ * <h2>Test Steps</h2>
+ * - [6.2.1] Digest.
+ * .
+ */
+
+static void cry_test_006_002_setup(void) {
+ memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+ memset(digest, 0, MAX_DIGEST_SIZE_INWORD);
+ memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0);
+ cryStart(&CRYD1, &configSHA_Polling);
+
+
+}
+
+static void cry_test_006_002_teardown(void) {
+ cryStop(&CRYD1);
+}
+
+static void cry_test_006_002_execute(void) {
+ cryerror_t ret;
+ uint32_t *ref;
+
+ /* [6.2.1] Digest.*/
+ test_set_step(1);
+ {
+
+ //---- One Block Test
+ ret = crySHA256(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+ test_assert(ret == CRY_NOERROR, "sha256 failed");
+
+ SHOW_DATA(digest,8);
+
+ ref = (uint32_t*)refSHA_SHA256_3;
+ for (int i = 0; i < 8; i++) {
+ test_assert(digest[i] == ref[i], "sha256 digest mismatch");
+ }
+
+ //---- Multi Block Test
+ memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+ memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1);
+
+ ret = crySHA256(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+ test_assert(ret == CRY_NOERROR, "sha256 failed");
+
+
+ SHOW_DATA(digest,8);
+
+ ref = (uint32_t*)refSHA_SHA256_56;
+ for (int i = 0; i < 8; i++) {
+ test_assert(digest[i] == ref[i], "sha256 digest mismatch");
+ }
+
+
+ }
+}
+
+static const testcase_t cry_test_006_002 = {
+ "SHA256 Polling",
+ cry_test_006_002_setup,
+ cry_test_006_002_teardown,
+ cry_test_006_002_execute
+};
+
+/**
+ * @page cry_test_006_003 [6.3] SHA512 Polling
+ *
+ * <h2>Description</h2>
+ * testing SHA512 in polled mode.
+ *
+ * <h2>Test Steps</h2>
+ * - [6.3.1] Digest.
+ * .
+ */
+
+static void cry_test_006_003_setup(void) {
+ memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+ memset(digest, 0, MAX_DIGEST_SIZE_INWORD);
+ memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0);
+ cryStart(&CRYD1, &configSHA_Polling);
+
+
+}
+
+static void cry_test_006_003_teardown(void) {
+ cryStop(&CRYD1);
+}
+
+static void cry_test_006_003_execute(void) {
+ cryerror_t ret;
+ uint32_t *ref;
+
+ /* [6.3.1] Digest.*/
+ test_set_step(1);
+ {
+ //---- One Block Test
+ ret = crySHA512(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+ test_assert(ret == CRY_NOERROR, "sha512 failed");
+
+ SHOW_DATA(digest,16);
+
+ ref = (uint32_t*)refSHA_SHA512_3;
+ for (int i = 0; i < 16; i++) {
+ test_assert(digest[i] == ref[i], "sha512 digest mismatch");
+ }
+
+ //---- Multi Block Test
+ memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN);
+ memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1);
+
+ ret = crySHA512(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest);
+
+ test_assert(ret == CRY_NOERROR, "sha512 failed");
+
+
+ SHOW_DATA(digest,16);
+
+ ref = (uint32_t*)refSHA_SHA512_56;
+ for (int i = 0; i < 16; i++) {
+ test_assert(digest[i] == ref[i], "sha512 digest mismatch");
+ }
+
+
+ }
+}
+
+static const testcase_t cry_test_006_003 = {
+ "SHA512 Polling",
+ cry_test_006_003_setup,
+ cry_test_006_003_teardown,
+ cry_test_006_003_execute
+};
+
+/****************************************************************************
+ * Exported data.
+ ****************************************************************************/
+
+/**
+ * @brief Array of test cases.
+ */
+const testcase_t * const cry_test_sequence_006_array[] = {
+ &cry_test_006_001,
+ &cry_test_006_002,
+ &cry_test_006_003,
+ NULL
+};
+
+/**
+ * @brief SHA.
+ */
+const testsequence_t cry_test_sequence_006 = {
+ "SHA",
+ cry_test_sequence_006_array
+};
diff --git a/test/crypto/source/test/cry_test_sequence_006.h b/test/crypto/source/test/cry_test_sequence_006.h
new file mode 100644
index 000000000..8bc6061ce
--- /dev/null
+++ b/test/crypto/source/test/cry_test_sequence_006.h
@@ -0,0 +1,27 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 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.
+*/
+
+/**
+ * @file cry_test_sequence_006.h
+ * @brief Test Sequence 006 header.
+ */
+
+#ifndef CRY_TEST_SEQUENCE_006_H
+#define CRY_TEST_SEQUENCE_006_H
+
+extern const testsequence_t cry_test_sequence_006;
+
+#endif /* CRY_TEST_SEQUENCE_006_H */
diff --git a/test/crypto/source/testref/ref_sha.c b/test/crypto/source/testref/ref_sha.c
new file mode 100644
index 000000000..bada14c93
--- /dev/null
+++ b/test/crypto/source/testref/ref_sha.c
@@ -0,0 +1,58 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 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"
+
+const uint8_t refSHA_SHA1_3[]={
+0x3A,0x98,0x7A,0xCF,0x8C,0xBC,0x10,0x28,0xB7,0xDB,
+0xC8,0x6B,0xD0,0x86,0x83,0x11,0x51,0x89,0x9A,0x2B,
+
+};
+const uint8_t refSHA_SHA1_56[]={
+0x84,0x98,0x3E,0x44,0x1C,0x3B,0xD2,0x6E,0xBA,0xAE,
+0x4A,0xA1,0xF9,0x51,0x29,0xE5,0xE5,0x46,0x70,0xF1,
+
+};
+const uint8_t refSHA_SHA256_3[]={
+0xC0,0xDD,0xD6,0x2C,0x77,0x17,0x18,0x0E,0x7F,0xFB,
+0x8A,0x15,0xBB,0x96,0x74,0xD3,0xEC,0x92,0x59,0x2E,
+0x0B,0x7A,0xC7,0xD1,0xD5,0x28,0x98,0x36,0xB4,0x55,
+0x3B,0xE2,
+};
+const uint8_t refSHA_SHA256_56[]={
+0x24,0x8D,0x6A,0x61,0xD2,0x06,0x38,0xB8,0xE5,0xC0,
+0x26,0x93,0x0C,0x3E,0x60,0x39,0xA3,0x3C,0xE4,0x59,
+0x64,0xFF,0x21,0x67,0xF6,0xEC,0xED,0xD4,0x19,0xDB,
+0x06,0xC1,
+};
+const uint8_t refSHA_SHA512_3[]={
+0x3E,0xBB,0x6E,0x93,0x1E,0xAA,0x4D,0xCF,0x74,0x1A,
+0xD1,0x23,0x37,0xD4,0xF7,0x10,0x5B,0x02,0xD4,0xA9,
+0xB1,0x94,0x21,0x4E,0x88,0x55,0x9E,0x8E,0x41,0xEC,
+0x04,0xD3,0x20,0xE9,0x6A,0x3C,0xF9,0x12,0xED,0x27,
+0x34,0x29,0x35,0xA6,0xF8,0x9D,0x1F,0x5C,0x1A,0x5C,
+0xD7,0xF7,0xFF,0xBF,0xA9,0xB1,0xBE,0x1A,0x41,0x62,
+0xED,0x32,0x3C,0x7A,
+};
+const uint8_t refSHA_SHA512_56[]={
+0x20,0x4A,0x8F,0xC6,0xDD,0xA8,0x2F,0x0A,0x0C,0xED,
+0x7B,0xEB,0x8E,0x08,0xA4,0x16,0x57,0xC1,0x6E,0xF4,
+0x68,0xB2,0x28,0xA8,0x27,0x9B,0xE3,0x31,0xA7,0x03,
+0xC3,0x35,0x96,0xFD,0x15,0xC1,0x3B,0x1B,0x07,0xF9,
+0xAA,0x1D,0x3B,0xEA,0x57,0x78,0x9C,0xA0,0x31,0xAD,
+0x85,0xC7,0xA7,0x1D,0xD7,0x03,0x54,0xEC,0x63,0x12,
+0x38,0xCA,0x34,0x45,
+};
diff --git a/test/crypto/source/testref/ref_sha.h b/test/crypto/source/testref/ref_sha.h
new file mode 100644
index 000000000..b487d519b
--- /dev/null
+++ b/test/crypto/source/testref/ref_sha.h
@@ -0,0 +1,25 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 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 TEST_REF_SHA_H_
+#define TEST_REF_SHA_H_
+
+extern const uint8_t refSHA_SHA1_3[];
+extern const uint8_t refSHA_SHA1_56[];
+extern const uint8_t refSHA_SHA256_3[];
+extern const uint8_t refSHA_SHA256_56[];
+extern const uint8_t refSHA_SHA512_3[];
+extern const uint8_t refSHA_SHA512_56[];
+#endif //TEST_REF_SHA_H_