mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-26 10:05:46 +01:00
Split hashing operations out into an mbedTLS hash driver
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
This commit is contained in:
parent
17605f072b
commit
0e307647e6
@ -89,35 +89,10 @@ typedef struct {
|
||||
|
||||
struct psa_hash_operation_s
|
||||
{
|
||||
psa_algorithm_t alg;
|
||||
union
|
||||
{
|
||||
unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
|
||||
#if defined(MBEDTLS_MD2_C)
|
||||
mbedtls_md2_context md2;
|
||||
#endif
|
||||
#if defined(MBEDTLS_MD4_C)
|
||||
mbedtls_md4_context md4;
|
||||
#endif
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
mbedtls_md5_context md5;
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
mbedtls_ripemd160_context ripemd160;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
mbedtls_sha1_context sha1;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
mbedtls_sha256_context sha256;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
mbedtls_sha512_context sha512;
|
||||
#endif
|
||||
} ctx;
|
||||
psa_operation_driver_context_t ctx;
|
||||
};
|
||||
|
||||
#define PSA_HASH_OPERATION_INIT {0, {0}}
|
||||
#define PSA_HASH_OPERATION_INIT {{0, 0}}
|
||||
static inline struct psa_hash_operation_s psa_hash_operation_init( void )
|
||||
{
|
||||
const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
|
||||
@ -127,6 +102,8 @@ static inline struct psa_hash_operation_s psa_hash_operation_init( void )
|
||||
#if defined(MBEDTLS_MD_C)
|
||||
typedef struct
|
||||
{
|
||||
/** The HMAC algorithm in use */
|
||||
psa_algorithm_t alg;
|
||||
/** The hash context. */
|
||||
struct psa_hash_operation_s hash_ctx;
|
||||
/** The HMAC part of the context. */
|
||||
|
@ -64,6 +64,7 @@ set(src_crypto
|
||||
psa_crypto_client.c
|
||||
psa_crypto_driver_wrappers.c
|
||||
psa_crypto_ecp.c
|
||||
psa_crypto_hash.c
|
||||
psa_crypto_rsa.c
|
||||
psa_crypto_se.c
|
||||
psa_crypto_slot_management.c
|
||||
|
@ -121,6 +121,7 @@ OBJS_CRYPTO= \
|
||||
psa_crypto_client.o \
|
||||
psa_crypto_driver_wrappers.o \
|
||||
psa_crypto_ecp.o \
|
||||
psa_crypto_hash.o \
|
||||
psa_crypto_rsa.o \
|
||||
psa_crypto_se.o \
|
||||
psa_crypto_slot_management.o \
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include "psa_crypto_invasive.h"
|
||||
#include "psa_crypto_driver_wrappers.h"
|
||||
#include "psa_crypto_ecp.h"
|
||||
#include "psa_crypto_hash.h"
|
||||
#include "psa_crypto_rsa.h"
|
||||
#include "psa_crypto_ecp.h"
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
||||
@ -2196,219 +2197,58 @@ const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
|
||||
|
||||
psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
|
||||
{
|
||||
switch( operation->alg )
|
||||
if( operation != NULL )
|
||||
{
|
||||
case 0:
|
||||
/* The object has (apparently) been initialized but it is not
|
||||
* in use. It's ok to call abort on such an object, and there's
|
||||
* nothing to do. */
|
||||
break;
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
mbedtls_md2_free( &operation->ctx.md2 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
mbedtls_md4_free( &operation->ctx.md4 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
mbedtls_md5_free( &operation->ctx.md5 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
mbedtls_sha1_free( &operation->ctx.sha1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
mbedtls_sha256_free( &operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
mbedtls_sha256_free( &operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
mbedtls_sha512_free( &operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
mbedtls_sha512_free( &operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
if( operation->ctx.ctx != NULL )
|
||||
{
|
||||
psa_status_t status = mbedtls_psa_hash_abort( operation->ctx.ctx );
|
||||
mbedtls_free( operation->ctx.ctx );
|
||||
operation->ctx.ctx = NULL;
|
||||
return( status );
|
||||
}
|
||||
operation->alg = 0;
|
||||
else
|
||||
{
|
||||
// Multiple consequent calls to abort return success
|
||||
return( PSA_SUCCESS );
|
||||
}
|
||||
}
|
||||
else
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
|
||||
psa_algorithm_t alg )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if( operation == NULL || !PSA_ALG_IS_HASH( alg ) )
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
|
||||
/* A context must be freshly initialized before it can be set up. */
|
||||
if( operation->alg != 0 )
|
||||
{
|
||||
if( operation->ctx.ctx != NULL )
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
|
||||
switch( alg )
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
mbedtls_md2_init( &operation->ctx.md2 );
|
||||
ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
mbedtls_md4_init( &operation->ctx.md4 );
|
||||
ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
mbedtls_md5_init( &operation->ctx.md5 );
|
||||
ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
|
||||
ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
mbedtls_sha1_init( &operation->ctx.sha1 );
|
||||
ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
mbedtls_sha256_init( &operation->ctx.sha256 );
|
||||
ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
mbedtls_sha256_init( &operation->ctx.sha256 );
|
||||
ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
mbedtls_sha512_init( &operation->ctx.sha512 );
|
||||
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
mbedtls_sha512_init( &operation->ctx.sha512 );
|
||||
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ALG_IS_HASH( alg ) ?
|
||||
PSA_ERROR_NOT_SUPPORTED :
|
||||
PSA_ERROR_INVALID_ARGUMENT );
|
||||
}
|
||||
if( ret == 0 )
|
||||
operation->alg = alg;
|
||||
else
|
||||
operation->ctx.ctx = mbedtls_calloc( 1, sizeof(mbedtls_psa_hash_operation_t) );
|
||||
status = mbedtls_psa_hash_setup( operation->ctx.ctx, alg );
|
||||
if( status != PSA_SUCCESS )
|
||||
psa_hash_abort( operation );
|
||||
return( mbedtls_to_psa_error( ret ) );
|
||||
return( status );
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_update( psa_hash_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
size_t input_length )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* Don't require hash implementations to behave correctly on a
|
||||
* zero-length input, which may have an invalid pointer. */
|
||||
if( input_length == 0 )
|
||||
return( PSA_SUCCESS );
|
||||
|
||||
switch( operation->alg )
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
ret = mbedtls_md2_update_ret( &operation->ctx.md2,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
ret = mbedtls_md4_update_ret( &operation->ctx.md4,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
ret = mbedtls_md5_update_ret( &operation->ctx.md5,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
(void)input;
|
||||
if( operation == NULL )
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
if( operation->ctx.ctx == NULL )
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
|
||||
if( ret != 0 )
|
||||
psa_status_t status = mbedtls_psa_hash_update( operation->ctx.ctx,
|
||||
input, input_length );
|
||||
if( status != PSA_SUCCESS )
|
||||
psa_hash_abort( operation );
|
||||
return( mbedtls_to_psa_error( ret ) );
|
||||
return( status );
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
|
||||
@ -2416,89 +2256,16 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
|
||||
size_t hash_size,
|
||||
size_t *hash_length )
|
||||
{
|
||||
psa_status_t status;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
|
||||
|
||||
/* Fill the output buffer with something that isn't a valid hash
|
||||
* (barring an attack on the hash and deliberately-crafted input),
|
||||
* in case the caller doesn't check the return status properly. */
|
||||
*hash_length = hash_size;
|
||||
/* If hash_size is 0 then hash may be NULL and then the
|
||||
* call to memset would have undefined behavior. */
|
||||
if( hash_size != 0 )
|
||||
memset( hash, '!', hash_size );
|
||||
|
||||
if( hash_size < actual_hash_length )
|
||||
{
|
||||
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
switch( operation->alg )
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
if( operation == NULL )
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
if( operation->ctx.ctx == NULL )
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
status = mbedtls_to_psa_error( ret );
|
||||
|
||||
exit:
|
||||
if( status == PSA_SUCCESS )
|
||||
{
|
||||
*hash_length = actual_hash_length;
|
||||
return( psa_hash_abort( operation ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
psa_status_t status = mbedtls_psa_hash_finish( operation->ctx.ctx,
|
||||
hash, hash_size, hash_length );
|
||||
psa_hash_abort( operation );
|
||||
return( status );
|
||||
}
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
|
||||
const uint8_t *hash,
|
||||
@ -2523,26 +2290,8 @@ psa_status_t psa_hash_compute( psa_algorithm_t alg,
|
||||
uint8_t *hash, size_t hash_size,
|
||||
size_t *hash_length )
|
||||
{
|
||||
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
*hash_length = hash_size;
|
||||
status = psa_hash_setup( &operation, alg );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
status = psa_hash_update( &operation, input, input_length );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
status = psa_hash_finish( &operation, hash, hash_size, hash_length );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
|
||||
exit:
|
||||
if( status == PSA_SUCCESS )
|
||||
status = psa_hash_abort( &operation );
|
||||
else
|
||||
psa_hash_abort( &operation );
|
||||
return( status );
|
||||
return( mbedtls_psa_hash_compute( alg, input, input_length,
|
||||
hash, hash_size, hash_length ) );
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_compare( psa_algorithm_t alg,
|
||||
@ -2573,73 +2322,15 @@ exit:
|
||||
psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
|
||||
psa_hash_operation_t *target_operation )
|
||||
{
|
||||
if( target_operation->alg != 0 )
|
||||
if( source_operation == NULL || target_operation == NULL )
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
if( source_operation->ctx.ctx == NULL )
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
if( target_operation->ctx.ctx != NULL )
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
|
||||
switch( source_operation->alg )
|
||||
{
|
||||
case 0:
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
mbedtls_md2_clone( &target_operation->ctx.md2,
|
||||
&source_operation->ctx.md2 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
mbedtls_md4_clone( &target_operation->ctx.md4,
|
||||
&source_operation->ctx.md4 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
mbedtls_md5_clone( &target_operation->ctx.md5,
|
||||
&source_operation->ctx.md5 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
|
||||
&source_operation->ctx.ripemd160 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
mbedtls_sha1_clone( &target_operation->ctx.sha1,
|
||||
&source_operation->ctx.sha1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
mbedtls_sha256_clone( &target_operation->ctx.sha256,
|
||||
&source_operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
mbedtls_sha256_clone( &target_operation->ctx.sha256,
|
||||
&source_operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
mbedtls_sha512_clone( &target_operation->ctx.sha512,
|
||||
&source_operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
mbedtls_sha512_clone( &target_operation->ctx.sha512,
|
||||
&source_operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ERROR_NOT_SUPPORTED );
|
||||
}
|
||||
|
||||
target_operation->alg = source_operation->alg;
|
||||
return( PSA_SUCCESS );
|
||||
target_operation->ctx.ctx = mbedtls_calloc(1, sizeof(mbedtls_psa_hash_operation_t));
|
||||
return( mbedtls_psa_hash_clone( source_operation->ctx.ctx, target_operation->ctx.ctx ) );
|
||||
}
|
||||
|
||||
|
||||
@ -2795,7 +2486,7 @@ static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
|
||||
if( PSA_ALG_IS_HMAC( operation->alg ) )
|
||||
{
|
||||
/* We'll set up the hash operation later in psa_hmac_setup_internal. */
|
||||
operation->ctx.hmac.hash_ctx.alg = 0;
|
||||
operation->ctx.hmac.alg = 0;
|
||||
status = PSA_SUCCESS;
|
||||
}
|
||||
else
|
||||
@ -2902,6 +2593,8 @@ static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
|
||||
size_t block_size = psa_get_hash_block_size( hash_alg );
|
||||
psa_status_t status;
|
||||
|
||||
hmac->alg = hash_alg;
|
||||
|
||||
/* Sanity checks on block_size, to guarantee that there won't be a buffer
|
||||
* overflow below. This should never trigger if the hash algorithm
|
||||
* is implemented correctly. */
|
||||
@ -3119,7 +2812,7 @@ static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
|
||||
size_t mac_size )
|
||||
{
|
||||
uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
|
||||
psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
|
||||
psa_algorithm_t hash_alg = hmac->alg;
|
||||
size_t hash_size = 0;
|
||||
size_t block_size = psa_get_hash_block_size( hash_alg );
|
||||
psa_status_t status;
|
||||
|
442
library/psa_crypto_hash.c
Normal file
442
library/psa_crypto_hash.c
Normal file
@ -0,0 +1,442 @@
|
||||
/*
|
||||
* PSA hashing layer on top of Mbed TLS software crypto
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
|
||||
#include <psa/crypto.h>
|
||||
#include "psa_crypto_core.h"
|
||||
#include "psa_crypto_hash.h"
|
||||
|
||||
#include <mbedtls/error.h>
|
||||
#include <string.h>
|
||||
|
||||
psa_status_t mbedtls_psa_hash_compute(
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t *input,
|
||||
size_t input_length,
|
||||
uint8_t *hash,
|
||||
size_t hash_size,
|
||||
size_t *hash_length)
|
||||
{
|
||||
mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
*hash_length = hash_size;
|
||||
status = mbedtls_psa_hash_setup( &operation, alg );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
status = mbedtls_psa_hash_update( &operation, input, input_length );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
|
||||
exit:
|
||||
if( status == PSA_SUCCESS )
|
||||
status = mbedtls_psa_hash_abort( &operation );
|
||||
else
|
||||
mbedtls_psa_hash_abort( &operation );
|
||||
return( status );
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_hash_setup(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
psa_algorithm_t alg )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* A context must be freshly initialized before it can be set up. */
|
||||
if( operation->alg != 0 )
|
||||
{
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
|
||||
switch( alg )
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
mbedtls_md2_init( &operation->ctx.md2 );
|
||||
ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
mbedtls_md4_init( &operation->ctx.md4 );
|
||||
ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
mbedtls_md5_init( &operation->ctx.md5 );
|
||||
ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
|
||||
ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
mbedtls_sha1_init( &operation->ctx.sha1 );
|
||||
ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
mbedtls_sha256_init( &operation->ctx.sha256 );
|
||||
ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
mbedtls_sha256_init( &operation->ctx.sha256 );
|
||||
ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
mbedtls_sha512_init( &operation->ctx.sha512 );
|
||||
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
mbedtls_sha512_init( &operation->ctx.sha512 );
|
||||
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ALG_IS_HASH( alg ) ?
|
||||
PSA_ERROR_NOT_SUPPORTED :
|
||||
PSA_ERROR_INVALID_ARGUMENT );
|
||||
}
|
||||
if( ret == 0 )
|
||||
operation->alg = alg;
|
||||
else
|
||||
mbedtls_psa_hash_abort( operation );
|
||||
return( mbedtls_to_psa_error( ret ) );
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_hash_clone(
|
||||
const mbedtls_psa_hash_operation_t *source_operation,
|
||||
mbedtls_psa_hash_operation_t *target_operation )
|
||||
{
|
||||
switch( source_operation->alg )
|
||||
{
|
||||
case 0:
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
mbedtls_md2_clone( &target_operation->ctx.md2,
|
||||
&source_operation->ctx.md2 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
mbedtls_md4_clone( &target_operation->ctx.md4,
|
||||
&source_operation->ctx.md4 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
mbedtls_md5_clone( &target_operation->ctx.md5,
|
||||
&source_operation->ctx.md5 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
|
||||
&source_operation->ctx.ripemd160 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
mbedtls_sha1_clone( &target_operation->ctx.sha1,
|
||||
&source_operation->ctx.sha1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
mbedtls_sha256_clone( &target_operation->ctx.sha256,
|
||||
&source_operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
mbedtls_sha256_clone( &target_operation->ctx.sha256,
|
||||
&source_operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
mbedtls_sha512_clone( &target_operation->ctx.sha512,
|
||||
&source_operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
mbedtls_sha512_clone( &target_operation->ctx.sha512,
|
||||
&source_operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ERROR_NOT_SUPPORTED );
|
||||
}
|
||||
|
||||
target_operation->alg = source_operation->alg;
|
||||
return( PSA_SUCCESS );
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_hash_update(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
size_t input_length )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* Don't require hash implementations to behave correctly on a
|
||||
* zero-length input, which may have an invalid pointer. */
|
||||
if( input_length == 0 )
|
||||
return( PSA_SUCCESS );
|
||||
|
||||
switch( operation->alg )
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
ret = mbedtls_md2_update_ret( &operation->ctx.md2,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
ret = mbedtls_md4_update_ret( &operation->ctx.md4,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
ret = mbedtls_md5_update_ret( &operation->ctx.md5,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
|
||||
input, input_length );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
(void)input;
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
|
||||
if( ret != 0 )
|
||||
mbedtls_psa_hash_abort( operation );
|
||||
return( mbedtls_to_psa_error( ret ) );
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_hash_finish(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
uint8_t *hash,
|
||||
size_t hash_size,
|
||||
size_t *hash_length )
|
||||
{
|
||||
psa_status_t status;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
|
||||
|
||||
/* Fill the output buffer with something that isn't a valid hash
|
||||
* (barring an attack on the hash and deliberately-crafted input),
|
||||
* in case the caller doesn't check the return status properly. */
|
||||
*hash_length = hash_size;
|
||||
/* If hash_size is 0 then hash may be NULL and then the
|
||||
* call to memset would have undefined behavior. */
|
||||
if( hash_size != 0 )
|
||||
memset( hash, '!', hash_size );
|
||||
|
||||
if( hash_size < actual_hash_length )
|
||||
{
|
||||
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
switch( operation->alg )
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
status = mbedtls_to_psa_error( ret );
|
||||
|
||||
exit:
|
||||
if( status == PSA_SUCCESS )
|
||||
{
|
||||
*hash_length = actual_hash_length;
|
||||
return( mbedtls_psa_hash_abort( operation ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_psa_hash_abort( operation );
|
||||
return( status );
|
||||
}
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_hash_abort(
|
||||
mbedtls_psa_hash_operation_t *operation )
|
||||
{
|
||||
switch( operation->alg )
|
||||
{
|
||||
case 0:
|
||||
/* The object has (apparently) been initialized but it is not
|
||||
* in use. It's ok to call abort on such an object, and there's
|
||||
* nothing to do. */
|
||||
break;
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
||||
case PSA_ALG_MD2:
|
||||
mbedtls_md2_free( &operation->ctx.md2 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
||||
case PSA_ALG_MD4:
|
||||
mbedtls_md4_free( &operation->ctx.md4 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
||||
case PSA_ALG_MD5:
|
||||
mbedtls_md5_free( &operation->ctx.md5 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
||||
case PSA_ALG_RIPEMD160:
|
||||
mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
||||
case PSA_ALG_SHA_1:
|
||||
mbedtls_sha1_free( &operation->ctx.sha1 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
||||
case PSA_ALG_SHA_224:
|
||||
mbedtls_sha256_free( &operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
||||
case PSA_ALG_SHA_256:
|
||||
mbedtls_sha256_free( &operation->ctx.sha256 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
||||
case PSA_ALG_SHA_384:
|
||||
mbedtls_sha512_free( &operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
||||
case PSA_ALG_SHA_512:
|
||||
mbedtls_sha512_free( &operation->ctx.sha512 );
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
}
|
||||
operation->alg = 0;
|
||||
return( PSA_SUCCESS );
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
236
library/psa_crypto_hash.h
Normal file
236
library/psa_crypto_hash.h
Normal file
@ -0,0 +1,236 @@
|
||||
/*
|
||||
* PSA hashing layer on top of Mbed TLS software crypto
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef PSA_CRYPTO_HASH_H
|
||||
#define PSA_CRYPTO_HASH_H
|
||||
|
||||
#include <psa/crypto.h>
|
||||
#include "mbedtls/md2.h"
|
||||
#include "mbedtls/md4.h"
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/ripemd160.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
psa_algorithm_t alg;
|
||||
union
|
||||
{
|
||||
unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
|
||||
#if defined(MBEDTLS_MD2_C)
|
||||
mbedtls_md2_context md2;
|
||||
#endif
|
||||
#if defined(MBEDTLS_MD4_C)
|
||||
mbedtls_md4_context md4;
|
||||
#endif
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
mbedtls_md5_context md5;
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
mbedtls_ripemd160_context ripemd160;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
mbedtls_sha1_context sha1;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
mbedtls_sha256_context sha256;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
mbedtls_sha512_context sha512;
|
||||
#endif
|
||||
} ctx;
|
||||
} mbedtls_psa_hash_operation_t;
|
||||
|
||||
#define MBEDTLS_PSA_HASH_OPERATION_INIT {0, {0}}
|
||||
|
||||
/** Calculate the hash (digest) of a message using Mbed TLS routines.
|
||||
*
|
||||
* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
|
||||
* such that #PSA_ALG_IS_HASH(\p alg) is true).
|
||||
* \param[in] input Buffer containing the message to hash.
|
||||
* \param input_length Size of the \p input buffer in bytes.
|
||||
* \param[out] hash Buffer where the hash is to be written.
|
||||
* \param hash_size Size of the \p hash buffer in bytes.
|
||||
* \param[out] hash_length On success, the number of bytes
|
||||
* that make up the hash value. This is always
|
||||
* #PSA_HASH_LENGTH(\p alg).
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not supported or is not a hash algorithm.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* \p hash_size is too small
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_compute(
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t *input,
|
||||
size_t input_length,
|
||||
uint8_t *hash,
|
||||
size_t hash_size,
|
||||
size_t *hash_length);
|
||||
|
||||
/** Set up a multipart hash operation using Mbed TLS routines.
|
||||
*
|
||||
* If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
|
||||
* operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
|
||||
* core may call mbedtls_psa_hash_abort() at any time after the operation
|
||||
* has been initialized.
|
||||
*
|
||||
* After a successful call to mbedtls_psa_hash_setup(), the core must
|
||||
* eventually terminate the operation. The following events terminate an
|
||||
* operation:
|
||||
* - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
|
||||
* - A call to mbedtls_psa_hash_abort().
|
||||
*
|
||||
* \param[in,out] operation The operation object to set up. It must have
|
||||
* been initialized to all-zero and not yet be in use.
|
||||
* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
|
||||
* such that #PSA_ALG_IS_HASH(\p alg) is true).
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not a supported hash algorithm.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be inactive).
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_setup(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
psa_algorithm_t alg );
|
||||
|
||||
/** Clone an Mbed TLS hash operation.
|
||||
*
|
||||
* This function copies the state of an ongoing hash operation to
|
||||
* a new operation object. In other words, this function is equivalent
|
||||
* to calling mbedtls_psa_hash_setup() on \p target_operation with the same
|
||||
* algorithm that \p source_operation was set up for, then
|
||||
* mbedtls_psa_hash_update() on \p target_operation with the same input that
|
||||
* that was passed to \p source_operation. After this function returns, the
|
||||
* two objects are independent, i.e. subsequent calls involving one of
|
||||
* the objects do not affect the other object.
|
||||
*
|
||||
* \param[in] source_operation The active hash operation to clone.
|
||||
* \param[in,out] target_operation The operation object to set up.
|
||||
* It must be initialized but not active.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The \p source_operation state is not valid (it must be active).
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The \p target_operation state is not valid (it must be inactive).
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_clone(
|
||||
const mbedtls_psa_hash_operation_t *source_operation,
|
||||
mbedtls_psa_hash_operation_t *target_operation );
|
||||
|
||||
/** Add a message fragment to a multipart Mbed TLS hash operation.
|
||||
*
|
||||
* The application must call mbedtls_psa_hash_setup() before calling this function.
|
||||
*
|
||||
* If this function returns an error status, the operation enters an error
|
||||
* state and must be aborted by calling mbedtls_psa_hash_abort().
|
||||
*
|
||||
* \param[in,out] operation Active hash operation.
|
||||
* \param[in] input Buffer containing the message fragment to hash.
|
||||
* \param input_length Size of the \p input buffer in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it muct be active).
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_update(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
size_t input_length );
|
||||
|
||||
/** Finish the calculation of the Mbed TLS-calculated hash of a message.
|
||||
*
|
||||
* The application must call mbedtls_psa_hash_setup() before calling this function.
|
||||
* This function calculates the hash of the message formed by concatenating
|
||||
* the inputs passed to preceding calls to mbedtls_psa_hash_update().
|
||||
*
|
||||
* When this function returns successfuly, the operation becomes inactive.
|
||||
* If this function returns an error status, the operation enters an error
|
||||
* state and must be aborted by calling mbedtls_psa_hash_abort().
|
||||
*
|
||||
* \param[in,out] operation Active hash operation.
|
||||
* \param[out] hash Buffer where the hash is to be written.
|
||||
* \param hash_size Size of the \p hash buffer in bytes.
|
||||
* \param[out] hash_length On success, the number of bytes
|
||||
* that make up the hash value. This is always
|
||||
* #PSA_HASH_LENGTH(\c alg) where \c alg is the
|
||||
* hash algorithm that is calculated.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be active).
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p hash buffer is too small. You can determine a
|
||||
* sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
|
||||
* where \c alg is the hash algorithm that is calculated.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_finish(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
uint8_t *hash,
|
||||
size_t hash_size,
|
||||
size_t *hash_length );
|
||||
|
||||
/** Abort an Mbed TLS hash operation.
|
||||
*
|
||||
* Aborting an operation frees all associated resources except for the
|
||||
* \p operation structure itself. Once aborted, the operation object
|
||||
* can be reused for another operation by calling
|
||||
* mbedtls_psa_hash_setup() again.
|
||||
*
|
||||
* You may call this function any time after the operation object has
|
||||
* been initialized by one of the methods described in #psa_hash_operation_t.
|
||||
*
|
||||
* In particular, calling mbedtls_psa_hash_abort() after the operation has been
|
||||
* terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
|
||||
* mbedtls_psa_hash_verify() is safe and has no effect.
|
||||
*
|
||||
* \param[in,out] operation Initialized hash operation.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_abort(
|
||||
mbedtls_psa_hash_operation_t *operation );
|
||||
|
||||
#endif /* PSA_CRYPTO_HASH_H */
|
@ -251,6 +251,7 @@
|
||||
<ClInclude Include="..\..\library\psa_crypto_core.h" />
|
||||
<ClInclude Include="..\..\library\psa_crypto_driver_wrappers.h" />
|
||||
<ClInclude Include="..\..\library\psa_crypto_ecp.h" />
|
||||
<ClInclude Include="..\..\library\psa_crypto_hash.h" />
|
||||
<ClInclude Include="..\..\library\psa_crypto_invasive.h" />
|
||||
<ClInclude Include="..\..\library\psa_crypto_its.h" />
|
||||
<ClInclude Include="..\..\library\psa_crypto_random_impl.h" />
|
||||
@ -324,6 +325,7 @@
|
||||
<ClCompile Include="..\..\library\psa_crypto_client.c" />
|
||||
<ClCompile Include="..\..\library\psa_crypto_driver_wrappers.c" />
|
||||
<ClCompile Include="..\..\library\psa_crypto_ecp.c" />
|
||||
<ClCompile Include="..\..\library\psa_crypto_hash.c" />
|
||||
<ClCompile Include="..\..\library\psa_crypto_rsa.c" />
|
||||
<ClCompile Include="..\..\library\psa_crypto_se.c" />
|
||||
<ClCompile Include="..\..\library\psa_crypto_slot_management.c" />
|
||||
|
Loading…
Reference in New Issue
Block a user