[NUC472] Refine coding style

pull/4925/head
ccli8 2017-08-17 14:26:15 +08:00
parent 0c5b860409
commit 315b684bd9
17 changed files with 578 additions and 659 deletions

View File

@ -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;
} }
@ -43,7 +45,7 @@ static uint32_t au32MyAESIV[4] = {
extern volatile int g_AES_done; extern volatile int g_AES_done;
// Must be a multiple of 16 bytes block size // Must be a multiple of 16 bytes block size
#define MAX_DMA_CHAIN_SIZE (16*6) #define MAX_DMA_CHAIN_SIZE (16*6)
static uint8_t au8OutputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4); static uint8_t au8OutputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4);
static uint8_t au8InputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4); static uint8_t au8InputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4);
@ -51,19 +53,18 @@ static uint8_t au8InputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4);
static void swapInitVector(unsigned char iv[16]) static void swapInitVector(unsigned char iv[16])
{ {
unsigned int* piv; unsigned int* piv;
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) | (((*piv) & 0xFF000000) >> 24);
(((*piv) & 0xFF000000) >> 24); piv++;
piv++; }
} }
}
//volatile void CRYPTO_IRQHandler() //volatile void CRYPTO_IRQHandler()
//{ //{
@ -74,54 +75,51 @@ static void swapInitVector(unsigned char iv[16])
//} //}
// AES available channel 0~3 // AES available channel 0~3
static unsigned char channel_flag[4]={0x00,0x00,0x00,0x00}; // 0: idle, 1: busy 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;
{ return i;
channel_flag[i] = 0x01; }
return i; }
} return(-1);
}
return(-1);
} }
static void channel_free(int i) static void channel_free(int i)
{ {
if( i >=0 && i < (int)sizeof(channel_flag) ) if( i >=0 && i < (int)sizeof(channel_flag) )
channel_flag[i] = 0x00; channel_flag[i] = 0x00;
} }
void mbedtls_aes_init( mbedtls_aes_context *ctx ) void mbedtls_aes_init( mbedtls_aes_context *ctx )
{ {
int i =-1; int i =-1;
// sw_mbedtls_aes_init(ctx);
// return;
// sw_mbedtls_aes_init(ctx);
// return;
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);
} }
ctx->channel = i; ctx->channel = i;
ctx->iv = au32MyAESIV; ctx->iv = au32MyAESIV;
/* Unlock protected registers */ /* Unlock protected registers */
SYS_UnlockReg(); SYS_UnlockReg();
CLK_EnableModuleClock(CRPT_MODULE); CLK_EnableModuleClock(CRPT_MODULE);
/* Lock protected registers */ /* Lock protected registers */
SYS_LockReg(); SYS_LockReg();
NVIC_EnableIRQ(CRPT_IRQn); NVIC_EnableIRQ(CRPT_IRQn);
AES_ENABLE_INT(); AES_ENABLE_INT();
} }
void mbedtls_aes_free( mbedtls_aes_context *ctx ) void mbedtls_aes_free( mbedtls_aes_context *ctx )
@ -138,8 +136,8 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx )
// SYS_LockReg(); // SYS_LockReg();
// NVIC_DisableIRQ(CRPT_IRQn); // NVIC_DisableIRQ(CRPT_IRQn);
// AES_DISABLE_INT(); // AES_DISABLE_INT();
channel_free(ctx->channel); channel_free(ctx->channel);
mbedtls_zeroize( ctx, sizeof( mbedtls_aes_context ) ); mbedtls_zeroize( ctx, sizeof( mbedtls_aes_context ) );
} }
@ -147,35 +145,34 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx )
* AES key schedule (encryption) * AES key schedule (encryption)
*/ */
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits ) unsigned int keybits )
{ {
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;
case 192: case 192:
ctx->keySize = AES_KEY_SIZE_192; ctx->keySize = AES_KEY_SIZE_192;
break; break;
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) | (*(key+3+i*4) );
(*(key+3+i*4) ); }
}
AES_SetKey(ctx->channel, ctx->buf, ctx->keySize); AES_SetKey(ctx->channel, ctx->buf, ctx->keySize);
@ -186,14 +183,14 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
* AES key schedule (decryption) * AES key schedule (decryption)
*/ */
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits ) unsigned int keybits )
{ {
int ret; int ret;
/* Also checks keybits */ /* Also checks keybits */
if( ( ret = mbedtls_aes_setkey_enc( ctx, key, keybits ) ) != 0 ) if( ( ret = mbedtls_aes_setkey_enc( ctx, key, keybits ) ) != 0 )
goto exit; goto exit;
exit: exit:
@ -202,30 +199,28 @@ exit:
static void __nvt_aes_crypt( mbedtls_aes_context *ctx, static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16], int dataSize) unsigned char output[16], int dataSize)
{ {
unsigned char* pIn; unsigned char* pIn;
unsigned char* pOut; unsigned char* pOut;
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
pOut = au8OutputData;
} else {
pOut = output;
} }
if( (((uint32_t)output) & 0x03) || (dataSize%4)) // HW CFB output byte count must be multiple of word
{
pOut = au8OutputData;
} else {
pOut = output;
}
AES_SetDMATransfer(ctx->channel, (uint32_t)pIn, (uint32_t)pOut, dataSize); AES_SetDMATransfer(ctx->channel, (uint32_t)pIn, (uint32_t)pOut, dataSize);
g_AES_done = 0; g_AES_done = 0;
AES_Start(ctx->channel, CRYPTO_DMA_ONE_SHOT); AES_Start(ctx->channel, CRYPTO_DMA_ONE_SHOT);
@ -243,10 +238,10 @@ void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
unsigned char output[16] ) unsigned char output[16] )
{ {
ctx->encDec = 1; ctx->encDec = 1;
__nvt_aes_crypt(ctx, input, output, 16); __nvt_aes_crypt(ctx, input, output, 16);
} }
/* /*
@ -256,10 +251,10 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16] ) unsigned char output[16] )
{ {
ctx->encDec = 0;
__nvt_aes_crypt(ctx, input, output, 16); ctx->encDec = 0;
__nvt_aes_crypt(ctx, input, output, 16);
} }
@ -268,18 +263,18 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
* AES-ECB block encryption/decryption * AES-ECB block encryption/decryption
*/ */
int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
int mode, int mode,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16] ) unsigned char output[16] )
{ {
ctx->opMode = AES_MODE_ECB; ctx->opMode = AES_MODE_ECB;
if( mode == MBEDTLS_AES_ENCRYPT ) if( mode == MBEDTLS_AES_ENCRYPT )
mbedtls_aes_encrypt( ctx, input, output ); mbedtls_aes_encrypt( ctx, input, output );
else else
mbedtls_aes_decrypt( ctx, input, output ); mbedtls_aes_decrypt( ctx, input, output );
return( 0 ); return( 0 );
} }
@ -289,49 +284,46 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
* AES-CBC buffer encryption/decryption * AES-CBC buffer encryption/decryption
*/ */
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
int mode, int mode,
size_t len, size_t len,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
unsigned char temp[16]; unsigned char temp[16];
int length = len; int length = len;
int blockChainLen; int blockChainLen;
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 ) {
ctx->encDec = 1;
__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()
memcpy( iv, output+blockChainLen-16, 16 );
} else {
memcpy( temp, input+blockChainLen-16, 16 );
ctx->encDec = 0;
__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()
memcpy( iv, temp, 16 );
}
length -= blockChainLen;
input += blockChainLen;
output += blockChainLen;
if(length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length; // For last remainder block chain
if( mode == MBEDTLS_AES_ENCRYPT )
{
ctx->encDec = 1;
__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()
memcpy( iv, output+blockChainLen-16, 16 );
}else{
memcpy( temp, input+blockChainLen-16, 16 );
ctx->encDec = 0;
__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()
memcpy( iv, temp, 16 );
}
length -= blockChainLen;
input += blockChainLen;
output += blockChainLen;
if(length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length; // For last remainder block chain
} }
return( 0 ); return( 0 );
@ -344,49 +336,42 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
*/ */
/* Support partial block encryption/decryption */ /* Support partial block encryption/decryption */
static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx, static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx,
int mode, int mode,
size_t length, size_t length,
size_t *iv_off, size_t *iv_off,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
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); }
}
c = *input++; c = *input++;
*output++ = (unsigned char)( c ^ iv[n] ); *output++ = (unsigned char)( c ^ iv[n] );
iv[n] = (unsigned char) c; iv[n] = (unsigned char) c;
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); }
}
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
n = ( n + 1 ) & 0x0F; n = ( n + 1 ) & 0x0F;
@ -399,75 +384,70 @@ static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx,
} }
int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
int mode, int mode,
size_t len, size_t len,
size_t *iv_off, size_t *iv_off,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
size_t n = *iv_off; size_t n = *iv_off;
unsigned char temp[16]; unsigned char temp[16];
int length=len; int length=len;
int blockChainLen; int blockChainLen;
int remLen=0; int remLen=0;
int ivLen; int ivLen;
// proceed: start with partial block by ECB mode first
if( n !=0 ) {
__nvt_aes_crypt_partial_block_cfb128(ctx, mode, 16 - n , iv_off, iv, input, output);
input += (16 - n);
output += (16 - n);
length -= (16 - n);
}
// 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
{
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
} else if(length%4) { // Need reserved DMA buffer once for last chain
blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? (length - length%16) : length );
} else { // Not need reserved DMA buffer
blockChainLen = length;
}
// proceed: start with block alignment
while( length > 0 )
{
ctx->opMode = AES_MODE_CFB;
swapInitVector(iv); // iv SWAP // proceed: start with partial block by ECB mode first
if( n !=0 ) {
ctx->iv = (uint32_t *)iv; __nvt_aes_crypt_partial_block_cfb128(ctx, mode, 16 - n, iv_off, iv, input, output);
remLen = blockChainLen%16; input += (16 - n);
ivLen = (( remLen > 0) ? remLen: 16 ); output += (16 - n);
length -= (16 - n);
if( mode == MBEDTLS_AES_DECRYPT ) }
{
memcpy(temp, input+blockChainLen - ivLen, ivLen); // For address or byte count non-word alignment, go through reserved DMA buffer.
if(blockChainLen >= 16) memcpy(ctx->prv_iv, input+blockChainLen-remLen-16 , 16); if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) ) { // Must reserved DMA buffer for each block
ctx->encDec = 0; blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
__nvt_aes_crypt(ctx, input, output, blockChainLen); } else if(length%4) { // Need reserved DMA buffer once for last chain
memcpy(iv,temp, ivLen); blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? (length - length%16) : length );
} } else { // Not need reserved DMA buffer
else blockChainLen = length;
{ }
ctx->encDec = 1;
__nvt_aes_crypt(ctx, input, output, blockChainLen); // proceed: start with block alignment
if(blockChainLen >= 16) memcpy(ctx->prv_iv, output+blockChainLen-remLen-16 , 16); while( length > 0 ) {
memcpy(iv,output+blockChainLen-ivLen,ivLen);
} ctx->opMode = AES_MODE_CFB;
length -= blockChainLen;
swapInitVector(iv); // iv SWAP
ctx->iv = (uint32_t *)iv;
remLen = blockChainLen%16;
ivLen = (( remLen > 0) ? remLen: 16 );
if( mode == MBEDTLS_AES_DECRYPT ) {
memcpy(temp, input+blockChainLen - ivLen, ivLen);
if(blockChainLen >= 16) memcpy(ctx->prv_iv, input+blockChainLen-remLen-16, 16);
ctx->encDec = 0;
__nvt_aes_crypt(ctx, input, output, blockChainLen);
memcpy(iv,temp, ivLen);
} else {
ctx->encDec = 1;
__nvt_aes_crypt(ctx, input, output, blockChainLen);
if(blockChainLen >= 16) memcpy(ctx->prv_iv, output+blockChainLen-remLen-16, 16);
memcpy(iv,output+blockChainLen-ivLen,ivLen);
}
length -= blockChainLen;
input += blockChainLen; input += blockChainLen;
output += blockChainLen; output += blockChainLen;
if(length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length; // For last remainder block chain if(length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length; // For last remainder block chain
} }
*iv_off = remLen; *iv_off = remLen;
return( 0 ); return( 0 );
} }
@ -475,17 +455,16 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
* AES-CFB8 buffer encryption/decryption * AES-CFB8 buffer encryption/decryption
*/ */
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
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 );
@ -509,18 +488,17 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
* AES-CTR buffer encryption/decryption * AES-CTR buffer encryption/decryption
*/ */
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
size_t length, size_t length,
size_t *nc_off, size_t *nc_off,
unsigned char nonce_counter[16], unsigned char nonce_counter[16],
unsigned char stream_block[16], unsigned char stream_block[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
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 );

