2018-01-27 22:32:46 +00:00
|
|
|
/*
|
|
|
|
* PSA crypto layer on top of Mbed TLS crypto
|
|
|
|
*/
|
2020-06-15 09:59:37 +00:00
|
|
|
/*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2018-01-27 22:32:46 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-02 23:43:33 +00:00
|
|
|
#include "common.h"
|
2018-01-27 22:32:46 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
2018-07-03 10:16:15 +00:00
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
|
|
|
|
#include "check_crypto_config.h"
|
|
|
|
#endif
|
|
|
|
|
2019-02-14 08:28:02 +00:00
|
|
|
#include "psa_crypto_service_integration.h"
|
2018-01-27 22:32:46 +00:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2018-12-07 17:24:41 +00:00
|
|
|
#include "psa_crypto_core.h"
|
2018-11-20 20:59:56 +00:00
|
|
|
#include "psa_crypto_invasive.h"
|
2020-07-16 18:28:36 +00:00
|
|
|
#include "psa_crypto_driver_wrappers.h"
|
2020-11-25 14:25:26 +00:00
|
|
|
#include "psa_crypto_ecp.h"
|
|
|
|
#include "psa_crypto_rsa.h"
|
2020-11-20 17:17:42 +00:00
|
|
|
#include "psa_crypto_ecp.h"
|
2019-06-26 09:24:49 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-06-24 12:34:43 +00:00
|
|
|
#include "psa_crypto_se.h"
|
2019-06-26 09:24:49 +00:00
|
|
|
#endif
|
2018-11-30 17:54:54 +00:00
|
|
|
#include "psa_crypto_slot_management.h"
|
2018-06-18 16:27:26 +00:00
|
|
|
/* Include internal declarations that are useful for implementing persistently
|
|
|
|
* stored keys. */
|
|
|
|
#include "psa_crypto_storage.h"
|
|
|
|
|
2020-12-14 15:43:58 +00:00
|
|
|
#include "psa_crypto_random_impl.h"
|
2020-11-13 14:39:19 +00:00
|
|
|
|
2019-07-30 19:32:04 +00:00
|
|
|
#include <assert.h>
|
2018-01-28 12:16:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mbedtls/platform.h"
|
2019-05-06 13:26:23 +00:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_C)
|
2018-01-28 12:16:24 +00:00
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#endif
|
|
|
|
|
2020-11-13 17:46:25 +00:00
|
|
|
#include "mbedtls/aes.h"
|
2018-02-07 19:59:33 +00:00
|
|
|
#include "mbedtls/arc4.h"
|
2018-06-21 07:35:35 +00:00
|
|
|
#include "mbedtls/asn1.h"
|
2019-01-10 10:23:21 +00:00
|
|
|
#include "mbedtls/asn1write.h"
|
2018-06-26 14:10:23 +00:00
|
|
|
#include "mbedtls/bignum.h"
|
2018-02-07 19:59:33 +00:00
|
|
|
#include "mbedtls/blowfish.h"
|
|
|
|
#include "mbedtls/camellia.h"
|
2019-05-06 13:25:00 +00:00
|
|
|
#include "mbedtls/chacha20.h"
|
|
|
|
#include "mbedtls/chachapoly.h"
|
2018-02-07 19:59:33 +00:00
|
|
|
#include "mbedtls/cipher.h"
|
|
|
|
#include "mbedtls/ccm.h"
|
|
|
|
#include "mbedtls/cmac.h"
|
|
|
|
#include "mbedtls/des.h"
|
2018-09-18 10:11:27 +00:00
|
|
|
#include "mbedtls/ecdh.h"
|
2018-01-28 17:16:59 +00:00
|
|
|
#include "mbedtls/ecp.h"
|
2018-01-27 22:32:46 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
2018-02-07 19:59:33 +00:00
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/gcm.h"
|
|
|
|
#include "mbedtls/md2.h"
|
|
|
|
#include "mbedtls/md4.h"
|
|
|
|
#include "mbedtls/md5.h"
|
2018-02-03 21:44:14 +00:00
|
|
|
#include "mbedtls/md.h"
|
|
|
|
#include "mbedtls/md_internal.h"
|
2018-01-28 12:16:24 +00:00
|
|
|
#include "mbedtls/pk.h"
|
2018-01-28 17:16:59 +00:00
|
|
|
#include "mbedtls/pk_internal.h"
|
2018-12-07 17:14:53 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 13:21:35 +00:00
|
|
|
#include "mbedtls/error.h"
|
2018-02-07 19:59:33 +00:00
|
|
|
#include "mbedtls/ripemd160.h"
|
2018-01-28 17:16:59 +00:00
|
|
|
#include "mbedtls/rsa.h"
|
2018-02-07 19:59:33 +00:00
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
#include "mbedtls/sha512.h"
|
|
|
|
#include "mbedtls/xtea.h"
|
|
|
|
|
2018-08-01 13:45:45 +00:00
|
|
|
#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
|
|
|
|
|
2018-02-07 20:05:37 +00:00
|
|
|
/* constant-time buffer comparison */
|
|
|
|
static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned char diff = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
diff |= a[i] ^ b[i];
|
|
|
|
|
|
|
|
return( diff );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-28 12:16:24 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Global data, support functions and library management */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2018-06-21 12:15:31 +00:00
|
|
|
static int key_type_is_raw_bytes( psa_key_type_t type )
|
|
|
|
{
|
2018-08-10 14:03:41 +00:00
|
|
|
return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
|
2018-06-21 12:15:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 11:27:09 +00:00
|
|
|
/* Values for psa_global_data_t::rng_state */
|
|
|
|
#define RNG_NOT_INITIALIZED 0
|
|
|
|
#define RNG_INITIALIZED 1
|
|
|
|
#define RNG_SEEDED 2
|
2018-11-20 20:42:52 +00:00
|
|
|
|
2018-06-18 13:41:12 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_random_context_t rng;
|
2018-11-20 20:42:52 +00:00
|
|
|
unsigned initialized : 1;
|
2018-12-04 11:27:09 +00:00
|
|
|
unsigned rng_state : 2;
|
2018-01-27 22:32:46 +00:00
|
|
|
} psa_global_data_t;
|
|
|
|
|
|
|
|
static psa_global_data_t global_data;
|
|
|
|
|
2020-12-14 13:54:06 +00:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
|
|
|
|
&global_data.rng.drbg;
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 13:24:41 +00:00
|
|
|
#define GUARD_MODULE_INITIALIZED \
|
|
|
|
if( global_data.initialized == 0 ) \
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2020-07-20 13:31:37 +00:00
|
|
|
psa_status_t mbedtls_to_psa_error( int ret )
|
2018-01-27 22:32:46 +00:00
|
|
|
{
|
2021-01-06 19:04:23 +00:00
|
|
|
/* Mbed TLS error codes can combine a high-level error code and a
|
|
|
|
* low-level error code. The low-level error usually reflects the
|
|
|
|
* root cause better, so dispatch on that preferably. */
|
|
|
|
int low_level_ret = - ( -ret & 0x007f );
|
|
|
|
switch( low_level_ret != 0 ? low_level_ret : ret )
|
2018-01-27 22:32:46 +00:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return( PSA_SUCCESS );
|
2018-02-07 19:59:33 +00:00
|
|
|
|
|
|
|
case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
|
|
|
|
case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
|
|
|
|
case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2018-06-21 07:35:35 +00:00
|
|
|
case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
|
|
|
|
case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
|
|
|
|
case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
|
|
|
|
case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
|
|
|
|
case MBEDTLS_ERR_ASN1_INVALID_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
|
2019-02-20 13:57:28 +00:00
|
|
|
#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
|
2019-02-11 12:21:12 +00:00
|
|
|
case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
|
2019-02-20 13:57:28 +00:00
|
|
|
#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
|
|
|
|
case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
|
|
|
|
#endif
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2019-02-20 13:57:28 +00:00
|
|
|
#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
|
2019-02-11 12:21:12 +00:00
|
|
|
case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
|
2019-02-20 13:57:28 +00:00
|
|
|
#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
|
|
|
|
case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
|
|
|
|
#endif
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_CCM_BAD_INPUT:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_CCM_AUTH_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2019-05-06 13:25:00 +00:00
|
|
|
case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
|
|
|
|
return( PSA_ERROR_INVALID_PADDING );
|
|
|
|
case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
|
2020-09-28 14:11:33 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
|
2019-05-16 19:35:18 +00:00
|
|
|
return( PSA_ERROR_CORRUPTION_DETECTED );
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2020-11-13 20:31:17 +00:00
|
|
|
#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
|
|
|
|
defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
|
2020-11-23 20:00:09 +00:00
|
|
|
/* Only check CTR_DRBG error codes if underlying mbedtls_xxx
|
|
|
|
* functions are passed a CTR_DRBG instance. */
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
2020-11-13 20:31:17 +00:00
|
|
|
#endif
|
2018-02-07 19:59:33 +00:00
|
|
|
|
|
|
|
case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2018-01-27 22:32:46 +00:00
|
|
|
case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
|
|
|
|
case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
|
|
|
|
case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
2018-02-07 19:59:33 +00:00
|
|
|
|
|
|
|
case MBEDTLS_ERR_GCM_AUTH_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
case MBEDTLS_ERR_GCM_BAD_INPUT:
|
2018-08-21 13:07:38 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2020-11-13 20:31:17 +00:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
|
|
|
|
defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
2020-11-23 20:00:09 +00:00
|
|
|
/* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
|
|
|
|
* functions are passed a HMAC_DRBG instance. */
|
2020-11-13 20:31:17 +00:00
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
|
|
|
|
case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
|
|
|
|
case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_MD_ALLOC_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
case MBEDTLS_ERR_MD_FILE_IO_ERROR:
|
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
|
|
|
case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2018-10-29 18:24:33 +00:00
|
|
|
case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
|
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
|
|
|
case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_MPI_ALLOC_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
|
2018-01-28 12:16:24 +00:00
|
|
|
case MBEDTLS_ERR_PK_ALLOC_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
case MBEDTLS_ERR_PK_TYPE_MISMATCH:
|
|
|
|
case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_PK_FILE_IO_ERROR:
|
2018-02-07 19:59:33 +00:00
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
2018-01-28 12:16:24 +00:00
|
|
|
case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
|
|
|
|
case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
|
|
|
|
case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
|
|
|
|
return( PSA_ERROR_NOT_PERMITTED );
|
|
|
|
case MBEDTLS_ERR_PK_INVALID_PUBKEY:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_PK_INVALID_ALG:
|
|
|
|
case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
|
|
|
|
case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2019-05-06 13:26:23 +00:00
|
|
|
case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_RSA_INVALID_PADDING:
|
|
|
|
return( PSA_ERROR_INVALID_PADDING );
|
|
|
|
case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
|
|
|
|
case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
|
2019-05-16 19:35:18 +00:00
|
|
|
return( PSA_ERROR_CORRUPTION_DETECTED );
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_RSA_VERIFY_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
case MBEDTLS_ERR_RSA_RNG_FAILED:
|
2020-11-24 23:09:47 +00:00
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
2018-02-07 19:59:33 +00:00
|
|
|
case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
|
|
|
|
case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
|
|
|
|
case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
|
|
|
|
2018-05-08 08:18:38 +00:00
|
|
|
case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
|
2018-05-09 13:07:36 +00:00
|
|
|
case MBEDTLS_ERR_ECP_INVALID_KEY:
|
2018-05-08 08:18:38 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-05-09 13:07:36 +00:00
|
|
|
case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
|
|
|
|
case MBEDTLS_ERR_ECP_VERIFY_FAILED:
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
case MBEDTLS_ERR_ECP_ALLOC_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
2020-11-24 23:09:47 +00:00
|
|
|
case MBEDTLS_ERR_ECP_RANDOM_FAILED:
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
2018-05-09 13:07:36 +00:00
|
|
|
case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
|
|
|
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
2020-11-24 23:09:47 +00:00
|
|
|
|
2019-11-22 12:48:59 +00:00
|
|
|
case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
|
|
|
|
return( PSA_ERROR_CORRUPTION_DETECTED );
|
2018-05-08 08:18:38 +00:00
|
|
|
|
2018-01-27 22:32:46 +00:00
|
|
|
default:
|
2019-02-14 11:48:10 +00:00
|
|
|
return( PSA_ERROR_GENERIC_ERROR );
|
2018-01-27 22:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:01:38 +00:00
|
|
|
|
2018-06-18 19:49:39 +00:00
|
|
|
|
|
|
|
|
2018-01-28 12:16:24 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Key management */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2019-07-12 21:44:37 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
|
|
|
|
{
|
2019-07-30 18:06:31 +00:00
|
|
|
return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
|
2019-07-12 21:44:37 +00:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
|
|
|
|
* current test driver in key_management.c is using this function
|
|
|
|
* when accelerators are used for ECC key pair and public key.
|
|
|
|
* Once that dependency is resolved these guards can be removed.
|
|
|
|
*/
|
2020-11-04 20:28:15 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
2020-11-10 16:50:04 +00:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
|
|
|
defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
|
|
|
|
defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
|
2020-06-02 16:19:28 +00:00
|
|
|
mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
|
2021-01-27 14:44:45 +00:00
|
|
|
size_t bits,
|
|
|
|
int bits_is_sloppy )
|
2018-06-19 22:20:32 +00:00
|
|
|
{
|
|
|
|
switch( curve )
|
|
|
|
{
|
2020-06-02 16:19:28 +00:00
|
|
|
case PSA_ECC_FAMILY_SECP_R1:
|
2021-01-27 14:44:45 +00:00
|
|
|
switch( bits )
|
2019-12-03 16:24:19 +00:00
|
|
|
{
|
2021-01-27 14:44:45 +00:00
|
|
|
case 192:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP192R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 224:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP224R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 256:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP256R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 384:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP384R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 521:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP521R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 528:
|
|
|
|
if( bits_is_sloppy )
|
|
|
|
return( MBEDTLS_ECP_DP_SECP521R1 );
|
|
|
|
break;
|
2019-12-03 16:24:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-02 16:19:28 +00:00
|
|
|
case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
|
2021-01-27 14:44:45 +00:00
|
|
|
switch( bits )
|
2019-12-03 16:24:19 +00:00
|
|
|
{
|
2021-01-27 14:44:45 +00:00
|
|
|
case 256:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_BP256R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 384:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_BP384R1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 512:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_BP512R1 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-02 16:19:28 +00:00
|
|
|
case PSA_ECC_FAMILY_MONTGOMERY:
|
2021-01-27 14:44:45 +00:00
|
|
|
switch( bits )
|
2019-12-03 16:24:19 +00:00
|
|
|
{
|
2021-01-27 14:44:45 +00:00
|
|
|
case 255:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_CURVE25519 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 256:
|
|
|
|
if( bits_is_sloppy )
|
|
|
|
return( MBEDTLS_ECP_DP_CURVE25519 );
|
|
|
|
break;
|
|
|
|
case 448:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_CURVE448 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-02 16:19:28 +00:00
|
|
|
case PSA_ECC_FAMILY_SECP_K1:
|
2021-01-27 14:44:45 +00:00
|
|
|
switch( bits )
|
2019-12-03 16:24:19 +00:00
|
|
|
{
|
2021-01-27 14:44:45 +00:00
|
|
|
case 192:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP192K1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 224:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP224K1 );
|
2021-01-27 14:44:45 +00:00
|
|
|
case 256:
|
2019-12-03 16:24:19 +00:00
|
|
|
return( MBEDTLS_ECP_DP_SECP256K1 );
|
|
|
|
}
|
|
|
|
break;
|
2018-06-19 22:20:32 +00:00
|
|
|
}
|
2021-01-27 14:44:45 +00:00
|
|
|
|
|
|
|
return( MBEDTLS_ECP_DP_NONE );
|
2018-06-19 22:20:32 +00:00
|
|
|
}
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
2020-11-10 16:50:04 +00:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
|
|
|
* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
|
2018-06-19 22:20:32 +00:00
|
|
|
|
2020-07-24 20:04:59 +00:00
|
|
|
static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
|
|
|
|
size_t bits )
|
2018-06-19 19:31:50 +00:00
|
|
|
{
|
|
|
|
/* Check that the bit size is acceptable for the key type */
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case PSA_KEY_TYPE_RAW_DATA:
|
|
|
|
case PSA_KEY_TYPE_HMAC:
|
2018-07-12 15:17:20 +00:00
|
|
|
case PSA_KEY_TYPE_DERIVE:
|
|
|
|
break;
|
2021-01-26 18:39:16 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
|
2018-06-19 19:31:50 +00:00
|
|
|
case PSA_KEY_TYPE_AES:
|
|
|
|
if( bits != 128 && bits != 192 && bits != 256 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-01-26 18:39:16 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
|
2018-06-19 19:31:50 +00:00
|
|
|
case PSA_KEY_TYPE_CAMELLIA:
|
|
|
|
if( bits != 128 && bits != 192 && bits != 256 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-01-26 18:39:16 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2018-06-19 19:31:50 +00:00
|
|
|
case PSA_KEY_TYPE_DES:
|
|
|
|
if( bits != 64 && bits != 128 && bits != 192 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
break;
|
|
|
|
#endif
|
2021-01-26 18:39:16 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
|
2018-06-19 19:31:50 +00:00
|
|
|
case PSA_KEY_TYPE_ARC4:
|
|
|
|
if( bits < 8 || bits > 2048 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
break;
|
2019-05-06 13:25:00 +00:00
|
|
|
#endif
|
2021-02-16 19:54:35 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
|
2019-05-06 13:25:00 +00:00
|
|
|
case PSA_KEY_TYPE_CHACHA20:
|
|
|
|
if( bits != 256 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
break;
|
2018-06-19 19:31:50 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
2018-06-21 07:32:47 +00:00
|
|
|
if( bits % 8 != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-06-19 19:31:50 +00:00
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-07-30 19:32:04 +00:00
|
|
|
/** Return the size of the key in the given slot, in bits.
|
|
|
|
*
|
|
|
|
* \param[in] slot A key slot.
|
|
|
|
*
|
|
|
|
* \return The key size in bits, read from the metadata in the slot.
|
|
|
|
*/
|
|
|
|
static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
|
|
|
|
{
|
|
|
|
return( slot->attr.bits );
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:30:13 +00:00
|
|
|
/** Try to allocate a buffer to an empty key slot.
|
|
|
|
*
|
|
|
|
* \param[in,out] slot Key slot to attach buffer to.
|
|
|
|
* \param[in] buffer_length Requested size of the buffer.
|
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
|
|
|
* The buffer has been successfully allocated.
|
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
|
|
|
* Not enough memory was available for allocation.
|
|
|
|
* \retval #PSA_ERROR_ALREADY_EXISTS
|
|
|
|
* Trying to allocate a buffer to a non-empty key slot.
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
|
|
|
|
size_t buffer_length )
|
|
|
|
{
|
2020-11-25 16:52:23 +00:00
|
|
|
if( slot->key.data != NULL )
|
2020-08-05 13:43:42 +00:00
|
|
|
return( PSA_ERROR_ALREADY_EXISTS );
|
2020-07-28 12:30:13 +00:00
|
|
|
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.data = mbedtls_calloc( 1, buffer_length );
|
|
|
|
if( slot->key.data == NULL )
|
2020-08-05 13:43:42 +00:00
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
2020-07-28 12:30:13 +00:00
|
|
|
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.bytes = buffer_length;
|
2020-08-05 13:43:42 +00:00
|
|
|
return( PSA_SUCCESS );
|
2020-07-28 12:30:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:27:40 +00:00
|
|
|
psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
|
|
|
psa_status_t status = psa_allocate_buffer_to_slot( slot,
|
|
|
|
data_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-11-25 16:52:23 +00:00
|
|
|
memcpy( slot->key.data, data, data_length );
|
2020-10-13 18:27:40 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2020-11-29 10:14:53 +00:00
|
|
|
psa_status_t psa_import_key_into_slot(
|
2020-11-28 14:54:54 +00:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *data, size_t data_length,
|
|
|
|
uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
size_t *key_buffer_length, size_t *bits )
|
2018-01-28 12:16:24 +00:00
|
|
|
{
|
2020-11-28 14:54:54 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_key_type_t type = attributes->core.type;
|
2018-01-28 12:16:24 +00:00
|
|
|
|
2020-07-24 20:04:59 +00:00
|
|
|
/* zero-length keys are never supported. */
|
|
|
|
if( data_length == 0 )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
2020-11-28 14:54:54 +00:00
|
|
|
if( key_type_is_raw_bytes( type ) )
|
2018-01-28 12:16:24 +00:00
|
|
|
{
|
2020-11-28 14:54:54 +00:00
|
|
|
*bits = PSA_BYTES_TO_BITS( data_length );
|
2020-07-24 20:04:59 +00:00
|
|
|
|
2020-07-28 12:30:13 +00:00
|
|
|
/* Ensure that the bytes-to-bits conversion hasn't overflown. */
|
|
|
|
if( data_length > SIZE_MAX / 8 )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
2019-08-07 08:59:45 +00:00
|
|
|
/* Enforce a size limit, and in particular ensure that the bit
|
|
|
|
* size fits in its representation type. */
|
2020-11-28 14:54:54 +00:00
|
|
|
if( ( *bits ) > PSA_MAX_KEY_BITS )
|
2019-07-30 15:26:54 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2020-07-24 20:04:59 +00:00
|
|
|
|
2020-11-28 14:54:54 +00:00
|
|
|
status = validate_unstructured_key_bit_size( type, *bits );
|
2018-06-19 19:31:50 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-08-05 13:43:42 +00:00
|
|
|
return( status );
|
2020-07-24 20:04:59 +00:00
|
|
|
|
2020-11-28 14:14:42 +00:00
|
|
|
/* Copy the key material. */
|
|
|
|
memcpy( key_buffer, data, data_length );
|
|
|
|
*key_buffer_length = data_length;
|
|
|
|
(void)key_buffer_size;
|
2020-07-24 20:04:59 +00:00
|
|
|
|
2020-10-29 10:42:22 +00:00
|
|
|
return( PSA_SUCCESS );
|
2018-01-28 12:16:24 +00:00
|
|
|
}
|
2020-11-28 14:54:54 +00:00
|
|
|
else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
|
2019-01-10 11:42:27 +00:00
|
|
|
{
|
2020-11-04 20:28:15 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
2020-11-28 14:54:54 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_ECC( type ) )
|
2020-10-23 09:37:05 +00:00
|
|
|
{
|
2020-11-28 14:54:54 +00:00
|
|
|
return( mbedtls_psa_ecp_import_key( attributes,
|
|
|
|
data, data_length,
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
key_buffer_length,
|
|
|
|
bits ) );
|
2020-10-23 09:37:05 +00:00
|
|
|
}
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2020-11-28 14:54:54 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_RSA( type ) )
|
2020-10-23 09:37:05 +00:00
|
|
|
{
|
2020-11-28 14:54:54 +00:00
|
|
|
return( mbedtls_psa_rsa_import_key( attributes,
|
|
|
|
data, data_length,
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
key_buffer_length,
|
|
|
|
bits ) );
|
2020-10-23 09:37:05 +00:00
|
|
|
}
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2018-01-28 12:16:24 +00:00
|
|
|
}
|
2020-11-28 14:54:54 +00:00
|
|
|
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2018-07-16 10:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 12:40:11 +00:00
|
|
|
/** Calculate the intersection of two algorithm usage policies.
|
|
|
|
*
|
|
|
|
* Return 0 (which allows no operation) on incompatibility.
|
|
|
|
*/
|
|
|
|
static psa_algorithm_t psa_key_policy_algorithm_intersection(
|
|
|
|
psa_algorithm_t alg1,
|
|
|
|
psa_algorithm_t alg2 )
|
|
|
|
{
|
2019-05-22 09:45:59 +00:00
|
|
|
/* Common case: both sides actually specify the same policy. */
|
2019-01-19 12:40:11 +00:00
|
|
|
if( alg1 == alg2 )
|
|
|
|
return( alg1 );
|
|
|
|
/* If the policies are from the same hash-and-sign family, check
|
|
|
|
* if one is a wildcard. If so the other has the specific algorithm. */
|
|
|
|
if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
|
|
|
|
PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
|
|
|
|
( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
|
|
|
|
{
|
|
|
|
if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
|
|
|
|
return( alg2 );
|
|
|
|
if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
|
|
|
|
return( alg1 );
|
|
|
|
}
|
|
|
|
/* If the policies are incompatible, allow nothing. */
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:33:38 +00:00
|
|
|
static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
|
|
|
|
psa_algorithm_t requested_alg )
|
2019-01-14 15:06:39 +00:00
|
|
|
{
|
2019-05-22 09:45:59 +00:00
|
|
|
/* Common case: the policy only allows requested_alg. */
|
2019-05-10 17:33:38 +00:00
|
|
|
if( requested_alg == policy_alg )
|
2019-01-14 15:06:39 +00:00
|
|
|
return( 1 );
|
2019-05-10 17:33:38 +00:00
|
|
|
/* If policy_alg is a hash-and-sign with a wildcard for the hash,
|
2019-05-22 09:45:59 +00:00
|
|
|
* and requested_alg is the same hash-and-sign family with any hash,
|
|
|
|
* then requested_alg is compliant with policy_alg. */
|
2019-05-10 17:33:38 +00:00
|
|
|
if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
|
|
|
|
PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
|
2019-01-14 15:06:39 +00:00
|
|
|
{
|
2019-05-10 17:33:38 +00:00
|
|
|
return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
|
|
|
|
( requested_alg & ~PSA_ALG_HASH_MASK ) );
|
2019-01-14 15:06:39 +00:00
|
|
|
}
|
2020-10-05 14:02:45 +00:00
|
|
|
/* If policy_alg is a generic key agreement operation, then using it for
|
2020-10-15 15:07:12 +00:00
|
|
|
* a key derivation with that key agreement should also be allowed. This
|
|
|
|
* behaviour is expected to be defined in a future specification version. */
|
2020-10-05 14:02:45 +00:00
|
|
|
if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
|
|
|
|
PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
|
|
|
|
{
|
|
|
|
return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
|
|
|
|
policy_alg );
|
|
|
|
}
|
2019-01-14 15:06:39 +00:00
|
|
|
/* If it isn't permitted, it's forbidden. */
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:33:38 +00:00
|
|
|
/** Test whether a policy permits an algorithm.
|
|
|
|
*
|
|
|
|
* The caller must test usage flags separately.
|
|
|
|
*/
|
|
|
|
static int psa_key_policy_permits( const psa_key_policy_t *policy,
|
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
|
|
|
return( psa_key_algorithm_permits( policy->alg, alg ) ||
|
|
|
|
psa_key_algorithm_permits( policy->alg2, alg ) );
|
|
|
|
}
|
|
|
|
|
2019-01-19 12:40:11 +00:00
|
|
|
/** Restrict a key policy based on a constraint.
|
|
|
|
*
|
|
|
|
* \param[in,out] policy The policy to restrict.
|
|
|
|
* \param[in] constraint The policy constraint to apply.
|
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
|
|
|
* \c *policy contains the intersection of the original value of
|
|
|
|
* \c *policy and \c *constraint.
|
|
|
|
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
|
|
|
* \c *policy and \c *constraint are incompatible.
|
|
|
|
* \c *policy is unchanged.
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_restrict_key_policy(
|
|
|
|
psa_key_policy_t *policy,
|
|
|
|
const psa_key_policy_t *constraint )
|
|
|
|
{
|
|
|
|
psa_algorithm_t intersection_alg =
|
|
|
|
psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
|
2019-05-10 17:33:38 +00:00
|
|
|
psa_algorithm_t intersection_alg2 =
|
|
|
|
psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
|
2019-01-19 12:40:11 +00:00
|
|
|
if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2019-05-10 17:33:38 +00:00
|
|
|
if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2019-01-19 12:40:11 +00:00
|
|
|
policy->usage &= constraint->usage;
|
|
|
|
policy->alg = intersection_alg;
|
2019-05-10 17:33:38 +00:00
|
|
|
policy->alg2 = intersection_alg2;
|
2019-01-19 12:40:11 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
/** Get the description of a key given its identifier and policy constraints
|
|
|
|
* and lock it.
|
2020-10-22 13:24:49 +00:00
|
|
|
*
|
2020-11-14 15:35:34 +00:00
|
|
|
* The key must have allow all the usage flags set in \p usage. If \p alg is
|
|
|
|
* nonzero, the key must allow operations with this algorithm.
|
2020-11-11 14:04:25 +00:00
|
|
|
*
|
2020-11-14 15:35:34 +00:00
|
|
|
* In case of a persistent key, the function loads the description of the key
|
|
|
|
* into a key slot if not already done.
|
|
|
|
*
|
|
|
|
* On success, the returned key slot is locked. It is the responsibility of
|
|
|
|
* the caller to unlock the key slot when it does not access it anymore.
|
2020-10-22 13:24:49 +00:00
|
|
|
*/
|
2020-11-14 15:35:34 +00:00
|
|
|
static psa_status_t psa_get_and_lock_key_slot_with_policy(
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
psa_key_slot_t **p_slot,
|
|
|
|
psa_key_usage_t usage,
|
|
|
|
psa_algorithm_t alg )
|
2018-07-16 10:21:11 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_key_slot_t *slot;
|
2018-07-16 10:21:11 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot( key, p_slot );
|
2018-07-16 10:21:11 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2020-10-22 13:24:49 +00:00
|
|
|
slot = *p_slot;
|
2018-07-16 10:21:11 +00:00
|
|
|
|
|
|
|
/* Enforce that usage policy for the key slot contains all the flags
|
|
|
|
* required by the usage parameter. There is one exception: public
|
|
|
|
* keys can always be exported, so we treat public key objects as
|
|
|
|
* if they had the export flag. */
|
2019-07-30 18:06:31 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
|
2018-07-16 10:21:11 +00:00
|
|
|
usage &= ~PSA_KEY_USAGE_EXPORT;
|
2020-10-22 13:24:49 +00:00
|
|
|
|
|
|
|
status = PSA_ERROR_NOT_PERMITTED;
|
2019-07-30 18:06:31 +00:00
|
|
|
if( ( slot->attr.policy.usage & usage ) != usage )
|
2020-10-22 13:24:49 +00:00
|
|
|
goto error;
|
2019-01-14 15:06:39 +00:00
|
|
|
|
|
|
|
/* Enforce that the usage policy permits the requested algortihm. */
|
2019-07-30 18:06:31 +00:00
|
|
|
if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
|
2020-10-22 13:24:49 +00:00
|
|
|
goto error;
|
2018-07-16 10:21:11 +00:00
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
*p_slot = NULL;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
|
|
|
return( status );
|
2018-07-16 10:21:11 +00:00
|
|
|
}
|
2018-07-13 12:18:51 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
/** Get a key slot containing a transparent key and lock it.
|
2019-07-24 11:30:31 +00:00
|
|
|
*
|
|
|
|
* A transparent key is a key for which the key material is directly
|
|
|
|
* available, as opposed to a key in a secure element.
|
|
|
|
*
|
2020-11-14 15:35:34 +00:00
|
|
|
* This is a temporary function to use instead of
|
|
|
|
* psa_get_and_lock_key_slot_with_policy() until secure element support is
|
|
|
|
* fully implemented.
|
2020-10-22 13:24:49 +00:00
|
|
|
*
|
2020-11-14 15:35:34 +00:00
|
|
|
* On success, the returned key slot is locked. It is the responsibility of the
|
|
|
|
* caller to unlock the key slot when it does not access it anymore.
|
2019-07-24 11:30:31 +00:00
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2020-11-14 15:35:34 +00:00
|
|
|
static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
psa_key_slot_t **p_slot,
|
|
|
|
psa_key_usage_t usage,
|
|
|
|
psa_algorithm_t alg )
|
2019-07-24 11:30:31 +00:00
|
|
|
{
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
|
|
|
|
usage, alg );
|
2019-07-24 11:30:31 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2019-07-25 09:31:23 +00:00
|
|
|
if( psa_key_slot_is_external( *p_slot ) )
|
2019-07-24 11:30:31 +00:00
|
|
|
{
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_unlock_key_slot( *p_slot );
|
2019-07-24 11:30:31 +00:00
|
|
|
*p_slot = NULL;
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2019-07-24 11:30:31 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
/* With no secure element support, all keys are transparent. */
|
2020-11-14 15:35:34 +00:00
|
|
|
#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
|
|
|
|
psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
|
2019-07-24 11:30:31 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2018-12-03 10:58:46 +00:00
|
|
|
/** Wipe key data from a slot. Preserve metadata such as the policy. */
|
2018-12-04 16:12:32 +00:00
|
|
|
static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
|
2018-07-13 12:18:51 +00:00
|
|
|
{
|
2020-11-25 16:52:23 +00:00
|
|
|
/* Data pointer will always be either a valid pointer or NULL in an
|
|
|
|
* initialized slot, so we can just free it. */
|
|
|
|
if( slot->key.data != NULL )
|
|
|
|
mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
|
|
|
|
|
|
|
|
mbedtls_free( slot->key.data );
|
|
|
|
slot->key.data = NULL;
|
|
|
|
slot->key.bytes = 0;
|
2018-01-28 12:16:24 +00:00
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2018-12-03 10:58:46 +00:00
|
|
|
/** Completely wipe a slot in memory, including its policy.
|
|
|
|
* Persistent storage is not affected. */
|
2018-12-10 15:29:04 +00:00
|
|
|
psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
|
2018-12-03 10:58:46 +00:00
|
|
|
{
|
|
|
|
psa_status_t status = psa_remove_key_data_from_memory( slot );
|
2020-10-30 13:07:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* As the return error code may not be handled in case of multiple errors,
|
2020-11-14 15:35:34 +00:00
|
|
|
* do our best to report an unexpected lock counter: if available
|
2020-10-30 13:07:07 +00:00
|
|
|
* call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
|
|
|
|
* part of the execution of a test suite this will stop the test suite
|
2020-11-13 09:11:01 +00:00
|
|
|
* execution).
|
2020-10-30 13:07:07 +00:00
|
|
|
*/
|
2020-11-14 15:35:34 +00:00
|
|
|
if( slot->lock_count != 1 )
|
2020-10-30 13:07:07 +00:00
|
|
|
{
|
|
|
|
#ifdef MBEDTLS_CHECK_PARAMS
|
2020-11-14 15:35:34 +00:00
|
|
|
MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
|
2020-10-30 13:07:07 +00:00
|
|
|
#endif
|
|
|
|
status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:01:08 +00:00
|
|
|
/* Multipart operations may still be using the key. This is safe
|
|
|
|
* because all multipart operation objects are independent from
|
|
|
|
* the key slot: if they need to access the key after the setup
|
|
|
|
* phase, they have a copy of the key. Note that this means that
|
|
|
|
* key material can linger until all operations are completed. */
|
2018-12-03 10:58:46 +00:00
|
|
|
/* At this point, key material and other type-specific content has
|
|
|
|
* been wiped. Clear remaining metadata. We can call memset and not
|
|
|
|
* zeroize because the metadata is not particularly sensitive. */
|
|
|
|
memset( slot, 0, sizeof( *slot ) );
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
|
2018-11-15 14:48:15 +00:00
|
|
|
{
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2019-08-13 13:58:36 +00:00
|
|
|
psa_status_t status; /* status of the last operation */
|
|
|
|
psa_status_t overall_status = PSA_SUCCESS;
|
2019-07-12 21:46:38 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
psa_se_drv_table_entry_t *driver;
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-11-15 14:48:15 +00:00
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
if( mbedtls_svc_key_id_is_null( key ) )
|
2019-10-08 13:48:25 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
2020-10-29 16:51:10 +00:00
|
|
|
/*
|
2020-11-10 17:08:03 +00:00
|
|
|
* Get the description of the key in a key slot. In case of a persistent
|
2020-10-29 16:51:10 +00:00
|
|
|
* key, this will load the key description from persistent memory if not
|
|
|
|
* done yet. We cannot avoid this loading as without it we don't know if
|
|
|
|
* the key is operated by an SE or not and this information is needed by
|
|
|
|
* the current implementation.
|
|
|
|
*/
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot( key, &slot );
|
2018-11-15 14:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-10-29 16:51:10 +00:00
|
|
|
/*
|
|
|
|
* If the key slot containing the key description is under access by the
|
|
|
|
* library (apart from the present access), the key cannot be destroyed
|
|
|
|
* yet. For the time being, just return in error. Eventually (to be
|
|
|
|
* implemented), the key should be destroyed when all accesses have
|
|
|
|
* stopped.
|
|
|
|
*/
|
2020-11-14 15:35:34 +00:00
|
|
|
if( slot->lock_count > 1 )
|
2020-10-29 16:51:10 +00:00
|
|
|
{
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_unlock_key_slot( slot );
|
2020-10-29 16:51:10 +00:00
|
|
|
return( PSA_ERROR_GENERIC_ERROR );
|
|
|
|
}
|
|
|
|
|
2019-07-12 21:46:38 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-07-30 18:06:31 +00:00
|
|
|
driver = psa_get_se_driver_entry( slot->attr.lifetime );
|
2019-07-12 21:46:38 +00:00
|
|
|
if( driver != NULL )
|
2019-07-22 17:30:34 +00:00
|
|
|
{
|
2019-07-25 09:32:45 +00:00
|
|
|
/* For a key in a secure element, we need to do three things:
|
|
|
|
* remove the key file in internal storage, destroy the
|
|
|
|
* key inside the secure element, and update the driver's
|
|
|
|
* persistent data. Start a transaction that will encompass these
|
|
|
|
* three actions. */
|
2019-07-22 17:30:34 +00:00
|
|
|
psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
|
2019-07-30 18:06:31 +00:00
|
|
|
psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
|
2020-11-25 16:52:23 +00:00
|
|
|
psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
|
2019-07-30 18:06:31 +00:00
|
|
|
psa_crypto_transaction.key.id = slot->attr.id;
|
2019-07-22 17:30:34 +00:00
|
|
|
status = psa_crypto_save_transaction( );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
2019-07-25 16:02:52 +00:00
|
|
|
(void) psa_crypto_stop_transaction( );
|
2019-08-13 13:58:36 +00:00
|
|
|
/* We should still try to destroy the key in the secure
|
|
|
|
* element and the key metadata in storage. This is especially
|
|
|
|
* important if the error is that the storage is full.
|
|
|
|
* But how to do it exactly without risking an inconsistent
|
|
|
|
* state after a reset?
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/215
|
|
|
|
*/
|
|
|
|
overall_status = status;
|
|
|
|
goto exit;
|
2019-07-22 17:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 16:52:23 +00:00
|
|
|
status = psa_destroy_se_key( driver,
|
|
|
|
psa_key_slot_get_slot_number( slot ) );
|
2019-08-13 13:58:36 +00:00
|
|
|
if( overall_status == PSA_SUCCESS )
|
|
|
|
overall_status = status;
|
2019-07-22 17:30:34 +00:00
|
|
|
}
|
2019-07-12 21:46:38 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-11-15 14:48:15 +00:00
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2020-10-23 16:00:55 +00:00
|
|
|
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
|
2018-11-15 14:48:15 +00:00
|
|
|
{
|
2019-08-13 13:58:36 +00:00
|
|
|
status = psa_destroy_persistent_key( slot->attr.id );
|
|
|
|
if( overall_status == PSA_SUCCESS )
|
|
|
|
overall_status = status;
|
|
|
|
|
2019-08-13 13:14:20 +00:00
|
|
|
/* TODO: other slots may have a copy of the same key. We should
|
|
|
|
* invalidate them.
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/214
|
|
|
|
*/
|
2018-11-15 14:48:15 +00:00
|
|
|
}
|
2018-06-18 16:27:26 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2018-11-15 14:48:15 +00:00
|
|
|
|
2019-07-22 17:30:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
if( driver != NULL )
|
2018-06-18 16:27:26 +00:00
|
|
|
{
|
2019-07-25 09:31:48 +00:00
|
|
|
status = psa_save_se_persistent_data( driver );
|
2019-08-13 13:58:36 +00:00
|
|
|
if( overall_status == PSA_SUCCESS )
|
|
|
|
overall_status = status;
|
|
|
|
status = psa_crypto_stop_transaction( );
|
|
|
|
if( overall_status == PSA_SUCCESS )
|
|
|
|
overall_status = status;
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
2019-07-22 17:30:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-08-13 13:58:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
exit:
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-12-03 10:58:46 +00:00
|
|
|
status = psa_wipe_key_slot( slot );
|
2019-08-13 13:58:36 +00:00
|
|
|
/* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
|
|
|
|
if( overall_status == PSA_SUCCESS )
|
|
|
|
overall_status = status;
|
|
|
|
return( overall_status );
|
2018-11-15 14:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2020-11-01 05:06:54 +00:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2019-04-26 14:06:02 +00:00
|
|
|
static psa_status_t psa_get_rsa_public_exponent(
|
|
|
|
const mbedtls_rsa_context *rsa,
|
|
|
|
psa_key_attributes_t *attributes )
|
|
|
|
{
|
|
|
|
mbedtls_mpi mpi;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-04-26 14:06:02 +00:00
|
|
|
uint8_t *buffer = NULL;
|
|
|
|
size_t buflen;
|
|
|
|
mbedtls_mpi_init( &mpi );
|
|
|
|
|
|
|
|
ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2019-04-26 15:37:21 +00:00
|
|
|
if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
|
|
|
|
{
|
|
|
|
/* It's the default value, which is reported as an empty string,
|
|
|
|
* so there's nothing to do. */
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-04-26 14:06:02 +00:00
|
|
|
|
|
|
|
buflen = mbedtls_mpi_size( &mpi );
|
|
|
|
buffer = mbedtls_calloc( 1, buflen );
|
|
|
|
if( buffer == NULL )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
|
|
|
attributes->domain_parameters = buffer;
|
|
|
|
attributes->domain_parameters_size = buflen;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &mpi );
|
|
|
|
if( ret != 0 )
|
|
|
|
mbedtls_free( buffer );
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
2018-07-06 14:02:09 +00:00
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
2020-11-04 20:28:15 +00:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2018-07-06 14:02:09 +00:00
|
|
|
|
2019-07-23 09:58:03 +00:00
|
|
|
/** Retrieve all the publicly-accessible attributes of a key.
|
|
|
|
*/
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
|
2019-04-18 19:44:46 +00:00
|
|
|
psa_key_attributes_t *attributes )
|
2018-01-28 12:16:24 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-01-28 12:16:24 +00:00
|
|
|
|
2019-04-18 19:44:46 +00:00
|
|
|
psa_reset_key_attributes( attributes );
|
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-07-06 14:02:09 +00:00
|
|
|
|
2019-07-31 12:15:34 +00:00
|
|
|
attributes->core = slot->attr;
|
2019-08-02 18:15:51 +00:00
|
|
|
attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
|
|
|
|
MBEDTLS_PSA_KA_MASK_DUAL_USE );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
if( psa_key_slot_is_external( slot ) )
|
2020-11-25 16:52:23 +00:00
|
|
|
psa_set_key_slot_number( attributes,
|
|
|
|
psa_key_slot_get_slot_number( slot ) );
|
2019-08-02 18:15:51 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-04-26 14:06:02 +00:00
|
|
|
|
2019-07-30 18:06:31 +00:00
|
|
|
switch( slot->attr.type )
|
2019-04-26 14:06:02 +00:00
|
|
|
{
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2020-11-01 05:06:54 +00:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2019-05-16 17:39:54 +00:00
|
|
|
case PSA_KEY_TYPE_RSA_KEY_PAIR:
|
2019-04-26 14:06:02 +00:00
|
|
|
case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
|
2019-07-24 17:09:30 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-13 11:15:34 +00:00
|
|
|
/* TODO: reporting the public exponent for opaque keys
|
2019-08-13 14:17:16 +00:00
|
|
|
* is not yet implemented.
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/216
|
|
|
|
*/
|
2019-08-02 18:15:51 +00:00
|
|
|
if( psa_key_slot_is_external( slot ) )
|
2019-07-24 17:09:30 +00:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2020-07-24 20:48:15 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_rsa_load_representation(
|
|
|
|
slot->attr.type,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
break;
|
|
|
|
|
2020-07-28 12:30:39 +00:00
|
|
|
status = psa_get_rsa_public_exponent( rsa,
|
2020-07-24 20:48:15 +00:00
|
|
|
attributes );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
mbedtls_free( rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
}
|
2019-04-26 14:06:02 +00:00
|
|
|
break;
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
2020-11-04 20:28:15 +00:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2019-04-26 14:06:02 +00:00
|
|
|
default:
|
|
|
|
/* Nothing else to do. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
psa_reset_key_attributes( attributes );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-01-28 12:16:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 18:15:51 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
psa_status_t psa_get_key_slot_number(
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
psa_key_slot_number_t *slot_number )
|
|
|
|
{
|
|
|
|
if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
|
|
|
|
{
|
|
|
|
*slot_number = attributes->slot_number;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2020-11-25 15:46:05 +00:00
|
|
|
static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
2020-07-24 20:48:15 +00:00
|
|
|
uint8_t *data,
|
|
|
|
size_t data_size,
|
|
|
|
size_t *data_length )
|
|
|
|
{
|
2020-11-25 15:46:05 +00:00
|
|
|
if( key_buffer_size > data_size )
|
2020-07-24 20:48:15 +00:00
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
2020-11-25 15:46:05 +00:00
|
|
|
memcpy( data, key_buffer, key_buffer_size );
|
|
|
|
memset( data + key_buffer_size, 0,
|
|
|
|
data_size - key_buffer_size );
|
|
|
|
*data_length = key_buffer_size;
|
2020-07-24 20:48:15 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2020-11-26 13:46:37 +00:00
|
|
|
psa_status_t psa_export_key_internal(
|
2020-11-25 15:46:05 +00:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
uint8_t *data, size_t data_size, size_t *data_length )
|
2018-01-28 12:16:24 +00:00
|
|
|
{
|
2020-11-25 15:46:05 +00:00
|
|
|
psa_key_type_t type = attributes->core.type;
|
|
|
|
|
|
|
|
if( key_type_is_raw_bytes( type ) ||
|
|
|
|
PSA_KEY_TYPE_IS_RSA( type ) ||
|
|
|
|
PSA_KEY_TYPE_IS_ECC( type ) )
|
2020-11-26 12:36:23 +00:00
|
|
|
{
|
|
|
|
return( psa_export_key_buffer_internal(
|
2020-11-25 15:46:05 +00:00
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
data, data_size, data_length ) );
|
2020-11-26 12:36:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This shouldn't happen in the reference implementation, but
|
|
|
|
it is valid for a special-purpose implementation to omit
|
|
|
|
support for exporting certain key types. */
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
|
|
|
|
uint8_t *data,
|
|
|
|
size_t data_size,
|
|
|
|
size_t *data_length )
|
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
/* Reject a zero-length output buffer now, since this can never be a
|
|
|
|
* valid key representation. This way we know that data must be a valid
|
|
|
|
* pointer and we can do things like memset(data, ..., data_size). */
|
|
|
|
if( data_size == 0 )
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
|
2020-11-26 12:36:23 +00:00
|
|
|
/* Set the key to empty now, so that even when there are errors, we always
|
|
|
|
* set data_length to a value between 0 and data_size. On error, setting
|
|
|
|
* the key to empty is a good choice because an empty key representation is
|
|
|
|
* unlikely to be accepted anywhere. */
|
2018-06-27 16:20:43 +00:00
|
|
|
*data_length = 0;
|
|
|
|
|
2020-11-26 12:36:23 +00:00
|
|
|
/* Export requires the EXPORT flag. There is an exception for public keys,
|
|
|
|
* which don't require any flag, but
|
|
|
|
* psa_get_and_lock_key_slot_with_policy() takes care of this.
|
|
|
|
*/
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy( key, &slot,
|
|
|
|
PSA_KEY_USAGE_EXPORT, 0 );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-03-28 10:17:44 +00:00
|
|
|
|
2020-11-25 15:46:05 +00:00
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
.core = slot->attr
|
|
|
|
};
|
2020-11-26 14:16:05 +00:00
|
|
|
status = psa_driver_wrapper_export_key( &attributes,
|
2020-11-25 15:46:05 +00:00
|
|
|
slot->key.data, slot->key.bytes,
|
|
|
|
data, data_size, data_length );
|
2020-11-26 12:36:23 +00:00
|
|
|
|
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
|
|
|
|
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
|
|
|
}
|
|
|
|
|
2020-11-26 13:46:37 +00:00
|
|
|
psa_status_t psa_export_public_key_internal(
|
2020-11-25 15:46:05 +00:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
uint8_t *data,
|
|
|
|
size_t data_size,
|
|
|
|
size_t *data_length )
|
2020-11-26 12:36:23 +00:00
|
|
|
{
|
2020-11-25 15:46:05 +00:00
|
|
|
psa_key_type_t type = attributes->core.type;
|
|
|
|
|
|
|
|
if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
|
2018-01-28 12:16:24 +00:00
|
|
|
{
|
2020-11-25 15:46:05 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
|
2020-07-24 21:20:24 +00:00
|
|
|
{
|
|
|
|
/* Exporting public -> public */
|
2020-11-26 12:36:23 +00:00
|
|
|
return( psa_export_key_buffer_internal(
|
2020-11-25 15:46:05 +00:00
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
data, data_size, data_length ) );
|
2020-07-24 21:20:24 +00:00
|
|
|
}
|
2020-10-14 12:39:20 +00:00
|
|
|
|
2020-11-25 15:46:05 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_RSA( type ) )
|
2018-01-28 17:16:59 +00:00
|
|
|
{
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2020-11-26 15:36:16 +00:00
|
|
|
return( mbedtls_psa_rsa_export_public_key( attributes,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
data,
|
|
|
|
data_size,
|
|
|
|
data_length ) );
|
2018-07-24 15:33:30 +00:00
|
|
|
#else
|
2020-07-24 21:20:24 +00:00
|
|
|
/* We don't know how to convert a private RSA key to public. */
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2020-07-24 21:20:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
2020-11-26 15:36:16 +00:00
|
|
|
return( mbedtls_psa_ecp_export_public_key( attributes,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
data,
|
|
|
|
data_size,
|
|
|
|
data_length ) );
|
2018-07-24 15:33:30 +00:00
|
|
|
#else
|
2020-07-24 21:20:24 +00:00
|
|
|
/* We don't know how to convert a private ECC key to public */
|
2018-04-16 15:16:20 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
|
2018-01-28 17:16:59 +00:00
|
|
|
}
|
2018-01-28 12:16:24 +00:00
|
|
|
}
|
2020-07-24 21:20:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This shouldn't happen in the reference implementation, but
|
|
|
|
it is valid for a special-purpose implementation to omit
|
|
|
|
support for exporting certain key types. */
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
2018-01-28 12:16:24 +00:00
|
|
|
}
|
2020-11-25 15:46:05 +00:00
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
|
2018-06-18 13:41:12 +00:00
|
|
|
uint8_t *data,
|
|
|
|
size_t data_size,
|
|
|
|
size_t *data_length )
|
2018-04-03 12:30:03 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-11-07 16:00:44 +00:00
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
/* Reject a zero-length output buffer now, since this can never be a
|
|
|
|
* valid key representation. This way we know that data must be a valid
|
|
|
|
* pointer and we can do things like memset(data, ..., data_size). */
|
|
|
|
if( data_size == 0 )
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
|
2018-11-07 16:00:44 +00:00
|
|
|
/* Set the key to empty now, so that even when there are errors, we always
|
|
|
|
* set data_length to a value between 0 and data_size. On error, setting
|
|
|
|
* the key to empty is a good choice because an empty key representation is
|
|
|
|
* unlikely to be accepted anywhere. */
|
|
|
|
*data_length = 0;
|
|
|
|
|
|
|
|
/* Exporting a public key doesn't require a usage flag. */
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
|
2018-11-07 16:00:44 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-26 12:36:23 +00:00
|
|
|
if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:46:05 +00:00
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
.core = slot->attr
|
|
|
|
};
|
2020-11-26 14:16:05 +00:00
|
|
|
status = psa_driver_wrapper_export_public_key(
|
2020-11-25 15:46:05 +00:00
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
data, data_size, data_length );
|
2020-11-26 12:36:23 +00:00
|
|
|
|
|
|
|
exit:
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-04-03 12:30:03 +00:00
|
|
|
}
|
2018-01-28 12:16:24 +00:00
|
|
|
|
2019-08-02 17:19:39 +00:00
|
|
|
#if defined(static_assert)
|
|
|
|
static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
|
|
|
|
"One or more key attribute flag is listed as both external-only and dual-use" );
|
2019-08-05 15:32:13 +00:00
|
|
|
static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
|
2019-08-07 16:19:46 +00:00
|
|
|
"One or more key attribute flag is listed as both internal-only and dual-use" );
|
2019-08-05 15:32:13 +00:00
|
|
|
static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
|
2019-08-02 17:19:39 +00:00
|
|
|
"One or more key attribute flag is listed as both internal-only and external-only" );
|
|
|
|
#endif
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
/** Validate that a key policy is internally well-formed.
|
|
|
|
*
|
|
|
|
* This function only rejects invalid policies. It does not validate the
|
|
|
|
* consistency of the policy with respect to other attributes of the key
|
|
|
|
* such as the key type.
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
|
2018-11-07 16:05:30 +00:00
|
|
|
{
|
2019-04-17 13:05:45 +00:00
|
|
|
if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
|
2019-05-14 12:24:28 +00:00
|
|
|
PSA_KEY_USAGE_COPY |
|
2019-04-17 13:05:45 +00:00
|
|
|
PSA_KEY_USAGE_ENCRYPT |
|
|
|
|
PSA_KEY_USAGE_DECRYPT |
|
2019-11-26 16:01:59 +00:00
|
|
|
PSA_KEY_USAGE_SIGN_HASH |
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH |
|
2019-04-17 13:05:45 +00:00
|
|
|
PSA_KEY_USAGE_DERIVE ) ) != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
2018-11-07 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
/** Validate the internal consistency of key attributes.
|
|
|
|
*
|
|
|
|
* This function only rejects invalid attribute values. If does not
|
|
|
|
* validate the consistency of the attributes with any key data that may
|
|
|
|
* be involved in the creation of the key.
|
|
|
|
*
|
|
|
|
* Call this function early in the key creation process.
|
|
|
|
*
|
|
|
|
* \param[in] attributes Key attributes for the new key.
|
|
|
|
* \param[out] p_drv On any return, the driver for the key, if any.
|
|
|
|
* NULL for a transparent key.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_validate_key_attributes(
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
psa_se_drv_table_entry_t **p_drv )
|
2019-01-19 12:40:11 +00:00
|
|
|
{
|
2020-06-08 16:37:19 +00:00
|
|
|
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
|
2020-07-17 14:11:30 +00:00
|
|
|
psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
|
2020-10-23 15:11:13 +00:00
|
|
|
mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
|
2019-01-19 12:40:11 +00:00
|
|
|
|
2020-10-29 14:26:43 +00:00
|
|
|
status = psa_validate_key_location( lifetime, p_drv );
|
2020-06-08 16:37:19 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-07-17 14:11:30 +00:00
|
|
|
status = psa_validate_key_persistence( lifetime );
|
2020-06-08 16:37:19 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2019-07-31 15:21:46 +00:00
|
|
|
|
2020-10-23 15:11:13 +00:00
|
|
|
if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
|
|
|
|
{
|
|
|
|
if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
else
|
2020-07-17 14:11:30 +00:00
|
|
|
{
|
2020-11-11 13:57:44 +00:00
|
|
|
status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
|
2020-07-17 14:11:30 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
status = psa_validate_key_policy( &attributes->core.policy );
|
2019-01-19 12:40:11 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-07-31 15:21:46 +00:00
|
|
|
return( status );
|
2019-01-19 12:40:11 +00:00
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
/* Refuse to create overly large keys.
|
|
|
|
* Note that this doesn't trigger on import if the attributes don't
|
|
|
|
* explicitly specify a size (so psa_get_key_bits returns 0), so
|
|
|
|
* psa_import_key() needs its own checks. */
|
|
|
|
if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
2019-08-02 17:19:39 +00:00
|
|
|
/* Reject invalid flags. These should not be reachable through the API. */
|
|
|
|
if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
|
|
|
|
MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
return( PSA_SUCCESS );
|
2019-01-19 12:40:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
/** Prepare a key slot to receive key material.
|
|
|
|
*
|
|
|
|
* This function allocates a key slot and sets its metadata.
|
|
|
|
*
|
|
|
|
* If this function fails, call psa_fail_key_creation().
|
|
|
|
*
|
2019-04-28 09:37:03 +00:00
|
|
|
* This function is intended to be used as follows:
|
|
|
|
* -# Call psa_start_key_creation() to allocate a key slot, prepare
|
2020-08-04 07:51:30 +00:00
|
|
|
* it with the specified attributes, and in case of a volatile key assign it
|
|
|
|
* a volatile key identifier.
|
2019-04-28 09:37:03 +00:00
|
|
|
* -# Populate the slot with the key material.
|
|
|
|
* -# Call psa_finish_key_creation() to finalize the creation of the slot.
|
|
|
|
* In case of failure at any step, stop the sequence and call
|
|
|
|
* psa_fail_key_creation().
|
|
|
|
*
|
2020-11-14 15:35:34 +00:00
|
|
|
* On success, the key slot is locked. It is the responsibility of the caller
|
|
|
|
* to unlock the key slot when it does not access it anymore.
|
2020-10-22 13:24:49 +00:00
|
|
|
*
|
2019-07-15 20:02:14 +00:00
|
|
|
* \param method An identification of the calling function.
|
2019-06-26 16:34:38 +00:00
|
|
|
* \param[in] attributes Key attributes for the new key.
|
|
|
|
* \param[out] p_slot On success, a pointer to the prepared slot.
|
|
|
|
* \param[out] p_drv On any return, the driver for the key, if any.
|
|
|
|
* NULL for a transparent key.
|
2019-04-28 09:37:03 +00:00
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
|
|
|
* The key slot is ready to receive key material.
|
|
|
|
* \return If this function fails, the key slot is an invalid state.
|
|
|
|
* You must call psa_fail_key_creation() to wipe and free the slot.
|
2019-04-17 13:05:45 +00:00
|
|
|
*/
|
|
|
|
static psa_status_t psa_start_key_creation(
|
2019-07-15 20:02:14 +00:00
|
|
|
psa_key_creation_method_t method,
|
2019-04-17 13:05:45 +00:00
|
|
|
const psa_key_attributes_t *attributes,
|
2019-06-26 16:34:38 +00:00
|
|
|
psa_key_slot_t **p_slot,
|
2019-07-12 21:40:35 +00:00
|
|
|
psa_se_drv_table_entry_t **p_drv )
|
2019-01-19 12:40:11 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2020-07-17 12:13:26 +00:00
|
|
|
psa_key_id_t volatile_key_id;
|
2019-04-17 13:05:45 +00:00
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2019-07-15 20:02:14 +00:00
|
|
|
(void) method;
|
2019-06-26 16:34:38 +00:00
|
|
|
*p_drv = NULL;
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
status = psa_validate_key_attributes( attributes, p_drv );
|
2019-01-19 12:40:11 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-07-31 09:26:37 +00:00
|
|
|
status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
|
2019-01-19 12:40:11 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2019-04-17 13:05:45 +00:00
|
|
|
slot = *p_slot;
|
|
|
|
|
2019-07-31 12:15:34 +00:00
|
|
|
/* We're storing the declared bit-size of the key. It's up to each
|
|
|
|
* creation mechanism to verify that this information is correct.
|
|
|
|
* It's automatically correct for mechanisms that use the bit-size as
|
2019-07-30 19:32:04 +00:00
|
|
|
* an input (generate, device) but not for those where the bit-size
|
2020-07-31 09:26:37 +00:00
|
|
|
* is optional (import, copy). In case of a volatile key, assign it the
|
|
|
|
* volatile key identifier associated to the slot returned to contain its
|
|
|
|
* definition. */
|
2019-07-31 12:15:34 +00:00
|
|
|
|
|
|
|
slot->attr = attributes->core;
|
2020-07-31 09:26:37 +00:00
|
|
|
if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
|
|
|
|
{
|
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
|
|
|
slot->attr.id = volatile_key_id;
|
|
|
|
#else
|
|
|
|
slot->attr.id.key_id = volatile_key_id;
|
|
|
|
#endif
|
|
|
|
}
|
2019-07-30 15:26:54 +00:00
|
|
|
|
2019-08-02 17:19:39 +00:00
|
|
|
/* Erase external-only flags from the internal copy. To access
|
2019-08-07 13:42:14 +00:00
|
|
|
* external-only flags, query `attributes`. Thanks to the check
|
|
|
|
* in psa_validate_key_attributes(), this leaves the dual-use
|
2019-08-07 16:19:59 +00:00
|
|
|
* flags and any internal flag that psa_get_empty_key_slot()
|
2019-08-07 13:42:14 +00:00
|
|
|
* may have set. */
|
|
|
|
slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
|
2019-08-02 17:19:39 +00:00
|
|
|
|
2019-07-12 21:46:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-05 13:55:54 +00:00
|
|
|
/* For a key in a secure element, we need to do three things
|
2020-06-08 16:29:44 +00:00
|
|
|
* when creating or registering a persistent key:
|
2019-07-25 09:32:45 +00:00
|
|
|
* create the key file in internal storage, create the
|
|
|
|
* key inside the secure element, and update the driver's
|
2020-06-08 16:29:44 +00:00
|
|
|
* persistent data. This is done by starting a transaction that will
|
|
|
|
* encompass these three actions.
|
|
|
|
* For registering a volatile key, we just need to find an appropriate
|
|
|
|
* slot number inside the SE. Since the key is designated volatile, creating
|
|
|
|
* a transaction is not required. */
|
2019-07-25 09:32:45 +00:00
|
|
|
/* The first thing to do is to find a slot number for the new key.
|
|
|
|
* We save the slot number in persistent storage as part of the
|
|
|
|
* transaction data. It will be needed to recover if the power
|
|
|
|
* fails during the key creation process, to clean up on the secure
|
|
|
|
* element side after restarting. Obtaining a slot number from the
|
|
|
|
* secure element driver updates its persistent state, but we do not yet
|
|
|
|
* save the driver's persistent state, so that if the power fails,
|
2019-07-22 17:30:34 +00:00
|
|
|
* we can roll back to a state where the key doesn't exist. */
|
2019-10-01 12:18:35 +00:00
|
|
|
if( *p_drv != NULL )
|
2019-07-12 21:46:04 +00:00
|
|
|
{
|
2020-11-25 16:52:23 +00:00
|
|
|
psa_key_slot_number_t slot_number;
|
2019-08-05 14:44:14 +00:00
|
|
|
status = psa_find_se_slot_for_key( attributes, method, *p_drv,
|
2020-11-25 16:52:23 +00:00
|
|
|
&slot_number );
|
2019-07-12 21:46:04 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2020-06-08 16:29:44 +00:00
|
|
|
|
|
|
|
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
|
2019-07-25 16:02:52 +00:00
|
|
|
{
|
2020-06-08 16:29:44 +00:00
|
|
|
psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
|
|
|
|
psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
|
2020-11-25 16:52:23 +00:00
|
|
|
psa_crypto_transaction.key.slot = slot_number;
|
2020-06-08 16:29:44 +00:00
|
|
|
psa_crypto_transaction.key.id = slot->attr.id;
|
|
|
|
status = psa_crypto_save_transaction( );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
(void) psa_crypto_stop_transaction( );
|
|
|
|
return( status );
|
|
|
|
}
|
2019-07-25 16:02:52 +00:00
|
|
|
}
|
2020-11-25 16:52:23 +00:00
|
|
|
|
|
|
|
status = psa_copy_key_material_into_slot(
|
|
|
|
slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
|
2019-01-19 12:40:11 +00:00
|
|
|
}
|
2019-10-01 12:18:35 +00:00
|
|
|
|
|
|
|
if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
|
|
|
|
{
|
|
|
|
/* Key registration only makes sense with a secure element. */
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
2019-07-12 21:46:04 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-01-19 12:40:11 +00:00
|
|
|
|
2020-07-31 09:26:37 +00:00
|
|
|
return( PSA_SUCCESS );
|
2019-01-19 12:40:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
/** Finalize the creation of a key once its key material has been set.
|
|
|
|
*
|
|
|
|
* This entails writing the key to persistent storage.
|
|
|
|
*
|
|
|
|
* If this function fails, call psa_fail_key_creation().
|
2019-04-28 09:37:03 +00:00
|
|
|
* See the documentation of psa_start_key_creation() for the intended use
|
|
|
|
* of this function.
|
2019-04-17 13:05:45 +00:00
|
|
|
*
|
2020-11-14 15:35:34 +00:00
|
|
|
* If the finalization succeeds, the function unlocks the key slot (it was
|
|
|
|
* locked by psa_start_key_creation()) and the key slot cannot be accessed
|
|
|
|
* anymore as part of the key creation process.
|
2020-11-14 10:28:25 +00:00
|
|
|
*
|
2019-06-26 16:34:38 +00:00
|
|
|
* \param[in,out] slot Pointer to the slot with key material.
|
|
|
|
* \param[in] driver The secure element driver for the key,
|
|
|
|
* or NULL for a transparent key.
|
2020-11-14 11:10:32 +00:00
|
|
|
* \param[out] key On success, identifier of the key. Note that the
|
|
|
|
* key identifier is also stored in the key slot.
|
2019-04-28 09:37:03 +00:00
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
2020-08-04 07:51:30 +00:00
|
|
|
* The key was successfully created.
|
2020-11-09 16:42:55 +00:00
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
|
|
|
* \retval #PSA_ERROR_ALREADY_EXISTS
|
|
|
|
* \retval #PSA_ERROR_DATA_INVALID
|
|
|
|
* \retval #PSA_ERROR_DATA_CORRUPT
|
2020-11-30 15:50:34 +00:00
|
|
|
* \retval #PSA_ERROR_STORAGE_FAILURE
|
2020-11-09 16:42:55 +00:00
|
|
|
*
|
2019-04-28 09:37:03 +00:00
|
|
|
* \return If this function fails, the key slot is an invalid state.
|
|
|
|
* You must call psa_fail_key_creation() to wipe and free the slot.
|
2019-04-17 13:05:45 +00:00
|
|
|
*/
|
2019-06-26 16:34:38 +00:00
|
|
|
static psa_status_t psa_finish_key_creation(
|
|
|
|
psa_key_slot_t *slot,
|
2020-11-14 11:10:32 +00:00
|
|
|
psa_se_drv_table_entry_t *driver,
|
|
|
|
mbedtls_svc_key_id_t *key)
|
2019-04-17 13:05:45 +00:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
2019-04-25 11:47:40 +00:00
|
|
|
(void) slot;
|
2019-06-26 16:34:38 +00:00
|
|
|
(void) driver;
|
2019-04-17 13:05:45 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2020-06-08 16:28:25 +00:00
|
|
|
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
|
2019-04-17 13:05:45 +00:00
|
|
|
{
|
2019-07-23 14:13:14 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
if( driver != NULL )
|
|
|
|
{
|
2019-07-30 19:32:04 +00:00
|
|
|
psa_se_key_data_storage_t data;
|
2020-11-25 16:52:23 +00:00
|
|
|
psa_key_slot_number_t slot_number =
|
|
|
|
psa_key_slot_get_slot_number( slot ) ;
|
|
|
|
|
2019-07-30 19:32:04 +00:00
|
|
|
#if defined(static_assert)
|
2020-11-25 16:52:23 +00:00
|
|
|
static_assert( sizeof( slot_number ) ==
|
2019-07-30 19:32:04 +00:00
|
|
|
sizeof( data.slot_number ),
|
|
|
|
"Slot number size does not match psa_se_key_data_storage_t" );
|
|
|
|
#endif
|
2020-11-25 16:52:23 +00:00
|
|
|
memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
|
2019-07-31 12:15:34 +00:00
|
|
|
status = psa_save_persistent_key( &slot->attr,
|
2019-07-30 19:32:04 +00:00
|
|
|
(uint8_t*) &data,
|
|
|
|
sizeof( data ) );
|
2019-07-23 14:13:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
{
|
2020-10-29 10:42:22 +00:00
|
|
|
/* Key material is saved in export representation in the slot, so
|
|
|
|
* just pass the slot buffer for storage. */
|
|
|
|
status = psa_save_persistent_key( &slot->attr,
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes );
|
2019-07-23 14:13:14 +00:00
|
|
|
}
|
2019-04-17 13:05:45 +00:00
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
|
|
|
|
2019-07-12 21:46:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-05 13:55:54 +00:00
|
|
|
/* Finish the transaction for a key creation. This does not
|
|
|
|
* happen when registering an existing key. Detect this case
|
|
|
|
* by checking whether a transaction is in progress (actual
|
2020-06-08 16:29:44 +00:00
|
|
|
* creation of a persistent key in a secure element requires a transaction,
|
|
|
|
* but registration or volatile key creation doesn't use one). */
|
2019-08-05 13:55:54 +00:00
|
|
|
if( driver != NULL &&
|
|
|
|
psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
|
2019-07-12 21:46:04 +00:00
|
|
|
{
|
|
|
|
status = psa_save_se_persistent_data( driver );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
2019-07-30 18:06:31 +00:00
|
|
|
psa_destroy_persistent_key( slot->attr.id );
|
2019-07-12 21:46:04 +00:00
|
|
|
return( status );
|
|
|
|
}
|
2019-07-22 17:30:34 +00:00
|
|
|
status = psa_crypto_stop_transaction( );
|
2019-07-12 21:46:04 +00:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2020-11-14 10:28:25 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
2020-11-14 11:10:32 +00:00
|
|
|
{
|
|
|
|
*key = slot->attr.id;
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_unlock_key_slot( slot );
|
2020-11-14 11:10:32 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
}
|
2020-11-14 10:28:25 +00:00
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Abort the creation of a key.
|
|
|
|
*
|
|
|
|
* You may call this function after calling psa_start_key_creation(),
|
|
|
|
* or after psa_finish_key_creation() fails. In other circumstances, this
|
|
|
|
* function may not clean up persistent storage.
|
2019-04-28 09:37:03 +00:00
|
|
|
* See the documentation of psa_start_key_creation() for the intended use
|
|
|
|
* of this function.
|
2019-04-17 13:05:45 +00:00
|
|
|
*
|
2019-06-26 16:34:38 +00:00
|
|
|
* \param[in,out] slot Pointer to the slot with key material.
|
|
|
|
* \param[in] driver The secure element driver for the key,
|
|
|
|
* or NULL for a transparent key.
|
2019-04-17 13:05:45 +00:00
|
|
|
*/
|
2019-06-26 16:34:38 +00:00
|
|
|
static void psa_fail_key_creation( psa_key_slot_t *slot,
|
2019-07-12 21:40:35 +00:00
|
|
|
psa_se_drv_table_entry_t *driver )
|
2019-04-17 13:05:45 +00:00
|
|
|
{
|
2019-06-26 16:34:38 +00:00
|
|
|
(void) driver;
|
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
if( slot == NULL )
|
|
|
|
return;
|
2019-06-26 16:34:38 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-13 11:15:34 +00:00
|
|
|
/* TODO: If the key has already been created in the secure
|
2019-06-26 16:34:38 +00:00
|
|
|
* element, and the failure happened later (when saving metadata
|
|
|
|
* to internal storage), we need to destroy the key in the secure
|
2019-08-13 14:17:16 +00:00
|
|
|
* element.
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/217
|
|
|
|
*/
|
2019-07-22 17:30:34 +00:00
|
|
|
|
2019-08-05 13:55:54 +00:00
|
|
|
/* Abort the ongoing transaction if any (there may not be one if
|
|
|
|
* the creation process failed before starting one, or if the
|
|
|
|
* key creation is a registration of a key in a secure element).
|
|
|
|
* Earlier functions must already have done what it takes to undo any
|
|
|
|
* partial creation. All that's left is to update the transaction data
|
|
|
|
* itself. */
|
2019-07-22 17:30:34 +00:00
|
|
|
(void) psa_crypto_stop_transaction( );
|
2019-06-26 16:34:38 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
psa_wipe_key_slot( slot );
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
/** Validate optional attributes during key creation.
|
|
|
|
*
|
|
|
|
* Some key attributes are optional during key creation. If they are
|
|
|
|
* specified in the attributes structure, check that they are consistent
|
|
|
|
* with the data in the slot.
|
|
|
|
*
|
|
|
|
* This function should be called near the end of key creation, after
|
|
|
|
* the slot in memory is fully populated but before saving persistent data.
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_validate_optional_attributes(
|
2019-05-03 14:57:15 +00:00
|
|
|
const psa_key_slot_t *slot,
|
|
|
|
const psa_key_attributes_t *attributes )
|
|
|
|
{
|
2019-07-30 11:48:52 +00:00
|
|
|
if( attributes->core.type != 0 )
|
2019-05-03 14:57:15 +00:00
|
|
|
{
|
2019-07-30 18:06:31 +00:00
|
|
|
if( attributes->core.type != slot->attr.type )
|
2019-05-03 14:57:15 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( attributes->domain_parameters_size != 0 )
|
|
|
|
{
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
|
2019-05-03 14:57:15 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-07-28 12:30:13 +00:00
|
|
|
mbedtls_mpi actual, required;
|
2020-07-30 09:36:45 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-11-25 13:58:33 +00:00
|
|
|
psa_status_t status = mbedtls_psa_rsa_load_representation(
|
|
|
|
slot->attr.type,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-08-05 13:43:42 +00:00
|
|
|
return( status );
|
2020-07-28 12:30:13 +00:00
|
|
|
|
2019-05-03 14:57:15 +00:00
|
|
|
mbedtls_mpi_init( &actual );
|
|
|
|
mbedtls_mpi_init( &required );
|
2020-07-28 12:30:39 +00:00
|
|
|
ret = mbedtls_rsa_export( rsa,
|
2019-05-03 14:57:15 +00:00
|
|
|
NULL, NULL, NULL, NULL, &actual );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
mbedtls_free( rsa );
|
2019-05-03 14:57:15 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
goto rsa_exit;
|
|
|
|
ret = mbedtls_mpi_read_binary( &required,
|
|
|
|
attributes->domain_parameters,
|
|
|
|
attributes->domain_parameters_size );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto rsa_exit;
|
|
|
|
if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
|
|
|
|
ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
rsa_exit:
|
|
|
|
mbedtls_mpi_free( &actual );
|
|
|
|
mbedtls_mpi_free( &required );
|
|
|
|
if( ret != 0)
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
|
|
|
else
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2019-05-03 14:57:15 +00:00
|
|
|
{
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:48:52 +00:00
|
|
|
if( attributes->core.bits != 0 )
|
2019-05-03 14:57:15 +00:00
|
|
|
{
|
2019-07-30 19:32:04 +00:00
|
|
|
if( attributes->core.bits != slot->attr.bits )
|
2019-05-03 14:57:15 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *data,
|
2019-05-15 18:15:10 +00:00
|
|
|
size_t data_length,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t *key )
|
2019-04-17 13:05:45 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-07-12 21:40:35 +00:00
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2020-11-30 11:11:01 +00:00
|
|
|
size_t bits;
|
2019-05-03 14:57:15 +00:00
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
2019-09-12 17:03:13 +00:00
|
|
|
/* Reject zero-length symmetric keys (including raw data key objects).
|
|
|
|
* This also rejects any key which might be encoded as an empty string,
|
|
|
|
* which is never valid. */
|
|
|
|
if( data_length == 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2019-07-15 20:02:14 +00:00
|
|
|
status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
|
2020-11-14 11:10:32 +00:00
|
|
|
&slot, &driver );
|
2019-05-03 14:57:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2020-11-30 11:11:01 +00:00
|
|
|
/* In the case of a transparent key or an opaque key stored in local
|
|
|
|
* storage (thus not in the case of generating a key in a secure element
|
|
|
|
* or cryptoprocessor with storage), we have to allocate a buffer to
|
|
|
|
* hold the generated key material. */
|
|
|
|
if( slot->key.data == NULL )
|
2019-07-12 21:47:28 +00:00
|
|
|
{
|
2020-11-30 11:11:01 +00:00
|
|
|
status = psa_allocate_buffer_to_slot( slot, data_length );
|
2019-07-30 19:32:04 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-07-12 21:47:28 +00:00
|
|
|
}
|
2020-11-30 11:11:01 +00:00
|
|
|
|
|
|
|
bits = slot->attr.bits;
|
|
|
|
status = psa_driver_wrapper_import_key( attributes,
|
|
|
|
data, data_length,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&slot->key.bytes, &bits );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if( slot->attr.bits == 0 )
|
|
|
|
slot->attr.bits = (psa_key_bits_t) bits;
|
|
|
|
else if( bits != slot->attr.bits )
|
2021-01-15 16:36:02 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
2019-07-12 21:47:28 +00:00
|
|
|
}
|
2020-11-28 14:54:54 +00:00
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
status = psa_validate_optional_attributes( slot, attributes );
|
2019-07-24 18:25:59 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-05-03 14:57:15 +00:00
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_finish_key_creation( slot, driver, key );
|
2019-05-03 14:57:15 +00:00
|
|
|
exit:
|
2019-04-17 13:05:45 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-06-26 16:34:38 +00:00
|
|
|
psa_fail_key_creation( slot, driver );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2019-04-17 13:05:45 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:55:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
psa_status_t mbedtls_psa_register_se_key(
|
|
|
|
const psa_key_attributes_t *attributes )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot = NULL;
|
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-08-05 13:55:54 +00:00
|
|
|
|
|
|
|
/* Leaving attributes unspecified is not currently supported.
|
|
|
|
* It could make sense to query the key type and size from the
|
|
|
|
* secure element, but not all secure elements support this
|
|
|
|
* and the driver HAL doesn't currently support it. */
|
|
|
|
if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
if( psa_get_key_bits( attributes ) == 0 )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
|
|
|
status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
|
2020-11-14 11:10:32 +00:00
|
|
|
&slot, &driver );
|
2019-08-05 13:55:54 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_finish_key_creation( slot, driver, &key );
|
2019-08-05 13:55:54 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
psa_fail_key_creation( slot, driver );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2019-08-05 13:55:54 +00:00
|
|
|
/* Registration doesn't keep the key in RAM. */
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_close_key( key );
|
2019-08-05 13:55:54 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2019-01-19 12:40:11 +00:00
|
|
|
static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
|
2019-04-18 19:44:46 +00:00
|
|
|
psa_key_slot_t *target )
|
2019-01-19 12:40:11 +00:00
|
|
|
{
|
2020-10-13 18:27:40 +00:00
|
|
|
psa_status_t status = psa_copy_key_material_into_slot( target,
|
2020-11-25 16:52:23 +00:00
|
|
|
source->key.data,
|
|
|
|
source->key.bytes );
|
2019-01-19 12:40:11 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-10-13 12:35:45 +00:00
|
|
|
return( status );
|
|
|
|
|
2019-07-30 18:06:31 +00:00
|
|
|
target->attr.type = source->attr.type;
|
2020-10-13 12:35:45 +00:00
|
|
|
target->attr.bits = source->attr.bits;
|
2019-01-19 12:40:11 +00:00
|
|
|
|
2020-10-13 12:35:45 +00:00
|
|
|
return( PSA_SUCCESS );
|
2019-01-19 12:40:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
|
2019-04-18 19:44:46 +00:00
|
|
|
const psa_key_attributes_t *specified_attributes,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t *target_key )
|
2019-04-18 19:44:46 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2019-04-18 19:44:46 +00:00
|
|
|
psa_key_slot_t *source_slot = NULL;
|
|
|
|
psa_key_slot_t *target_slot = NULL;
|
|
|
|
psa_key_attributes_t actual_attributes = *specified_attributes;
|
2019-07-12 21:40:35 +00:00
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2019-04-18 19:44:46 +00:00
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
*target_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
|
2019-04-18 19:44:46 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2019-07-31 15:21:46 +00:00
|
|
|
status = psa_validate_optional_attributes( source_slot,
|
|
|
|
specified_attributes );
|
2019-05-03 14:57:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2019-07-30 11:48:52 +00:00
|
|
|
status = psa_restrict_key_policy( &actual_attributes.core.policy,
|
2019-07-30 18:06:31 +00:00
|
|
|
&source_slot->attr.policy );
|
2019-04-18 19:44:46 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
|
|
|
|
&target_slot, &driver );
|
2019-04-18 19:44:46 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2019-07-24 11:44:30 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
if( driver != NULL )
|
|
|
|
{
|
|
|
|
/* Copying to a secure element is not implemented yet. */
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2019-04-18 19:44:46 +00:00
|
|
|
status = psa_copy_key_material( source_slot, target_slot );
|
2019-05-03 14:57:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-04-18 19:44:46 +00:00
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_finish_key_creation( target_slot, driver, target_key );
|
2019-04-18 19:44:46 +00:00
|
|
|
exit:
|
|
|
|
if( status != PSA_SUCCESS )
|
2019-06-26 16:34:38 +00:00
|
|
|
psa_fail_key_creation( target_slot, driver );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( source_slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2019-04-18 19:44:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 19:49:39 +00:00
|
|
|
|
|
|
|
|
2018-02-03 21:44:14 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Message digests */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
2020-11-01 05:06:54 +00:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
|
2020-11-04 20:28:15 +00:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2018-03-07 15:42:59 +00:00
|
|
|
static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
|
2018-02-03 21:44:14 +00:00
|
|
|
{
|
|
|
|
switch( alg )
|
|
|
|
{
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_MD2:
|
|
|
|
return( &mbedtls_md2_info );
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_MD4:
|
|
|
|
return( &mbedtls_md4_info );
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_MD5:
|
|
|
|
return( &mbedtls_md5_info );
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
|
|
|
return( &mbedtls_ripemd160_info );
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
|
|
|
return( &mbedtls_sha1_info );
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
|
|
|
return( &mbedtls_sha224_info );
|
2020-11-27 16:48:46 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
|
|
|
return( &mbedtls_sha256_info );
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
|
|
|
return( &mbedtls_sha384_info );
|
2019-07-17 14:28:21 +00:00
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2018-02-03 21:44:14 +00:00
|
|
|
case PSA_ALG_SHA_512:
|
|
|
|
return( &mbedtls_sha512_info );
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
2020-11-04 20:28:15 +00:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2018-02-03 21:44:14 +00:00
|
|
|
|
2018-02-07 20:05:37 +00:00
|
|
|
psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
|
|
|
|
{
|
|
|
|
switch( operation->alg )
|
|
|
|
{
|
2018-06-26 13:04:31 +00:00
|
|
|
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;
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD2:
|
|
|
|
mbedtls_md2_free( &operation->ctx.md2 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD4:
|
|
|
|
mbedtls_md4_free( &operation->ctx.md4 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD5:
|
|
|
|
mbedtls_md5_free( &operation->ctx.md5 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
|
|
|
mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
|
|
|
mbedtls_sha1_free( &operation->ctx.sha1 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
2020-12-03 14:01:32 +00:00
|
|
|
mbedtls_sha256_free( &operation->ctx.sha256 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
|
|
|
mbedtls_sha256_free( &operation->ctx.sha256 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
2020-12-03 14:01:32 +00:00
|
|
|
mbedtls_sha512_free( &operation->ctx.sha512 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_512:
|
|
|
|
mbedtls_sha512_free( &operation->ctx.sha512 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2018-06-21 14:57:07 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-07 20:05:37 +00:00
|
|
|
}
|
|
|
|
operation->alg = 0;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2018-07-08 17:46:38 +00:00
|
|
|
psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
|
2018-02-07 20:05:37 +00:00
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-02-19 09:25:10 +00:00
|
|
|
|
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
|
|
|
if( operation->alg != 0 )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
|
2018-02-07 20:05:37 +00:00
|
|
|
switch( alg )
|
|
|
|
{
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD2:
|
|
|
|
mbedtls_md2_init( &operation->ctx.md2 );
|
|
|
|
ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD4:
|
|
|
|
mbedtls_md4_init( &operation->ctx.md4 );
|
|
|
|
ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD5:
|
|
|
|
mbedtls_md5_init( &operation->ctx.md5 );
|
|
|
|
ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
|
|
|
mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
|
|
|
|
ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
|
|
|
mbedtls_sha1_init( &operation->ctx.sha1 );
|
|
|
|
ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
|
|
|
mbedtls_sha256_init( &operation->ctx.sha256 );
|
|
|
|
ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
|
|
|
|
break;
|
2020-11-27 16:48:46 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
|
|
|
mbedtls_sha256_init( &operation->ctx.sha256 );
|
|
|
|
ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
|
|
|
mbedtls_sha512_init( &operation->ctx.sha512 );
|
|
|
|
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
|
|
|
|
break;
|
2020-01-07 09:13:18 +00:00
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_512:
|
|
|
|
mbedtls_sha512_init( &operation->ctx.sha512 );
|
|
|
|
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2018-06-20 14:21:04 +00:00
|
|
|
return( PSA_ALG_IS_HASH( alg ) ?
|
|
|
|
PSA_ERROR_NOT_SUPPORTED :
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
2018-02-07 20:05:37 +00:00
|
|
|
}
|
|
|
|
if( ret == 0 )
|
|
|
|
operation->alg = alg;
|
|
|
|
else
|
|
|
|
psa_hash_abort( operation );
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_hash_update( psa_hash_operation_t *operation,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-07-12 14:58:43 +00:00
|
|
|
|
|
|
|
/* 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 );
|
|
|
|
|
2018-02-07 20:05:37 +00:00
|
|
|
switch( operation->alg )
|
|
|
|
{
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD2:
|
|
|
|
ret = mbedtls_md2_update_ret( &operation->ctx.md2,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD4:
|
|
|
|
ret = mbedtls_md4_update_ret( &operation->ctx.md4,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD5:
|
|
|
|
ret = mbedtls_md5_update_ret( &operation->ctx.md5,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
|
|
|
ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
|
|
|
ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
2020-12-03 14:01:32 +00:00
|
|
|
ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
|
|
|
ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
2020-12-03 14:01:32 +00:00
|
|
|
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_512:
|
|
|
|
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
|
|
|
|
input, input_length );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2020-11-27 16:48:46 +00:00
|
|
|
(void)input;
|
2019-02-15 13:52:25 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-07 20:05:37 +00:00
|
|
|
}
|
2018-07-12 14:58:43 +00:00
|
|
|
|
2018-02-07 20:05:37 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
psa_hash_abort( operation );
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
|
|
|
|
uint8_t *hash,
|
|
|
|
size_t hash_size,
|
|
|
|
size_t *hash_length )
|
|
|
|
{
|
2018-08-02 10:14:17 +00:00
|
|
|
psa_status_t status;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-12-18 13:23:51 +00:00
|
|
|
size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
|
2018-02-07 20:05:37 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2018-07-02 10:15:28 +00:00
|
|
|
*hash_length = hash_size;
|
2018-06-28 17:31:31 +00:00
|
|
|
/* 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 );
|
2018-02-07 20:05:37 +00:00
|
|
|
|
|
|
|
if( hash_size < actual_hash_length )
|
2018-08-02 10:14:17 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-02-07 20:05:37 +00:00
|
|
|
|
|
|
|
switch( operation->alg )
|
|
|
|
{
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD2:
|
|
|
|
ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD4:
|
|
|
|
ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_MD5:
|
|
|
|
ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
|
|
|
ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
|
|
|
ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
2020-12-03 14:01:32 +00:00
|
|
|
ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
|
|
|
ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
2020-12-03 14:01:32 +00:00
|
|
|
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2018-02-07 20:05:37 +00:00
|
|
|
case PSA_ALG_SHA_512:
|
|
|
|
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2019-02-15 13:52:25 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-07 20:05:37 +00:00
|
|
|
}
|
2018-08-02 10:14:17 +00:00
|
|
|
status = mbedtls_to_psa_error( ret );
|
2018-02-07 20:05:37 +00:00
|
|
|
|
2018-08-02 10:14:17 +00:00
|
|
|
exit:
|
|
|
|
if( status == PSA_SUCCESS )
|
2018-02-07 20:05:37 +00:00
|
|
|
{
|
2018-07-02 10:15:28 +00:00
|
|
|
*hash_length = actual_hash_length;
|
2018-02-07 20:05:37 +00:00
|
|
|
return( psa_hash_abort( operation ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
psa_hash_abort( operation );
|
2018-08-02 10:14:17 +00:00
|
|
|
return( status );
|
2018-02-07 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:41:12 +00:00
|
|
|
psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length )
|
2018-02-07 20:05:37 +00:00
|
|
|
{
|
|
|
|
uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
size_t actual_hash_length;
|
|
|
|
psa_status_t status = psa_hash_finish( operation,
|
|
|
|
actual_hash, sizeof( actual_hash ),
|
|
|
|
&actual_hash_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
if( actual_hash_length != hash_length )
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:33:58 +00:00
|
|
|
psa_status_t 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 )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
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_verify( &operation, hash, 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 );
|
|
|
|
}
|
|
|
|
|
2019-01-22 16:56:16 +00:00
|
|
|
psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
|
|
|
|
psa_hash_operation_t *target_operation )
|
2019-01-19 11:03:41 +00:00
|
|
|
{
|
|
|
|
if( target_operation->alg != 0 )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
switch( source_operation->alg )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_MD2:
|
|
|
|
mbedtls_md2_clone( &target_operation->ctx.md2,
|
|
|
|
&source_operation->ctx.md2 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_MD4:
|
|
|
|
mbedtls_md4_clone( &target_operation->ctx.md4,
|
|
|
|
&source_operation->ctx.md4 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_MD5:
|
|
|
|
mbedtls_md5_clone( &target_operation->ctx.md5,
|
|
|
|
&source_operation->ctx.md5 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
|
|
|
mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
|
|
|
|
&source_operation->ctx.ripemd160 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-11-27 16:48:46 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
|
|
|
mbedtls_sha1_clone( &target_operation->ctx.sha1,
|
|
|
|
&source_operation->ctx.sha1 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
2020-12-03 14:01:32 +00:00
|
|
|
mbedtls_sha256_clone( &target_operation->ctx.sha256,
|
|
|
|
&source_operation->ctx.sha256 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
|
|
|
mbedtls_sha256_clone( &target_operation->ctx.sha256,
|
|
|
|
&source_operation->ctx.sha256 );
|
|
|
|
break;
|
|
|
|
#endif
|
2020-12-03 14:01:32 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2019-01-19 11:03:41 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
2020-12-03 14:01:32 +00:00
|
|
|
mbedtls_sha512_clone( &target_operation->ctx.sha512,
|
|
|
|
&source_operation->ctx.sha512 );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2019-01-19 11:03:41 +00:00
|
|
|
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 );
|
|
|
|
}
|
2018-02-07 20:05:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
2018-02-08 09:02:12 +00:00
|
|
|
/* MAC */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2018-03-07 15:42:59 +00:00
|
|
|
static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
2018-02-08 09:02:12 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
psa_key_type_t key_type,
|
2018-06-18 13:41:12 +00:00
|
|
|
size_t key_bits,
|
2018-06-03 12:04:51 +00:00
|
|
|
mbedtls_cipher_id_t* cipher_id )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
|
|
|
mbedtls_cipher_mode_t mode;
|
2018-06-03 12:04:51 +00:00
|
|
|
mbedtls_cipher_id_t cipher_id_tmp;
|
2018-02-08 09:02:12 +00:00
|
|
|
|
2018-08-17 17:47:52 +00:00
|
|
|
if( PSA_ALG_IS_AEAD( alg ) )
|
2020-12-16 10:36:46 +00:00
|
|
|
alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
|
2018-08-17 17:47:52 +00:00
|
|
|
|
2018-02-08 09:02:12 +00:00
|
|
|
if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
|
|
|
|
{
|
2018-06-17 11:41:30 +00:00
|
|
|
switch( alg )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2020-12-07 17:20:28 +00:00
|
|
|
case PSA_ALG_STREAM_CIPHER:
|
2018-02-08 09:02:12 +00:00
|
|
|
mode = MBEDTLS_MODE_STREAM;
|
|
|
|
break;
|
2018-08-21 12:02:45 +00:00
|
|
|
case PSA_ALG_CTR:
|
|
|
|
mode = MBEDTLS_MODE_CTR;
|
2018-02-08 09:02:12 +00:00
|
|
|
break;
|
2018-08-21 12:02:45 +00:00
|
|
|
case PSA_ALG_CFB:
|
2018-02-08 09:02:12 +00:00
|
|
|
mode = MBEDTLS_MODE_CFB;
|
|
|
|
break;
|
2018-08-21 12:02:45 +00:00
|
|
|
case PSA_ALG_OFB:
|
2018-02-08 09:02:12 +00:00
|
|
|
mode = MBEDTLS_MODE_OFB;
|
|
|
|
break;
|
2020-07-06 12:08:59 +00:00
|
|
|
case PSA_ALG_ECB_NO_PADDING:
|
|
|
|
mode = MBEDTLS_MODE_ECB;
|
|
|
|
break;
|
2018-08-21 12:02:45 +00:00
|
|
|
case PSA_ALG_CBC_NO_PADDING:
|
|
|
|
mode = MBEDTLS_MODE_CBC;
|
|
|
|
break;
|
|
|
|
case PSA_ALG_CBC_PKCS7:
|
|
|
|
mode = MBEDTLS_MODE_CBC;
|
2018-02-08 09:02:12 +00:00
|
|
|
break;
|
2020-12-16 10:36:46 +00:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
|
2018-02-08 09:02:12 +00:00
|
|
|
mode = MBEDTLS_MODE_CCM;
|
|
|
|
break;
|
2020-12-16 10:36:46 +00:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
|
2018-02-08 09:02:12 +00:00
|
|
|
mode = MBEDTLS_MODE_GCM;
|
|
|
|
break;
|
2020-12-16 10:36:46 +00:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
|
2019-05-06 13:25:00 +00:00
|
|
|
mode = MBEDTLS_MODE_CHACHAPOLY;
|
|
|
|
break;
|
2018-02-08 09:02:12 +00:00
|
|
|
default:
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( alg == PSA_ALG_CMAC )
|
|
|
|
mode = MBEDTLS_MODE_ECB;
|
|
|
|
else
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
switch( key_type )
|
|
|
|
{
|
|
|
|
case PSA_KEY_TYPE_AES:
|
2018-06-03 12:04:51 +00:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
|
2018-02-08 09:02:12 +00:00
|
|
|
break;
|
|
|
|
case PSA_KEY_TYPE_DES:
|
2018-06-21 07:40:04 +00:00
|
|
|
/* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
|
|
|
|
* and 192 for three-key Triple-DES. */
|
2018-02-08 09:02:12 +00:00
|
|
|
if( key_bits == 64 )
|
2018-06-03 12:04:51 +00:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
|
2018-02-08 09:02:12 +00:00
|
|
|
else
|
2018-06-03 12:04:51 +00:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
|
2018-06-21 07:40:04 +00:00
|
|
|
/* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
|
|
|
|
* but two-key Triple-DES is functionally three-key Triple-DES
|
|
|
|
* with K1=K3, so that's how we present it to mbedtls. */
|
|
|
|
if( key_bits == 128 )
|
|
|
|
key_bits = 192;
|
2018-02-08 09:02:12 +00:00
|
|
|
break;
|
|
|
|
case PSA_KEY_TYPE_CAMELLIA:
|
2018-06-03 12:04:51 +00:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
|
2018-02-08 09:02:12 +00:00
|
|
|
break;
|
|
|
|
case PSA_KEY_TYPE_ARC4:
|
2018-06-03 12:04:51 +00:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
|
2018-02-08 09:02:12 +00:00
|
|
|
break;
|
2019-05-06 13:25:00 +00:00
|
|
|
case PSA_KEY_TYPE_CHACHA20:
|
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
|
|
|
|
break;
|
2018-02-08 09:02:12 +00:00
|
|
|
default:
|
|
|
|
return( NULL );
|
|
|
|
}
|
2018-06-03 12:04:51 +00:00
|
|
|
if( cipher_id != NULL )
|
2018-06-03 14:20:42 +00:00
|
|
|
*cipher_id = cipher_id_tmp;
|
2018-02-08 09:02:12 +00:00
|
|
|
|
2018-06-26 13:16:54 +00:00
|
|
|
return( mbedtls_cipher_info_from_values( cipher_id_tmp,
|
|
|
|
(int) key_bits, mode ) );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-06-18 09:24:33 +00:00
|
|
|
static size_t psa_get_hash_block_size( psa_algorithm_t alg )
|
2018-06-17 11:41:10 +00:00
|
|
|
{
|
2018-06-18 13:41:12 +00:00
|
|
|
switch( alg )
|
2018-06-17 11:41:10 +00:00
|
|
|
{
|
|
|
|
case PSA_ALG_MD2:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 16 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_MD4:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 64 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_MD5:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 64 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_RIPEMD160:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 64 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_SHA_1:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 64 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_SHA_224:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 64 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_SHA_256:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 64 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_SHA_384:
|
2018-06-18 09:24:33 +00:00
|
|
|
return( 128 );
|
2018-06-17 11:41:10 +00:00
|
|
|
case PSA_ALG_SHA_512:
|
2018-06-18 13:41:12 +00:00
|
|
|
return( 128 );
|
|
|
|
default:
|
|
|
|
return( 0 );
|
2018-06-17 11:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
|
2018-06-07 10:27:47 +00:00
|
|
|
|
2018-06-20 14:05:20 +00:00
|
|
|
/* Initialize the MAC operation structure. Once this function has been
|
|
|
|
* called, psa_mac_abort can run and will do the right thing. */
|
|
|
|
static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
|
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
operation->alg = alg;
|
|
|
|
operation->key_set = 0;
|
|
|
|
operation->iv_set = 0;
|
|
|
|
operation->iv_required = 0;
|
|
|
|
operation->has_input = 0;
|
2018-07-08 18:12:23 +00:00
|
|
|
operation->is_sign = 0;
|
2018-06-20 14:05:20 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
|
|
|
if( alg == PSA_ALG_CMAC )
|
|
|
|
{
|
|
|
|
operation->iv_required = 0;
|
|
|
|
mbedtls_cipher_init( &operation->ctx.cmac );
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-06-20 14:05:20 +00:00
|
|
|
if( PSA_ALG_IS_HMAC( operation->alg ) )
|
|
|
|
{
|
2018-07-12 15:07:52 +00:00
|
|
|
/* We'll set up the hash operation later in psa_hmac_setup_internal. */
|
|
|
|
operation->ctx.hmac.hash_ctx.alg = 0;
|
|
|
|
status = PSA_SUCCESS;
|
2018-06-20 14:05:20 +00:00
|
|
|
}
|
|
|
|
else
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-06-20 14:05:20 +00:00
|
|
|
{
|
2018-06-20 14:21:04 +00:00
|
|
|
if( ! PSA_ALG_IS_MAC( alg ) )
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2018-06-20 14:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
memset( operation, 0, sizeof( *operation ) );
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-07-12 15:04:55 +00:00
|
|
|
static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
|
|
|
|
{
|
2018-12-07 17:14:53 +00:00
|
|
|
mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
|
2018-07-12 15:04:55 +00:00
|
|
|
return( psa_hash_abort( &hmac->hash_ctx ) );
|
|
|
|
}
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-07-12 15:04:55 +00:00
|
|
|
|
2018-02-08 09:02:12 +00:00
|
|
|
psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
|
|
|
|
{
|
2018-07-08 18:51:54 +00:00
|
|
|
if( operation->alg == 0 )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2018-07-08 18:51:54 +00:00
|
|
|
/* 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. */
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
else
|
2018-02-08 09:02:12 +00:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2018-07-08 18:51:54 +00:00
|
|
|
if( operation->alg == PSA_ALG_CMAC )
|
|
|
|
{
|
|
|
|
mbedtls_cipher_free( &operation->ctx.cmac );
|
|
|
|
}
|
|
|
|
else
|
2018-02-08 09:02:12 +00:00
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-07-08 18:51:54 +00:00
|
|
|
if( PSA_ALG_IS_HMAC( operation->alg ) )
|
|
|
|
{
|
2018-07-12 15:04:55 +00:00
|
|
|
psa_hmac_abort_internal( &operation->ctx.hmac );
|
2018-07-08 18:51:54 +00:00
|
|
|
}
|
|
|
|
else
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-07-08 18:51:54 +00:00
|
|
|
{
|
|
|
|
/* Sanity check (shouldn't happen: operation->alg should
|
|
|
|
* always have been initialized to a valid value). */
|
|
|
|
goto bad_state;
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
2018-04-04 12:43:05 +00:00
|
|
|
|
2018-02-08 09:02:12 +00:00
|
|
|
operation->alg = 0;
|
|
|
|
operation->key_set = 0;
|
|
|
|
operation->iv_set = 0;
|
|
|
|
operation->iv_required = 0;
|
|
|
|
operation->has_input = 0;
|
2018-07-08 18:12:23 +00:00
|
|
|
operation->is_sign = 0;
|
2018-04-04 12:43:05 +00:00
|
|
|
|
2018-02-08 09:02:12 +00:00
|
|
|
return( PSA_SUCCESS );
|
2018-07-08 18:51:54 +00:00
|
|
|
|
|
|
|
bad_state:
|
|
|
|
/* If abort is called on an uninitialized object, we can't trust
|
|
|
|
* anything. Wipe the object in case it contains confidential data.
|
|
|
|
* This may result in a memory leak if a pointer gets overwritten,
|
|
|
|
* but it's too late to do anything about this. */
|
|
|
|
memset( operation, 0, sizeof( *operation ) );
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 09:57:35 +00:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2018-07-08 18:12:23 +00:00
|
|
|
static int psa_cmac_setup( psa_mac_operation_t *operation,
|
2018-06-11 15:26:17 +00:00
|
|
|
size_t key_bits,
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot,
|
2018-06-11 15:26:17 +00:00
|
|
|
const mbedtls_cipher_info_t *cipher_info )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-06-11 15:26:17 +00:00
|
|
|
|
|
|
|
operation->mac_size = cipher_info->block_size;
|
|
|
|
|
|
|
|
ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.data,
|
2018-06-11 15:26:17 +00:00
|
|
|
key_bits );
|
|
|
|
return( ret );
|
|
|
|
}
|
2018-06-19 09:57:35 +00:00
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-07-12 15:04:55 +00:00
|
|
|
static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
|
|
|
|
const uint8_t *key,
|
|
|
|
size_t key_length,
|
|
|
|
psa_algorithm_t hash_alg )
|
2018-06-11 15:26:17 +00:00
|
|
|
{
|
2019-07-15 09:06:38 +00:00
|
|
|
uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
|
2018-06-11 15:26:17 +00:00
|
|
|
size_t i;
|
2020-12-18 13:23:51 +00:00
|
|
|
size_t hash_size = PSA_HASH_LENGTH( hash_alg );
|
2018-07-12 15:04:55 +00:00
|
|
|
size_t block_size = psa_get_hash_block_size( hash_alg );
|
2018-06-11 15:26:17 +00:00
|
|
|
psa_status_t status;
|
|
|
|
|
2018-07-15 22:36:29 +00:00
|
|
|
/* 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. */
|
|
|
|
/* The size checks against the ipad and opad buffers cannot be written
|
|
|
|
* `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
|
|
|
|
* because that triggers -Wlogical-op on GCC 7.3. */
|
|
|
|
if( block_size > sizeof( ipad ) )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
if( block_size > sizeof( hmac->opad ) )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
if( block_size < hash_size )
|
2018-06-11 15:26:17 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
2018-06-11 16:12:58 +00:00
|
|
|
if( key_length > block_size )
|
2018-06-11 15:26:17 +00:00
|
|
|
{
|
2019-11-28 19:07:20 +00:00
|
|
|
status = psa_hash_compute( hash_alg, key, key_length,
|
|
|
|
ipad, sizeof( ipad ), &key_length );
|
2018-06-11 15:26:17 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-07-17 14:24:34 +00:00
|
|
|
goto cleanup;
|
2018-06-11 15:26:17 +00:00
|
|
|
}
|
2018-07-12 15:07:03 +00:00
|
|
|
/* A 0-length key is not commonly used in HMAC when used as a MAC,
|
|
|
|
* but it is permitted. It is common when HMAC is used in HKDF, for
|
|
|
|
* example. Don't call `memcpy` in the 0-length because `key` could be
|
|
|
|
* an invalid pointer which would make the behavior undefined. */
|
|
|
|
else if( key_length != 0 )
|
2018-07-12 15:04:55 +00:00
|
|
|
memcpy( ipad, key, key_length );
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2018-06-11 16:12:58 +00:00
|
|
|
/* ipad contains the key followed by garbage. Xor and fill with 0x36
|
|
|
|
* to create the ipad value. */
|
|
|
|
for( i = 0; i < key_length; i++ )
|
|
|
|
ipad[i] ^= 0x36;
|
|
|
|
memset( ipad + key_length, 0x36, block_size - key_length );
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2018-06-11 16:12:58 +00:00
|
|
|
/* Copy the key material from ipad to opad, flipping the requisite bits,
|
|
|
|
* and filling the rest of opad with the requisite constant. */
|
2018-06-11 15:26:17 +00:00
|
|
|
for( i = 0; i < key_length; i++ )
|
2018-07-12 15:04:55 +00:00
|
|
|
hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
|
|
|
|
memset( hmac->opad + key_length, 0x5C, block_size - key_length );
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2018-07-12 15:04:55 +00:00
|
|
|
status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
|
2018-06-11 15:26:17 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-06-11 15:42:48 +00:00
|
|
|
goto cleanup;
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2018-07-12 15:04:55 +00:00
|
|
|
status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
|
2018-06-11 15:42:48 +00:00
|
|
|
|
|
|
|
cleanup:
|
2020-08-05 13:43:42 +00:00
|
|
|
mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
|
2018-06-11 15:42:48 +00:00
|
|
|
|
2018-06-11 15:26:17 +00:00
|
|
|
return( status );
|
|
|
|
}
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2018-07-08 18:12:23 +00:00
|
|
|
static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-07-08 18:12:23 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
int is_sign )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-02-08 09:02:12 +00:00
|
|
|
size_t key_bits;
|
2018-07-08 18:12:23 +00:00
|
|
|
psa_key_usage_t usage =
|
2019-11-26 16:01:59 +00:00
|
|
|
is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
|
2019-07-15 09:06:38 +00:00
|
|
|
uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
|
2018-10-17 16:28:05 +00:00
|
|
|
psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
|
2018-02-08 09:02:12 +00:00
|
|
|
|
2019-02-19 09:25:10 +00:00
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
|
|
|
if( operation->alg != 0 )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:18:45 +00:00
|
|
|
status = psa_mac_init( operation, full_length_alg );
|
2018-06-20 14:05:20 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-07-08 18:12:23 +00:00
|
|
|
if( is_sign )
|
|
|
|
operation->is_sign = 1;
|
2018-02-08 09:02:12 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
key, &slot, usage, alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-07-08 18:51:54 +00:00
|
|
|
goto exit;
|
2019-04-18 10:53:01 +00:00
|
|
|
key_bits = psa_get_key_slot_bits( slot );
|
2018-07-06 14:07:47 +00:00
|
|
|
|
2018-07-08 18:51:54 +00:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2018-08-14 13:18:45 +00:00
|
|
|
if( full_length_alg == PSA_ALG_CMAC )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2018-07-08 18:51:54 +00:00
|
|
|
const mbedtls_cipher_info_t *cipher_info =
|
2018-08-14 13:18:45 +00:00
|
|
|
mbedtls_cipher_info_from_psa( full_length_alg,
|
2019-07-30 18:06:31 +00:00
|
|
|
slot->attr.type, key_bits, NULL );
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 09:02:12 +00:00
|
|
|
if( cipher_info == NULL )
|
2018-07-08 18:51:54 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-02-08 09:02:12 +00:00
|
|
|
operation->mac_size = cipher_info->block_size;
|
2018-07-08 18:51:54 +00:00
|
|
|
ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
|
|
|
|
status = mbedtls_to_psa_error( ret );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
2018-07-08 18:51:54 +00:00
|
|
|
else
|
2018-02-08 09:02:12 +00:00
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-08-14 13:18:45 +00:00
|
|
|
if( PSA_ALG_IS_HMAC( full_length_alg ) )
|
2018-07-08 18:51:54 +00:00
|
|
|
{
|
2018-08-22 16:25:41 +00:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
|
2018-07-12 15:04:55 +00:00
|
|
|
if( hash_alg == 0 )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-07-15 22:36:29 +00:00
|
|
|
|
2020-12-18 13:23:51 +00:00
|
|
|
operation->mac_size = PSA_HASH_LENGTH( hash_alg );
|
2018-07-15 22:36:29 +00:00
|
|
|
/* Sanity check. This shouldn't fail on a valid configuration. */
|
|
|
|
if( operation->mac_size == 0 ||
|
|
|
|
operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:06:31 +00:00
|
|
|
if( slot->attr.type != PSA_KEY_TYPE_HMAC )
|
2018-07-12 15:04:55 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-07-15 22:36:29 +00:00
|
|
|
|
2018-07-12 15:04:55 +00:00
|
|
|
status = psa_hmac_setup_internal( &operation->ctx.hmac,
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
2018-07-12 15:04:55 +00:00
|
|
|
hash_alg );
|
2018-07-08 18:51:54 +00:00
|
|
|
}
|
|
|
|
else
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-07-08 18:51:54 +00:00
|
|
|
{
|
2018-11-23 15:11:20 +00:00
|
|
|
(void) key_bits;
|
2018-07-08 18:51:54 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
2018-06-11 15:26:17 +00:00
|
|
|
|
2018-08-14 13:18:45 +00:00
|
|
|
if( truncated == 0 )
|
|
|
|
{
|
|
|
|
/* The "normal" case: untruncated algorithm. Nothing to do. */
|
|
|
|
}
|
|
|
|
else if( truncated < 4 )
|
|
|
|
{
|
2018-08-21 12:55:08 +00:00
|
|
|
/* A very short MAC is too short for security since it can be
|
|
|
|
* brute-forced. Ancient protocols with 32-bit MACs do exist,
|
|
|
|
* so we make this our minimum, even though 32 bits is still
|
|
|
|
* too small for security. */
|
2018-08-14 13:18:45 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
else if( truncated > operation->mac_size )
|
|
|
|
{
|
|
|
|
/* It's impossible to "truncate" to a larger length. */
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
operation->mac_size = truncated;
|
|
|
|
|
2018-07-08 18:51:54 +00:00
|
|
|
exit:
|
2018-06-11 15:26:17 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2018-06-11 15:42:48 +00:00
|
|
|
psa_mac_abort( operation );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
2018-06-11 15:26:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
operation->key_set = 1;
|
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 18:12:23 +00:00
|
|
|
psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-07-08 18:12:23 +00:00
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
2020-08-04 07:51:30 +00:00
|
|
|
return( psa_mac_setup( operation, key, alg, 1 ) );
|
2018-07-08 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-07-08 18:12:23 +00:00
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
2020-08-04 07:51:30 +00:00
|
|
|
return( psa_mac_setup( operation, key, alg, 0 ) );
|
2018-07-08 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:02:12 +00:00
|
|
|
psa_status_t psa_mac_update( psa_mac_operation_t *operation,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length )
|
|
|
|
{
|
2018-07-08 18:51:54 +00:00
|
|
|
psa_status_t status = PSA_ERROR_BAD_STATE;
|
2018-02-08 09:02:12 +00:00
|
|
|
if( ! operation->key_set )
|
2019-02-20 15:37:15 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-08 09:02:12 +00:00
|
|
|
if( operation->iv_required && ! operation->iv_set )
|
2019-02-20 15:37:15 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-08 09:02:12 +00:00
|
|
|
operation->has_input = 1;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2018-07-08 18:51:54 +00:00
|
|
|
if( operation->alg == PSA_ALG_CMAC )
|
|
|
|
{
|
|
|
|
int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
|
2018-02-08 09:02:12 +00:00
|
|
|
input, input_length );
|
2018-07-08 18:51:54 +00:00
|
|
|
status = mbedtls_to_psa_error( ret );
|
|
|
|
}
|
|
|
|
else
|
2018-02-08 09:02:12 +00:00
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-07-08 18:51:54 +00:00
|
|
|
if( PSA_ALG_IS_HMAC( operation->alg ) )
|
|
|
|
{
|
|
|
|
status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
|
|
|
|
input_length );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
2018-07-08 18:51:54 +00:00
|
|
|
else
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-06-04 13:03:32 +00:00
|
|
|
{
|
2018-07-08 18:51:54 +00:00
|
|
|
/* This shouldn't happen if `operation` was initialized by
|
|
|
|
* a setup function. */
|
2019-02-20 15:37:15 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-06-04 13:03:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 18:51:54 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
psa_mac_abort( operation );
|
2018-06-18 14:04:39 +00:00
|
|
|
return( status );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-07-12 15:04:55 +00:00
|
|
|
static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
|
|
|
|
uint8_t *mac,
|
|
|
|
size_t mac_size )
|
|
|
|
{
|
2019-07-15 09:06:38 +00:00
|
|
|
uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
|
2018-07-12 15:04:55 +00:00
|
|
|
psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
|
|
|
|
size_t hash_size = 0;
|
|
|
|
size_t block_size = psa_get_hash_block_size( hash_alg );
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
/* From here on, tmp needs to be wiped. */
|
|
|
|
|
|
|
|
status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2018-08-14 13:18:45 +00:00
|
|
|
status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
memcpy( mac, tmp, mac_size );
|
2018-07-12 15:04:55 +00:00
|
|
|
|
|
|
|
exit:
|
2018-12-07 17:14:53 +00:00
|
|
|
mbedtls_platform_zeroize( tmp, hash_size );
|
2018-07-12 15:04:55 +00:00
|
|
|
return( status );
|
|
|
|
}
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-07-12 15:04:55 +00:00
|
|
|
|
2018-04-02 15:34:15 +00:00
|
|
|
static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
|
2018-06-18 13:41:12 +00:00
|
|
|
uint8_t *mac,
|
2018-07-08 18:35:02 +00:00
|
|
|
size_t mac_size )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2018-07-02 10:15:39 +00:00
|
|
|
if( ! operation->key_set )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
if( operation->iv_required && ! operation->iv_set )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2018-02-08 09:02:12 +00:00
|
|
|
if( mac_size < operation->mac_size )
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2018-07-08 18:51:54 +00:00
|
|
|
if( operation->alg == PSA_ALG_CMAC )
|
|
|
|
{
|
2020-12-18 13:23:51 +00:00
|
|
|
uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
|
2018-08-14 13:18:45 +00:00
|
|
|
int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
|
|
|
|
if( ret == 0 )
|
2018-08-21 12:55:49 +00:00
|
|
|
memcpy( mac, tmp, operation->mac_size );
|
2018-12-07 17:14:53 +00:00
|
|
|
mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
|
2018-07-08 18:51:54 +00:00
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
|
|
|
else
|
2018-02-08 09:02:12 +00:00
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2018-07-08 18:51:54 +00:00
|
|
|
if( PSA_ALG_IS_HMAC( operation->alg ) )
|
|
|
|
{
|
2018-07-12 15:04:55 +00:00
|
|
|
return( psa_hmac_finish_internal( &operation->ctx.hmac,
|
2018-08-14 13:18:45 +00:00
|
|
|
mac, operation->mac_size ) );
|
2018-07-08 18:51:54 +00:00
|
|
|
}
|
|
|
|
else
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2018-07-08 18:51:54 +00:00
|
|
|
{
|
|
|
|
/* This shouldn't happen if `operation` was initialized by
|
|
|
|
* a setup function. */
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 17:56:25 +00:00
|
|
|
psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
|
|
|
|
uint8_t *mac,
|
|
|
|
size_t mac_size,
|
|
|
|
size_t *mac_length )
|
2018-04-02 15:34:15 +00:00
|
|
|
{
|
2018-07-08 18:35:02 +00:00
|
|
|
psa_status_t status;
|
|
|
|
|
2019-02-15 14:05:35 +00:00
|
|
|
if( operation->alg == 0 )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
|
2018-07-08 18:35:02 +00:00
|
|
|
/* Fill the output buffer with something that isn't a valid mac
|
|
|
|
* (barring an attack on the mac and deliberately-crafted input),
|
|
|
|
* in case the caller doesn't check the return status properly. */
|
|
|
|
*mac_length = mac_size;
|
|
|
|
/* If mac_size is 0 then mac may be NULL and then the
|
|
|
|
* call to memset would have undefined behavior. */
|
|
|
|
if( mac_size != 0 )
|
|
|
|
memset( mac, '!', mac_size );
|
|
|
|
|
2018-07-08 18:12:23 +00:00
|
|
|
if( ! operation->is_sign )
|
|
|
|
{
|
2019-02-20 15:37:15 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-07-08 18:12:23 +00:00
|
|
|
}
|
2018-04-02 15:34:15 +00:00
|
|
|
|
2018-07-08 18:35:02 +00:00
|
|
|
status = psa_mac_finish_internal( operation, mac, mac_size );
|
|
|
|
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_mac_abort( operation );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
*mac_length = operation->mac_size;
|
|
|
|
else
|
|
|
|
memset( mac, '!', mac_size );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psa_mac_abort( operation );
|
|
|
|
return( status );
|
2018-04-02 15:34:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 17:56:25 +00:00
|
|
|
psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
|
|
|
|
const uint8_t *mac,
|
|
|
|
size_t mac_length )
|
2018-02-08 09:02:12 +00:00
|
|
|
{
|
2018-06-18 21:25:51 +00:00
|
|
|
uint8_t actual_mac[PSA_MAC_MAX_SIZE];
|
2018-04-02 15:34:15 +00:00
|
|
|
psa_status_t status;
|
|
|
|
|
2019-02-15 14:05:35 +00:00
|
|
|
if( operation->alg == 0 )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
|
2018-07-08 18:12:23 +00:00
|
|
|
if( operation->is_sign )
|
|
|
|
{
|
2019-02-20 15:37:15 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-07-08 18:35:02 +00:00
|
|
|
}
|
|
|
|
if( operation->mac_size != mac_length )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
|
|
|
goto cleanup;
|
2018-07-08 18:12:23 +00:00
|
|
|
}
|
2018-04-02 15:34:15 +00:00
|
|
|
|
|
|
|
status = psa_mac_finish_internal( operation,
|
2018-07-08 18:35:02 +00:00
|
|
|
actual_mac, sizeof( actual_mac ) );
|
2020-01-20 15:31:06 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
2018-07-08 18:35:02 +00:00
|
|
|
|
|
|
|
if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
status = psa_mac_abort( operation );
|
|
|
|
else
|
|
|
|
psa_mac_abort( operation );
|
|
|
|
|
2018-12-07 17:14:53 +00:00
|
|
|
mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
|
2018-08-14 13:18:45 +00:00
|
|
|
|
2018-07-08 18:35:02 +00:00
|
|
|
return( status );
|
2018-02-08 09:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-03 21:44:14 +00:00
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* Asymmetric cryptography */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2018-06-08 14:34:46 +00:00
|
|
|
/* Decode the hash algorithm from alg and store the mbedtls encoding in
|
2018-06-29 21:36:35 +00:00
|
|
|
* md_alg. Verify that the hash length is acceptable. */
|
2018-06-08 14:34:46 +00:00
|
|
|
static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
|
|
|
|
size_t hash_length,
|
|
|
|
mbedtls_md_type_t *md_alg )
|
2018-02-03 21:44:14 +00:00
|
|
|
{
|
2018-06-26 13:50:08 +00:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
|
2018-06-08 14:09:36 +00:00
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
|
2018-06-29 21:36:35 +00:00
|
|
|
*md_alg = mbedtls_md_get_type( md_info );
|
|
|
|
|
|
|
|
/* The Mbed TLS RSA module uses an unsigned int for hash length
|
|
|
|
* parameters. Validate that it fits so that we don't risk an
|
|
|
|
* overflow later. */
|
2018-06-04 13:40:31 +00:00
|
|
|
#if SIZE_MAX > UINT_MAX
|
2018-06-29 21:36:35 +00:00
|
|
|
if( hash_length > UINT_MAX )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-06-04 13:40:31 +00:00
|
|
|
#endif
|
2018-06-29 21:36:35 +00:00
|
|
|
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
2018-06-29 21:36:35 +00:00
|
|
|
/* For PKCS#1 v1.5 signature, if using a hash, the hash length
|
|
|
|
* must be correct. */
|
|
|
|
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
|
|
|
|
alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
|
2018-06-04 13:40:31 +00:00
|
|
|
{
|
2018-06-29 21:36:35 +00:00
|
|
|
if( md_info == NULL )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2018-06-08 14:09:36 +00:00
|
|
|
if( mbedtls_md_get_size( md_info ) != hash_length )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-06-29 21:36:35 +00:00
|
|
|
}
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
2018-06-29 21:36:35 +00:00
|
|
|
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2018-06-29 21:36:35 +00:00
|
|
|
/* PSS requires a hash internally. */
|
|
|
|
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
|
|
|
{
|
2018-06-08 14:09:36 +00:00
|
|
|
if( md_info == NULL )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2018-06-04 13:40:31 +00:00
|
|
|
}
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
|
2018-06-29 21:36:35 +00:00
|
|
|
|
2018-06-08 14:09:36 +00:00
|
|
|
return( PSA_SUCCESS );
|
2018-06-04 13:40:31 +00:00
|
|
|
}
|
2018-02-03 21:44:14 +00:00
|
|
|
|
2018-06-27 13:42:46 +00:00
|
|
|
static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length,
|
|
|
|
uint8_t *signature,
|
|
|
|
size_t signature_size,
|
|
|
|
size_t *signature_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-06-27 13:42:46 +00:00
|
|
|
mbedtls_md_type_t md_alg;
|
|
|
|
|
|
|
|
status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2018-06-29 15:49:35 +00:00
|
|
|
if( signature_size < mbedtls_rsa_get_len( rsa ) )
|
2018-06-27 13:42:46 +00:00
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
2018-06-27 13:42:46 +00:00
|
|
|
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
|
|
|
|
{
|
|
|
|
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
|
|
|
|
MBEDTLS_MD_NONE );
|
|
|
|
ret = mbedtls_rsa_pkcs1_sign( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2018-06-27 13:42:46 +00:00
|
|
|
MBEDTLS_RSA_PRIVATE,
|
2018-06-26 13:20:51 +00:00
|
|
|
md_alg,
|
|
|
|
(unsigned int) hash_length,
|
|
|
|
hash,
|
2018-06-27 13:42:46 +00:00
|
|
|
signature );
|
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2018-06-27 13:42:46 +00:00
|
|
|
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
|
|
|
{
|
|
|
|
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
|
|
|
ret = mbedtls_rsa_rsassa_pss_sign( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2018-06-27 13:42:46 +00:00
|
|
|
MBEDTLS_RSA_PRIVATE,
|
2018-06-29 21:36:35 +00:00
|
|
|
MBEDTLS_MD_NONE,
|
2018-06-26 13:20:51 +00:00
|
|
|
(unsigned int) hash_length,
|
|
|
|
hash,
|
2018-06-27 13:42:46 +00:00
|
|
|
signature );
|
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
|
2018-06-27 13:42:46 +00:00
|
|
|
{
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret == 0 )
|
2018-06-29 15:49:35 +00:00
|
|
|
*signature_length = mbedtls_rsa_get_len( rsa );
|
2018-06-27 13:42:46 +00:00
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length,
|
|
|
|
const uint8_t *signature,
|
|
|
|
size_t signature_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-06-27 13:42:46 +00:00
|
|
|
mbedtls_md_type_t md_alg;
|
|
|
|
|
|
|
|
status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2019-09-12 20:08:23 +00:00
|
|
|
if( signature_length != mbedtls_rsa_get_len( rsa ) )
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
2018-06-27 13:42:46 +00:00
|
|
|
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
2018-06-27 13:42:46 +00:00
|
|
|
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
|
|
|
|
{
|
|
|
|
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
|
|
|
|
MBEDTLS_MD_NONE );
|
|
|
|
ret = mbedtls_rsa_pkcs1_verify( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2018-06-27 13:42:46 +00:00
|
|
|
MBEDTLS_RSA_PUBLIC,
|
|
|
|
md_alg,
|
2018-06-26 13:20:51 +00:00
|
|
|
(unsigned int) hash_length,
|
2018-06-27 13:42:46 +00:00
|
|
|
hash,
|
|
|
|
signature );
|
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2018-06-27 13:42:46 +00:00
|
|
|
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
|
|
|
{
|
|
|
|
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
|
|
|
ret = mbedtls_rsa_rsassa_pss_verify( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2018-06-27 13:42:46 +00:00
|
|
|
MBEDTLS_RSA_PUBLIC,
|
2018-06-29 21:36:35 +00:00
|
|
|
MBEDTLS_MD_NONE,
|
2018-06-26 13:20:51 +00:00
|
|
|
(unsigned int) hash_length,
|
|
|
|
hash,
|
2018-06-27 13:42:46 +00:00
|
|
|
signature );
|
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
|
2018-06-27 13:42:46 +00:00
|
|
|
{
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
2018-09-13 18:37:48 +00:00
|
|
|
|
|
|
|
/* Mbed TLS distinguishes "invalid padding" from "valid padding but
|
|
|
|
* the rest of the signature is invalid". This has little use in
|
|
|
|
* practice and PSA doesn't report this distinction. */
|
|
|
|
if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
2018-06-27 13:42:46 +00:00
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
|
2020-11-04 20:28:15 +00:00
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2018-06-26 14:10:23 +00:00
|
|
|
/* `ecp` cannot be const because `ecp->grp` needs to be non-const
|
|
|
|
* for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
|
|
|
|
* (even though these functions don't modify it). */
|
|
|
|
static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length,
|
|
|
|
uint8_t *signature,
|
|
|
|
size_t signature_size,
|
|
|
|
size_t *signature_length )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-06-26 14:10:23 +00:00
|
|
|
mbedtls_mpi r, s;
|
2018-06-28 11:56:01 +00:00
|
|
|
size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
|
2018-06-26 14:10:23 +00:00
|
|
|
mbedtls_mpi_init( &r );
|
|
|
|
mbedtls_mpi_init( &s );
|
|
|
|
|
2018-06-28 11:56:01 +00:00
|
|
|
if( signature_size < 2 * curve_bytes )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-06-26 14:10:23 +00:00
|
|
|
|
2020-10-14 02:58:20 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2018-06-26 14:10:23 +00:00
|
|
|
if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
|
|
|
|
{
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
|
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
|
|
|
|
mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
|
2019-09-05 13:06:34 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
|
|
|
|
&ecp->d, hash,
|
|
|
|
hash_length, md_alg,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE ) );
|
2018-06-26 14:10:23 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-14 02:58:20 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2018-06-26 14:10:23 +00:00
|
|
|
{
|
2018-11-16 15:02:56 +00:00
|
|
|
(void) alg;
|
2018-06-26 14:10:23 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
|
|
|
|
hash, hash_length,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE ) );
|
2018-06-26 14:10:23 +00:00
|
|
|
}
|
2018-06-28 11:56:01 +00:00
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
|
|
|
|
signature,
|
|
|
|
curve_bytes ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
|
|
|
|
signature + curve_bytes,
|
|
|
|
curve_bytes ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_mpi_free( &r );
|
|
|
|
mbedtls_mpi_free( &s );
|
|
|
|
if( ret == 0 )
|
|
|
|
*signature_length = 2 * curve_bytes;
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length,
|
|
|
|
const uint8_t *signature,
|
|
|
|
size_t signature_length )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-06-28 11:56:01 +00:00
|
|
|
mbedtls_mpi r, s;
|
|
|
|
size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
|
|
|
|
mbedtls_mpi_init( &r );
|
|
|
|
mbedtls_mpi_init( &s );
|
|
|
|
|
|
|
|
if( signature_length != 2 * curve_bytes )
|
|
|
|
return( PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
|
|
|
|
signature,
|
|
|
|
curve_bytes ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
|
|
|
|
signature + curve_bytes,
|
|
|
|
curve_bytes ) );
|
|
|
|
|
2020-07-24 21:09:52 +00:00
|
|
|
/* Check whether the public part is loaded. If not, load it. */
|
|
|
|
if( mbedtls_ecp_is_zero( &ecp->Q ) )
|
|
|
|
{
|
|
|
|
MBEDTLS_MPI_CHK(
|
|
|
|
mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
|
2020-12-14 13:54:06 +00:00
|
|
|
mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
|
2020-07-24 21:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:56:01 +00:00
|
|
|
ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
|
|
|
|
&ecp->Q, &r, &s );
|
2018-06-26 14:10:23 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_mpi_free( &r );
|
|
|
|
mbedtls_mpi_free( &s );
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
|
|
|
}
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2018-06-26 14:10:23 +00:00
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
|
2019-11-26 16:01:59 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length,
|
|
|
|
uint8_t *signature,
|
|
|
|
size_t signature_size,
|
|
|
|
size_t *signature_length )
|
2018-02-03 21:44:14 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-06-28 10:21:19 +00:00
|
|
|
|
|
|
|
*signature_length = signature_size;
|
2019-09-12 20:05:59 +00:00
|
|
|
/* Immediately reject a zero-length signature buffer. This guarantees
|
|
|
|
* that signature must be a valid pointer. (On the other hand, the hash
|
|
|
|
* buffer can in principle be empty since it doesn't actually have
|
|
|
|
* to be a hash.) */
|
|
|
|
if( signature_size == 0 )
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
2018-06-28 10:21:19 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot_with_policy( key, &slot,
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH,
|
|
|
|
alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-06-28 10:21:19 +00:00
|
|
|
goto exit;
|
2019-07-30 18:06:31 +00:00
|
|
|
if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
|
2018-06-28 10:21:19 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-02-03 21:44:14 +00:00
|
|
|
|
2020-07-16 18:28:36 +00:00
|
|
|
/* Try any of the available accelerators first */
|
|
|
|
status = psa_driver_wrapper_sign_hash( slot,
|
|
|
|
alg,
|
|
|
|
hash,
|
|
|
|
hash_length,
|
|
|
|
signature,
|
|
|
|
signature_size,
|
|
|
|
signature_length );
|
2020-09-04 11:06:39 +00:00
|
|
|
if( status != PSA_ERROR_NOT_SUPPORTED ||
|
|
|
|
psa_key_lifetime_is_external( slot->attr.lifetime ) )
|
2020-07-16 18:28:36 +00:00
|
|
|
goto exit;
|
|
|
|
|
2020-07-17 14:43:05 +00:00
|
|
|
/* If the operation was not supported by any accelerator, try fallback. */
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
|
2018-02-03 21:44:14 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_rsa_load_representation( slot->attr.type,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2020-07-28 12:30:39 +00:00
|
|
|
status = psa_rsa_sign( rsa,
|
2018-06-28 10:21:19 +00:00
|
|
|
alg,
|
|
|
|
hash, hash_length,
|
|
|
|
signature, signature_size,
|
|
|
|
signature_length );
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
mbedtls_free( rsa );
|
2018-02-03 21:44:14 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
|
2019-07-30 18:06:31 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
|
2018-02-03 21:44:14 +00:00
|
|
|
{
|
2020-11-04 20:28:15 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2018-11-16 15:02:56 +00:00
|
|
|
if(
|
2020-10-14 02:58:20 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2018-11-16 15:02:56 +00:00
|
|
|
PSA_ALG_IS_ECDSA( alg )
|
|
|
|
#else
|
|
|
|
PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
|
|
|
|
#endif
|
|
|
|
)
|
2020-07-24 21:09:52 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_ecp_load_representation( slot->attr.type,
|
2021-01-27 14:44:45 +00:00
|
|
|
slot->attr.bits,
|
2020-11-25 13:58:33 +00:00
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&ecp );
|
2020-07-24 21:09:52 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2020-07-28 12:30:39 +00:00
|
|
|
status = psa_ecdsa_sign( ecp,
|
2018-06-28 10:21:19 +00:00
|
|
|
alg,
|
|
|
|
hash, hash_length,
|
|
|
|
signature, signature_size,
|
|
|
|
signature_length );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair_free( ecp );
|
|
|
|
mbedtls_free( ecp );
|
2020-07-24 21:09:52 +00:00
|
|
|
}
|
2018-06-26 14:10:23 +00:00
|
|
|
else
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2018-06-26 14:10:23 +00:00
|
|
|
{
|
2018-06-28 10:21:19 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2018-06-26 14:10:23 +00:00
|
|
|
}
|
2018-05-08 08:18:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-28 10:21:19 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2018-02-03 21:44:14 +00:00
|
|
|
}
|
2018-06-28 10:21:19 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
/* Fill the unused part of the output buffer (the whole buffer on error,
|
|
|
|
* the trailing part on success) with something that isn't a valid mac
|
|
|
|
* (barring an attack on the mac and deliberately-crafted input),
|
|
|
|
* in case the caller doesn't check the return status properly. */
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
memset( signature + *signature_length, '!',
|
|
|
|
signature_size - *signature_length );
|
2019-09-12 20:05:59 +00:00
|
|
|
else
|
2018-06-28 10:21:19 +00:00
|
|
|
memset( signature, '!', signature_size );
|
2018-06-28 17:31:31 +00:00
|
|
|
/* If signature_size is 0 then we have nothing to do. We must not call
|
|
|
|
* memset because signature may be NULL in this case. */
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-05-08 08:18:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
|
2019-11-26 16:01:59 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *hash,
|
|
|
|
size_t hash_length,
|
|
|
|
const uint8_t *signature,
|
|
|
|
size_t signature_length )
|
2018-05-08 08:18:38 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-06-27 13:42:46 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot_with_policy( key, &slot,
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH,
|
|
|
|
alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-05-08 08:18:38 +00:00
|
|
|
|
2020-07-17 17:46:15 +00:00
|
|
|
/* Try any of the available accelerators first */
|
|
|
|
status = psa_driver_wrapper_verify_hash( slot,
|
|
|
|
alg,
|
|
|
|
hash,
|
|
|
|
hash_length,
|
|
|
|
signature,
|
|
|
|
signature_length );
|
2020-09-04 11:06:39 +00:00
|
|
|
if( status != PSA_ERROR_NOT_SUPPORTED ||
|
|
|
|
psa_key_lifetime_is_external( slot->attr.lifetime ) )
|
2020-10-22 13:24:49 +00:00
|
|
|
goto exit;
|
2020-07-17 17:46:15 +00:00
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_rsa_load_representation( slot->attr.type,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-10-22 13:24:49 +00:00
|
|
|
goto exit;
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-07-28 12:30:39 +00:00
|
|
|
status = psa_rsa_verify( rsa,
|
2020-07-24 20:48:15 +00:00
|
|
|
alg,
|
|
|
|
hash, hash_length,
|
|
|
|
signature, signature_length );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
mbedtls_free( rsa );
|
2020-10-22 13:24:49 +00:00
|
|
|
goto exit;
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
|
2019-07-30 18:06:31 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
|
2018-05-08 08:18:38 +00:00
|
|
|
{
|
2020-11-04 20:28:15 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2018-06-26 14:10:23 +00:00
|
|
|
if( PSA_ALG_IS_ECDSA( alg ) )
|
2020-07-24 21:09:52 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_ecp_load_representation( slot->attr.type,
|
2021-01-27 14:44:45 +00:00
|
|
|
slot->attr.bits,
|
2020-11-25 13:58:33 +00:00
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&ecp );
|
2020-07-24 21:09:52 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-10-22 13:24:49 +00:00
|
|
|
goto exit;
|
2020-07-28 12:30:39 +00:00
|
|
|
status = psa_ecdsa_verify( ecp,
|
2020-07-24 21:09:52 +00:00
|
|
|
hash, hash_length,
|
|
|
|
signature, signature_length );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair_free( ecp );
|
|
|
|
mbedtls_free( ecp );
|
2020-10-22 13:24:49 +00:00
|
|
|
goto exit;
|
2020-07-24 21:09:52 +00:00
|
|
|
}
|
2018-06-26 14:10:23 +00:00
|
|
|
else
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2018-06-26 14:10:23 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
2018-06-26 14:10:23 +00:00
|
|
|
}
|
2018-05-08 08:18:38 +00:00
|
|
|
}
|
2018-02-03 21:44:14 +00:00
|
|
|
else
|
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2018-02-03 21:44:14 +00:00
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
|
|
|
exit:
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-02-03 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2018-06-29 22:21:29 +00:00
|
|
|
static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
|
|
|
|
mbedtls_rsa_context *rsa )
|
|
|
|
{
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
|
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
|
|
|
|
mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
|
|
|
|
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
|
|
|
}
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
2018-06-29 22:21:29 +00:00
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
|
2018-06-08 14:09:36 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
const uint8_t *salt,
|
|
|
|
size_t salt_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-07-06 15:01:38 +00:00
|
|
|
|
2018-07-24 14:34:10 +00:00
|
|
|
(void) input;
|
|
|
|
(void) input_length;
|
|
|
|
(void) salt;
|
|
|
|
(void) output;
|
|
|
|
(void) output_size;
|
|
|
|
|
2018-06-04 13:43:12 +00:00
|
|
|
*output_length = 0;
|
2018-05-02 20:16:26 +00:00
|
|
|
|
2018-06-30 17:04:35 +00:00
|
|
|
if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2019-07-30 18:06:31 +00:00
|
|
|
if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
|
|
|
|
PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
|
2020-10-22 13:24:49 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-05-02 20:16:26 +00:00
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_rsa_load_representation( slot->attr.type,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-08-03 12:46:03 +00:00
|
|
|
goto rsa_exit;
|
2020-07-28 12:30:39 +00:00
|
|
|
|
|
|
|
if( output_size < mbedtls_rsa_get_len( rsa ) )
|
2020-07-24 20:48:15 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto rsa_exit;
|
2020-07-24 20:48:15 +00:00
|
|
|
}
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
|
2018-05-31 11:16:08 +00:00
|
|
|
if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_rsa_pkcs1_encrypt( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2020-08-03 12:46:03 +00:00
|
|
|
MBEDTLS_RSA_PUBLIC,
|
|
|
|
input_length,
|
|
|
|
input,
|
|
|
|
output ) );
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2018-06-26 13:53:48 +00:00
|
|
|
if( PSA_ALG_IS_RSA_OAEP( alg ) )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
psa_rsa_oaep_set_padding_mode( alg, rsa );
|
2020-08-03 12:46:03 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_rsa_rsaes_oaep_encrypt( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2020-08-03 12:46:03 +00:00
|
|
|
MBEDTLS_RSA_PUBLIC,
|
|
|
|
salt, salt_length,
|
|
|
|
input_length,
|
|
|
|
input,
|
|
|
|
output ) );
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto rsa_exit;
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
2020-08-03 12:46:03 +00:00
|
|
|
rsa_exit:
|
|
|
|
if( status == PSA_SUCCESS )
|
2020-07-28 12:30:39 +00:00
|
|
|
*output_length = mbedtls_rsa_get_len( rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
mbedtls_free( rsa );
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
2020-11-10 16:50:04 +00:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
|
|
|
exit:
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-05-31 11:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
|
2018-06-08 14:09:36 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
const uint8_t *salt,
|
|
|
|
size_t salt_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-07-06 15:01:38 +00:00
|
|
|
|
2018-07-24 14:34:10 +00:00
|
|
|
(void) input;
|
|
|
|
(void) input_length;
|
|
|
|
(void) salt;
|
|
|
|
(void) output;
|
|
|
|
(void) output_size;
|
|
|
|
|
2018-06-04 13:43:12 +00:00
|
|
|
*output_length = 0;
|
2018-05-02 20:16:26 +00:00
|
|
|
|
2018-06-30 17:04:35 +00:00
|
|
|
if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2019-07-30 18:06:31 +00:00
|
|
|
if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
|
2020-10-22 13:24:49 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-05-02 20:16:26 +00:00
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_rsa_load_representation( slot->attr.type,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&rsa );
|
2020-07-24 20:48:15 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-10-22 13:24:49 +00:00
|
|
|
goto exit;
|
2018-05-02 20:16:26 +00:00
|
|
|
|
2020-07-28 12:30:39 +00:00
|
|
|
if( input_length != mbedtls_rsa_get_len( rsa ) )
|
2020-07-24 20:48:15 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto rsa_exit;
|
2020-07-24 20:48:15 +00:00
|
|
|
}
|
2018-05-02 20:16:26 +00:00
|
|
|
|
2020-11-01 05:06:54 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
|
2018-05-31 11:16:08 +00:00
|
|
|
if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_rsa_pkcs1_decrypt( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2020-08-03 12:46:03 +00:00
|
|
|
MBEDTLS_RSA_PRIVATE,
|
|
|
|
output_length,
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
output_size ) );
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2018-06-26 13:53:48 +00:00
|
|
|
if( PSA_ALG_IS_RSA_OAEP( alg ) )
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-07-28 12:30:39 +00:00
|
|
|
psa_rsa_oaep_set_padding_mode( alg, rsa );
|
2020-08-03 12:46:03 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_rsa_rsaes_oaep_decrypt( rsa,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
2020-08-03 12:46:03 +00:00
|
|
|
MBEDTLS_RSA_PRIVATE,
|
|
|
|
salt, salt_length,
|
|
|
|
output_length,
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
output_size ) );
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-01 05:06:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-08-03 12:46:03 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
2018-06-04 13:36:15 +00:00
|
|
|
|
2020-08-03 12:46:03 +00:00
|
|
|
rsa_exit:
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
mbedtls_free( rsa );
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
2018-05-02 20:16:26 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2018-05-02 20:16:26 +00:00
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
|
|
|
exit:
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-05-31 11:25:48 +00:00
|
|
|
}
|
2018-02-03 21:44:14 +00:00
|
|
|
|
2018-06-18 19:49:39 +00:00
|
|
|
|
|
|
|
|
2018-03-12 13:59:30 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Symmetric cryptography */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2018-06-04 14:22:46 +00:00
|
|
|
static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-06-04 14:23:10 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
mbedtls_operation_t cipher_operation )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2019-02-25 16:43:14 +00:00
|
|
|
int ret = 0;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-03-12 13:59:30 +00:00
|
|
|
size_t key_bits;
|
|
|
|
const mbedtls_cipher_info_t *cipher_info = NULL;
|
2018-07-06 15:01:38 +00:00
|
|
|
psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
|
|
|
|
PSA_KEY_USAGE_ENCRYPT :
|
|
|
|
PSA_KEY_USAGE_DECRYPT );
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2020-09-01 13:56:14 +00:00
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
|
|
|
if( operation->alg != 0 )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2020-09-10 12:54:14 +00:00
|
|
|
/* The requested algorithm must be one that can be processed by cipher. */
|
|
|
|
if( ! PSA_ALG_IS_CIPHER( alg ) )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
/* Fetch key material from key storage. */
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
|
2020-09-11 09:44:50 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* Initialize the operation struct members, except for alg. The alg member
|
|
|
|
* is used to indicate to psa_cipher_abort that there are resources to free,
|
|
|
|
* so we only set it after resources have been allocated/initialized. */
|
2020-09-10 12:54:14 +00:00
|
|
|
operation->key_set = 0;
|
|
|
|
operation->iv_set = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
operation->mbedtls_in_use = 0;
|
2020-09-10 12:54:14 +00:00
|
|
|
operation->iv_size = 0;
|
|
|
|
operation->block_size = 0;
|
|
|
|
if( alg == PSA_ALG_ECB_NO_PADDING )
|
|
|
|
operation->iv_required = 0;
|
|
|
|
else
|
|
|
|
operation->iv_required = 1;
|
|
|
|
|
|
|
|
/* Try doing the operation through a driver before using software fallback. */
|
2020-07-28 16:49:51 +00:00
|
|
|
if( cipher_operation == MBEDTLS_ENCRYPT )
|
2020-09-09 10:01:43 +00:00
|
|
|
status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
|
2020-07-28 16:49:51 +00:00
|
|
|
slot,
|
|
|
|
alg );
|
|
|
|
else
|
2020-09-09 10:01:43 +00:00
|
|
|
status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
|
2020-07-28 16:49:51 +00:00
|
|
|
slot,
|
|
|
|
alg );
|
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
2020-09-14 11:14:31 +00:00
|
|
|
{
|
2020-09-11 09:44:50 +00:00
|
|
|
/* Once the driver context is initialised, it needs to be freed using
|
|
|
|
* psa_cipher_abort. Indicate this through setting alg. */
|
|
|
|
operation->alg = alg;
|
2020-09-14 11:14:31 +00:00
|
|
|
}
|
2020-09-11 09:44:50 +00:00
|
|
|
|
2020-09-01 13:56:14 +00:00
|
|
|
if( status != PSA_ERROR_NOT_SUPPORTED ||
|
|
|
|
psa_key_lifetime_is_external( slot->attr.lifetime ) )
|
|
|
|
goto exit;
|
|
|
|
|
2020-09-10 12:54:14 +00:00
|
|
|
/* Proceed with initializing an mbed TLS cipher context if no driver is
|
2020-09-01 13:56:14 +00:00
|
|
|
* available for the given algorithm & key. */
|
|
|
|
mbedtls_cipher_init( &operation->ctx.cipher );
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2020-09-10 12:54:14 +00:00
|
|
|
/* Once the cipher context is initialised, it needs to be freed using
|
2020-09-11 09:44:50 +00:00
|
|
|
* psa_cipher_abort. Indicate there is something to be freed through setting
|
|
|
|
* alg, and indicate the operation is being done using mbedtls crypto through
|
|
|
|
* setting mbedtls_in_use. */
|
2020-09-10 12:54:14 +00:00
|
|
|
operation->alg = alg;
|
2020-09-11 09:44:50 +00:00
|
|
|
operation->mbedtls_in_use = 1;
|
2020-09-10 12:54:14 +00:00
|
|
|
|
2019-04-18 10:53:01 +00:00
|
|
|
key_bits = psa_get_key_slot_bits( slot );
|
2019-07-30 18:06:31 +00:00
|
|
|
cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
|
2018-03-12 13:59:30 +00:00
|
|
|
if( cipher_info == NULL )
|
2019-02-25 16:43:14 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-03-12 13:59:30 +00:00
|
|
|
|
|
|
|
ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
|
2018-04-04 12:43:05 +00:00
|
|
|
if( ret != 0 )
|
2019-02-25 16:43:14 +00:00
|
|
|
goto exit;
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2021-02-11 18:02:00 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
|
2018-06-21 07:40:04 +00:00
|
|
|
{
|
|
|
|
/* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
|
2019-07-15 09:06:38 +00:00
|
|
|
uint8_t keys[24];
|
2020-11-25 16:52:23 +00:00
|
|
|
memcpy( keys, slot->key.data, 16 );
|
|
|
|
memcpy( keys + 16, slot->key.data, 8 );
|
2018-06-21 07:40:04 +00:00
|
|
|
ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
|
|
|
|
keys,
|
|
|
|
192, cipher_operation );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.data,
|
2018-06-26 13:16:54 +00:00
|
|
|
(int) key_bits, cipher_operation );
|
2018-06-21 07:40:04 +00:00
|
|
|
}
|
2018-04-04 12:43:05 +00:00
|
|
|
if( ret != 0 )
|
2019-02-25 16:43:14 +00:00
|
|
|
goto exit;
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2021-01-26 18:51:12 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
|
2018-08-21 12:02:45 +00:00
|
|
|
switch( alg )
|
2018-03-18 11:57:31 +00:00
|
|
|
{
|
2018-08-21 12:02:45 +00:00
|
|
|
case PSA_ALG_CBC_NO_PADDING:
|
|
|
|
ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
|
|
|
|
MBEDTLS_PADDING_NONE );
|
|
|
|
break;
|
|
|
|
case PSA_ALG_CBC_PKCS7:
|
|
|
|
ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
|
|
|
|
MBEDTLS_PADDING_PKCS7 );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* The algorithm doesn't involve padding. */
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( ret != 0 )
|
2019-02-25 16:43:14 +00:00
|
|
|
goto exit;
|
2021-02-09 23:27:55 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
|
2018-03-18 11:57:31 +00:00
|
|
|
|
2018-08-21 12:02:45 +00:00
|
|
|
operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
|
2020-12-18 13:23:51 +00:00
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
|
2020-08-25 09:47:50 +00:00
|
|
|
if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
|
|
|
|
alg != PSA_ALG_ECB_NO_PADDING )
|
2018-03-18 11:57:31 +00:00
|
|
|
{
|
2020-12-18 13:23:51 +00:00
|
|
|
operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
|
2018-03-18 11:57:31 +00:00
|
|
|
}
|
2021-02-16 19:54:35 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
|
2019-05-06 13:25:00 +00:00
|
|
|
else
|
2020-12-07 23:01:31 +00:00
|
|
|
if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
|
2019-05-06 13:25:00 +00:00
|
|
|
operation->iv_size = 12;
|
|
|
|
#endif
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2020-09-09 13:28:49 +00:00
|
|
|
status = PSA_SUCCESS;
|
|
|
|
|
2019-02-25 16:43:14 +00:00
|
|
|
exit:
|
2020-09-09 13:28:49 +00:00
|
|
|
if( ret != 0 )
|
2019-02-25 16:43:14 +00:00
|
|
|
status = mbedtls_to_psa_error( ret );
|
2020-09-09 13:28:49 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
/* Update operation flags for both driver and software implementations */
|
|
|
|
operation->key_set = 1;
|
|
|
|
}
|
|
|
|
else
|
2019-02-25 16:43:14 +00:00
|
|
|
psa_cipher_abort( operation );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 19:39:34 +00:00
|
|
|
psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-07-08 19:39:34 +00:00
|
|
|
psa_algorithm_t alg )
|
2018-03-18 11:57:31 +00:00
|
|
|
{
|
2020-08-04 07:51:30 +00:00
|
|
|
return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
|
2018-03-18 11:57:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 19:39:34 +00:00
|
|
|
psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-07-08 19:39:34 +00:00
|
|
|
psa_algorithm_t alg )
|
2018-03-18 11:57:31 +00:00
|
|
|
{
|
2020-08-04 07:51:30 +00:00
|
|
|
return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
|
2018-03-18 11:57:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 19:39:34 +00:00
|
|
|
psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
|
2019-05-15 11:33:23 +00:00
|
|
|
uint8_t *iv,
|
2018-07-08 19:39:34 +00:00
|
|
|
size_t iv_size,
|
|
|
|
size_t *iv_length )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2018-08-02 10:56:32 +00:00
|
|
|
psa_status_t status;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-09-09 13:28:49 +00:00
|
|
|
if( operation->iv_set || ! operation->iv_required )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2020-09-01 13:56:14 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( operation->mbedtls_in_use == 0 )
|
2020-09-03 13:30:32 +00:00
|
|
|
{
|
2020-09-09 10:01:43 +00:00
|
|
|
status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
|
2020-09-03 13:30:32 +00:00
|
|
|
iv,
|
|
|
|
iv_size,
|
|
|
|
iv_length );
|
|
|
|
goto exit;
|
|
|
|
}
|
2020-09-01 13:56:14 +00:00
|
|
|
|
2018-04-04 12:43:05 +00:00
|
|
|
if( iv_size < operation->iv_size )
|
|
|
|
{
|
2018-08-02 10:56:32 +00:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-04-04 12:43:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2020-12-14 13:54:06 +00:00
|
|
|
ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
|
2020-11-13 16:02:26 +00:00
|
|
|
iv, operation->iv_size );
|
2018-04-04 12:43:05 +00:00
|
|
|
if( ret != 0 )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2018-08-02 10:56:32 +00:00
|
|
|
status = mbedtls_to_psa_error( ret );
|
2018-06-04 14:22:46 +00:00
|
|
|
goto exit;
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
2018-06-04 14:22:46 +00:00
|
|
|
|
2018-03-18 11:57:31 +00:00
|
|
|
*iv_length = operation->iv_size;
|
2018-08-02 10:56:32 +00:00
|
|
|
status = psa_cipher_set_iv( operation, iv, *iv_length );
|
2018-04-04 12:43:05 +00:00
|
|
|
|
2018-05-31 11:07:14 +00:00
|
|
|
exit:
|
2020-09-09 13:28:49 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
operation->iv_set = 1;
|
|
|
|
else
|
2018-05-31 11:07:14 +00:00
|
|
|
psa_cipher_abort( operation );
|
2018-08-02 10:56:32 +00:00
|
|
|
return( status );
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 19:39:34 +00:00
|
|
|
psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
|
2019-05-15 11:33:23 +00:00
|
|
|
const uint8_t *iv,
|
2018-07-08 19:39:34 +00:00
|
|
|
size_t iv_length )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2018-08-02 10:56:32 +00:00
|
|
|
psa_status_t status;
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-09-09 13:28:49 +00:00
|
|
|
if( operation->iv_set || ! operation->iv_required )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2020-09-01 13:56:14 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( operation->mbedtls_in_use == 0 )
|
2020-09-03 13:30:32 +00:00
|
|
|
{
|
2020-09-09 10:01:43 +00:00
|
|
|
status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
|
2020-09-03 13:30:32 +00:00
|
|
|
iv,
|
|
|
|
iv_length );
|
|
|
|
goto exit;
|
|
|
|
}
|
2020-09-01 13:56:14 +00:00
|
|
|
|
2018-08-02 10:56:32 +00:00
|
|
|
if( iv_length != operation->iv_size )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2018-08-02 10:56:32 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
2018-08-02 10:56:32 +00:00
|
|
|
ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
|
|
|
|
status = mbedtls_to_psa_error( ret );
|
|
|
|
exit:
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
operation->iv_set = 1;
|
|
|
|
else
|
|
|
|
psa_cipher_abort( operation );
|
|
|
|
return( status );
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:14:14 +00:00
|
|
|
/* Process input for which the algorithm is set to ECB mode. This requires
|
|
|
|
* manual processing, since the PSA API is defined as being able to process
|
|
|
|
* arbitrary-length calls to psa_cipher_update() with ECB mode, but the
|
|
|
|
* underlying mbedtls_cipher_update only takes full blocks. */
|
|
|
|
static psa_status_t psa_cipher_update_ecb_internal(
|
|
|
|
mbedtls_cipher_context_t *ctx,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length )
|
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t block_size = ctx->cipher_info->block_size;
|
|
|
|
size_t internal_output_length = 0;
|
|
|
|
*output_length = 0;
|
|
|
|
|
|
|
|
if( input_length == 0 )
|
|
|
|
{
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctx->unprocessed_len > 0 )
|
|
|
|
{
|
|
|
|
/* Fill up to block size, and run the block if there's a full one. */
|
|
|
|
size_t bytes_to_copy = block_size - ctx->unprocessed_len;
|
|
|
|
|
|
|
|
if( input_length < bytes_to_copy )
|
|
|
|
bytes_to_copy = input_length;
|
|
|
|
|
|
|
|
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
|
|
|
|
input, bytes_to_copy );
|
|
|
|
input_length -= bytes_to_copy;
|
|
|
|
input += bytes_to_copy;
|
|
|
|
ctx->unprocessed_len += bytes_to_copy;
|
|
|
|
|
|
|
|
if( ctx->unprocessed_len == block_size )
|
|
|
|
{
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_cipher_update( ctx,
|
|
|
|
ctx->unprocessed_data,
|
|
|
|
block_size,
|
|
|
|
output, &internal_output_length ) );
|
|
|
|
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
output += internal_output_length;
|
|
|
|
output_size -= internal_output_length;
|
|
|
|
*output_length += internal_output_length;
|
|
|
|
ctx->unprocessed_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while( input_length >= block_size )
|
|
|
|
{
|
|
|
|
/* Run all full blocks we have, one by one */
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_cipher_update( ctx, input,
|
|
|
|
block_size,
|
|
|
|
output, &internal_output_length ) );
|
|
|
|
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
input_length -= block_size;
|
|
|
|
input += block_size;
|
|
|
|
|
|
|
|
output += internal_output_length;
|
|
|
|
output_size -= internal_output_length;
|
|
|
|
*output_length += internal_output_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( input_length > 0 )
|
|
|
|
{
|
|
|
|
/* Save unprocessed bytes for later processing */
|
|
|
|
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
|
|
|
|
input, input_length );
|
|
|
|
ctx->unprocessed_len += input_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2018-06-04 14:22:46 +00:00
|
|
|
psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
2019-05-15 11:33:23 +00:00
|
|
|
uint8_t *output,
|
2018-06-04 14:22:46 +00:00
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2020-08-25 13:13:13 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-06-04 15:17:16 +00:00
|
|
|
size_t expected_output_size;
|
2020-09-09 13:28:49 +00:00
|
|
|
if( operation->alg == 0 )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
if( operation->iv_required && ! operation->iv_set )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2019-02-15 14:12:05 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( operation->mbedtls_in_use == 0 )
|
2020-09-03 13:30:32 +00:00
|
|
|
{
|
2020-09-09 10:01:43 +00:00
|
|
|
status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
|
2020-09-03 13:30:32 +00:00
|
|
|
input,
|
|
|
|
input_length,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
output_length );
|
|
|
|
goto exit;
|
|
|
|
}
|
2020-09-01 13:56:14 +00:00
|
|
|
|
2018-08-21 12:02:45 +00:00
|
|
|
if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
|
2018-06-04 15:17:16 +00:00
|
|
|
{
|
|
|
|
/* Take the unprocessed partial block left over from previous
|
|
|
|
* update calls, if any, plus the input to this call. Remove
|
|
|
|
* the last partial block, if any. You get the data that will be
|
|
|
|
* output in this call. */
|
|
|
|
expected_output_size =
|
|
|
|
( operation->ctx.cipher.unprocessed_len + input_length )
|
|
|
|
/ operation->block_size * operation->block_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expected_output_size = input_length;
|
|
|
|
}
|
2018-08-02 10:56:32 +00:00
|
|
|
|
2018-06-04 15:17:16 +00:00
|
|
|
if( output_size < expected_output_size )
|
2018-08-02 10:56:32 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-03-12 16:16:40 +00:00
|
|
|
|
2020-08-25 13:13:13 +00:00
|
|
|
if( operation->alg == PSA_ALG_ECB_NO_PADDING )
|
|
|
|
{
|
|
|
|
/* mbedtls_cipher_update has an API inconsistency: it will only
|
|
|
|
* process a single block at a time in ECB mode. Abstract away that
|
|
|
|
* inconsistency here to match the PSA API behaviour. */
|
2020-09-07 15:14:14 +00:00
|
|
|
status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
|
|
|
|
input,
|
|
|
|
input_length,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
output_length );
|
2020-08-25 13:13:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_cipher_update( &operation->ctx.cipher, input,
|
|
|
|
input_length, output, output_length ) );
|
|
|
|
}
|
2018-08-02 10:56:32 +00:00
|
|
|
exit:
|
|
|
|
if( status != PSA_SUCCESS )
|
2018-03-12 13:59:30 +00:00
|
|
|
psa_cipher_abort( operation );
|
2018-08-02 10:56:32 +00:00
|
|
|
return( status );
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 14:22:46 +00:00
|
|
|
psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length )
|
2018-03-12 13:59:30 +00:00
|
|
|
{
|
2019-02-14 11:48:10 +00:00
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2018-06-04 14:22:46 +00:00
|
|
|
uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
|
2020-09-09 13:28:49 +00:00
|
|
|
if( operation->alg == 0 )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
if( operation->iv_required && ! operation->iv_set )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2018-04-22 17:19:20 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( operation->mbedtls_in_use == 0 )
|
2020-09-03 13:30:32 +00:00
|
|
|
{
|
2020-09-09 10:01:43 +00:00
|
|
|
status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
|
2020-09-03 13:30:32 +00:00
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
output_length );
|
2020-09-11 09:44:50 +00:00
|
|
|
goto exit;
|
2020-09-03 13:30:32 +00:00
|
|
|
}
|
2020-09-01 13:56:14 +00:00
|
|
|
|
2020-07-06 12:08:59 +00:00
|
|
|
if( operation->ctx.cipher.unprocessed_len != 0 )
|
2018-04-22 17:19:20 +00:00
|
|
|
{
|
2020-07-06 12:08:59 +00:00
|
|
|
if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
|
2020-09-28 14:11:33 +00:00
|
|
|
operation->alg == PSA_ALG_CBC_NO_PADDING )
|
2020-08-25 09:47:50 +00:00
|
|
|
{
|
2018-08-21 12:02:45 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2020-09-11 09:44:50 +00:00
|
|
|
goto exit;
|
2020-07-06 12:08:59 +00:00
|
|
|
}
|
2018-04-30 12:45:34 +00:00
|
|
|
}
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_cipher_finish( &operation->ctx.cipher,
|
|
|
|
temp_output_buffer,
|
|
|
|
output_length ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2018-07-09 15:04:51 +00:00
|
|
|
|
2018-06-28 17:31:31 +00:00
|
|
|
if( *output_length == 0 )
|
2018-07-09 15:04:51 +00:00
|
|
|
; /* Nothing to copy. Note that output may be NULL in this case. */
|
2018-06-28 17:31:31 +00:00
|
|
|
else if( output_size >= *output_length )
|
2018-04-30 12:45:34 +00:00
|
|
|
memcpy( output, temp_output_buffer, *output_length );
|
|
|
|
else
|
2018-07-09 15:04:51 +00:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-03-12 13:59:30 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
exit:
|
|
|
|
if( operation->mbedtls_in_use == 1 )
|
|
|
|
mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
|
2018-07-09 15:04:51 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
return( psa_cipher_abort( operation ) );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*output_length = 0;
|
|
|
|
(void) psa_cipher_abort( operation );
|
2018-07-09 15:04:51 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
return( status );
|
|
|
|
}
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 14:22:46 +00:00
|
|
|
psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
|
|
|
|
{
|
2018-06-20 14:05:20 +00:00
|
|
|
if( operation->alg == 0 )
|
2018-06-26 13:04:31 +00:00
|
|
|
{
|
2020-09-10 12:54:14 +00:00
|
|
|
/* The object has (apparently) been initialized but it is not (yet)
|
2018-06-26 13:04:31 +00:00
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
* nothing to do. */
|
2018-06-20 14:05:20 +00:00
|
|
|
return( PSA_SUCCESS );
|
2018-06-26 13:04:31 +00:00
|
|
|
}
|
2018-06-20 14:05:20 +00:00
|
|
|
|
2018-06-21 14:57:07 +00:00
|
|
|
/* Sanity check (shouldn't happen: operation->alg should
|
|
|
|
* always have been initialized to a valid value). */
|
|
|
|
if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
if( operation->mbedtls_in_use == 0 )
|
2020-09-09 10:01:43 +00:00
|
|
|
psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
|
2020-09-01 13:56:14 +00:00
|
|
|
else
|
|
|
|
mbedtls_cipher_free( &operation->ctx.cipher );
|
2018-06-04 14:22:46 +00:00
|
|
|
|
2018-04-04 12:43:05 +00:00
|
|
|
operation->alg = 0;
|
2018-04-30 09:31:04 +00:00
|
|
|
operation->key_set = 0;
|
|
|
|
operation->iv_set = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
operation->mbedtls_in_use = 0;
|
2018-04-30 09:31:04 +00:00
|
|
|
operation->iv_size = 0;
|
2018-04-04 12:43:05 +00:00
|
|
|
operation->block_size = 0;
|
2018-04-30 09:31:04 +00:00
|
|
|
operation->iv_required = 0;
|
2018-04-04 12:43:05 +00:00
|
|
|
|
2018-05-31 11:07:14 +00:00
|
|
|
return( PSA_SUCCESS );
|
2018-03-12 13:59:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 15:06:50 +00:00
|
|
|
|
2018-06-18 19:49:39 +00:00
|
|
|
|
|
|
|
|
2018-04-25 21:53:03 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* AEAD */
|
|
|
|
/****************************************************************/
|
2018-06-18 19:49:39 +00:00
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-08-17 16:11:56 +00:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
union
|
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
|
2018-08-17 16:11:56 +00:00
|
|
|
#if defined(MBEDTLS_CCM_C)
|
|
|
|
mbedtls_ccm_context ccm;
|
|
|
|
#endif /* MBEDTLS_CCM_C */
|
|
|
|
#if defined(MBEDTLS_GCM_C)
|
|
|
|
mbedtls_gcm_context gcm;
|
|
|
|
#endif /* MBEDTLS_GCM_C */
|
2019-05-06 13:25:00 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
|
|
|
mbedtls_chachapoly_context chachapoly;
|
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
2018-08-17 16:11:56 +00:00
|
|
|
} ctx;
|
2018-08-17 17:47:52 +00:00
|
|
|
psa_algorithm_t core_alg;
|
|
|
|
uint8_t full_tag_length;
|
2018-08-17 16:11:56 +00:00
|
|
|
uint8_t tag_length;
|
|
|
|
} aead_operation_t;
|
|
|
|
|
2020-10-22 13:24:49 +00:00
|
|
|
#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
|
|
|
|
|
2019-01-14 17:36:12 +00:00
|
|
|
static void psa_aead_abort_internal( aead_operation_t *operation )
|
2018-08-17 16:11:56 +00:00
|
|
|
{
|
2018-08-21 14:38:05 +00:00
|
|
|
switch( operation->core_alg )
|
2018-08-17 16:11:56 +00:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_CCM_C)
|
|
|
|
case PSA_ALG_CCM:
|
|
|
|
mbedtls_ccm_free( &operation->ctx.ccm );
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_CCM_C */
|
2018-11-28 16:30:58 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2018-08-17 16:11:56 +00:00
|
|
|
case PSA_ALG_GCM:
|
|
|
|
mbedtls_gcm_free( &operation->ctx.gcm );
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_unlock_key_slot( operation->slot );
|
2018-08-17 16:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static psa_status_t psa_aead_setup( aead_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key,
|
2018-08-17 16:11:56 +00:00
|
|
|
psa_key_usage_t usage,
|
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
size_t key_bits;
|
|
|
|
mbedtls_cipher_id_t cipher_id;
|
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
key, &operation->slot, usage, alg );
|
2018-08-17 16:11:56 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2019-04-18 10:53:01 +00:00
|
|
|
key_bits = psa_get_key_slot_bits( operation->slot );
|
2018-08-17 16:11:56 +00:00
|
|
|
|
|
|
|
operation->cipher_info =
|
2019-07-30 18:06:31 +00:00
|
|
|
mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
|
2018-08-17 16:11:56 +00:00
|
|
|
&cipher_id );
|
|
|
|
if( operation->cipher_info == NULL )
|
2020-10-22 13:24:49 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-08-17 16:11:56 +00:00
|
|
|
|
2020-12-16 10:36:46 +00:00
|
|
|
switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
|
2018-08-17 16:11:56 +00:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_CCM_C)
|
2020-12-16 10:36:46 +00:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
|
2018-08-17 17:47:52 +00:00
|
|
|
operation->core_alg = PSA_ALG_CCM;
|
|
|
|
operation->full_tag_length = 16;
|
2019-05-06 13:27:16 +00:00
|
|
|
/* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
|
|
|
|
* The call to mbedtls_ccm_encrypt_and_tag or
|
|
|
|
* mbedtls_ccm_auth_decrypt will validate the tag length. */
|
2020-12-18 13:23:51 +00:00
|
|
|
if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
|
2020-10-22 13:24:49 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-08-17 16:11:56 +00:00
|
|
|
mbedtls_ccm_init( &operation->ctx.ccm );
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
|
2020-11-25 16:52:23 +00:00
|
|
|
operation->slot->key.data,
|
2018-08-17 16:11:56 +00:00
|
|
|
(unsigned int) key_bits ) );
|
|
|
|
if( status != 0 )
|
|
|
|
goto cleanup;
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_CCM_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2020-12-16 10:36:46 +00:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
|
2018-08-17 17:47:52 +00:00
|
|
|
operation->core_alg = PSA_ALG_GCM;
|
|
|
|
operation->full_tag_length = 16;
|
2019-05-06 13:27:16 +00:00
|
|
|
/* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
|
|
|
|
* The call to mbedtls_gcm_crypt_and_tag or
|
|
|
|
* mbedtls_gcm_auth_decrypt will validate the tag length. */
|
2020-12-18 13:23:51 +00:00
|
|
|
if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
|
2020-10-22 13:24:49 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-08-17 16:11:56 +00:00
|
|
|
mbedtls_gcm_init( &operation->ctx.gcm );
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
|
2020-11-25 16:52:23 +00:00
|
|
|
operation->slot->key.data,
|
2018-08-17 16:11:56 +00:00
|
|
|
(unsigned int) key_bits ) );
|
2019-05-06 13:27:16 +00:00
|
|
|
if( status != 0 )
|
|
|
|
goto cleanup;
|
2018-08-17 16:11:56 +00:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
|
2019-05-06 13:25:00 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2020-12-16 10:36:46 +00:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
|
2019-05-06 13:25:00 +00:00
|
|
|
operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
|
|
|
|
operation->full_tag_length = 16;
|
|
|
|
/* We only support the default tag length. */
|
|
|
|
if( alg != PSA_ALG_CHACHA20_POLY1305 )
|
2020-10-22 13:24:49 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-05-06 13:25:00 +00:00
|
|
|
mbedtls_chachapoly_init( &operation->ctx.chachapoly );
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
|
2020-11-25 16:52:23 +00:00
|
|
|
operation->slot->key.data ) );
|
2019-05-06 13:25:00 +00:00
|
|
|
if( status != 0 )
|
|
|
|
goto cleanup;
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
default:
|
2020-10-22 13:24:49 +00:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto cleanup;
|
2018-08-17 16:11:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:52 +00:00
|
|
|
if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
|
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
|
|
|
cleanup:
|
2019-01-14 17:36:12 +00:00
|
|
|
psa_aead_abort_internal( operation );
|
2018-08-17 16:11:56 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
|
2018-04-25 21:53:03 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *nonce,
|
|
|
|
size_t nonce_length,
|
|
|
|
const uint8_t *additional_data,
|
|
|
|
size_t additional_data_length,
|
|
|
|
const uint8_t *plaintext,
|
|
|
|
size_t plaintext_length,
|
|
|
|
uint8_t *ciphertext,
|
|
|
|
size_t ciphertext_size,
|
|
|
|
size_t *ciphertext_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2020-10-22 13:24:49 +00:00
|
|
|
aead_operation_t operation = AEAD_OPERATION_INIT;
|
2018-06-03 14:19:55 +00:00
|
|
|
uint8_t *tag;
|
2018-06-18 13:41:12 +00:00
|
|
|
|
2018-06-03 12:05:47 +00:00
|
|
|
*ciphertext_length = 0;
|
2018-05-09 11:58:32 +00:00
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-05-09 09:24:42 +00:00
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
/* For all currently supported modes, the tag is at the end of the
|
|
|
|
* ciphertext. */
|
|
|
|
if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
tag = ciphertext + plaintext_length;
|
2018-04-25 21:53:03 +00:00
|
|
|
|
2018-11-28 16:30:58 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2018-08-17 17:47:52 +00:00
|
|
|
if( operation.core_alg == PSA_ALG_GCM )
|
2018-04-25 21:53:03 +00:00
|
|
|
{
|
2018-08-17 16:11:56 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
|
|
|
|
MBEDTLS_GCM_ENCRYPT,
|
|
|
|
plaintext_length,
|
|
|
|
nonce, nonce_length,
|
|
|
|
additional_data, additional_data_length,
|
|
|
|
plaintext, ciphertext,
|
|
|
|
operation.tag_length, tag ) );
|
2018-04-25 21:53:03 +00:00
|
|
|
}
|
2018-11-28 16:30:58 +00:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
#if defined(MBEDTLS_CCM_C)
|
|
|
|
if( operation.core_alg == PSA_ALG_CCM )
|
2018-04-25 21:53:03 +00:00
|
|
|
{
|
2018-08-17 16:11:56 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
|
|
|
|
plaintext_length,
|
|
|
|
nonce, nonce_length,
|
|
|
|
additional_data,
|
|
|
|
additional_data_length,
|
|
|
|
plaintext, ciphertext,
|
|
|
|
tag, operation.tag_length ) );
|
2018-04-25 21:53:03 +00:00
|
|
|
}
|
2018-05-09 12:40:09 +00:00
|
|
|
else
|
2018-11-28 16:30:58 +00:00
|
|
|
#endif /* MBEDTLS_CCM_C */
|
2019-05-06 13:25:00 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
|
|
|
if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
|
2018-05-09 12:40:09 +00:00
|
|
|
{
|
2019-05-06 13:25:00 +00:00
|
|
|
if( nonce_length != 12 || operation.tag_length != 16 )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
|
|
|
|
plaintext_length,
|
|
|
|
nonce,
|
|
|
|
additional_data,
|
|
|
|
additional_data_length,
|
|
|
|
plaintext,
|
|
|
|
ciphertext,
|
|
|
|
tag ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
2018-05-09 12:40:09 +00:00
|
|
|
{
|
2021-02-10 16:13:28 +00:00
|
|
|
(void) tag;
|
2018-06-03 12:07:38 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2018-05-09 12:40:09 +00:00
|
|
|
}
|
2018-06-18 13:41:12 +00:00
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
if( status != PSA_SUCCESS && ciphertext_size != 0 )
|
|
|
|
memset( ciphertext, 0, ciphertext_size );
|
2018-06-18 13:41:12 +00:00
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
exit:
|
2019-01-14 17:36:12 +00:00
|
|
|
psa_aead_abort_internal( &operation );
|
2018-08-17 16:11:56 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
*ciphertext_length = plaintext_length + operation.tag_length;
|
|
|
|
return( status );
|
2018-04-25 21:53:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 17:23:52 +00:00
|
|
|
/* Locate the tag in a ciphertext buffer containing the encrypted data
|
|
|
|
* followed by the tag. Return the length of the part preceding the tag in
|
|
|
|
* *plaintext_length. This is the size of the plaintext in modes where
|
|
|
|
* the encrypted data has the same size as the plaintext, such as
|
|
|
|
* CCM and GCM. */
|
|
|
|
static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
|
|
|
|
const uint8_t *ciphertext,
|
|
|
|
size_t ciphertext_length,
|
|
|
|
size_t plaintext_size,
|
|
|
|
const uint8_t **p_tag )
|
|
|
|
{
|
|
|
|
size_t payload_length;
|
|
|
|
if( tag_length > ciphertext_length )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
payload_length = ciphertext_length - tag_length;
|
|
|
|
if( payload_length > plaintext_size )
|
|
|
|
return( PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
*p_tag = ciphertext + payload_length;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
|
2018-04-25 21:53:03 +00:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *nonce,
|
|
|
|
size_t nonce_length,
|
|
|
|
const uint8_t *additional_data,
|
|
|
|
size_t additional_data_length,
|
|
|
|
const uint8_t *ciphertext,
|
|
|
|
size_t ciphertext_length,
|
|
|
|
uint8_t *plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
size_t *plaintext_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2020-10-22 13:24:49 +00:00
|
|
|
aead_operation_t operation = AEAD_OPERATION_INIT;
|
2018-08-17 16:11:56 +00:00
|
|
|
const uint8_t *tag = NULL;
|
2018-06-18 13:41:12 +00:00
|
|
|
|
2018-06-01 17:23:52 +00:00
|
|
|
*plaintext_length = 0;
|
2018-04-26 09:07:35 +00:00
|
|
|
|
2020-08-04 07:51:30 +00:00
|
|
|
status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
|
2018-07-06 15:01:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-04-25 21:53:03 +00:00
|
|
|
|
2019-05-06 13:27:16 +00:00
|
|
|
status = psa_aead_unpadded_locate_tag( operation.tag_length,
|
|
|
|
ciphertext, ciphertext_length,
|
|
|
|
plaintext_size, &tag );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2018-11-28 16:30:58 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2018-08-17 17:47:52 +00:00
|
|
|
if( operation.core_alg == PSA_ALG_GCM )
|
2018-04-25 21:53:03 +00:00
|
|
|
{
|
2018-08-17 16:11:56 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
|
|
|
|
ciphertext_length - operation.tag_length,
|
|
|
|
nonce, nonce_length,
|
|
|
|
additional_data,
|
|
|
|
additional_data_length,
|
|
|
|
tag, operation.tag_length,
|
|
|
|
ciphertext, plaintext ) );
|
2018-04-25 21:53:03 +00:00
|
|
|
}
|
2018-11-28 16:30:58 +00:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
#if defined(MBEDTLS_CCM_C)
|
|
|
|
if( operation.core_alg == PSA_ALG_CCM )
|
2018-04-25 21:53:03 +00:00
|
|
|
{
|
2018-08-17 16:11:56 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
|
|
|
|
ciphertext_length - operation.tag_length,
|
|
|
|
nonce, nonce_length,
|
|
|
|
additional_data,
|
|
|
|
additional_data_length,
|
|
|
|
ciphertext, plaintext,
|
|
|
|
tag, operation.tag_length ) );
|
2018-04-25 21:53:03 +00:00
|
|
|
}
|
2018-06-01 11:39:53 +00:00
|
|
|
else
|
2018-11-28 16:30:58 +00:00
|
|
|
#endif /* MBEDTLS_CCM_C */
|
2019-05-06 13:25:00 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
|
|
|
if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
|
|
|
|
{
|
|
|
|
if( nonce_length != 12 || operation.tag_length != 16 )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
|
|
|
|
ciphertext_length - operation.tag_length,
|
|
|
|
nonce,
|
|
|
|
additional_data,
|
|
|
|
additional_data_length,
|
|
|
|
tag,
|
|
|
|
ciphertext,
|
|
|
|
plaintext ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
2018-06-01 11:39:53 +00:00
|
|
|
{
|
2018-06-03 12:07:38 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2018-06-01 11:39:53 +00:00
|
|
|
}
|
2018-06-01 14:28:30 +00:00
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
if( status != PSA_SUCCESS && plaintext_size != 0 )
|
|
|
|
memset( plaintext, 0, plaintext_size );
|
2018-06-03 14:20:42 +00:00
|
|
|
|
2018-08-17 16:11:56 +00:00
|
|
|
exit:
|
2019-01-14 17:36:12 +00:00
|
|
|
psa_aead_abort_internal( &operation );
|
2018-08-17 16:11:56 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
*plaintext_length = ciphertext_length - operation.tag_length;
|
|
|
|
return( status );
|
2018-04-25 21:53:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 15:06:50 +00:00
|
|
|
|
2018-06-18 19:49:39 +00:00
|
|
|
|
2018-06-19 20:00:52 +00:00
|
|
|
/****************************************************************/
|
2018-07-12 15:12:33 +00:00
|
|
|
/* Generators */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
|
|
|
#define AT_LEAST_ONE_BUILTIN_KDF
|
|
|
|
#endif
|
|
|
|
|
2019-01-07 21:59:38 +00:00
|
|
|
#define HKDF_STATE_INIT 0 /* no input yet */
|
|
|
|
#define HKDF_STATE_STARTED 1 /* got salt */
|
|
|
|
#define HKDF_STATE_KEYED 2 /* got key */
|
|
|
|
#define HKDF_STATE_OUTPUT 3 /* output started */
|
|
|
|
|
2019-05-16 14:59:18 +00:00
|
|
|
static psa_algorithm_t psa_key_derivation_get_kdf_alg(
|
2019-05-16 15:31:03 +00:00
|
|
|
const psa_key_derivation_operation_t *operation )
|
2019-01-16 14:53:06 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
|
|
|
|
return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
|
2019-01-16 14:53:06 +00:00
|
|
|
else
|
2019-05-16 15:31:03 +00:00
|
|
|
return( operation->alg );
|
2019-01-16 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
|
2019-01-16 14:53:06 +00:00
|
|
|
if( kdf_alg == 0 )
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
|
|
|
/* 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. */
|
|
|
|
}
|
|
|
|
else
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2019-01-16 14:53:06 +00:00
|
|
|
if( PSA_ALG_IS_HKDF( kdf_alg ) )
|
2018-07-12 15:22:21 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
mbedtls_free( operation->ctx.hkdf.info );
|
|
|
|
status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
|
2018-07-12 15:22:21 +00:00
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
else
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
|
|
|
if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
|
2019-05-16 15:31:03 +00:00
|
|
|
/* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
|
2019-01-16 14:53:06 +00:00
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
|
2018-10-09 16:33:01 +00:00
|
|
|
{
|
2019-06-11 09:37:28 +00:00
|
|
|
if( operation->ctx.tls12_prf.seed != NULL )
|
2018-10-09 16:33:01 +00:00
|
|
|
{
|
2019-06-11 09:37:28 +00:00
|
|
|
mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
|
|
|
|
operation->ctx.tls12_prf.seed_length );
|
|
|
|
mbedtls_free( operation->ctx.tls12_prf.seed );
|
2018-10-09 16:33:01 +00:00
|
|
|
}
|
2018-11-13 20:50:45 +00:00
|
|
|
|
2019-06-11 09:37:28 +00:00
|
|
|
if( operation->ctx.tls12_prf.label != NULL )
|
2018-11-13 20:50:45 +00:00
|
|
|
{
|
2019-06-11 09:37:28 +00:00
|
|
|
mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
|
|
|
|
operation->ctx.tls12_prf.label_length );
|
|
|
|
mbedtls_free( operation->ctx.tls12_prf.label );
|
2018-11-13 20:50:45 +00:00
|
|
|
}
|
2019-06-11 09:37:28 +00:00
|
|
|
|
|
|
|
status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
|
|
|
|
|
|
|
|
/* We leave the fields Ai and output_block to be erased safely by the
|
|
|
|
* mbedtls_platform_zeroize() in the end of this function. */
|
2018-10-09 16:33:01 +00:00
|
|
|
}
|
2018-07-12 15:22:21 +00:00
|
|
|
else
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2019-06-11 09:22:26 +00:00
|
|
|
mbedtls_platform_zeroize( operation, sizeof( *operation ) );
|
2018-07-12 15:12:33 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
|
2018-07-12 15:12:33 +00:00
|
|
|
size_t *capacity)
|
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
if( operation->alg == 0 )
|
2019-02-15 13:05:49 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
/* This is a blank key derivation operation. */
|
2020-08-05 13:43:42 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2019-02-15 13:05:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
*capacity = operation->capacity;
|
2018-07-12 15:12:33 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
|
2019-01-07 21:59:38 +00:00
|
|
|
size_t capacity )
|
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
if( operation->alg == 0 )
|
2019-01-07 21:59:38 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2019-05-16 15:31:03 +00:00
|
|
|
if( capacity > operation->capacity )
|
2019-01-07 21:59:38 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2019-05-16 15:31:03 +00:00
|
|
|
operation->capacity = capacity;
|
2018-07-12 15:12:33 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2019-05-16 15:31:03 +00:00
|
|
|
/* Read some bytes from an HKDF-based operation. This performs a chunk
|
2018-07-12 15:22:21 +00:00
|
|
|
* of the expand phase of the HKDF algorithm. */
|
2019-05-16 14:59:18 +00:00
|
|
|
static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
|
2018-07-12 15:22:21 +00:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_length )
|
|
|
|
{
|
2020-12-18 13:23:51 +00:00
|
|
|
uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
|
2018-07-12 15:22:21 +00:00
|
|
|
psa_status_t status;
|
|
|
|
|
2019-01-07 21:59:38 +00:00
|
|
|
if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
hkdf->state = HKDF_STATE_OUTPUT;
|
|
|
|
|
2018-07-12 15:22:21 +00:00
|
|
|
while( output_length != 0 )
|
|
|
|
{
|
|
|
|
/* Copy what remains of the current block */
|
|
|
|
uint8_t n = hash_length - hkdf->offset_in_block;
|
|
|
|
if( n > output_length )
|
|
|
|
n = (uint8_t) output_length;
|
|
|
|
memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
|
|
|
|
output += n;
|
|
|
|
output_length -= n;
|
|
|
|
hkdf->offset_in_block += n;
|
2018-07-17 19:06:59 +00:00
|
|
|
if( output_length == 0 )
|
2018-07-12 15:22:21 +00:00
|
|
|
break;
|
2018-07-17 19:06:59 +00:00
|
|
|
/* We can't be wanting more output after block 0xff, otherwise
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 13:28:51 +00:00
|
|
|
* the capacity check in psa_key_derivation_output_bytes() would have
|
2019-05-16 15:31:03 +00:00
|
|
|
* prevented this call. It could happen only if the operation
|
2018-07-17 19:06:59 +00:00
|
|
|
* object was corrupted or if this function is called directly
|
|
|
|
* inside the library. */
|
|
|
|
if( hkdf->block_number == 0xff )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2018-07-12 15:22:21 +00:00
|
|
|
|
|
|
|
/* We need a new block */
|
|
|
|
++hkdf->block_number;
|
|
|
|
hkdf->offset_in_block = 0;
|
|
|
|
status = psa_hmac_setup_internal( &hkdf->hmac,
|
|
|
|
hkdf->prk, hash_length,
|
|
|
|
hash_alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
if( hkdf->block_number != 1 )
|
|
|
|
{
|
|
|
|
status = psa_hash_update( &hkdf->hmac.hash_ctx,
|
|
|
|
hkdf->output_block,
|
|
|
|
hash_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
status = psa_hash_update( &hkdf->hmac.hash_ctx,
|
|
|
|
hkdf->info,
|
|
|
|
hkdf->info_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
status = psa_hash_update( &hkdf->hmac.hash_ctx,
|
|
|
|
&hkdf->block_number, 1 );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
status = psa_hmac_finish_internal( &hkdf->hmac,
|
|
|
|
hkdf->output_block,
|
|
|
|
sizeof( hkdf->output_block ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
|
2018-10-09 16:33:01 +00:00
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2019-06-17 11:58:10 +00:00
|
|
|
static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
|
|
|
|
psa_tls12_prf_key_derivation_t *tls12_prf,
|
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
|
2020-12-18 13:23:51 +00:00
|
|
|
uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
|
2019-06-19 11:21:20 +00:00
|
|
|
psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_status_t status, cleanup_status;
|
2018-10-09 16:33:01 +00:00
|
|
|
|
2019-06-17 11:58:10 +00:00
|
|
|
/* We can't be wanting more output after block 0xff, otherwise
|
|
|
|
* the capacity check in psa_key_derivation_output_bytes() would have
|
|
|
|
* prevented this call. It could happen only if the operation
|
|
|
|
* object was corrupted or if this function is called directly
|
|
|
|
* inside the library. */
|
|
|
|
if( tls12_prf->block_number == 0xff )
|
2019-06-25 09:15:04 +00:00
|
|
|
return( PSA_ERROR_CORRUPTION_DETECTED );
|
2019-06-17 11:58:10 +00:00
|
|
|
|
|
|
|
/* We need a new block */
|
|
|
|
++tls12_prf->block_number;
|
2019-06-19 11:10:49 +00:00
|
|
|
tls12_prf->left_in_block = hash_length;
|
2019-06-17 11:58:10 +00:00
|
|
|
|
|
|
|
/* Recall the definition of the TLS-1.2-PRF from RFC 5246:
|
|
|
|
*
|
|
|
|
* PRF(secret, label, seed) = P_<hash>(secret, label + seed)
|
|
|
|
*
|
|
|
|
* P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
|
|
|
|
* HMAC_hash(secret, A(2) + seed) +
|
|
|
|
* HMAC_hash(secret, A(3) + seed) + ...
|
|
|
|
*
|
|
|
|
* A(0) = seed
|
2019-06-19 11:21:20 +00:00
|
|
|
* A(i) = HMAC_hash(secret, A(i-1))
|
2019-06-17 11:58:10 +00:00
|
|
|
*
|
2019-06-19 11:21:20 +00:00
|
|
|
* The `psa_tls12_prf_key_derivation` structure saves the block
|
2019-06-17 11:58:10 +00:00
|
|
|
* `HMAC_hash(secret, A(i) + seed)` from which the output
|
2019-06-26 11:50:36 +00:00
|
|
|
* is currently extracted as `output_block` and where i is
|
|
|
|
* `block_number`.
|
2019-06-17 11:58:10 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-19 11:21:20 +00:00
|
|
|
/* Save the hash context before using it, to preserve the hash state with
|
|
|
|
* only the inner padding in it. We need this, because inner padding depends
|
|
|
|
* on the key (secret in the RFC's terminology). */
|
|
|
|
status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Calculate A(i) where i = tls12_prf->block_number. */
|
|
|
|
if( tls12_prf->block_number == 1 )
|
|
|
|
{
|
|
|
|
/* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
|
|
|
|
* the variable seed and in this instance means it in the context of the
|
|
|
|
* P_hash function, where seed = label + seed.) */
|
|
|
|
status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
|
|
|
|
tls12_prf->label, tls12_prf->label_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
|
|
|
|
tls12_prf->seed, tls12_prf->seed_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A(i) = HMAC_hash(secret, A(i-1)) */
|
|
|
|
status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
|
|
|
|
tls12_prf->Ai, hash_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_hmac_finish_internal( &tls12_prf->hmac,
|
|
|
|
tls12_prf->Ai, hash_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Calculate HMAC_hash(secret, A(i) + label + seed). */
|
|
|
|
status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
|
|
|
|
tls12_prf->Ai, hash_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
|
|
|
|
tls12_prf->label, tls12_prf->label_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
|
|
|
|
tls12_prf->seed, tls12_prf->seed_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
status = psa_hmac_finish_internal( &tls12_prf->hmac,
|
|
|
|
tls12_prf->output_block, hash_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-06-17 11:58:10 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2019-06-19 11:21:20 +00:00
|
|
|
cleanup_status = psa_hash_abort( &backup );
|
|
|
|
if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
|
|
|
|
status = cleanup_status;
|
|
|
|
|
|
|
|
return( status );
|
2019-06-17 11:58:10 +00:00
|
|
|
}
|
2018-10-09 16:33:01 +00:00
|
|
|
|
2019-06-19 11:10:49 +00:00
|
|
|
static psa_status_t psa_key_derivation_tls12_prf_read(
|
|
|
|
psa_tls12_prf_key_derivation_t *tls12_prf,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_length )
|
|
|
|
{
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
|
2020-12-18 13:23:51 +00:00
|
|
|
uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
|
2019-06-19 11:10:49 +00:00
|
|
|
psa_status_t status;
|
|
|
|
uint8_t offset, length;
|
|
|
|
|
|
|
|
while( output_length != 0 )
|
|
|
|
{
|
|
|
|
/* Check if we have fully processed the current block. */
|
|
|
|
if( tls12_prf->left_in_block == 0 )
|
|
|
|
{
|
|
|
|
status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
|
|
|
|
alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tls12_prf->left_in_block > output_length )
|
|
|
|
length = (uint8_t) output_length;
|
|
|
|
else
|
|
|
|
length = tls12_prf->left_in_block;
|
|
|
|
|
|
|
|
offset = hash_length - tls12_prf->left_in_block;
|
|
|
|
memcpy( output, tls12_prf->output_block + offset, length );
|
|
|
|
output += length;
|
|
|
|
output_length -= length;
|
|
|
|
tls12_prf->left_in_block -= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
|
|
|
|
* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2018-07-12 15:22:21 +00:00
|
|
|
|
2019-06-17 11:38:20 +00:00
|
|
|
psa_status_t psa_key_derivation_output_bytes(
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_length )
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
|
2018-07-12 15:12:33 +00:00
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
if( operation->alg == 0 )
|
2019-02-15 13:05:49 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
/* This is a blank operation. */
|
2020-08-05 13:43:42 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2019-02-15 13:05:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
if( output_length > operation->capacity )
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
operation->capacity = 0;
|
2018-07-12 15:12:33 +00:00
|
|
|
/* Go through the error path to wipe all confidential data now
|
2019-05-16 15:31:03 +00:00
|
|
|
* that the operation object is useless. */
|
2019-02-14 11:48:10 +00:00
|
|
|
status = PSA_ERROR_INSUFFICIENT_DATA;
|
2018-07-12 15:12:33 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2019-05-16 15:31:03 +00:00
|
|
|
if( output_length == 0 && operation->capacity == 0 )
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
/* Edge case: this is a finished operation, and 0 bytes
|
2019-02-15 13:05:49 +00:00
|
|
|
* were requested. The right error in this case could
|
2018-07-12 15:12:33 +00:00
|
|
|
* be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
|
|
|
|
* INSUFFICIENT_CAPACITY, which is right for a finished
|
2019-05-16 15:31:03 +00:00
|
|
|
* operation, for consistency with the case when
|
2018-07-12 15:12:33 +00:00
|
|
|
* output_length > 0. */
|
2019-02-14 11:48:10 +00:00
|
|
|
return( PSA_ERROR_INSUFFICIENT_DATA );
|
2018-07-12 15:12:33 +00:00
|
|
|
}
|
2019-05-16 15:31:03 +00:00
|
|
|
operation->capacity -= output_length;
|
2018-07-12 15:12:33 +00:00
|
|
|
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2019-01-16 14:53:06 +00:00
|
|
|
if( PSA_ALG_IS_HKDF( kdf_alg ) )
|
2018-07-12 15:22:21 +00:00
|
|
|
{
|
2019-01-16 14:53:06 +00:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
|
2018-07-12 15:22:21 +00:00
|
|
|
output, output_length );
|
|
|
|
}
|
2019-06-14 10:05:39 +00:00
|
|
|
else
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2019-06-14 10:05:39 +00:00
|
|
|
if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
|
2020-11-17 06:08:34 +00:00
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
|
2018-10-09 16:33:01 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
|
2019-01-16 14:53:06 +00:00
|
|
|
kdf_alg, output,
|
2018-10-09 16:33:01 +00:00
|
|
|
output_length );
|
|
|
|
}
|
2018-07-12 15:22:21 +00:00
|
|
|
else
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
|
|
|
|
* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
2021-02-10 16:19:22 +00:00
|
|
|
(void) kdf_alg;
|
2018-07-12 15:12:33 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
2019-02-15 13:05:49 +00:00
|
|
|
/* Preserve the algorithm upon errors, but clear all sensitive state.
|
2019-05-16 15:31:03 +00:00
|
|
|
* This allows us to differentiate between exhausted operations and
|
|
|
|
* blank operations, so we can return PSA_ERROR_BAD_STATE on blank
|
|
|
|
* operations. */
|
|
|
|
psa_algorithm_t alg = operation->alg;
|
|
|
|
psa_key_derivation_abort( operation );
|
|
|
|
operation->alg = alg;
|
2018-07-12 15:12:33 +00:00
|
|
|
memset( output, '!', output_length );
|
|
|
|
}
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2021-02-09 23:28:23 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2018-07-19 15:05:42 +00:00
|
|
|
static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
|
|
|
|
{
|
|
|
|
if( data_size >= 8 )
|
|
|
|
mbedtls_des_key_set_parity( data );
|
|
|
|
if( data_size >= 16 )
|
|
|
|
mbedtls_des_key_set_parity( data + 8 );
|
|
|
|
if( data_size >= 24 )
|
|
|
|
mbedtls_des_key_set_parity( data + 16 );
|
|
|
|
}
|
2021-02-09 23:28:23 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
|
2018-07-19 15:05:42 +00:00
|
|
|
|
2019-05-03 14:44:28 +00:00
|
|
|
static psa_status_t psa_generate_derived_key_internal(
|
2019-04-18 10:53:30 +00:00
|
|
|
psa_key_slot_t *slot,
|
|
|
|
size_t bits,
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_key_derivation_operation_t *operation )
|
2018-07-12 15:12:33 +00:00
|
|
|
{
|
|
|
|
uint8_t *data = NULL;
|
|
|
|
size_t bytes = PSA_BITS_TO_BYTES( bits );
|
|
|
|
psa_status_t status;
|
|
|
|
|
2019-07-30 18:06:31 +00:00
|
|
|
if( ! key_type_is_raw_bytes( slot->attr.type ) )
|
2018-07-12 15:12:33 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
if( bits % 8 != 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
data = mbedtls_calloc( 1, bytes );
|
|
|
|
if( data == NULL )
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_key_derivation_output_bytes( operation, data, bytes );
|
2018-07-12 15:12:33 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2021-02-11 18:02:00 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2019-07-30 18:06:31 +00:00
|
|
|
if( slot->attr.type == PSA_KEY_TYPE_DES )
|
2018-07-19 15:05:42 +00:00
|
|
|
psa_des_set_key_parity( data, bytes );
|
2021-02-12 15:08:46 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
|
2020-11-28 14:14:42 +00:00
|
|
|
|
|
|
|
status = psa_allocate_buffer_to_slot( slot, bytes );
|
|
|
|
if( status != PSA_SUCCESS )
|
2021-02-09 18:58:20 +00:00
|
|
|
goto exit;
|
2020-11-28 14:14:42 +00:00
|
|
|
|
2020-11-28 17:06:53 +00:00
|
|
|
slot->attr.bits = (psa_key_bits_t) bits;
|
2020-11-28 14:54:54 +00:00
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
.core = slot->attr
|
|
|
|
};
|
|
|
|
|
2020-11-28 17:06:53 +00:00
|
|
|
status = psa_driver_wrapper_import_key( &attributes,
|
|
|
|
data, bytes,
|
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes,
|
|
|
|
&slot->key.bytes, &bits );
|
|
|
|
if( bits != slot->attr.bits )
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2018-07-12 15:12:33 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( data );
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 13:28:51 +00:00
|
|
|
psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_key_derivation_operation_t *operation,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t *key )
|
2019-04-18 10:53:30 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-07-12 21:40:35 +00:00
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2019-09-12 17:03:13 +00:00
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
2019-09-12 17:03:13 +00:00
|
|
|
/* Reject any attempt to create a zero-length key so that we don't
|
|
|
|
* risk tripping up later, e.g. on a malloc(0) that returns NULL. */
|
|
|
|
if( psa_get_key_bits( attributes ) == 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2019-09-24 16:21:06 +00:00
|
|
|
if( ! operation->can_output_key )
|
|
|
|
return( PSA_ERROR_NOT_PERMITTED );
|
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
|
|
|
|
&slot, &driver );
|
2019-07-24 11:44:30 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
if( driver != NULL )
|
|
|
|
{
|
|
|
|
/* Deriving a key in a secure element is not implemented yet. */
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-04-18 10:53:30 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
2019-05-03 14:44:28 +00:00
|
|
|
status = psa_generate_derived_key_internal( slot,
|
2019-07-30 11:48:52 +00:00
|
|
|
attributes->core.bits,
|
2019-05-16 15:31:03 +00:00
|
|
|
operation );
|
2019-04-18 10:53:30 +00:00
|
|
|
}
|
|
|
|
if( status == PSA_SUCCESS )
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_finish_key_creation( slot, driver, key );
|
2019-04-18 10:53:30 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-06-26 16:34:38 +00:00
|
|
|
psa_fail_key_creation( slot, driver );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2019-04-18 10:53:30 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2018-07-12 15:12:33 +00:00
|
|
|
|
|
|
|
|
2018-07-12 15:17:20 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Key derivation */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#ifdef AT_LEAST_ONE_BUILTIN_KDF
|
2019-01-16 14:53:06 +00:00
|
|
|
static psa_status_t psa_key_derivation_setup_kdf(
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_key_derivation_operation_t *operation,
|
2019-01-16 14:53:06 +00:00
|
|
|
psa_algorithm_t kdf_alg )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2020-11-17 06:08:34 +00:00
|
|
|
int is_kdf_alg_supported;
|
|
|
|
|
2019-06-20 14:09:30 +00:00
|
|
|
/* Make sure that operation->ctx is properly zero-initialised. (Macro
|
|
|
|
* initialisers for this union leave some bytes unspecified.) */
|
|
|
|
memset( &operation->ctx, 0, sizeof( operation->ctx ) );
|
|
|
|
|
2019-01-16 14:53:06 +00:00
|
|
|
/* Make sure that kdf_alg is a supported key derivation algorithm. */
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2020-11-17 06:08:34 +00:00
|
|
|
if( PSA_ALG_IS_HKDF( kdf_alg ) )
|
|
|
|
is_kdf_alg_supported = 1;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
|
|
|
|
if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
|
|
|
|
is_kdf_alg_supported = 1;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
|
|
|
if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
|
|
|
|
is_kdf_alg_supported = 1;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
is_kdf_alg_supported = 0;
|
|
|
|
|
|
|
|
if( is_kdf_alg_supported )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2019-01-16 14:53:06 +00:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
|
2020-12-18 13:23:51 +00:00
|
|
|
size_t hash_size = PSA_HASH_LENGTH( hash_alg );
|
2019-01-07 21:59:38 +00:00
|
|
|
if( hash_size == 0 )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2019-01-16 14:53:06 +00:00
|
|
|
if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
|
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
|
2019-04-12 13:06:27 +00:00
|
|
|
! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
2019-05-16 15:31:03 +00:00
|
|
|
operation->capacity = 255 * hash_size;
|
2019-01-16 14:53:06 +00:00
|
|
|
return( PSA_SUCCESS );
|
2019-01-07 21:59:38 +00:00
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2019-01-16 14:53:06 +00:00
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* AT_LEAST_ONE_BUILTIN_KDF */
|
2019-01-16 14:53:06 +00:00
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
|
2019-01-16 14:53:06 +00:00
|
|
|
psa_algorithm_t alg )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
if( operation->alg != 0 )
|
2019-01-16 14:53:06 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2019-01-18 15:44:49 +00:00
|
|
|
if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2020-11-17 06:08:34 +00:00
|
|
|
#ifdef AT_LEAST_ONE_BUILTIN_KDF
|
2019-01-18 15:44:49 +00:00
|
|
|
else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
|
2019-01-16 14:53:06 +00:00
|
|
|
{
|
|
|
|
psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_key_derivation_setup_kdf( operation, kdf_alg );
|
2019-01-16 14:53:06 +00:00
|
|
|
}
|
|
|
|
else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
|
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_key_derivation_setup_kdf( operation, alg );
|
2019-01-16 14:53:06 +00:00
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif
|
2019-01-07 21:59:38 +00:00
|
|
|
else
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2019-01-16 14:53:06 +00:00
|
|
|
|
|
|
|
if( status == PSA_SUCCESS )
|
2019-05-16 15:31:03 +00:00
|
|
|
operation->alg = alg;
|
2019-01-16 14:53:06 +00:00
|
|
|
return( status );
|
2019-01-07 21:59:38 +00:00
|
|
|
}
|
|
|
|
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2019-05-16 14:59:18 +00:00
|
|
|
static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
|
2019-01-07 21:59:38 +00:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
switch( step )
|
|
|
|
{
|
2019-05-16 14:05:19 +00:00
|
|
|
case PSA_KEY_DERIVATION_INPUT_SALT:
|
2019-04-12 13:11:49 +00:00
|
|
|
if( hkdf->state != HKDF_STATE_INIT )
|
2019-01-07 21:59:38 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2019-04-12 13:11:49 +00:00
|
|
|
status = psa_hmac_setup_internal( &hkdf->hmac,
|
|
|
|
data, data_length,
|
|
|
|
hash_alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
hkdf->state = HKDF_STATE_STARTED;
|
|
|
|
return( PSA_SUCCESS );
|
2019-05-16 14:05:19 +00:00
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2019-01-07 21:59:38 +00:00
|
|
|
/* If no salt was provided, use an empty salt. */
|
|
|
|
if( hkdf->state == HKDF_STATE_INIT )
|
|
|
|
{
|
|
|
|
status = psa_hmac_setup_internal( &hkdf->hmac,
|
|
|
|
NULL, 0,
|
2019-04-11 19:22:52 +00:00
|
|
|
hash_alg );
|
2019-01-07 21:59:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
hkdf->state = HKDF_STATE_STARTED;
|
|
|
|
}
|
2019-04-12 13:11:49 +00:00
|
|
|
if( hkdf->state != HKDF_STATE_STARTED )
|
2019-01-07 21:59:38 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2019-04-12 13:11:49 +00:00
|
|
|
status = psa_hash_update( &hkdf->hmac.hash_ctx,
|
|
|
|
data, data_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
status = psa_hmac_finish_internal( &hkdf->hmac,
|
|
|
|
hkdf->prk,
|
|
|
|
sizeof( hkdf->prk ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2020-12-18 13:23:51 +00:00
|
|
|
hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
|
2019-04-12 13:11:49 +00:00
|
|
|
hkdf->block_number = 0;
|
|
|
|
hkdf->state = HKDF_STATE_KEYED;
|
|
|
|
return( PSA_SUCCESS );
|
2019-05-16 14:05:19 +00:00
|
|
|
case PSA_KEY_DERIVATION_INPUT_INFO:
|
2019-01-07 21:59:38 +00:00
|
|
|
if( hkdf->state == HKDF_STATE_OUTPUT )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
if( hkdf->info_set )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
hkdf->info_length = data_length;
|
|
|
|
if( data_length != 0 )
|
|
|
|
{
|
|
|
|
hkdf->info = mbedtls_calloc( 1, data_length );
|
|
|
|
if( hkdf->info == NULL )
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
memcpy( hkdf->info, data, data_length );
|
|
|
|
}
|
|
|
|
hkdf->info_set = 1;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
default:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
|
2019-01-07 21:59:38 +00:00
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2019-06-13 08:05:41 +00:00
|
|
|
static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
2020-12-13 13:55:14 +00:00
|
|
|
if( prf->state != PSA_TLS12_PRF_STATE_INIT )
|
2019-06-13 08:05:41 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2019-07-04 08:11:38 +00:00
|
|
|
if( data_length != 0 )
|
|
|
|
{
|
|
|
|
prf->seed = mbedtls_calloc( 1, data_length );
|
|
|
|
if( prf->seed == NULL )
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
2019-06-13 08:05:41 +00:00
|
|
|
|
2019-07-04 08:11:38 +00:00
|
|
|
memcpy( prf->seed, data, data_length );
|
|
|
|
prf->seed_length = data_length;
|
|
|
|
}
|
2019-06-13 08:05:41 +00:00
|
|
|
|
2020-12-13 13:55:14 +00:00
|
|
|
prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
|
2019-06-13 08:05:41 +00:00
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:26:34 +00:00
|
|
|
static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2020-12-13 13:55:14 +00:00
|
|
|
if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
|
2019-06-13 13:26:34 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-12-13 13:55:14 +00:00
|
|
|
prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
|
2019-06-13 13:26:34 +00:00
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:15:47 +00:00
|
|
|
static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
2020-12-13 13:55:14 +00:00
|
|
|
if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
|
2019-06-13 08:15:47 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
2019-01-07 21:59:38 +00:00
|
|
|
|
2019-07-04 08:11:38 +00:00
|
|
|
if( data_length != 0 )
|
2019-01-16 14:53:06 +00:00
|
|
|
{
|
2019-07-04 08:11:38 +00:00
|
|
|
prf->label = mbedtls_calloc( 1, data_length );
|
|
|
|
if( prf->label == NULL )
|
2019-01-16 14:53:06 +00:00
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
2019-06-13 08:15:47 +00:00
|
|
|
|
2019-07-04 08:11:38 +00:00
|
|
|
memcpy( prf->label, data, data_length );
|
|
|
|
prf->label_length = data_length;
|
2019-01-16 14:53:06 +00:00
|
|
|
}
|
2019-06-13 08:15:47 +00:00
|
|
|
|
2020-12-13 13:55:14 +00:00
|
|
|
prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
|
2019-06-13 08:15:47 +00:00
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:30:30 +00:00
|
|
|
static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
|
|
|
switch( step )
|
|
|
|
{
|
2019-06-13 08:05:41 +00:00
|
|
|
case PSA_KEY_DERIVATION_INPUT_SEED:
|
|
|
|
return( psa_tls12_prf_set_seed( prf, data, data_length ) );
|
2019-06-13 13:26:34 +00:00
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
|
|
|
return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
|
2019-06-13 08:15:47 +00:00
|
|
|
case PSA_KEY_DERIVATION_INPUT_LABEL:
|
|
|
|
return( psa_tls12_prf_set_label( prf, data, data_length ) );
|
2019-06-11 14:30:30 +00:00
|
|
|
default:
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
|
|
|
|
* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
|
|
|
static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
|
|
|
|
psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2020-12-18 13:23:51 +00:00
|
|
|
uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
|
2020-11-17 06:08:34 +00:00
|
|
|
uint8_t *cur = pms;
|
|
|
|
|
2020-12-18 13:23:51 +00:00
|
|
|
if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
|
2020-11-17 06:08:34 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
/* Quoting RFC 4279, Section 2:
|
|
|
|
*
|
|
|
|
* The premaster secret is formed as follows: if the PSK is N octets
|
|
|
|
* long, concatenate a uint16 with the value N, N zero octets, a second
|
|
|
|
* uint16 with the value N, and the PSK itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*cur++ = ( data_length >> 8 ) & 0xff;
|
|
|
|
*cur++ = ( data_length >> 0 ) & 0xff;
|
|
|
|
memset( cur, 0, data_length );
|
|
|
|
cur += data_length;
|
|
|
|
*cur++ = pms[0];
|
|
|
|
*cur++ = pms[1];
|
|
|
|
memcpy( cur, data, data_length );
|
|
|
|
cur += data_length;
|
|
|
|
|
|
|
|
status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
|
|
|
|
|
|
|
|
mbedtls_platform_zeroize( pms, sizeof( pms ) );
|
|
|
|
return( status );
|
|
|
|
}
|
2019-06-17 07:44:03 +00:00
|
|
|
|
|
|
|
static psa_status_t psa_tls12_prf_psk_to_ms_input(
|
|
|
|
psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
|
|
|
if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
|
2019-06-28 12:35:36 +00:00
|
|
|
{
|
2019-06-17 07:44:03 +00:00
|
|
|
return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
|
|
|
|
data, data_length ) );
|
2019-06-28 12:35:36 +00:00
|
|
|
}
|
2019-06-17 07:44:03 +00:00
|
|
|
|
|
|
|
return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2019-01-07 21:59:38 +00:00
|
|
|
|
2019-09-24 14:21:10 +00:00
|
|
|
/** Check whether the given key type is acceptable for the given
|
|
|
|
* input step of a key derivation.
|
|
|
|
*
|
|
|
|
* Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
|
|
|
|
* Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
|
|
|
|
* Both secret and non-secret inputs can alternatively have the type
|
|
|
|
* #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
|
|
|
|
* that the input was passed as a buffer rather than via a key object.
|
|
|
|
*/
|
2019-09-23 16:13:17 +00:00
|
|
|
static int psa_key_derivation_check_input_type(
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
psa_key_type_t key_type )
|
|
|
|
{
|
|
|
|
switch( step )
|
|
|
|
{
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2019-09-24 14:21:10 +00:00
|
|
|
if( key_type == PSA_KEY_TYPE_DERIVE )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
if( key_type == PSA_KEY_TYPE_NONE )
|
2019-09-23 16:13:17 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
break;
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_LABEL:
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SALT:
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_INFO:
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SEED:
|
2019-09-24 14:21:10 +00:00
|
|
|
if( key_type == PSA_KEY_TYPE_RAW_DATA )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
if( key_type == PSA_KEY_TYPE_NONE )
|
2019-09-23 16:13:17 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:54:46 +00:00
|
|
|
static psa_status_t psa_key_derivation_input_internal(
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_key_derivation_operation_t *operation,
|
2019-01-08 09:31:27 +00:00
|
|
|
psa_key_derivation_step_t step,
|
2019-09-23 16:13:17 +00:00
|
|
|
psa_key_type_t key_type,
|
2019-01-08 09:31:27 +00:00
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2019-09-23 17:22:55 +00:00
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
|
|
|
|
|
|
|
|
status = psa_key_derivation_check_input_type( step, key_type );
|
2019-09-23 16:13:17 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
Phase 2 support for MBEDTLS_PSA_CRYPTO_CONFIG
This phase adds in support for the following features being
added to the list of features that can be configured in the
include/psa/crypto_config.h header file using the PSA_WANT_ALG_xxx
macros: ECDH, HMAC, HKDF, and RSA. These changes include changes to
the PSA crypto library to use the appropriate new guards that
will allow the feature to be compiled in or out either using
new PSA_WANT_ALG_xxx or the previous MBEDTLS_xxx macros.
For HKDF and HMAC, most of the PSA library code did not have a
specific matching MBEDTLS_xxx macro for that feature, but was instead
using the generic dependent MBEDTLS_MD_C macro. The ECDH and RSA
features more closely aligned with a direct replacement with a similar
macro.
The new tests for RSA, HMAC, and HKDF would normally unset additional
dependent macros, but when attempting to implement that level of
testing it required removal of too many core features like MD_C, PK_C,
ECP_C and other low level features. This may point to additional phases of
work to complete the transition of these features to the new model.
Signed-off-by: John Durkop <john.durkop@fermatsoftware.com>
2020-10-30 04:37:36 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2019-01-16 14:53:06 +00:00
|
|
|
if( PSA_ALG_IS_HKDF( kdf_alg ) )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_hkdf_input( &operation->ctx.hkdf,
|
2019-01-16 14:53:06 +00:00
|
|
|
PSA_ALG_HKDF_GET_HASH( kdf_alg ),
|
2019-01-07 21:59:38 +00:00
|
|
|
step, data, data_length );
|
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
|
|
|
|
if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2019-06-11 14:30:30 +00:00
|
|
|
status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
|
|
|
|
PSA_ALG_HKDF_GET_HASH( kdf_alg ),
|
|
|
|
step, data, data_length );
|
2019-06-17 07:44:03 +00:00
|
|
|
}
|
2020-11-17 06:08:34 +00:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
|
|
|
if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
|
2019-06-17 07:44:03 +00:00
|
|
|
{
|
|
|
|
status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
|
|
|
|
PSA_ALG_HKDF_GET_HASH( kdf_alg ),
|
|
|
|
step, data, data_length );
|
2019-01-07 21:59:38 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2019-05-16 15:31:03 +00:00
|
|
|
/* This can't happen unless the operation object was not initialized */
|
2021-02-10 16:19:22 +00:00
|
|
|
(void) data;
|
|
|
|
(void) data_length;
|
|
|
|
(void) kdf_alg;
|
2019-01-07 21:59:38 +00:00
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:13:17 +00:00
|
|
|
exit:
|
2019-01-07 21:59:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_key_derivation_abort( operation );
|
2019-01-07 21:59:38 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:35:55 +00:00
|
|
|
psa_status_t psa_key_derivation_input_bytes(
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
2019-01-08 09:31:27 +00:00
|
|
|
{
|
2019-09-24 14:21:10 +00:00
|
|
|
return( psa_key_derivation_input_internal( operation, step,
|
|
|
|
PSA_KEY_TYPE_NONE,
|
2019-06-14 10:27:57 +00:00
|
|
|
data, data_length ) );
|
2019-01-08 09:31:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 10:35:55 +00:00
|
|
|
psa_status_t psa_key_derivation_input_key(
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
psa_key_derivation_step_t step,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t key )
|
2019-01-07 21:59:38 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2019-01-07 21:59:38 +00:00
|
|
|
psa_key_slot_t *slot;
|
2019-09-24 16:21:06 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
|
2019-01-07 21:59:38 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-09-23 16:17:40 +00:00
|
|
|
{
|
|
|
|
psa_key_derivation_abort( operation );
|
2019-01-07 21:59:38 +00:00
|
|
|
return( status );
|
2019-09-23 16:17:40 +00:00
|
|
|
}
|
2019-09-24 16:21:06 +00:00
|
|
|
|
|
|
|
/* Passing a key object as a SECRET input unlocks the permission
|
|
|
|
* to output to a key object. */
|
|
|
|
if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
|
|
|
|
operation->can_output_key = 1;
|
|
|
|
|
2020-10-22 13:24:49 +00:00
|
|
|
status = psa_key_derivation_input_internal( operation,
|
|
|
|
step, slot->attr.type,
|
2020-11-25 16:52:23 +00:00
|
|
|
slot->key.data,
|
|
|
|
slot->key.bytes );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2019-01-07 21:59:38 +00:00
|
|
|
}
|
2018-07-12 15:17:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-18 10:01:02 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Key agreement */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2018-09-18 10:11:27 +00:00
|
|
|
static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
|
|
|
|
size_t peer_key_length,
|
|
|
|
const mbedtls_ecp_keypair *our_key,
|
|
|
|
uint8_t *shared_secret,
|
|
|
|
size_t shared_secret_size,
|
|
|
|
size_t *shared_secret_length )
|
|
|
|
{
|
2020-08-05 14:31:39 +00:00
|
|
|
mbedtls_ecp_keypair *their_key = NULL;
|
2018-09-18 10:11:27 +00:00
|
|
|
mbedtls_ecdh_context ecdh;
|
2019-01-10 19:38:51 +00:00
|
|
|
psa_status_t status;
|
2019-12-12 15:58:00 +00:00
|
|
|
size_t bits = 0;
|
2020-06-02 16:19:28 +00:00
|
|
|
psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
|
2018-09-18 10:11:27 +00:00
|
|
|
mbedtls_ecdh_init( &ecdh );
|
|
|
|
|
2020-11-25 13:58:33 +00:00
|
|
|
status = mbedtls_psa_ecp_load_representation(
|
|
|
|
PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
|
2021-01-27 14:44:45 +00:00
|
|
|
bits,
|
2020-11-25 13:58:33 +00:00
|
|
|
peer_key,
|
|
|
|
peer_key_length,
|
|
|
|
&their_key );
|
2019-01-10 19:38:51 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-11-14 19:51:23 +00:00
|
|
|
goto exit;
|
|
|
|
|
2019-01-10 19:38:51 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
|
2019-01-10 19:38:51 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-09-18 10:11:27 +00:00
|
|
|
goto exit;
|
2019-01-10 19:38:51 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
2018-09-18 10:11:27 +00:00
|
|
|
goto exit;
|
|
|
|
|
2019-01-10 19:38:51 +00:00
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
mbedtls_ecdh_calc_secret( &ecdh,
|
|
|
|
shared_secret_length,
|
|
|
|
shared_secret, shared_secret_size,
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_get_random,
|
2020-12-14 13:54:06 +00:00
|
|
|
MBEDTLS_PSA_RANDOM_STATE ) );
|
2019-12-12 15:58:00 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
|
|
|
|
status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-09-18 10:11:27 +00:00
|
|
|
|
|
|
|
exit:
|
2019-12-20 13:09:55 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
mbedtls_platform_zeroize( shared_secret, shared_secret_size );
|
2018-09-18 10:11:27 +00:00
|
|
|
mbedtls_ecdh_free( &ecdh );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair_free( their_key );
|
2020-07-30 09:36:45 +00:00
|
|
|
mbedtls_free( their_key );
|
2020-07-28 12:30:39 +00:00
|
|
|
|
2019-01-10 19:38:51 +00:00
|
|
|
return( status );
|
2018-09-18 10:11:27 +00:00
|
|
|
}
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
|
2018-09-18 10:11:27 +00:00
|
|
|
|
2018-09-18 10:01:02 +00:00
|
|
|
#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
|
|
|
|
|
2019-04-11 19:23:21 +00:00
|
|
|
static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
|
|
|
|
psa_key_slot_t *private_key,
|
|
|
|
const uint8_t *peer_key,
|
|
|
|
size_t peer_key_length,
|
|
|
|
uint8_t *shared_secret,
|
|
|
|
size_t shared_secret_size,
|
|
|
|
size_t *shared_secret_length )
|
|
|
|
{
|
|
|
|
switch( alg )
|
|
|
|
{
|
2020-11-10 16:50:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2019-04-11 19:23:21 +00:00
|
|
|
case PSA_ALG_ECDH:
|
2019-07-30 18:06:31 +00:00
|
|
|
if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
|
2019-04-11 19:23:21 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
2020-11-25 13:58:33 +00:00
|
|
|
psa_status_t status = mbedtls_psa_ecp_load_representation(
|
|
|
|
private_key->attr.type,
|
2021-01-27 14:44:45 +00:00
|
|
|
private_key->attr.bits,
|
2020-11-25 13:58:33 +00:00
|
|
|
private_key->key.data,
|
|
|
|
private_key->key.bytes,
|
|
|
|
&ecp );
|
2020-07-24 21:09:52 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2020-08-05 13:43:42 +00:00
|
|
|
return( status );
|
2020-07-24 21:09:52 +00:00
|
|
|
status = psa_key_agreement_ecdh( peer_key, peer_key_length,
|
2020-07-28 12:30:39 +00:00
|
|
|
ecp,
|
2020-07-24 21:09:52 +00:00
|
|
|
shared_secret, shared_secret_size,
|
|
|
|
shared_secret_length );
|
2020-07-28 12:30:39 +00:00
|
|
|
mbedtls_ecp_keypair_free( ecp );
|
|
|
|
mbedtls_free( ecp );
|
2020-08-05 13:43:42 +00:00
|
|
|
return( status );
|
2020-11-10 16:50:04 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
|
2019-04-11 19:23:21 +00:00
|
|
|
default:
|
|
|
|
(void) private_key;
|
|
|
|
(void) peer_key;
|
|
|
|
(void) peer_key_length;
|
|
|
|
(void) shared_secret;
|
|
|
|
(void) shared_secret_size;
|
|
|
|
(void) shared_secret_length;
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 13:28:51 +00:00
|
|
|
/* Note that if this function fails, you must call psa_key_derivation_abort()
|
2018-11-16 15:05:06 +00:00
|
|
|
* to potentially free embedded data structures and wipe confidential data.
|
|
|
|
*/
|
2019-05-16 15:31:03 +00:00
|
|
|
static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
|
2019-01-16 14:53:06 +00:00
|
|
|
psa_key_derivation_step_t step,
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *private_key,
|
2018-09-18 10:01:02 +00:00
|
|
|
const uint8_t *peer_key,
|
2019-01-16 14:53:06 +00:00
|
|
|
size_t peer_key_length )
|
2018-09-18 10:01:02 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
|
|
|
|
size_t shared_secret_length = 0;
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
|
2018-09-18 10:01:02 +00:00
|
|
|
|
|
|
|
/* Step 1: run the secret agreement algorithm to generate the shared
|
|
|
|
* secret. */
|
2019-04-11 19:23:21 +00:00
|
|
|
status = psa_key_agreement_raw_internal( ka_alg,
|
|
|
|
private_key,
|
|
|
|
peer_key, peer_key_length,
|
2018-09-18 10:11:27 +00:00
|
|
|
shared_secret,
|
|
|
|
sizeof( shared_secret ),
|
|
|
|
&shared_secret_length );
|
2018-09-18 10:01:02 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* Step 2: set up the key derivation to generate key material from
|
2019-09-23 16:13:17 +00:00
|
|
|
* the shared secret. A shared secret is permitted wherever a key
|
|
|
|
* of type DERIVE is permitted. */
|
2019-06-12 14:54:46 +00:00
|
|
|
status = psa_key_derivation_input_internal( operation, step,
|
2019-09-23 16:13:17 +00:00
|
|
|
PSA_KEY_TYPE_DERIVE,
|
2019-06-12 14:54:46 +00:00
|
|
|
shared_secret,
|
|
|
|
shared_secret_length );
|
2018-09-18 10:01:02 +00:00
|
|
|
exit:
|
2018-12-07 17:14:53 +00:00
|
|
|
mbedtls_platform_zeroize( shared_secret, shared_secret_length );
|
2018-09-18 10:01:02 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
|
2019-05-16 15:53:40 +00:00
|
|
|
psa_key_derivation_step_t step,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t private_key,
|
2019-05-16 15:53:40 +00:00
|
|
|
const uint8_t *peer_key,
|
|
|
|
size_t peer_key_length )
|
2018-09-18 10:01:02 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 16:12:32 +00:00
|
|
|
psa_key_slot_t *slot;
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2019-05-16 15:31:03 +00:00
|
|
|
if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
|
2018-09-18 10:01:02 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
|
2018-09-18 10:01:02 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2019-05-16 15:31:03 +00:00
|
|
|
status = psa_key_agreement_internal( operation, step,
|
2018-11-16 15:05:06 +00:00
|
|
|
slot,
|
2019-01-16 14:53:06 +00:00
|
|
|
peer_key, peer_key_length );
|
2018-11-16 15:05:06 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-05-16 15:31:03 +00:00
|
|
|
psa_key_derivation_abort( operation );
|
2020-10-15 15:07:12 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If a private key has been added as SECRET, we allow the derived
|
|
|
|
* key material to be used as a key in PSA Crypto. */
|
|
|
|
if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
|
|
|
|
operation->can_output_key = 1;
|
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2018-09-18 10:01:02 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 16:00:41 +00:00
|
|
|
psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t private_key,
|
2019-05-16 16:00:41 +00:00
|
|
|
const uint8_t *peer_key,
|
|
|
|
size_t peer_key_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length )
|
2019-04-11 19:23:21 +00:00
|
|
|
{
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 15:35:34 +00:00
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-10-22 13:24:49 +00:00
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-04-11 19:23:21 +00:00
|
|
|
|
|
|
|
if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
2020-11-14 15:35:34 +00:00
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
|
|
|
private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
|
2019-04-11 19:23:21 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
status = psa_key_agreement_raw_internal( alg, slot,
|
|
|
|
peer_key, peer_key_length,
|
|
|
|
output, output_size,
|
|
|
|
output_length );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
/* If an error happens and is not handled properly, the output
|
|
|
|
* may be used as a key to protect sensitive data. Arrange for such
|
|
|
|
* a key to be random, which is likely to result in decryption or
|
|
|
|
* verification errors. This is better than filling the buffer with
|
|
|
|
* some constant data such as zeros, which would result in the data
|
|
|
|
* being protected with a reproducible, easily knowable key.
|
|
|
|
*/
|
|
|
|
psa_generate_random( output, output_size );
|
|
|
|
*output_length = output_size;
|
|
|
|
}
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
unlock_status = psa_unlock_key_slot( slot );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2020-11-14 15:35:34 +00:00
|
|
|
return( ( status == PSA_SUCCESS ) ? unlock_status : status );
|
2019-04-11 19:23:21 +00:00
|
|
|
}
|
2018-09-18 10:01:02 +00:00
|
|
|
|
|
|
|
|
2020-11-13 16:02:26 +00:00
|
|
|
|
2018-07-12 15:12:33 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Random generation */
|
2018-06-19 20:00:52 +00:00
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-13 16:02:26 +00:00
|
|
|
/** Initialize the PSA random generator.
|
|
|
|
*/
|
|
|
|
static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
|
|
|
|
{
|
2020-11-13 17:47:18 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
memset( rng, 0, sizeof( *rng ) );
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
|
|
|
|
2020-11-13 16:02:26 +00:00
|
|
|
/* Set default configuration if
|
|
|
|
* mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
|
|
|
|
if( rng->entropy_init == NULL )
|
|
|
|
rng->entropy_init = mbedtls_entropy_init;
|
|
|
|
if( rng->entropy_free == NULL )
|
|
|
|
rng->entropy_free = mbedtls_entropy_free;
|
|
|
|
|
|
|
|
rng->entropy_init( &rng->entropy );
|
|
|
|
#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
|
|
|
|
defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
|
|
|
|
/* The PSA entropy injection feature depends on using NV seed as an entropy
|
|
|
|
* source. Add NV seed as an entropy source for PSA entropy injection. */
|
|
|
|
mbedtls_entropy_add_source( &rng->entropy,
|
|
|
|
mbedtls_nv_seed_poll, NULL,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG );
|
|
|
|
#endif
|
|
|
|
|
2020-12-14 13:54:06 +00:00
|
|
|
mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
|
2020-11-13 17:47:18 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 16:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Deinitialize the PSA random generator.
|
|
|
|
*/
|
|
|
|
static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
|
|
|
|
{
|
2020-11-13 17:47:18 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
memset( rng, 0, sizeof( *rng ) );
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-12-14 13:54:06 +00:00
|
|
|
mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
|
2020-11-13 16:02:26 +00:00
|
|
|
rng->entropy_free( &rng->entropy );
|
2020-11-13 17:47:18 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 16:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Seed the PSA random generator.
|
|
|
|
*/
|
|
|
|
static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
|
|
|
|
{
|
2020-11-13 17:47:18 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
/* Do nothing: the external RNG seeds itself. */
|
|
|
|
(void) rng;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 16:02:26 +00:00
|
|
|
const unsigned char drbg_seed[] = "PSA";
|
2020-12-14 13:54:06 +00:00
|
|
|
int ret = mbedtls_psa_drbg_seed( &rng->entropy,
|
|
|
|
drbg_seed, sizeof( drbg_seed ) - 1 );
|
2020-11-13 16:02:26 +00:00
|
|
|
return mbedtls_to_psa_error( ret );
|
2020-11-13 17:47:18 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 16:02:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:00:52 +00:00
|
|
|
psa_status_t psa_generate_random( uint8_t *output,
|
|
|
|
size_t output_size )
|
|
|
|
{
|
2018-09-06 13:24:41 +00:00
|
|
|
GUARD_MODULE_INITIALIZED;
|
|
|
|
|
2020-11-13 17:47:18 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
|
|
|
|
output, output_size,
|
|
|
|
&output_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
/* Breaking up a request into smaller chunks is currently not supported
|
|
|
|
* for the extrernal RNG interface. */
|
|
|
|
if( output_length != output_size )
|
|
|
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
|
|
|
|
2021-01-04 20:01:07 +00:00
|
|
|
while( output_size > 0 )
|
2019-08-07 11:49:00 +00:00
|
|
|
{
|
2021-01-04 20:01:07 +00:00
|
|
|
size_t request_size =
|
|
|
|
( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
|
|
|
|
MBEDTLS_PSA_RANDOM_MAX_REQUEST :
|
|
|
|
output_size );
|
2021-01-05 13:10:59 +00:00
|
|
|
int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output, request_size );
|
2019-08-07 11:49:00 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
return( mbedtls_to_psa_error( ret ) );
|
2021-01-04 20:01:07 +00:00
|
|
|
output_size -= request_size;
|
|
|
|
output += request_size;
|
2019-08-07 11:49:00 +00:00
|
|
|
}
|
2021-01-05 13:10:59 +00:00
|
|
|
return( PSA_SUCCESS );
|
2020-11-13 17:47:18 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2018-06-19 20:00:52 +00:00
|
|
|
}
|
2019-08-07 11:49:00 +00:00
|
|
|
|
2020-12-14 14:33:44 +00:00
|
|
|
/* Wrapper function allowing the classic API to use the PSA RNG.
|
2021-01-05 15:03:55 +00:00
|
|
|
*
|
|
|
|
* `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
|
|
|
|
* `psa_generate_random(...)`. The state parameter is ignored since the
|
|
|
|
* PSA API doesn't support passing an explicit state.
|
|
|
|
*
|
|
|
|
* In the non-external case, psa_generate_random() calls an
|
|
|
|
* `mbedtls_xxx_drbg_random` function which has exactly the same signature
|
|
|
|
* and semantics as mbedtls_psa_get_random(). As an optimization,
|
|
|
|
* instead of doing this back-and-forth between the PSA API and the
|
|
|
|
* classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
|
|
|
|
* as a constant function pointer to `mbedtls_xxx_drbg_random`.
|
2020-12-14 14:33:44 +00:00
|
|
|
*/
|
|
|
|
#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
int mbedtls_psa_get_random( void *p_rng,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_size )
|
|
|
|
{
|
2021-01-05 15:03:55 +00:00
|
|
|
/* This function takes a pointer to the RNG state because that's what
|
|
|
|
* classic mbedtls functions using an RNG expect. The PSA RNG manages
|
|
|
|
* its own state internally and doesn't let the caller access that state.
|
|
|
|
* So we just ignore the state parameter, and in practice we'll pass
|
|
|
|
* NULL. */
|
2020-12-14 14:33:44 +00:00
|
|
|
(void) p_rng;
|
|
|
|
psa_status_t status = psa_generate_random( output, output_size );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
return( 0 );
|
|
|
|
else
|
|
|
|
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
2018-06-19 20:00:52 +00:00
|
|
|
}
|
2020-12-14 14:33:44 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2018-06-19 20:00:52 +00:00
|
|
|
|
2019-02-25 10:04:06 +00:00
|
|
|
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
|
|
|
#include "mbedtls/entropy_poll.h"
|
|
|
|
|
2019-07-15 09:06:15 +00:00
|
|
|
psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
|
2018-11-19 09:53:02 +00:00
|
|
|
size_t seed_size )
|
|
|
|
{
|
|
|
|
if( global_data.initialized )
|
|
|
|
return( PSA_ERROR_NOT_PERMITTED );
|
2018-11-19 09:53:55 +00:00
|
|
|
|
|
|
|
if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
|
|
|
|
( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
|
|
|
|
( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2019-02-25 10:04:06 +00:00
|
|
|
return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
|
2018-11-19 09:53:02 +00:00
|
|
|
}
|
2019-02-25 10:04:06 +00:00
|
|
|
#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
|
2018-11-19 09:53:02 +00:00
|
|
|
|
2021-02-08 15:10:05 +00:00
|
|
|
/** Validate the key type and size for key generation
|
2020-10-05 07:42:02 +00:00
|
|
|
*
|
2021-02-08 15:10:05 +00:00
|
|
|
* \param type The key type
|
|
|
|
* \param bits The number of bits of the key
|
2020-10-05 07:42:02 +00:00
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
2021-02-08 15:10:05 +00:00
|
|
|
* The key type and size are valid.
|
2020-10-05 07:42:02 +00:00
|
|
|
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
|
|
|
* The size in bits of the key is not valid.
|
|
|
|
* \retval #PSA_ERROR_NOT_SUPPORTED
|
|
|
|
* The type and/or the size in bits of the key or the combination of
|
|
|
|
* the two is not supported.
|
|
|
|
*/
|
2021-02-08 15:10:05 +00:00
|
|
|
static psa_status_t psa_validate_key_type_and_size_for_key_generation(
|
|
|
|
psa_key_type_t type, size_t bits )
|
2020-10-05 07:42:02 +00:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
if( key_type_is_raw_bytes( type ) )
|
|
|
|
{
|
|
|
|
status = validate_unstructured_key_bit_size( type, bits );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
else
|
2020-12-07 10:07:24 +00:00
|
|
|
#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
|
2020-10-05 07:42:02 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
|
|
|
|
{
|
|
|
|
if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
|
|
|
/* Accept only byte-aligned keys, for the same reasons as
|
|
|
|
* in psa_import_rsa_key(). */
|
|
|
|
if( bits % 8 != 0 )
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
else
|
2020-12-07 10:07:24 +00:00
|
|
|
#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
|
2020-10-05 07:42:02 +00:00
|
|
|
|
2020-12-07 10:07:24 +00:00
|
|
|
#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
|
2020-10-05 07:42:02 +00:00
|
|
|
if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
|
|
|
|
{
|
2021-02-16 08:01:16 +00:00
|
|
|
/* To avoid empty block, return successfully here. */
|
|
|
|
return( PSA_SUCCESS );
|
2020-10-05 07:42:02 +00:00
|
|
|
}
|
|
|
|
else
|
2020-12-07 10:07:24 +00:00
|
|
|
#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
|
2020-10-05 07:42:02 +00:00
|
|
|
{
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:30:40 +00:00
|
|
|
psa_status_t psa_generate_key_internal(
|
2020-10-02 18:02:04 +00:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
|
2018-06-19 20:00:52 +00:00
|
|
|
{
|
2020-10-02 18:11:59 +00:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-10-02 18:02:04 +00:00
|
|
|
psa_key_type_t type = attributes->core.type;
|
2018-06-19 22:20:32 +00:00
|
|
|
|
2020-10-02 18:02:04 +00:00
|
|
|
if( ( attributes->domain_parameters == NULL ) &&
|
|
|
|
( attributes->domain_parameters_size != 0 ) )
|
2018-06-19 22:20:32 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-07-06 15:01:38 +00:00
|
|
|
|
2018-06-21 12:15:31 +00:00
|
|
|
if( key_type_is_raw_bytes( type ) )
|
2018-06-19 22:20:32 +00:00
|
|
|
{
|
2020-10-02 18:02:04 +00:00
|
|
|
status = psa_generate_random( key_buffer, key_buffer_size );
|
2018-06-19 22:20:32 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2020-07-24 20:04:59 +00:00
|
|
|
|
2021-02-11 18:02:00 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2018-06-19 22:20:32 +00:00
|
|
|
if( type == PSA_KEY_TYPE_DES )
|
2020-10-02 18:02:04 +00:00
|
|
|
psa_des_set_key_parity( key_buffer, key_buffer_size );
|
2021-02-12 15:08:46 +00:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
|
2018-06-19 22:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
|
2020-11-17 06:08:34 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
|
2019-05-16 17:39:54 +00:00
|
|
|
if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
|
2018-06-19 22:20:32 +00:00
|
|
|
{
|
2020-11-05 16:36:40 +00:00
|
|
|
return( mbedtls_psa_rsa_generate_key( attributes,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
key_buffer_length ) );
|
2018-06-19 22:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-17 06:08:34 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
|
2018-06-19 22:20:32 +00:00
|
|
|
|
2020-11-04 20:28:15 +00:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
|
2019-05-16 17:39:54 +00:00
|
|
|
if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
|
2018-06-19 22:20:32 +00:00
|
|
|
{
|
2020-11-20 17:17:42 +00:00
|
|
|
return( mbedtls_psa_ecp_generate_key( attributes,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
key_buffer_length ) );
|
2018-06-19 22:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-04 20:28:15 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
|
2020-08-05 13:43:42 +00:00
|
|
|
{
|
2020-10-02 18:02:04 +00:00
|
|
|
(void)key_buffer_length;
|
2018-06-19 22:20:32 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2020-08-05 13:43:42 +00:00
|
|
|
}
|
2018-06-19 22:20:32 +00:00
|
|
|
|
2019-04-18 10:53:30 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2018-11-07 16:05:30 +00:00
|
|
|
|
2019-05-16 17:42:05 +00:00
|
|
|
psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
|
2020-08-04 07:51:30 +00:00
|
|
|
mbedtls_svc_key_id_t *key )
|
2019-04-18 10:53:30 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-07-12 21:40:35 +00:00
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2020-10-05 08:02:26 +00:00
|
|
|
size_t key_buffer_size;
|
2019-08-06 16:36:36 +00:00
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
2019-09-12 17:03:13 +00:00
|
|
|
/* Reject any attempt to create a zero-length key so that we don't
|
|
|
|
* risk tripping up later, e.g. on a malloc(0) that returns NULL. */
|
|
|
|
if( psa_get_key_bits( attributes ) == 0 )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
|
|
|
|
&slot, &driver );
|
2019-08-06 16:36:36 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2020-10-13 06:32:21 +00:00
|
|
|
/* In the case of a transparent key or an opaque key stored in local
|
|
|
|
* storage (thus not in the case of generating a key in a secure element
|
|
|
|
* or cryptoprocessor with storage), we have to allocate a buffer to
|
|
|
|
* hold the generated key material. */
|
|
|
|
if( slot->key.data == NULL )
|
|
|
|
{
|
|
|
|
if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
|
|
|
|
PSA_KEY_LOCATION_LOCAL_STORAGE )
|
|
|
|
{
|
2021-02-08 15:10:05 +00:00
|
|
|
status = psa_validate_key_type_and_size_for_key_generation(
|
|
|
|
attributes->core.type, attributes->core.bits );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
|
|
|
|
attributes->core.type,
|
|
|
|
attributes->core.bits );
|
2020-10-13 06:32:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = psa_driver_wrapper_get_key_buffer_size(
|
|
|
|
attributes, &key_buffer_size );
|
2021-02-08 15:10:05 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2020-10-13 06:32:21 +00:00
|
|
|
}
|
2020-10-05 08:02:26 +00:00
|
|
|
|
2020-10-13 06:32:21 +00:00
|
|
|
status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
}
|
2020-10-05 08:02:26 +00:00
|
|
|
|
2020-10-13 06:32:21 +00:00
|
|
|
status = psa_driver_wrapper_generate_key( attributes,
|
|
|
|
slot->key.data, slot->key.bytes, &slot->key.bytes );
|
2019-08-06 16:36:36 +00:00
|
|
|
|
2020-10-05 08:02:26 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
psa_remove_key_data_from_memory( slot );
|
|
|
|
|
2019-08-06 16:36:36 +00:00
|
|
|
exit:
|
2019-04-18 10:53:30 +00:00
|
|
|
if( status == PSA_SUCCESS )
|
2020-11-14 11:10:32 +00:00
|
|
|
status = psa_finish_key_creation( slot, driver, key );
|
2019-04-18 10:53:30 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-06-26 16:34:38 +00:00
|
|
|
psa_fail_key_creation( slot, driver );
|
2020-10-22 13:24:49 +00:00
|
|
|
|
2018-11-07 16:05:30 +00:00
|
|
|
return( status );
|
2018-06-19 20:00:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 12:16:24 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Module setup */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-11-13 17:47:18 +00:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
2018-11-20 20:59:56 +00:00
|
|
|
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 );
|
2020-11-13 16:02:26 +00:00
|
|
|
global_data.rng.entropy_init = entropy_init;
|
|
|
|
global_data.rng.entropy_free = entropy_free;
|
2018-11-20 20:59:56 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2020-11-13 17:47:18 +00:00
|
|
|
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
|
2018-11-20 20:59:56 +00:00
|
|
|
|
2018-01-27 22:32:46 +00:00
|
|
|
void mbedtls_psa_crypto_free( void )
|
|
|
|
{
|
2018-12-10 15:29:04 +00:00
|
|
|
psa_wipe_all_key_slots( );
|
2018-11-20 20:42:52 +00:00
|
|
|
if( global_data.rng_state != RNG_NOT_INITIALIZED )
|
|
|
|
{
|
2020-11-13 16:02:26 +00:00
|
|
|
mbedtls_psa_random_free( &global_data.rng );
|
2018-11-15 14:48:15 +00:00
|
|
|
}
|
2018-11-20 20:42:52 +00:00
|
|
|
/* Wipe all remaining data, including configuration.
|
|
|
|
* In particular, this sets all state indicator to the value
|
|
|
|
* indicating "uninitialized". */
|
2018-12-07 17:14:53 +00:00
|
|
|
mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
|
2019-06-26 09:24:49 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-06-24 12:34:43 +00:00
|
|
|
/* Unregister all secure element drivers, so that we restart from
|
|
|
|
* a pristine state. */
|
|
|
|
psa_unregister_all_se_drivers( );
|
2019-06-26 09:24:49 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-01-27 22:32:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:52:59 +00:00
|
|
|
#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
|
|
|
|
/** Recover a transaction that was interrupted by a power failure.
|
|
|
|
*
|
|
|
|
* This function is called during initialization, before psa_crypto_init()
|
|
|
|
* returns. If this function returns a failure status, the initialization
|
|
|
|
* fails.
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_crypto_recover_transaction(
|
|
|
|
const psa_crypto_transaction_t *transaction )
|
|
|
|
{
|
|
|
|
switch( transaction->unknown.type )
|
|
|
|
{
|
|
|
|
case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
|
|
|
|
case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
|
2019-08-13 11:15:34 +00:00
|
|
|
/* TODO - fall through to the failure case until this
|
2019-08-13 14:17:16 +00:00
|
|
|
* is implemented.
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/218
|
|
|
|
*/
|
2019-07-25 15:52:59 +00:00
|
|
|
default:
|
|
|
|
/* We found an unsupported transaction in the storage.
|
|
|
|
* We don't know what state the storage is in. Give up. */
|
2020-11-09 16:39:56 +00:00
|
|
|
return( PSA_ERROR_DATA_INVALID );
|
2019-07-25 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
|
|
|
|
|
2018-01-27 22:32:46 +00:00
|
|
|
psa_status_t psa_crypto_init( void )
|
|
|
|
{
|
2018-12-10 15:29:04 +00:00
|
|
|
psa_status_t status;
|
2018-01-27 22:32:46 +00:00
|
|
|
|
2018-11-20 20:42:52 +00:00
|
|
|
/* Double initialization is explicitly allowed. */
|
2018-01-27 22:32:46 +00:00
|
|
|
if( global_data.initialized != 0 )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
2020-11-13 16:02:26 +00:00
|
|
|
/* Initialize and seed the random generator. */
|
|
|
|
mbedtls_psa_random_init( &global_data.rng );
|
2018-11-20 20:42:52 +00:00
|
|
|
global_data.rng_state = RNG_INITIALIZED;
|
2020-11-13 16:02:26 +00:00
|
|
|
status = mbedtls_psa_random_seed( &global_data.rng );
|
2018-12-10 15:29:04 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-01-27 22:32:46 +00:00
|
|
|
goto exit;
|
2018-11-20 20:42:52 +00:00
|
|
|
global_data.rng_state = RNG_SEEDED;
|
|
|
|
|
2018-12-10 15:29:04 +00:00
|
|
|
status = psa_initialize_key_slots( );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2018-01-27 22:32:46 +00:00
|
|
|
|
2019-10-01 13:22:29 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
status = psa_init_all_se_drivers( );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2019-07-24 13:56:31 +00:00
|
|
|
#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
|
2019-07-22 17:30:34 +00:00
|
|
|
status = psa_crypto_load_transaction( );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
2019-07-25 15:52:59 +00:00
|
|
|
status = psa_crypto_recover_transaction( &psa_crypto_transaction );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
status = psa_crypto_stop_transaction( );
|
2019-07-22 17:30:34 +00:00
|
|
|
}
|
|
|
|
else if( status == PSA_ERROR_DOES_NOT_EXIST )
|
|
|
|
{
|
|
|
|
/* There's no transaction to complete. It's all good. */
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
}
|
2019-07-24 13:56:31 +00:00
|
|
|
#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
|
2019-07-22 17:30:34 +00:00
|
|
|
|
2018-11-20 20:42:52 +00:00
|
|
|
/* All done. */
|
2018-03-07 13:16:44 +00:00
|
|
|
global_data.initialized = 1;
|
|
|
|
|
2018-01-27 22:32:46 +00:00
|
|
|
exit:
|
2018-12-10 15:29:04 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-01-27 22:32:46 +00:00
|
|
|
mbedtls_psa_crypto_free( );
|
2018-12-10 15:29:04 +00:00
|
|
|
return( status );
|
2018-01-27 22:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|