Skip to content

Commit

Permalink
Merge pull request #10 from renesas/FSPRA-940-mbedtls-3-4-0
Browse files Browse the repository at this point in the history
Fspra 940 mbedtls 3 4 0
  • Loading branch information
michaelthomasj authored May 10, 2023
2 parents 1873d3b + cb729ff commit bde9358
Show file tree
Hide file tree
Showing 22 changed files with 920 additions and 110 deletions.
2 changes: 1 addition & 1 deletion include/mbedtls/build_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#endif

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/mbedtls_config.h"
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/psa/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ psa_status_t psa_hash_compare(psa_algorithm_t alg,
const uint8_t *input,
size_t input_length,
const uint8_t *hash,
size_t hash_length);
const size_t hash_length);

/** The type of the state data structure for multipart hash operations.
*
Expand Down Expand Up @@ -1259,7 +1259,7 @@ psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
const uint8_t *input,
size_t input_length,
const uint8_t *mac,
size_t mac_length);
const size_t mac_length);

/** The type of the state data structure for multipart MAC operations.
*
Expand Down
169 changes: 169 additions & 0 deletions include/psa/crypto_accel_driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* \file psa/crypto_accel_driver.h
* \brief PSA cryptography accelerator driver module
*
* This header declares types and function signatures for cryptography
* drivers that access key material directly. This is meant for
* on-chip cryptography accelerators.
*
* This file is part of the PSA Crypto Driver Model, containing functions for
* driver developers to implement to enable hardware to be called in a
* standardized way by a PSA Cryptographic API implementation. The functions
* comprising the driver model, which driver authors implement, are not
* intended to be called by application developers.
*/

/*
* 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_ACCEL_DRIVER_H
#define PSA_CRYPTO_ACCEL_DRIVER_H

#include "crypto_driver_common.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Import vendor defined key data into a slot.
*
* `slot->type` must have been set previously.
* This function assumes that the slot does not contain any key material yet.
* On failure, the slot content is unchanged.
*
* Persistent storage is not affected.
*
* \param[in,out] slot The key slot to import data into.
* Its `type` field must have previously been set to
* the desired key type.
* It must not contain any key material yet.
* \param[in] data Buffer containing the key material to parse and import.
* \param data_length Size of \p data in bytes.
* \param write_to_persistent_memory Specify if the imported key needs to be written to persistent memory.
*
* \retval PSA_SUCCESS
* \retval PSA_ERROR_INVALID_ARGUMENT
* \retval PSA_ERROR_NOT_SUPPORTED
* \retval PSA_ERROR_INSUFFICIENT_MEMORY
* \retval Implementation dependent
*/
psa_status_t psa_import_key_into_slot_vendor(const psa_key_attributes_t * attributes,
psa_key_slot_t * slot,
const uint8_t * data,
size_t data_length,
mbedtls_svc_key_id_t * key,
bool write_to_persistent_memory);

/**
* \brief Generate a vendor defined key or key pair.
*
* \note This function has to be defined by the vendor if MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C
* is defined. Do not use this function directly;
* to generate a key, use psa_generate_key() instead.
*
* \param[in] slot
* \param[in] bits
* \param[in] domain_parameters
* \param[in] domain_parameters_size
*
*
* \retval #PSA_SUCCESS
* Success.
* If the key is persistent, the key material and the key's metadata
* have been saved to persistent storage.
*
* \retval #PSA_ERROR_NOT_SUPPORTED
* \retval Implementation dependent.
*/
psa_status_t psa_generate_key_vendor(psa_key_slot_t * slot,
size_t bits,
const uint8_t * domain_parameters,
size_t domain_parameters_size);

/**
* \brief Generate symmetric key of vendor defined format.
*
* \warning This function **can** fail! Callers MUST check the return status
* and MUST NOT use the content of the output buffer if the return
* status is not #PSA_SUCCESS.
*
* \note This function has to be defined by the vendor if MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C
* is defined.
* A weakly linked version is provided by default and returns
* PSA_ERROR_NOT_SUPPORTED. Do not use this function directly;
* to generate a key, use psa_generate_key() instead.
*
* \param[in] type Type of symmetric key to be generated.
* \param[out] output Output buffer for the generated data.
* \param[out] output_size Number of bytes to generate and output.
*
* \retval #PSA_SUCCESS
* \retval #PSA_ERROR_NOT_SUPPORTED
* \retval Implementation dependent
*/
psa_status_t psa_generate_symmetric_vendor(psa_key_type_t type, size_t bits, uint8_t * output, size_t output_size);

