Import Mbed OS hard-float snapshot
This commit is contained in:
66
connectivity/mbedtls/platform/inc/platform_alt.h
Normal file
66
connectivity/mbedtls/platform/inc/platform_alt.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* platform_alt.h
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __PLATFORM_ALT__
|
||||
#define __PLATFORM_ALT__
|
||||
#include "platform_mbed.h"
|
||||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
||||
#include "crypto_device_platform.h"
|
||||
/**
|
||||
* \brief The platform context structure.
|
||||
*
|
||||
* \note This structure may be used to assist platform-specific
|
||||
* setup or teardown operations.
|
||||
*/
|
||||
typedef struct {
|
||||
crypto_platform_ctx platform_impl_ctx; /* A context holding all the platform specific context for cryptography. Should be defined in crypto_platform.h */
|
||||
int reference_count;
|
||||
}
|
||||
mbedtls_platform_context;
|
||||
|
||||
|
||||
/**
|
||||
* \brief This function performs any platform initialization operations,
|
||||
* needed for setting up cryptographic modules.
|
||||
*
|
||||
* \param ctx The platform specific context.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
*
|
||||
* \note This function is intended to allow platform-specific initialization for Mbed TLS,
|
||||
* and is called before initializing the Mbed TLS functions. Its
|
||||
* implementation is platform-specific, and its implementation MUST be provided.
|
||||
*
|
||||
*/
|
||||
int crypto_platform_setup( crypto_platform_ctx *unused_ctx );
|
||||
|
||||
/**
|
||||
* \brief This function performs any platform teardown operations, to disable cryptographic operations.
|
||||
*
|
||||
* \param ctx The platform specific context.
|
||||
*
|
||||
* \note This function is intended to free any resource used Mbed TLS by the platform.
|
||||
* Its implementation is platform-specific,and its implementation MUST be provided.
|
||||
*
|
||||
*/
|
||||
void crypto_platform_terminate( crypto_platform_ctx *unused_ctx );
|
||||
#endif
|
||||
#endif /* __PLATFORM_ALT__ */
|
||||
|
||||
68
connectivity/mbedtls/platform/inc/platform_mbed.h
Normal file
68
connectivity/mbedtls/platform/inc/platform_mbed.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (C) 2006-2016, 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 __PLATFORM_MBED__H__
|
||||
#define __PLATFORM_MBED__H__
|
||||
|
||||
#if (defined(FEATURE_PSA) && defined(MBEDTLS_ENTROPY_NV_SEED))
|
||||
|
||||
#include "default_random_seed.h"
|
||||
|
||||
#if !defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO)
|
||||
#define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO mbed_default_seed_read
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
|
||||
#define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO mbed_default_seed_write
|
||||
#endif
|
||||
|
||||
/* Automatically enable the Mbed Crypto entropy injection API if
|
||||
* MBEDTLS_ENTROPY_NV_SEED is enabled. */
|
||||
#define MBEDTLS_PSA_INJECT_ENTROPY
|
||||
|
||||
#endif // (defined(FEATURE_PSA) && defined(MBEDTLS_ENTROPY_NV_SEED))
|
||||
|
||||
#if DEVICE_TRNG
|
||||
#define MBEDTLS_ENTROPY_HARDWARE_ALT
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
|
||||
#include "mbedtls_device.h"
|
||||
#endif
|
||||
|
||||
#if defined(FEATURE_PSA)
|
||||
/* The following configurations are a needed for Mbed Crypto submodule.
|
||||
* They are related to the persistent key storage feature.
|
||||
*/
|
||||
#define MBEDTLS_PSA_CRYPTO_STORAGE_C
|
||||
#define MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C
|
||||
#undef MBEDTLS_PSA_CRYPTO_STORAGE_FILE_C
|
||||
|
||||
#define MBEDTLS_ENTROPY_HARDWARE_ALT
|
||||
#endif
|
||||
|
||||
/*
|
||||
* MBEDTLS_ERR_PLATFORM_HW_FAILED is deprecated and should not be used.
|
||||
*/
|
||||
#define MBEDTLS_ERR_PLATFORM_HW_FAILED -0x0080
|
||||
|
||||
#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
|
||||
|
||||
#endif // __PLATFORM_MBED__H__
|
||||
93
connectivity/mbedtls/platform/inc/shared_rng.h
Normal file
93
connectivity/mbedtls/platform/inc/shared_rng.h
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* shared_rng.h
|
||||
*
|
||||
* Copyright (C) 2019-2020, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SHARED_RNG_H
|
||||
#define SHARED_RNG_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_CONF_RNG)
|
||||
|
||||
#define MBED_SHARED_RNG_NOT_INITIALIZED -1 /**< init_global_rng not called before global_rng */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "mbedtls/hmac_drbg.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
|
||||
/**
|
||||
* \brief Initializes hmac ready for rng
|
||||
*
|
||||
* \return 0 if successful, or
|
||||
* MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
|
||||
* MBEDTLS_ERR_MD_ALLOC_FAILED, or
|
||||
* MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
|
||||
*/
|
||||
int init_global_rng();
|
||||
|
||||
/**
|
||||
* \brief Global HMAC_DRBG generate random
|
||||
*
|
||||
* \note Automatically reseeds if reseed_counter is reached or PR is enabled.
|
||||
* \note init_global_rng function must be called
|
||||
* before calling this function!
|
||||
*
|
||||
* \param ctx DRBG context
|
||||
* \param dst Buffer to fill
|
||||
* \param len Length of the buffer
|
||||
*
|
||||
* \return 0 if successful, or
|
||||
* MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
|
||||
* MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG or
|
||||
* MBED_SHARED_RNG_NOT_INITIALIZED
|
||||
*/
|
||||
int global_rng( void *ctx, unsigned char *dst, size_t len );
|
||||
|
||||
/**
|
||||
* \brief Free allocated resources
|
||||
*/
|
||||
void free_global_rng();
|
||||
|
||||
/**
|
||||
* \brief Getter function for global hmac context
|
||||
*
|
||||
* \return global hmac context
|
||||
*/
|
||||
mbedtls_hmac_drbg_context *get_global_hmac_drbg();
|
||||
|
||||
/**
|
||||
* \brief Getter function for global entropy context
|
||||
*
|
||||
* \return global entropy context
|
||||
*/
|
||||
mbedtls_entropy_context *get_global_entropy();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MBEDTLS_SSL_CONF_RNG
|
||||
#endif // SHARED_RNG_H
|
||||
43
connectivity/mbedtls/platform/src/mbed_trng.cpp
Normal file
43
connectivity/mbedtls/platform/src/mbed_trng.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2016 ARM Limited
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if defined(DEVICE_TRNG) || defined(FEATURE_PSA)
|
||||
|
||||
#include "hal/trng_api.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
SingletonPtr<PlatformMutex> mbedtls_mutex;
|
||||
|
||||
extern "C"
|
||||
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) {
|
||||
#if defined(DEVICE_TRNG)
|
||||
trng_t trng_obj;
|
||||
mbedtls_mutex->lock();
|
||||
trng_init(&trng_obj);
|
||||
int ret = trng_get_bytes(&trng_obj, output, len, olen);
|
||||
trng_free(&trng_obj);
|
||||
mbedtls_mutex->unlock();
|
||||
return ret;
|
||||
#else /* defined(FEATURE_PSA) */
|
||||
mbedtls_mutex->lock();
|
||||
int ret = trng_get_bytes(NULL, output, len, olen);
|
||||
mbedtls_mutex->unlock();
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
57
connectivity/mbedtls/platform/src/platform_alt.cpp
Normal file
57
connectivity/mbedtls/platform/src/platform_alt.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* platform_alt.c
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
mbedtls_platform_context plat_ctx = { { 0 } };
|
||||
extern SingletonPtr<PlatformMutex> mbedtls_mutex;
|
||||
|
||||
int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx )
|
||||
{
|
||||
int ret = 0;
|
||||
mbedtls_mutex->lock();
|
||||
++plat_ctx.reference_count;
|
||||
|
||||
if( plat_ctx.reference_count == 1 )
|
||||
{
|
||||
/* call platform specific code to setup crypto driver */
|
||||
ret = crypto_platform_setup( &plat_ctx.platform_impl_ctx );
|
||||
}
|
||||
mbedtls_mutex->unlock();
|
||||
return ( ret );
|
||||
}
|
||||
|
||||
void mbedtls_platform_teardown( mbedtls_platform_context *unused_ctx )
|
||||
{
|
||||
mbedtls_mutex->lock();
|
||||
--plat_ctx.reference_count;
|
||||
if( plat_ctx.reference_count < 1 )
|
||||
{
|
||||
/* call platform specific code to terminate crypto driver */
|
||||
crypto_platform_terminate( &plat_ctx.platform_impl_ctx );
|
||||
plat_ctx.reference_count = 0;
|
||||
}
|
||||
mbedtls_mutex->unlock();
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/
|
||||
77
connectivity/mbedtls/platform/src/shared_rng.cpp
Normal file
77
connectivity/mbedtls/platform/src/shared_rng.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* shared_rng.cpp
|
||||
*
|
||||
* Copyright (C) 2019-2020, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "shared_rng.h"
|
||||
|
||||
#if defined(MBEDTLS_SSL_CONF_RNG)
|
||||
|
||||
#include "mbed_trace.h"
|
||||
|
||||
#define TRACE_GROUP "SRNG"
|
||||
|
||||
mbedtls_hmac_drbg_context global_hmac_drbg;
|
||||
mbedtls_entropy_context global_entropy;
|
||||
static bool is_initialized = false;
|
||||
|
||||
int init_global_rng()
|
||||
{
|
||||
mbedtls_entropy_init(&global_entropy);
|
||||
mbedtls_hmac_drbg_init(&global_hmac_drbg);
|
||||
|
||||
int ret = mbedtls_hmac_drbg_seed(&global_hmac_drbg,
|
||||
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
|
||||
mbedtls_entropy_func, &global_entropy, NULL, 0);
|
||||
|
||||
if (ret != 0) {
|
||||
tr_error(" init_global_rng failed! mbedtls_hmac_drbg_seed returned -0x%x", -ret);
|
||||
free_global_rng();
|
||||
} else {
|
||||
is_initialized = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void free_global_rng()
|
||||
{
|
||||
mbedtls_entropy_free(&global_entropy);
|
||||
mbedtls_hmac_drbg_free(&global_hmac_drbg);
|
||||
is_initialized = false;
|
||||
}
|
||||
|
||||
int global_rng( void *ctx, unsigned char *dst, size_t len )
|
||||
{
|
||||
if (!is_initialized) {
|
||||
return MBED_SHARED_RNG_NOT_INITIALIZED;
|
||||
}
|
||||
return mbedtls_hmac_drbg_random(&global_hmac_drbg, dst, len);
|
||||
}
|
||||
|
||||
mbedtls_hmac_drbg_context *get_global_hmac_drbg()
|
||||
{
|
||||
return &global_hmac_drbg;
|
||||
}
|
||||
|
||||
mbedtls_entropy_context *get_global_entropy()
|
||||
{
|
||||
return &global_entropy;
|
||||
}
|
||||
|
||||
#endif // MBEDTLS_SSL_CONF_RNG
|
||||
Reference in New Issue
Block a user