mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-22 22:00:39 -06:00
Externals: Update mbedtls to 2.28.0
This commit is contained in:
213
Externals/mbedtls/library/sha512.c
vendored
213
Externals/mbedtls/library/sha512.c
vendored
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* FIPS-180-2 compliant SHA-384/512 implementation
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
@ -15,8 +15,6 @@
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
/*
|
||||
* The SHA-512 Secure Hash Standard was published by NIST in 2002.
|
||||
@ -24,16 +22,13 @@
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
|
||||
#include "mbedtls/sha512.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#if defined(_MSC_VER) || defined(__WATCOMC__)
|
||||
#define UL64(x) x##ui64
|
||||
@ -61,36 +56,14 @@
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_ALT)
|
||||
|
||||
/*
|
||||
* 64-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_UINT64_BE
|
||||
#define GET_UINT64_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint64_t) (b)[(i) ] << 56 ) \
|
||||
| ( (uint64_t) (b)[(i) + 1] << 48 ) \
|
||||
| ( (uint64_t) (b)[(i) + 2] << 40 ) \
|
||||
| ( (uint64_t) (b)[(i) + 3] << 32 ) \
|
||||
| ( (uint64_t) (b)[(i) + 4] << 24 ) \
|
||||
| ( (uint64_t) (b)[(i) + 5] << 16 ) \
|
||||
| ( (uint64_t) (b)[(i) + 6] << 8 ) \
|
||||
| ( (uint64_t) (b)[(i) + 7] ); \
|
||||
#if defined(MBEDTLS_SHA512_SMALLER)
|
||||
static void sha512_put_uint64_be( uint64_t n, unsigned char *b, uint8_t i )
|
||||
{
|
||||
MBEDTLS_PUT_UINT64_BE(n, b, i);
|
||||
}
|
||||
#endif /* GET_UINT64_BE */
|
||||
|
||||
#ifndef PUT_UINT64_BE
|
||||
#define PUT_UINT64_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
|
||||
(b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 7] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif /* PUT_UINT64_BE */
|
||||
#else
|
||||
#define sha512_put_uint64_be MBEDTLS_PUT_UINT64_BE
|
||||
#endif /* MBEDTLS_SHA512_SMALLER */
|
||||
|
||||
void mbedtls_sha512_init( mbedtls_sha512_context *ctx )
|
||||
{
|
||||
@ -122,7 +95,11 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
|
||||
int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
{
|
||||
SHA512_VALIDATE_RET( ctx != NULL );
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 );
|
||||
#else
|
||||
SHA512_VALIDATE_RET( is384 == 0 );
|
||||
#endif
|
||||
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
@ -141,6 +118,9 @@ int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
return( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA );
|
||||
#else
|
||||
/* SHA-384 */
|
||||
ctx->state[0] = UL64(0xCBBB9D5DC1059ED8);
|
||||
ctx->state[1] = UL64(0x629A292A367CD507);
|
||||
@ -150,9 +130,12 @@ int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
ctx->state[5] = UL64(0x8EB44A8768581511);
|
||||
ctx->state[6] = UL64(0xDB0C2E0D64F98FA7);
|
||||
ctx->state[7] = UL64(0x47B5481DBEFA4FA4);
|
||||
#endif /* MBEDTLS_SHA512_NO_SHA384 */
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
ctx->is384 = is384;
|
||||
#endif
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
@ -218,14 +201,17 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128] )
|
||||
{
|
||||
int i;
|
||||
uint64_t temp1, temp2, W[80];
|
||||
uint64_t A, B, C, D, E, F, G, H;
|
||||
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 );
|
||||
|
||||
#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))
|
||||
@ -233,58 +219,82 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
|
||||
#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) \
|
||||
{ \
|
||||
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
|
||||
temp2 = S2(a) + F0(a,b,c); \
|
||||
d += temp1; h = temp1 + temp2; \
|
||||
}
|
||||
#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)); \
|
||||
(d) += local.temp1; (h) = local.temp1 + local.temp2; \
|
||||
} while( 0 )
|
||||
|
||||
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
|
||||
{
|
||||
local.W[i] = S1(local.W[i - 2]) + local.W[i - 7] +
|
||||
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] );
|
||||
|
||||
local.temp1 = local.A[7]; local.A[7] = local.A[6];
|
||||
local.A[6] = local.A[5]; local.A[5] = local.A[4];
|
||||
local.A[4] = local.A[3]; local.A[3] = local.A[2];
|
||||
local.A[2] = local.A[1]; local.A[1] = local.A[0];
|
||||
local.A[0] = local.temp1;
|
||||
}
|
||||
#else /* MBEDTLS_SHA512_SMALLER */
|
||||
for( i = 0; i < 16; i++ )
|
||||
{
|
||||
GET_UINT64_BE( W[i], data, i << 3 );
|
||||
local.W[i] = MBEDTLS_GET_UINT64_BE( data, i << 3 );
|
||||
}
|
||||
|
||||
for( ; i < 80; i++ )
|
||||
{
|
||||
W[i] = S1(W[i - 2]) + W[i - 7] +
|
||||
S0(W[i - 15]) + W[i - 16];
|
||||
local.W[i] = S1(local.W[i - 2]) + local.W[i - 7] +
|
||||
S0(local.W[i - 15]) + local.W[i - 16];
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
F = ctx->state[5];
|
||||
G = ctx->state[6];
|
||||
H = ctx->state[7];
|
||||
i = 0;
|
||||
|
||||
do
|
||||
{
|
||||
P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++;
|
||||
P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++;
|
||||
P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++;
|
||||
P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++;
|
||||
P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++;
|
||||
P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++;
|
||||
P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++;
|
||||
P( B, C, D, E, F, G, H, A, W[i], K[i] ); 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] ); 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 */
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
ctx->state[4] += E;
|
||||
ctx->state[5] += F;
|
||||
ctx->state[6] += G;
|
||||
ctx->state[7] += H;
|
||||
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 ) );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
@ -305,7 +315,7 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
int ret;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t fill;
|
||||
unsigned int left;
|
||||
|
||||
@ -365,7 +375,7 @@ void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
|
||||
int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64] )
|
||||
{
|
||||
int ret;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned used;
|
||||
uint64_t high, low;
|
||||
|
||||
@ -402,8 +412,8 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_UINT64_BE( high, ctx->buffer, 112 );
|
||||
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 );
|
||||
@ -411,17 +421,19 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
|
||||
/*
|
||||
* Output final state
|
||||
*/
|
||||
PUT_UINT64_BE( ctx->state[0], output, 0 );
|
||||
PUT_UINT64_BE( ctx->state[1], output, 8 );
|
||||
PUT_UINT64_BE( ctx->state[2], output, 16 );
|
||||
PUT_UINT64_BE( ctx->state[3], output, 24 );
|
||||
PUT_UINT64_BE( ctx->state[4], output, 32 );
|
||||
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 );
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
if( ctx->is384 == 0 )
|
||||
#endif
|
||||
{
|
||||
PUT_UINT64_BE( ctx->state[6], output, 48 );
|
||||
PUT_UINT64_BE( ctx->state[7], output, 56 );
|
||||
sha512_put_uint64_be( ctx->state[6], output, 48 );
|
||||
sha512_put_uint64_be( ctx->state[7], output, 56 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
@ -445,10 +457,14 @@ int mbedtls_sha512_ret( const unsigned char *input,
|
||||
unsigned char output[64],
|
||||
int is384 )
|
||||
{
|
||||
int ret;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_sha512_context ctx;
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 );
|
||||
#else
|
||||
SHA512_VALIDATE_RET( is384 == 0 );
|
||||
#endif
|
||||
SHA512_VALIDATE_RET( ilen == 0 || input != NULL );
|
||||
SHA512_VALIDATE_RET( (unsigned char *)output != NULL );
|
||||
|
||||
@ -487,8 +503,7 @@ void mbedtls_sha512( const unsigned char *input,
|
||||
static const unsigned char sha512_test_buf[3][113] =
|
||||
{
|
||||
{ "abc" },
|
||||
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
|
||||
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
|
||||
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
|
||||
{ "" }
|
||||
};
|
||||
|
||||
@ -497,8 +512,9 @@ static const size_t sha512_test_buflen[3] =
|
||||
3, 112, 1000
|
||||
};
|
||||
|
||||
static const unsigned char sha512_test_sum[6][64] =
|
||||
static const unsigned char sha512_test_sum[][64] =
|
||||
{
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
/*
|
||||
* SHA-384 test vectors
|
||||
*/
|
||||
@ -520,6 +536,7 @@ static const unsigned char sha512_test_sum[6][64] =
|
||||
0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B,
|
||||
0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB,
|
||||
0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 },
|
||||
#endif /* !MBEDTLS_SHA512_NO_SHA384 */
|
||||
|
||||
/*
|
||||
* SHA-512 test vectors
|
||||
@ -550,6 +567,8 @@ static const unsigned char sha512_test_sum[6][64] =
|
||||
0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
|
||||
};
|
||||
|
||||
#define ARRAY_LENGTH( a ) ( sizeof( a ) / sizeof( ( a )[0] ) )
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
@ -571,10 +590,14 @@ int mbedtls_sha512_self_test( int verbose )
|
||||
|
||||
mbedtls_sha512_init( &ctx );
|
||||
|
||||
for( i = 0; i < 6; i++ )
|
||||
for( i = 0; i < (int) ARRAY_LENGTH(sha512_test_sum); i++ )
|
||||
{
|
||||
j = i % 3;
|
||||
#if !defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
k = i < 3;
|
||||
#else
|
||||
k = 0;
|
||||
#endif
|
||||
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 );
|
||||
@ -630,6 +653,8 @@ exit:
|
||||
return( ret );
|
||||
}
|
||||
|
||||
#undef ARRAY_LENGTH
|
||||
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
#endif /* MBEDTLS_SHA512_C */
|
||||
|
Reference in New Issue
Block a user