/** Finalize the creation of a vendor defined key once its key material has been set.
*
* This entails writing the key to persistent storage.
*
* This function is to be called only by psa_finish_key_creation().
*
* \param[in,out] slot Pointer to the slot with key material.
*
* \retval #PSA_SUCCESS
* The key was successfully created. The handle is now valid.
* \return If this function fails, the key slot is an invalid state.
*/
psa_status_t psa_finish_key_creation_vendor(psa_key_slot_t * slot);

/**
* \brief Perform vendor specific setup for cipher operations.
*
*
* \note This function has to be defined by the vendor if MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C
* is defined.
* A weakly linked version is provided by default and returns
* PSA_ERROR_NOT_SUPPORTED. Do not use this function directly;
* to generate a key, use psa_generate_key() instead.
*
* \param[in,out] operation The operation object to set up. It must have
* been initialized as per the documentation for
* #psa_cipher_operation_t and not yet in use.
* \param handle Handle to the key to use for the operation.
* It must remain valid until the operation
* terminates.
* \param alg The cipher algorithm to compute
* (\c PSA_ALG_XXX value such that
* #PSA_ALG_IS_CIPHER(\p alg) is true).
*
* \retval #PSA_SUCCESS
* Success.
* \retval #PSA_ERROR_NOT_SUPPORTED
* .
*/
psa_status_t psa_cipher_setup_vendor(psa_cipher_operation_t * operation,
psa_key_slot_t * slot,
psa_algorithm_t alg,
mbedtls_operation_t cipher_operation);

/**@}*/

#ifdef __cplusplus
}
#endif

