added SAMA HMAC + fixed TRNG 128 bit generation

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@12042 110e8d01-0319-4d1e-a829-52ad28d1bb01
This commit is contained in:
areviu 2018-05-19 13:15:06 +00:00
parent e9399566d7
commit 7454fa6ad5
15 changed files with 1093 additions and 57 deletions

View File

@ -179,6 +179,21 @@ cryerror_t sama_sha_lld_init(CRYDriver *cryp, struct sha_data *sha)
algoregval = SHA_MR_ALGO_SHA512;
break;
#endif
case CRY_HMACSHA_1:
algoregval = SHA_MR_ALGO_HMAC_SHA1;
break;
case CRY_HMACSHA_224:
algoregval = SHA_MR_ALGO_HMAC_SHA224;
break;
case CRY_HMACSHA_256:
algoregval = SHA_MR_ALGO_HMAC_SHA256;
break;
case CRY_HMACSHA_384:
algoregval = SHA_MR_ALGO_HMAC_SHA384;
break;
case CRY_HMACSHA_512:
algoregval = SHA_MR_ALGO_HMAC_SHA512;
break;
default:
osalMutexUnlock(&cryp->mutex);
return CRY_ERR_INV_ALGO;
@ -232,14 +247,19 @@ static uint32_t shaOutputSize(shadalgo_t algo)
{
switch (algo) {
case CRY_SHA_1:
case CRY_HMACSHA_1:
return 20;
case CRY_SHA_224:
case CRY_HMACSHA_224:
return 28;
case CRY_SHA_256:
case CRY_HMACSHA_256:
return 32;
case CRY_SHA_384:
case CRY_HMACSHA_384:
return 48;
case CRY_SHA_512:
case CRY_HMACSHA_512:
return 64;
default:
return 0;
@ -248,28 +268,42 @@ static uint32_t shaOutputSize(shadalgo_t algo)
static uint32_t shadPaddedMessSize(uint8_t mode, uint32_t len)
{
uint32_t k;
uint32_t k;
switch (mode) {
case CRY_SHA_1:
case CRY_SHA_224:
case CRY_SHA_256:
k = (512 + 448 - (((len * 8) % 512) + 1)) % 512;
len += (k - 7) / 8 + 9;
break;
case CRY_SHA_384:
case CRY_SHA_512:
k = (1024 + 896 - (((len * 8) % 1024) + 1)) % 1024;
len += (k - 7) / 8 + 17;
break;
}
return len;
switch (mode) {
case CRY_SHA_1:
case CRY_SHA_224:
case CRY_SHA_256:
case CRY_HMACSHA_1:
case CRY_HMACSHA_224:
case CRY_HMACSHA_256:
k = (512 + 448 - (((len * 8) % 512) + 1)) % 512;
len += (k - 7) / 8 + 9;
break;
case CRY_SHA_384:
case CRY_SHA_512:
case CRY_HMACSHA_384:
case CRY_HMACSHA_512:
k = (1024 + 896 - (((len * 8) % 1024) + 1)) % 1024;
len += (k - 7) / 8 + 17;
break;
}
return len;
}
uint8_t shaBlockSize(shadalgo_t algo)
{
if ( (algo == CRY_SHA_384) || (algo == CRY_SHA_512) ) {
return 128;
switch(algo)
{
case CRY_SHA_384:
case CRY_HMACSHA_384:
case CRY_SHA_512:
case CRY_HMACSHA_512:
return 128;
default:
break;
}
return 64;

View File

@ -786,9 +786,6 @@ cryerror_t cry_lld_decrypt_AES_CTR(CRYDriver *cryp,
*
* @notapi
*/
uint8_t gcmbuff[32*2];
cryerror_t cry_lld_encrypt_AES_GCM(CRYDriver *cryp,
crykey_t key_id,
size_t size,
@ -1457,6 +1454,10 @@ cryerror_t cry_lld_SHA512_final(CRYDriver *cryp, SHA512Context *sha512ctxp,
*/
cryerror_t cry_lld_TRNG(CRYDriver *cryp, uint8_t *out) {
size_t i=0;
osalMutexLock(&cryp->mutex);
if (!(cryp->enabledPer & TRNG_PER)) {
cryp->enabledPer |= TRNG_PER;
pmcEnableTRNG();
@ -1464,14 +1465,298 @@ cryerror_t cry_lld_TRNG(CRYDriver *cryp, uint8_t *out) {
//start trng
TRNG->TRNG_CR = TRNG_CR_ENABLE | TRNG_CR_KEY_PASSWD;
}
while (i<4) {
while (!(TRNG->TRNG_ISR & TRNG_ISR_DATRDY));
while (!(TRNG->TRNG_ISR & TRNG_ISR_DATRDY));
((uint32_t*) out)[i] = TRNG->TRNG_ODATA;
i++;
}
*((uint32_t*) out) = TRNG->TRNG_ODATA;
osalMutexUnlock(&cryp->mutex);
return (cryerror_t)CRY_NOERROR;
}
/**
* @brief Hash initialization using HMAC_SHA256.
* @note Use of this algorithm is not recommended because proven weak.
*
* @param[in] cryp pointer to the @p CRYDriver object
* @param[out] hmacsha256ctxp pointer to a HMAC_SHA256 context to be
* initialized
* @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_HMACSHA256_init(CRYDriver *cryp,
HMACSHA256Context *hmacsha256ctxp) {
hmacsha256ctxp->kipad = 0;
if (cryp->key0_size > HAL_CRY_MAX_KEY_SIZE)
return CRY_ERR_INV_KEY_SIZE;
if (cryp->key0_size > 64) //this implementation doesn't hash the key
return CRY_ERR_INV_KEY_TYPE;
return cry_lld_SHA256_init(cryp,&hmacsha256ctxp->shacontext);
}
/**
* @brief Hash update using HMAC.
* @note Use of this algorithm is not recommended because proven weak.
*
* @param[in] cryp pointer to the @p CRYDriver object
* @param[in] hmacsha256ctxp pointer to a HMAC_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_HMACSHA256_update(CRYDriver *cryp,
HMACSHA256Context *hmacsha256ctxp,
size_t size,
const uint8_t *in) {
uint8_t i;
cryerror_t res;
uint32_t buffer[16];
if (hmacsha256ctxp->kipad == 0)
{
memset(buffer,0,64);
memcpy(buffer,cryp->key0_buffer,cryp->key0_size);
memset((uint8_t *)buffer + cryp->key0_size, 0, 64 - cryp->key0_size);
for (i = 0; i < 16; ++i) {
buffer[i] ^= 0x36363636;
}
res = cry_lld_SHA256_update(cryp,&hmacsha256ctxp->shacontext,64,(const uint8_t *)buffer);
hmacsha256ctxp->kipad = 1;
}
if (res!= CRY_NOERROR)
return res;
return cry_lld_SHA256_update(cryp,&hmacsha256ctxp->shacontext,size,in);
}
/**
* @brief Hash finalization using HMAC.
* @note Use of this algorithm is not recommended because proven weak.
*
* @param[in] cryp pointer to the @p CRYDriver object
* @param[in] hmacsha256ctxp pointer to a HMAC_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_HMACSHA256_final(CRYDriver *cryp,
HMACSHA256Context *hmacsha256ctxp,
uint8_t *out) {
uint8_t i;
cryerror_t res;
uint32_t buffer[16]; //max block size for sha256
uint8_t digest[32];
//H( k1pad || m )
res = cry_lld_SHA256_final(cryp, &hmacsha256ctxp->shacontext,digest);
if (res!= CRY_NOERROR)
return res;
res = cry_lld_SHA256_init(cryp,&hmacsha256ctxp->shacontext);
if (res!= CRY_NOERROR)
return res;
memset(buffer,0,64);
memcpy(buffer,cryp->key0_buffer,cryp->key0_size);
memset((uint8_t *)buffer + cryp->key0_size, 0, 64 - cryp->key0_size);
for (i = 0; i < 16; ++i) {
buffer[i] ^= 0x5C5C5C5C;
}
// k+opad || H( k+ipad || m )
res = cry_lld_SHA256_update(cryp,&hmacsha256ctxp->shacontext,64,(const uint8_t *)buffer);
if (res!= CRY_NOERROR)
return res;
res = cry_lld_SHA256_update(cryp,&hmacsha256ctxp->shacontext,32,digest);
if (res!= CRY_NOERROR)
return res;
hmacsha256ctxp->shacontext.sha.out = out;
return cry_lld_SHA256_final(cryp, &hmacsha256ctxp->shacontext,out);
}
/**
* @brief Hash initialization using HMAC_SHA512.
* @note Use of this algorithm is not recommended because proven weak.
*
* @param[in] cryp pointer to the @p CRYDriver object
* @param[out] hmacsha512ctxp pointer to a HMAC_SHA512 context to be
* initialized
* @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_HMACSHA512_init(CRYDriver *cryp,
HMACSHA512Context *hmacsha512ctxp) {
hmacsha512ctxp->kipad = 0;
if (cryp->key0_size > HAL_CRY_MAX_KEY_SIZE)
return CRY_ERR_INV_KEY_SIZE;
if (cryp->key0_size > 128) //this implementation doesn't hash the key
return CRY_ERR_INV_KEY_TYPE;
return cry_lld_SHA512_init(cryp,&hmacsha512ctxp->shacontext);
}
/**
* @brief Hash update using HMAC.
* @note Use of this algorithm is not recommended because proven weak.
*
* @param[in] cryp pointer to the @p CRYDriver object
* @param[in] hmacsha512ctxp pointer to a HMAC_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_HMACSHA512_update(CRYDriver *cryp,
HMACSHA512Context *hmacsha512ctxp,
size_t size,
const uint8_t *in) {
cryerror_t res;
uint8_t i;
uint32_t buffer[32];
if (hmacsha512ctxp->kipad == 0)
{
memset(buffer,0,128);
memcpy(buffer,cryp->key0_buffer,cryp->key0_size);
memset((uint8_t *)buffer + cryp->key0_size, 0, 128 - cryp->key0_size);
for (i = 0; i < 32; ++i) {
buffer[i] ^= 0x36363636;
}
res = cry_lld_SHA512_update(cryp,&hmacsha512ctxp->shacontext,128,(const uint8_t *)buffer);
if (res!= CRY_NOERROR)
return res;
hmacsha512ctxp->kipad = 1;
}
return cry_lld_SHA512_update(cryp,&hmacsha512ctxp->shacontext,size,in);
}
/**
* @brief Hash finalization using HMAC.
* @note Use of this algorithm is not recommended because proven weak.
*
* @param[in] cryp pointer to the @p CRYDriver object
* @param[in] hmacsha512ctxp pointer to a HMAC_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_HMACSHA512_final(CRYDriver *cryp,
HMACSHA512Context *hmacsha512ctxp,
uint8_t *out) {
uint8_t i;
cryerror_t res;
uint32_t buffer[32]; //max block size for sha256
uint8_t digest[64];
//H( k1pad || m )
res = cry_lld_SHA512_final(cryp, &hmacsha512ctxp->shacontext,digest);
if (res!= CRY_NOERROR)
return res;
res = cry_lld_SHA512_init(cryp,&hmacsha512ctxp->shacontext);
if (res!= CRY_NOERROR)
return res;
memset(buffer,0,128);
memcpy(buffer,cryp->key0_buffer,cryp->key0_size);
memset((uint8_t *)buffer + cryp->key0_size, 0, 128 - cryp->key0_size);
for (i = 0; i < 32; ++i) {
buffer[i] ^= 0x5C5C5C5C;
}
// k+opad || H( k+ipad || m )
res = cry_lld_SHA512_update(cryp,&hmacsha512ctxp->shacontext,128,(const uint8_t *)buffer);
if (res!= CRY_NOERROR)
return res;
res = cry_lld_SHA512_update(cryp,&hmacsha512ctxp->shacontext,64,digest);
if (res!= CRY_NOERROR)
return res;
hmacsha512ctxp->shacontext.sha.out = out;
return cry_lld_SHA512_final(cryp, &hmacsha512ctxp->shacontext,out);
}
#endif /* HAL_USE_CRY */
/** @} */

View File

@ -15,7 +15,7 @@
*/
/**
* @file hal_cry_lld.h
* @file hal_crypto_lld.h
* @brief PLATFORM cryptographic subsystem low level driver header.
*
* @addtogroup CRYPTO
@ -47,6 +47,8 @@
#define CRY_LLD_SUPPORTS_SHA1 TRUE
#define CRY_LLD_SUPPORTS_SHA256 TRUE
#define CRY_LLD_SUPPORTS_SHA512 TRUE
#define CRY_LLD_SUPPORTS_HMAC_SHA256 TRUE
#define CRY_LLD_SUPPORTS_HMAC_SHA512 TRUE
#define CRY_LLD_SUPPORTS_TRNG TRUE
/** @{ */
@ -118,7 +120,13 @@ typedef enum {
CRY_SHA_224,
CRY_SHA_256,
CRY_SHA_384,
CRY_SHA_512
CRY_SHA_512,
CRY_HMACSHA_1,
CRY_HMACSHA_224,
CRY_HMACSHA_256,
CRY_HMACSHA_384,
CRY_HMACSHA_512,
}shadalgo_t;
@ -241,6 +249,25 @@ typedef struct {
struct sha_data sha;
} SHA512Context;
#endif
#if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || defined(__DOXYGEN__)
/**
* @brief Type of a HMAC_SHA256 context.
*/
typedef struct {
SHA256Context shacontext;
uint8_t kipad;
} HMACSHA256Context;
#endif
#if (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || defined(__DOXYGEN__)
/**
* @brief Type of a HMAC_SHA512 context.
*/
typedef struct {
SHA512Context shacontext;
uint8_t kipad;
} HMACSHA512Context;
#endif
/*===========================================================================*/
/* Driver macros. */
@ -381,6 +408,22 @@ extern "C" {
size_t size, const uint8_t *in);
cryerror_t cry_lld_SHA512_final(CRYDriver *cryp, SHA512Context *sha512ctxp,
uint8_t *out);
cryerror_t cry_lld_HMACSHA256_init(CRYDriver *cryp,
HMACSHA256Context *hmacsha256ctxp);
cryerror_t cry_lld_HMACSHA256_update(CRYDriver *cryp,
HMACSHA256Context *hmacsha256ctxp,
size_t size, const uint8_t *in);
cryerror_t cry_lld_HMACSHA256_final(CRYDriver *cryp,
HMACSHA256Context *hmacsha256ctxp,
uint8_t *out);
cryerror_t cry_lld_HMACSHA512_init(CRYDriver *cryp,
HMACSHA512Context *hmacsha512ctxp);
cryerror_t cry_lld_HMACSHA512_update(CRYDriver *cryp,
HMACSHA512Context *hmacsha512ctxp,
size_t size, const uint8_t *in);
cryerror_t cry_lld_HMACSHA512_final(CRYDriver *cryp,
HMACSHA512Context *hmacsha512ctxp,
uint8_t *out);
cryerror_t cry_lld_TRNG(CRYDriver *cryp, uint8_t *out);
#ifdef __cplusplus
}

View File

@ -2369,23 +2369,23 @@ cryStart(&CRYD1, &configTRNG_Polling);
</tags>
<code>
<value><![CDATA[
uint32_t random[10];
int i,j;
for (i=0;i<10;i++)
{
ret = cryTRNG(&CRYD1,(uint8_t*)&random[i]);
uint32_t random[4];
int i;
test_assert(ret == CRY_NOERROR , "failed random");
ret = cryTRNG(&CRYD1,(uint8_t*)&random);
SHOW_DATA(&random[i],1);
test_assert(ret == CRY_NOERROR , "failed random");
test_assert(random[i] != 0 , "failed random generation (zero)");
SHOW_DATA(&random[0],4);
for (i=0;i<4;i++) {
test_assert(random[i] != 0 , "failed random generation (zero)");
}
for (i=1;i<4;i++)
{
test_assert(random[i-1] != random[i] , "failed random generation");
}
for (j=0;j<i;j++)
{
test_assert(random[i] != random[j] , "failed random generation");
}
}
]]></value>
</code>
@ -3641,6 +3641,278 @@ for (i = 0;i<TEST_GCM_LEN;i++) {
</cases>
</sequence>
<sequence>
<type index="0">
<value>Internal Tests</value>
</type>
<brief>
<value>HMAC</value>
</brief>
<description>
<value>HMAC testing</value>
</description>
<condition>
<value />
</condition>
<shared_code>
<value><![CDATA[
#include <string.h>
#include "ref_hmac.h"
#define plaintext msg_clear
#define HMACOUT msg_encrypted
#define shabuffer msg_decrypted
#define MAX_SHA_BLOCK_SIZE TEST_MSG_DATA_BYTE_LEN
#define MAX_SHA_BLOCK_SIZE_INWORD (MAX_SHA_BLOCK_SIZE/4)
static const CRYConfig config_Polling=
{
TRANSFER_POLLING,
0
};
static const CRYConfig config_DMA=
{
TRANSFER_DMA,
0
};
static const int hmackeys_size[]=
{
20,
4,
20
};
static const uint8_t hmackey_1[]=
{
0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,
0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,
};
// "Hi There",
static const uint8_t hmacmsg1[8]= "Hi There";
static const size_t hmacmsg_size[]=
{
8,
};
static void cryHMAC256(CRYDriver *cryp) {
cryerror_t ret;
HMACSHA256Context hmacshactxp;
uint8_t *keyp;
hmacshactxp.shacontext.sha.sha_buffer = (uint8_t*)&shabuffer[0];
hmacshactxp.shacontext.sha.sha_buffer_size = MAX_SHA_BLOCK_SIZE;
keyp =(uint8_t *)hmackey_1;
ret = cryLoadTransientKey(&CRYD1, (cryalgorithm_t) cry_algo_hmac,hmackeys_size[0], keyp);
test_assert(ret == CRY_NOERROR, "failed load transient key");
ret = cryHMACSHA256Init(cryp,&hmacshactxp);
test_assert(ret == CRY_NOERROR, "failed init HMACSHA256");
ret = cryHMACSHA256Update(cryp,&hmacshactxp,hmacmsg_size[0],(const uint8_t *)&hmacmsg1);
test_assert(ret == CRY_NOERROR, "failed update HMACSHA256");
ret = cryHMACSHA256Final(cryp,&hmacshactxp,(uint8_t *)HMACOUT);
test_assert(ret == CRY_NOERROR, "failed final HMACSHA256");
SHOW_DATA(HMACOUT,8);
}
static void cryHMAC512(CRYDriver *cryp) {
cryerror_t ret;
HMACSHA512Context hmacshactxp;
uint8_t *keyp;
hmacshactxp.shacontext.sha.sha_buffer = (uint8_t*)&shabuffer[0];
hmacshactxp.shacontext.sha.sha_buffer_size = MAX_SHA_BLOCK_SIZE;
keyp =(uint8_t *)hmackey_1;
ret = cryLoadTransientKey(&CRYD1, (cryalgorithm_t) cry_algo_hmac,hmackeys_size[0], keyp);
test_assert(ret == CRY_NOERROR, "failed load transient key");
ret = cryHMACSHA512Init(cryp,&hmacshactxp);
test_assert(ret == CRY_NOERROR, "failed init HMACSHA512");
ret = cryHMACSHA512Update(cryp,&hmacshactxp,hmacmsg_size[0],(const uint8_t *)&hmacmsg1);
test_assert(ret == CRY_NOERROR, "failed update HMACSHA512");
ret = cryHMACSHA512Final(cryp,&hmacshactxp,(uint8_t *)HMACOUT);
test_assert(ret == CRY_NOERROR, "failed final HMACSHA512");
SHOW_DATA(HMACOUT,16);
}
]]></value>
</shared_code>
<cases>
<case>
<brief>
<value>HMAC Polling</value>
</brief>
<description>
<value>testing HMAC in polled mode</value>
</description>
<condition>
<value />
</condition>
<various_code>
<setup_code>
<value><![CDATA[
memset(HMACOUT, 0xff, TEST_MSG_DATA_BYTE_LEN);
cryStart(&CRYD1, &config_Polling);
]]></value>
</setup_code>
<teardown_code>
<value><![CDATA[cryStop(&CRYD1);]]></value>
</teardown_code>
<local_variables>
<value><![CDATA[
]]></value>
</local_variables>
</various_code>
<steps>
<step>
<description>
<value>hmac 256</value>
</description>
<tags>
<value />
</tags>
<code>
<value><![CDATA[
cryHMAC256(&CRYD1);
for (int i = 0; i < 8; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC256_1)[i], "hmac mismatch");
}
]]></value>
</code>
</step>
<step>
<description>
<value>hmac 512</value>
</description>
<tags>
<value />
</tags>
<code>
<value><![CDATA[
cryHMAC512(&CRYD1);
for (int i = 0; i < 16; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC512_1)[i], "hmac mismatch");
}
]]></value>
</code>
</step>
</steps>
</case>
<case>
<brief>
<value>HMAC DMA</value>
</brief>
<description>
<value>testing HMAC in DMA mode</value>
</description>
<condition>
<value />
</condition>
<various_code>
<setup_code>
<value><![CDATA[
memset(HMACOUT, 0xff, TEST_MSG_DATA_BYTE_LEN);
cryStart(&CRYD1, &config_DMA);
]]></value>
</setup_code>
<teardown_code>
<value><![CDATA[cryStop(&CRYD1);]]></value>
</teardown_code>
<local_variables>
<value><![CDATA[
]]></value>
</local_variables>
</various_code>
<steps>
<step>
<description>
<value>hmac 256</value>
</description>
<tags>
<value />
</tags>
<code>
<value><![CDATA[
cryHMAC256(&CRYD1);
for (int i = 0; i < 8; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC256_1)[i], "hmac mismatch");
}
]]></value>
</code>
</step>
<step>
<description>
<value>hmac 512</value>
</description>
<tags>
<value />
</tags>
<code>
<value><![CDATA[
cryHMAC512(&CRYD1);
for (int i = 0; i < 16; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC512_1)[i], "hmac mismatch");
}
]]></value>
</code>
</step>
</steps>
</case>
</cases>
</sequence>
</sequences>
</instance>
</instances>

