mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Merge pull request #209 from ARMmbed/psa-init_tests
Test psa_crypto_init and make it more robust
This commit is contained in:
@ -43,6 +43,7 @@
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "psa_crypto_invasive.h"
|
||||
/* Include internal declarations that are useful for implementing persistently
|
||||
* stored keys. */
|
||||
#include "psa_crypto_storage.h"
|
||||
@ -146,12 +147,23 @@ static int key_type_is_raw_bytes( psa_key_type_t type )
|
||||
return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
|
||||
}
|
||||
|
||||
enum rng_state
|
||||
{
|
||||
RNG_NOT_INITIALIZED = 0,
|
||||
RNG_INITIALIZED,
|
||||
RNG_SEEDED,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int initialized;
|
||||
void (* entropy_init )( mbedtls_entropy_context *ctx );
|
||||
void (* entropy_free )( mbedtls_entropy_context *ctx );
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
|
||||
unsigned initialized : 1;
|
||||
enum rng_state rng_state : 2;
|
||||
unsigned key_slots_initialized : 1;
|
||||
} psa_global_data_t;
|
||||
|
||||
static psa_global_data_t global_data;
|
||||
@ -4439,23 +4451,42 @@ psa_status_t psa_generate_key( psa_key_slot_t key,
|
||||
/* Module setup */
|
||||
/****************************************************************/
|
||||
|
||||
psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
|
||||
void (* entropy_init )( mbedtls_entropy_context *ctx ),
|
||||
void (* entropy_free )( mbedtls_entropy_context *ctx ) )
|
||||
{
|
||||
if( global_data.rng_state != RNG_NOT_INITIALIZED )
|
||||
return( PSA_ERROR_BAD_STATE );
|
||||
global_data.entropy_init = entropy_init;
|
||||
global_data.entropy_free = entropy_free;
|
||||
return( PSA_SUCCESS );
|
||||
}
|
||||
|
||||
void mbedtls_psa_crypto_free( void )
|
||||
{
|
||||
psa_key_slot_t key;
|
||||
key_slot_t *slot;
|
||||
psa_status_t status;
|
||||
|
||||
for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
|
||||
if( global_data.key_slots_initialized )
|
||||
{
|
||||
status = psa_get_key_slot( key, &slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
continue;
|
||||
psa_remove_key_data_from_memory( slot );
|
||||
/* Zeroize the slot to wipe metadata such as policies. */
|
||||
mbedtls_zeroize( slot, sizeof( *slot ) );
|
||||
for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
|
||||
{
|
||||
status = psa_get_key_slot( key, &slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
continue;
|
||||
psa_remove_key_data_from_memory( slot );
|
||||
/* Zeroize the slot to wipe metadata such as policies. */
|
||||
mbedtls_zeroize( slot, sizeof( *slot ) );
|
||||
}
|
||||
}
|
||||
mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
|
||||
mbedtls_entropy_free( &global_data.entropy );
|
||||
if( global_data.rng_state != RNG_NOT_INITIALIZED )
|
||||
{
|
||||
mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
|
||||
global_data.entropy_free( &global_data.entropy );
|
||||
}
|
||||
/* Wipe all remaining data, including configuration.
|
||||
* In particular, this sets all state indicator to the value
|
||||
* indicating "uninitialized". */
|
||||
mbedtls_zeroize( &global_data, sizeof( global_data ) );
|
||||
}
|
||||
|
||||
@ -4464,20 +4495,35 @@ psa_status_t psa_crypto_init( void )
|
||||
int ret;
|
||||
const unsigned char drbg_seed[] = "PSA";
|
||||
|
||||
/* Double initialization is explicitly allowed. */
|
||||
if( global_data.initialized != 0 )
|
||||
return( PSA_SUCCESS );
|
||||
|
||||
mbedtls_zeroize( &global_data, sizeof( global_data ) );
|
||||
mbedtls_entropy_init( &global_data.entropy );
|
||||
mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
|
||||
/* Set default configuration if
|
||||
* mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
|
||||
if( global_data.entropy_init == NULL )
|
||||
global_data.entropy_init = mbedtls_entropy_init;
|
||||
if( global_data.entropy_free == NULL )
|
||||
global_data.entropy_free = mbedtls_entropy_free;
|
||||
|
||||
/* Initialize the random generator. */
|
||||
global_data.entropy_init( &global_data.entropy );
|
||||
mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
|
||||
global_data.rng_state = RNG_INITIALIZED;
|
||||
ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
|
||||
mbedtls_entropy_func,
|
||||
&global_data.entropy,
|
||||
drbg_seed, sizeof( drbg_seed ) - 1 );
|
||||
if( ret != 0 )
|
||||
goto exit;
|
||||
global_data.rng_state = RNG_SEEDED;
|
||||
|
||||
/* Initialize the key slots. Zero-initialization has made all key
|
||||
* slots empty, so there is nothing to do. In a future version we will
|
||||
* load data from storage. */
|
||||
global_data.key_slots_initialized = 1;
|
||||
|
||||
/* All done. */
|
||||
global_data.initialized = 1;
|
||||
|
||||
exit:
|
||||
|
79
library/psa_crypto_invasive.h
Normal file
79
library/psa_crypto_invasive.h
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* \file psa_crypto_invasive.h
|
||||
*
|
||||
* \brief PSA cryptography module: invasive interfaces for test only.
|
||||
*
|
||||
* The interfaces in this file are intended for testing purposes only.
|
||||
* They MUST NOT be made available to clients over IPC in integrations
|
||||
* with isolation, and they SHOULD NOT be made available in library
|
||||
* integrations except when building the library for testing.
|
||||
*/
|
||||
/*
|
||||
* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
||||
* 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.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef PSA_CRYPTO_INVASIVE_H
|
||||
#define PSA_CRYPTO_INVASIVE_H
|
||||
|
||||
#if defined(MBEDTLS_CONFIG_FILE)
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#else
|
||||
#include "mbedtls/config.h"
|
||||
#endif
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "mbedtls/entropy.h"
|
||||
|
||||
/** \brief Configure entropy sources.
|
||||
*
|
||||
* This function may only be called before a call to psa_crypto_init(),
|
||||
* or after a call to mbedtls_psa_crypto_free() and before any
|
||||
* subsequent call to psa_crypto_init().
|
||||
*
|
||||
* This function is only intended for test purposes. The functionality
|
||||
* it provides is also useful for system integrators, but
|
||||
* system integrators should configure entropy drivers instead of
|
||||
* breaking through to the Mbed TLS API.
|
||||
*
|
||||
* \param entropy_init Function to initialize the entropy context
|
||||
* and set up the desired entropy sources.
|
||||
* It is called by psa_crypto_init().
|
||||
* By default this is mbedtls_entropy_init().
|
||||
* This function cannot report failures directly.
|
||||
* To indicate a failure, set the entropy context
|
||||
* to a state where mbedtls_entropy_func() will
|
||||
* return an error.
|
||||
* \param entropy_free Function to free the entropy context
|
||||
* and associated resources.
|
||||
* It is called by mbedtls_psa_crypto_free().
|
||||
* By default this is mbedtls_entropy_free().
|
||||
*
|
||||
* \retval PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval PSA_ERROR_NOT_PERMITTED
|
||||
* The caller does not have the permission to configure
|
||||
* entropy sources.
|
||||
* \retval PSA_ERROR_BAD_STATE
|
||||
* The library has already been initialized.
|
||||
*/
|
||||
psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
|
||||
void (* entropy_init )( mbedtls_entropy_context *ctx ),
|
||||
void (* entropy_free )( mbedtls_entropy_context *ctx ) );
|
||||
|
||||
#endif /* PSA_CRYPTO_INVASIVE_H */
|
Reference in New Issue
Block a user