#endif /* PSA_CRYPTO_ACCEL_DRIVER_H */
2 changes: 1 addition & 1 deletion include/psa/crypto_sizes.h
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@
*/
#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
(PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
(key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
PSA_KEY_TYPE_IS_RSA_KEY_PAIR (key_type) ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
(key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
(key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
(key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Expand Down
38 changes: 30 additions & 8 deletions include/psa/crypto_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@
*/
#define PSA_KEY_TYPE_AES ((psa_key_type_t) 0x2400)

/** Whether a key type is AES; plaintext or wrapped. */
#define PSA_KEY_TYPE_IS_AES(type) ((((type) == PSA_KEY_TYPE_AES) != 0) || \
(((type) == (PSA_KEY_TYPE_VENDOR_FLAG | PSA_KEY_TYPE_AES)) != 0))

/** Key for a cipher, AEAD or MAC algorithm based on the
* ARIA block cipher. */
#define PSA_KEY_TYPE_ARIA ((psa_key_type_t) 0x2406)
Expand Down Expand Up @@ -546,10 +550,17 @@
*
* The size of an RSA key is the bit size of the modulus.
*/
#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t) 0x7001)
#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)

/** Whether a key type is an RSA key pair; standard or vendor. */
#define PSA_KEY_TYPE_IS_RSA_KEY_PAIR(type) \
((type == PSA_KEY_TYPE_RSA_KEY_PAIR) || \
(type == (PSA_KEY_TYPE_RSA_KEY_PAIR | PSA_KEY_TYPE_VENDOR_FLAG)))

/** Whether a key type is an RSA key (pair or public-only). */
#define PSA_KEY_TYPE_IS_RSA(type) \
(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) || \
(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == (PSA_KEY_TYPE_RSA_PUBLIC_KEY | PSA_KEY_TYPE_VENDOR_FLAG)))

#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4100)
#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t) 0x7100)
Expand Down Expand Up @@ -579,16 +590,22 @@

/** Whether a key type is an elliptic curve key (pair or public-only). */
#define PSA_KEY_TYPE_IS_ECC(type) \
((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
(((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) || \
((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
~PSA_KEY_TYPE_ECC_CURVE_MASK) == (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | PSA_KEY_TYPE_VENDOR_FLAG)))
/** Whether a key type is an elliptic curve key pair. */
#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
(((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
((((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
PSA_KEY_TYPE_ECC_KEY_PAIR_BASE) || \
(((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
(PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | PSA_KEY_TYPE_VENDOR_FLAG)))
/** Whether a key type is an elliptic curve public key. */
#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
(((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
((((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) || \
(((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
(PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | PSA_KEY_TYPE_VENDOR_FLAG)))

/** Extract the curve from an elliptic curve key type. */
#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
Expand Down Expand Up @@ -2301,6 +2318,11 @@
*/
#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t) 0x00000001)

#define PSA_KEY_LIFETIME_IS_PERSISTENT(lifetime) \
(((lifetime) & PSA_KEY_LIFETIME_PERSISTENT) != 0)

#define PSA_KEY_LIFETIME_VENDOR_FLAG ((psa_key_lifetime_t)0x80000000)

/** The persistence level of volatile keys.
*
* See ::psa_key_persistence_t for more information.
Expand Down
2 changes: 1 addition & 1 deletion library/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "common.h"

#if defined(MBEDTLS_CIPHER_C)
#if defined(MBEDTLS_CIPHER_C) && !defined(MBEDTLS_CIPHER_ALT)

#include "mbedtls/cipher.h"
#include "cipher_wrap.h"
Expand Down
6 changes: 3 additions & 3 deletions library/constant_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ unsigned mbedtls_ct_size_bool_eq(size_t x,
return 1 ^ diff1;
}

#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C)

/** Constant-flow "greater than" comparison:
* return x > y
Expand Down Expand Up @@ -413,7 +413,7 @@ signed char mbedtls_ct_base64_dec_value(unsigned char c)

#endif /* MBEDTLS_BASE64_C */

#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C)

/** Shift some data towards the left inside a buffer.
*
Expand Down Expand Up @@ -910,7 +910,7 @@ int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,

#endif /* MBEDTLS_BIGNUM_C */

#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C)

int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
size_t ilen,
Expand Down
2 changes: 1 addition & 1 deletion library/ctr_drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "common.h"

#if defined(MBEDTLS_CTR_DRBG_C)
#if !defined(MBEDTLS_CTR_DRBG_C_ALT)

#include "mbedtls/ctr_drbg.h"
#include "mbedtls/platform_util.h"
Expand Down
10 changes: 10 additions & 0 deletions library/ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@

#include <string.h>

#if !defined(MBEDTLS_ECDH_ALT)

/* Parameter validation macros based on platform_util.h */
#define ECDH_VALIDATE_RET( cond ) \
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
#define ECDH_VALIDATE( cond ) \
MBEDTLS_INTERNAL_VALIDATE( cond )

#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
#endif
Expand Down Expand Up @@ -694,4 +702,6 @@ int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
}
#endif
}

#endif /* !MBEDTLS_ECDH_ALT */
#endif /* MBEDTLS_ECDH_C */
8 changes: 6 additions & 2 deletions library/entropy.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
#include <stdio.h>
#endif

#if defined(MBEDTLS_ENTROPY_NV_SEED) || defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#endif

#include "mbedtls/platform.h"

#if defined(MBEDTLS_SELF_TEST) && !defined(MBEDTLS_PLATFORM_C)
#include <stdio.h>
#define mbedtls_printf printf
#endif /* MBEDTLS_SELF_TEST */

#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */

Expand Down
4 changes: 2 additions & 2 deletions library/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ static void platform_free_uninit(void *ptr)
#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
#endif /* !MBEDTLS_PLATFORM_STD_FREE */

static void * (*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
static void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
void * (*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;

void *mbedtls_calloc(size_t nmemb, size_t size)
{
Expand Down
2 changes: 1 addition & 1 deletion library/platform_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
*/
#if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !defined(__STDC_LIB_EXT1__) \
&& !defined(_WIN32)
static void *(*const volatile memset_func)(void *, int, size_t) = memset;
void *(*const volatile memset_func)(void *, int, size_t) = memset;
#endif

void mbedtls_platform_zeroize(void *buf, size_t len)
Expand Down
Loading

0 comments on commit bde9358

Please sign in to comment.