View File

@ -1,9 +1,10 @@
# List of all the ChibiOS OS Library test files.
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/testref/ref_gcm.c \
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/testref/ref_gcm.c \
${CHIBIOS}/test/crypto/source/testref/ref_hmac.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 \
@ -11,7 +12,8 @@ TESTSRC += ${CHIBIOS}/test/crypto/source/test/cry_test_root.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_005.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_006.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_007.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_008.c
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_008.c \
${CHIBIOS}/test/crypto/source/test/cry_test_sequence_009.c
# Required include directories
TESTINC += ${CHIBIOS}/test/crypto/source/testref \
${CHIBIOS}/test/crypto/source/test

View File

@ -3,3 +3,6 @@
%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_empty,sha_sha1_3,sha_sha1_56,sha_sha1_64,sha_sha1_128,sha_sha256_3,sha_sha256_56,sha_sha256_64,sha_sha256_128,sha_sha512_3,sha_sha512_56,sha_sha512_64,sha_sha512_128 -o ref_sha -p ../source/testref
%PYTHON%\python genfile.py -f hmac_hmac256_1,hmac_hmac256_2,hmac_hmac512_1,hmac_hmac512_2 -o ref_hmac -p ../source/testref

View File

@ -14,8 +14,12 @@ echo|set /p="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" >
echo|set /p="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" > plaintext_5
echo|set /p="Hi There" > plaintext_6
echo|set /p="what do ya want for nothing?" > plaintext_7
call aes_ref.bat
call des_ref.bat
call sha_ref.bat
call hmac_ref.bat
call gen_cfiles.bat

View File

@ -0,0 +1,13 @@
call %OPENSSL%\openssl dgst -sha256 -hmac " " -c -binary -out hmac_hmac256_1 plaintext_6
ren hmac_hmac256_1 hmac_hmac256_1.enc
call %OPENSSL%\openssl dgst -sha256 -hmac "Jefe" -c -binary -out hmac_hmac256_2 plaintext_7
ren hmac_hmac256_2 hmac_hmac256_2.enc
call %OPENSSL%\openssl dgst -sha512 -hmac " " -c -binary -out hmac_hmac512_1 plaintext_6
ren hmac_hmac512_1 hmac_hmac512_1.enc
call %OPENSSL%\openssl dgst -sha512 -hmac "Jefe" -c -binary -out hmac_hmac512_2 plaintext_7
ren hmac_hmac512_2 hmac_hmac512_2.enc

View File

@ -29,6 +29,7 @@
* - @subpage cry_test_sequence_006
* - @subpage cry_test_sequence_007
* - @subpage cry_test_sequence_008
* - @subpage cry_test_sequence_009
* .
*/
@ -58,6 +59,7 @@ const testsequence_t * const cry_test_suite_array[] = {
&cry_test_sequence_006,
&cry_test_sequence_007,
&cry_test_sequence_008,
&cry_test_sequence_009,
NULL
};

