mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-20 21:33:47 -07:00
mbedtls: make style
This commit is contained in:
parent
6324e2e746
commit
b1d6eaf2f7
177 changed files with 37224 additions and 41821 deletions
|
@ -83,31 +83,30 @@
|
|||
* \param c The index of 'c' in the state.
|
||||
* \param d The index of 'd' in the state.
|
||||
*/
|
||||
static inline void chacha20_quarter_round( uint32_t state[16],
|
||||
size_t a,
|
||||
size_t b,
|
||||
size_t c,
|
||||
size_t d )
|
||||
{
|
||||
static inline void chacha20_quarter_round(uint32_t state[16],
|
||||
size_t a,
|
||||
size_t b,
|
||||
size_t c,
|
||||
size_t d) {
|
||||
/* a += b; d ^= a; d <<<= 16; */
|
||||
state[a] += state[b];
|
||||
state[d] ^= state[a];
|
||||
state[d] = ROTL32( state[d], 16 );
|
||||
state[d] = ROTL32(state[d], 16);
|
||||
|
||||
/* c += d; b ^= c; b <<<= 12 */
|
||||
state[c] += state[d];
|
||||
state[b] ^= state[c];
|
||||
state[b] = ROTL32( state[b], 12 );
|
||||
state[b] = ROTL32(state[b], 12);
|
||||
|
||||
/* a += b; d ^= a; d <<<= 8; */
|
||||
state[a] += state[b];
|
||||
state[d] ^= state[a];
|
||||
state[d] = ROTL32( state[d], 8 );
|
||||
state[d] = ROTL32(state[d], 8);
|
||||
|
||||
/* c += d; b ^= c; b <<<= 7; */
|
||||
state[c] += state[d];
|
||||
state[b] ^= state[c];
|
||||
state[b] = ROTL32( state[b], 7 );
|
||||
state[b] = ROTL32(state[b], 7);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -118,17 +117,16 @@ static inline void chacha20_quarter_round( uint32_t state[16],
|
|||
*
|
||||
* \param state The ChaCha20 state to update.
|
||||
*/
|
||||
static void chacha20_inner_block( uint32_t state[16] )
|
||||
{
|
||||
chacha20_quarter_round( state, 0, 4, 8, 12 );
|
||||
chacha20_quarter_round( state, 1, 5, 9, 13 );
|
||||
chacha20_quarter_round( state, 2, 6, 10, 14 );
|
||||
chacha20_quarter_round( state, 3, 7, 11, 15 );
|
||||
static void chacha20_inner_block(uint32_t state[16]) {
|
||||
chacha20_quarter_round(state, 0, 4, 8, 12);
|
||||
chacha20_quarter_round(state, 1, 5, 9, 13);
|
||||
chacha20_quarter_round(state, 2, 6, 10, 14);
|
||||
chacha20_quarter_round(state, 3, 7, 11, 15);
|
||||
|
||||
chacha20_quarter_round( state, 0, 5, 10, 15 );
|
||||
chacha20_quarter_round( state, 1, 6, 11, 12 );
|
||||
chacha20_quarter_round( state, 2, 7, 8, 13 );
|
||||
chacha20_quarter_round( state, 3, 4, 9, 14 );
|
||||
chacha20_quarter_round(state, 0, 5, 10, 15);
|
||||
chacha20_quarter_round(state, 1, 6, 11, 12);
|
||||
chacha20_quarter_round(state, 2, 7, 8, 13);
|
||||
chacha20_quarter_round(state, 3, 4, 9, 14);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -137,18 +135,17 @@ static void chacha20_inner_block( uint32_t state[16] )
|
|||
* \param initial_state The initial ChaCha20 state (key, nonce, counter).
|
||||
* \param keystream Generated keystream bytes are written to this buffer.
|
||||
*/
|
||||
static void chacha20_block( const uint32_t initial_state[16],
|
||||
unsigned char keystream[64] )
|
||||
{
|
||||
static void chacha20_block(const uint32_t initial_state[16],
|
||||
unsigned char keystream[64]) {
|
||||
uint32_t working_state[16];
|
||||
size_t i;
|
||||
|
||||
memcpy( working_state,
|
||||
initial_state,
|
||||
CHACHA20_BLOCK_SIZE_BYTES );
|
||||
memcpy(working_state,
|
||||
initial_state,
|
||||
CHACHA20_BLOCK_SIZE_BYTES);
|
||||
|
||||
for( i = 0U; i < 10U; i++ )
|
||||
chacha20_inner_block( working_state );
|
||||
for (i = 0U; i < 10U; i++)
|
||||
chacha20_inner_block(working_state);
|
||||
|
||||
working_state[ 0] += initial_state[ 0];
|
||||
working_state[ 1] += initial_state[ 1];
|
||||
|
@ -167,43 +164,38 @@ static void chacha20_block( const uint32_t initial_state[16],
|
|||
working_state[14] += initial_state[14];
|
||||
working_state[15] += initial_state[15];
|
||||
|
||||
for( i = 0U; i < 16; i++ )
|
||||
{
|
||||
for (i = 0U; i < 16; i++) {
|
||||
size_t offset = i * 4U;
|
||||
|
||||
keystream[offset ] = (unsigned char)( working_state[i] );
|
||||
keystream[offset + 1U] = (unsigned char)( working_state[i] >> 8 );
|
||||
keystream[offset + 2U] = (unsigned char)( working_state[i] >> 16 );
|
||||
keystream[offset + 3U] = (unsigned char)( working_state[i] >> 24 );
|
||||
keystream[offset ] = (unsigned char)(working_state[i]);
|
||||
keystream[offset + 1U] = (unsigned char)(working_state[i] >> 8);
|
||||
keystream[offset + 2U] = (unsigned char)(working_state[i] >> 16);
|
||||
keystream[offset + 3U] = (unsigned char)(working_state[i] >> 24);
|
||||
}
|
||||
|
||||
mbedtls_platform_zeroize( working_state, sizeof( working_state ) );
|
||||
mbedtls_platform_zeroize(working_state, sizeof(working_state));
|
||||
}
|
||||
|
||||
void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx )
|
||||
{
|
||||
CHACHA20_VALIDATE( ctx != NULL );
|
||||
void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx) {
|
||||
CHACHA20_VALIDATE(ctx != NULL);
|
||||
|
||||
mbedtls_platform_zeroize( ctx->state, sizeof( ctx->state ) );
|
||||
mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) );
|
||||
mbedtls_platform_zeroize(ctx->state, sizeof(ctx->state));
|
||||
mbedtls_platform_zeroize(ctx->keystream8, sizeof(ctx->keystream8));
|
||||
|
||||
/* Initially, there's no keystream bytes available */
|
||||
ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES;
|
||||
}
|
||||
|
||||
void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx )
|
||||
{
|
||||
if( ctx != NULL )
|
||||
{
|
||||
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_chacha20_context ) );
|
||||
void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx) {
|
||||
if (ctx != NULL) {
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_chacha20_context));
|
||||
}
|
||||
}
|
||||
|
||||
int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
|
||||
const unsigned char key[32] )
|
||||
{
|
||||
CHACHA20_VALIDATE_RET( ctx != NULL );
|
||||
CHACHA20_VALIDATE_RET( key != NULL );
|
||||
int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
|
||||
const unsigned char key[32]) {
|
||||
CHACHA20_VALIDATE_RET(ctx != NULL);
|
||||
CHACHA20_VALIDATE_RET(key != NULL);
|
||||
|
||||
/* ChaCha20 constants - the string "expand 32-byte k" */
|
||||
ctx->state[0] = 0x61707865;
|
||||
|
@ -212,58 +204,55 @@ int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
|
|||
ctx->state[3] = 0x6b206574;
|
||||
|
||||
/* Set key */
|
||||
ctx->state[4] = BYTES_TO_U32_LE( key, 0 );
|
||||
ctx->state[5] = BYTES_TO_U32_LE( key, 4 );
|
||||
ctx->state[6] = BYTES_TO_U32_LE( key, 8 );
|
||||
ctx->state[7] = BYTES_TO_U32_LE( key, 12 );
|
||||
ctx->state[8] = BYTES_TO_U32_LE( key, 16 );
|
||||
ctx->state[9] = BYTES_TO_U32_LE( key, 20 );
|
||||
ctx->state[10] = BYTES_TO_U32_LE( key, 24 );
|
||||
ctx->state[11] = BYTES_TO_U32_LE( key, 28 );
|
||||
ctx->state[4] = BYTES_TO_U32_LE(key, 0);
|
||||
ctx->state[5] = BYTES_TO_U32_LE(key, 4);
|
||||
ctx->state[6] = BYTES_TO_U32_LE(key, 8);
|
||||
ctx->state[7] = BYTES_TO_U32_LE(key, 12);
|
||||
ctx->state[8] = BYTES_TO_U32_LE(key, 16);
|
||||
ctx->state[9] = BYTES_TO_U32_LE(key, 20);
|
||||
ctx->state[10] = BYTES_TO_U32_LE(key, 24);
|
||||
ctx->state[11] = BYTES_TO_U32_LE(key, 28);
|
||||
|
||||
return( 0 );
|
||||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
|
||||
const unsigned char nonce[12],
|
||||
uint32_t counter )
|
||||
{
|
||||
CHACHA20_VALIDATE_RET( ctx != NULL );
|
||||
CHACHA20_VALIDATE_RET( nonce != NULL );
|
||||
int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
|
||||
const unsigned char nonce[12],
|
||||
uint32_t counter) {
|
||||
CHACHA20_VALIDATE_RET(ctx != NULL);
|
||||
CHACHA20_VALIDATE_RET(nonce != NULL);
|
||||
|
||||
/* Counter */
|
||||
ctx->state[12] = counter;
|
||||
|
||||
/* Nonce */
|
||||
ctx->state[13] = BYTES_TO_U32_LE( nonce, 0 );
|
||||
ctx->state[14] = BYTES_TO_U32_LE( nonce, 4 );
|
||||
ctx->state[15] = BYTES_TO_U32_LE( nonce, 8 );
|
||||
ctx->state[13] = BYTES_TO_U32_LE(nonce, 0);
|
||||
ctx->state[14] = BYTES_TO_U32_LE(nonce, 4);
|
||||
ctx->state[15] = BYTES_TO_U32_LE(nonce, 8);
|
||||
|
||||
mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) );
|
||||
mbedtls_platform_zeroize(ctx->keystream8, sizeof(ctx->keystream8));
|
||||
|
||||
/* Initially, there's no keystream bytes available */
|
||||
ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES;
|
||||
|
||||
return( 0 );
|
||||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
|
||||
size_t size,
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
|
||||
size_t size,
|
||||
const unsigned char *input,
|
||||
unsigned char *output) {
|
||||
size_t offset = 0U;
|
||||
size_t i;
|
||||
|
||||
CHACHA20_VALIDATE_RET( ctx != NULL );
|
||||
CHACHA20_VALIDATE_RET( size == 0 || input != NULL );
|
||||
CHACHA20_VALIDATE_RET( size == 0 || output != NULL );
|
||||
CHACHA20_VALIDATE_RET(ctx != NULL);
|
||||
CHACHA20_VALIDATE_RET(size == 0 || input != NULL);
|
||||
CHACHA20_VALIDATE_RET(size == 0 || output != NULL);
|
||||
|
||||
/* Use leftover keystream bytes, if available */
|
||||
while( size > 0U && ctx->keystream_bytes_used < CHACHA20_BLOCK_SIZE_BYTES )
|
||||
{
|
||||
while (size > 0U && ctx->keystream_bytes_used < CHACHA20_BLOCK_SIZE_BYTES) {
|
||||
output[offset] = input[offset]
|
||||
^ ctx->keystream8[ctx->keystream_bytes_used];
|
||||
^ ctx->keystream8[ctx->keystream_bytes_used];
|
||||
|
||||
ctx->keystream_bytes_used++;
|
||||
offset++;
|
||||
|
@ -271,22 +260,20 @@ int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
|
|||
}
|
||||
|
||||
/* Process full blocks */
|
||||
while( size >= CHACHA20_BLOCK_SIZE_BYTES )
|
||||
{
|
||||
while (size >= CHACHA20_BLOCK_SIZE_BYTES) {
|
||||
/* Generate new keystream block and increment counter */
|
||||
chacha20_block( ctx->state, ctx->keystream8 );
|
||||
chacha20_block(ctx->state, ctx->keystream8);
|
||||
ctx->state[CHACHA20_CTR_INDEX]++;
|
||||
|
||||
for( i = 0U; i < 64U; i += 8U )
|
||||
{
|
||||
for (i = 0U; i < 64U; i += 8U) {
|
||||
output[offset + i ] = input[offset + i ] ^ ctx->keystream8[i ];
|
||||
output[offset + i+1] = input[offset + i+1] ^ ctx->keystream8[i+1];
|
||||
output[offset + i+2] = input[offset + i+2] ^ ctx->keystream8[i+2];
|
||||
output[offset + i+3] = input[offset + i+3] ^ ctx->keystream8[i+3];
|
||||
output[offset + i+4] = input[offset + i+4] ^ ctx->keystream8[i+4];
|
||||
output[offset + i+5] = input[offset + i+5] ^ ctx->keystream8[i+5];
|
||||
output[offset + i+6] = input[offset + i+6] ^ ctx->keystream8[i+6];
|
||||
output[offset + i+7] = input[offset + i+7] ^ ctx->keystream8[i+7];
|
||||
output[offset + i + 1] = input[offset + i + 1] ^ ctx->keystream8[i + 1];
|
||||
output[offset + i + 2] = input[offset + i + 2] ^ ctx->keystream8[i + 2];
|
||||
output[offset + i + 3] = input[offset + i + 3] ^ ctx->keystream8[i + 3];
|
||||
output[offset + i + 4] = input[offset + i + 4] ^ ctx->keystream8[i + 4];
|
||||
output[offset + i + 5] = input[offset + i + 5] ^ ctx->keystream8[i + 5];
|
||||
output[offset + i + 6] = input[offset + i + 6] ^ ctx->keystream8[i + 6];
|
||||
output[offset + i + 7] = input[offset + i + 7] ^ ctx->keystream8[i + 7];
|
||||
}
|
||||
|
||||
offset += CHACHA20_BLOCK_SIZE_BYTES;
|
||||
|
@ -294,14 +281,12 @@ int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
|
|||
}
|
||||
|
||||
/* Last (partial) block */
|
||||
if( size > 0U )
|
||||
{
|
||||
if (size > 0U) {
|
||||
/* Generate new keystream block and increment counter */
|
||||
chacha20_block( ctx->state, ctx->keystream8 );
|
||||
chacha20_block(ctx->state, ctx->keystream8);
|
||||
ctx->state[CHACHA20_CTR_INDEX]++;
|
||||
|
||||
for( i = 0U; i < size; i++)
|
||||
{
|
||||
for (i = 0U; i < size; i++) {
|
||||
output[offset + i] = input[offset + i] ^ ctx->keystream8[i];
|
||||
}
|
||||
|
||||
|
@ -309,47 +294,45 @@ int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
|
|||
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_chacha20_crypt( const unsigned char key[32],
|
||||
const unsigned char nonce[12],
|
||||
uint32_t counter,
|
||||
size_t data_len,
|
||||
const unsigned char* input,
|
||||
unsigned char* output )
|
||||
{
|
||||
int mbedtls_chacha20_crypt(const unsigned char key[32],
|
||||
const unsigned char nonce[12],
|
||||
uint32_t counter,
|
||||
size_t data_len,
|
||||
const unsigned char *input,
|
||||
unsigned char *output) {
|
||||
mbedtls_chacha20_context ctx;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
CHACHA20_VALIDATE_RET( key != NULL );
|
||||
CHACHA20_VALIDATE_RET( nonce != NULL );
|
||||
CHACHA20_VALIDATE_RET( data_len == 0 || input != NULL );
|
||||
CHACHA20_VALIDATE_RET( data_len == 0 || output != NULL );
|
||||
CHACHA20_VALIDATE_RET(key != NULL);
|
||||
CHACHA20_VALIDATE_RET(nonce != NULL);
|
||||
CHACHA20_VALIDATE_RET(data_len == 0 || input != NULL);
|
||||
CHACHA20_VALIDATE_RET(data_len == 0 || output != NULL);
|
||||
|
||||
mbedtls_chacha20_init( &ctx );
|
||||
mbedtls_chacha20_init(&ctx);
|
||||
|
||||
ret = mbedtls_chacha20_setkey( &ctx, key );
|
||||
if( ret != 0 )
|
||||
ret = mbedtls_chacha20_setkey(&ctx, key);
|
||||
if (ret != 0)
|
||||
goto cleanup;
|
||||
|
||||
ret = mbedtls_chacha20_starts( &ctx, nonce, counter );
|
||||
if( ret != 0 )
|
||||
ret = mbedtls_chacha20_starts(&ctx, nonce, counter);
|
||||
if (ret != 0)
|
||||
goto cleanup;
|
||||
|
||||
ret = mbedtls_chacha20_update( &ctx, data_len, input, output );
|
||||
ret = mbedtls_chacha20_update(&ctx, data_len, input, output);
|
||||
|
||||
cleanup:
|
||||
mbedtls_chacha20_free( &ctx );
|
||||
return( ret );
|
||||
mbedtls_chacha20_free(&ctx);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#endif /* !MBEDTLS_CHACHA20_ALT */
|
||||
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
|
||||
static const unsigned char test_keys[2][32] =
|
||||
{
|
||||
static const unsigned char test_keys[2][32] = {
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
@ -364,8 +347,7 @@ static const unsigned char test_keys[2][32] =
|
|||
}
|
||||
};
|
||||
|
||||
static const unsigned char test_nonces[2][12] =
|
||||
{
|
||||
static const unsigned char test_nonces[2][12] = {
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
|
@ -376,14 +358,12 @@ static const unsigned char test_nonces[2][12] =
|
|||
}
|
||||
};
|
||||
|
||||
static const uint32_t test_counters[2] =
|
||||
{
|
||||
static const uint32_t test_counters[2] = {
|
||||
0U,
|
||||
1U
|
||||
};
|
||||
|
||||
static const unsigned char test_input[2][375] =
|
||||
{
|
||||
static const unsigned char test_input[2][375] = {
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
@ -445,8 +425,7 @@ static const unsigned char test_input[2][375] =
|
|||
}
|
||||
};
|
||||
|
||||
static const unsigned char test_output[2][375] =
|
||||
{
|
||||
static const unsigned char test_output[2][375] = {
|
||||
{
|
||||
0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
|
||||
0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
|
||||
|
@ -508,8 +487,7 @@ static const unsigned char test_output[2][375] =
|
|||
}
|
||||
};
|
||||
|
||||
static const size_t test_lengths[2] =
|
||||
{
|
||||
static const size_t test_lengths[2] = {
|
||||
64U,
|
||||
375U
|
||||
};
|
||||
|
@ -530,37 +508,35 @@ static const size_t test_lengths[2] =
|
|||
} \
|
||||
while( 0 )
|
||||
|
||||
int mbedtls_chacha20_self_test( int verbose )
|
||||
{
|
||||
int mbedtls_chacha20_self_test(int verbose) {
|
||||
unsigned char output[381];
|
||||
unsigned i;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
for( i = 0U; i < 2U; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( " ChaCha20 test %u ", i );
|
||||
for (i = 0U; i < 2U; i++) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf(" ChaCha20 test %u ", i);
|
||||
|
||||
ret = mbedtls_chacha20_crypt( test_keys[i],
|
||||
test_nonces[i],
|
||||
test_counters[i],
|
||||
test_lengths[i],
|
||||
test_input[i],
|
||||
output );
|
||||
ret = mbedtls_chacha20_crypt(test_keys[i],
|
||||
test_nonces[i],
|
||||
test_counters[i],
|
||||
test_lengths[i],
|
||||
test_input[i],
|
||||
output);
|
||||
|
||||
ASSERT( 0 == ret, ( "error code: %i\n", ret ) );
|
||||
ASSERT(0 == ret, ("error code: %i\n", ret));
|
||||
|
||||
ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ),
|
||||
( "failed (output)\n" ) );
|
||||
ASSERT(0 == memcmp(output, test_output[i], test_lengths[i]),
|
||||
("failed (output)\n"));
|
||||
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "passed\n" );
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("passed\n");
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "\n" );
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("\n");
|
||||
|
||||
return( 0 );
|
||||
return (0);
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue