Ran astyle on features/mbedtls/targets/TARGET_Samsung.

pull/12106/head
Andrew Chong 2020-01-06 14:47:07 +08:00
parent 26f7e26568
commit d242850de4
4 changed files with 442 additions and 425 deletions

View File

@ -112,13 +112,10 @@ int mbedtls_sha256_starts_ret(mbedtls_sha256_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) {
{
ctx->is224 = 0; ctx->is224 = 0;
memset(ctx, 0, sizeof(mbedtls_sha256_context)); memset(ctx, 0, sizeof(mbedtls_sha256_context));
} } else {
else
{
/* SHA-224 */ /* SHA-224 */
ctx->state[0] = 0xC1059ED8; ctx->state[0] = 0xC1059ED8;
ctx->state[1] = 0x367CD507; ctx->state[1] = 0x367CD507;
@ -142,8 +139,7 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *
{ {
if (ctx->is224) { if (ctx->is224) {
mbedtls_sha256_sw_update_ret(ctx, input, ilen); mbedtls_sha256_sw_update_ret(ctx, input, ilen);
} } else {
else {
if (ilen > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) { if (ilen > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
// H/W SHA has limitation to seperated API with oversized message. // H/W SHA has limitation to seperated API with oversized message.
// fall back to S/W SHA-256 // fall back to S/W SHA-256
@ -178,9 +174,9 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *
*/ */
int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32]) int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32])
{ {
if(ctx->is224 || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) if (ctx->is224 || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
mbedtls_sha256_sw_finish_ret(ctx, output); mbedtls_sha256_sw_finish_ret(ctx, output);
else { } else {
int ret = FAIL; int ret = FAIL;
unsigned int object_id; unsigned int object_id;
unsigned int block_byte_len; unsigned int block_byte_len;
@ -227,8 +223,7 @@ int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[
return 0; return 0;
} }
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,
@ -283,28 +278,36 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
SHA256_VALIDATE_RET(ctx != NULL); SHA256_VALIDATE_RET(ctx != NULL);
SHA256_VALIDATE_RET((const unsigned char *)data != NULL); SHA256_VALIDATE_RET((const unsigned char *)data != NULL);
for( i = 0; i < 8; i++ ) for (i = 0; i < 8; i++) {
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 {
R(i); R(i);
}
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]);
@ -315,8 +318,7 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
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]);
@ -328,8 +330,9 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
} }
#endif /* MBEDTLS_SHA256_SMALLER */ #endif /* MBEDTLS_SHA256_SMALLER */
for( i = 0; i < 8; i++ ) for (i = 0; i < 8; i++) {
ctx->state[i] += A[i]; ctx->state[i] += A[i];
}
return 0; return 0;
} }
@ -351,18 +354,16 @@ int mbedtls_sha256_sw_finish_ret(mbedtls_sha256_context *ctx, unsigned char outp
ctx->buffer[used++] = 0x80; ctx->buffer[used++] = 0x80;
if( used <= 56 ) if (used <= 56) {
{
/* Enough room for padding + length in current block */ /* Enough room for padding + length in current block */
memset(ctx->buffer + used, 0, 56 - used); memset(ctx->buffer + used, 0, 56 - used);
} } else {
else
{
/* We'll need an extra block */ /* We'll need an extra block */
memset(ctx->buffer + used, 0, 64 - used); memset(ctx->buffer + used, 0, 64 - used);
if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
return (ret); return (ret);
}
memset(ctx->buffer, 0, 56); memset(ctx->buffer, 0, 56);
} }
@ -377,8 +378,9 @@ int mbedtls_sha256_sw_finish_ret(mbedtls_sha256_context *ctx, unsigned char outp
PUT_UINT32_BE(high, ctx->buffer, 56); PUT_UINT32_BE(high, ctx->buffer, 56);
PUT_UINT32_BE(low, ctx->buffer, 60); PUT_UINT32_BE(low, ctx->buffer, 60);
if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
return (ret); return (ret);
}
/* /*
* Output final state * Output final state
@ -391,8 +393,9 @@ int mbedtls_sha256_sw_finish_ret(mbedtls_sha256_context *ctx, unsigned char outp
PUT_UINT32_BE(ctx->state[5], output, 20); PUT_UINT32_BE(ctx->state[5], output, 20);
PUT_UINT32_BE(ctx->state[6], output, 24); PUT_UINT32_BE(ctx->state[6], output, 24);
if( ctx->is224 == 0 ) if (ctx->is224 == 0) {
PUT_UINT32_BE(ctx->state[7], output, 28); PUT_UINT32_BE(ctx->state[7], output, 28);
}
return 0; return 0;
} }
@ -406,8 +409,9 @@ int mbedtls_sha256_sw_update_ret(mbedtls_sha256_context *ctx, const unsigned cha
SHA256_VALIDATE_RET(ctx != NULL); SHA256_VALIDATE_RET(ctx != NULL);
SHA256_VALIDATE_RET(ilen == 0 || input != NULL); SHA256_VALIDATE_RET(ilen == 0 || input != NULL);
if( ilen == 0 ) if (ilen == 0) {
return (0); return (0);
}
left = ctx->total[0] & 0x3F; left = ctx->total[0] & 0x3F;
fill = 64 - left; fill = 64 - left;
@ -415,32 +419,34 @@ int mbedtls_sha256_sw_update_ret(mbedtls_sha256_context *ctx, const unsigned cha
ctx->total[0] += (uint32_t) ilen; ctx->total[0] += (uint32_t) ilen;
ctx->total[0] &= 0xFFFFFFFF; ctx->total[0] &= 0xFFFFFFFF;
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);
if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
return (ret); return (ret);
}
input += fill; input += fill;
ilen -= fill; ilen -= fill;
left = 0; left = 0;
} }
while( ilen >= 64 ) while (ilen >= 64) {
{ if ((ret = mbedtls_internal_sha256_process(ctx, input)) != 0) {
if( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 )
return (ret); return (ret);
}
input += 64; input += 64;
ilen -= 64; ilen -= 64;
} }
if( ilen > 0 ) if (ilen > 0) {
memcpy((void *)(ctx->buffer + left), input, ilen); memcpy((void *)(ctx->buffer + left), input, ilen);
}
return 0; return 0;
} }

View File

@ -107,8 +107,9 @@ void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
void mbedtls_sha512_free(mbedtls_sha512_context *ctx) void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
{ {
if( ctx == NULL ) if (ctx == NULL) {
return; return;
}
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha512_context)); mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha512_context));
} }
@ -207,10 +208,11 @@ int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx, unsigned char output[
} }
//! assign hash_byte_len to compare returned result from sss_fw after hash operation //! assign hash_byte_len to compare returned result from sss_fw after hash operation
if( ctx->is384 == 0 ) if (ctx->is384 == 0) {
object_id = OID_SHA2_512; object_id = OID_SHA2_512;
else } else {
object_id = OID_SHA2_384; object_id = OID_SHA2_384;
}
block_byte_len = 64; block_byte_len = 64;
@ -246,8 +248,7 @@ int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx, unsigned char output[
/* /*
* Round constants * Round constants
*/ */
static const uint64_t K[80] = static const uint64_t K[80] = {
{
UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD), UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD),
UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC), UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC),
UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019), UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019),
@ -320,56 +321,65 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
(d) += temp1; (h) = temp1 + temp2; \ (d) += temp1; (h) = temp1 + temp2; \
} while( 0 ) } while( 0 )
for( i = 0; i < 8; i++ ) for (i = 0; i < 8; i++) {
A[i] = ctx->state[i]; A[i] = ctx->state[i];
}
#if defined(MBEDTLS_SHA512_SMALLER) #if defined(MBEDTLS_SHA512_SMALLER)
for( i = 0; i < 80; i++ ) for (i = 0; i < 80; i++) {
{ if (i < 16) {
if( i < 16 )
{
GET_UINT64_BE(W[i], data, i << 3); GET_UINT64_BE(W[i], data, i << 3);
} } else {
else
{
W[i] = S1(W[i - 2]) + W[i - 7] + W[i] = S1(W[i - 2]) + W[i - 7] +
S0(W[i - 15]) + W[i - 16]; S0(W[i - 15]) + W[i - 16];
} }
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_SHA512_SMALLER */ #else /* MBEDTLS_SHA512_SMALLER */
for( i = 0; i < 16; i++ ) for (i = 0; i < 16; i++) {
{
GET_UINT64_BE(W[i], data, i << 3); GET_UINT64_BE(W[i], data, i << 3);
} }
for( ; i < 80; i++ ) for (; i < 80; i++) {
{
W[i] = S1(W[i - 2]) + W[i - 7] + W[i] = S1(W[i - 2]) + W[i - 7] +
S0(W[i - 15]) + W[i - 16]; S0(W[i - 15]) + W[i - 16];
} }
i = 0; i = 0;
do do {
{ 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] ); i++; i++;
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i], K[i] ); i++; P(A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i], K[i]);
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i], K[i] ); i++; i++;
P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i], K[i] ); i++; P(A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i], K[i]);
P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i], K[i] ); i++; i++;
P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i], K[i] ); i++; P(A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i], K[i]);
P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i], K[i] ); i++; i++;
P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i], K[i] ); i++; P(A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i], K[i]);
} i++;
while( i < 80 ); P(A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i], K[i]);
i++;
P(A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i], K[i]);
i++;
P(A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i], K[i]);
i++;
} while (i < 80);
#endif /* MBEDTLS_SHA512_SMALLER */ #endif /* MBEDTLS_SHA512_SMALLER */
for( i = 0; i < 8; i++ ) for (i = 0; i < 8; i++) {
ctx->state[i] += A[i]; ctx->state[i] += A[i];
}
return 0; return 0;
} }
@ -391,18 +401,16 @@ int mbedtls_sha512_sw_finish_ret( mbedtls_sha512_context *ctx,
ctx->buffer[used++] = 0x80; ctx->buffer[used++] = 0x80;
if( used <= 112 ) if (used <= 112) {
{
/* Enough room for padding + length in current block */ /* Enough room for padding + length in current block */
memset(ctx->buffer + used, 0, 112 - used); memset(ctx->buffer + used, 0, 112 - used);
} } else {
else
{
/* We'll need an extra block */ /* We'll need an extra block */
memset(ctx->buffer + used, 0, 128 - used); memset(ctx->buffer + used, 0, 128 - used);
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
return (ret); return (ret);
}
memset(ctx->buffer, 0, 112); memset(ctx->buffer, 0, 112);
} }
@ -417,8 +425,9 @@ int mbedtls_sha512_sw_finish_ret( mbedtls_sha512_context *ctx,
sha512_put_uint64_be(high, ctx->buffer, 112); sha512_put_uint64_be(high, ctx->buffer, 112);
sha512_put_uint64_be(low, ctx->buffer, 120); sha512_put_uint64_be(low, ctx->buffer, 120);
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
return (ret); return (ret);
}
/* /*
* Output final state * Output final state
@ -430,8 +439,7 @@ int mbedtls_sha512_sw_finish_ret( mbedtls_sha512_context *ctx,
sha512_put_uint64_be(ctx->state[4], output, 32); sha512_put_uint64_be(ctx->state[4], output, 32);
sha512_put_uint64_be(ctx->state[5], output, 40); sha512_put_uint64_be(ctx->state[5], output, 40);
if( ctx->is384 == 0 ) if (ctx->is384 == 0) {
{
sha512_put_uint64_be(ctx->state[6], output, 48); sha512_put_uint64_be(ctx->state[6], output, 48);
sha512_put_uint64_be(ctx->state[7], output, 56); sha512_put_uint64_be(ctx->state[7], output, 56);
} }
@ -448,40 +456,43 @@ int mbedtls_sha512_sw_update_ret(mbedtls_sha512_context *ctx, const unsigned cha
SHA512_VALIDATE_RET(ctx != NULL); SHA512_VALIDATE_RET(ctx != NULL);
SHA512_VALIDATE_RET(ilen == 0 || input != NULL); SHA512_VALIDATE_RET(ilen == 0 || input != NULL);
if( ilen == 0 ) if (ilen == 0) {
return (0); return (0);
}
left = (unsigned int)(ctx->total[0] & 0x7F); left = (unsigned int)(ctx->total[0] & 0x7F);
fill = 128 - left; fill = 128 - left;
ctx->total[0] += (uint64_t) ilen; ctx->total[0] += (uint64_t) ilen;
if( ctx->total[0] < (uint64_t) ilen ) if (ctx->total[0] < (uint64_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);
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
return (ret); return (ret);
}
input += fill; input += fill;
ilen -= fill; ilen -= fill;
left = 0; left = 0;
} }
while( ilen >= 128 ) while (ilen >= 128) {
{ if ((ret = mbedtls_internal_sha512_process(ctx, input)) != 0) {
if( ( ret = mbedtls_internal_sha512_process( ctx, input ) ) != 0 )
return (ret); return (ret);
}
input += 128; input += 128;
ilen -= 128; ilen -= 128;
} }
if( ilen > 0 ) if (ilen > 0) {
memcpy((void *)(ctx->buffer + left), input, ilen); memcpy((void *)(ctx->buffer + left), input, ilen);
}
return 0; return 0;
} }