View File

@ -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;
@ -51,7 +50,7 @@ typedef struct
uint32_t swapType; uint32_t swapType;
uint32_t *iv; uint32_t *iv;
unsigned char prv_iv[16]; unsigned char prv_iv[16];
uint32_t buf[8]; uint32_t buf[8];
} }
mbedtls_aes_context; mbedtls_aes_context;
@ -79,7 +78,7 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx );
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
*/ */
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits ); unsigned int keybits );
/** /**
* \brief AES key schedule (decryption) * \brief AES key schedule (decryption)
@ -91,7 +90,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
*/ */
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits ); unsigned int keybits );
/** /**
* \brief AES-ECB block encryption/decryption * \brief AES-ECB block encryption/decryption
@ -104,9 +103,9 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
int mode, int mode,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16] ); unsigned char output[16] );
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
/** /**
@ -132,11 +131,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
*/ */
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CBC */ #endif /* MBEDTLS_CIPHER_MODE_CBC */
#if defined(MBEDTLS_CIPHER_MODE_CFB) #if defined(MBEDTLS_CIPHER_MODE_CFB)
@ -166,12 +165,12 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
int mode, int mode,
size_t length, size_t length,
size_t *iv_off, size_t *iv_off,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
/** /**
* \brief AES-CFB8 buffer encryption/decryption. * \brief AES-CFB8 buffer encryption/decryption.
@ -198,11 +197,11 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /*MBEDTLS_CIPHER_MODE_CFB */ #endif /*MBEDTLS_CIPHER_MODE_CFB */
#if defined(MBEDTLS_CIPHER_MODE_CTR) #if defined(MBEDTLS_CIPHER_MODE_CTR)
@ -229,12 +228,12 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
size_t length, size_t length,
size_t *nc_off, size_t *nc_off,
unsigned char nonce_counter[16], unsigned char nonce_counter[16],
unsigned char stream_block[16], unsigned char stream_block[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CTR */ #endif /* MBEDTLS_CIPHER_MODE_CTR */
/** /**

View File

@ -30,7 +30,7 @@ void crypto_init(void)
return; return;
} }
crypto_inited = 1; crypto_inited = 1;
CLK_EnableModuleClock(CRPT_MODULE); CLK_EnableModuleClock(CRPT_MODULE);
} }
@ -48,11 +48,10 @@ 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;
} }
} }
void crypto_sha_release(void) void crypto_sha_release(void)

View File

@ -24,8 +24,8 @@
#include "nu_bitutil.h" #include "nu_bitutil.h"
#include "mbed_toolchain.h" #include "mbed_toolchain.h"
static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_SIZE], int enc, uint32_t tdes_opmode, size_t length, static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_SIZE], int enc, uint32_t tdes_opmode, size_t length,
unsigned char iv[8], const unsigned char *input, unsigned char *output); unsigned char iv[8], const unsigned char *input, unsigned char *output);
void mbedtls_des_init(mbedtls_des_context *ctx) void mbedtls_des_init(mbedtls_des_context *ctx)
{ {
@ -58,15 +58,16 @@ void mbedtls_des3_free( mbedtls_des3_context *ctx )
} }
static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8, static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81, 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112, 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140, 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168, 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
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 },
@ -157,7 +157,7 @@ int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MB
memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE); memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
memcpy(ctx->key[1], key, MBEDTLS_DES_KEY_SIZE); memcpy(ctx->key[1], key, MBEDTLS_DES_KEY_SIZE);
memcpy(ctx->key[2], key, MBEDTLS_DES_KEY_SIZE); memcpy(ctx->key[2], key, MBEDTLS_DES_KEY_SIZE);
return 0; return 0;
} }
@ -180,7 +180,7 @@ int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MB
* Triple-DES key schedule (112-bit, encryption) * Triple-DES key schedule (112-bit, encryption)
*/ */
int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx, int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
{ {
ctx->enc = 1; ctx->enc = 1;
// Keying option 2: K1 and K2 are independent, and K3 = K1. // Keying option 2: K1 and K2 are independent, and K3 = K1.
@ -196,7 +196,7 @@ int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
* Triple-DES key schedule (112-bit, decryption) * Triple-DES key schedule (112-bit, decryption)
*/ */
int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx, int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
{ {
ctx->enc = 0; ctx->enc = 0;
// Keying option 2: K1 and K2 are independent, and K3 = K1. // Keying option 2: K1 and K2 are independent, and K3 = K1.
@ -212,7 +212,7 @@ int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
* Triple-DES key schedule (168-bit, encryption) * Triple-DES key schedule (168-bit, encryption)
*/ */
int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx, int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
{ {
ctx->enc = 1; ctx->enc = 1;
// Keying option 1: All three keys are independent. // Keying option 1: All three keys are independent.
@ -228,7 +228,7 @@ int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
* Triple-DES key schedule (168-bit, decryption) * Triple-DES key schedule (168-bit, decryption)
*/ */
int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx, int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
{ {
ctx->enc = 0; ctx->enc = 0;
// Keying option 1: All three keys are independent. // Keying option 1: All three keys are independent.
@ -244,8 +244,8 @@ int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
* DES-ECB block encryption/decryption * DES-ECB block encryption/decryption
*/ */
int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ) unsigned char output[8] )
{ {
unsigned char iv[8] = {0x00}; unsigned char iv[8] = {0x00};
return mbedtls_des_docrypt(ctx->keyopt, ctx->key, ctx->enc, DES_MODE_ECB, 8, iv, input, output); return mbedtls_des_docrypt(ctx->keyopt, ctx->key, ctx->enc, DES_MODE_ECB, 8, iv, input, output);
@ -256,11 +256,11 @@ int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
* DES-CBC buffer encryption/decryption * DES-CBC buffer encryption/decryption
*/ */
int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx, int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
return mbedtls_des_docrypt(ctx->keyopt, ctx->key, mode == MBEDTLS_DES_ENCRYPT, DES_MODE_CBC, length, iv, input, output); return mbedtls_des_docrypt(ctx->keyopt, ctx->key, mode == MBEDTLS_DES_ENCRYPT, DES_MODE_CBC, length, iv, input, output);
} }
@ -270,8 +270,8 @@ int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
* 3DES-ECB block encryption/decryption * 3DES-ECB block encryption/decryption
*/ */
int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ) unsigned char output[8] )
{ {
unsigned char iv[8] = {0x00}; unsigned char iv[8] = {0x00};
return mbedtls_des_docrypt(ctx->keyopt, ctx->key, ctx->enc, TDES_MODE_ECB, 8, iv, input, output); return mbedtls_des_docrypt(ctx->keyopt, ctx->key, ctx->enc, TDES_MODE_ECB, 8, iv, input, output);
@ -282,11 +282,11 @@ int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
* 3DES-CBC buffer encryption/decryption * 3DES-CBC buffer encryption/decryption
*/ */
int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx, int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
return mbedtls_des_docrypt(ctx->keyopt, ctx->key, mode == MBEDTLS_DES_ENCRYPT, TDES_MODE_CBC, length, iv, input, output); return mbedtls_des_docrypt(ctx->keyopt, ctx->key, mode == MBEDTLS_DES_ENCRYPT, TDES_MODE_CBC, length, iv, input, output);
} }
@ -294,24 +294,23 @@ int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_SIZE], int enc, uint32_t tdes_opmode, size_t length, static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_SIZE], int enc, uint32_t tdes_opmode, size_t length,
unsigned char iv[8], const unsigned char *input, unsigned char *output) unsigned char iv[8], const unsigned char *input, unsigned char *output)
{ {
if (length % 8) { if (length % 8) {
return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH; return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH;
} }
// NOTE: Don't call driver function TDES_Open in BSP because it doesn't support TDES_CTL[3KEYS] setting. // NOTE: Don't call driver function TDES_Open in BSP because it doesn't support TDES_CTL[3KEYS] setting.
CRPT->TDES_CTL = (0 << CRPT_TDES_CTL_CHANNEL_Pos) | (enc << CRPT_TDES_CTL_ENCRPT_Pos) | CRPT->TDES_CTL = (0 << CRPT_TDES_CTL_CHANNEL_Pos) | (enc << CRPT_TDES_CTL_ENCRPT_Pos) |
tdes_opmode | (TDES_IN_OUT_WHL_SWAP << CRPT_TDES_CTL_BLKSWAP_Pos); tdes_opmode | (TDES_IN_OUT_WHL_SWAP << CRPT_TDES_CTL_BLKSWAP_Pos);
// Keying option 1: All three keys are independent. // Keying option 1: All three keys are independent.
// Keying option 2: K1 and K2 are independent, and K3 = K1. // Keying option 2: K1 and K2 are independent, and K3 = K1.
// 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;
} }
@ -331,72 +330,71 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
*tdes_key ++ = val; *tdes_key ++ = val;
val = nu_get32_be(key[2] + 4); val = nu_get32_be(key[2] + 4);
*tdes_key ++ = val; *tdes_key ++ = val;
uint32_t rmn = length; uint32_t rmn = length;
const unsigned char *in_pos = input; const unsigned char *in_pos = input;
unsigned char *out_pos = output; unsigned char *out_pos = output;
// Must be a multiple of 64-bit block size // Must be a multiple of 64-bit block size
#define MAXSIZE_DMABUF (8 * 5) #define MAXSIZE_DMABUF (8 * 5)
MBED_ALIGN(4) uint8_t dmabuf_in[MAXSIZE_DMABUF]; MBED_ALIGN(4) uint8_t dmabuf_in[MAXSIZE_DMABUF];
MBED_ALIGN(4) uint8_t dmabuf_out[MAXSIZE_DMABUF]; MBED_ALIGN(4) uint8_t dmabuf_out[MAXSIZE_DMABUF];
while (rmn) { while (rmn) {
uint32_t data_len = (rmn <= MAXSIZE_DMABUF) ? rmn : MAXSIZE_DMABUF; uint32_t data_len = (rmn <= MAXSIZE_DMABUF) ? rmn : MAXSIZE_DMABUF;
uint32_t ivh, ivl; uint32_t ivh, ivl;
ivh = nu_get32_be(iv); ivh = nu_get32_be(iv);
ivl = nu_get32_be(iv + 4); ivl = nu_get32_be(iv + 4);
TDES_SetInitVect(0, ivh, ivl); TDES_SetInitVect(0, ivh, ivl);
memcpy(dmabuf_in, in_pos, data_len); memcpy(dmabuf_in, in_pos, data_len);
TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len); TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len);
// Start enc/dec. // Start enc/dec.
// NOTE: Don't call driver function TDES_Start in BSP because it will override TDES_CTL[3KEYS] setting. // NOTE: Don't call driver function TDES_Start in BSP because it will override TDES_CTL[3KEYS] setting.
CRPT->TDES_CTL |= CRPT_TDES_CTL_START_Msk | (CRYPTO_DMA_ONE_SHOT << CRPT_TDES_CTL_DMALAST_Pos); CRPT->TDES_CTL |= CRPT_TDES_CTL_START_Msk | (CRYPTO_DMA_ONE_SHOT << CRPT_TDES_CTL_DMALAST_Pos);
while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk); while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk);
memcpy(out_pos, dmabuf_out, data_len); memcpy(out_pos, dmabuf_out, data_len);
in_pos += data_len; in_pos += data_len;
out_pos += data_len; out_pos += data_len;
rmn -= data_len; rmn -= data_len;
// Update IV for next block enc/dec in next function call // Update IV for next block enc/dec in next function call
switch (tdes_opmode) { switch (tdes_opmode) {
case DES_MODE_OFB: case DES_MODE_OFB:
case TDES_MODE_OFB: { case TDES_MODE_OFB: {
// OFB: IV (enc/dec) = output block XOR input block // OFB: IV (enc/dec) = output block XOR input block
uint32_t lbh, lbl; uint32_t lbh, lbl;
// Last block of input data // Last block of input data
lbh = nu_get32_be(dmabuf_in + data_len - 8 + 4); lbh = nu_get32_be(dmabuf_in + data_len - 8 + 4);
lbl = nu_get32_be(dmabuf_in + data_len - 8 + 0); lbl = nu_get32_be(dmabuf_in + data_len - 8 + 0);
// Last block of output data // Last block of output data
ivh = nu_get32_be(dmabuf_out + 4); ivh = nu_get32_be(dmabuf_out + 4);
ivl = nu_get32_be(dmabuf_out + 0); ivl = nu_get32_be(dmabuf_out + 0);
ivh = ivh ^ lbh; ivh = ivh ^ lbh;
ivl = ivl ^ lbl; ivl = ivl ^ lbl;
nu_set32_be(iv + 4, ivh); nu_set32_be(iv + 4, ivh);
nu_set32_be(iv, ivl); nu_set32_be(iv, ivl);
break; break;
} }
case DES_MODE_CBC: case DES_MODE_CBC:
case DES_MODE_CFB: case DES_MODE_CFB:
case TDES_MODE_CBC: case TDES_MODE_CBC:
case TDES_MODE_CFB: { case TDES_MODE_CFB: {
// CBC/CFB: IV (enc) = output block // CBC/CFB: IV (enc) = output block
// 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);
}
} }
} }
}
} }
return 0; return 0;
} }

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#ifndef MBEDTLS_DES_ALT_H #ifndef MBEDTLS_DES_ALT_H
#define MBEDTLS_DES_ALT_H #define MBEDTLS_DES_ALT_H
@ -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 */
@ -140,7 +138,7 @@ int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MB
* \return 0 * \return 0
*/ */
int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx, int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
/** /**
* \brief Triple-DES key schedule (112-bit, decryption) * \brief Triple-DES key schedule (112-bit, decryption)
@ -151,7 +149,7 @@ int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
* \return 0 * \return 0
*/ */
int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx, int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
/** /**
* \brief Triple-DES key schedule (168-bit, encryption) * \brief Triple-DES key schedule (168-bit, encryption)
@ -162,7 +160,7 @@ int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
* \return 0 * \return 0
*/ */
int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx, int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
/** /**
* \brief Triple-DES key schedule (168-bit, decryption) * \brief Triple-DES key schedule (168-bit, decryption)
@ -173,7 +171,7 @@ int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
* \return 0 * \return 0
*/ */
int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx, int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
/** /**
* \brief DES-ECB block encryption/decryption * \brief DES-ECB block encryption/decryption
@ -185,8 +183,8 @@ int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ); unsigned char output[8] );
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
/** /**
@ -208,11 +206,11 @@ int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
* \param output buffer holding the output data * \param output buffer holding the output data
*/ */
int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx, int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CBC */ #endif /* MBEDTLS_CIPHER_MODE_CBC */
/** /**
@ -225,8 +223,8 @@ int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ); unsigned char output[8] );
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
/** /**
@ -250,11 +248,11 @@ int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
* \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
*/ */
int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx, int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CBC */ #endif /* MBEDTLS_CIPHER_MODE_CBC */
/** /**

View File

@ -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,
@ -317,15 +309,16 @@ void mbedtls_des3_sw_free( mbedtls_des3_sw_context *ctx )
} }
static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8, static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81, 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112, 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140, 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168, 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
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,18 +407,22 @@ 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 ] )
| (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
| (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
| (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
| (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
| (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
X &= 0x0FFFFFFF; X &= 0x0FFFFFFF;
Y &= 0x0FFFFFFF; Y &= 0x0FFFFFFF;
@ -434,42 +430,38 @@ 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;
} }
*SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
| ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
| ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
| ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
| ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
| ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
| ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
| ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
| ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
| ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
| ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
*SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
| ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
| ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
| ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
| ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
| ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
| ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
| ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
| ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
| ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
| ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
} }
} }
@ -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];
@ -530,7 +520,7 @@ static void des3_set2key( uint32_t esk[96],
* Triple-DES key schedule (112-bit, encryption) * Triple-DES key schedule (112-bit, encryption)
*/ */
int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
{ {
uint32_t sk[96]; uint32_t sk[96];
@ -544,7 +534,7 @@ int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx,
* Triple-DES key schedule (112-bit, decryption) * Triple-DES key schedule (112-bit, decryption)
*/ */
int mbedtls_des3_sw_set2key_dec( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set2key_dec( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
{ {
uint32_t sk[96]; uint32_t sk[96];
@ -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];
@ -581,7 +570,7 @@ static void des3_set3key( uint32_t esk[96],
* Triple-DES key schedule (168-bit, encryption) * Triple-DES key schedule (168-bit, encryption)
*/ */
int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
{ {
uint32_t sk[96]; uint32_t sk[96];
@ -595,7 +584,7 @@ int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx,
* Triple-DES key schedule (168-bit, decryption) * Triple-DES key schedule (168-bit, decryption)
*/ */
int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
{ {
uint32_t sk[96]; uint32_t sk[96];
@ -609,8 +598,8 @@ int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx,
* DES-ECB block encryption/decryption * DES-ECB block encryption/decryption
*/ */
int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx, int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ) unsigned char output[8] )
{ {
int i; int i;
uint32_t X, Y, T, *SK; uint32_t X, Y, T, *SK;
@ -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 );
} }
@ -641,11 +629,11 @@ int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx,
* DES-CBC buffer encryption/decryption * DES-CBC buffer encryption/decryption
*/ */
int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx, int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
int i; int i;
unsigned char temp[8]; unsigned char temp[8];
@ -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 );
@ -694,8 +677,8 @@ int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx,
* 3DES-ECB block encryption/decryption * 3DES-ECB block encryption/decryption
*/ */
int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ) unsigned char output[8] )
{ {
int i; int i;
uint32_t X, Y, T, *SK; uint32_t X, Y, T, *SK;
@ -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 );
} }
@ -738,11 +718,11 @@ int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx,
* 3DES-CBC buffer encryption/decryption * 3DES-CBC buffer encryption/decryption
*/ */
int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
int i; int i;
unsigned char temp[8]; unsigned char temp[8];
@ -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 );

View File

@ -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;
@ -141,7 +139,7 @@ int mbedtls_des_sw_setkey_dec( mbedtls_des_sw_context *ctx, const unsigned char
* \return 0 * \return 0
*/ */
int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
/** /**
* \brief Triple-DES key schedule (112-bit, decryption) * \brief Triple-DES key schedule (112-bit, decryption)
@ -152,7 +150,7 @@ int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx,
* \return 0 * \return 0
*/ */
int mbedtls_des3_sw_set2key_dec( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set2key_dec( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
/** /**
* \brief Triple-DES key schedule (168-bit, encryption) * \brief Triple-DES key schedule (168-bit, encryption)
@ -163,7 +161,7 @@ int mbedtls_des3_sw_set2key_dec( mbedtls_des3_sw_context *ctx,
* \return 0 * \return 0
*/ */
int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
/** /**
* \brief Triple-DES key schedule (168-bit, decryption) * \brief Triple-DES key schedule (168-bit, decryption)
@ -174,7 +172,7 @@ int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx,
* \return 0 * \return 0
*/ */
int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
/** /**
* \brief DES-ECB block encryption/decryption * \brief DES-ECB block encryption/decryption
@ -186,8 +184,8 @@ int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx, int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ); unsigned char output[8] );
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
/** /**
@ -209,11 +207,11 @@ int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx,
* \param output buffer holding the output data * \param output buffer holding the output data
*/ */
int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx, int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CBC */ #endif /* MBEDTLS_CIPHER_MODE_CBC */
/** /**
@ -226,8 +224,8 @@ int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx,
const unsigned char input[8], const unsigned char input[8],
unsigned char output[8] ); unsigned char output[8] );
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
/** /**
@ -251,11 +249,11 @@ int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx,
* \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
*/ */
int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx, int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[8], unsigned char iv[8],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CBC */ #endif /* MBEDTLS_CIPHER_MODE_CBC */
/** /**
@ -267,7 +265,7 @@ int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx,
* \param key Base key * \param key Base key
*/ */
void mbedtls_des_sw_setkey( uint32_t SK[32], void mbedtls_des_sw_setkey( uint32_t SK[32],
const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -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);
} }
} }

View File

@ -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;

View File

@ -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;
} }
/* /*
@ -81,7 +83,7 @@ void mbedtls_sha1_sw_free( mbedtls_sha1_sw_context *ctx )
} }
void mbedtls_sha1_sw_clone( mbedtls_sha1_sw_context *dst, void mbedtls_sha1_sw_clone( mbedtls_sha1_sw_context *dst,
const mbedtls_sha1_sw_context *src ) const mbedtls_sha1_sw_context *src )
{ {
*dst = *src; *dst = *src;
} }
@ -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,9 +297,8 @@ 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,
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
@ -315,7 +314,7 @@ void mbedtls_sha1_sw_finish( mbedtls_sha1_sw_context *ctx, unsigned char output[
unsigned char msglen[8]; unsigned char msglen[8];
high = ( ctx->total[0] >> 29 ) high = ( ctx->total[0] >> 29 )
| ( ctx->total[1] << 3 ); | ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 ); low = ( ctx->total[0] << 3 );
PUT_UINT32_BE( high, msglen, 0 ); PUT_UINT32_BE( high, msglen, 0 );

View File

@ -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 */
@ -67,7 +66,7 @@ void mbedtls_sha1_sw_free( mbedtls_sha1_sw_context *ctx );
* \param src The context to be cloned * \param src The context to be cloned
*/ */
void mbedtls_sha1_sw_clone( mbedtls_sha1_sw_context *dst, void mbedtls_sha1_sw_clone( mbedtls_sha1_sw_context *dst,
const mbedtls_sha1_sw_context *src ); const mbedtls_sha1_sw_context *src );
/** /**
* \brief SHA-1 context setup * \brief SHA-1 context setup

View File

@ -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,14 +43,13 @@ 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);
} }
} }
void mbedtls_sha256_clone(mbedtls_sha256_context *dst, void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
const mbedtls_sha256_context *src) const mbedtls_sha256_context *src)
{ {
if (src->ishw) { if (src->ishw) {
// Clone S/W ctx from H/W ctx // Clone S/W ctx from H/W 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;
} }
@ -85,11 +82,10 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
* SHA-256 context setup * SHA-256 context setup
*/ */
void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224) 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);
} }
} }