View File

@ -32,6 +32,7 @@
#include "cry_test_sequence_006.h"
#include "cry_test_sequence_007.h"
#include "cry_test_sequence_008.h"
#include "cry_test_sequence_009.h"
#if !defined(__DOXYGEN__)

View File

@ -78,23 +78,23 @@ static void cry_test_005_001_execute(void) {
/* [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]);
uint32_t random[4];
int i;
test_assert(ret == CRY_NOERROR , "failed random");
ret = cryTRNG(&CRYD1,(uint8_t*)&random);
SHOW_DATA(&random[i],1);
test_assert(ret == CRY_NOERROR , "failed random");
test_assert(random[i] != 0 , "failed random generation (zero)");
SHOW_DATA(&random[0],4);
for (i=0;i<4;i++) {
test_assert(random[i] != 0 , "failed random generation (zero)");
}
for (i=1;i<4;i++)
{
test_assert(random[i-1] != random[i] , "failed random generation");
}
for (j=0;j<i;j++)
{
test_assert(random[i] != random[j] , "failed random generation");
}
}
}
}

View File

@ -0,0 +1,279 @@
/*
ChibiOS - Copyright (C) 2006..2018 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_009.c
* @brief Test Sequence 009 code.
*
* @page cry_test_sequence_009 [9] HMAC
*
* File: @ref cry_test_sequence_009.c
*
* <h2>Description</h2>
* HMAC testing.
*
* <h2>Test Cases</h2>
* - @subpage cry_test_009_001
* - @subpage cry_test_009_002
* .
*/
/****************************************************************************
* Shared code.
****************************************************************************/
#include <string.h>
#include "ref_hmac.h"
#define plaintext msg_clear
#define HMACOUT msg_encrypted
#define shabuffer msg_decrypted
#define MAX_SHA_BLOCK_SIZE TEST_MSG_DATA_BYTE_LEN
#define MAX_SHA_BLOCK_SIZE_INWORD (MAX_SHA_BLOCK_SIZE/4)
static const CRYConfig config_Polling=
{
TRANSFER_POLLING,
0
};
static const CRYConfig config_DMA=
{
TRANSFER_DMA,
0
};
static const int hmackeys_size[]=
{
20,
4,
20
};
static const uint8_t hmackey_1[]=
{
0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,
0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,
};
// "Hi There",
static const uint8_t hmacmsg1[8]= "Hi There";
static const size_t hmacmsg_size[]=
{
8,
};
static void cryHMAC256(CRYDriver *cryp) {
cryerror_t ret;
HMACSHA256Context hmacshactxp;
uint8_t *keyp;
hmacshactxp.shacontext.sha.sha_buffer = (uint8_t*)&shabuffer[0];
hmacshactxp.shacontext.sha.sha_buffer_size = MAX_SHA_BLOCK_SIZE;
keyp =(uint8_t *)hmackey_1;
ret = cryLoadTransientKey(&CRYD1, (cryalgorithm_t) cry_algo_hmac,hmackeys_size[0], keyp);
test_assert(ret == CRY_NOERROR, "failed load transient key");
ret = cryHMACSHA256Init(cryp,&hmacshactxp);
test_assert(ret == CRY_NOERROR, "failed init HMACSHA256");
ret = cryHMACSHA256Update(cryp,&hmacshactxp,hmacmsg_size[0],(const uint8_t *)&hmacmsg1);
test_assert(ret == CRY_NOERROR, "failed update HMACSHA256");
ret = cryHMACSHA256Final(cryp,&hmacshactxp,(uint8_t *)HMACOUT);
test_assert(ret == CRY_NOERROR, "failed final HMACSHA256");
SHOW_DATA(HMACOUT,8);
}
static void cryHMAC512(CRYDriver *cryp) {
cryerror_t ret;
HMACSHA512Context hmacshactxp;
uint8_t *keyp;
hmacshactxp.shacontext.sha.sha_buffer = (uint8_t*)&shabuffer[0];
hmacshactxp.shacontext.sha.sha_buffer_size = MAX_SHA_BLOCK_SIZE;
keyp =(uint8_t *)hmackey_1;
ret = cryLoadTransientKey(&CRYD1, (cryalgorithm_t) cry_algo_hmac,hmackeys_size[0], keyp);
test_assert(ret == CRY_NOERROR, "failed load transient key");
ret = cryHMACSHA512Init(cryp,&hmacshactxp);
test_assert(ret == CRY_NOERROR, "failed init HMACSHA512");
ret = cryHMACSHA512Update(cryp,&hmacshactxp,hmacmsg_size[0],(const uint8_t *)&hmacmsg1);
test_assert(ret == CRY_NOERROR, "failed update HMACSHA512");
ret = cryHMACSHA512Final(cryp,&hmacshactxp,(uint8_t *)HMACOUT);
test_assert(ret == CRY_NOERROR, "failed final HMACSHA512");
SHOW_DATA(HMACOUT,16);
}
/****************************************************************************
* Test cases.
****************************************************************************/
/**
* @page cry_test_009_001 [9.1] HMAC Polling
*
* <h2>Description</h2>
* testing HMAC in polled mode.
*
* <h2>Test Steps</h2>
* - [9.1.1] hmac 256.
* - [9.1.2] hmac 512.
* .
*/
static void cry_test_009_001_setup(void) {
memset(HMACOUT, 0xff, TEST_MSG_DATA_BYTE_LEN);
cryStart(&CRYD1, &config_Polling);
}
static void cry_test_009_001_teardown(void) {
cryStop(&CRYD1);
}
static void cry_test_009_001_execute(void) {
/* [9.1.1] hmac 256.*/
test_set_step(1);
{
cryHMAC256(&CRYD1);
for (int i = 0; i < 8; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC256_1)[i], "hmac mismatch");
}
}
/* [9.1.2] hmac 512.*/
test_set_step(2);
{
cryHMAC512(&CRYD1);
for (int i = 0; i < 16; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC512_1)[i], "hmac mismatch");
}
}
}
static const testcase_t cry_test_009_001 = {
"HMAC Polling",
cry_test_009_001_setup,
cry_test_009_001_teardown,
cry_test_009_001_execute
};
/**
* @page cry_test_009_002 [9.2] HMAC DMA
*
* <h2>Description</h2>
* testing HMAC in DMA mode.
*
* <h2>Test Steps</h2>
* - [9.2.1] hmac 256.
* - [9.2.2] hmac 512.
* .
*/
static void cry_test_009_002_setup(void) {
memset(HMACOUT, 0xff, TEST_MSG_DATA_BYTE_LEN);
cryStart(&CRYD1, &config_DMA);
}
static void cry_test_009_002_teardown(void) {
cryStop(&CRYD1);
}
static void cry_test_009_002_execute(void) {
/* [9.2.1] hmac 256.*/
test_set_step(1);
{
cryHMAC256(&CRYD1);
for (int i = 0; i < 8; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC256_1)[i], "hmac mismatch");
}
}
/* [9.2.2] hmac 512.*/
test_set_step(2);
{
cryHMAC512(&CRYD1);
for (int i = 0; i < 16; i++) {
test_assert(HMACOUT[i] == ((uint32_t*) refHMAC_HMAC512_1)[i], "hmac mismatch");
}
}
}
static const testcase_t cry_test_009_002 = {
"HMAC DMA",
cry_test_009_002_setup,
cry_test_009_002_teardown,
cry_test_009_002_execute
};
/****************************************************************************
* Exported data.
****************************************************************************/
/**
* @brief Array of test cases.
*/
const testcase_t * const cry_test_sequence_009_array[] = {
&cry_test_009_001,
&cry_test_009_002,
NULL
};
/**
* @brief HMAC.
*/
const testsequence_t cry_test_sequence_009 = {
"HMAC",
cry_test_sequence_009_array
};

View File

@ -0,0 +1,27 @@
/*
ChibiOS - Copyright (C) 2006..2018 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_009.h
* @brief Test Sequence 009 header.
*/
#ifndef CRY_TEST_SEQUENCE_009_H
#define CRY_TEST_SEQUENCE_009_H
extern const testsequence_t cry_test_sequence_009;
#endif /* CRY_TEST_SEQUENCE_009_H */

View File

@ -0,0 +1,48 @@
/*
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 refHMAC_HMAC256_1[]={
0xB0,0x34,0x4C,0x61,0xD8,0xDB,0x38,0x53,0x5C,0xA8,
0xAF,0xCE,0xAF,0x0B,0xF1,0x2B,0x88,0x1D,0xC2,0x00,
0xC9,0x83,0x3D,0xA7,0x26,0xE9,0x37,0x6C,0x2E,0x32,
0xCF,0xF7,
};
const uint8_t refHMAC_HMAC256_2[]={
0x5B,0xDC,0xC1,0x46,0xBF,0x60,0x75,0x4E,0x6A,0x04,
0x24,0x26,0x08,0x95,0x75,0xC7,0x5A,0x00,0x3F,0x08,
0x9D,0x27,0x39,0x83,0x9D,0xEC,0x58,0xB9,0x64,0xEC,
0x38,0x43,
};
const uint8_t refHMAC_HMAC512_1[]={
0x87,0xAA,0x7C,0xDE,0xA5,0xEF,0x61,0x9D,0x4F,0xF0,
0xB4,0x24,0x1A,0x1D,0x6C,0xB0,0x23,0x79,0xF4,0xE2,
0xCE,0x4E,0xC2,0x78,0x7A,0xD0,0xB3,0x05,0x45,0xE1,
0x7C,0xDE,0xDA,0xA8,0x33,0xB7,0xD6,0xB8,0xA7,0x02,
0x03,0x8B,0x27,0x4E,0xAE,0xA3,0xF4,0xE4,0xBE,0x9D,
0x91,0x4E,0xEB,0x61,0xF1,0x70,0x2E,0x69,0x6C,0x20,
0x3A,0x12,0x68,0x54,
};
const uint8_t refHMAC_HMAC512_2[]={
0x16,0x4B,0x7A,0x7B,0xFC,0xF8,0x19,0xE2,0xE3,0x95,
0xFB,0xE7,0x3B,0x56,0xE0,0xA3,0x87,0xBD,0x64,0x22,
0x2E,0x83,0x1F,0xD6,0x10,0x27,0x0C,0xD7,0xEA,0x25,
0x05,0x54,0x97,0x58,0xBF,0x75,0xC0,0x5A,0x99,0x4A,
0x6D,0x03,0x4F,0x65,0xF8,0xF0,0xE6,0xFD,0xCA,0xEA,
0xB1,0xA3,0x4D,0x4A,0x6B,0x4B,0x63,0x6E,0x07,0x0A,
0x38,0xBC,0xE7,0x37,
};

View File

@ -0,0 +1,23 @@
/*
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_HMAC_H_
#define TEST_REF_HMAC_H_
extern const uint8_t refHMAC_HMAC256_1[];
extern const uint8_t refHMAC_HMAC256_2[];
extern const uint8_t refHMAC_HMAC512_1[];
extern const uint8_t refHMAC_HMAC512_2[];
#endif //TEST_REF_HMAC_H_