mirror of https://github.com/ARMmbed/mbed-os.git
Review fixes
parent
9e2e2248f0
commit
6cd9af7070
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
#include "secure_time_test_utils.h"
|
#include "secure_time_test_utils.h"
|
||||||
#include "secure_time_client_spe.h"
|
#include "secure_time_client_spe.h"
|
||||||
|
#include "secure_time_utils.h"
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,15 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enumeration for the possible blob signature algorithms
|
||||||
|
*/
|
||||||
|
typedef enum signature_alg {
|
||||||
|
SIGNATURE_ALG_INVALID = 0, /**< Invalid algorithm type */
|
||||||
|
SIGNATURE_ALG_SHA256_ECDSA = 1, /**< ECDSA on a SHA256 hash */
|
||||||
|
SIGNATURE_ALG_MAX = SIGNATURE_ALG_SHA256_ECDSA
|
||||||
|
} SignatureAlg;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Verify the data buffer signature.
|
* Verify the data buffer signature.
|
||||||
*
|
*
|
||||||
|
|
|
@ -22,17 +22,20 @@
|
||||||
#include "mbedtls/entropy.h"
|
#include "mbedtls/entropy.h"
|
||||||
#include "mbedtls/ctr_drbg.h"
|
#include "mbedtls/ctr_drbg.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#if SECURE_TIME_ENABLED
|
#if SECURE_TIME_ENABLED
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Structure containing contexts for random number generation.
|
* Structure containing contexts for random number generation.
|
||||||
*/
|
*/
|
||||||
typedef struct secure_time_random_ctx {
|
typedef struct secure_time_random_ctx {
|
||||||
|
bool initialized;
|
||||||
mbedtls_ctr_drbg_context ctr_drbg_ctx; /* CTR_DRBG context structure. */
|
mbedtls_ctr_drbg_context ctr_drbg_ctx; /* CTR_DRBG context structure. */
|
||||||
mbedtls_entropy_context entropy_ctx; /* Entropy context structure. */
|
mbedtls_entropy_context entropy_ctx; /* Entropy context structure. */
|
||||||
} secure_time_random_ctx_t;
|
} secure_time_random_ctx_t;
|
||||||
|
|
||||||
secure_time_random_ctx_t *random_ctx = NULL;
|
static secure_time_random_ctx_t random_ctx = {0};
|
||||||
|
|
||||||
static void random_ctx_init(secure_time_random_ctx_t *ctx)
|
static void random_ctx_init(secure_time_random_ctx_t *ctx)
|
||||||
{
|
{
|
||||||
|
@ -85,7 +88,7 @@ static void calculate_hash(
|
||||||
error("mbedtls_md_info_from_type() returned NULL!");
|
error("mbedtls_md_info_from_type() returned NULL!");
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mbedtls_md(md_info, (const unsigned char *)data, data_size, hash);
|
rc = mbedtls_md(md_info, data, data_size, hash);
|
||||||
if (SECURE_TIME_SUCCESS != rc) {
|
if (SECURE_TIME_SUCCESS != rc) {
|
||||||
error("mbedtls_md() failed! (rc=%d)", rc);
|
error("mbedtls_md() failed! (rc=%d)", rc);
|
||||||
}
|
}
|
||||||
|
@ -101,7 +104,7 @@ int32_t secure_time_verify_signature(
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int rc = SECURE_TIME_SUCCESS;
|
int rc = SECURE_TIME_SUCCESS;
|
||||||
uint8_t hash[MBEDTLS_MD_MAX_SIZE] = {0};
|
uint8_t hash[MBEDTLS_MD_MAX_SIZE];
|
||||||
mbedtls_pk_context pubkey_ctx = {0};
|
mbedtls_pk_context pubkey_ctx = {0};
|
||||||
|
|
||||||
mbedtls_md_type_t md_type = md_type_from_signature_alg(SIGNATURE_ALG_SHA256_ECDSA);
|
mbedtls_md_type_t md_type = md_type_from_signature_alg(SIGNATURE_ALG_SHA256_ECDSA);
|
||||||
|
@ -122,14 +125,7 @@ int32_t secure_time_verify_signature(
|
||||||
error("Unable to verify signature");
|
error("Unable to verify signature");
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mbedtls_pk_verify(
|
rc = mbedtls_pk_verify(&pubkey_ctx, md_type, hash, 0, sign, sign_size);
|
||||||
&pubkey_ctx,
|
|
||||||
md_type,
|
|
||||||
hash,
|
|
||||||
0,
|
|
||||||
(const unsigned char *)sign,
|
|
||||||
sign_size
|
|
||||||
);
|
|
||||||
if (SECURE_TIME_SUCCESS != rc) {
|
if (SECURE_TIME_SUCCESS != rc) {
|
||||||
rc = SECURE_TIME_SIGNATURE_VERIFICATION_FAILED;
|
rc = SECURE_TIME_SIGNATURE_VERIFICATION_FAILED;
|
||||||
}
|
}
|
||||||
|
@ -142,15 +138,12 @@ void secure_time_generate_random_bytes(size_t size, void *random_buf)
|
||||||
{
|
{
|
||||||
int rc = SECURE_TIME_SUCCESS;
|
int rc = SECURE_TIME_SUCCESS;
|
||||||
|
|
||||||
if (NULL == random_ctx) {
|
if (false == random_ctx.initialized) {
|
||||||
random_ctx = (secure_time_random_ctx_t *)malloc(sizeof(*random_ctx));
|
random_ctx_init(&random_ctx);
|
||||||
if (NULL == random_ctx) {
|
random_ctx.initialized = true;
|
||||||
error("Failed to allocate memory for random_ctx!");
|
|
||||||
}
|
|
||||||
random_ctx_init(random_ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mbedtls_ctr_drbg_random(&(random_ctx->ctr_drbg_ctx), (unsigned char *)random_buf, size);
|
rc = mbedtls_ctr_drbg_random(&(random_ctx.ctr_drbg_ctx), random_buf, size);
|
||||||
if (SECURE_TIME_SUCCESS != rc) {
|
if (SECURE_TIME_SUCCESS != rc) {
|
||||||
error("mbedtls_ctr_drbg_random() failed! (rc=%d)", rc);
|
error("mbedtls_ctr_drbg_random() failed! (rc=%d)", rc);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
},
|
},
|
||||||
"target_overrides": {
|
"target_overrides": {
|
||||||
"NUCLEO_F410RB": {
|
"NUCLEO_F410RB": {
|
||||||
"enabled": 0
|
"enabled": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,51 +38,6 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**< Maximal allowed blob size in bytes. */
|
|
||||||
#define SECURE_TIME_MAX_BLOB_SIZE_BYTES (10 * 1024UL)
|
|
||||||
|
|
||||||
/**< Timestamp size in bytes. */
|
|
||||||
#define SECURE_TIME_TIMESTAMP_SIZE_BYTES (8UL)
|
|
||||||
|
|
||||||
/**< Nonce size in bytes. */
|
|
||||||
#define SECURE_TIME_NONCE_SIZE_BYTES (8UL)
|
|
||||||
|
|
||||||
/**< The size of delegation length in bytes. */
|
|
||||||
#define SECURE_TIME_DELEGATION_LENGTH_SIZE_BYTES (2UL)
|
|
||||||
|
|
||||||
/**< The size of public key length field in bytes. */
|
|
||||||
#define SECURE_TIME_PUBKEY_LENGTH_SIZE_BYTES (2UL)
|
|
||||||
|
|
||||||
/**< The size of signature length field in bytes. */
|
|
||||||
#define SECURE_TIME_SIGNATURE_LENGTH_SIZE_BYTES (2UL)
|
|
||||||
|
|
||||||
/**< The size of public key length field in bytes. */
|
|
||||||
#define SECURE_TIME_PUBKEY_LENGTH_SIZE_BYTES (2UL)
|
|
||||||
|
|
||||||
/**< The size of the constant length blob header. */
|
|
||||||
#define SECURE_TIME_BLOB_HEADER_SIZE_BYTES \
|
|
||||||
( \
|
|
||||||
SECURE_TIME_TIMESTAMP_SIZE_BYTES + \
|
|
||||||
SECURE_TIME_NONCE_SIZE_BYTES + \
|
|
||||||
SECURE_TIME_DELEGATION_LENGTH_SIZE_BYTES \
|
|
||||||
)
|
|
||||||
|
|
||||||
/**< The location of the delegation length field in the blob. */
|
|
||||||
#define SECURE_TIME_DELEGATION_LENGTH_OFFSET \
|
|
||||||
( \
|
|
||||||
SECURE_TIME_TIMESTAMP_SIZE_BYTES + \
|
|
||||||
SECURE_TIME_NONCE_SIZE_BYTES \
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enumeration for the possible blob signature algorithms
|
|
||||||
*/
|
|
||||||
typedef enum signature_alg {
|
|
||||||
SIGNATURE_ALG_INVALID = 0, /**< Invalid algorithm type */
|
|
||||||
SIGNATURE_ALG_SHA256_ECDSA = 1, /**< ECDSA on a SHA256 hash */
|
|
||||||
SIGNATURE_ALG_MAX = SIGNATURE_ALG_SHA256_ECDSA
|
|
||||||
} SignatureAlg;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory-setup provisioning of public key to be used by secure_time_set_trusted().
|
* Factory-setup provisioning of public key to be used by secure_time_set_trusted().
|
||||||
* Defined as a weak function which by default tries to write the public key to NVStore.
|
* Defined as a weak function which by default tries to write the public key to NVStore.
|
||||||
|
@ -94,7 +49,7 @@ typedef enum signature_alg {
|
||||||
* @param[in] key_size Size in bytes of public key.
|
* @param[in] key_size Size in bytes of public key.
|
||||||
* @return 0 or negative error code if failed.
|
* @return 0 or negative error code if failed.
|
||||||
*/
|
*/
|
||||||
int32_t secure_time_set_stored_public_key(const void* pubkey, size_t key_size);
|
int32_t secure_time_set_stored_public_key(const void *pubkey, size_t key_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the previously-provisioned public key.
|
* Return the previously-provisioned public key.
|
||||||
|
@ -107,7 +62,7 @@ int32_t secure_time_set_stored_public_key(const void* pubkey, size_t key_size);
|
||||||
* @param[out] actual_size Actual size in bytes of the returned public key.
|
* @param[out] actual_size Actual size in bytes of the returned public key.
|
||||||
* @return 0 or negative error code if failed.
|
* @return 0 or negative error code if failed.
|
||||||
*/
|
*/
|
||||||
int32_t secure_time_get_stored_public_key(uint8_t *pubkey, size_t size, size_t *actual_size);
|
int32_t secure_time_get_stored_public_key(void *pubkey, size_t size, size_t *actual_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the size in bytes of the previously-provisioned public key.
|
* Return the size in bytes of the previously-provisioned public key.
|
||||||
|
|
|
@ -31,7 +31,7 @@ int32_t secure_time_set_stored_public_key(const void* pubkey, size_t key_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t secure_time_get_stored_public_key(
|
int32_t secure_time_get_stored_public_key(
|
||||||
uint8_t *pubkey,
|
void *pubkey,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t *actual_size
|
size_t *actual_size
|
||||||
)
|
)
|
||||||
|
|
|
@ -66,7 +66,7 @@ static void invalidate_nonce(nonce_data_t *nonce_data)
|
||||||
nonce_data->nonce = 0;
|
nonce_data->nonce = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_nonce_valid(nonce_data_t *nonce)
|
static bool is_nonce_valid(const nonce_data_t *nonce)
|
||||||
{
|
{
|
||||||
return (SECURE_TIME_NONCE_GENERATION_TIME_INVALID != nonce->generation_time);
|
return (SECURE_TIME_NONCE_GENERATION_TIME_INVALID != nonce->generation_time);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ uint64_t secure_time_get_impl(void);
|
||||||
int32_t secure_time_set_stored_public_key_impl(const void* pubkey, size_t key_size);
|
int32_t secure_time_set_stored_public_key_impl(const void* pubkey, size_t key_size);
|
||||||
|
|
||||||
int32_t secure_time_get_stored_public_key_impl(
|
int32_t secure_time_get_stored_public_key_impl(
|
||||||
uint8_t *pubkey,
|
void *pubkey,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t *actual_size
|
size_t *actual_size
|
||||||
);
|
);
|
||||||
|
|
|
@ -22,6 +22,42 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*< Maximal allowed blob size in bytes. */
|
||||||
|
#define SECURE_TIME_MAX_BLOB_SIZE_BYTES (10 * 1024UL)
|
||||||
|
|
||||||
|
/*< Timestamp size in bytes. */
|
||||||
|
#define SECURE_TIME_TIMESTAMP_SIZE_BYTES (8UL)
|
||||||
|
|
||||||
|
/*< Nonce size in bytes. */
|
||||||
|
#define SECURE_TIME_NONCE_SIZE_BYTES (8UL)
|
||||||
|
|
||||||
|
/*< The size of delegation length in bytes. */
|
||||||
|
#define SECURE_TIME_DELEGATION_LENGTH_SIZE_BYTES (2UL)
|
||||||
|
|
||||||
|
/*< The size of public key length field in bytes. */
|
||||||
|
#define SECURE_TIME_PUBKEY_LENGTH_SIZE_BYTES (2UL)
|
||||||
|
|
||||||
|
/*< The size of signature length field in bytes. */
|
||||||
|
#define SECURE_TIME_SIGNATURE_LENGTH_SIZE_BYTES (2UL)
|
||||||
|
|
||||||
|
/*< The size of public key length field in bytes. */
|
||||||
|
#define SECURE_TIME_PUBKEY_LENGTH_SIZE_BYTES (2UL)
|
||||||
|
|
||||||
|
/*< The size of the constant length blob header. */
|
||||||
|
#define SECURE_TIME_BLOB_HEADER_SIZE_BYTES \
|
||||||
|
( \
|
||||||
|
SECURE_TIME_TIMESTAMP_SIZE_BYTES + \
|
||||||
|
SECURE_TIME_NONCE_SIZE_BYTES + \
|
||||||
|
SECURE_TIME_DELEGATION_LENGTH_SIZE_BYTES \
|
||||||
|
)
|
||||||
|
|
||||||
|
/*< The location of the delegation length field in the blob. */
|
||||||
|
#define SECURE_TIME_DELEGATION_LENGTH_OFFSET \
|
||||||
|
( \
|
||||||
|
SECURE_TIME_TIMESTAMP_SIZE_BYTES + \
|
||||||
|
SECURE_TIME_NONCE_SIZE_BYTES \
|
||||||
|
)
|
||||||
|
|
||||||
#define SECURE_TIME_MIN_RTC_LATENCY_SEC (100UL)
|
#define SECURE_TIME_MIN_RTC_LATENCY_SEC (100UL)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -33,7 +33,7 @@ MBED_WEAK int32_t secure_time_set_stored_public_key_impl(const void* pubkey, siz
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
MBED_WEAK int32_t secure_time_get_stored_public_key_impl(uint8_t *pubkey, size_t size, size_t *actual_size)
|
MBED_WEAK int32_t secure_time_get_stored_public_key_impl(void *pubkey, size_t size, size_t *actual_size)
|
||||||
{
|
{
|
||||||
if (NULL == pubkey) {
|
if (NULL == pubkey) {
|
||||||
error("pubkey is NULL!");
|
error("pubkey is NULL!");
|
||||||
|
|
Loading…
Reference in New Issue