View File

@ -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;
@ -80,7 +79,7 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
* \param ilen length of the input data * \param ilen length of the input data
*/ */
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
size_t ilen ); size_t ilen );
/** /**
* \brief SHA-256 final digest * \brief SHA-256 final digest

View File

@ -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;
} }
/* /*
@ -84,7 +86,7 @@ void mbedtls_sha256_sw_free( mbedtls_sha256_sw_context *ctx )
} }
void mbedtls_sha256_sw_clone( mbedtls_sha256_sw_context *dst, void mbedtls_sha256_sw_clone( mbedtls_sha256_sw_context *dst,
const mbedtls_sha256_sw_context *src ) const mbedtls_sha256_sw_context *src )
{ {
*dst = *src; *dst = *src;
} }
@ -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] );
@ -229,7 +231,7 @@ void mbedtls_sha256_sw_process( mbedtls_sha256_sw_context *ctx, const unsigned c
* SHA-256 process buffer * SHA-256 process buffer
*/ */
void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned char *input, void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned char *input,
size_t ilen ) size_t ilen )
{ {
size_t fill; size_t fill;
uint32_t left; uint32_t left;
@ -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,9 +266,8 @@ 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,
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
@ -284,7 +283,7 @@ void mbedtls_sha256_sw_finish( mbedtls_sha256_sw_context *ctx, unsigned char out
unsigned char msglen[8]; unsigned char msglen[8];
high = ( ctx->total[0] >> 29 ) high = ( ctx->total[0] >> 29 )
| ( ctx->total[1] << 3 ); | ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 ); low = ( ctx->total[0] << 3 );
PUT_UINT32_BE( high, msglen, 0 ); PUT_UINT32_BE( high, msglen, 0 );

View File

@ -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 */
@ -68,7 +67,7 @@ void mbedtls_sha256_sw_free( mbedtls_sha256_sw_context *ctx );
* \param src The context to be cloned * \param src The context to be cloned
*/ */
void mbedtls_sha256_sw_clone( mbedtls_sha256_sw_context *dst, void mbedtls_sha256_sw_clone( mbedtls_sha256_sw_context *dst,
const mbedtls_sha256_sw_context *src ); const mbedtls_sha256_sw_context *src );
/** /**
* \brief SHA-256 context setup * \brief SHA-256 context setup
@ -86,7 +85,7 @@ void mbedtls_sha256_sw_starts( mbedtls_sha256_sw_context *ctx, int is224 );
* \param ilen length of the input data * \param ilen length of the input data
*/ */
void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned char *input, void mbedtls_sha256_sw_update( mbedtls_sha256_sw_context *ctx, const unsigned char *input,
size_t ilen ); size_t ilen );
/** /**
* \brief SHA-256 final digest * \brief SHA-256 final digest

View File

@ -53,7 +53,7 @@ void mbedtls_sha1_hw_free(crypto_sha_context *ctx)
} }
void mbedtls_sha1_hw_clone(crypto_sha_context *dst, void mbedtls_sha1_hw_clone(crypto_sha_context *dst,
const crypto_sha_context *src) const crypto_sha_context *src)
{ {
*dst = *src; *dst = *src;
} }
@ -62,17 +62,17 @@ void mbedtls_sha1_hw_starts(crypto_sha_context *ctx)
{ {
// NOTE: mbedtls may call mbedtls_shaXXX_starts multiple times and then call the ending mbedtls_shaXXX_finish. Guard from it. // NOTE: mbedtls may call mbedtls_shaXXX_starts multiple times and then call the ending mbedtls_shaXXX_finish. Guard from it.
CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk; CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
ctx->total = 0; ctx->total = 0;
ctx->buffer_left = 0; ctx->buffer_left = 0;
ctx->blocksize = 64; ctx->blocksize = 64;
ctx->blocksize_mask = 0x3F; ctx->blocksize_mask = 0x3F;
SHA_Open(SHA_MODE_SHA1, SHA_NO_SWAP); SHA_Open(SHA_MODE_SHA1, SHA_NO_SWAP);
// Ensure we have correct initial inernal states in SHA_DGST registers even though SHA H/W is not actually started. // Ensure we have correct initial inernal states in SHA_DGST registers even though SHA H/W is not actually started.
CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk; CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk;
return; return;
} }
@ -88,12 +88,11 @@ void mbedtls_sha1_hw_finish(crypto_sha_context *ctx, unsigned char output[20])
crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1); crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1);
ctx->buffer_left = 0; ctx->buffer_left = 0;
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);
mbedtls_sha1_sw_starts(&ctx_sw); mbedtls_sha1_sw_starts(&ctx_sw);
mbedtls_sha1_sw_finish(&ctx_sw, output); mbedtls_sha1_sw_finish(&ctx_sw, output);
@ -128,7 +127,7 @@ void mbedtls_sha256_hw_free(crypto_sha_context *ctx)
} }
void mbedtls_sha256_hw_clone(crypto_sha_context *dst, void mbedtls_sha256_hw_clone(crypto_sha_context *dst,
const crypto_sha_context *src) const crypto_sha_context *src)
{ {
*dst = *src; *dst = *src;
} }
@ -137,7 +136,7 @@ void mbedtls_sha256_hw_starts( crypto_sha_context *ctx, int is224)
{ {
// NOTE: mbedtls may call mbedtls_shaXXX_starts multiple times and then call the ending mbedtls_shaXXX_finish. Guard from it. // NOTE: mbedtls may call mbedtls_shaXXX_starts multiple times and then call the ending mbedtls_shaXXX_finish. Guard from it.
CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk; CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
ctx->total = 0; ctx->total = 0;
ctx->buffer_left = 0; ctx->buffer_left = 0;
ctx->blocksize = 64; ctx->blocksize = 64;
@ -145,10 +144,10 @@ void mbedtls_sha256_hw_starts( crypto_sha_context *ctx, int is224)
ctx->is224 = is224; ctx->is224 = is224;
SHA_Open(is224 ? SHA_MODE_SHA224 : SHA_MODE_SHA256, SHA_NO_SWAP); SHA_Open(is224 ? SHA_MODE_SHA224 : SHA_MODE_SHA256, SHA_NO_SWAP);
// Ensure we have correct initial inernal states in SHA_DGST registers even though SHA H/W is not actually started. // Ensure we have correct initial inernal states in SHA_DGST registers even though SHA H/W is not actually started.
CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk; CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk;
return; return;
} }
@ -164,12 +163,11 @@ void mbedtls_sha256_hw_finish(crypto_sha_context *ctx, unsigned char output[32])
crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1); crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1);
ctx->buffer_left = 0; ctx->buffer_left = 0;
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);
mbedtls_sha256_sw_starts(&ctx_sw, ctx->is224); mbedtls_sha256_sw_starts(&ctx_sw, ctx->is224);
mbedtls_sha256_sw_finish(&ctx_sw, output); mbedtls_sha256_sw_finish(&ctx_sw, output);
@ -193,7 +191,7 @@ void crypto_sha_update(crypto_sha_context *ctx, const unsigned char *input, size
if (ilen == 0) { if (ilen == 0) {
return; return;
} }
size_t fill = ctx->blocksize - ctx->buffer_left; size_t fill = ctx->blocksize - ctx->buffer_left;
ctx->total += (uint32_t) ilen; ctx->total += (uint32_t) ilen;
@ -208,7 +206,7 @@ void crypto_sha_update(crypto_sha_context *ctx, const unsigned char *input, size
ctx->buffer_left = 0; ctx->buffer_left = 0;
} }
} }
while (ilen > ctx->blocksize) { while (ilen > ctx->blocksize) {
crypto_sha_update_nobuf(ctx, input, ctx->blocksize, 0); crypto_sha_update_nobuf(ctx, input, ctx->blocksize, 0);
input += ctx->blocksize; input += ctx->blocksize;
@ -227,16 +225,16 @@ void crypto_sha_update_nobuf(crypto_sha_context *ctx, const unsigned char *input
// 1. Last block which may be incomplete // 1. Last block which may be incomplete
// 2. Non-last block which is complete // 2. Non-last block which is complete
MBED_ASSERT(islast || ilen == ctx->blocksize); MBED_ASSERT(islast || ilen == ctx->blocksize);
const unsigned char *in_pos = input; const unsigned char *in_pos = input;
int rmn = ilen; int rmn = ilen;
uint32_t sha_ctl_start = (CRPT->SHA_CTL & ~(CRPT_SHA_CTL_DMALAST_Msk | CRPT_SHA_CTL_DMAEN_Msk)) | CRPT_SHA_CTL_START_Msk; uint32_t sha_ctl_start = (CRPT->SHA_CTL & ~(CRPT_SHA_CTL_DMALAST_Msk | CRPT_SHA_CTL_DMAEN_Msk)) | CRPT_SHA_CTL_START_Msk;
uint32_t sha_opmode = (CRPT->SHA_CTL & CRPT_SHA_CTL_OPMODE_Msk) >> CRPT_SHA_CTL_OPMODE_Pos; uint32_t sha_opmode = (CRPT->SHA_CTL & CRPT_SHA_CTL_OPMODE_Msk) >> CRPT_SHA_CTL_OPMODE_Pos;
uint32_t DGST0_old, DGST1_old, DGST2_old, DGST3_old, DGST4_old, DGST5_old, DGST6_old, DGST7_old; uint32_t DGST0_old, DGST1_old, DGST2_old, DGST3_old, DGST4_old, DGST5_old, DGST6_old, DGST7_old;
while (rmn > 0) { while (rmn > 0) {
CRPT->SHA_CTL = sha_ctl_start; CRPT->SHA_CTL = sha_ctl_start;
uint32_t data = nu_get32_be(in_pos); uint32_t data = nu_get32_be(in_pos);
if (rmn <= 4) { // Last word of a (in)complete block if (rmn <= 4) { // Last word of a (in)complete block
if (islast) { if (islast) {
@ -246,60 +244,57 @@ 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;
case SHA_MODE_SHA224: case SHA_MODE_SHA224:
DGST5_old = CRPT->SHA_DGST5; DGST5_old = CRPT->SHA_DGST5;
DGST6_old = CRPT->SHA_DGST6; DGST6_old = CRPT->SHA_DGST6;
case SHA_MODE_SHA1: case SHA_MODE_SHA1:
DGST0_old = CRPT->SHA_DGST0; DGST0_old = CRPT->SHA_DGST0;
DGST1_old = CRPT->SHA_DGST1; DGST1_old = CRPT->SHA_DGST1;
DGST2_old = CRPT->SHA_DGST2; DGST2_old = CRPT->SHA_DGST2;
DGST3_old = CRPT->SHA_DGST3; DGST3_old = CRPT->SHA_DGST3;
DGST4_old = CRPT->SHA_DGST4; DGST4_old = CRPT->SHA_DGST4;
} }
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));
CRPT->SHA_DATIN = data; CRPT->SHA_DATIN = data;
in_pos += 4; in_pos += 4;
rmn -= 4; rmn -= 4;
} }
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;
while (! isfinish) { while (! isfinish) {
switch (sha_opmode) { switch (sha_opmode) {
case SHA_MODE_SHA256: case SHA_MODE_SHA256:
if (DGST7_old != CRPT->SHA_DGST7) { if (DGST7_old != CRPT->SHA_DGST7) {
isfinish = 1; isfinish = 1;
break; break;
} }
case SHA_MODE_SHA224: case SHA_MODE_SHA224:
if (DGST5_old != CRPT->SHA_DGST5 || DGST6_old != CRPT->SHA_DGST6) { if (DGST5_old != CRPT->SHA_DGST5 || DGST6_old != CRPT->SHA_DGST6) {
isfinish = 1; isfinish = 1;
break; break;
} }
case SHA_MODE_SHA1: case SHA_MODE_SHA1:
if (DGST0_old != CRPT->SHA_DGST0 || DGST1_old != CRPT->SHA_DGST1 || DGST2_old != CRPT->SHA_DGST2 || if (DGST0_old != CRPT->SHA_DGST0 || DGST1_old != CRPT->SHA_DGST1 || DGST2_old != CRPT->SHA_DGST2 ||
DGST3_old != CRPT->SHA_DGST3 || DGST4_old != CRPT->SHA_DGST4) { DGST3_old != CRPT->SHA_DGST3 || DGST4_old != CRPT->SHA_DGST4) {
isfinish = 1; isfinish = 1;
break; break;
} }
} }
} }
} }
@ -310,7 +305,7 @@ void crypto_sha_getinternstate(unsigned char output[], size_t olen)
uint32_t *in_pos = (uint32_t *) &CRPT->SHA_DGST0; uint32_t *in_pos = (uint32_t *) &CRPT->SHA_DGST0;
unsigned char *out_pos = output; unsigned char *out_pos = output;
uint32_t rmn = olen; uint32_t rmn = olen;
while (rmn) { while (rmn) {
uint32_t val = *in_pos ++; uint32_t val = *in_pos ++;
nu_set32_be(out_pos, val); nu_set32_be(out_pos, val);

View File

@ -29,14 +29,13 @@ 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;
uint16_t blocksize; /*!< block size */ uint16_t blocksize; /*!< block size */
uint32_t blocksize_mask; /*!< block size mask */ uint32_t blocksize_mask; /*!< block size mask */
int is224; /*!< 0 => SHA-256, else SHA-224 */ int is224; /*!< 0 => SHA-256, else SHA-224 */
} }
crypto_sha_context; crypto_sha_context;
@ -51,7 +50,7 @@ void crypto_sha_getinternstate(unsigned char output[], size_t olen);
void mbedtls_sha1_hw_init( crypto_sha_context *ctx ); void mbedtls_sha1_hw_init( crypto_sha_context *ctx );
void mbedtls_sha1_hw_free( crypto_sha_context *ctx ); void mbedtls_sha1_hw_free( crypto_sha_context *ctx );
void mbedtls_sha1_hw_clone( crypto_sha_context *dst, void mbedtls_sha1_hw_clone( crypto_sha_context *dst,
const crypto_sha_context *src ); const crypto_sha_context *src );
void mbedtls_sha1_hw_starts( crypto_sha_context *ctx ); void mbedtls_sha1_hw_starts( crypto_sha_context *ctx );
void mbedtls_sha1_hw_update( crypto_sha_context *ctx, const unsigned char *input, size_t ilen ); void mbedtls_sha1_hw_update( crypto_sha_context *ctx, const unsigned char *input, size_t ilen );
void mbedtls_sha1_hw_finish( crypto_sha_context *ctx, unsigned char output[20] ); void mbedtls_sha1_hw_finish( crypto_sha_context *ctx, unsigned char output[20] );
@ -66,10 +65,10 @@ void mbedtls_sha1_hw_process( crypto_sha_context *ctx, const unsigned char data[
void mbedtls_sha256_hw_init( crypto_sha_context *ctx ); void mbedtls_sha256_hw_init( crypto_sha_context *ctx );
void mbedtls_sha256_hw_free( crypto_sha_context *ctx ); void mbedtls_sha256_hw_free( crypto_sha_context *ctx );
void mbedtls_sha256_hw_clone( crypto_sha_context *dst, void mbedtls_sha256_hw_clone( crypto_sha_context *dst,
const crypto_sha_context *src ); const crypto_sha_context *src );
void mbedtls_sha256_hw_starts( crypto_sha_context *ctx, int is224 ); void mbedtls_sha256_hw_starts( crypto_sha_context *ctx, int is224 );
void mbedtls_sha256_hw_update( crypto_sha_context *ctx, const unsigned char *input, void mbedtls_sha256_hw_update( crypto_sha_context *ctx, const unsigned char *input,
size_t ilen ); size_t ilen );
void mbedtls_sha256_hw_finish( crypto_sha_context *ctx, unsigned char output[32] ); void mbedtls_sha256_hw_finish( crypto_sha_context *ctx, unsigned char output[32] );
void mbedtls_sha256_hw_process( crypto_sha_context *ctx, const unsigned char data[64] ); void mbedtls_sha256_hw_process( crypto_sha_context *ctx, const unsigned char data[64] );