mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Switch to the new code style
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
415
library/sha512.c
415
library/sha512.c
@ -41,13 +41,13 @@
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#define SHA512_VALIDATE_RET(cond) \
|
||||
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA512_BAD_INPUT_DATA )
|
||||
#define SHA512_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
|
||||
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_SHA512_BAD_INPUT_DATA)
|
||||
#define SHA512_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond)
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_ALT)
|
||||
|
||||
#if defined(MBEDTLS_SHA512_SMALLER)
|
||||
static void sha512_put_uint64_be( uint64_t n, unsigned char *b, uint8_t i )
|
||||
static void sha512_put_uint64_be(uint64_t n, unsigned char *b, uint8_t i)
|
||||
{
|
||||
MBEDTLS_PUT_UINT64_BE(n, b, i);
|
||||
}
|
||||
@ -55,26 +55,27 @@ static void sha512_put_uint64_be( uint64_t n, unsigned char *b, uint8_t i )
|
||||
#define sha512_put_uint64_be MBEDTLS_PUT_UINT64_BE
|
||||
#endif /* MBEDTLS_SHA512_SMALLER */
|
||||
|
||||
void mbedtls_sha512_init( mbedtls_sha512_context *ctx )
|
||||
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
|
||||
{
|
||||
SHA512_VALIDATE( ctx != NULL );
|
||||
SHA512_VALIDATE(ctx != NULL);
|
||||
|
||||
memset( ctx, 0, sizeof( mbedtls_sha512_context ) );
|
||||
memset(ctx, 0, sizeof(mbedtls_sha512_context));
|
||||
}
|
||||
|
||||
void mbedtls_sha512_free( mbedtls_sha512_context *ctx )
|
||||
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
|
||||
{
|
||||
if( ctx == NULL )
|
||||
if (ctx == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha512_context));
|
||||
}
|
||||
|
||||
void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
|
||||
const mbedtls_sha512_context *src )
|
||||
void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
|
||||
const mbedtls_sha512_context *src)
|
||||
{
|
||||
SHA512_VALIDATE( dst != NULL );
|
||||
SHA512_VALIDATE( src != NULL );
|
||||
SHA512_VALIDATE(dst != NULL);
|
||||
SHA512_VALIDATE(src != NULL);
|
||||
|
||||
*dst = *src;
|
||||
}
|
||||
@ -82,20 +83,19 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
|
||||
/*
|
||||
* SHA-512 context setup
|
||||
*/
|
||||
int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
|
||||
{
|
||||
SHA512_VALIDATE_RET( ctx != NULL );
|
||||
SHA512_VALIDATE_RET(ctx != NULL);
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 );
|
||||
SHA512_VALIDATE_RET(is384 == 0 || is384 == 1);
|
||||
#else
|
||||
SHA512_VALIDATE_RET( is384 == 0 );
|
||||
SHA512_VALIDATE_RET(is384 == 0);
|
||||
#endif
|
||||
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
if( is384 == 0 )
|
||||
{
|
||||
if (is384 == 0) {
|
||||
/* SHA-512 */
|
||||
ctx->state[0] = UL64(0x6A09E667F3BCC908);
|
||||
ctx->state[1] = UL64(0xBB67AE8584CAA73B);
|
||||
@ -105,11 +105,9 @@ int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
ctx->state[5] = UL64(0x9B05688C2B3E6C1F);
|
||||
ctx->state[6] = UL64(0x1F83D9ABFB41BD6B);
|
||||
ctx->state[7] = UL64(0x5BE0CD19137E2179);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
#if defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
return( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA );
|
||||
return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
|
||||
#else
|
||||
/* SHA-384 */
|
||||
ctx->state[0] = UL64(0xCBBB9D5DC1059ED8);
|
||||
@ -127,14 +125,14 @@ int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
ctx->is384 = is384;
|
||||
#endif
|
||||
|
||||
return( 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
|
||||
int is384 )
|
||||
void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
|
||||
int is384)
|
||||
{
|
||||
mbedtls_sha512_starts_ret( ctx, is384 );
|
||||
mbedtls_sha512_starts_ret(ctx, is384);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -187,57 +185,53 @@ static const uint64_t K[80] =
|
||||
UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
|
||||
};
|
||||
|
||||
int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128] )
|
||||
int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128])
|
||||
{
|
||||
int i;
|
||||
struct
|
||||
{
|
||||
struct {
|
||||
uint64_t temp1, temp2, W[80];
|
||||
uint64_t A[8];
|
||||
} local;
|
||||
|
||||
SHA512_VALIDATE_RET( ctx != NULL );
|
||||
SHA512_VALIDATE_RET( (const unsigned char *)data != NULL );
|
||||
SHA512_VALIDATE_RET(ctx != NULL);
|
||||
SHA512_VALIDATE_RET((const unsigned char *) data != NULL);
|
||||
|
||||
#define SHR(x,n) ((x) >> (n))
|
||||
#define ROTR(x,n) (SHR((x),(n)) | ((x) << (64 - (n))))
|
||||
#define SHR(x, n) ((x) >> (n))
|
||||
#define ROTR(x, n) (SHR((x), (n)) | ((x) << (64 - (n))))
|
||||
|
||||
#define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
|
||||
#define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
|
||||
#define S1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
|
||||
|
||||
#define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
|
||||
#define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
|
||||
#define S2(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
|
||||
#define S3(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
|
||||
|
||||
#define F0(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
|
||||
#define F1(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
#define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
|
||||
#define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
|
||||
#define P(a,b,c,d,e,f,g,h,x,K) \
|
||||
#define P(a, b, c, d, e, f, g, h, x, K) \
|
||||
do \
|
||||
{ \
|
||||
local.temp1 = (h) + S3(e) + F1((e),(f),(g)) + (K) + (x); \
|
||||
local.temp2 = S2(a) + F0((a),(b),(c)); \
|
||||
local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
|
||||
local.temp2 = S2(a) + F0((a), (b), (c)); \
|
||||
(d) += local.temp1; (h) = local.temp1 + local.temp2; \
|
||||
} while( 0 )
|
||||
} while (0)
|
||||
|
||||
for( i = 0; i < 8; i++ )
|
||||
for (i = 0; i < 8; i++) {
|
||||
local.A[i] = ctx->state[i];
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SHA512_SMALLER)
|
||||
for( i = 0; i < 80; i++ )
|
||||
{
|
||||
if( i < 16 )
|
||||
{
|
||||
local.W[i] = MBEDTLS_GET_UINT64_BE( data, i << 3 );
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < 80; i++) {
|
||||
if (i < 16) {
|
||||
local.W[i] = MBEDTLS_GET_UINT64_BE(data, i << 3);
|
||||
} else {
|
||||
local.W[i] = S1(local.W[i - 2]) + local.W[i - 7] +
|
||||
S0(local.W[i - 15]) + local.W[i - 16];
|
||||
S0(local.W[i - 15]) + local.W[i - 16];
|
||||
}
|
||||
|
||||
P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
|
||||
local.A[5], local.A[6], local.A[7], local.W[i], K[i] );
|
||||
P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
|
||||
local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
|
||||
|
||||
local.temp1 = local.A[7]; local.A[7] = local.A[6];
|
||||
local.A[6] = local.A[5]; local.A[5] = local.A[4];
|
||||
@ -246,54 +240,51 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
|
||||
local.A[0] = local.temp1;
|
||||
}
|
||||
#else /* MBEDTLS_SHA512_SMALLER */
|
||||
for( i = 0; i < 16; i++ )
|
||||
{
|
||||
local.W[i] = MBEDTLS_GET_UINT64_BE( data, i << 3 );
|
||||
for (i = 0; i < 16; i++) {
|
||||
local.W[i] = MBEDTLS_GET_UINT64_BE(data, i << 3);
|
||||
}
|
||||
|
||||
for( ; i < 80; i++ )
|
||||
{
|
||||
for (; i < 80; i++) {
|
||||
local.W[i] = S1(local.W[i - 2]) + local.W[i - 7] +
|
||||
S0(local.W[i - 15]) + local.W[i - 16];
|
||||
S0(local.W[i - 15]) + local.W[i - 16];
|
||||
}
|
||||
|
||||
i = 0;
|
||||
do
|
||||
{
|
||||
P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
|
||||
local.A[5], local.A[6], local.A[7], local.W[i], K[i] ); i++;
|
||||
P( local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
|
||||
local.A[4], local.A[5], local.A[6], local.W[i], K[i] ); i++;
|
||||
P( local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
|
||||
local.A[3], local.A[4], local.A[5], local.W[i], K[i] ); i++;
|
||||
P( local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
|
||||
local.A[2], local.A[3], local.A[4], local.W[i], K[i] ); i++;
|
||||
P( local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
|
||||
local.A[1], local.A[2], local.A[3], local.W[i], K[i] ); i++;
|
||||
P( local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
|
||||
local.A[0], local.A[1], local.A[2], local.W[i], K[i] ); i++;
|
||||
P( local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
|
||||
local.A[7], local.A[0], local.A[1], local.W[i], K[i] ); i++;
|
||||
P( local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
|
||||
local.A[6], local.A[7], local.A[0], local.W[i], K[i] ); i++;
|
||||
}
|
||||
while( i < 80 );
|
||||
do {
|
||||
P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
|
||||
local.A[5], local.A[6], local.A[7], local.W[i], K[i]); i++;
|
||||
P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
|
||||
local.A[4], local.A[5], local.A[6], local.W[i], K[i]); i++;
|
||||
P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
|
||||
local.A[3], local.A[4], local.A[5], local.W[i], K[i]); i++;
|
||||
P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
|
||||
local.A[2], local.A[3], local.A[4], local.W[i], K[i]); i++;
|
||||
P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
|
||||
local.A[1], local.A[2], local.A[3], local.W[i], K[i]); i++;
|
||||
P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
|
||||
local.A[0], local.A[1], local.A[2], local.W[i], K[i]); i++;
|
||||
P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
|
||||
local.A[7], local.A[0], local.A[1], local.W[i], K[i]); i++;
|
||||
P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
|
||||
local.A[6], local.A[7], local.A[0], local.W[i], K[i]); i++;
|
||||
} while (i < 80);
|
||||
#endif /* MBEDTLS_SHA512_SMALLER */
|
||||
|
||||
for( i = 0; i < 8; i++ )
|
||||
for (i = 0; i < 8; i++) {
|
||||
ctx->state[i] += local.A[i];
|
||||
}
|
||||
|
||||
/* Zeroise buffers and variables to clear sensitive data from memory. */
|
||||
mbedtls_platform_zeroize( &local, sizeof( local ) );
|
||||
mbedtls_platform_zeroize(&local, sizeof(local));
|
||||
|
||||
return( 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_process( mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128] )
|
||||
void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128])
|
||||
{
|
||||
mbedtls_internal_sha512_process( ctx, data );
|
||||
mbedtls_internal_sha512_process(ctx, data);
|
||||
}
|
||||
#endif
|
||||
#endif /* !MBEDTLS_SHA512_PROCESS_ALT */
|
||||
@ -301,76 +292,79 @@ void mbedtls_sha512_process( mbedtls_sha512_context *ctx,
|
||||
/*
|
||||
* SHA-512 process buffer
|
||||
*/
|
||||
int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen )
|
||||
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t fill;
|
||||
unsigned int left;
|
||||
|
||||
SHA512_VALIDATE_RET( ctx != NULL );
|
||||
SHA512_VALIDATE_RET( ilen == 0 || input != NULL );
|
||||
SHA512_VALIDATE_RET(ctx != NULL);
|
||||
SHA512_VALIDATE_RET(ilen == 0 || input != NULL);
|
||||
|
||||
if( ilen == 0 )
|
||||
return( 0 );
|
||||
if (ilen == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
left = (unsigned int) (ctx->total[0] & 0x7F);
|
||||
fill = 128 - left;
|
||||
|
||||
ctx->total[0] += (uint64_t) ilen;
|
||||
|
||||
if( ctx->total[0] < (uint64_t) ilen )
|
||||
if (ctx->total[0] < (uint64_t) ilen) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
if( left && ilen >= fill )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||
if (left && ilen >= fill) {
|
||||
memcpy((void *) (ctx->buffer + left), input, fill);
|
||||
|
||||
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 )
|
||||
return( ret );
|
||||
if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while( ilen >= 128 )
|
||||
{
|
||||
if( ( ret = mbedtls_internal_sha512_process( ctx, input ) ) != 0 )
|
||||
return( ret );
|
||||
while (ilen >= 128) {
|
||||
if ((ret = mbedtls_internal_sha512_process(ctx, input)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
input += 128;
|
||||
ilen -= 128;
|
||||
}
|
||||
|
||||
if( ilen > 0 )
|
||||
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
||||
if (ilen > 0) {
|
||||
memcpy((void *) (ctx->buffer + left), input, ilen);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen )
|
||||
void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
mbedtls_sha512_update_ret( ctx, input, ilen );
|
||||
mbedtls_sha512_update_ret(ctx, input, ilen);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* SHA-512 final digest
|
||||
*/
|
||||
int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64] )
|
||||
int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64])
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned used;
|
||||
uint64_t high, low;
|
||||
|
||||
SHA512_VALIDATE_RET( ctx != NULL );
|
||||
SHA512_VALIDATE_RET( (unsigned char *)output != NULL );
|
||||
SHA512_VALIDATE_RET(ctx != NULL);
|
||||
SHA512_VALIDATE_RET((unsigned char *) output != NULL);
|
||||
|
||||
/*
|
||||
* Add padding: 0x80 then 0x00 until 16 bytes remain for the length
|
||||
@ -379,63 +373,61 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
|
||||
|
||||
ctx->buffer[used++] = 0x80;
|
||||
|
||||
if( used <= 112 )
|
||||
{
|
||||
if (used <= 112) {
|
||||
/* Enough room for padding + length in current block */
|
||||
memset( ctx->buffer + used, 0, 112 - used );
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(ctx->buffer + used, 0, 112 - used);
|
||||
} else {
|
||||
/* 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 )
|
||||
return( ret );
|
||||
if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
memset( ctx->buffer, 0, 112 );
|
||||
memset(ctx->buffer, 0, 112);
|
||||
}
|
||||
|
||||
/*
|
||||
* Add message length
|
||||
*/
|
||||
high = ( ctx->total[0] >> 61 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
high = (ctx->total[0] >> 61)
|
||||
| (ctx->total[1] << 3);
|
||||
low = (ctx->total[0] << 3);
|
||||
|
||||
sha512_put_uint64_be( high, ctx->buffer, 112 );
|
||||
sha512_put_uint64_be( low, ctx->buffer, 120 );
|
||||
sha512_put_uint64_be(high, ctx->buffer, 112);
|
||||
sha512_put_uint64_be(low, ctx->buffer, 120);
|
||||
|
||||
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 )
|
||||
return( ret );
|
||||
if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Output final state
|
||||
*/
|
||||
sha512_put_uint64_be( ctx->state[0], output, 0 );
|
||||
sha512_put_uint64_be( ctx->state[1], output, 8 );
|
||||
sha512_put_uint64_be( ctx->state[2], output, 16 );
|
||||
sha512_put_uint64_be( ctx->state[3], output, 24 );
|
||||
sha512_put_uint64_be( ctx->state[4], output, 32 );
|
||||
sha512_put_uint64_be( ctx->state[5], output, 40 );
|
||||
sha512_put_uint64_be(ctx->state[0], output, 0);
|
||||
sha512_put_uint64_be(ctx->state[1], output, 8);
|
||||
sha512_put_uint64_be(ctx->state[2], output, 16);
|
||||
sha512_put_uint64_be(ctx->state[3], output, 24);
|
||||
sha512_put_uint64_be(ctx->state[4], output, 32);
|
||||
sha512_put_uint64_be(ctx->state[5], output, 40);
|
||||
|
||||
int truncated = 0;
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
truncated = ctx->is384;
|
||||
#endif
|
||||
if( !truncated )
|
||||
{
|
||||
sha512_put_uint64_be( ctx->state[6], output, 48 );
|
||||
sha512_put_uint64_be( ctx->state[7], output, 56 );
|
||||
if (!truncated) {
|
||||
sha512_put_uint64_be(ctx->state[6], output, 48);
|
||||
sha512_put_uint64_be(ctx->state[7], output, 56);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64] )
|
||||
void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64])
|
||||
{
|
||||
mbedtls_sha512_finish_ret( ctx, output );
|
||||
mbedtls_sha512_finish_ret(ctx, output);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -444,46 +436,49 @@ void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
|
||||
/*
|
||||
* output = SHA-512( input buffer )
|
||||
*/
|
||||
int mbedtls_sha512_ret( const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384 )
|
||||
int mbedtls_sha512_ret(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_sha512_context ctx;
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 );
|
||||
SHA512_VALIDATE_RET(is384 == 0 || is384 == 1);
|
||||
#else
|
||||
SHA512_VALIDATE_RET( is384 == 0 );
|
||||
SHA512_VALIDATE_RET(is384 == 0);
|
||||
#endif
|
||||
SHA512_VALIDATE_RET( ilen == 0 || input != NULL );
|
||||
SHA512_VALIDATE_RET( (unsigned char *)output != NULL );
|
||||
SHA512_VALIDATE_RET(ilen == 0 || input != NULL);
|
||||
SHA512_VALIDATE_RET((unsigned char *) output != NULL);
|
||||
|
||||
mbedtls_sha512_init( &ctx );
|
||||
mbedtls_sha512_init(&ctx);
|
||||
|
||||
if( ( ret = mbedtls_sha512_starts_ret( &ctx, is384 ) ) != 0 )
|
||||
if ((ret = mbedtls_sha512_starts_ret(&ctx, is384)) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_sha512_update_ret( &ctx, input, ilen ) ) != 0 )
|
||||
if ((ret = mbedtls_sha512_update_ret(&ctx, input, ilen)) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_sha512_finish_ret( &ctx, output ) ) != 0 )
|
||||
if ((ret = mbedtls_sha512_finish_ret(&ctx, output)) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_sha512_free( &ctx );
|
||||
mbedtls_sha512_free(&ctx);
|
||||
|
||||
return( ret );
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512( const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384 )
|
||||
void mbedtls_sha512(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384)
|
||||
{
|
||||
mbedtls_sha512_ret( input, ilen, output, is384 );
|
||||
mbedtls_sha512_ret(input, ilen, output, is384);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -495,7 +490,9 @@ void mbedtls_sha512( const unsigned char *input,
|
||||
static const unsigned char sha512_test_buf[3][113] =
|
||||
{
|
||||
{ "abc" },
|
||||
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
|
||||
{
|
||||
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
|
||||
},
|
||||
{ "" }
|
||||
};
|
||||
|
||||
@ -559,31 +556,30 @@ static const unsigned char sha512_test_sum[][64] =
|
||||
0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
|
||||
};
|
||||
|
||||
#define ARRAY_LENGTH( a ) ( sizeof( a ) / sizeof( ( a )[0] ) )
|
||||
#define ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_sha512_self_test( int verbose )
|
||||
int mbedtls_sha512_self_test(int verbose)
|
||||
{
|
||||
int i, j, k, buflen, ret = 0;
|
||||
unsigned char *buf;
|
||||
unsigned char sha512sum[64];
|
||||
mbedtls_sha512_context ctx;
|
||||
|
||||
buf = mbedtls_calloc( 1024, sizeof(unsigned char) );
|
||||
if( NULL == buf )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "Buffer allocation failed\n" );
|
||||
buf = mbedtls_calloc(1024, sizeof(unsigned char));
|
||||
if (NULL == buf) {
|
||||
if (verbose != 0) {
|
||||
mbedtls_printf("Buffer allocation failed\n");
|
||||
}
|
||||
|
||||
return( 1 );
|
||||
return 1;
|
||||
}
|
||||
|
||||
mbedtls_sha512_init( &ctx );
|
||||
mbedtls_sha512_init(&ctx);
|
||||
|
||||
for( i = 0; i < (int) ARRAY_LENGTH(sha512_test_sum); i++ )
|
||||
{
|
||||
for (i = 0; i < (int) ARRAY_LENGTH(sha512_test_sum); i++) {
|
||||
j = i % 3;
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
k = i < 3;
|
||||
@ -591,58 +587,61 @@ int mbedtls_sha512_self_test( int verbose )
|
||||
k = 0;
|
||||
#endif
|
||||
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 );
|
||||
if (verbose != 0) {
|
||||
mbedtls_printf(" SHA-%d test #%d: ", 512 - k * 128, j + 1);
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_sha512_starts_ret( &ctx, k ) ) != 0 )
|
||||
if ((ret = mbedtls_sha512_starts_ret(&ctx, k)) != 0) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if( j == 2 )
|
||||
{
|
||||
memset( buf, 'a', buflen = 1000 );
|
||||
if (j == 2) {
|
||||
memset(buf, 'a', buflen = 1000);
|
||||
|
||||
for( j = 0; j < 1000; j++ )
|
||||
{
|
||||
ret = mbedtls_sha512_update_ret( &ctx, buf, buflen );
|
||||
if( ret != 0 )
|
||||
for (j = 0; j < 1000; j++) {
|
||||
ret = mbedtls_sha512_update_ret(&ctx, buf, buflen);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ret = mbedtls_sha512_update_ret(&ctx, sha512_test_buf[j],
|
||||
sha512_test_buflen[j]);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = mbedtls_sha512_update_ret( &ctx, sha512_test_buf[j],
|
||||
sha512_test_buflen[j] );
|
||||
if( ret != 0 )
|
||||
goto fail;
|
||||
|
||||
if ((ret = mbedtls_sha512_finish_ret(&ctx, sha512sum)) != 0) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_sha512_finish_ret( &ctx, sha512sum ) ) != 0 )
|
||||
goto fail;
|
||||
|
||||
if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 )
|
||||
{
|
||||
if (memcmp(sha512sum, sha512_test_sum[i], 64 - k * 16) != 0) {
|
||||
ret = 1;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
goto exit;
|
||||
|
||||
fail:
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "failed\n" );
|
||||
if (verbose != 0) {
|
||||
mbedtls_printf("failed\n");
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_sha512_free( &ctx );
|
||||
mbedtls_free( buf );
|
||||
mbedtls_sha512_free(&ctx);
|
||||
mbedtls_free(buf);
|
||||
|
||||
return( ret );
|
||||
return ret;
|
||||
}
|
||||
|
||||
#undef ARRAY_LENGTH
|
||||
|
Reference in New Issue
Block a user