mirror of https://github.com/ARMmbed/mbed-os.git
[NUC472] Refine coding style
parent
0c5b860409
commit
315b684bd9
|
@ -32,8 +32,10 @@
|
||||||
#include "mbed_assert.h"
|
#include "mbed_assert.h"
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
static void mbedtls_zeroize( void *v, size_t n )
|
||||||
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
|
{
|
||||||
|
volatile unsigned char *p = (unsigned char*)v;
|
||||||
|
while( n-- ) *p++ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,8 +57,7 @@ static void swapInitVector(unsigned char iv[16])
|
||||||
int i;
|
int i;
|
||||||
// iv SWAP
|
// iv SWAP
|
||||||
piv = (unsigned int*)iv;
|
piv = (unsigned int*)iv;
|
||||||
for( i=0; i< 4; i++)
|
for( i=0; i< 4; i++) {
|
||||||
{
|
|
||||||
*piv = (((*piv) & 0x000000FF) << 24) |
|
*piv = (((*piv) & 0x000000FF) << 24) |
|
||||||
(((*piv) & 0x0000FF00) << 8) |
|
(((*piv) & 0x0000FF00) << 8) |
|
||||||
(((*piv) & 0x00FF0000) >> 8) |
|
(((*piv) & 0x00FF0000) >> 8) |
|
||||||
|
@ -78,10 +79,8 @@ static unsigned char channel_flag[4]={0x00,0x00,0x00,0x00}; // 0: idle, 1: busy
|
||||||
static int channel_alloc()
|
static int channel_alloc()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i=0; i< (int)sizeof(channel_flag); i++)
|
for(i=0; i< (int)sizeof(channel_flag); i++) {
|
||||||
{
|
if( channel_flag[i] == 0x00 ) {
|
||||||
if( channel_flag[i] == 0x00 )
|
|
||||||
{
|
|
||||||
channel_flag[i] = 0x01;
|
channel_flag[i] = 0x01;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -106,8 +105,7 @@ void mbedtls_aes_init( mbedtls_aes_context *ctx )
|
||||||
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
|
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
|
||||||
|
|
||||||
ctx->swapType = AES_IN_OUT_SWAP;
|
ctx->swapType = AES_IN_OUT_SWAP;
|
||||||
while( (i = channel_alloc()) < 0 )
|
while( (i = channel_alloc()) < 0 ) {
|
||||||
{
|
|
||||||
mbed_assert_internal("No available AES channel", __FILE__, __LINE__);
|
mbed_assert_internal("No available AES channel", __FILE__, __LINE__);
|
||||||
//osDelay(300);
|
//osDelay(300);
|
||||||
}
|
}
|
||||||
|
@ -152,8 +150,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
|
|
||||||
switch( keybits )
|
switch( keybits ) {
|
||||||
{
|
|
||||||
case 128:
|
case 128:
|
||||||
ctx->keySize = AES_KEY_SIZE_128;
|
ctx->keySize = AES_KEY_SIZE_128;
|
||||||
break;
|
break;
|
||||||
|
@ -163,14 +160,14 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
|
||||||
case 256:
|
case 256:
|
||||||
ctx->keySize = AES_KEY_SIZE_256;
|
ctx->keySize = AES_KEY_SIZE_256;
|
||||||
break;
|
break;
|
||||||
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
|
default :
|
||||||
|
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// key swap
|
// key swap
|
||||||
for( i = 0; i < ( keybits >> 5 ); i++ )
|
for( i = 0; i < ( keybits >> 5 ); i++ ) {
|
||||||
{
|
|
||||||
ctx->buf[i] = (*(key+i*4) << 24) |
|
ctx->buf[i] = (*(key+i*4) << 24) |
|
||||||
(*(key+1+i*4) << 16) |
|
(*(key+1+i*4) << 16) |
|
||||||
(*(key+2+i*4) << 8) |
|
(*(key+2+i*4) << 8) |
|
||||||
|
@ -211,15 +208,13 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
|
||||||
|
|
||||||
AES_Open(ctx->channel, ctx->encDec, ctx->opMode, ctx->keySize, ctx->swapType);
|
AES_Open(ctx->channel, ctx->encDec, ctx->opMode, ctx->keySize, ctx->swapType);
|
||||||
AES_SetInitVect(ctx->channel, ctx->iv);
|
AES_SetInitVect(ctx->channel, ctx->iv);
|
||||||
if( ((uint32_t)input) & 0x03 )
|
if( ((uint32_t)input) & 0x03 ) {
|
||||||
{
|
|
||||||
memcpy(au8InputData, input, dataSize);
|
memcpy(au8InputData, input, dataSize);
|
||||||
pIn = au8InputData;
|
pIn = au8InputData;
|
||||||
} else {
|
} else {
|
||||||
pIn = (unsigned char*)input;
|
pIn = (unsigned char*)input;
|
||||||
}
|
}
|
||||||
if( (((uint32_t)output) & 0x03) || (dataSize%4)) // HW CFB output byte count must be multiple of word
|
if( (((uint32_t)output) & 0x03) || (dataSize%4)) { // HW CFB output byte count must be multiple of word
|
||||||
{
|
|
||||||
pOut = au8OutputData;
|
pOut = au8OutputData;
|
||||||
} else {
|
} else {
|
||||||
pOut = output;
|
pOut = output;
|
||||||
|
@ -301,21 +296,18 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
|
||||||
if( length % 16 )
|
if( length % 16 )
|
||||||
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
|
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
|
||||||
|
|
||||||
if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) )
|
if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) ) {
|
||||||
{
|
|
||||||
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
|
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
|
||||||
} else {
|
} else {
|
||||||
blockChainLen = length;
|
blockChainLen = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
while( length > 0 )
|
while( length > 0 ) {
|
||||||
{
|
|
||||||
ctx->opMode = AES_MODE_CBC;
|
ctx->opMode = AES_MODE_CBC;
|
||||||
swapInitVector(iv); // iv SWAP
|
swapInitVector(iv); // iv SWAP
|
||||||
ctx->iv = (uint32_t *)iv;
|
ctx->iv = (uint32_t *)iv;
|
||||||
|
|
||||||
if( mode == MBEDTLS_AES_ENCRYPT )
|
if( mode == MBEDTLS_AES_ENCRYPT ) {
|
||||||
{
|
|
||||||
ctx->encDec = 1;
|
ctx->encDec = 1;
|
||||||
__nvt_aes_crypt(ctx, input, output, blockChainLen);
|
__nvt_aes_crypt(ctx, input, output, blockChainLen);
|
||||||
// if( blockChainLen == length ) break; // finish last block chain but still need to prepare next iv for mbedtls_aes_self_test()
|
// if( blockChainLen == length ) break; // finish last block chain but still need to prepare next iv for mbedtls_aes_self_test()
|
||||||
|
@ -354,14 +346,11 @@ static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx,
|
||||||
int c;
|
int c;
|
||||||
size_t n = *iv_off;
|
size_t n = *iv_off;
|
||||||
unsigned char iv_tmp[16];
|
unsigned char iv_tmp[16];
|
||||||
if( mode == MBEDTLS_AES_DECRYPT )
|
if( mode == MBEDTLS_AES_DECRYPT ) {
|
||||||
{
|
while( length-- ) {
|
||||||
while( length-- )
|
|
||||||
{
|
|
||||||
if( n == 0)
|
if( n == 0)
|
||||||
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
|
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
|
||||||
else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode
|
else if( ctx->opMode == AES_MODE_CFB) { // For previous cryption is CFB mode
|
||||||
{
|
|
||||||
memcpy(iv_tmp, iv, n);
|
memcpy(iv_tmp, iv, n);
|
||||||
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
|
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
|
||||||
memcpy(iv, iv_tmp, n);
|
memcpy(iv, iv_tmp, n);
|
||||||
|
@ -373,15 +362,11 @@ static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx,
|
||||||
|
|
||||||
n = ( n + 1 ) & 0x0F;
|
n = ( n + 1 ) & 0x0F;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
while( length-- ) {
|
||||||
{
|
|
||||||
while( length-- )
|
|
||||||
{
|
|
||||||
if( n == 0 )
|
if( n == 0 )
|
||||||
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
|
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
|
||||||
else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode
|
else if( ctx->opMode == AES_MODE_CFB) { // For previous cryption is CFB mode
|
||||||
{
|
|
||||||
memcpy(iv_tmp, iv, n);
|
memcpy(iv_tmp, iv, n);
|
||||||
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
|
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
|
||||||
memcpy(iv, iv_tmp, n);
|
memcpy(iv, iv_tmp, n);
|
||||||
|
@ -423,8 +408,7 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
// For address or byte count non-word alignment, go through reserved DMA buffer.
|
// For address or byte count non-word alignment, go through reserved DMA buffer.
|
||||||
if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) ) // Must reserved DMA buffer for each block
|
if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) ) { // Must reserved DMA buffer for each block
|
||||||
{
|
|
||||||
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
|
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
|
||||||
} else if(length%4) { // Need reserved DMA buffer once for last chain
|
} else if(length%4) { // Need reserved DMA buffer once for last chain
|
||||||
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? (length - length%16) : length );
|
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? (length - length%16) : length );
|
||||||
|
@ -433,8 +417,7 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
// proceed: start with block alignment
|
// proceed: start with block alignment
|
||||||
while( length > 0 )
|
while( length > 0 ) {
|
||||||
{
|
|
||||||
|
|
||||||
ctx->opMode = AES_MODE_CFB;
|
ctx->opMode = AES_MODE_CFB;
|
||||||
|
|
||||||
|
@ -444,16 +427,13 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
|
||||||
remLen = blockChainLen%16;
|
remLen = blockChainLen%16;
|
||||||
ivLen = (( remLen > 0) ? remLen: 16 );
|
ivLen = (( remLen > 0) ? remLen: 16 );
|
||||||
|
|
||||||
if( mode == MBEDTLS_AES_DECRYPT )
|
if( mode == MBEDTLS_AES_DECRYPT ) {
|
||||||
{
|
|
||||||
memcpy(temp, input+blockChainLen - ivLen, ivLen);
|
memcpy(temp, input+blockChainLen - ivLen, ivLen);
|
||||||
if(blockChainLen >= 16) memcpy(ctx->prv_iv, input+blockChainLen-remLen-16, 16);
|
if(blockChainLen >= 16) memcpy(ctx->prv_iv, input+blockChainLen-remLen-16, 16);
|
||||||
ctx->encDec = 0;
|
ctx->encDec = 0;
|
||||||
__nvt_aes_crypt(ctx, input, output, blockChainLen);
|
__nvt_aes_crypt(ctx, input, output, blockChainLen);
|
||||||
memcpy(iv,temp, ivLen);
|
memcpy(iv,temp, ivLen);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
ctx->encDec = 1;
|
ctx->encDec = 1;
|
||||||
__nvt_aes_crypt(ctx, input, output, blockChainLen);
|
__nvt_aes_crypt(ctx, input, output, blockChainLen);
|
||||||
if(blockChainLen >= 16) memcpy(ctx->prv_iv, output+blockChainLen-remLen-16, 16);
|
if(blockChainLen >= 16) memcpy(ctx->prv_iv, output+blockChainLen-remLen-16, 16);
|
||||||
|
@ -484,8 +464,7 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
unsigned char ov[17];
|
unsigned char ov[17];
|
||||||
|
|
||||||
while( length-- )
|
while( length-- ) {
|
||||||
{
|
|
||||||
memcpy( ov, iv, 16 );
|
memcpy( ov, iv, 16 );
|
||||||
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
|
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
|
||||||
|
|
||||||
|
@ -519,8 +498,7 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
|
||||||
int c, i;
|
int c, i;
|
||||||
size_t n = *nc_off;
|
size_t n = *nc_off;
|
||||||
|
|
||||||
while( length-- )
|
while( length-- ) {
|
||||||
{
|
|
||||||
if( n == 0 ) {
|
if( n == 0 ) {
|
||||||
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block );
|
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block );
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,7 @@ extern "C" {
|
||||||
* - to simplify key expansion in the 256-bit case by
|
* - to simplify key expansion in the 256-bit case by
|
||||||
* generating an extra round key
|
* generating an extra round key
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
uint32_t keySize;
|
uint32_t keySize;
|
||||||
uint32_t encDec;
|
uint32_t encDec;
|
||||||
uint32_t opMode;
|
uint32_t opMode;
|
||||||
|
|
|
@ -48,8 +48,7 @@ int crypto_sha_acquire(void)
|
||||||
if (crypto_sha_avail) {
|
if (crypto_sha_avail) {
|
||||||
crypto_sha_avail = 0;
|
crypto_sha_avail = 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,8 @@ static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
|
||||||
171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
|
171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
|
||||||
199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
|
199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
|
||||||
227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
|
227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
|
||||||
254 };
|
254
|
||||||
|
};
|
||||||
|
|
||||||
void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||||
{
|
{
|
||||||
|
@ -114,8 +115,7 @@ int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SI
|
||||||
|
|
||||||
#define WEAK_KEY_COUNT 16
|
#define WEAK_KEY_COUNT 16
|
||||||
|
|
||||||
static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
|
static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] = {
|
||||||
{
|
|
||||||
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
|
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
|
||||||
{ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
|
{ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
|
||||||
{ 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
|
{ 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
|
||||||
|
@ -310,8 +310,7 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
|
||||||
// Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
|
// Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
|
||||||
if (keyopt == 1) {
|
if (keyopt == 1) {
|
||||||
CRPT->TDES_CTL |= CRPT_TDES_CTL_3KEYS_Msk;
|
CRPT->TDES_CTL |= CRPT_TDES_CTL_3KEYS_Msk;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
CRPT->TDES_CTL &= ~CRPT_TDES_CTL_3KEYS_Msk;
|
CRPT->TDES_CTL &= ~CRPT_TDES_CTL_3KEYS_Msk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,8 +388,7 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
|
||||||
// IV (dec) = input block
|
// IV (dec) = input block
|
||||||
if (enc) {
|
if (enc) {
|
||||||
memcpy(iv, dmabuf_out + data_len - 8, 8);
|
memcpy(iv, dmabuf_out + data_len - 8, 8);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
memcpy(iv, dmabuf_in + data_len - 8, 8);
|
memcpy(iv, dmabuf_in + data_len - 8, 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ extern "C" {
|
||||||
/**
|
/**
|
||||||
* \brief DES context structure
|
* \brief DES context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
int enc; /*!< 0: dec, 1: enc */
|
int enc; /*!< 0: dec, 1: enc */
|
||||||
uint16_t keyopt;
|
uint16_t keyopt;
|
||||||
uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
|
uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
|
||||||
|
@ -44,8 +43,7 @@ mbedtls_des_context;
|
||||||
/**
|
/**
|
||||||
* \brief Triple-DES context structure
|
* \brief Triple-DES context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
int enc; /*!< 0: dec, 1: enc */
|
int enc; /*!< 0: dec, 1: enc */
|
||||||
uint16_t keyopt;
|
uint16_t keyopt;
|
||||||
uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
|
uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
|
||||||
|
|
|
@ -33,8 +33,10 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
static void mbedtls_zeroize( void *v, size_t n )
|
||||||
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
|
{
|
||||||
|
volatile unsigned char *p = (unsigned char*)v;
|
||||||
|
while( n-- ) *p++ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -63,8 +65,7 @@ static void mbedtls_zeroize( void *v, size_t n ) {
|
||||||
/*
|
/*
|
||||||
* Expanded DES S-boxes
|
* Expanded DES S-boxes
|
||||||
*/
|
*/
|
||||||
static const uint32_t SB1[64] =
|
static const uint32_t SB1[64] = {
|
||||||
{
|
|
||||||
0x01010400, 0x00000000, 0x00010000, 0x01010404,
|
0x01010400, 0x00000000, 0x00010000, 0x01010404,
|
||||||
0x01010004, 0x00010404, 0x00000004, 0x00010000,
|
0x01010004, 0x00010404, 0x00000004, 0x00010000,
|
||||||
0x00000400, 0x01010400, 0x01010404, 0x00000400,
|
0x00000400, 0x01010400, 0x01010404, 0x00000400,
|
||||||
|
@ -83,8 +84,7 @@ static const uint32_t SB1[64] =
|
||||||
0x00010004, 0x00010400, 0x00000000, 0x01010004
|
0x00010004, 0x00010400, 0x00000000, 0x01010004
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB2[64] =
|
static const uint32_t SB2[64] = {
|
||||||
{
|
|
||||||
0x80108020, 0x80008000, 0x00008000, 0x00108020,
|
0x80108020, 0x80008000, 0x00008000, 0x00108020,
|
||||||
0x00100000, 0x00000020, 0x80100020, 0x80008020,
|
0x00100000, 0x00000020, 0x80100020, 0x80008020,
|
||||||
0x80000020, 0x80108020, 0x80108000, 0x80000000,
|
0x80000020, 0x80108020, 0x80108000, 0x80000000,
|
||||||
|
@ -103,8 +103,7 @@ static const uint32_t SB2[64] =
|
||||||
0x80000000, 0x80100020, 0x80108020, 0x00108000
|
0x80000000, 0x80100020, 0x80108020, 0x00108000
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB3[64] =
|
static const uint32_t SB3[64] = {
|
||||||
{
|
|
||||||
0x00000208, 0x08020200, 0x00000000, 0x08020008,
|
0x00000208, 0x08020200, 0x00000000, 0x08020008,
|
||||||
0x08000200, 0x00000000, 0x00020208, 0x08000200,
|
0x08000200, 0x00000000, 0x00020208, 0x08000200,
|
||||||
0x00020008, 0x08000008, 0x08000008, 0x00020000,
|
0x00020008, 0x08000008, 0x08000008, 0x00020000,
|
||||||
|
@ -123,8 +122,7 @@ static const uint32_t SB3[64] =
|
||||||
0x00020208, 0x00000008, 0x08020008, 0x00020200
|
0x00020208, 0x00000008, 0x08020008, 0x00020200
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB4[64] =
|
static const uint32_t SB4[64] = {
|
||||||
{
|
|
||||||
0x00802001, 0x00002081, 0x00002081, 0x00000080,
|
0x00802001, 0x00002081, 0x00002081, 0x00000080,
|
||||||
0x00802080, 0x00800081, 0x00800001, 0x00002001,
|
0x00802080, 0x00800081, 0x00800001, 0x00002001,
|
||||||
0x00000000, 0x00802000, 0x00802000, 0x00802081,
|
0x00000000, 0x00802000, 0x00802000, 0x00802081,
|
||||||
|
@ -143,8 +141,7 @@ static const uint32_t SB4[64] =
|
||||||
0x00000080, 0x00800000, 0x00002000, 0x00802080
|
0x00000080, 0x00800000, 0x00002000, 0x00802080
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB5[64] =
|
static const uint32_t SB5[64] = {
|
||||||
{
|
|
||||||
0x00000100, 0x02080100, 0x02080000, 0x42000100,
|
0x00000100, 0x02080100, 0x02080000, 0x42000100,
|
||||||
0x00080000, 0x00000100, 0x40000000, 0x02080000,
|
0x00080000, 0x00000100, 0x40000000, 0x02080000,
|
||||||
0x40080100, 0x00080000, 0x02000100, 0x40080100,
|
0x40080100, 0x00080000, 0x02000100, 0x40080100,
|
||||||
|
@ -163,8 +160,7 @@ static const uint32_t SB5[64] =
|
||||||
0x00000000, 0x40080000, 0x02080100, 0x40000100
|
0x00000000, 0x40080000, 0x02080100, 0x40000100
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB6[64] =
|
static const uint32_t SB6[64] = {
|
||||||
{
|
|
||||||
0x20000010, 0x20400000, 0x00004000, 0x20404010,
|
0x20000010, 0x20400000, 0x00004000, 0x20404010,
|
||||||
0x20400000, 0x00000010, 0x20404010, 0x00400000,
|
0x20400000, 0x00000010, 0x20404010, 0x00400000,
|
||||||
0x20004000, 0x00404010, 0x00400000, 0x20000010,
|
0x20004000, 0x00404010, 0x00400000, 0x20000010,
|
||||||
|
@ -183,8 +179,7 @@ static const uint32_t SB6[64] =
|
||||||
0x20404000, 0x20000000, 0x00400010, 0x20004010
|
0x20404000, 0x20000000, 0x00400010, 0x20004010
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB7[64] =
|
static const uint32_t SB7[64] = {
|
||||||
{
|
|
||||||
0x00200000, 0x04200002, 0x04000802, 0x00000000,
|
0x00200000, 0x04200002, 0x04000802, 0x00000000,
|
||||||
0x00000800, 0x04000802, 0x00200802, 0x04200800,
|
0x00000800, 0x04000802, 0x00200802, 0x04200800,
|
||||||
0x04200802, 0x00200000, 0x00000000, 0x04000002,
|
0x04200802, 0x00200000, 0x00000000, 0x04000002,
|
||||||
|
@ -203,8 +198,7 @@ static const uint32_t SB7[64] =
|
||||||
0x04000002, 0x04000800, 0x00000800, 0x00200002
|
0x04000002, 0x04000800, 0x00000800, 0x00200002
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t SB8[64] =
|
static const uint32_t SB8[64] = {
|
||||||
{
|
|
||||||
0x10001040, 0x00001000, 0x00040000, 0x10041040,
|
0x10001040, 0x00001000, 0x00040000, 0x10041040,
|
||||||
0x10000000, 0x10001040, 0x00000040, 0x10000000,
|
0x10000000, 0x10001040, 0x00000040, 0x10000000,
|
||||||
0x00040040, 0x10040000, 0x10041040, 0x00041000,
|
0x00040040, 0x10040000, 0x10041040, 0x00041000,
|
||||||
|
@ -226,16 +220,14 @@ static const uint32_t SB8[64] =
|
||||||
/*
|
/*
|
||||||
* PC1: left and right halves bit-swap
|
* PC1: left and right halves bit-swap
|
||||||
*/
|
*/
|
||||||
static const uint32_t LHs[16] =
|
static const uint32_t LHs[16] = {
|
||||||
{
|
|
||||||
0x00000000, 0x00000001, 0x00000100, 0x00000101,
|
0x00000000, 0x00000001, 0x00000100, 0x00000101,
|
||||||
0x00010000, 0x00010001, 0x00010100, 0x00010101,
|
0x00010000, 0x00010001, 0x00010100, 0x00010101,
|
||||||
0x01000000, 0x01000001, 0x01000100, 0x01000101,
|
0x01000000, 0x01000001, 0x01000100, 0x01000101,
|
||||||
0x01010000, 0x01010001, 0x01010100, 0x01010101
|
0x01010000, 0x01010001, 0x01010100, 0x01010101
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint32_t RHs[16] =
|
static const uint32_t RHs[16] = {
|
||||||
{
|
|
||||||
0x00000000, 0x01000000, 0x00010000, 0x01010000,
|
0x00000000, 0x01000000, 0x00010000, 0x01010000,
|
||||||
0x00000100, 0x01000100, 0x00010100, 0x01010100,
|
0x00000100, 0x01000100, 0x00010100, 0x01010100,
|
||||||
0x00000001, 0x01000001, 0x00010001, 0x01010001,
|
0x00000001, 0x01000001, 0x00010001, 0x01010001,
|
||||||
|
@ -325,7 +317,8 @@ static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
|
||||||
171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
|
171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
|
||||||
199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
|
199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
|
||||||
227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
|
227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
|
||||||
254 };
|
254
|
||||||
|
};
|
||||||
|
|
||||||
void mbedtls_des_sw_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
void mbedtls_des_sw_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
||||||
{
|
{
|
||||||
|
@ -372,8 +365,7 @@ int mbedtls_des_sw_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY
|
||||||
|
|
||||||
#define WEAK_KEY_COUNT 16
|
#define WEAK_KEY_COUNT 16
|
||||||
|
|
||||||
static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
|
static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] = {
|
||||||
{
|
|
||||||
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
|
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
|
||||||
{ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
|
{ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
|
||||||
{ 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
|
{ 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
|
||||||
|
@ -415,8 +407,12 @@ void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KE
|
||||||
/*
|
/*
|
||||||
* Permuted Choice 1
|
* Permuted Choice 1
|
||||||
*/
|
*/
|
||||||
T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
|
T = ((Y >> 4) ^ X) & 0x0F0F0F0F;
|
||||||
T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
|
X ^= T;
|
||||||
|
Y ^= (T << 4);
|
||||||
|
T = ((Y ) ^ X) & 0x10101010;
|
||||||
|
X ^= T;
|
||||||
|
Y ^= (T );
|
||||||
|
|
||||||
X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
|
X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
|
||||||
| (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
|
| (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
|
||||||
|
@ -434,15 +430,11 @@ void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KE
|
||||||
/*
|
/*
|
||||||
* calculate subkeys
|
* calculate subkeys
|
||||||
*/
|
*/
|
||||||
for( i = 0; i < 16; i++ )
|
for( i = 0; i < 16; i++ ) {
|
||||||
{
|
if( i < 2 || i == 8 || i == 15 ) {
|
||||||
if( i < 2 || i == 8 || i == 15 )
|
|
||||||
{
|
|
||||||
X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
|
X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
|
||||||
Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
|
Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
|
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
|
||||||
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
|
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
|
||||||
}
|
}
|
||||||
|
@ -492,8 +484,7 @@ int mbedtls_des_sw_setkey_dec( mbedtls_des_sw_context *ctx, const unsigned char
|
||||||
|
|
||||||
mbedtls_des_setkey( ctx->sk, key );
|
mbedtls_des_setkey( ctx->sk, key );
|
||||||
|
|
||||||
for( i = 0; i < 16; i += 2 )
|
for( i = 0; i < 16; i += 2 ) {
|
||||||
{
|
|
||||||
SWAP( ctx->sk[i ], ctx->sk[30 - i] );
|
SWAP( ctx->sk[i ], ctx->sk[30 - i] );
|
||||||
SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
|
SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
|
||||||
}
|
}
|
||||||
|
@ -510,8 +501,7 @@ static void des3_set2key( uint32_t esk[96],
|
||||||
mbedtls_des_setkey( esk, key );
|
mbedtls_des_setkey( esk, key );
|
||||||
mbedtls_des_setkey( dsk + 32, key + 8 );
|
mbedtls_des_setkey( dsk + 32, key + 8 );
|
||||||
|
|
||||||
for( i = 0; i < 32; i += 2 )
|
for( i = 0; i < 32; i += 2 ) {
|
||||||
{
|
|
||||||
dsk[i ] = esk[30 - i];
|
dsk[i ] = esk[30 - i];
|
||||||
dsk[i + 1] = esk[31 - i];
|
dsk[i + 1] = esk[31 - i];
|
||||||
|
|
||||||
|
@ -564,8 +554,7 @@ static void des3_set3key( uint32_t esk[96],
|
||||||
mbedtls_des_setkey( dsk + 32, key + 8 );
|
mbedtls_des_setkey( dsk + 32, key + 8 );
|
||||||
mbedtls_des_setkey( esk + 64, key + 16 );
|
mbedtls_des_setkey( esk + 64, key + 16 );
|
||||||
|
|
||||||
for( i = 0; i < 32; i += 2 )
|
for( i = 0; i < 32; i += 2 ) {
|
||||||
{
|
|
||||||
dsk[i ] = esk[94 - i];
|
dsk[i ] = esk[94 - i];
|
||||||
dsk[i + 1] = esk[95 - i];
|
dsk[i + 1] = esk[95 - i];
|
||||||
|
|
||||||
|
@ -622,8 +611,7 @@ int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx,
|
||||||
|
|
||||||
DES_IP( X, Y );
|
DES_IP( X, Y );
|
||||||
|
|
||||||
for( i = 0; i < 8; i++ )
|
for( i = 0; i < 8; i++ ) {
|
||||||
{
|
|
||||||
DES_ROUND( Y, X );
|
DES_ROUND( Y, X );
|
||||||
DES_ROUND( X, Y );
|
DES_ROUND( X, Y );
|
||||||
}
|
}
|
||||||
|
@ -653,10 +641,8 @@ int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx,
|
||||||
if( length % 8 )
|
if( length % 8 )
|
||||||
return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
|
return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
|
||||||
|
|
||||||
if( mode == MBEDTLS_DES_ENCRYPT )
|
if( mode == MBEDTLS_DES_ENCRYPT ) {
|
||||||
{
|
while( length > 0 ) {
|
||||||
while( length > 0 )
|
|
||||||
{
|
|
||||||
for( i = 0; i < 8; i++ )
|
for( i = 0; i < 8; i++ )
|
||||||
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
||||||
|
|
||||||
|
@ -667,11 +653,8 @@ int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx,
|
||||||
output += 8;
|
output += 8;
|
||||||
length -= 8;
|
length -= 8;
|
||||||
}
|
}
|
||||||
}
|
} else { /* MBEDTLS_DES_DECRYPT */
|
||||||
else /* MBEDTLS_DES_DECRYPT */
|
while( length > 0 ) {
|
||||||
{
|
|
||||||
while( length > 0 )
|
|
||||||
{
|
|
||||||
memcpy( temp, input, 8 );
|
memcpy( temp, input, 8 );
|
||||||
mbedtls_des_sw_crypt_ecb( ctx, input, output );
|
mbedtls_des_sw_crypt_ecb( ctx, input, output );
|
||||||
|
|
||||||
|
@ -707,20 +690,17 @@ int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx,
|
||||||
|
|
||||||
DES_IP( X, Y );
|
DES_IP( X, Y );
|
||||||
|
|
||||||
for( i = 0; i < 8; i++ )
|
for( i = 0; i < 8; i++ ) {
|
||||||
{
|
|
||||||
DES_ROUND( Y, X );
|
DES_ROUND( Y, X );
|
||||||
DES_ROUND( X, Y );
|
DES_ROUND( X, Y );
|
||||||
}
|
}
|
||||||
|
|
||||||
for( i = 0; i < 8; i++ )
|
for( i = 0; i < 8; i++ ) {
|
||||||
{
|
|
||||||
DES_ROUND( X, Y );
|
DES_ROUND( X, Y );
|
||||||
DES_ROUND( Y, X );
|
DES_ROUND( Y, X );
|
||||||
}
|
}
|
||||||
|
|
||||||
for( i = 0; i < 8; i++ )
|
for( i = 0; i < 8; i++ ) {
|
||||||
{
|
|
||||||
DES_ROUND( Y, X );
|
DES_ROUND( Y, X );
|
||||||
DES_ROUND( X, Y );
|
DES_ROUND( X, Y );
|
||||||
}
|
}
|
||||||
|
@ -750,10 +730,8 @@ int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx,
|
||||||
if( length % 8 )
|
if( length % 8 )
|
||||||
return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
|
return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
|
||||||
|
|
||||||
if( mode == MBEDTLS_DES_ENCRYPT )
|
if( mode == MBEDTLS_DES_ENCRYPT ) {
|
||||||
{
|
while( length > 0 ) {
|
||||||
while( length > 0 )
|
|
||||||
{
|
|
||||||
for( i = 0; i < 8; i++ )
|
for( i = 0; i < 8; i++ )
|
||||||
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
||||||
|
|
||||||
|
@ -764,11 +742,8 @@ int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx,
|
||||||
output += 8;
|
output += 8;
|
||||||
length -= 8;
|
length -= 8;
|
||||||
}
|
}
|
||||||
}
|
} else { /* MBEDTLS_DES_DECRYPT */
|
||||||
else /* MBEDTLS_DES_DECRYPT */
|
while( length > 0 ) {
|
||||||
{
|
|
||||||
while( length > 0 )
|
|
||||||
{
|
|
||||||
memcpy( temp, input, 8 );
|
memcpy( temp, input, 8 );
|
||||||
mbedtls_des3_sw_crypt_ecb( ctx, input, output );
|
mbedtls_des3_sw_crypt_ecb( ctx, input, output );
|
||||||
|
|
||||||
|
|
|
@ -38,8 +38,7 @@ extern "C" {
|
||||||
/**
|
/**
|
||||||
* \brief DES context structure
|
* \brief DES context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
uint32_t sk[32]; /*!< DES subkeys */
|
uint32_t sk[32]; /*!< DES subkeys */
|
||||||
}
|
}
|
||||||
mbedtls_des_sw_context;
|
mbedtls_des_sw_context;
|
||||||
|
@ -47,8 +46,7 @@ mbedtls_des_sw_context;
|
||||||
/**
|
/**
|
||||||
* \brief Triple-DES context structure
|
* \brief Triple-DES context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
uint32_t sk[96]; /*!< 3DES subkeys */
|
uint32_t sk[96]; /*!< 3DES subkeys */
|
||||||
}
|
}
|
||||||
mbedtls_des3_sw_context;
|
mbedtls_des3_sw_context;
|
||||||
|
|
|
@ -28,8 +28,7 @@ void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
|
||||||
if (crypto_sha_acquire()) {
|
if (crypto_sha_acquire()) {
|
||||||
ctx->ishw = 1;
|
ctx->ishw = 1;
|
||||||
mbedtls_sha1_hw_init(&ctx->hw_ctx);
|
mbedtls_sha1_hw_init(&ctx->hw_ctx);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ctx->ishw = 0;
|
ctx->ishw = 0;
|
||||||
mbedtls_sha1_sw_init(&ctx->sw_ctx);
|
mbedtls_sha1_sw_init(&ctx->sw_ctx);
|
||||||
}
|
}
|
||||||
|
@ -44,8 +43,7 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha1_hw_free(&ctx->hw_ctx);
|
mbedtls_sha1_hw_free(&ctx->hw_ctx);
|
||||||
crypto_sha_release();
|
crypto_sha_release();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha1_sw_free(&ctx->sw_ctx);
|
mbedtls_sha1_sw_free(&ctx->sw_ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,8 +71,7 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
|
||||||
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
|
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
|
||||||
mbedtls_sha1_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
|
mbedtls_sha1_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Clone S/W ctx from S/W ctx
|
// Clone S/W ctx from S/W ctx
|
||||||
dst->sw_ctx = src->sw_ctx;
|
dst->sw_ctx = src->sw_ctx;
|
||||||
}
|
}
|
||||||
|
@ -87,8 +84,7 @@ void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha1_hw_starts(&ctx->hw_ctx);
|
mbedtls_sha1_hw_starts(&ctx->hw_ctx);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha1_sw_starts(&ctx->sw_ctx);
|
mbedtls_sha1_sw_starts(&ctx->sw_ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,8 +96,7 @@ void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input,
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha1_hw_update(&ctx->hw_ctx, input, ilen);
|
mbedtls_sha1_hw_update(&ctx->hw_ctx, input, ilen);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha1_sw_update(&ctx->sw_ctx, input, ilen);
|
mbedtls_sha1_sw_update(&ctx->sw_ctx, input, ilen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,8 +108,7 @@ void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha1_hw_finish(&ctx->hw_ctx, output);
|
mbedtls_sha1_hw_finish(&ctx->hw_ctx, output);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha1_sw_finish(&ctx->sw_ctx, output);
|
mbedtls_sha1_sw_finish(&ctx->sw_ctx, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,8 +117,7 @@ void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha1_hw_process(&ctx->hw_ctx, data);
|
mbedtls_sha1_hw_process(&ctx->hw_ctx, data);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha1_sw_process(&ctx->sw_ctx, data);
|
mbedtls_sha1_sw_process(&ctx->sw_ctx, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ struct mbedtls_sha1_context_s;
|
||||||
/**
|
/**
|
||||||
* \brief SHA-1 context structure
|
* \brief SHA-1 context structure
|
||||||
*/
|
*/
|
||||||
typedef struct mbedtls_sha1_context_s
|
typedef struct mbedtls_sha1_context_s {
|
||||||
{
|
|
||||||
int ishw;
|
int ishw;
|
||||||
crypto_sha_context hw_ctx;
|
crypto_sha_context hw_ctx;
|
||||||
mbedtls_sha1_sw_context sw_ctx;
|
mbedtls_sha1_sw_context sw_ctx;
|
||||||
|
|
|
@ -40,8 +40,10 @@
|
||||||
#endif /* MBEDTLS_SELF_TEST */
|
#endif /* MBEDTLS_SELF_TEST */
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
static void mbedtls_zeroize( void *v, size_t n )
|
||||||
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
|
{
|
||||||
|
volatile unsigned char *p = (unsigned char*)v;
|
||||||
|
while( n-- ) *p++ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -277,8 +279,7 @@ void mbedtls_sha1_sw_update( mbedtls_sha1_sw_context *ctx, const unsigned char *
|
||||||
if( ctx->total[0] < (uint32_t) ilen )
|
if( ctx->total[0] < (uint32_t) ilen )
|
||||||
ctx->total[1]++;
|
ctx->total[1]++;
|
||||||
|
|
||||||
if( left && ilen >= fill )
|
if( left && ilen >= fill ) {
|
||||||
{
|
|
||||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||||
mbedtls_sha1_sw_process( ctx, ctx->buffer );
|
mbedtls_sha1_sw_process( ctx, ctx->buffer );
|
||||||
input += fill;
|
input += fill;
|
||||||
|
@ -286,8 +287,7 @@ void mbedtls_sha1_sw_update( mbedtls_sha1_sw_context *ctx, const unsigned char *
|
||||||
left = 0;
|
left = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while( ilen >= 64 )
|
while( ilen >= 64 ) {
|
||||||
{
|
|
||||||
mbedtls_sha1_sw_process( ctx, input );
|
mbedtls_sha1_sw_process( ctx, input );
|
||||||
input += 64;
|
input += 64;
|
||||||
ilen -= 64;
|
ilen -= 64;
|
||||||
|
@ -297,8 +297,7 @@ void mbedtls_sha1_sw_update( mbedtls_sha1_sw_context *ctx, const unsigned char *
|
||||||
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
||||||
}
|
}
|
||||||
|
|
||||||
static const unsigned char sha1_padding[64] =
|
static const unsigned char sha1_padding[64] = {
|
||||||
{
|
|
||||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
|
|
@ -38,8 +38,7 @@ extern "C" {
|
||||||
/**
|
/**
|
||||||
* \brief SHA-1 context structure
|
* \brief SHA-1 context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
uint32_t total[2]; /*!< number of bytes processed */
|
uint32_t total[2]; /*!< number of bytes processed */
|
||||||
uint32_t state[5]; /*!< intermediate digest state */
|
uint32_t state[5]; /*!< intermediate digest state */
|
||||||
unsigned char buffer[64]; /*!< data block being processed */
|
unsigned char buffer[64]; /*!< data block being processed */
|
||||||
|
|
|
@ -28,8 +28,7 @@ void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
|
||||||
if (crypto_sha_acquire()) {
|
if (crypto_sha_acquire()) {
|
||||||
ctx->ishw = 1;
|
ctx->ishw = 1;
|
||||||
mbedtls_sha256_hw_init(&ctx->hw_ctx);
|
mbedtls_sha256_hw_init(&ctx->hw_ctx);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ctx->ishw = 0;
|
ctx->ishw = 0;
|
||||||
mbedtls_sha256_sw_init(&ctx->sw_ctx);
|
mbedtls_sha256_sw_init(&ctx->sw_ctx);
|
||||||
}
|
}
|
||||||
|
@ -44,8 +43,7 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha256_hw_free(&ctx->hw_ctx);
|
mbedtls_sha256_hw_free(&ctx->hw_ctx);
|
||||||
crypto_sha_release();
|
crypto_sha_release();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha256_sw_free(&ctx->sw_ctx);
|
mbedtls_sha256_sw_free(&ctx->sw_ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,8 +72,7 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
|
||||||
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
|
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
|
||||||
mbedtls_sha256_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
|
mbedtls_sha256_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Clone S/W ctx from S/W ctx
|
// Clone S/W ctx from S/W ctx
|
||||||
dst->sw_ctx = src->sw_ctx;
|
dst->sw_ctx = src->sw_ctx;
|
||||||
}
|
}
|
||||||
|
@ -88,8 +85,7 @@ void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha256_hw_starts(&ctx->hw_ctx, is224);
|
mbedtls_sha256_hw_starts(&ctx->hw_ctx, is224);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha256_sw_starts(&ctx->sw_ctx, is224);
|
mbedtls_sha256_sw_starts(&ctx->sw_ctx, is224);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,8 +97,7 @@ void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *inp
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha256_hw_update(&ctx->hw_ctx, input, ilen);
|
mbedtls_sha256_hw_update(&ctx->hw_ctx, input, ilen);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha256_sw_update(&ctx->sw_ctx, input, ilen);
|
mbedtls_sha256_sw_update(&ctx->sw_ctx, input, ilen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,8 +109,7 @@ void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32]
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha256_hw_finish(&ctx->hw_ctx, output);
|
mbedtls_sha256_hw_finish(&ctx->hw_ctx, output);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha256_sw_finish(&ctx->sw_ctx, output);
|
mbedtls_sha256_sw_finish(&ctx->sw_ctx, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,8 +118,7 @@ void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char dat
|
||||||
{
|
{
|
||||||
if (ctx->ishw) {
|
if (ctx->ishw) {
|
||||||
mbedtls_sha256_hw_process(&ctx->hw_ctx, data);
|
mbedtls_sha256_hw_process(&ctx->hw_ctx, data);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha256_sw_process(&ctx->sw_ctx, data);
|
mbedtls_sha256_sw_process(&ctx->sw_ctx, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ struct mbedtls_sha256_context_s;
|
||||||
/**
|
/**
|
||||||
* \brief SHA-256 context structure
|
* \brief SHA-256 context structure
|
||||||
*/
|
*/
|
||||||
typedef struct mbedtls_sha256_context_s
|
typedef struct mbedtls_sha256_context_s {
|
||||||
{
|
|
||||||
int ishw;
|
int ishw;
|
||||||
crypto_sha_context hw_ctx;
|
crypto_sha_context hw_ctx;
|
||||||
mbedtls_sha256_sw_context sw_ctx;
|
mbedtls_sha256_sw_context sw_ctx;
|
||||||
|
|
|
@ -43,8 +43,10 @@
|
||||||
#endif /* MBEDTLS_SELF_TEST */
|
#endif /* MBEDTLS_SELF_TEST */
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
static void mbedtls_zeroize( void *v, size_t n )
|
||||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
{
|
||||||
|
volatile unsigned char *p = v;
|
||||||
|
while( n-- ) *p++ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -97,8 +99,7 @@ void mbedtls_sha256_sw_starts( mbedtls_sha256_sw_context *ctx, int is224 )
|
||||||
ctx->total[0] = 0;
|
ctx->total[0] = 0;
|
||||||
ctx->total[1] = 0;
|
ctx->total[1] = 0;
|
||||||
|
|
||||||
if( is224 == 0 )
|
if( is224 == 0 ) {
|
||||||
{
|
|
||||||
/* SHA-256 */
|
/* SHA-256 */
|
||||||
ctx->state[0] = 0x6A09E667;
|
ctx->state[0] = 0x6A09E667;
|
||||||
ctx->state[1] = 0xBB67AE85;
|
ctx->state[1] = 0xBB67AE85;
|
||||||
|
@ -108,9 +109,7 @@ void mbedtls_sha256_sw_starts( mbedtls_sha256_sw_context *ctx, int is224 )
|
||||||
ctx->state[5] = 0x9B05688C;
|
ctx->state[5] = 0x9B05688C;
|
||||||
ctx->state[6] = 0x1F83D9AB;
|
ctx->state[6] = 0x1F83D9AB;
|
||||||
ctx->state[7] = 0x5BE0CD19;
|
ctx->state[7] = 0x5BE0CD19;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
/* SHA-224 */
|
/* SHA-224 */
|
||||||
ctx->state[0] = 0xC1059ED8;
|
ctx->state[0] = 0xC1059ED8;
|
||||||
ctx->state[1] = 0x367CD507;
|
ctx->state[1] = 0x367CD507;
|
||||||
|
@ -125,8 +124,7 @@ void mbedtls_sha256_sw_starts( mbedtls_sha256_sw_context *ctx, int is224 )
|
||||||
ctx->is224 = is224;
|
ctx->is224 = is224;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint32_t K[] =
|
static const uint32_t K[] = {
|
||||||
{
|
|
||||||
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
|
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
|
||||||
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
|
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
|
||||||
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
|
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
|
||||||
|
@ -180,8 +178,7 @@ void mbedtls_sha256_sw_process( mbedtls_sha256_sw_context *ctx, const unsigned c
|
||||||
A[i] = ctx->state[i];
|
A[i] = ctx->state[i];
|
||||||
|
|
||||||
#if defined(MBEDTLS_SHA256_SMALLER)
|
#if defined(MBEDTLS_SHA256_SMALLER)
|
||||||
for( i = 0; i < 64; i++ )
|
for( i = 0; i < 64; i++ ) {
|
||||||
{
|
|
||||||
if( i < 16 )
|
if( i < 16 )
|
||||||
GET_UINT32_BE( W[i], data, 4 * i );
|
GET_UINT32_BE( W[i], data, 4 * i );
|
||||||
else
|
else
|
||||||
|
@ -189,15 +186,21 @@ void mbedtls_sha256_sw_process( mbedtls_sha256_sw_context *ctx, const unsigned c
|
||||||
|
|
||||||
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
|
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
|
||||||
|
|
||||||
temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
|
temp1 = A[7];
|
||||||
A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
|
A[7] = A[6];
|
||||||
|
A[6] = A[5];
|
||||||
|
A[5] = A[4];
|
||||||
|
A[4] = A[3];
|
||||||
|
A[3] = A[2];
|
||||||
|
A[2] = A[1];
|
||||||
|
A[1] = A[0];
|
||||||
|
A[0] = temp1;
|
||||||
}
|
}
|
||||||
#else /* MBEDTLS_SHA256_SMALLER */
|
#else /* MBEDTLS_SHA256_SMALLER */
|
||||||
for( i = 0; i < 16; i++ )
|
for( i = 0; i < 16; i++ )
|
||||||
GET_UINT32_BE( W[i], data, 4 * i );
|
GET_UINT32_BE( W[i], data, 4 * i );
|
||||||
|
|
||||||
for( i = 0; i < 16; i += 8 )
|
for( i = 0; i < 16; i += 8 ) {
|
||||||
{
|
|
||||||
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
|
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
|
||||||
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
|
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
|
||||||
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
|
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
|
||||||
|
@ -208,8 +211,7 @@ void mbedtls_sha256_sw_process( mbedtls_sha256_sw_context *ctx, const unsigned c
|
||||||
P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
|
P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
|
||||||
}
|
}
|
||||||
|
|
||||||
for( i = 16; i < 64; i += 8 )
|
for( i = 16; i < 64; i += 8 ) {
|
||||||
{
|
|
||||||
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
|
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
|
||||||
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
|
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
|
||||||
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
|
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
|
||||||
|
@ -246,8 +248,7 @@ void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned ch
|
||||||
if( ctx->total[0] < (uint32_t) ilen )
|
if( ctx->total[0] < (uint32_t) ilen )
|
||||||
ctx->total[1]++;
|
ctx->total[1]++;
|
||||||
|
|
||||||
if( left && ilen >= fill )
|
if( left && ilen >= fill ) {
|
||||||
{
|
|
||||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||||
mbedtls_sha256_sw_process( ctx, ctx->buffer );
|
mbedtls_sha256_sw_process( ctx, ctx->buffer );
|
||||||
input += fill;
|
input += fill;
|
||||||
|
@ -255,8 +256,7 @@ void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned ch
|
||||||
left = 0;
|
left = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while( ilen >= 64 )
|
while( ilen >= 64 ) {
|
||||||
{
|
|
||||||
mbedtls_sha256_sw_process( ctx, input );
|
mbedtls_sha256_sw_process( ctx, input );
|
||||||
input += 64;
|
input += 64;
|
||||||
ilen -= 64;
|
ilen -= 64;
|
||||||
|
@ -266,8 +266,7 @@ void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned ch
|
||||||
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
||||||
}
|
}
|
||||||
|
|
||||||
static const unsigned char sha256_padding[64] =
|
static const unsigned char sha256_padding[64] = {
|
||||||
{
|
|
||||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
|
|
@ -38,8 +38,7 @@ extern "C" {
|
||||||
/**
|
/**
|
||||||
* \brief SHA-256 context structure
|
* \brief SHA-256 context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
uint32_t total[2]; /*!< number of bytes processed */
|
uint32_t total[2]; /*!< number of bytes processed */
|
||||||
uint32_t state[8]; /*!< intermediate digest state */
|
uint32_t state[8]; /*!< intermediate digest state */
|
||||||
unsigned char buffer[64]; /*!< data block being processed */
|
unsigned char buffer[64]; /*!< data block being processed */
|
||||||
|
|
|
@ -90,8 +90,7 @@ void mbedtls_sha1_hw_finish(crypto_sha_context *ctx, unsigned char output[20])
|
||||||
crypto_sha_getinternstate(output, 20);
|
crypto_sha_getinternstate(output, 20);
|
||||||
|
|
||||||
CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
|
CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha1_sw_context ctx_sw;
|
mbedtls_sha1_sw_context ctx_sw;
|
||||||
|
|
||||||
mbedtls_sha1_sw_init(&ctx_sw);
|
mbedtls_sha1_sw_init(&ctx_sw);
|
||||||
|
@ -166,8 +165,7 @@ void mbedtls_sha256_hw_finish(crypto_sha_context *ctx, unsigned char output[32])
|
||||||
crypto_sha_getinternstate(output, ctx->is224 ? 28 : 32);
|
crypto_sha_getinternstate(output, ctx->is224 ? 28 : 32);
|
||||||
|
|
||||||
CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
|
CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mbedtls_sha256_sw_context ctx_sw;
|
mbedtls_sha256_sw_context ctx_sw;
|
||||||
|
|
||||||
mbedtls_sha256_sw_init(&ctx_sw);
|
mbedtls_sha256_sw_init(&ctx_sw);
|
||||||
|
@ -246,8 +244,7 @@ void crypto_sha_update_nobuf(crypto_sha_context *ctx, const unsigned char *input
|
||||||
}
|
}
|
||||||
CRPT->SHA_DMACNT = lastblock_size;
|
CRPT->SHA_DMACNT = lastblock_size;
|
||||||
CRPT->SHA_CTL = sha_ctl_start | CRPT_SHA_CTL_DMALAST_Msk;
|
CRPT->SHA_CTL = sha_ctl_start | CRPT_SHA_CTL_DMALAST_Msk;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
switch (sha_opmode) {
|
switch (sha_opmode) {
|
||||||
case SHA_MODE_SHA256:
|
case SHA_MODE_SHA256:
|
||||||
DGST7_old = CRPT->SHA_DGST7;
|
DGST7_old = CRPT->SHA_DGST7;
|
||||||
|
@ -264,8 +261,7 @@ void crypto_sha_update_nobuf(crypto_sha_context *ctx, const unsigned char *input
|
||||||
|
|
||||||
CRPT->SHA_CTL = sha_ctl_start;
|
CRPT->SHA_CTL = sha_ctl_start;
|
||||||
}
|
}
|
||||||
}
|
} else { // Non-last word of a complete block
|
||||||
else { // Non-last word of a complete block
|
|
||||||
CRPT->SHA_CTL = sha_ctl_start;
|
CRPT->SHA_CTL = sha_ctl_start;
|
||||||
}
|
}
|
||||||
while (! (CRPT->SHA_STS & CRPT_SHA_STS_DATINREQ_Msk));
|
while (! (CRPT->SHA_STS & CRPT_SHA_STS_DATINREQ_Msk));
|
||||||
|
@ -277,8 +273,7 @@ void crypto_sha_update_nobuf(crypto_sha_context *ctx, const unsigned char *input
|
||||||
|
|
||||||
if (islast) { // Finish of last block
|
if (islast) { // Finish of last block
|
||||||
while (CRPT->SHA_STS & CRPT_SHA_STS_BUSY_Msk);
|
while (CRPT->SHA_STS & CRPT_SHA_STS_BUSY_Msk);
|
||||||
}
|
} else { // Finish of non-last block
|
||||||
else { // Finish of non-last block
|
|
||||||
// No H/W flag to indicate finish of non-last block process.
|
// No H/W flag to indicate finish of non-last block process.
|
||||||
// Values of SHA_DGSTx registers will change as last word of the block is input, so use it for judgement.
|
// Values of SHA_DGSTx registers will change as last word of the block is input, so use it for judgement.
|
||||||
int isfinish = 0;
|
int isfinish = 0;
|
||||||
|
|
|
@ -29,8 +29,7 @@ extern "C" {
|
||||||
/**
|
/**
|
||||||
* \brief SHA context structure
|
* \brief SHA context structure
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
uint32_t total; /*!< number of bytes processed */
|
uint32_t total; /*!< number of bytes processed */
|
||||||
unsigned char buffer[128]; /*!< data block being processed. Max of SHA-1/SHA-256/SHA-512 */
|
unsigned char buffer[128]; /*!< data block being processed. Max of SHA-1/SHA-256/SHA-512 */
|
||||||
uint16_t buffer_left;
|
uint16_t buffer_left;
|
||||||
|
|
Loading…
Reference in New Issue