2018-01-27 23:32:46 +01:00
|
|
|
|
/*
|
|
|
|
|
* PSA crypto layer on top of Mbed TLS crypto
|
|
|
|
|
*/
|
2020-06-15 11:59:37 +02:00
|
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2018-01-27 23:32:46 +01: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-03 01:43:33 +02:00
|
|
|
|
#include "common.h"
|
2018-01-27 23:32:46 +01:00
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
2018-07-03 12:16:15 +02:00
|
|
|
|
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
|
|
|
|
|
#include "check_crypto_config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-01-27 23:32:46 +01:00
|
|
|
|
#include "psa/crypto.h"
|
2021-11-24 08:47:29 +01:00
|
|
|
|
#include "psa/crypto_values.h"
|
2018-01-27 23:32:46 +01:00
|
|
|
|
|
2020-12-14 14:56:02 +01:00
|
|
|
|
#include "psa_crypto_cipher.h"
|
2018-12-07 18:24:41 +01:00
|
|
|
|
#include "psa_crypto_core.h"
|
2018-11-20 21:59:56 +01:00
|
|
|
|
#include "psa_crypto_invasive.h"
|
2020-07-16 20:28:36 +02:00
|
|
|
|
#include "psa_crypto_driver_wrappers.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
|
#include "psa_crypto_ecp.h"
|
2021-03-15 11:07:12 +01:00
|
|
|
|
#include "psa_crypto_hash.h"
|
2021-03-19 17:39:17 +01:00
|
|
|
|
#include "psa_crypto_mac.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
|
#include "psa_crypto_rsa.h"
|
2020-11-20 18:17:42 +01:00
|
|
|
|
#include "psa_crypto_ecp.h"
|
2019-06-26 11:24:49 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-06-24 14:34:43 +02:00
|
|
|
|
#include "psa_crypto_se.h"
|
2019-06-26 11:24:49 +02:00
|
|
|
|
#endif
|
2018-11-30 18:54:54 +01:00
|
|
|
|
#include "psa_crypto_slot_management.h"
|
2018-06-18 18:27:26 +02:00
|
|
|
|
/* Include internal declarations that are useful for implementing persistently
|
|
|
|
|
* stored keys. */
|
|
|
|
|
#include "psa_crypto_storage.h"
|
|
|
|
|
|
2020-12-14 16:43:58 +01:00
|
|
|
|
#include "psa_crypto_random_impl.h"
|
2020-11-13 15:39:19 +01:00
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
2020-11-13 18:46:25 +01:00
|
|
|
|
#include "mbedtls/aes.h"
|
2018-06-21 09:35:35 +02:00
|
|
|
|
#include "mbedtls/asn1.h"
|
2019-01-10 11:23:21 +01:00
|
|
|
|
#include "mbedtls/asn1write.h"
|
2018-06-26 16:10:23 +02:00
|
|
|
|
#include "mbedtls/bignum.h"
|
2018-02-07 20:59:33 +01:00
|
|
|
|
#include "mbedtls/camellia.h"
|
2019-05-06 15:25:00 +02:00
|
|
|
|
#include "mbedtls/chacha20.h"
|
|
|
|
|
#include "mbedtls/chachapoly.h"
|
2018-02-07 20:59:33 +01:00
|
|
|
|
#include "mbedtls/cipher.h"
|
|
|
|
|
#include "mbedtls/ccm.h"
|
|
|
|
|
#include "mbedtls/cmac.h"
|
|
|
|
|
#include "mbedtls/des.h"
|
2018-09-18 12:11:27 +02:00
|
|
|
|
#include "mbedtls/ecdh.h"
|
2018-01-28 18:16:59 +01:00
|
|
|
|
#include "mbedtls/ecp.h"
|
2018-01-27 23:32:46 +01:00
|
|
|
|
#include "mbedtls/entropy.h"
|
2018-02-07 20:59:33 +01:00
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
|
#include "mbedtls/gcm.h"
|
|
|
|
|
#include "mbedtls/md5.h"
|
2018-02-03 22:44:14 +01:00
|
|
|
|
#include "mbedtls/md.h"
|
2021-03-09 18:03:29 +01:00
|
|
|
|
#include "md_wrap.h"
|
2018-01-28 13:16:24 +01:00
|
|
|
|
#include "mbedtls/pk.h"
|
2021-03-09 18:03:29 +01:00
|
|
|
|
#include "pk_wrap.h"
|
2018-12-07 18:14:53 +01:00
|
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
|
#include "mbedtls/error.h"
|
2018-02-07 20:59:33 +01:00
|
|
|
|
#include "mbedtls/ripemd160.h"
|
2018-01-28 18:16:59 +01:00
|
|
|
|
#include "mbedtls/rsa.h"
|
2018-02-07 20:59:33 +01:00
|
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
|
#include "mbedtls/sha512.h"
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#include "hash_info.h"
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
|
2018-08-01 15:45:45 +02:00
|
|
|
|
|
2022-06-09 14:44:55 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#define BUILTIN_ALG_ANY_HKDF 1
|
2022-06-09 14:44:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-03-13 16:06:09 +01:00
|
|
|
|
/* The only two JPAKE user/peer identifiers supported for the time being. */
|
|
|
|
|
static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
|
|
|
|
|
static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
|
2023-03-06 15:40:35 +01:00
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Global data, support functions and library management */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static int key_type_is_raw_bytes(psa_key_type_t type)
|
2018-06-21 14:15:31 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
|
2018-06-21 14:15:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 12:27:09 +01: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 21:42:52 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
typedef struct {
|
2018-11-20 21:42:52 +01:00
|
|
|
|
unsigned initialized : 1;
|
2018-12-04 12:27:09 +01:00
|
|
|
|
unsigned rng_state : 2;
|
2023-03-09 11:17:43 +01:00
|
|
|
|
unsigned drivers_initialized : 1;
|
2021-11-08 22:20:03 +01:00
|
|
|
|
mbedtls_psa_random_context_t rng;
|
2018-01-27 23:32:46 +01:00
|
|
|
|
} psa_global_data_t;
|
|
|
|
|
|
|
|
|
|
static psa_global_data_t global_data;
|
|
|
|
|
|
2020-12-14 14:54:06 +01: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 15:24:41 +02:00
|
|
|
|
#define GUARD_MODULE_INITIALIZED \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (global_data.initialized == 0) \
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2018-09-06 15:24:41 +02:00
|
|
|
|
|
2023-03-09 11:17:43 +01:00
|
|
|
|
int psa_can_do_hash(psa_algorithm_t hash_alg)
|
|
|
|
|
{
|
|
|
|
|
(void) hash_alg;
|
|
|
|
|
return global_data.drivers_initialized;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t mbedtls_to_psa_error(int ret)
|
2018-01-27 23:32:46 +01:00
|
|
|
|
{
|
2021-01-06 20:04:23 +01: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
int low_level_ret = -(-ret & 0x007f);
|
|
|
|
|
switch (low_level_ret != 0 ? low_level_ret : ret) {
|
2018-01-27 23:32:46 +01:00
|
|
|
|
case 0:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
|
|
|
|
|
case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-06-21 09:35:35 +02: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:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-06-21 09:35:35 +02:00
|
|
|
|
case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
2018-06-21 09:35:35 +02:00
|
|
|
|
case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-06-21 09:35:35 +02:00
|
|
|
|
|
2019-02-20 14:57:28 +01:00
|
|
|
|
#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
|
2019-02-11 13:21:12 +01:00
|
|
|
|
case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
|
2019-02-20 14:57:28 +01:00
|
|
|
|
#endif
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_CCM_BAD_INPUT:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CCM_AUTH_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2019-05-06 15:25:00 +02:00
|
|
|
|
case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-05-06 15:25:00 +02:00
|
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2019-05-06 15:25:00 +02:00
|
|
|
|
case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2019-05-06 15:25:00 +02:00
|
|
|
|
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_PADDING;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
|
|
|
|
|
defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
|
2020-11-23 21:00:09 +01:00
|
|
|
|
/* Only check CTR_DRBG error codes if underlying mbedtls_xxx
|
|
|
|
|
* functions are passed a CTR_DRBG instance. */
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
|
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2020-11-13 21:31:17 +01:00
|
|
|
|
#endif
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2018-01-27 23:32:46 +01:00
|
|
|
|
case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
|
|
|
|
|
case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
|
|
|
|
|
case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_GCM_AUTH_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2021-09-30 16:39:07 +02:00
|
|
|
|
case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_GCM_BAD_INPUT:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2020-11-13 21:31:17 +01:00
|
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
2020-11-23 21:00:09 +01:00
|
|
|
|
/* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
|
|
|
|
|
* functions are passed a HMAC_DRBG instance. */
|
2020-11-13 21:31:17 +01:00
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2020-11-13 21:31:17 +01:00
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
|
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-11-13 21:31:17 +01:00
|
|
|
|
case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2020-11-13 21:31:17 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MD_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MD_FILE_IO_ERROR:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_STORAGE_FAILURE;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_STORAGE_FAILURE;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
case MBEDTLS_ERR_MPI_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
2018-10-29 19:24:33 +01:00
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_TYPE_MISMATCH:
|
|
|
|
|
case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_FILE_IO_ERROR:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_STORAGE_FAILURE;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
|
|
|
|
|
case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
|
|
|
|
|
case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_PERMITTED;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_INVALID_PUBKEY:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_INVALID_ALG:
|
|
|
|
|
case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
|
|
|
|
|
case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2021-06-23 20:32:27 +02:00
|
|
|
|
case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
|
2019-05-06 15:26:23 +02:00
|
|
|
|
case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_HARDWARE_FAILURE;
|
2019-05-06 15:26:23 +02:00
|
|
|
|
case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2019-05-06 15:26:23 +02:00
|
|
|
|
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_INVALID_PADDING:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_PADDING;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_HARDWARE_FAILURE;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
|
|
|
|
|
case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_VERIFY_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-02-07 20:59:33 +01:00
|
|
|
|
case MBEDTLS_ERR_RSA_RNG_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2021-04-15 12:23:55 +02:00
|
|
|
|
|
2018-05-08 10:18:38 +02:00
|
|
|
|
case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
|
2018-05-09 15:07:36 +02:00
|
|
|
|
case MBEDTLS_ERR_ECP_INVALID_KEY:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-05-09 15:07:36 +02:00
|
|
|
|
case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
2018-05-09 15:07:36 +02:00
|
|
|
|
case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-05-09 15:07:36 +02:00
|
|
|
|
case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
|
|
|
|
|
case MBEDTLS_ERR_ECP_VERIFY_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
2018-05-09 15:07:36 +02:00
|
|
|
|
case MBEDTLS_ERR_ECP_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
2020-11-25 00:09:47 +01:00
|
|
|
|
case MBEDTLS_ERR_ECP_RANDOM_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
2020-11-25 00:09:47 +01:00
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
case MBEDTLS_ERR_ECP_IN_PROGRESS:
|
|
|
|
|
return PSA_OPERATION_INCOMPLETE;
|
|
|
|
|
|
2019-11-22 13:48:59 +01:00
|
|
|
|
case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2018-05-08 10:18:38 +02:00
|
|
|
|
|
2018-01-27 23:32:46 +01:00
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_GENERIC_ERROR;
|
2018-01-27 23:32:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 19:11:59 +01:00
|
|
|
|
/**
|
|
|
|
|
* \brief For output buffers which contain "tags"
|
|
|
|
|
* (outputs that may be checked for validity like
|
|
|
|
|
* hashes, MACs and signatures), 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 tag (barring an
|
|
|
|
|
* attack on the tag and deliberately-crafted
|
|
|
|
|
* input), in case the caller doesn't check the
|
|
|
|
|
* return status properly.
|
|
|
|
|
*
|
|
|
|
|
* \param output_buffer Pointer to buffer to wipe. May not be NULL
|
|
|
|
|
* unless \p output_buffer_size is zero.
|
|
|
|
|
* \param status Status of function called to generate
|
|
|
|
|
* output_buffer originally
|
|
|
|
|
* \param output_buffer_size Size of output buffer. If zero, \p output_buffer
|
|
|
|
|
* could be NULL.
|
|
|
|
|
* \param output_buffer_length Length of data written to output_buffer, must be
|
|
|
|
|
* less than \p output_buffer_size
|
|
|
|
|
*/
|
|
|
|
|
static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
|
2023-02-26 17:57:05 +01:00
|
|
|
|
size_t output_buffer_size, size_t output_buffer_length)
|
|
|
|
|
{
|
2023-02-24 19:11:59 +01:00
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
|
|
if (output_buffer_size == 0) {
|
|
|
|
|
/* If output_buffer_size is 0 then we have nothing to do. We must not
|
|
|
|
|
call memset because output_buffer may be NULL in this case */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
offset = output_buffer_length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(output_buffer + offset, '!', output_buffer_size - offset);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 17:01:38 +02:00
|
|
|
|
|
2018-06-18 21:49:39 +02:00
|
|
|
|
|
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Key management */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
2020-11-10 17:50:04 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
2021-11-19 18:02:34 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
|
|
|
|
|
size_t bits,
|
|
|
|
|
int bits_is_sloppy)
|
2018-06-20 00:20:32 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (curve) {
|
2020-06-02 18:19:28 +02:00
|
|
|
|
case PSA_ECC_FAMILY_SECP_R1:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (bits) {
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_R1_192)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 192:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP192R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_R1_224)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 224:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP224R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_R1_256)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 256:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP256R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_R1_384)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 384:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP384R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_R1_521)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 521:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP521R1;
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 528:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits_is_sloppy) {
|
|
|
|
|
return MBEDTLS_ECP_DP_SECP521R1;
|
|
|
|
|
}
|
2021-01-27 15:44:45 +01:00
|
|
|
|
break;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
2019-12-03 17:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2020-06-02 18:19:28 +02:00
|
|
|
|
case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (bits) {
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 256:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_BP256R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 384:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_BP384R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 512:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_BP512R1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
2019-12-03 17:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2020-06-02 18:19:28 +02:00
|
|
|
|
case PSA_ECC_FAMILY_MONTGOMERY:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (bits) {
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#if defined(PSA_WANT_ECC_MONTGOMERY_255)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 255:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_CURVE25519;
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 256:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits_is_sloppy) {
|
|
|
|
|
return MBEDTLS_ECP_DP_CURVE25519;
|
|
|
|
|
}
|
2021-01-27 15:44:45 +01:00
|
|
|
|
break;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_MONTGOMERY_448)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 448:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_CURVE448;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
2019-12-03 17:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2020-06-02 18:19:28 +02:00
|
|
|
|
case PSA_ECC_FAMILY_SECP_K1:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (bits) {
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_K1_192)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 192:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP192K1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_K1_224)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 224:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP224K1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(PSA_WANT_ECC_SECP_K1_256)
|
2021-01-27 15:44:45 +01:00
|
|
|
|
case 256:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_SECP256K1;
|
2021-03-23 13:12:34 +01:00
|
|
|
|
#endif
|
2019-12-03 17:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
2018-06-20 00:20:32 +02:00
|
|
|
|
}
|
2021-01-27 15:44:45 +01:00
|
|
|
|
|
2021-03-23 14:17:55 +01:00
|
|
|
|
(void) bits_is_sloppy;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return MBEDTLS_ECP_DP_NONE;
|
2018-06-20 00:20:32 +02:00
|
|
|
|
}
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
2021-11-19 18:02:34 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
|
|
|
|
|
size_t bits)
|
2018-06-19 21:31:50 +02:00
|
|
|
|
{
|
|
|
|
|
/* Check that the bit size is acceptable for the key type */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (type) {
|
2018-06-19 21:31:50 +02:00
|
|
|
|
case PSA_KEY_TYPE_RAW_DATA:
|
|
|
|
|
case PSA_KEY_TYPE_HMAC:
|
2018-07-12 17:17:20 +02:00
|
|
|
|
case PSA_KEY_TYPE_DERIVE:
|
2022-05-25 11:30:27 +02:00
|
|
|
|
case PSA_KEY_TYPE_PASSWORD:
|
|
|
|
|
case PSA_KEY_TYPE_PASSWORD_HASH:
|
2018-07-12 17:17:20 +02:00
|
|
|
|
break;
|
2021-03-12 09:59:30 +01:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_AES)
|
2018-06-19 21:31:50 +02:00
|
|
|
|
case PSA_KEY_TYPE_AES:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits != 128 && bits != 192 && bits != 256) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-06-19 21:31:50 +02:00
|
|
|
|
break;
|
|
|
|
|
#endif
|
2021-09-21 11:59:39 +02:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_ARIA)
|
|
|
|
|
case PSA_KEY_TYPE_ARIA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits != 128 && bits != 192 && bits != 256) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-06-19 21:31:50 +02:00
|
|
|
|
break;
|
|
|
|
|
#endif
|
2021-03-12 09:59:30 +01:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
|
2018-06-19 21:31:50 +02:00
|
|
|
|
case PSA_KEY_TYPE_CAMELLIA:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits != 128 && bits != 192 && bits != 256) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-06-19 21:31:50 +02:00
|
|
|
|
break;
|
|
|
|
|
#endif
|
2021-03-12 09:59:30 +01:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_DES)
|
2018-06-19 21:31:50 +02:00
|
|
|
|
case PSA_KEY_TYPE_DES:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits != 64 && bits != 128 && bits != 192) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-06-19 21:31:50 +02:00
|
|
|
|
break;
|
|
|
|
|
#endif
|
2021-03-12 09:59:30 +01:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
|
2019-05-06 15:25:00 +02:00
|
|
|
|
case PSA_KEY_TYPE_CHACHA20:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits != 256) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-05-06 15:25:00 +02:00
|
|
|
|
break;
|
2018-06-19 21:31:50 +02:00
|
|
|
|
#endif
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
if (bits % 8 != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-06-19 21:31:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-06-19 21:31:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/** Check whether a given key type is valid for use with a given MAC algorithm
|
2021-03-02 15:11:57 +01:00
|
|
|
|
*
|
2021-03-08 12:00:27 +01:00
|
|
|
|
* Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
|
|
|
|
|
* when called with the validated \p algorithm and \p key_type is well-defined.
|
2021-03-03 19:04:05 +01:00
|
|
|
|
*
|
|
|
|
|
* \param[in] algorithm The specific MAC algorithm (can be wildcard).
|
2021-03-02 15:11:57 +01:00
|
|
|
|
* \param[in] key_type The key type of the key to be used with the
|
|
|
|
|
* \p algorithm.
|
|
|
|
|
*
|
|
|
|
|
* \retval #PSA_SUCCESS
|
2021-03-03 19:04:05 +01:00
|
|
|
|
* The \p key_type is valid for use with the \p algorithm
|
2021-03-02 21:29:45 +01:00
|
|
|
|
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
2021-03-03 19:04:05 +01:00
|
|
|
|
* The \p key_type is not valid for use with the \p algorithm
|
2021-03-02 15:11:57 +01:00
|
|
|
|
*/
|
2021-03-03 19:04:05 +01:00
|
|
|
|
MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
|
2021-03-02 21:31:17 +01:00
|
|
|
|
psa_algorithm_t algorithm,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_type_t key_type)
|
2021-03-02 15:11:57 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HMAC(algorithm)) {
|
|
|
|
|
if (key_type == PSA_KEY_TYPE_HMAC) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-03-02 15:11:57 +01:00
|
|
|
|
}
|
2021-03-03 19:04:05 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
|
|
|
|
|
* key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
|
|
|
|
|
PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
|
|
|
|
|
* the block length (larger than 1) for block ciphers. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-03-03 19:04:05 +01:00
|
|
|
|
}
|
2021-03-02 15:11:57 +01:00
|
|
|
|
}
|
2021-03-03 19:04:05 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2021-03-02 15:11:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
|
|
|
|
|
size_t buffer_length)
|
2020-07-28 14:30:13 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->key.data != NULL) {
|
|
|
|
|
return PSA_ERROR_ALREADY_EXISTS;
|
|
|
|
|
}
|
2020-07-28 14:30:13 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
slot->key.data = mbedtls_calloc(1, buffer_length);
|
|
|
|
|
if (slot->key.data == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2020-07-28 14:30:13 +02:00
|
|
|
|
|
2020-11-25 17:52:23 +01:00
|
|
|
|
slot->key.bytes = buffer_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2020-07-28 14:30:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
size_t data_length)
|
2020-10-13 20:27:40 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_allocate_buffer_to_slot(slot,
|
|
|
|
|
data_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-10-13 20:27:40 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(slot->key.data, data, data_length);
|
|
|
|
|
return PSA_SUCCESS;
|
2020-10-13 20:27:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 11:14:53 +01:00
|
|
|
|
psa_status_t psa_import_key_into_slot(
|
2020-11-28 15:54:54 +01:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *data, size_t data_length,
|
|
|
|
|
uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t *key_buffer_length, size_t *bits)
|
2018-01-28 13:16:24 +01:00
|
|
|
|
{
|
2020-11-28 15:54:54 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_type_t type = attributes->core.type;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
2020-07-24 22:04:59 +02:00
|
|
|
|
/* zero-length keys are never supported. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length == 0) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2020-07-24 22:04:59 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type_is_raw_bytes(type)) {
|
|
|
|
|
*bits = PSA_BYTES_TO_BITS(data_length);
|
2020-07-24 22:04:59 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_unstructured_key_bit_size(attributes->core.type,
|
|
|
|
|
*bits);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-07-24 22:04:59 +02:00
|
|
|
|
|
2020-11-28 15:14:42 +01:00
|
|
|
|
/* Copy the key material. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(key_buffer, data, data_length);
|
2020-11-28 15:14:42 +01:00
|
|
|
|
*key_buffer_length = data_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(void) key_buffer_size;
|
2020-07-24 22:04:59 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
} else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
|
|
|
|
if (PSA_KEY_TYPE_IS_ECC(type)) {
|
|
|
|
|
return mbedtls_psa_ecp_import_key(attributes,
|
|
|
|
|
data, data_length,
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
key_buffer_length,
|
|
|
|
|
bits);
|
2020-10-23 11:37:05 +02:00
|
|
|
|
}
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
|
2020-11-01 06:06:54 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
|
|
|
|
if (PSA_KEY_TYPE_IS_RSA(type)) {
|
|
|
|
|
return mbedtls_psa_rsa_import_key(attributes,
|
|
|
|
|
data, data_length,
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
key_buffer_length,
|
|
|
|
|
bits);
|
2020-10-23 11:37:05 +02:00
|
|
|
|
}
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2018-01-28 13:16:24 +01:00
|
|
|
|
}
|
2020-11-28 15:54:54 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-07-16 12:21:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 13:40:11 +01: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(
|
2021-03-02 16:34:49 +01:00
|
|
|
|
psa_key_type_t key_type,
|
2019-01-19 13:40:11 +01:00
|
|
|
|
psa_algorithm_t alg1,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t alg2)
|
2019-01-19 13:40:11 +01:00
|
|
|
|
{
|
2019-05-22 11:45:59 +02:00
|
|
|
|
/* Common case: both sides actually specify the same policy. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg1 == alg2) {
|
|
|
|
|
return alg1;
|
|
|
|
|
}
|
2021-02-18 12:07:20 +01:00
|
|
|
|
/* 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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_SIGN_HASH(alg1) &&
|
|
|
|
|
PSA_ALG_IS_SIGN_HASH(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;
|
|
|
|
|
}
|
2021-02-18 12:07:20 +01:00
|
|
|
|
}
|
|
|
|
|
/* If the policies are from the same AEAD family, check whether
|
2021-02-22 17:05:56 +01:00
|
|
|
|
* one of them is a minimum-tag-length wildcard. Calculate the most
|
2021-02-18 12:07:20 +01:00
|
|
|
|
* restrictive tag length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
|
|
|
|
|
(PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
|
|
|
|
|
PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
|
|
|
|
|
size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
|
|
|
|
|
size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
|
2021-03-08 12:00:27 +01:00
|
|
|
|
size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
|
2021-02-18 12:07:20 +01:00
|
|
|
|
|
2021-02-22 17:05:56 +01:00
|
|
|
|
/* If both are wildcards, return most restrictive wildcard */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
|
|
|
|
|
((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
|
|
|
|
|
return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
|
|
|
|
|
alg1, restricted_len);
|
2021-02-18 12:03:50 +01:00
|
|
|
|
}
|
2021-02-18 12:07:20 +01:00
|
|
|
|
/* If only one is a wildcard, return specific algorithm if compatible. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
|
|
|
|
|
(alg1_len <= alg2_len)) {
|
|
|
|
|
return alg2;
|
2021-02-18 12:03:50 +01:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
|
|
|
|
|
(alg2_len <= alg1_len)) {
|
|
|
|
|
return alg1;
|
2021-02-18 12:07:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* If the policies are from the same MAC family, check whether one
|
|
|
|
|
* of them is a minimum-MAC-length policy. Calculate the most
|
|
|
|
|
* restrictive tag length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
|
|
|
|
|
(PSA_ALG_FULL_LENGTH_MAC(alg1) ==
|
|
|
|
|
PSA_ALG_FULL_LENGTH_MAC(alg2))) {
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/* Validate the combination of key type and algorithm. Since the base
|
|
|
|
|
* algorithm of alg1 and alg2 are the same, we only need this once. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-03-02 16:34:49 +01:00
|
|
|
|
|
2021-03-03 20:47:40 +01:00
|
|
|
|
/* Get the (exact or at-least) output lengths for both sides of the
|
|
|
|
|
* requested intersection. None of the currently supported algorithms
|
|
|
|
|
* have an output length dependent on the actual key size, so setting it
|
|
|
|
|
* to a bogus value of 0 is currently OK.
|
|
|
|
|
*
|
2021-03-03 19:04:05 +01:00
|
|
|
|
* Note that for at-least-this-length wildcard algorithms, the output
|
|
|
|
|
* length is set to the shortest allowed length, which allows us to
|
|
|
|
|
* calculate the most restrictive tag length for the intersection. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
|
|
|
|
|
size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
|
2021-03-08 12:00:27 +01:00
|
|
|
|
size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
|
2021-02-18 12:03:50 +01:00
|
|
|
|
|
2021-02-25 10:20:29 +01:00
|
|
|
|
/* If both are wildcards, return most restrictive wildcard */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
|
|
|
|
|
((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
|
|
|
|
|
return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
|
2021-02-18 12:07:20 +01:00
|
|
|
|
}
|
2021-03-03 20:47:40 +01:00
|
|
|
|
|
|
|
|
|
/* If only one is an at-least-this-length policy, the intersection would
|
|
|
|
|
* be the other (fixed-length) policy as long as said fixed length is
|
|
|
|
|
* equal to or larger than the shortest allowed length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
|
|
|
|
|
return (alg1_len <= alg2_len) ? alg2 : 0;
|
2021-02-18 12:07:20 +01:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
|
|
|
|
|
return (alg2_len <= alg1_len) ? alg1 : 0;
|
2021-02-18 12:03:50 +01:00
|
|
|
|
}
|
2021-03-03 20:47:40 +01:00
|
|
|
|
|
2021-03-08 12:00:27 +01:00
|
|
|
|
/* If none of them are wildcards, check whether they define the same tag
|
|
|
|
|
* length. This is still possible here when one is default-length and
|
|
|
|
|
* the other specific-length. Ensure to always return the
|
|
|
|
|
* specific-length version for the intersection. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg1_len == alg2_len) {
|
|
|
|
|
return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
|
|
|
|
|
}
|
2019-01-19 13:40:11 +01:00
|
|
|
|
}
|
|
|
|
|
/* If the policies are incompatible, allow nothing. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return 0;
|
2019-01-19 13:40:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static int psa_key_algorithm_permits(psa_key_type_t key_type,
|
|
|
|
|
psa_algorithm_t policy_alg,
|
|
|
|
|
psa_algorithm_t requested_alg)
|
2019-01-14 16:06:39 +01:00
|
|
|
|
{
|
2019-05-22 11:45:59 +02:00
|
|
|
|
/* Common case: the policy only allows requested_alg. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (requested_alg == policy_alg) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2021-02-18 12:07:20 +01:00
|
|
|
|
/* If policy_alg is a hash-and-sign with a wildcard for the hash,
|
|
|
|
|
* and requested_alg is the same hash-and-sign family with any hash,
|
|
|
|
|
* then requested_alg is compliant with policy_alg. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
|
|
|
|
|
PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
|
|
|
|
|
return (policy_alg & ~PSA_ALG_HASH_MASK) ==
|
|
|
|
|
(requested_alg & ~PSA_ALG_HASH_MASK);
|
2021-02-18 12:07:20 +01:00
|
|
|
|
}
|
|
|
|
|
/* If policy_alg is a wildcard AEAD algorithm of the same base as
|
|
|
|
|
* the requested algorithm, check the requested tag length to be
|
|
|
|
|
* equal-length or longer than the wildcard-specified length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_AEAD(policy_alg) &&
|
|
|
|
|
PSA_ALG_IS_AEAD(requested_alg) &&
|
|
|
|
|
(PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
|
|
|
|
|
PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
|
|
|
|
|
((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
|
|
|
|
|
return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
|
|
|
|
|
PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
|
2021-02-18 12:07:20 +01:00
|
|
|
|
}
|
2021-03-08 12:00:27 +01:00
|
|
|
|
/* If policy_alg is a MAC algorithm of the same base as the requested
|
|
|
|
|
* algorithm, check whether their MAC lengths are compatible. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_MAC(policy_alg) &&
|
|
|
|
|
PSA_ALG_IS_MAC(requested_alg) &&
|
|
|
|
|
(PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
|
|
|
|
|
PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/* Validate the combination of key type and algorithm. Since the policy
|
|
|
|
|
* and requested algorithms are the same, we only need this once. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-03-03 19:04:05 +01:00
|
|
|
|
|
2021-03-03 20:47:40 +01:00
|
|
|
|
/* Get both the requested output length for the algorithm which is to be
|
|
|
|
|
* verified, and the default output length for the base algorithm.
|
|
|
|
|
* Note that none of the currently supported algorithms have an output
|
|
|
|
|
* length dependent on actual key size, so setting it to a bogus value
|
|
|
|
|
* of 0 is currently OK. */
|
2021-03-03 19:04:05 +01:00
|
|
|
|
size_t requested_output_length = PSA_MAC_LENGTH(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key_type, 0, requested_alg);
|
2021-03-03 19:04:05 +01:00
|
|
|
|
size_t default_output_length = PSA_MAC_LENGTH(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key_type, 0,
|
|
|
|
|
PSA_ALG_FULL_LENGTH_MAC(requested_alg));
|
2021-03-02 15:11:57 +01:00
|
|
|
|
|
|
|
|
|
/* If the policy is default-length, only allow an algorithm with
|
|
|
|
|
* a declared exact-length matching the default. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
|
|
|
|
|
return requested_output_length == default_output_length;
|
|
|
|
|
}
|
2021-03-02 15:11:57 +01:00
|
|
|
|
|
|
|
|
|
/* If the requested algorithm is default-length, allow it if the policy
|
2021-03-08 12:00:27 +01:00
|
|
|
|
* length exactly matches the default length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
|
|
|
|
|
PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
|
|
|
|
|
return 1;
|
2021-03-02 15:11:57 +01:00
|
|
|
|
}
|
2021-02-18 12:03:50 +01:00
|
|
|
|
|
2021-03-08 12:00:27 +01:00
|
|
|
|
/* If policy_alg is an at-least-this-length wildcard MAC algorithm,
|
|
|
|
|
* check for the requested MAC length to be equal to or longer than the
|
|
|
|
|
* minimum allowed length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
|
|
|
|
|
return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
|
|
|
|
|
requested_output_length;
|
2021-03-02 15:11:57 +01:00
|
|
|
|
}
|
2019-01-14 16:06:39 +01:00
|
|
|
|
}
|
2020-10-05 16:02:45 +02:00
|
|
|
|
/* If policy_alg is a generic key agreement operation, then using it for
|
2020-10-15 17:07:12 +02:00
|
|
|
|
* a key derivation with that key agreement should also be allowed. This
|
|
|
|
|
* behaviour is expected to be defined in a future specification version. */
|
2023-01-11 14:50:10 +01: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;
|
2020-10-05 16:02:45 +02:00
|
|
|
|
}
|
2021-02-18 12:03:50 +01:00
|
|
|
|
/* If it isn't explicitly permitted, it's forbidden. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return 0;
|
2019-01-14 16:06:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 19:33:38 +02:00
|
|
|
|
/** Test whether a policy permits an algorithm.
|
|
|
|
|
*
|
|
|
|
|
* The caller must test usage flags separately.
|
2021-02-23 14:18:32 +01:00
|
|
|
|
*
|
2021-03-02 21:27:42 +01:00
|
|
|
|
* \note This function requires providing the key type for which the policy is
|
|
|
|
|
* being validated, since some algorithm policy definitions (e.g. MAC)
|
|
|
|
|
* have different properties depending on what kind of cipher it is
|
|
|
|
|
* combined with.
|
|
|
|
|
*
|
2021-02-23 14:18:32 +01:00
|
|
|
|
* \retval PSA_SUCCESS When \p alg is a specific algorithm
|
|
|
|
|
* allowed by the \p policy.
|
|
|
|
|
* \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
|
|
|
|
|
* \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
|
|
|
|
|
* the \p policy does not allow it.
|
2019-05-10 19:33:38 +02:00
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
|
|
|
|
|
psa_key_type_t key_type,
|
|
|
|
|
psa_algorithm_t alg)
|
2019-05-10 19:33:38 +02:00
|
|
|
|
{
|
2021-02-25 11:29:17 +01:00
|
|
|
|
/* '0' is not a valid algorithm */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg == 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-02-25 11:29:17 +01:00
|
|
|
|
|
2021-02-23 14:18:32 +01:00
|
|
|
|
/* A requested algorithm cannot be a wildcard. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_WILDCARD(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-02-23 14:18:32 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
|
|
|
|
|
psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_NOT_PERMITTED;
|
|
|
|
|
}
|
2019-05-10 19:33:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 13:40:11 +01:00
|
|
|
|
/** Restrict a key policy based on a constraint.
|
2021-03-02 21:27:42 +01:00
|
|
|
|
*
|
|
|
|
|
* \note This function requires providing the key type for which the policy is
|
|
|
|
|
* being restricted, since some algorithm policy definitions (e.g. MAC)
|
|
|
|
|
* have different properties depending on what kind of cipher it is
|
|
|
|
|
* combined with.
|
2019-01-19 13:40:11 +01:00
|
|
|
|
*
|
2021-03-02 16:34:49 +01:00
|
|
|
|
* \param[in] key_type The key type for which to restrict the policy
|
2019-01-19 13:40:11 +01:00
|
|
|
|
* \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
|
2021-03-02 16:34:49 +01:00
|
|
|
|
* \c key_type, \c *policy and \c *constraint are incompatible.
|
2019-01-19 13:40:11 +01:00
|
|
|
|
* \c *policy is unchanged.
|
|
|
|
|
*/
|
|
|
|
|
static psa_status_t psa_restrict_key_policy(
|
2021-03-02 16:34:49 +01:00
|
|
|
|
psa_key_type_t key_type,
|
2019-01-19 13:40:11 +01:00
|
|
|
|
psa_key_policy_t *policy,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const psa_key_policy_t *constraint)
|
2019-01-19 13:40:11 +01:00
|
|
|
|
{
|
|
|
|
|
psa_algorithm_t intersection_alg =
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_policy_algorithm_intersection(key_type, policy->alg,
|
|
|
|
|
constraint->alg);
|
2019-05-10 19:33:38 +02:00
|
|
|
|
psa_algorithm_t intersection_alg2 =
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_policy_algorithm_intersection(key_type, policy->alg2,
|
|
|
|
|
constraint->alg2);
|
|
|
|
|
if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-01-19 13:40:11 +01:00
|
|
|
|
policy->usage &= constraint->usage;
|
|
|
|
|
policy->alg = intersection_alg;
|
2019-05-10 19:33:38 +02:00
|
|
|
|
policy->alg2 = intersection_alg2;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-01-19 13:40:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 10:51:35 +01:00
|
|
|
|
/** Get the description of a key given its identifier and policy constraints
|
|
|
|
|
* and lock it.
|
|
|
|
|
*
|
|
|
|
|
* 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. If \p alg is
|
|
|
|
|
* zero, the algorithm is not checked.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
static psa_status_t psa_get_and_lock_key_slot_with_policy(
|
2020-11-14 16:35:34 +01:00
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_key_slot_t **p_slot,
|
|
|
|
|
psa_key_usage_t usage,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t alg)
|
2018-07-16 12:21:11 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-12-22 13:34:47 +01:00
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2018-07-16 12:21:11 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot(key, p_slot);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
slot = *p_slot;
|
2018-07-16 12:21:11 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
|
2018-07-16 12:21:11 +02:00
|
|
|
|
usage &= ~PSA_KEY_USAGE_EXPORT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((slot->attr.policy.usage & usage) != usage) {
|
2021-03-02 11:44:51 +01:00
|
|
|
|
status = PSA_ERROR_NOT_PERMITTED;
|
2020-10-22 15:24:49 +02:00
|
|
|
|
goto error;
|
2021-03-02 11:44:51 +01:00
|
|
|
|
}
|
2019-01-14 16:06:39 +01:00
|
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
|
/* Enforce that the usage policy permits the requested algorithm. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg != 0) {
|
|
|
|
|
status = psa_key_policy_permits(&slot->attr.policy,
|
|
|
|
|
slot->attr.type,
|
|
|
|
|
alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-02-23 14:18:32 +01:00
|
|
|
|
goto error;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-02-23 14:18:32 +01:00
|
|
|
|
}
|
2018-07-16 12:21:11 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
*p_slot = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-07-16 12:21:11 +02:00
|
|
|
|
}
|
2018-07-13 14:18:51 +02:00
|
|
|
|
|
2020-11-14 16:35:34 +01:00
|
|
|
|
/** Get a key slot containing a transparent key and lock it.
|
2019-07-24 13:30:31 +02:00
|
|
|
|
*
|
|
|
|
|
* A transparent key is a key for which the key material is directly
|
2021-08-24 15:39:44 +02:00
|
|
|
|
* available, as opposed to a key in a secure element and/or to be used
|
|
|
|
|
* by a secure element.
|
2019-07-24 13:30:31 +02:00
|
|
|
|
*
|
2021-08-24 15:39:44 +02:00
|
|
|
|
* This is a temporary function that may be used instead of
|
|
|
|
|
* psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
|
|
|
|
|
* for a cryptographic operation.
|
2020-10-22 15:24:49 +02:00
|
|
|
|
*
|
2020-11-14 16:35:34 +01: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 13:30:31 +02:00
|
|
|
|
*/
|
2020-11-14 16:35:34 +01: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,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t alg)
|
2019-07-24 13:30:31 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
|
|
|
|
|
usage, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
|
|
|
|
|
psa_unlock_key_slot(*p_slot);
|
2019-07-24 13:30:31 +02:00
|
|
|
|
*p_slot = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2019-07-24 13:30:31 +02:00
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-07-24 13:30:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
|
2018-07-13 14:18:51 +02:00
|
|
|
|
{
|
2020-11-25 17:52:23 +01:00
|
|
|
|
/* Data pointer will always be either a valid pointer or NULL in an
|
|
|
|
|
* initialized slot, so we can just free it. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->key.data != NULL) {
|
|
|
|
|
mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
|
|
|
|
|
}
|
2020-11-25 17:52:23 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_free(slot->key.data);
|
2020-11-25 17:52:23 +01:00
|
|
|
|
slot->key.data = NULL;
|
|
|
|
|
slot->key.bytes = 0;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 11:58:46 +01:00
|
|
|
|
/** Completely wipe a slot in memory, including its policy.
|
|
|
|
|
* Persistent storage is not affected. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = psa_remove_key_data_from_memory(slot);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* As the return error code may not be handled in case of multiple errors,
|
|
|
|
|
* do our best to report an unexpected lock counter. Assert with
|
|
|
|
|
* MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
|
|
|
|
|
* if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
|
|
|
|
|
* function is called as part of the execution of a test suite, the
|
|
|
|
|
* execution of the test suite is stopped in error if the assertion fails.
|
|
|
|
|
*/
|
|
|
|
|
if (slot->lock_count != 1) {
|
|
|
|
|
MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
|
2020-10-30 14:07:07 +01:00
|
|
|
|
status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 15:01:08 +02: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 11:58:46 +01: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(slot, 0, sizeof(*slot));
|
|
|
|
|
return status;
|
2018-12-03 11:58:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
|
2018-11-15 15:48:15 +01:00
|
|
|
|
{
|
2018-12-04 17:12:32 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2019-08-13 15:58:36 +02:00
|
|
|
|
psa_status_t status; /* status of the last operation */
|
|
|
|
|
psa_status_t overall_status = PSA_SUCCESS;
|
2019-07-12 23:46:38 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
|
psa_se_drv_table_entry_t *driver;
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-11-15 15:48:15 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mbedtls_svc_key_id_is_null(key)) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2019-10-08 15:48:25 +02:00
|
|
|
|
|
2020-10-29 17:51:10 +01:00
|
|
|
|
/*
|
2020-11-10 18:08:03 +01:00
|
|
|
|
* Get the description of the key in a key slot. In case of a persistent
|
2020-10-29 17:51:10 +01: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.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot(key, &slot);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-11-15 15:48:15 +01:00
|
|
|
|
|
2020-10-29 17:51:10 +01: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.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->lock_count > 1) {
|
|
|
|
|
psa_unlock_key_slot(slot);
|
|
|
|
|
return PSA_ERROR_GENERIC_ERROR;
|
2020-10-29 17:51:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
|
2021-04-21 22:32:05 +02:00
|
|
|
|
/* Refuse the destruction of a read-only key (which may or may not work
|
|
|
|
|
* if we attempt it, depending on whether the key is merely read-only
|
|
|
|
|
* by policy or actually physically read-only).
|
2021-06-07 23:27:54 +02:00
|
|
|
|
* Just do the best we can, which is to wipe the copy in memory
|
|
|
|
|
* (done in this function's cleanup code). */
|
|
|
|
|
overall_status = PSA_ERROR_NOT_PERMITTED;
|
|
|
|
|
goto exit;
|
2021-04-21 22:32:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 23:46:38 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
driver = psa_get_se_driver_entry(slot->attr.lifetime);
|
|
|
|
|
if (driver != NULL) {
|
2019-07-25 11:32:45 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
|
2019-07-30 20:06:31 +02:00
|
|
|
|
psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
|
2019-07-30 20:06:31 +02:00
|
|
|
|
psa_crypto_transaction.key.id = slot->attr.id;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_crypto_save_transaction();
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
(void) psa_crypto_stop_transaction();
|
2019-08-13 15:58:36 +02: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 19:30:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_destroy_se_key(driver,
|
|
|
|
|
psa_key_slot_get_slot_number(slot));
|
|
|
|
|
if (overall_status == PSA_SUCCESS) {
|
2019-08-13 15:58:36 +02:00
|
|
|
|
overall_status = status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-07-22 19:30:34 +02:00
|
|
|
|
}
|
2019-07-12 23:46:38 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-11-15 15:48:15 +01:00
|
|
|
|
|
2018-06-18 18:27:26 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
|
|
|
|
|
status = psa_destroy_persistent_key(slot->attr.id);
|
|
|
|
|
if (overall_status == PSA_SUCCESS) {
|
2019-08-13 15:58:36 +02:00
|
|
|
|
overall_status = status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-08-13 15:58:36 +02:00
|
|
|
|
|
2019-08-13 15:14:20 +02: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 15:48:15 +01:00
|
|
|
|
}
|
2018-06-18 18:27:26 +02:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2018-11-15 15:48:15 +01:00
|
|
|
|
|
2019-07-22 19:30:34 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (driver != NULL) {
|
|
|
|
|
status = psa_save_se_persistent_data(driver);
|
|
|
|
|
if (overall_status == PSA_SUCCESS) {
|
2019-08-13 15:58:36 +02:00
|
|
|
|
overall_status = status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_crypto_stop_transaction();
|
|
|
|
|
if (overall_status == PSA_SUCCESS) {
|
2019-08-13 15:58:36 +02:00
|
|
|
|
overall_status = status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2018-06-18 18:27:26 +02:00
|
|
|
|
}
|
2019-07-22 19:30:34 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
2019-08-13 15:58:36 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_wipe_key_slot(slot);
|
2019-08-13 15:58:36 +02:00
|
|
|
|
/* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-08-13 15:58:36 +02:00
|
|
|
|
overall_status = status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
return overall_status;
|
2018-11-15 15:48:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2020-11-01 06:06:54 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2019-04-26 16:06:02 +02:00
|
|
|
|
static psa_status_t psa_get_rsa_public_exponent(
|
|
|
|
|
const mbedtls_rsa_context *rsa,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_attributes_t *attributes)
|
2019-04-26 16:06:02 +02:00
|
|
|
|
{
|
|
|
|
|
mbedtls_mpi mpi;
|
2019-11-22 14:21:35 +01:00
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-04-26 16:06:02 +02:00
|
|
|
|
uint8_t *buffer = NULL;
|
|
|
|
|
size_t buflen;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_mpi_init(&mpi);
|
2019-04-26 16:06:02 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
|
|
|
|
|
if (ret != 0) {
|
2019-04-26 16:06:02 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
|
2019-04-26 17:37:21 +02:00
|
|
|
|
/* It's the default value, which is reported as an empty string,
|
|
|
|
|
* so there's nothing to do. */
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2019-04-26 16:06:02 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
buflen = mbedtls_mpi_size(&mpi);
|
|
|
|
|
buffer = mbedtls_calloc(1, buflen);
|
|
|
|
|
if (buffer == NULL) {
|
2019-04-26 16:06:02 +02:00
|
|
|
|
ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
|
|
|
|
|
if (ret != 0) {
|
2019-04-26 16:06:02 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-04-26 16:06:02 +02:00
|
|
|
|
attributes->domain_parameters = buffer;
|
|
|
|
|
attributes->domain_parameters_size = buflen;
|
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_mpi_free(&mpi);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
mbedtls_free(buffer);
|
|
|
|
|
}
|
|
|
|
|
return mbedtls_to_psa_error(ret);
|
2018-07-06 16:02:09 +02:00
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
2020-11-04 21:28:15 +01:00
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2018-07-06 16:02:09 +02:00
|
|
|
|
|
2019-07-23 11:58:03 +02:00
|
|
|
|
/** Retrieve all the publicly-accessible attributes of a key.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_key_attributes_t *attributes)
|
2018-01-28 13:16:24 +01:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 17:12:32 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_reset_key_attributes(attributes);
|
2019-04-18 21:44:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-07-06 16:02:09 +02:00
|
|
|
|
|
2019-07-31 14:15:34 +02:00
|
|
|
|
attributes->core = slot->attr;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
|
|
|
|
|
MBEDTLS_PSA_KA_MASK_DUAL_USE);
|
2019-08-02 20:15:51 +02:00
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
|
|
|
|
|
psa_set_key_slot_number(attributes,
|
|
|
|
|
psa_key_slot_get_slot_number(slot));
|
|
|
|
|
}
|
2019-08-02 20:15:51 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-04-26 16:06:02 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (slot->attr.type) {
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2020-11-01 06:06:54 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2019-05-16 19:39:54 +02:00
|
|
|
|
case PSA_KEY_TYPE_RSA_KEY_PAIR:
|
2019-04-26 16:06:02 +02:00
|
|
|
|
case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
|
2019-08-13 13:15:34 +02:00
|
|
|
|
/* TODO: reporting the public exponent for opaque keys
|
2019-08-13 16:17:16 +02:00
|
|
|
|
* is not yet implemented.
|
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/216
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
|
2020-07-28 14:30:39 +02:00
|
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-07-24 22:48:15 +02:00
|
|
|
|
|
2020-11-25 14:58:33 +01:00
|
|
|
|
status = mbedtls_psa_rsa_load_representation(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
slot->attr.type,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
&rsa);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-07-24 22:48:15 +02:00
|
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-07-24 22:48:15 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_rsa_public_exponent(rsa,
|
|
|
|
|
attributes);
|
|
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
|
mbedtls_free(rsa);
|
2020-07-24 22:48:15 +02:00
|
|
|
|
}
|
2019-04-26 16:06:02 +02:00
|
|
|
|
break;
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
2020-11-04 21:28:15 +01:00
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2019-04-26 16:06:02 +02:00
|
|
|
|
default:
|
|
|
|
|
/* Nothing else to do. */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_reset_key_attributes(attributes);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-01-28 13:16:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 20:15:51 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
|
psa_status_t psa_get_key_slot_number(
|
|
|
|
|
const psa_key_attributes_t *attributes,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_slot_number_t *slot_number)
|
2019-08-02 20:15:51 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
|
2019-08-02 20:15:51 +02:00
|
|
|
|
*slot_number = attributes->slot_number;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-08-02 20:15:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
|
|
|
|
|
size_t key_buffer_size,
|
|
|
|
|
uint8_t *data,
|
|
|
|
|
size_t data_size,
|
|
|
|
|
size_t *data_length)
|
|
|
|
|
{
|
|
|
|
|
if (key_buffer_size > data_size) {
|
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
|
|
|
|
memcpy(data, key_buffer, key_buffer_size);
|
|
|
|
|
memset(data + key_buffer_size, 0,
|
|
|
|
|
data_size - key_buffer_size);
|
2020-11-25 16:46:05 +01:00
|
|
|
|
*data_length = key_buffer_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2020-07-24 22:48:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 14:46:37 +01:00
|
|
|
|
psa_status_t psa_export_key_internal(
|
2020-11-25 16:46:05 +01:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t *data, size_t data_size, size_t *data_length)
|
2018-01-28 13:16:24 +01:00
|
|
|
|
{
|
2020-11-25 16:46:05 +01:00
|
|
|
|
psa_key_type_t type = attributes->core.type;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type_is_raw_bytes(type) ||
|
|
|
|
|
PSA_KEY_TYPE_IS_RSA(type) ||
|
|
|
|
|
PSA_KEY_TYPE_IS_ECC(type)) {
|
|
|
|
|
return psa_export_key_buffer_internal(
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
data, data_size, data_length);
|
|
|
|
|
} else {
|
2020-11-26 13:36:23 +01:00
|
|
|
|
/* 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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-11-26 13:36:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
|
|
|
|
|
uint8_t *data,
|
|
|
|
|
size_t data_size,
|
|
|
|
|
size_t *data_length)
|
2020-11-26 13:36:23 +01:00
|
|
|
|
{
|
|
|
|
|
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 13:22:38 +01: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). */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_size == 0) {
|
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
2021-01-18 13:22:38 +01:00
|
|
|
|
|
2020-11-26 13:36:23 +01: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 18:20:43 +02:00
|
|
|
|
*data_length = 0;
|
|
|
|
|
|
2020-11-26 13:36:23 +01: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.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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 12:17:44 +02:00
|
|
|
|
|
2020-11-25 16:46:05 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_export_key(&attributes,
|
|
|
|
|
slot->key.data, slot->key.bytes,
|
|
|
|
|
data, data_size, data_length);
|
2020-11-26 13:36:23 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-11-26 13:36:23 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2020-11-26 13:36:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 14:46:37 +01:00
|
|
|
|
psa_status_t psa_export_public_key_internal(
|
2020-11-25 16:46:05 +01:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
|
size_t key_buffer_size,
|
|
|
|
|
uint8_t *data,
|
|
|
|
|
size_t data_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t *data_length)
|
2020-11-26 13:36:23 +01:00
|
|
|
|
{
|
2020-11-25 16:46:05 +01:00
|
|
|
|
psa_key_type_t type = attributes->core.type;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
|
|
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
|
2020-07-24 23:20:24 +02:00
|
|
|
|
/* Exporting public -> public */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_export_key_buffer_internal(
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
data, data_size, data_length);
|
2020-07-24 23:20:24 +02:00
|
|
|
|
}
|
2020-10-14 14:39:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_RSA(type)) {
|
2020-11-01 06:06:54 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
|
|
|
|
return mbedtls_psa_rsa_export_public_key(attributes,
|
|
|
|
|
key_buffer,
|
|
|
|
|
key_buffer_size,
|
|
|
|
|
data,
|
|
|
|
|
data_size,
|
|
|
|
|
data_length);
|
2018-07-24 17:33:30 +02:00
|
|
|
|
#else
|
2020-07-24 23:20:24 +02:00
|
|
|
|
/* We don't know how to convert a private RSA key to public. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2020-11-10 17:50:04 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
|
|
|
|
return mbedtls_psa_ecp_export_public_key(attributes,
|
|
|
|
|
key_buffer,
|
|
|
|
|
key_buffer_size,
|
|
|
|
|
data,
|
|
|
|
|
data_size,
|
|
|
|
|
data_length);
|
2018-07-24 17:33:30 +02:00
|
|
|
|
#else
|
2020-07-24 23:20:24 +02:00
|
|
|
|
/* We don't know how to convert a private ECC key to public */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
|
2018-01-28 18:16:59 +01:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2020-07-24 23:20:24 +02:00
|
|
|
|
/* 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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-07-24 23:20:24 +02:00
|
|
|
|
}
|
2018-01-28 13:16:24 +01:00
|
|
|
|
}
|
2020-11-25 16:46:05 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
|
|
|
|
|
uint8_t *data,
|
|
|
|
|
size_t data_size,
|
|
|
|
|
size_t *data_length)
|
2018-04-03 14:30:03 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 17:12:32 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2018-11-07 17:00:44 +01:00
|
|
|
|
|
2021-01-18 13:22:38 +01: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). */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_size == 0) {
|
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
2021-01-18 13:22:38 +01:00
|
|
|
|
|
2018-11-07 17:00:44 +01: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
|
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2020-11-26 13:36:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 16:46:05 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
2020-11-26 15:16:05 +01:00
|
|
|
|
status = psa_driver_wrapper_export_public_key(
|
2020-11-25 16:46:05 +01:00
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
data, data_size, data_length);
|
2020-11-26 13:36:23 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-04-03 14:30:03 +02:00
|
|
|
|
}
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
2023-03-08 15:19:51 +01:00
|
|
|
|
MBEDTLS_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")
|
|
|
|
|
MBEDTLS_STATIC_ASSERT(
|
|
|
|
|
(PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
|
|
|
|
|
"One or more key attribute flag is listed as both internal-only and dual-use")
|
|
|
|
|
MBEDTLS_STATIC_ASSERT(
|
|
|
|
|
(PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
|
|
|
|
|
"One or more key attribute flag is listed as both internal-only and external-only")
|
2019-08-02 19:19:39 +02:00
|
|
|
|
|
2019-07-31 17:21:46 +02: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.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
|
2018-11-07 17:05:30 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
|
|
|
|
|
PSA_KEY_USAGE_COPY |
|
|
|
|
|
PSA_KEY_USAGE_ENCRYPT |
|
|
|
|
|
PSA_KEY_USAGE_DECRYPT |
|
|
|
|
|
PSA_KEY_USAGE_SIGN_MESSAGE |
|
|
|
|
|
PSA_KEY_USAGE_VERIFY_MESSAGE |
|
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH |
|
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH |
|
|
|
|
|
PSA_KEY_USAGE_VERIFY_DERIVATION |
|
|
|
|
|
PSA_KEY_USAGE_DERIVE)) != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-04-17 15:05:45 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-11-07 17:05:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 17:21:46 +02: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,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_se_drv_table_entry_t **p_drv)
|
2019-01-19 13:40:11 +01:00
|
|
|
|
{
|
2020-06-08 18:37:19 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
|
|
|
|
|
mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_key_location(lifetime, p_drv);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2019-07-31 17:21:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_key_persistence(lifetime);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
2020-10-23 17:11:13 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
|
|
if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
|
|
|
|
|
if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2020-07-17 16:11:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_key_policy(&attributes->core.policy);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
2019-07-31 17:21:46 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2019-07-31 17:21:46 +02:00
|
|
|
|
|
2019-08-02 19:19:39 +02:00
|
|
|
|
/* Reject invalid flags. These should not be reachable through the API. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
|
|
|
|
|
MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-08-02 19:19:39 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-01-19 13:40:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 15:05:45 +02: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 11:37:03 +02:00
|
|
|
|
* This function is intended to be used as follows:
|
|
|
|
|
* -# Call psa_start_key_creation() to allocate a key slot, prepare
|
2020-08-04 09:51:30 +02:00
|
|
|
|
* it with the specified attributes, and in case of a volatile key assign it
|
|
|
|
|
* a volatile key identifier.
|
2019-04-28 11:37:03 +02: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 16:35:34 +01: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 15:24:49 +02:00
|
|
|
|
*
|
2019-07-15 22:02:14 +02:00
|
|
|
|
* \param method An identification of the calling function.
|
2019-06-26 18:34:38 +02: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 11:37:03 +02: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 15:05:45 +02:00
|
|
|
|
*/
|
|
|
|
|
static psa_status_t psa_start_key_creation(
|
2019-07-15 22:02:14 +02:00
|
|
|
|
psa_key_creation_method_t method,
|
2019-04-17 15:05:45 +02:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
2019-06-26 18:34:38 +02:00
|
|
|
|
psa_key_slot_t **p_slot,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_se_drv_table_entry_t **p_drv)
|
2019-01-19 13:40:11 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
2020-07-17 14:13:26 +02:00
|
|
|
|
psa_key_id_t volatile_key_id;
|
2019-04-17 15:05:45 +02:00
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
|
2019-07-15 22:02:14 +02:00
|
|
|
|
(void) method;
|
2019-06-26 18:34:38 +02:00
|
|
|
|
*p_drv = NULL;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_key_attributes(attributes, p_drv);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2019-04-17 15:05:45 +02:00
|
|
|
|
slot = *p_slot;
|
|
|
|
|
|
2019-07-31 14:15:34 +02: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 21:32:04 +02:00
|
|
|
|
* an input (generate, device) but not for those where the bit-size
|
2020-07-31 11:26:37 +02: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 14:15:34 +02:00
|
|
|
|
|
|
|
|
|
slot->attr = attributes->core;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
|
2020-07-31 11:26:37 +02:00
|
|
|
|
#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 17:26:54 +02:00
|
|
|
|
|
2019-08-02 19:19:39 +02:00
|
|
|
|
/* Erase external-only flags from the internal copy. To access
|
2019-08-07 15:42:14 +02:00
|
|
|
|
* external-only flags, query `attributes`. Thanks to the check
|
|
|
|
|
* in psa_validate_key_attributes(), this leaves the dual-use
|
2019-08-07 18:19:59 +02:00
|
|
|
|
* flags and any internal flag that psa_get_empty_key_slot()
|
2019-08-07 15:42:14 +02:00
|
|
|
|
* may have set. */
|
|
|
|
|
slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
|
2019-08-02 19:19:39 +02:00
|
|
|
|
|
2019-07-12 23:46:04 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-05 15:55:54 +02:00
|
|
|
|
/* For a key in a secure element, we need to do three things
|
2020-06-08 18:29:44 +02:00
|
|
|
|
* when creating or registering a persistent key:
|
2019-07-25 11:32:45 +02:00
|
|
|
|
* create the key file in internal storage, create the
|
|
|
|
|
* key inside the secure element, and update the driver's
|
2020-06-08 18:29:44 +02: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 11:32:45 +02: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 19:30:34 +02:00
|
|
|
|
* we can roll back to a state where the key doesn't exist. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (*p_drv != NULL) {
|
2020-11-25 17:52:23 +01:00
|
|
|
|
psa_key_slot_number_t slot_number;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_find_se_slot_for_key(attributes, method, *p_drv,
|
|
|
|
|
&slot_number);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-06-08 18:29:44 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
|
|
|
|
|
psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
|
2020-06-08 18:29:44 +02:00
|
|
|
|
psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
|
2020-11-25 17:52:23 +01:00
|
|
|
|
psa_crypto_transaction.key.slot = slot_number;
|
2020-06-08 18:29:44 +02:00
|
|
|
|
psa_crypto_transaction.key.id = slot->attr.id;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_crypto_save_transaction();
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
(void) psa_crypto_stop_transaction();
|
|
|
|
|
return status;
|
2020-06-08 18:29:44 +02:00
|
|
|
|
}
|
2019-07-25 18:02:52 +02:00
|
|
|
|
}
|
2020-11-25 17:52:23 +01:00
|
|
|
|
|
|
|
|
|
status = psa_copy_key_material_into_slot(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
slot, (uint8_t *) (&slot_number), sizeof(slot_number));
|
2019-01-19 13:40:11 +01:00
|
|
|
|
}
|
2019-10-01 14:18:35 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
|
2019-10-01 14:18:35 +02:00
|
|
|
|
/* Key registration only makes sense with a secure element. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-10-01 14:18:35 +02:00
|
|
|
|
}
|
2019-07-12 23:46:04 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-01-19 13:40:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 15:05:45 +02: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 11:37:03 +02:00
|
|
|
|
* See the documentation of psa_start_key_creation() for the intended use
|
|
|
|
|
* of this function.
|
2019-04-17 15:05:45 +02:00
|
|
|
|
*
|
2020-11-14 16:35:34 +01: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 11:28:25 +01:00
|
|
|
|
*
|
2019-06-26 18:34:38 +02: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 12:10:32 +01: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 11:37:03 +02:00
|
|
|
|
*
|
|
|
|
|
* \retval #PSA_SUCCESS
|
2020-08-04 09:51:30 +02:00
|
|
|
|
* The key was successfully created.
|
2023-02-14 19:21:09 +01:00
|
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
|
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
|
|
|
|
* \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
|
|
|
|
|
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
|
|
|
|
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
|
|
|
|
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
2020-11-09 17:42:55 +01:00
|
|
|
|
*
|
2019-04-28 11:37:03 +02: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 15:05:45 +02:00
|
|
|
|
*/
|
2019-06-26 18:34:38 +02:00
|
|
|
|
static psa_status_t psa_finish_key_creation(
|
|
|
|
|
psa_key_slot_t *slot,
|
2020-11-14 12:10:32 +01:00
|
|
|
|
psa_se_drv_table_entry_t *driver,
|
|
|
|
|
mbedtls_svc_key_id_t *key)
|
2019-04-17 15:05:45 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
2019-04-25 13:47:40 +02:00
|
|
|
|
(void) slot;
|
2019-06-26 18:34:38 +02:00
|
|
|
|
(void) driver;
|
2019-04-17 15:05:45 +02:00
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
|
2019-07-23 16:13:14 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (driver != NULL) {
|
2019-07-30 21:32:04 +02:00
|
|
|
|
psa_se_key_data_storage_t data;
|
2020-11-25 17:52:23 +01:00
|
|
|
|
psa_key_slot_number_t slot_number =
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_slot_get_slot_number(slot);
|
2020-11-25 17:52:23 +01:00
|
|
|
|
|
2023-03-08 15:19:51 +01:00
|
|
|
|
MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
|
|
|
|
|
sizeof(data.slot_number),
|
|
|
|
|
"Slot number size does not match psa_se_key_data_storage_t");
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
|
|
|
|
|
status = psa_save_persistent_key(&slot->attr,
|
|
|
|
|
(uint8_t *) &data,
|
|
|
|
|
sizeof(data));
|
|
|
|
|
} else
|
2019-07-23 16:13:14 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
{
|
2020-10-29 11:42:22 +01:00
|
|
|
|
/* Key material is saved in export representation in the slot, so
|
|
|
|
|
* just pass the slot buffer for storage. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_save_persistent_key(&slot->attr,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes);
|
2019-07-23 16:13:14 +02:00
|
|
|
|
}
|
2019-04-17 15:05:45 +02:00
|
|
|
|
}
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
|
|
|
|
|
2019-07-12 23:46:04 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-05 15:55:54 +02: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 18:29:44 +02:00
|
|
|
|
* creation of a persistent key in a secure element requires a transaction,
|
|
|
|
|
* but registration or volatile key creation doesn't use one). */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (driver != NULL &&
|
|
|
|
|
psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
|
|
|
|
|
status = psa_save_se_persistent_data(driver);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_destroy_persistent_key(slot->attr.id);
|
|
|
|
|
return status;
|
2019-07-12 23:46:04 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_crypto_stop_transaction();
|
2019-07-12 23:46:04 +02:00
|
|
|
|
}
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2020-11-14 12:10:32 +01:00
|
|
|
|
*key = slot->attr.id;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_unlock_key_slot(slot);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-14 12:10:32 +01:00
|
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-11-14 12:10:32 +01:00
|
|
|
|
}
|
2020-11-14 11:28:25 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2019-04-17 15:05:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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 11:37:03 +02:00
|
|
|
|
* See the documentation of psa_start_key_creation() for the intended use
|
|
|
|
|
* of this function.
|
2019-04-17 15:05:45 +02:00
|
|
|
|
*
|
2019-06-26 18:34:38 +02: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 15:05:45 +02:00
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static void psa_fail_key_creation(psa_key_slot_t *slot,
|
|
|
|
|
psa_se_drv_table_entry_t *driver)
|
2019-04-17 15:05:45 +02:00
|
|
|
|
{
|
2019-06-26 18:34:38 +02:00
|
|
|
|
(void) driver;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot == NULL) {
|
2019-04-17 15:05:45 +02:00
|
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-26 18:34:38 +02:00
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-08-13 13:15:34 +02:00
|
|
|
|
/* TODO: If the key has already been created in the secure
|
2019-06-26 18:34:38 +02: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 16:17:16 +02:00
|
|
|
|
* element.
|
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/217
|
|
|
|
|
*/
|
2019-07-22 19:30:34 +02:00
|
|
|
|
|
2019-08-05 15:55:54 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(void) psa_crypto_stop_transaction();
|
2019-06-26 18:34:38 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_wipe_key_slot(slot);
|
2019-04-17 15:05:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 17:21:46 +02: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 16:57:15 +02:00
|
|
|
|
const psa_key_slot_t *slot,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const psa_key_attributes_t *attributes)
|
2019-05-03 16:57:15 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (attributes->core.type != 0) {
|
|
|
|
|
if (attributes->core.type != slot->attr.type) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-05-03 16:57:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (attributes->domain_parameters_size != 0) {
|
2020-11-01 06:06:54 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
|
|
|
|
if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
|
2020-07-28 14:30:39 +02:00
|
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2020-07-28 14:30:13 +02:00
|
|
|
|
mbedtls_mpi actual, required;
|
2020-07-30 11:36:45 +02:00
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-07-24 22:48:15 +02:00
|
|
|
|
|
2020-11-25 14:58:33 +01:00
|
|
|
|
psa_status_t status = mbedtls_psa_rsa_load_representation(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
slot->attr.type,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
&rsa);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mbedtls_mpi_init(&actual);
|
|
|
|
|
mbedtls_mpi_init(&required);
|
|
|
|
|
ret = mbedtls_rsa_export(rsa,
|
|
|
|
|
NULL, NULL, NULL, NULL, &actual);
|
|
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
|
mbedtls_free(rsa);
|
|
|
|
|
if (ret != 0) {
|
2019-05-03 16:57:15 +02:00
|
|
|
|
goto rsa_exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
ret = mbedtls_mpi_read_binary(&required,
|
|
|
|
|
attributes->domain_parameters,
|
|
|
|
|
attributes->domain_parameters_size);
|
|
|
|
|
if (ret != 0) {
|
2019-05-03 16:57:15 +02:00
|
|
|
|
goto rsa_exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
|
2019-05-03 16:57:15 +02:00
|
|
|
|
ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
rsa_exit:
|
|
|
|
|
mbedtls_mpi_free(&actual);
|
|
|
|
|
mbedtls_mpi_free(&required);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
return mbedtls_to_psa_error(ret);
|
|
|
|
|
}
|
|
|
|
|
} else
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2019-05-03 16:57:15 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-05-03 16:57:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (attributes->core.bits != 0) {
|
|
|
|
|
if (attributes->core.bits != slot->attr.bits) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-05-03 16:57:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-05-03 16:57:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
size_t data_length,
|
|
|
|
|
mbedtls_svc_key_id_t *key)
|
2019-04-17 15:05:45 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-07-12 23:40:35 +02:00
|
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2020-11-30 12:11:01 +01:00
|
|
|
|
size_t bits;
|
2021-06-14 06:34:16 +02:00
|
|
|
|
size_t storage_size = data_length;
|
2019-05-03 16:57:15 +02:00
|
|
|
|
|
2020-11-14 12:10:32 +01:00
|
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
|
2019-09-12 19:03:13 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length == 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-09-12 19:03:13 +02:00
|
|
|
|
|
2021-09-08 12:06:05 +02:00
|
|
|
|
/* Ensure that the bytes-to-bits conversion cannot overflow. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length > SIZE_MAX / 8) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2021-08-04 07:17:15 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
|
|
|
|
|
&slot, &driver);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-05-03 16:57:15 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-05-03 16:57:15 +02:00
|
|
|
|
|
2020-11-30 12:11:01 +01:00
|
|
|
|
/* In the case of a transparent key or an opaque key stored in local
|
2021-09-08 12:06:05 +02:00
|
|
|
|
* storage ( thus not in the case of importing a key in a secure element
|
|
|
|
|
* with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
|
|
|
|
|
* buffer to hold the imported key material. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->key.data == NULL) {
|
|
|
|
|
if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
|
2021-09-08 12:06:05 +02:00
|
|
|
|
status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes, data, data_length, &storage_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-14 06:34:16 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-14 06:34:16 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_allocate_buffer_to_slot(slot, storage_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-07-30 21:32:04 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-07-12 23:47:28 +02:00
|
|
|
|
}
|
2020-11-30 12:11:01 +01:00
|
|
|
|
|
|
|
|
|
bits = slot->attr.bits;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_import_key(attributes,
|
|
|
|
|
data, data_length,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
&slot->key.bytes, &bits);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-30 12:11:01 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-11-30 12:11:01 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->attr.bits == 0) {
|
2020-11-30 12:11:01 +01:00
|
|
|
|
slot->attr.bits = (psa_key_bits_t) bits;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else if (bits != slot->attr.bits) {
|
2021-01-15 17:36:02 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2019-07-12 23:47:28 +02:00
|
|
|
|
}
|
2020-11-28 15:54:54 +01:00
|
|
|
|
|
2021-08-04 07:17:15 +02:00
|
|
|
|
/* Enforce a size limit, and in particular ensure that the bit
|
|
|
|
|
* size fits in its representation type.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits > PSA_MAX_KEY_BITS) {
|
2021-08-04 07:17:15 +02:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_optional_attributes(slot, attributes);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-07-24 20:25:59 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-05-03 16:57:15 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_finish_key_creation(slot, driver, key);
|
2019-05-03 16:57:15 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_fail_key_creation(slot, driver);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2019-04-17 15:05:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 15:55:54 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
|
psa_status_t mbedtls_psa_register_se_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const psa_key_attributes_t *attributes)
|
2019-08-05 15:55:54 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
|
|
|
|
psa_key_slot_t *slot = NULL;
|
|
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2020-08-04 09:51:30 +02:00
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-08-05 15:55:54 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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,
|
|
|
|
|
&slot, &driver);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-08-05 15:55:54 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-08-05 15:55:54 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_finish_key_creation(slot, driver, &key);
|
2019-08-05 15:55:54 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_fail_key_creation(slot, driver);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2019-08-05 15:55:54 +02:00
|
|
|
|
/* Registration doesn't keep the key in RAM. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_close_key(key);
|
|
|
|
|
return status;
|
2019-08-05 15:55:54 +02:00
|
|
|
|
}
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
|
|
|
|
|
const psa_key_attributes_t *specified_attributes,
|
|
|
|
|
mbedtls_svc_key_id_t *target_key)
|
2019-04-18 21:44:46 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2019-04-18 21:44:46 +02: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 23:40:35 +02:00
|
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2021-07-04 22:48:48 +02:00
|
|
|
|
size_t storage_size = 0;
|
2019-04-18 21:44:46 +02:00
|
|
|
|
|
2020-11-14 12:10:32 +01:00
|
|
|
|
*target_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
|
2021-07-04 22:48:48 +02:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-04-18 21:44:46 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-04-18 21:44:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_validate_optional_attributes(source_slot,
|
|
|
|
|
specified_attributes);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-05-03 16:57:15 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-05-03 16:57:15 +02:00
|
|
|
|
|
2021-09-10 02:52:44 +02:00
|
|
|
|
/* The target key type and number of bits have been validated by
|
|
|
|
|
* psa_validate_optional_attributes() to be either equal to zero or
|
|
|
|
|
* equal to the ones of the source key. So it is safe to inherit
|
|
|
|
|
* them from the source key now."
|
2021-09-08 12:20:28 +02:00
|
|
|
|
* */
|
2021-09-10 02:52:44 +02:00
|
|
|
|
actual_attributes.core.bits = source_slot->attr.bits;
|
|
|
|
|
actual_attributes.core.type = source_slot->attr.type;
|
2021-09-08 12:20:28 +02:00
|
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_restrict_key_policy(source_slot->attr.type,
|
|
|
|
|
&actual_attributes.core.policy,
|
|
|
|
|
&source_slot->attr.policy);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-04-18 21:44:46 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-04-18 21:44:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
|
|
|
|
|
&target_slot, &driver);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-04-18 21:44:46 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
|
|
|
|
|
PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
|
2021-07-04 22:48:48 +02:00
|
|
|
|
/*
|
2021-09-10 02:52:44 +02:00
|
|
|
|
* If the source and target keys are stored in different locations,
|
2021-07-04 22:48:48 +02:00
|
|
|
|
* the source key would need to be exported as plaintext and re-imported
|
|
|
|
|
* in the other location. This has security implications which have not
|
2021-09-08 12:06:05 +02:00
|
|
|
|
* been fully mapped. For now, this can be achieved through
|
2021-07-04 22:48:48 +02:00
|
|
|
|
* appropriate API invocations from the application, if needed.
|
|
|
|
|
* */
|
2019-07-24 13:44:30 +02:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-07-04 22:48:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* When the source and target keys are within the same location,
|
2021-09-08 12:06:05 +02:00
|
|
|
|
* - For transparent keys it is a blind copy without any driver invocation,
|
2021-07-04 22:48:48 +02:00
|
|
|
|
* - For opaque keys this translates to an invocation of the drivers'
|
|
|
|
|
* copy_key entry point through the dispatch layer.
|
|
|
|
|
* */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
|
|
|
|
|
status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
|
|
|
|
|
&storage_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-04 22:48:48 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-02 12:27:47 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_allocate_buffer_to_slot(target_slot, storage_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-04 22:48:48 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-04-18 21:44:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_copy_key(&actual_attributes,
|
|
|
|
|
source_slot->key.data,
|
|
|
|
|
source_slot->key.bytes,
|
|
|
|
|
target_slot->key.data,
|
|
|
|
|
target_slot->key.bytes,
|
|
|
|
|
&target_slot->key.bytes);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-04 22:48:48 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
status = psa_copy_key_material_into_slot(target_slot,
|
2021-09-10 02:52:44 +02:00
|
|
|
|
source_slot->key.data,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
source_slot->key.bytes);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-04 22:48:48 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-02 12:27:47 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_finish_key_creation(target_slot, driver, target_key);
|
2019-04-18 21:44:46 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_fail_key_creation(target_slot, driver);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(source_slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2019-04-18 21:44:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-18 21:49:39 +02:00
|
|
|
|
|
|
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Message digests */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
|
2018-02-07 21:05:37 +01:00
|
|
|
|
{
|
2021-03-04 13:01:18 +01:00
|
|
|
|
/* Aborting a non-active operation is allowed */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-03-04 13:01:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_driver_wrapper_hash_abort(operation);
|
2021-03-04 13:01:18 +01:00
|
|
|
|
operation->id = 0;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-02-07 21:05:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
|
|
|
|
|
psa_algorithm_t alg)
|
2018-02-07 21:05:37 +01:00
|
|
|
|
{
|
2021-06-24 12:49:14 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2021-03-04 13:01:18 +01:00
|
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id != 0) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-03-04 13:01:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_HASH(alg)) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2019-02-19 10:25:10 +01:00
|
|
|
|
|
2021-03-15 19:00:14 +01:00
|
|
|
|
/* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
|
|
|
|
|
* directly zeroes the int-sized dummy member of the context union. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(&operation->ctx, 0, sizeof(operation->ctx));
|
2021-03-15 12:23:37 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_hash_setup(operation, alg);
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_hash_abort(operation);
|
|
|
|
|
}
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
|
|
|
|
return status;
|
2018-02-07 21:05:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_hash_update(psa_hash_operation_t *operation,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length)
|
2018-02-07 21:05:37 +01:00
|
|
|
|
{
|
2021-06-24 12:49:14 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2018-07-12 16:58:43 +02:00
|
|
|
|
|
2021-03-04 14:02:19 +01:00
|
|
|
|
/* Don't require hash implementations to behave correctly on a
|
|
|
|
|
* zero-length input, which may have an invalid pointer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (input_length == 0) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-03-04 14:02:19 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_hash_update(operation, input, input_length);
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_hash_abort(operation);
|
|
|
|
|
}
|
2021-03-04 13:01:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-02-07 21:05:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
|
|
|
|
|
uint8_t *hash,
|
|
|
|
|
size_t hash_size,
|
|
|
|
|
size_t *hash_length)
|
2018-02-07 21:05:37 +01:00
|
|
|
|
{
|
2021-03-08 18:41:12 +01:00
|
|
|
|
*hash_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2018-02-07 21:05:37 +01:00
|
|
|
|
|
2021-02-18 17:24:37 +01:00
|
|
|
|
psa_status_t status = psa_driver_wrapper_hash_finish(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
operation, hash, hash_size, hash_length);
|
|
|
|
|
psa_hash_abort(operation);
|
|
|
|
|
return status;
|
2018-02-07 21:05:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
|
|
|
|
|
const uint8_t *hash,
|
|
|
|
|
size_t hash_length)
|
2018-02-07 21:05:37 +01:00
|
|
|
|
{
|
2021-10-18 09:47:58 +02:00
|
|
|
|
uint8_t actual_hash[PSA_HASH_MAX_SIZE];
|
2018-02-07 21:05:37 +01:00
|
|
|
|
size_t actual_hash_length;
|
2021-03-04 13:01:18 +01:00
|
|
|
|
psa_status_t status = psa_hash_finish(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
operation,
|
|
|
|
|
actual_hash, sizeof(actual_hash),
|
|
|
|
|
&actual_hash_length);
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (actual_hash_length != hash_length) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-24 12:49:14 +02:00
|
|
|
|
psa_hash_abort(operation);
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-24 12:49:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-02-07 21:05:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01: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)
|
2019-11-28 19:33:58 +01:00
|
|
|
|
{
|
2021-03-08 18:41:12 +01:00
|
|
|
|
*hash_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_HASH(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-03-08 18:40:40 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_driver_wrapper_hash_compute(alg, input, input_length,
|
|
|
|
|
hash, hash_size, hash_length);
|
2019-11-28 19:33:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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)
|
2019-11-28 19:33:58 +01:00
|
|
|
|
{
|
2021-10-18 09:47:58 +02:00
|
|
|
|
uint8_t actual_hash[PSA_HASH_MAX_SIZE];
|
2021-02-18 16:22:53 +01:00
|
|
|
|
size_t actual_hash_length;
|
2021-03-08 18:40:40 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_HASH(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-03-08 18:40:40 +01:00
|
|
|
|
|
2021-02-18 17:24:37 +01:00
|
|
|
|
psa_status_t status = psa_driver_wrapper_hash_compute(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
alg, input, input_length,
|
|
|
|
|
actual_hash, sizeof(actual_hash),
|
|
|
|
|
&actual_hash_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-12-13 12:33:18 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (actual_hash_length != hash_length) {
|
2021-12-13 12:33:18 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
|
2021-12-13 12:33:18 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-12-13 12:33:18 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
|
|
|
|
|
return status;
|
2019-11-28 19:33:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
|
|
|
|
|
psa_hash_operation_t *target_operation)
|
2019-01-19 12:03:41 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (source_operation->id == 0 ||
|
|
|
|
|
target_operation->id != 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-03-04 13:01:18 +01:00
|
|
|
|
}
|
2019-01-19 12:03:41 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
|
|
|
|
|
target_operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_hash_abort(target_operation);
|
|
|
|
|
}
|
2021-03-04 13:01:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2019-01-19 12:03:41 +01:00
|
|
|
|
}
|
2018-02-07 21:05:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
2018-02-08 10:02:12 +01:00
|
|
|
|
/* MAC */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
|
2018-06-20 16:05:20 +02:00
|
|
|
|
{
|
2021-03-19 18:28:56 +01:00
|
|
|
|
/* Aborting a non-active operation is allowed */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2018-06-11 17:26:17 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_driver_wrapper_mac_abort(operation);
|
2021-05-07 14:14:37 +02:00
|
|
|
|
operation->mac_size = 0;
|
|
|
|
|
operation->is_sign = 0;
|
2021-03-19 18:28:56 +01:00
|
|
|
|
operation->id = 0;
|
2018-06-11 17:26:17 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-06-11 17:26:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 16:33:22 +02:00
|
|
|
|
static psa_status_t psa_mac_finalize_alg_and_key_validation(
|
|
|
|
|
psa_algorithm_t alg,
|
2021-06-17 16:46:44 +02:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t *mac_size)
|
2018-02-08 10:02:12 +01:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_type_t key_type = psa_get_key_type(attributes);
|
|
|
|
|
size_t key_bits = psa_get_key_bits(attributes);
|
2021-03-19 17:05:52 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_MAC(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-03-19 18:28:56 +01:00
|
|
|
|
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/* Validate the combination of key type and algorithm */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_key_can_do(alg, key_type);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2021-03-02 16:16:22 +01:00
|
|
|
|
|
2021-05-10 11:13:41 +02:00
|
|
|
|
/* Get the output length for the algorithm and key combination */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
*mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
|
2021-03-02 16:16:22 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (*mac_size < 4) {
|
2021-03-02 16:16:22 +01: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-03-02 16:16:22 +01:00
|
|
|
|
}
|
2018-07-06 16:07:47 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
|
|
|
|
|
PSA_ALG_FULL_LENGTH_MAC(alg))) {
|
2021-03-03 19:04:05 +01:00
|
|
|
|
/* It's impossible to "truncate" to a larger length than the full length
|
|
|
|
|
* of the algorithm. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2021-03-03 19:04:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (*mac_size > PSA_MAC_MAX_SIZE) {
|
2022-03-16 13:54:49 +01:00
|
|
|
|
/* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
|
|
|
|
|
* that is disabled in the compile-time configuration. The result can
|
|
|
|
|
* therefore be larger than PSA_MAC_MAX_SIZE, which does take the
|
|
|
|
|
* configuration into account. In this case, force a return of
|
|
|
|
|
* PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
|
|
|
|
|
* psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
|
|
|
|
|
* PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
|
|
|
|
|
* is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
|
|
|
|
|
* systematically generated tests. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2022-03-16 13:54:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-06-17 16:33:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
int is_sign)
|
2018-02-08 10:02:12 +01:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-06-24 12:49:45 +02:00
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2021-03-19 17:05:52 +01:00
|
|
|
|
|
2019-02-19 10:25:10 +01:00
|
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id != 0) {
|
2021-06-24 12:49:45 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-04-29 16:21:24 +02:00
|
|
|
|
|
2021-03-19 18:28:56 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key,
|
|
|
|
|
&slot,
|
|
|
|
|
is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
|
|
|
|
|
alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-24 12:49:45 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-02 16:16:22 +01:00
|
|
|
|
|
2021-03-19 18:28:56 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
|
|
|
|
|
&operation->mac_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-02 16:16:22 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-02 16:16:22 +01:00
|
|
|
|
|
2021-05-07 14:14:37 +02:00
|
|
|
|
operation->is_sign = is_sign;
|
2021-03-19 18:28:56 +01:00
|
|
|
|
/* Dispatch the MAC setup call with validated input */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (is_sign) {
|
|
|
|
|
status = psa_driver_wrapper_mac_sign_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg);
|
|
|
|
|
} else {
|
|
|
|
|
status = psa_driver_wrapper_mac_verify_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg);
|
2018-02-08 10:02:12 +01:00
|
|
|
|
}
|
2018-06-11 17:26:17 +02:00
|
|
|
|
|
2018-07-08 20:51:54 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_mac_abort(operation);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-02-08 10:02:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2018-07-08 20:12:23 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_mac_setup(operation, key, alg, 1);
|
2018-07-08 20:12:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2018-07-08 20:12:23 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_mac_setup(operation, key, alg, 0);
|
2018-07-08 20:12:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_mac_update(psa_mac_operation_t *operation,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length)
|
2018-02-08 10:02:12 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
2021-03-19 18:38:46 +01:00
|
|
|
|
/* Don't require hash implementations to behave correctly on a
|
|
|
|
|
* zero-length input, which may have an invalid pointer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (input_length == 0) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2018-06-04 15:03:32 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_driver_wrapper_mac_update(operation,
|
|
|
|
|
input, input_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_mac_abort(operation);
|
|
|
|
|
}
|
2021-03-19 18:38:46 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-02-08 10:02:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01: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 17:34:15 +02:00
|
|
|
|
{
|
2021-03-19 19:04:39 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-07-08 20:35:02 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-06-23 12:38:39 +02:00
|
|
|
|
}
|
2019-02-15 15:05:35 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!operation->is_sign) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-06-23 12:38:39 +02:00
|
|
|
|
}
|
2018-07-08 20:35:02 +02:00
|
|
|
|
|
2021-05-11 11:09:13 +02:00
|
|
|
|
/* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
|
|
|
|
|
* once all the error checks are done. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->mac_size == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-06-23 12:38:39 +02:00
|
|
|
|
}
|
2021-05-11 11:09:13 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mac_size < operation->mac_size) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-06-23 12:38:39 +02:00
|
|
|
|
}
|
2018-04-02 17:34:15 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_mac_sign_finish(operation,
|
|
|
|
|
mac, operation->mac_size,
|
|
|
|
|
mac_length);
|
2018-07-08 20:35:02 +02:00
|
|
|
|
|
2021-06-25 10:09:02 +02:00
|
|
|
|
exit:
|
2021-06-18 13:05:48 +02:00
|
|
|
|
/* In case of success, set the potential excess room in the output buffer
|
|
|
|
|
* to an invalid value, to avoid potentially leaking a longer MAC.
|
|
|
|
|
* In case of error, set the output length and content to a safe default,
|
|
|
|
|
* such that in case the caller misses an error check, the output would be
|
|
|
|
|
* an unachievable MAC.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-07 14:14:37 +02:00
|
|
|
|
*mac_length = mac_size;
|
2021-06-18 13:05:48 +02:00
|
|
|
|
operation->mac_size = 0;
|
2021-05-07 14:14:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 19:11:59 +01:00
|
|
|
|
psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
|
2021-06-18 13:05:48 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
abort_status = psa_mac_abort(operation);
|
2021-03-19 19:04:39 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status == PSA_SUCCESS ? abort_status : status;
|
2018-04-02 17:34:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
|
|
|
|
|
const uint8_t *mac,
|
|
|
|
|
size_t mac_length)
|
2018-02-08 10:02:12 +01:00
|
|
|
|
{
|
2021-03-19 19:04:39 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2019-02-15 15:05:35 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-06-23 12:38:39 +02:00
|
|
|
|
}
|
2018-04-02 17:34:15 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->is_sign) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-06-23 12:38:39 +02:00
|
|
|
|
}
|
2021-05-07 14:14:37 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->mac_size != mac_length) {
|
2021-05-07 14:14:37 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2021-06-25 10:09:02 +02:00
|
|
|
|
goto exit;
|
2021-05-07 14:14:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_mac_verify_finish(operation,
|
|
|
|
|
mac, mac_length);
|
2021-05-07 14:14:37 +02:00
|
|
|
|
|
2021-06-25 10:09:02 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
abort_status = psa_mac_abort(operation);
|
2018-07-08 20:35:02 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status == PSA_SUCCESS ? abort_status : status;
|
2018-02-08 10:02:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *mac,
|
|
|
|
|
size_t mac_size,
|
|
|
|
|
size_t *mac_length,
|
|
|
|
|
int is_sign)
|
2021-03-01 15:34:18 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-06-17 17:17:20 +02:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
uint8_t operation_mac_size = 0;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
2021-06-17 17:17:20 +02:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key,
|
|
|
|
|
&slot,
|
|
|
|
|
is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
|
|
|
|
|
alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-01 15:34:18 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
2021-06-17 17:17:20 +02:00
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
|
|
|
|
|
&operation_mac_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-01 15:34:18 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mac_size < operation_mac_size) {
|
2021-06-17 17:17:20 +02:00
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
goto exit;
|
2021-06-17 17:17:20 +02:00
|
|
|
|
}
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
2021-06-17 17:17:20 +02:00
|
|
|
|
status = psa_driver_wrapper_mac_compute(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data, slot->key.bytes,
|
|
|
|
|
alg,
|
|
|
|
|
input, input_length,
|
|
|
|
|
mac, operation_mac_size, mac_length);
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
2021-06-18 13:05:48 +02:00
|
|
|
|
/* In case of success, set the potential excess room in the output buffer
|
|
|
|
|
* to an invalid value, to avoid potentially leaking a longer MAC.
|
|
|
|
|
* In case of error, set the output length and content to a safe default,
|
|
|
|
|
* such that in case the caller misses an error check, the output would be
|
|
|
|
|
* an unachievable MAC.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-17 17:17:20 +02:00
|
|
|
|
*mac_length = mac_size;
|
2021-06-18 13:05:48 +02:00
|
|
|
|
operation_mac_size = 0;
|
2021-06-17 17:17:20 +02:00
|
|
|
|
}
|
2023-02-24 19:11:59 +01:00
|
|
|
|
|
|
|
|
|
psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2021-06-17 17:17:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
}
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
|
2021-03-01 15:34:18 +01:00
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t *mac,
|
|
|
|
|
size_t mac_size,
|
|
|
|
|
size_t *mac_length)
|
|
|
|
|
{
|
|
|
|
|
return psa_mac_compute_internal(key, alg,
|
|
|
|
|
input, input_length,
|
|
|
|
|
mac, mac_size, mac_length, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
const uint8_t *mac,
|
|
|
|
|
size_t mac_length)
|
2021-03-01 15:34:18 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-06-18 14:51:29 +02:00
|
|
|
|
uint8_t actual_mac[PSA_MAC_MAX_SIZE];
|
|
|
|
|
size_t actual_mac_length;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_compute_internal(key, alg,
|
|
|
|
|
input, input_length,
|
|
|
|
|
actual_mac, sizeof(actual_mac),
|
|
|
|
|
&actual_mac_length, 0);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-01 15:34:18 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mac_length != actual_mac_length) {
|
2021-06-18 14:51:29 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
goto exit;
|
2021-06-18 14:51:29 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
|
2021-06-18 14:51:29 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
goto exit;
|
2021-06-18 14:51:29 +02:00
|
|
|
|
}
|
2021-03-01 15:34:18 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-03-01 15:34:18 +01:00
|
|
|
|
}
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Asymmetric cryptography */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_sign_verify_check_alg(int input_is_message,
|
|
|
|
|
psa_algorithm_t alg)
|
2021-04-20 12:11:35 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (input_is_message) {
|
|
|
|
|
if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-04-20 12:11:35 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_SIGN_HASH(alg)) {
|
|
|
|
|
if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!PSA_ALG_IS_SIGN_HASH(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2021-05-05 13:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-05-05 13:56:27 +02:00
|
|
|
|
}
|
2021-04-20 12:11:35 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
|
|
|
|
|
int input_is_message,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *signature,
|
|
|
|
|
size_t signature_size,
|
|
|
|
|
size_t *signature_length)
|
2021-04-14 21:14:28 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
|
|
|
|
|
*signature_length = 0;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_sign_verify_check_alg(input_is_message, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-05 13:56:27 +02:00
|
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
|
|
|
|
/* Immediately reject a zero-length signature buffer. This guarantees
|
2021-05-05 13:54:55 +02:00
|
|
|
|
* that signature must be a valid pointer. (On the other hand, the input
|
2021-04-14 21:14:28 +02:00
|
|
|
|
* buffer can in principle be empty since it doesn't actually have
|
|
|
|
|
* to be a hash.) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (signature_size == 0) {
|
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2021-04-20 12:11:35 +02:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot,
|
|
|
|
|
input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
|
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH,
|
|
|
|
|
alg);
|
2021-04-20 12:11:35 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-04-14 21:14:28 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
|
2021-04-14 21:14:28 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-04-14 21:14:28 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (input_is_message) {
|
2021-04-22 11:32:19 +02:00
|
|
|
|
status = psa_driver_wrapper_sign_message(
|
2021-04-20 12:11:35 +02:00
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
2021-04-22 11:32:19 +02:00
|
|
|
|
alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_size, signature_length);
|
|
|
|
|
} else {
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2021-04-20 12:11:35 +02:00
|
|
|
|
status = psa_driver_wrapper_sign_hash(
|
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_size, signature_length);
|
2021-04-20 12:11:35 +02:00
|
|
|
|
}
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exit:
|
2023-02-21 16:07:40 +01:00
|
|
|
|
psa_wipe_tag_output_buffer(signature, status, signature_size,
|
|
|
|
|
*signature_length);
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2021-04-14 21:14:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
|
|
|
|
|
int input_is_message,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
const uint8_t *signature,
|
|
|
|
|
size_t signature_length)
|
2021-04-14 21:14:28 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_sign_verify_check_alg(input_is_message, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-05 13:56:27 +02:00
|
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-20 12:11:35 +02:00
|
|
|
|
|
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot,
|
|
|
|
|
input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
|
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH,
|
|
|
|
|
alg);
|
2021-04-20 12:11:35 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-04-14 21:14:28 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (input_is_message) {
|
2021-04-22 11:32:19 +02:00
|
|
|
|
status = psa_driver_wrapper_verify_message(
|
2021-04-20 12:11:35 +02:00
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
2021-04-22 11:32:19 +02:00
|
|
|
|
alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_length);
|
|
|
|
|
} else {
|
2021-04-20 12:11:35 +02:00
|
|
|
|
status = psa_driver_wrapper_verify_hash(
|
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_length);
|
2021-04-20 12:11:35 +02:00
|
|
|
|
}
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2021-04-14 21:14:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2021-04-20 12:11:35 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:18:36 +02:00
|
|
|
|
psa_status_t psa_sign_message_builtin(
|
2021-04-22 11:32:19 +02:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
|
size_t key_buffer_size,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *signature,
|
|
|
|
|
size_t signature_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t *signature_length)
|
2021-04-22 11:32:19 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_SIGN_HASH(alg)) {
|
2021-05-03 16:26:20 +02:00
|
|
|
|
size_t hash_length;
|
|
|
|
|
uint8_t hash[PSA_HASH_MAX_SIZE];
|
|
|
|
|
|
2021-04-29 16:48:44 +02:00
|
|
|
|
status = psa_driver_wrapper_hash_compute(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
PSA_ALG_SIGN_GET_HASH(alg),
|
|
|
|
|
input, input_length,
|
|
|
|
|
hash, sizeof(hash), &hash_length);
|
2021-04-22 11:32:19 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-04-29 16:48:44 +02:00
|
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-05-03 16:26:20 +02:00
|
|
|
|
|
|
|
|
|
return psa_driver_wrapper_sign_hash(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes, key_buffer, key_buffer_size,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_size, signature_length);
|
2021-04-29 16:48:44 +02:00
|
|
|
|
}
|
2021-04-22 11:32:19 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-04-22 11:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *signature,
|
|
|
|
|
size_t signature_size,
|
|
|
|
|
size_t *signature_length)
|
2021-04-20 12:11:35 +02:00
|
|
|
|
{
|
|
|
|
|
return psa_sign_internal(
|
2021-05-05 13:56:27 +02:00
|
|
|
|
key, 1, alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_size, signature_length);
|
2021-04-20 12:11:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:18:36 +02:00
|
|
|
|
psa_status_t psa_verify_message_builtin(
|
2021-04-22 11:32:19 +02:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
|
size_t key_buffer_size,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
const uint8_t *signature,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t signature_length)
|
2021-04-22 11:32:19 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_SIGN_HASH(alg)) {
|
2021-05-03 16:26:20 +02:00
|
|
|
|
size_t hash_length;
|
|
|
|
|
uint8_t hash[PSA_HASH_MAX_SIZE];
|
|
|
|
|
|
2021-04-29 16:48:44 +02:00
|
|
|
|
status = psa_driver_wrapper_hash_compute(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
PSA_ALG_SIGN_GET_HASH(alg),
|
|
|
|
|
input, input_length,
|
|
|
|
|
hash, sizeof(hash), &hash_length);
|
2021-04-22 11:32:19 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-04-29 16:48:44 +02:00
|
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-05-03 16:26:20 +02:00
|
|
|
|
|
|
|
|
|
return psa_driver_wrapper_verify_hash(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes, key_buffer, key_buffer_size,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_length);
|
2021-04-29 16:48:44 +02:00
|
|
|
|
}
|
2021-04-22 11:32:19 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-04-22 11:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
const uint8_t *signature,
|
|
|
|
|
size_t signature_length)
|
2021-04-20 12:11:35 +02:00
|
|
|
|
{
|
|
|
|
|
return psa_verify_internal(
|
2021-05-05 13:56:27 +02:00
|
|
|
|
key, 1, alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_length);
|
2021-04-14 21:14:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:18:36 +02:00
|
|
|
|
psa_status_t psa_sign_hash_builtin(
|
2020-12-08 16:32:23 +01:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
|
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t *signature, size_t signature_size, size_t *signature_length)
|
2018-02-03 22:44:14 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
|
|
|
|
|
if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
|
|
|
|
|
PSA_ALG_IS_RSA_PSS(alg)) {
|
2021-03-19 20:09:32 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
|
|
|
|
return mbedtls_psa_rsa_sign_hash(
|
|
|
|
|
attributes,
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_size, signature_length);
|
2020-11-10 17:50:04 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2021-03-19 20:09:32 +01:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
|
|
|
|
|
if (PSA_ALG_IS_ECDSA(alg)) {
|
2021-03-19 20:09:32 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
|
|
|
|
return mbedtls_psa_ecdsa_sign_hash(
|
|
|
|
|
attributes,
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_size, signature_length);
|
2021-03-19 20:09:32 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-06-26 16:10:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-28 13:56:01 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(void) key_buffer;
|
|
|
|
|
(void) key_buffer_size;
|
|
|
|
|
(void) hash;
|
|
|
|
|
(void) hash_length;
|
|
|
|
|
(void) signature;
|
|
|
|
|
(void) signature_size;
|
|
|
|
|
(void) signature_length;
|
2021-03-19 20:09:32 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2018-06-26 16:10:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
|
|
|
|
|
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 22:44:14 +01:00
|
|
|
|
{
|
2021-04-20 12:11:35 +02:00
|
|
|
|
return psa_sign_internal(
|
2021-05-05 13:56:27 +02:00
|
|
|
|
key, 0, alg, hash, hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_size, signature_length);
|
2018-05-08 10:18:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:18:36 +02:00
|
|
|
|
psa_status_t psa_verify_hash_builtin(
|
2020-12-08 16:32:23 +01:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
|
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const uint8_t *signature, size_t signature_length)
|
2018-05-08 10:18:38 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
|
|
|
|
|
if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
|
|
|
|
|
PSA_ALG_IS_RSA_PSS(alg)) {
|
2021-03-19 20:09:32 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
|
|
|
|
return mbedtls_psa_rsa_verify_hash(
|
|
|
|
|
attributes,
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_length);
|
2020-11-10 17:50:04 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2021-03-19 20:09:32 +01:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
|
|
|
|
|
if (PSA_ALG_IS_ECDSA(alg)) {
|
2021-03-19 20:09:32 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
|
|
|
|
return mbedtls_psa_ecdsa_verify_hash(
|
|
|
|
|
attributes,
|
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_length);
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2018-06-26 16:10:23 +02:00
|
|
|
|
}
|
2018-05-08 10:18:38 +02:00
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(void) key_buffer;
|
|
|
|
|
(void) key_buffer_size;
|
|
|
|
|
(void) hash;
|
|
|
|
|
(void) hash_length;
|
|
|
|
|
(void) signature;
|
|
|
|
|
(void) signature_length;
|
2021-03-19 20:09:32 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-12-08 16:32:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *hash,
|
|
|
|
|
size_t hash_length,
|
|
|
|
|
const uint8_t *signature,
|
|
|
|
|
size_t signature_length)
|
2020-12-08 16:32:23 +01:00
|
|
|
|
{
|
2021-04-20 12:11:35 +02:00
|
|
|
|
return psa_verify_internal(
|
2021-05-05 13:56:27 +02:00
|
|
|
|
key, 0, alg, hash, hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
signature, signature_length);
|
2018-02-03 22:44:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
|
|
|
|
|
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 22:16:26 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 17:12:32 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2018-07-06 17:01:38 +02:00
|
|
|
|
|
2018-07-24 16:34:10 +02:00
|
|
|
|
(void) input;
|
|
|
|
|
(void) input_length;
|
|
|
|
|
(void) salt;
|
|
|
|
|
(void) output;
|
|
|
|
|
(void) output_size;
|
|
|
|
|
|
2018-06-04 15:43:12 +02:00
|
|
|
|
*output_length = 0;
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-06-30 19:04:35 +02:00
|
|
|
|
|
2020-11-14 16:35:34 +01:00
|
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
|
|
|
|
|
PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
|
2020-10-22 15:24:49 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
2021-12-09 11:09:11 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-12-09 11:09:11 +01:00
|
|
|
|
};
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2021-12-09 11:09:11 +01:00
|
|
|
|
status = psa_driver_wrapper_asymmetric_encrypt(
|
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg, input, input_length, salt, salt_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
output, output_size, output_length);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-05-31 13:25:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
|
|
|
|
|
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 22:16:26 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 17:12:32 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2018-07-06 17:01:38 +02:00
|
|
|
|
|
2018-07-24 16:34:10 +02:00
|
|
|
|
(void) input;
|
|
|
|
|
(void) input_length;
|
|
|
|
|
(void) salt;
|
|
|
|
|
(void) output;
|
|
|
|
|
(void) output_size;
|
|
|
|
|
|
2018-06-04 15:43:12 +02:00
|
|
|
|
*output_length = 0;
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-06-30 19:04:35 +02:00
|
|
|
|
|
2020-11-14 16:35:34 +01:00
|
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
|
2020-10-22 15:24:49 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
2021-12-13 08:58:19 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-12-13 08:58:19 +01:00
|
|
|
|
};
|
2018-06-04 15:36:15 +02:00
|
|
|
|
|
2021-12-13 08:58:19 +01:00
|
|
|
|
status = psa_driver_wrapper_asymmetric_decrypt(
|
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg, input, input_length, salt, salt_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
output, output_size, output_length);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-05-31 13:25:48 +02:00
|
|
|
|
}
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
2022-11-30 20:16:02 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Asymmetric interruptible cryptography */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-02-21 15:19:23 +01:00
|
|
|
|
static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
|
|
|
|
|
|
2022-11-30 20:16:02 +01:00
|
|
|
|
void psa_interruptible_set_max_ops(uint32_t max_ops)
|
|
|
|
|
{
|
2023-02-21 15:19:23 +01:00
|
|
|
|
psa_interruptible_max_ops = max_ops;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t psa_interruptible_get_max_ops(void)
|
|
|
|
|
{
|
2023-02-21 15:19:23 +01:00
|
|
|
|
return psa_interruptible_max_ops;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t psa_sign_hash_get_num_ops(
|
|
|
|
|
const psa_sign_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
2022-12-15 18:00:30 +01:00
|
|
|
|
return operation->num_ops;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t psa_verify_hash_get_num_ops(
|
|
|
|
|
const psa_verify_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
2022-12-15 18:00:30 +01:00
|
|
|
|
return operation->num_ops;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
static psa_status_t psa_sign_hash_abort_internal(
|
|
|
|
|
psa_sign_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
|
|
|
|
if (operation->id == 0) {
|
|
|
|
|
/* The object has (apparently) been initialized but it is not (yet)
|
|
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
|
* nothing to do. */
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
|
|
status = psa_driver_wrapper_sign_hash_abort(operation);
|
|
|
|
|
|
|
|
|
|
operation->id = 0;
|
|
|
|
|
|
2023-02-07 13:51:21 +01:00
|
|
|
|
/* Do not clear either the error_occurred or num_ops elements here as they
|
|
|
|
|
* only want to be cleared by the application calling abort, not by abort
|
|
|
|
|
* being called at completion of an operation. */
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:16:02 +01:00
|
|
|
|
psa_status_t psa_sign_hash_start(
|
|
|
|
|
psa_sign_hash_interruptible_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key, psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *hash, size_t hash_length)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
/* Check that start has not been previously called, or operation has not
|
|
|
|
|
* previously errored. */
|
|
|
|
|
if (operation->id != 0 || operation->error_occurred) {
|
2022-11-30 20:16:02 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_sign_verify_check_alg(0, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
operation->error_occurred = 1;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot,
|
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH,
|
|
|
|
|
alg);
|
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
|
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-15 18:00:30 +01:00
|
|
|
|
/* Ensure ops count gets reset, in case of operation re-use. */
|
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2022-11-30 20:16:02 +01:00
|
|
|
|
status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes, alg,
|
|
|
|
|
hash, hash_length);
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
operation->error_occurred = 1;
|
2022-12-18 16:09:02 +01:00
|
|
|
|
psa_sign_hash_abort_internal(operation);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
if (unlock_status != PSA_SUCCESS) {
|
|
|
|
|
operation->error_occurred = 1;
|
|
|
|
|
}
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_sign_hash_complete(
|
|
|
|
|
psa_sign_hash_interruptible_operation_t *operation,
|
|
|
|
|
uint8_t *signature, size_t signature_size,
|
|
|
|
|
size_t *signature_length)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
|
|
*signature_length = 0;
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
/* Check that start has been called first, and that operation has not
|
|
|
|
|
* previously errored. */
|
|
|
|
|
if (operation->id == 0 || operation->error_occurred) {
|
2022-11-30 20:16:02 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 19:33:23 +01:00
|
|
|
|
/* Immediately reject a zero-length signature buffer. This guarantees that
|
|
|
|
|
* signature must be a valid pointer. */
|
2022-11-30 20:16:02 +01:00
|
|
|
|
if (signature_size == 0) {
|
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_driver_wrapper_sign_hash_complete(operation, signature,
|
|
|
|
|
signature_size,
|
|
|
|
|
signature_length);
|
|
|
|
|
|
2022-12-15 18:00:30 +01:00
|
|
|
|
/* Update ops count with work done. */
|
2023-02-07 13:43:11 +01:00
|
|
|
|
operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
|
2022-12-15 18:00:30 +01:00
|
|
|
|
|
2023-02-10 15:32:53 +01:00
|
|
|
|
exit:
|
|
|
|
|
|
2023-02-21 16:07:40 +01:00
|
|
|
|
psa_wipe_tag_output_buffer(signature, status, signature_size,
|
|
|
|
|
*signature_length);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
2023-02-10 15:22:22 +01:00
|
|
|
|
if (status != PSA_OPERATION_INCOMPLETE) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
operation->error_occurred = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
psa_sign_hash_abort_internal(operation);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_sign_hash_abort(
|
|
|
|
|
psa_sign_hash_interruptible_operation_t *operation)
|
2022-12-18 16:09:02 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
|
|
status = psa_sign_hash_abort_internal(operation);
|
|
|
|
|
|
|
|
|
|
/* We clear the number of ops done here, so that it is not cleared when
|
|
|
|
|
* the operation fails or succeeds, only on manual abort. */
|
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
/* Likewise, failure state. */
|
|
|
|
|
operation->error_occurred = 0;
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static psa_status_t psa_verify_hash_abort_internal(
|
|
|
|
|
psa_verify_hash_interruptible_operation_t *operation)
|
2022-11-30 20:16:02 +01:00
|
|
|
|
{
|
|
|
|
|
if (operation->id == 0) {
|
|
|
|
|
/* The object has (apparently) been initialized but it is not (yet)
|
|
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
|
* nothing to do. */
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
|
|
status = psa_driver_wrapper_verify_hash_abort(operation);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
|
|
|
|
operation->id = 0;
|
|
|
|
|
|
2023-02-07 13:51:21 +01:00
|
|
|
|
/* Do not clear either the error_occurred or num_ops elements here as they
|
|
|
|
|
* only want to be cleared by the application calling abort, not by abort
|
|
|
|
|
* being called at completion of an operation. */
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
return status;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_verify_hash_start(
|
|
|
|
|
psa_verify_hash_interruptible_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key, psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *hash, size_t hash_length,
|
|
|
|
|
const uint8_t *signature, size_t signature_length)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
/* Check that start has not been previously called, or operation has not
|
|
|
|
|
* previously errored. */
|
|
|
|
|
if (operation->id != 0 || operation->error_occurred) {
|
2022-11-30 20:16:02 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_sign_verify_check_alg(0, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
operation->error_occurred = 1;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot,
|
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH,
|
|
|
|
|
alg);
|
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
operation->error_occurred = 1;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-15 18:00:30 +01:00
|
|
|
|
/* Ensure ops count gets reset, in case of operation re-use. */
|
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2022-11-30 20:16:02 +01:00
|
|
|
|
status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg, hash, hash_length,
|
|
|
|
|
signature, signature_length);
|
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
operation->error_occurred = 1;
|
2022-12-18 16:09:02 +01:00
|
|
|
|
psa_verify_hash_abort_internal(operation);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
if (unlock_status != PSA_SUCCESS) {
|
|
|
|
|
operation->error_occurred = 1;
|
|
|
|
|
}
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_verify_hash_complete(
|
|
|
|
|
psa_verify_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
/* Check that start has been called first, and that operation has not
|
|
|
|
|
* previously errored. */
|
|
|
|
|
if (operation->id == 0 || operation->error_occurred) {
|
2022-11-30 20:16:02 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_driver_wrapper_verify_hash_complete(operation);
|
|
|
|
|
|
2022-12-15 18:00:30 +01:00
|
|
|
|
/* Update ops count with work done. */
|
2023-02-07 13:43:11 +01:00
|
|
|
|
operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
|
2022-12-15 18:00:30 +01:00
|
|
|
|
operation);
|
|
|
|
|
|
2023-02-10 15:32:53 +01:00
|
|
|
|
exit:
|
|
|
|
|
|
2022-11-30 20:16:02 +01:00
|
|
|
|
if (status != PSA_OPERATION_INCOMPLETE) {
|
2023-02-06 16:14:07 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
operation->error_occurred = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
psa_verify_hash_abort_internal(operation);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_verify_hash_abort(
|
|
|
|
|
psa_verify_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
2022-12-18 16:09:02 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
status = psa_verify_hash_abort_internal(operation);
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
/* We clear the number of ops done here, so that it is not cleared when
|
|
|
|
|
* the operation fails or succeeds, only on manual abort. */
|
|
|
|
|
operation->num_ops = 0;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
|
2023-02-06 16:14:07 +01:00
|
|
|
|
/* Likewise, failure state. */
|
|
|
|
|
operation->error_occurred = 0;
|
|
|
|
|
|
2022-12-18 16:09:02 +01:00
|
|
|
|
return status;
|
2022-11-30 20:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Asymmetric interruptible cryptography internal */
|
|
|
|
|
/* implementations */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
|
|
void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
|
|
|
|
|
{
|
2023-01-10 18:14:11 +01:00
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
|
|
|
|
/* Internal implementation uses zero to indicate infinite number max ops,
|
|
|
|
|
* therefore avoid this value, and set to minimum possible. */
|
|
|
|
|
if (max_ops == 0) {
|
|
|
|
|
max_ops = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mbedtls_ecp_set_max_ops(max_ops);
|
2023-02-21 15:19:23 +01:00
|
|
|
|
#else
|
|
|
|
|
(void) max_ops;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t mbedtls_psa_sign_hash_get_num_ops(
|
2023-02-19 19:43:45 +01:00
|
|
|
|
const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
|
2022-12-02 19:10:26 +01:00
|
|
|
|
{
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
2023-02-15 19:14:21 +01:00
|
|
|
|
return operation->num_ops;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#else
|
|
|
|
|
(void) operation;
|
|
|
|
|
return 0;
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t mbedtls_psa_verify_hash_get_num_ops(
|
2023-02-19 19:43:45 +01:00
|
|
|
|
const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
|
2022-12-02 19:10:26 +01:00
|
|
|
|
{
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
2023-02-15 19:14:21 +01:00
|
|
|
|
return operation->num_ops;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#else
|
|
|
|
|
(void) operation;
|
|
|
|
|
return 0;
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_sign_hash_start(
|
|
|
|
|
mbedtls_psa_sign_hash_interruptible_operation_t *operation,
|
|
|
|
|
const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
|
|
|
|
|
size_t key_buffer_size, psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *hash, size_t hash_length)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-02-09 15:30:24 +01:00
|
|
|
|
size_t required_hash_length;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!PSA_ALG_IS_ECDSA(alg)) {
|
2023-02-05 16:28:46 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2023-01-16 14:59:15 +01:00
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
2023-01-16 14:59:15 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-15 17:38:04 +01:00
|
|
|
|
mbedtls_ecdsa_restart_init(&operation->restart_ctx);
|
|
|
|
|
|
2023-02-15 19:14:21 +01:00
|
|
|
|
/* Ensure num_ops is zero'ed in case of context re-use. */
|
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
status = mbedtls_psa_ecp_load_representation(attributes->core.type,
|
|
|
|
|
attributes->core.bits,
|
|
|
|
|
key_buffer,
|
|
|
|
|
key_buffer_size,
|
|
|
|
|
&operation->ctx);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-05 14:41:57 +01:00
|
|
|
|
operation->coordinate_bytes = PSA_BITS_TO_BYTES(
|
2023-02-10 14:02:54 +01:00
|
|
|
|
operation->ctx->grp.nbits);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
|
|
|
|
|
operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
|
|
|
|
|
operation->alg = alg;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-09 15:30:24 +01:00
|
|
|
|
/* We only need to store the same length of hash as the private key size
|
|
|
|
|
* here, it would be truncated by the internal implementation anyway. */
|
|
|
|
|
required_hash_length = (hash_length < operation->coordinate_bytes ?
|
|
|
|
|
hash_length : operation->coordinate_bytes);
|
|
|
|
|
|
2023-02-15 19:23:53 +01:00
|
|
|
|
if (required_hash_length > sizeof(operation->hash)) {
|
|
|
|
|
/* Shouldn't happen, but better safe than sorry. */
|
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:30:24 +01:00
|
|
|
|
memcpy(operation->hash, hash, required_hash_length);
|
|
|
|
|
operation->hash_length = required_hash_length;
|
2023-01-16 14:59:15 +01:00
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
|
|
|
|
#else
|
2023-01-16 14:59:15 +01:00
|
|
|
|
(void) operation;
|
|
|
|
|
(void) key_buffer;
|
|
|
|
|
(void) key_buffer_size;
|
|
|
|
|
(void) alg;
|
|
|
|
|
(void) hash;
|
|
|
|
|
(void) hash_length;
|
|
|
|
|
(void) status;
|
2023-02-09 15:30:24 +01:00
|
|
|
|
(void) required_hash_length;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_sign_hash_complete(
|
|
|
|
|
mbedtls_psa_sign_hash_interruptible_operation_t *operation,
|
|
|
|
|
uint8_t *signature, size_t signature_size,
|
|
|
|
|
size_t *signature_length)
|
|
|
|
|
{
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
2023-02-15 17:38:04 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-02-07 12:21:10 +01:00
|
|
|
|
mbedtls_mpi r;
|
|
|
|
|
mbedtls_mpi s;
|
|
|
|
|
|
2023-02-03 15:59:11 +01:00
|
|
|
|
mbedtls_mpi_init(&r);
|
|
|
|
|
mbedtls_mpi_init(&s);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-21 15:19:23 +01:00
|
|
|
|
/* Ensure max_ops is set to the current value (or default). */
|
|
|
|
|
mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
|
|
|
|
|
|
2023-02-15 17:38:04 +01:00
|
|
|
|
if (signature_size < 2 * operation->coordinate_bytes) {
|
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
|
2023-02-03 15:59:11 +01:00
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
|
mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
|
2023-02-03 15:59:11 +01:00
|
|
|
|
&r,
|
|
|
|
|
&s,
|
2022-12-02 19:10:26 +01:00
|
|
|
|
&operation->ctx->d,
|
|
|
|
|
operation->hash,
|
|
|
|
|
operation->hash_length,
|
|
|
|
|
operation->md_alg,
|
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
|
&operation->restart_ctx));
|
|
|
|
|
#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2023-02-08 13:35:08 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
|
|
|
|
} else {
|
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
|
mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
|
2023-02-03 15:59:11 +01:00
|
|
|
|
&r,
|
|
|
|
|
&s,
|
2022-12-02 19:10:26 +01:00
|
|
|
|
&operation->ctx->d,
|
|
|
|
|
operation->hash,
|
|
|
|
|
operation->hash_length,
|
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
|
&operation->restart_ctx));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 19:43:45 +01:00
|
|
|
|
/* Hide the fact that the restart context only holds a delta of number of
|
|
|
|
|
* ops done during the last operation, not an absolute value. */
|
|
|
|
|
operation->num_ops += operation->restart_ctx.ecp.ops_done;
|
|
|
|
|
|
2023-02-08 13:35:08 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2022-12-02 19:10:26 +01:00
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-02-03 15:59:11 +01:00
|
|
|
|
mbedtls_mpi_write_binary(&r,
|
2022-12-02 19:10:26 +01:00
|
|
|
|
signature,
|
2023-02-05 14:41:57 +01:00
|
|
|
|
operation->coordinate_bytes)
|
|
|
|
|
);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 13:35:08 +01:00
|
|
|
|
goto exit;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-02-03 15:59:11 +01:00
|
|
|
|
mbedtls_mpi_write_binary(&s,
|
2022-12-02 19:10:26 +01:00
|
|
|
|
signature +
|
2023-02-05 14:41:57 +01:00
|
|
|
|
operation->coordinate_bytes,
|
|
|
|
|
operation->coordinate_bytes)
|
|
|
|
|
);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 13:35:08 +01:00
|
|
|
|
goto exit;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 14:41:57 +01:00
|
|
|
|
*signature_length = operation->coordinate_bytes * 2;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-08 13:35:08 +01:00
|
|
|
|
status = PSA_SUCCESS;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
}
|
2023-02-08 13:35:08 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
|
|
mbedtls_mpi_free(&r);
|
|
|
|
|
mbedtls_mpi_free(&s);
|
|
|
|
|
return status;
|
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
(void) operation;
|
|
|
|
|
(void) signature;
|
|
|
|
|
(void) signature_size;
|
|
|
|
|
(void) signature_length;
|
2018-06-18 21:49:39 +02:00
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_sign_hash_abort(
|
|
|
|
|
mbedtls_psa_sign_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
|
|
|
|
if (operation->ctx) {
|
|
|
|
|
mbedtls_ecdsa_free(operation->ctx);
|
|
|
|
|
mbedtls_free(operation->ctx);
|
2023-02-05 19:06:38 +01:00
|
|
|
|
operation->ctx = NULL;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mbedtls_ecdsa_restart_free(&operation->restart_ctx);
|
|
|
|
|
|
2023-02-15 19:14:21 +01:00
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
(void) operation;
|
|
|
|
|
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_verify_hash_start(
|
|
|
|
|
mbedtls_psa_verify_hash_interruptible_operation_t *operation,
|
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *hash, size_t hash_length,
|
|
|
|
|
const uint8_t *signature, size_t signature_length)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-02-05 14:41:57 +01:00
|
|
|
|
size_t coordinate_bytes = 0;
|
2023-02-09 15:30:24 +01:00
|
|
|
|
size_t required_hash_length = 0;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!PSA_ALG_IS_ECDSA(alg)) {
|
2023-02-05 16:28:46 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2023-01-16 14:59:15 +01:00
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
2023-01-16 14:59:15 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-15 17:38:04 +01:00
|
|
|
|
mbedtls_ecdsa_restart_init(&operation->restart_ctx);
|
|
|
|
|
mbedtls_mpi_init(&operation->r);
|
|
|
|
|
mbedtls_mpi_init(&operation->s);
|
|
|
|
|
|
2023-02-15 19:14:21 +01:00
|
|
|
|
/* Ensure num_ops is zero'ed in case of context re-use. */
|
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
status = mbedtls_psa_ecp_load_representation(attributes->core.type,
|
|
|
|
|
attributes->core.bits,
|
|
|
|
|
key_buffer,
|
|
|
|
|
key_buffer_size,
|
|
|
|
|
&operation->ctx);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-10 14:02:54 +01:00
|
|
|
|
coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-05 14:41:57 +01:00
|
|
|
|
if (signature_length != 2 * coordinate_bytes) {
|
2023-01-16 14:59:15 +01:00
|
|
|
|
return PSA_ERROR_INVALID_SIGNATURE;
|
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
|
mbedtls_mpi_read_binary(&operation->r,
|
|
|
|
|
signature,
|
2023-02-05 14:41:57 +01:00
|
|
|
|
coordinate_bytes));
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
status = mbedtls_to_psa_error(
|
|
|
|
|
mbedtls_mpi_read_binary(&operation->s,
|
|
|
|
|
signature +
|
2023-02-05 14:41:57 +01:00
|
|
|
|
coordinate_bytes,
|
|
|
|
|
coordinate_bytes));
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
|
status = mbedtls_psa_ecp_load_public_part(operation->ctx);
|
2023-02-06 16:59:09 +01:00
|
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
2023-01-16 14:59:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:30:24 +01:00
|
|
|
|
/* We only need to store the same length of hash as the private key size
|
|
|
|
|
* here, it would be truncated by the internal implementation anyway. */
|
|
|
|
|
required_hash_length = (hash_length < coordinate_bytes ? hash_length :
|
|
|
|
|
coordinate_bytes);
|
|
|
|
|
|
2023-02-15 19:23:53 +01:00
|
|
|
|
if (required_hash_length > sizeof(operation->hash)) {
|
|
|
|
|
/* Shouldn't happen, but better safe than sorry. */
|
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:30:24 +01:00
|
|
|
|
memcpy(operation->hash, hash, required_hash_length);
|
|
|
|
|
operation->hash_length = required_hash_length;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#else
|
2023-01-16 14:59:15 +01:00
|
|
|
|
(void) operation;
|
|
|
|
|
(void) key_buffer;
|
|
|
|
|
(void) key_buffer_size;
|
|
|
|
|
(void) alg;
|
|
|
|
|
(void) hash;
|
|
|
|
|
(void) hash_length;
|
|
|
|
|
(void) signature;
|
|
|
|
|
(void) signature_length;
|
|
|
|
|
(void) status;
|
2023-02-07 12:21:10 +01:00
|
|
|
|
(void) coordinate_bytes;
|
2023-02-09 15:30:24 +01:00
|
|
|
|
(void) required_hash_length;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
|
2023-01-16 14:59:15 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_verify_hash_complete(
|
|
|
|
|
mbedtls_psa_verify_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
2023-02-19 19:43:45 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-02-21 15:19:23 +01:00
|
|
|
|
/* Ensure max_ops is set to the current value (or default). */
|
|
|
|
|
mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
|
|
|
|
|
|
2023-02-19 19:43:45 +01:00
|
|
|
|
status = mbedtls_to_psa_error(
|
2022-12-02 19:10:26 +01:00
|
|
|
|
mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
|
|
|
|
|
operation->hash,
|
|
|
|
|
operation->hash_length,
|
|
|
|
|
&operation->ctx->Q,
|
|
|
|
|
&operation->r,
|
|
|
|
|
&operation->s,
|
|
|
|
|
&operation->restart_ctx));
|
|
|
|
|
|
2023-02-19 19:43:45 +01:00
|
|
|
|
/* Hide the fact that the restart context only holds a delta of number of
|
|
|
|
|
* ops done during the last operation, not an absolute value. */
|
|
|
|
|
operation->num_ops += operation->restart_ctx.ecp.ops_done;
|
|
|
|
|
|
|
|
|
|
return status;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
#else
|
|
|
|
|
(void) operation;
|
|
|
|
|
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_verify_hash_abort(
|
|
|
|
|
mbedtls_psa_verify_hash_interruptible_operation_t *operation)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
|
|
|
|
defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
|
|
|
|
|
|
if (operation->ctx) {
|
|
|
|
|
mbedtls_ecdsa_free(operation->ctx);
|
|
|
|
|
mbedtls_free(operation->ctx);
|
2023-02-05 19:06:38 +01:00
|
|
|
|
operation->ctx = NULL;
|
2022-12-02 19:10:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mbedtls_ecdsa_restart_free(&operation->restart_ctx);
|
|
|
|
|
|
2023-02-15 19:14:21 +01:00
|
|
|
|
operation->num_ops = 0;
|
|
|
|
|
|
2022-12-02 19:10:26 +01:00
|
|
|
|
mbedtls_mpi_free(&operation->r);
|
|
|
|
|
mbedtls_mpi_free(&operation->s);
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
(void) operation;
|
|
|
|
|
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
|
|
|
|
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
|
|
|
|
}
|
2018-06-18 21:49:39 +02:00
|
|
|
|
|
2018-03-12 14:59:30 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Symmetric cryptography */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
mbedtls_operation_t cipher_operation)
|
2020-10-01 16:38:28 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-06-24 12:49:45 +02:00
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
|
|
|
|
|
PSA_KEY_USAGE_ENCRYPT :
|
|
|
|
|
PSA_KEY_USAGE_DECRYPT);
|
2020-10-01 16:38:28 +02:00
|
|
|
|
|
|
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id != 0) {
|
2021-06-24 12:49:45 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2020-10-01 16:38:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_CIPHER(alg)) {
|
2021-06-24 12:49:45 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2020-10-01 16:38:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-10-01 16:38:28 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-10-01 16:38:28 +02:00
|
|
|
|
|
2021-03-10 08:34:23 +01:00
|
|
|
|
/* Initialize the operation struct members, except for id. The id member
|
2020-10-01 16:38:28 +02:00
|
|
|
|
* is used to indicate to psa_cipher_abort that there are resources to free,
|
2021-03-10 08:34:23 +01:00
|
|
|
|
* so we only set it (in the driver wrapper) after resources have been
|
|
|
|
|
* allocated/initialized. */
|
2020-10-01 16:38:28 +02:00
|
|
|
|
operation->iv_set = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg == PSA_ALG_ECB_NO_PADDING) {
|
2020-10-01 16:38:28 +02:00
|
|
|
|
operation->iv_required = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2020-10-01 16:38:28 +02:00
|
|
|
|
operation->iv_required = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
|
2020-10-01 16:38:28 +02:00
|
|
|
|
|
2020-12-14 14:36:06 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2020-12-14 14:36:06 +01:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-01 16:38:28 +02:00
|
|
|
|
/* Try doing the operation through a driver before using software fallback. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (cipher_operation == MBEDTLS_ENCRYPT) {
|
|
|
|
|
status = psa_driver_wrapper_cipher_encrypt_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg);
|
|
|
|
|
} else {
|
|
|
|
|
status = psa_driver_wrapper_cipher_decrypt_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg);
|
|
|
|
|
}
|
2020-10-01 16:38:28 +02:00
|
|
|
|
|
2019-02-25 17:43:14 +01:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_cipher_abort(operation);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-03-12 14:59:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2018-03-18 12:57:31 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
|
2018-03-18 12:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2018-03-18 12:57:31 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
|
2018-03-18 12:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
|
|
|
|
|
uint8_t *iv,
|
|
|
|
|
size_t iv_size,
|
|
|
|
|
size_t *iv_length)
|
2018-03-12 14:59:30 +01:00
|
|
|
|
{
|
2020-10-01 14:10:20 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-07-05 12:31:44 +02:00
|
|
|
|
uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
|
|
|
|
|
size_t default_iv_length;
|
2021-03-26 09:52:26 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2020-09-29 16:18:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->iv_set || !operation->iv_required) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2020-09-09 15:28:49 +02:00
|
|
|
|
}
|
2020-09-01 15:56:14 +02:00
|
|
|
|
|
2021-07-05 12:31:44 +02:00
|
|
|
|
default_iv_length = operation->default_iv_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (iv_size < default_iv_length) {
|
2021-03-26 09:52:26 +01:00
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
|
2021-07-05 12:31:44 +02:00
|
|
|
|
status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_generate_random(local_iv, default_iv_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-26 09:52:26 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2018-06-04 16:22:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_cipher_set_iv(operation,
|
|
|
|
|
local_iv, default_iv_length);
|
2021-03-26 09:52:26 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
memcpy(iv, local_iv, default_iv_length);
|
2021-07-05 12:31:44 +02:00
|
|
|
|
*iv_length = default_iv_length;
|
2020-09-09 15:28:49 +02:00
|
|
|
|
operation->iv_set = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2021-07-05 12:31:44 +02:00
|
|
|
|
*iv_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_cipher_abort(operation);
|
2021-07-05 12:31:44 +02:00
|
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-03-12 14:59:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
|
|
|
|
|
const uint8_t *iv,
|
|
|
|
|
size_t iv_length)
|
2018-03-12 14:59:30 +01:00
|
|
|
|
{
|
2020-10-01 14:10:20 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-09-29 16:18:05 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2020-09-29 16:18:05 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->iv_set || !operation->iv_required) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-03-26 10:15:08 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2020-09-01 15:56:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_cipher_set_iv(operation,
|
|
|
|
|
iv,
|
|
|
|
|
iv_length);
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
2021-06-23 12:38:39 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2018-08-02 12:56:32 +02:00
|
|
|
|
operation->iv_set = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_cipher_abort(operation);
|
|
|
|
|
}
|
|
|
|
|
return status;
|
2018-03-12 14:59:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_size,
|
|
|
|
|
size_t *output_length)
|
2018-03-12 14:59:30 +01:00
|
|
|
|
{
|
2020-08-25 15:13:13 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2020-09-09 15:28:49 +02:00
|
|
|
|
}
|
2021-06-23 12:38:39 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->iv_required && !operation->iv_set) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2020-09-09 15:28:49 +02:00
|
|
|
|
}
|
2019-02-15 15:12:05 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_cipher_update(operation,
|
|
|
|
|
input,
|
|
|
|
|
input_length,
|
|
|
|
|
output,
|
|
|
|
|
output_size,
|
|
|
|
|
output_length);
|
2021-06-23 12:38:39 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_cipher_abort(operation);
|
|
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-03-12 14:59:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01: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 14:59:30 +01:00
|
|
|
|
{
|
2019-02-14 12:48:10 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2020-09-09 15:28:49 +02:00
|
|
|
|
}
|
2021-06-23 12:38:39 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->iv_required && !operation->iv_set) {
|
2021-06-23 12:38:39 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2020-09-09 15:28:49 +02:00
|
|
|
|
}
|
2018-04-22 19:19:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_cipher_finish(operation,
|
|
|
|
|
output,
|
|
|
|
|
output_size,
|
|
|
|
|
output_length);
|
2021-06-23 12:38:39 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
return psa_cipher_abort(operation);
|
|
|
|
|
} else {
|
2020-09-11 11:44:50 +02:00
|
|
|
|
*output_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(void) psa_cipher_abort(operation);
|
2018-07-09 17:04:51 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2020-09-11 11:44:50 +02:00
|
|
|
|
}
|
2018-03-12 14:59:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
|
2018-06-04 16:22:46 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2020-09-10 14:54:14 +02:00
|
|
|
|
/* The object has (apparently) been initialized but it is not (yet)
|
2018-06-26 15:04:31 +02:00
|
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
|
* nothing to do. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-06-26 15:04:31 +02:00
|
|
|
|
}
|
2018-06-20 16:05:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_driver_wrapper_cipher_abort(operation);
|
2018-06-04 16:22:46 +02:00
|
|
|
|
|
2021-03-10 08:34:23 +01:00
|
|
|
|
operation->id = 0;
|
2018-04-30 11:31:04 +02:00
|
|
|
|
operation->iv_set = 0;
|
|
|
|
|
operation->iv_required = 0;
|
2018-04-04 14:43:05 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-03-12 14:59:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_size,
|
|
|
|
|
size_t *output_length)
|
2021-03-01 15:04:24 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-25 11:17:10 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-07-08 19:00:07 +02:00
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2021-07-09 18:44:58 +02:00
|
|
|
|
uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
|
|
|
|
|
size_t default_iv_length = 0;
|
2021-06-25 15:21:11 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_CIPHER(alg)) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot,
|
|
|
|
|
PSA_KEY_USAGE_ENCRYPT,
|
|
|
|
|
alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2021-03-25 11:17:10 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-03-25 11:17:10 +01:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
|
|
|
|
|
if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
|
2021-07-09 18:44:58 +02:00
|
|
|
|
status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (default_iv_length > 0) {
|
|
|
|
|
if (output_size < default_iv_length) {
|
2021-03-25 11:17:10 +01:00
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_generate_random(local_iv, default_iv_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-01 15:04:24 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 11:17:10 +01:00
|
|
|
|
status = psa_driver_wrapper_cipher_encrypt(
|
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
2021-07-09 18:44:58 +02:00
|
|
|
|
alg, local_iv, default_iv_length, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_buffer_offset(output, default_iv_length),
|
|
|
|
|
output_size - default_iv_length, output_length);
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
status = unlock_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
if (default_iv_length > 0) {
|
|
|
|
|
memcpy(output, local_iv, default_iv_length);
|
|
|
|
|
}
|
2021-07-09 18:44:58 +02:00
|
|
|
|
*output_length += default_iv_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
*output_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-07-08 19:00:07 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-03-01 15:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_size,
|
|
|
|
|
size_t *output_length)
|
2021-03-01 15:04:24 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-25 11:17:10 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-07-08 19:00:07 +02:00
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2021-06-25 15:21:11 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_CIPHER(alg)) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot,
|
|
|
|
|
PSA_KEY_USAGE_DECRYPT,
|
|
|
|
|
alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2021-03-25 11:17:10 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-03-25 11:17:10 +01:00
|
|
|
|
};
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
|
|
|
|
|
input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
|
2021-10-14 12:23:06 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
|
2021-06-25 15:25:38 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 11:17:10 +01:00
|
|
|
|
status = psa_driver_wrapper_cipher_decrypt(
|
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg, input, input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
output, output_size, output_length);
|
2021-03-01 15:04:24 +01:00
|
|
|
|
|
2021-06-25 15:25:38 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
status = unlock_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-07-08 19:00:07 +02:00
|
|
|
|
*output_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-07-08 19:00:07 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-03-01 15:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 23:53:03 +02:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* AEAD */
|
|
|
|
|
/****************************************************************/
|
2018-06-18 21:49:39 +02:00
|
|
|
|
|
2021-09-28 18:44:45 +02:00
|
|
|
|
/* Helper function to get the base algorithm from its variants. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
|
2021-09-28 18:44:45 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
|
2021-09-28 18:44:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Helper function to perform common nonce length checks. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
|
|
|
|
|
size_t nonce_length)
|
2021-09-28 12:14:27 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
|
2021-09-28 18:44:45 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (base_alg) {
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_GCM)
|
2021-10-13 11:12:08 +02:00
|
|
|
|
case PSA_ALG_GCM:
|
|
|
|
|
/* Not checking max nonce size here as GCM spec allows almost
|
2023-01-11 14:50:10 +01:00
|
|
|
|
* arbitrarily large nonces. Please note that we do not generally
|
|
|
|
|
* recommend the usage of nonces of greater length than
|
|
|
|
|
* PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
|
|
|
|
|
* size, which can then lead to collisions if you encrypt a very
|
|
|
|
|
* large number of messages.*/
|
|
|
|
|
if (nonce_length != 0) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-10-13 11:12:08 +02:00
|
|
|
|
break;
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_GCM */
|
|
|
|
|
#if defined(PSA_WANT_ALG_CCM)
|
2021-10-13 11:12:08 +02:00
|
|
|
|
case PSA_ALG_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (nonce_length >= 7 && nonce_length <= 13) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-10-13 11:12:08 +02:00
|
|
|
|
break;
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_CCM */
|
|
|
|
|
#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
|
2021-10-13 11:12:08 +02:00
|
|
|
|
case PSA_ALG_CHACHA20_POLY1305:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (nonce_length == 12) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
} else if (nonce_length == 8) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2021-11-15 20:04:15 +01:00
|
|
|
|
break;
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
|
2021-10-13 11:12:08 +02:00
|
|
|
|
default:
|
2022-09-27 13:55:37 +02:00
|
|
|
|
(void) nonce_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-10-13 11:12:08 +02:00
|
|
|
|
}
|
2021-09-28 12:14:27 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2021-09-28 12:14:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
|
2022-01-13 16:26:03 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-12-08 21:07:13 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-12-08 21:07:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
|
|
|
|
|
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)
|
2018-04-25 23:53:03 +02:00
|
|
|
|
{
|
2021-03-16 17:15:37 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
2018-06-18 15:41:12 +02:00
|
|
|
|
|
2018-06-03 14:05:47 +02:00
|
|
|
|
*ciphertext_length = 0;
|
2018-05-09 13:58:32 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_check_algorithm(alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2021-03-17 16:28:00 +01:00
|
|
|
|
|
2021-03-26 12:40:07 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-05-09 11:24:42 +02:00
|
|
|
|
|
2021-03-16 17:15:37 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-03-16 17:15:37 +01:00
|
|
|
|
};
|
2018-04-25 23:53:03 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_check_nonce_length(alg, nonce_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-09-28 12:14:27 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-09-28 12:14:27 +02:00
|
|
|
|
|
2021-03-17 16:08:20 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_encrypt(
|
2021-03-16 17:15:37 +01:00
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg,
|
|
|
|
|
nonce, nonce_length,
|
|
|
|
|
additional_data, additional_data_length,
|
|
|
|
|
plaintext, plaintext_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
ciphertext, ciphertext_size, ciphertext_length);
|
2018-06-18 15:41:12 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS && ciphertext_size != 0) {
|
|
|
|
|
memset(ciphertext, 0, ciphertext_size);
|
|
|
|
|
}
|
2018-06-18 15:41:12 +02:00
|
|
|
|
|
2021-09-28 12:14:27 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_unlock_key_slot(slot);
|
2018-04-25 23:53:03 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-06-01 19:23:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
|
|
|
|
|
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)
|
2018-04-25 23:53:03 +02:00
|
|
|
|
{
|
2021-03-16 17:15:37 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot;
|
2018-06-18 15:41:12 +02:00
|
|
|
|
|
2018-06-01 19:23:52 +02:00
|
|
|
|
*plaintext_length = 0;
|
2018-04-26 11:07:35 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_check_algorithm(alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2021-03-17 16:28:00 +01:00
|
|
|
|
|
2021-03-26 12:40:07 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-04-25 23:53:03 +02:00
|
|
|
|
|
2021-03-16 17:15:37 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2021-03-16 17:15:37 +01:00
|
|
|
|
};
|
2019-05-06 15:27:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_check_nonce_length(alg, nonce_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-09-28 12:14:27 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-09-28 12:14:27 +02:00
|
|
|
|
|
2021-03-17 16:08:20 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_decrypt(
|
2021-03-16 17:15:37 +01:00
|
|
|
|
&attributes, slot->key.data, slot->key.bytes,
|
|
|
|
|
alg,
|
|
|
|
|
nonce, nonce_length,
|
|
|
|
|
additional_data, additional_data_length,
|
|
|
|
|
ciphertext, ciphertext_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
plaintext, plaintext_size, plaintext_length);
|
2018-06-01 16:28:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS && plaintext_size != 0) {
|
|
|
|
|
memset(plaintext, 0, plaintext_size);
|
|
|
|
|
}
|
2018-06-03 16:20:42 +02:00
|
|
|
|
|
2021-09-28 12:14:27 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_unlock_key_slot(slot);
|
2021-03-16 17:15:37 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-04-25 23:53:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
|
|
|
|
|
{
|
|
|
|
|
const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
|
2021-12-19 17:00:12 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
|
2022-10-06 17:06:56 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_CCM)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
|
2021-12-19 17:00:12 +01:00
|
|
|
|
/* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-12-19 17:00:12 +01:00
|
|
|
|
break;
|
2022-10-06 17:06:56 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_CCM */
|
2021-12-19 17:00:12 +01:00
|
|
|
|
|
2022-10-06 17:06:56 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_GCM)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
|
2021-12-19 17:00:12 +01:00
|
|
|
|
/* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-12-19 17:00:12 +01:00
|
|
|
|
break;
|
2022-10-06 17:06:56 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_GCM */
|
2021-12-19 17:00:12 +01:00
|
|
|
|
|
2022-10-06 17:06:56 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
|
2021-12-19 17:00:12 +01:00
|
|
|
|
/* We only support the default tag length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (tag_len != 16) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-12-19 17:00:12 +01:00
|
|
|
|
break;
|
2022-10-06 17:06:56 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
|
2021-12-19 17:00:12 +01:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
(void) tag_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-12-19 17:00:12 +01:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-12-19 17:00:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 19:55:56 +02:00
|
|
|
|
/* Set the key for a multipart authenticated operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
|
|
|
|
|
int is_encrypt,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-08-19 19:55:56 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot = NULL;
|
|
|
|
|
psa_key_usage_t key_usage = 0;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_check_algorithm(alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-08-19 19:55:56 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id != 0) {
|
2021-08-19 19:55:56 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-05-20 15:29:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->nonce_set || operation->lengths_set ||
|
|
|
|
|
operation->ad_started || operation->body_started) {
|
2021-08-19 19:55:56 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-05-10 19:19:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (is_encrypt) {
|
2021-08-19 19:55:56 +02:00
|
|
|
|
key_usage = PSA_KEY_USAGE_ENCRYPT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2021-08-19 19:55:56 +02:00
|
|
|
|
key_usage = PSA_KEY_USAGE_DECRYPT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-24 19:17:52 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
|
|
|
|
|
alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = {
|
2021-08-19 19:55:56 +02:00
|
|
|
|
.core = slot->attr
|
2021-04-20 19:10:30 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
|
2022-10-08 17:54:30 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2022-10-08 17:54:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (is_encrypt) {
|
|
|
|
|
status = psa_driver_wrapper_aead_encrypt_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg);
|
|
|
|
|
} else {
|
|
|
|
|
status = psa_driver_wrapper_aead_decrypt_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes,
|
|
|
|
|
alg);
|
|
|
|
|
}
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-05-11 20:16:09 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
operation->key_type = psa_get_key_type(&attributes);
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2021-05-20 15:33:13 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-06-24 19:17:52 +02:00
|
|
|
|
status = unlock_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
operation->alg = psa_aead_get_base_algorithm(alg);
|
2021-08-23 19:59:49 +02:00
|
|
|
|
operation->is_encrypt = is_encrypt;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_aead_abort(operation);
|
2021-06-22 18:25:57 +02:00
|
|
|
|
}
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 19:55:56 +02:00
|
|
|
|
/* Set the key for a multipart authenticated encryption operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_aead_setup(operation, 1, key, alg);
|
2021-08-19 19:55:56 +02:00
|
|
|
|
}
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2021-08-19 19:55:56 +02:00
|
|
|
|
/* Set the key for a multipart authenticated decryption operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
|
psa_algorithm_t alg)
|
2021-08-19 19:55:56 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_aead_setup(operation, 0, key, alg);
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Generate a random nonce / IV for multipart AEAD operation */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
|
|
|
|
|
uint8_t *nonce,
|
|
|
|
|
size_t nonce_size,
|
|
|
|
|
size_t *nonce_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-11-26 18:53:58 +01:00
|
|
|
|
uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
|
2021-05-20 15:43:47 +02:00
|
|
|
|
size_t required_nonce_size;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2021-06-04 17:42:21 +02:00
|
|
|
|
*nonce_length = 0;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-05-20 15:29:20 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->nonce_set || !operation->is_encrypt) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:00:16 +02:00
|
|
|
|
/* For CCM, this size may not be correct according to the PSA
|
|
|
|
|
* specification. The PSA Crypto 1.0.1 specification states:
|
|
|
|
|
*
|
|
|
|
|
* CCM encodes the plaintext length pLen in L octets, with L the smallest
|
|
|
|
|
* integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
|
|
|
|
|
*
|
|
|
|
|
* However this restriction that L has to be the smallest integer is not
|
|
|
|
|
* applied in practice, and it is not implementable here since the
|
|
|
|
|
* plaintext length may or may not be known at this time. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
|
|
|
|
|
operation->alg);
|
|
|
|
|
if (nonce_size < required_nonce_size) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_generate_random(local_nonce, required_nonce_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2021-05-11 20:16:09 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
memcpy(nonce, local_nonce, required_nonce_size);
|
2021-04-20 19:10:30 +02:00
|
|
|
|
*nonce_length = required_nonce_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_aead_abort(operation);
|
2021-11-26 18:53:58 +01:00
|
|
|
|
}
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set the nonce for a multipart authenticated encryption or decryption
|
|
|
|
|
operation.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
|
|
|
|
|
const uint8_t *nonce,
|
|
|
|
|
size_t nonce_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-05-10 19:19:46 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-05-20 15:29:20 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->nonce_set) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_check_nonce_length(operation->alg, nonce_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-09-28 12:14:27 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2021-09-27 19:09:28 +02:00
|
|
|
|
}
|
2021-05-20 19:24:07 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
|
|
|
|
|
nonce_length);
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2021-05-11 20:16:09 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-05-10 19:19:46 +02:00
|
|
|
|
operation->nonce_set = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_aead_abort(operation);
|
|
|
|
|
}
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Declare the lengths of the message and additional data for multipart AEAD. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
|
|
|
|
|
size_t ad_length,
|
|
|
|
|
size_t plaintext_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-05-10 19:19:46 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-05-20 15:29:20 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->lengths_set || operation->ad_started ||
|
|
|
|
|
operation->body_started) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (operation->alg) {
|
2021-10-13 11:12:08 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_GCM)
|
|
|
|
|
case PSA_ALG_GCM:
|
|
|
|
|
/* Lengths can only be too large for GCM if size_t is bigger than 32
|
2023-01-11 14:50:10 +01:00
|
|
|
|
* bits. Without the guard this code will generate warnings on 32bit
|
|
|
|
|
* builds. */
|
2021-09-27 18:56:28 +02:00
|
|
|
|
#if SIZE_MAX > UINT32_MAX
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (((uint64_t) ad_length) >> 61 != 0 ||
|
|
|
|
|
((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
|
2021-10-13 11:12:08 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2021-09-27 18:56:28 +02:00
|
|
|
|
#endif
|
2021-10-13 11:12:08 +02:00
|
|
|
|
break;
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_GCM */
|
|
|
|
|
#if defined(PSA_WANT_ALG_CCM)
|
2021-10-13 11:12:08 +02:00
|
|
|
|
case PSA_ALG_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (ad_length > 0xFF00) {
|
2021-10-13 11:12:08 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_CCM */
|
|
|
|
|
#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
|
2021-10-13 11:12:08 +02:00
|
|
|
|
case PSA_ALG_CHACHA20_POLY1305:
|
|
|
|
|
/* No length restrictions for ChaChaPoly. */
|
|
|
|
|
break;
|
2021-09-29 15:10:20 +02:00
|
|
|
|
#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
|
2021-10-13 11:12:08 +02:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-09-27 18:56:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
|
|
|
|
|
plaintext_length);
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2021-05-11 20:16:09 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-05-20 18:25:06 +02:00
|
|
|
|
operation->ad_remaining = ad_length;
|
|
|
|
|
operation->body_remaining = plaintext_length;
|
2021-05-10 19:19:46 +02:00
|
|
|
|
operation->lengths_set = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_aead_abort(operation);
|
2021-05-20 18:25:06 +02:00
|
|
|
|
}
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
2021-09-01 11:33:14 +02:00
|
|
|
|
|
|
|
|
|
/* Pass additional data to an active multipart AEAD operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-05-10 19:19:46 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-05-20 15:29:20 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!operation->nonce_set || operation->body_started) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->lengths_set) {
|
|
|
|
|
if (operation->ad_remaining < input_length) {
|
2021-05-20 18:25:06 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operation->ad_remaining -= input_length;
|
|
|
|
|
}
|
2021-10-01 14:00:16 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_CCM)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
else if (operation->alg == PSA_ALG_CCM) {
|
2021-10-01 14:00:16 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
#endif /* PSA_WANT_ALG_CCM */
|
2021-05-20 18:25:06 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_update_ad(operation, input,
|
|
|
|
|
input_length);
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2021-05-11 20:16:09 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-05-10 19:19:46 +02:00
|
|
|
|
operation->ad_started = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_aead_abort(operation);
|
|
|
|
|
}
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Encrypt or decrypt a message fragment in an active multipart AEAD
|
|
|
|
|
operation.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_update(psa_aead_operation_t *operation,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_size,
|
|
|
|
|
size_t *output_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-05-10 19:19:46 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
|
|
|
|
*output_length = 0;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-05-20 15:29:20 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!operation->nonce_set) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->lengths_set) {
|
2021-05-20 18:25:06 +02:00
|
|
|
|
/* Additional data length was supplied, but not all the additional
|
|
|
|
|
data was supplied.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->ad_remaining != 0) {
|
2021-05-20 18:25:06 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Too much data provided. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->body_remaining < input_length) {
|
2021-05-20 18:25:06 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operation->body_remaining -= input_length;
|
|
|
|
|
}
|
2021-10-01 14:00:16 +02:00
|
|
|
|
#if defined(PSA_WANT_ALG_CCM)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
else if (operation->alg == PSA_ALG_CCM) {
|
2021-10-01 14:00:16 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
#endif /* PSA_WANT_ALG_CCM */
|
2021-05-20 18:25:06 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_update(operation, input, input_length,
|
|
|
|
|
output, output_size,
|
|
|
|
|
output_length);
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2021-05-11 20:16:09 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-05-10 19:19:46 +02:00
|
|
|
|
operation->body_started = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
psa_aead_abort(operation);
|
|
|
|
|
}
|
2021-05-10 19:19:46 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
|
2021-06-23 09:50:14 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0 || !operation->nonce_set) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2021-06-23 09:50:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->lengths_set && (operation->ad_remaining != 0 ||
|
|
|
|
|
operation->body_remaining != 0)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-06-23 09:50:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-06-23 09:50:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 19:10:30 +02:00
|
|
|
|
/* Finish encrypting a message in a multipart AEAD operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
|
|
|
|
|
uint8_t *ciphertext,
|
|
|
|
|
size_t ciphertext_size,
|
|
|
|
|
size_t *ciphertext_length,
|
|
|
|
|
uint8_t *tag,
|
|
|
|
|
size_t tag_size,
|
|
|
|
|
size_t *tag_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-05-11 20:16:09 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2021-04-20 19:10:30 +02:00
|
|
|
|
*ciphertext_length = 0;
|
2021-06-22 18:53:45 +02:00
|
|
|
|
*tag_length = tag_size;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_final_checks(operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!operation->is_encrypt) {
|
2021-06-23 09:50:14 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2021-05-20 18:25:06 +02:00
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_finish(operation, ciphertext,
|
|
|
|
|
ciphertext_size,
|
|
|
|
|
ciphertext_length,
|
|
|
|
|
tag, tag_size, tag_length);
|
2021-05-11 20:16:09 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-02-24 19:11:59 +01:00
|
|
|
|
|
|
|
|
|
|
2021-05-21 19:02:33 +02:00
|
|
|
|
/* In case the operation fails and the user fails to check for failure or
|
2021-09-19 19:34:50 +02:00
|
|
|
|
* the zero tag size, make sure the tag is set to something implausible.
|
|
|
|
|
* Even if the operation succeeds, make sure we clear the rest of the
|
|
|
|
|
* buffer to prevent potential leakage of anything previously placed in
|
|
|
|
|
* the same buffer.*/
|
2023-02-24 19:11:59 +01:00
|
|
|
|
psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
|
2021-05-21 19:02:33 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_aead_abort(operation);
|
2021-05-11 20:16:09 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finish authenticating and decrypting a message in a multipart AEAD
|
|
|
|
|
operation.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
|
|
|
|
|
uint8_t *plaintext,
|
|
|
|
|
size_t plaintext_size,
|
|
|
|
|
size_t *plaintext_length,
|
|
|
|
|
const uint8_t *tag,
|
|
|
|
|
size_t tag_length)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
2021-05-11 20:16:09 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2021-04-20 19:10:30 +02:00
|
|
|
|
*plaintext_length = 0;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_aead_final_checks(operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-05-20 15:29:20 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-05-20 15:29:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->is_encrypt) {
|
2021-05-11 20:16:09 +02:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_verify(operation, plaintext,
|
|
|
|
|
plaintext_size,
|
|
|
|
|
plaintext_length,
|
|
|
|
|
tag, tag_length);
|
2021-05-11 20:16:09 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_aead_abort(operation);
|
2021-05-11 20:16:09 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Abort an AEAD operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
|
2021-04-20 19:10:30 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->id == 0) {
|
2021-04-20 19:10:30 +02:00
|
|
|
|
/* The object has (apparently) been initialized but it is not (yet)
|
|
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
|
* nothing to do. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_aead_abort(operation);
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(operation, 0, sizeof(*operation));
|
2021-04-20 19:10:30 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2021-04-20 19:10:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 22:00:52 +02:00
|
|
|
|
/****************************************************************/
|
2018-07-12 17:12:33 +02:00
|
|
|
|
/* Generators */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF) || \
|
2020-11-17 07:08:34 +01:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
2022-07-29 16:00:16 +02:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#define AT_LEAST_ONE_BUILTIN_KDF
|
2021-05-06 17:58:36 +02:00
|
|
|
|
#endif /* At least one builtin KDF */
|
|
|
|
|
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF) || \
|
2021-05-06 17:58:36 +02:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
|
|
|
|
static psa_status_t psa_key_derivation_start_hmac(
|
|
|
|
|
psa_mac_operation_t *operation,
|
|
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
|
const uint8_t *hmac_key,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t hmac_key_length)
|
2021-05-06 17:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
|
|
|
|
|
psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
|
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
2021-05-06 17:58:36 +02:00
|
|
|
|
|
2021-05-07 14:14:37 +02:00
|
|
|
|
operation->is_sign = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
operation->mac_size = PSA_HASH_LENGTH(hash_alg);
|
2021-05-07 14:14:37 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_mac_sign_setup(operation,
|
|
|
|
|
&attributes,
|
|
|
|
|
hmac_key, hmac_key_length,
|
|
|
|
|
PSA_ALG_HMAC(hash_alg));
|
2021-05-06 17:58:36 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
|
return status;
|
2021-05-06 17:58:36 +02:00
|
|
|
|
}
|
|
|
|
|
#endif /* KDF algorithms reliant on HMAC */
|
2020-11-17 07:08:34 +01:00
|
|
|
|
|
2019-01-07 22:59:38 +01: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 16:59:18 +02:00
|
|
|
|
static psa_algorithm_t psa_key_derivation_get_kdf_alg(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const psa_key_derivation_operation_t *operation)
|
2019-01-16 15:53:06 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
|
|
|
|
|
return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
|
|
|
|
|
} else {
|
|
|
|
|
return operation->alg;
|
|
|
|
|
}
|
2019-01-16 15:53:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
|
|
|
|
|
if (kdf_alg == 0) {
|
2018-07-12 17:12:33 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
|
|
|
|
|
mbedtls_free(operation->ctx.hkdf.info);
|
|
|
|
|
status = psa_mac_abort(&operation->ctx.hkdf.hmac);
|
|
|
|
|
} else
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#endif /* BUILTIN_ALG_ANY_HKDF */
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
|
|
|
|
|
/* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
|
|
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
|
|
|
|
|
if (operation->ctx.tls12_prf.secret != NULL) {
|
|
|
|
|
mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
|
|
|
|
|
operation->ctx.tls12_prf.secret_length);
|
|
|
|
|
mbedtls_free(operation->ctx.tls12_prf.secret);
|
2021-04-29 19:32:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->ctx.tls12_prf.seed != NULL) {
|
|
|
|
|
mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
|
|
|
|
|
operation->ctx.tls12_prf.seed_length);
|
|
|
|
|
mbedtls_free(operation->ctx.tls12_prf.seed);
|
2018-10-09 18:33:01 +02:00
|
|
|
|
}
|
2018-11-13 21:50:45 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->ctx.tls12_prf.label != NULL) {
|
|
|
|
|
mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
|
|
|
|
|
operation->ctx.tls12_prf.label_length);
|
|
|
|
|
mbedtls_free(operation->ctx.tls12_prf.label);
|
2018-11-13 21:50:45 +01:00
|
|
|
|
}
|
2022-11-22 14:05:12 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->ctx.tls12_prf.other_secret != NULL) {
|
|
|
|
|
mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
|
|
|
|
|
operation->ctx.tls12_prf.other_secret_length);
|
|
|
|
|
mbedtls_free(operation->ctx.tls12_prf.other_secret);
|
2022-04-07 14:29:56 +02:00
|
|
|
|
}
|
2022-11-22 14:05:12 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2021-04-29 19:32:25 +02:00
|
|
|
|
status = PSA_SUCCESS;
|
2019-06-11 11:37:28 +02:00
|
|
|
|
|
|
|
|
|
/* We leave the fields Ai and output_block to be erased safely by the
|
|
|
|
|
* mbedtls_platform_zeroize() in the end of this function. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
|
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
|
|
|
|
|
mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
|
|
|
|
|
sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
|
|
|
|
|
} else
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(operation, sizeof(*operation));
|
|
|
|
|
return status;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
|
psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t *capacity)
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->alg == 0) {
|
2019-05-16 17:31:03 +02:00
|
|
|
|
/* This is a blank key derivation operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2019-02-15 14:05:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
|
*capacity = operation->capacity;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
|
|
|
|
|
size_t capacity)
|
2019-01-07 22:59:38 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->alg == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
if (capacity > operation->capacity) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-05-16 17:31:03 +02:00
|
|
|
|
operation->capacity = capacity;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF)
|
2022-05-19 11:45:20 +02:00
|
|
|
|
/* Read some bytes from an HKDF-based operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
|
|
|
|
|
psa_algorithm_t kdf_alg,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_length)
|
2018-07-12 17:22:21 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
|
|
|
|
|
uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
|
2021-04-29 19:11:25 +02:00
|
|
|
|
size_t hmac_output_length;
|
2018-07-12 17:22:21 +02:00
|
|
|
|
psa_status_t status;
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#else
|
|
|
|
|
const uint8_t last_block = 0xff;
|
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
|
2018-07-12 17:22:21 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->state < HKDF_STATE_KEYED ||
|
|
|
|
|
(!hkdf->info_set
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
&& !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
)) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2019-01-07 22:59:38 +01:00
|
|
|
|
hkdf->state = HKDF_STATE_OUTPUT;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
while (output_length != 0) {
|
2018-07-12 17:22:21 +02:00
|
|
|
|
/* Copy what remains of the current block */
|
|
|
|
|
uint8_t n = hash_length - hkdf->offset_in_block;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (n > output_length) {
|
2018-07-12 17:22:21 +02:00
|
|
|
|
n = (uint8_t) output_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
|
2018-07-12 17:22:21 +02:00
|
|
|
|
output += n;
|
|
|
|
|
output_length -= n;
|
|
|
|
|
hkdf->offset_in_block += n;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (output_length == 0) {
|
2018-07-12 17:22:21 +02:00
|
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2022-05-19 11:45:20 +02:00
|
|
|
|
/* We can't be wanting more output after the last block, 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 15:28:51 +02:00
|
|
|
|
* the capacity check in psa_key_derivation_output_bytes() would have
|
2019-05-16 17:31:03 +02:00
|
|
|
|
* prevented this call. It could happen only if the operation
|
2018-07-17 21:06:59 +02:00
|
|
|
|
* object was corrupted or if this function is called directly
|
|
|
|
|
* inside the library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->block_number == last_block) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2018-07-12 17:22:21 +02:00
|
|
|
|
|
|
|
|
|
/* We need a new block */
|
|
|
|
|
++hkdf->block_number;
|
|
|
|
|
hkdf->offset_in_block = 0;
|
2021-04-29 19:11:25 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_start_hmac(&hkdf->hmac,
|
|
|
|
|
hash_alg,
|
|
|
|
|
hkdf->prk,
|
|
|
|
|
hash_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2021-04-29 19:11:25 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->block_number != 1) {
|
|
|
|
|
status = psa_mac_update(&hkdf->hmac,
|
|
|
|
|
hkdf->output_block,
|
|
|
|
|
hash_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hkdf->hmac,
|
|
|
|
|
hkdf->info,
|
|
|
|
|
hkdf->info_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hkdf->hmac,
|
|
|
|
|
&hkdf->block_number, 1);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
status = psa_mac_sign_finish(&hkdf->hmac,
|
2021-04-29 19:11:25 +02:00
|
|
|
|
hkdf->output_block,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
sizeof(hkdf->output_block),
|
|
|
|
|
&hmac_output_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
2018-07-12 17:22:21 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
2018-07-12 17:22:21 +02:00
|
|
|
|
}
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#endif /* BUILTIN_ALG_ANY_HKDF */
|
2018-10-09 18:33:01 +02:00
|
|
|
|
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2019-06-17 13:58:10 +02:00
|
|
|
|
static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
|
|
|
|
|
psa_tls12_prf_key_derivation_t *tls12_prf,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t alg)
|
2019-06-17 13:58:10 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
|
|
|
|
|
uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
|
2021-04-29 19:32:25 +02:00
|
|
|
|
psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
|
|
|
|
|
size_t hmac_output_length;
|
2019-06-19 13:21:20 +02:00
|
|
|
|
psa_status_t status, cleanup_status;
|
2018-10-09 18:33:01 +02:00
|
|
|
|
|
2019-06-17 13:58:10 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (tls12_prf->block_number == 0xff) {
|
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
}
|
2019-06-17 13:58:10 +02:00
|
|
|
|
|
|
|
|
|
/* We need a new block */
|
|
|
|
|
++tls12_prf->block_number;
|
2019-06-19 13:10:49 +02:00
|
|
|
|
tls12_prf->left_in_block = hash_length;
|
2019-06-17 13:58:10 +02: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 13:21:20 +02:00
|
|
|
|
* A(i) = HMAC_hash(secret, A(i-1))
|
2019-06-17 13:58:10 +02:00
|
|
|
|
*
|
2019-06-19 13:21:20 +02:00
|
|
|
|
* The `psa_tls12_prf_key_derivation` structure saves the block
|
2019-06-17 13:58:10 +02:00
|
|
|
|
* `HMAC_hash(secret, A(i) + seed)` from which the output
|
2019-06-26 13:50:36 +02:00
|
|
|
|
* is currently extracted as `output_block` and where i is
|
|
|
|
|
* `block_number`.
|
2019-06-17 13:58:10 +02:00
|
|
|
|
*/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_start_hmac(&hmac,
|
|
|
|
|
hash_alg,
|
|
|
|
|
tls12_prf->secret,
|
|
|
|
|
tls12_prf->secret_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-19 13:21:20 +02:00
|
|
|
|
|
|
|
|
|
/* Calculate A(i) where i = tls12_prf->block_number. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (tls12_prf->block_number == 1) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
/* 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.) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_update(&hmac,
|
|
|
|
|
tls12_prf->label,
|
|
|
|
|
tls12_prf->label_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hmac,
|
|
|
|
|
tls12_prf->seed,
|
|
|
|
|
tls12_prf->seed_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
/* A(i) = HMAC_hash(secret, A(i-1)) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-19 13:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_mac_sign_finish(&hmac,
|
|
|
|
|
tls12_prf->Ai, hash_length,
|
|
|
|
|
&hmac_output_length);
|
|
|
|
|
if (hmac_output_length != hash_length) {
|
2021-04-29 19:32:25 +02:00
|
|
|
|
status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-19 13:21:20 +02:00
|
|
|
|
|
|
|
|
|
/* Calculate HMAC_hash(secret, A(i) + label + seed). */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_start_hmac(&hmac,
|
|
|
|
|
hash_alg,
|
|
|
|
|
tls12_prf->secret,
|
|
|
|
|
tls12_prf->secret_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_mac_sign_finish(&hmac,
|
|
|
|
|
tls12_prf->output_block, hash_length,
|
|
|
|
|
&hmac_output_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-19 13:21:20 +02:00
|
|
|
|
|
2019-06-17 13:58:10 +02:00
|
|
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
cleanup_status = psa_mac_abort(&hmac);
|
|
|
|
|
if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
|
2019-06-19 13:21:20 +02:00
|
|
|
|
status = cleanup_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-19 13:21:20 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2019-06-17 13:58:10 +02:00
|
|
|
|
}
|
2018-10-09 18:33:01 +02:00
|
|
|
|
|
2019-06-19 13:10:49 +02: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,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t output_length)
|
2019-06-19 13:10:49 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
|
|
|
|
|
uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
|
2019-06-19 13:10:49 +02:00
|
|
|
|
psa_status_t status;
|
|
|
|
|
uint8_t offset, length;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (tls12_prf->state) {
|
2021-06-11 22:41:46 +02:00
|
|
|
|
case PSA_TLS12_PRF_STATE_LABEL_SET:
|
|
|
|
|
tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
|
|
|
|
|
break;
|
|
|
|
|
case PSA_TLS12_PRF_STATE_OUTPUT:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-06-11 22:41:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
while (output_length != 0) {
|
2019-06-19 13:10:49 +02:00
|
|
|
|
/* Check if we have fully processed the current block. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-06-19 13:10:49 +02:00
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (tls12_prf->left_in_block > output_length) {
|
2019-06-19 13:10:49 +02:00
|
|
|
|
length = (uint8_t) output_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2019-06-19 13:10:49 +02:00
|
|
|
|
length = tls12_prf->left_in_block;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-06-19 13:10:49 +02:00
|
|
|
|
|
|
|
|
|
offset = hash_length - tls12_prf->left_in_block;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(output, tls12_prf->output_block + offset, length);
|
2019-06-19 13:10:49 +02:00
|
|
|
|
output += length;
|
|
|
|
|
output_length -= length;
|
|
|
|
|
tls12_prf->left_in_block -= length;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-06-19 13:10:49 +02:00
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
|
|
|
|
|
* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2018-07-12 17:22:21 +02:00
|
|
|
|
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
|
|
|
|
static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
|
|
|
|
|
psa_tls12_ecjpake_to_pms_t *ecjpake,
|
|
|
|
|
uint8_t *output,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t output_length)
|
2022-07-29 16:00:16 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-09-26 16:49:16 +02:00
|
|
|
|
size_t output_size = 0;
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (output_length != 32) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
|
|
|
|
|
PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
|
|
|
|
|
&output_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (output_size != output_length) {
|
|
|
|
|
return PSA_ERROR_GENERIC_ERROR;
|
|
|
|
|
}
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2022-07-29 16:00:16 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-17 13:38:20 +02:00
|
|
|
|
psa_status_t psa_key_derivation_output_bytes(
|
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
|
uint8_t *output,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t output_length)
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->alg == 0) {
|
2019-05-16 17:31:03 +02:00
|
|
|
|
/* This is a blank operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2019-02-15 14:05:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (output_length > operation->capacity) {
|
2019-05-16 17:31:03 +02:00
|
|
|
|
operation->capacity = 0;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
/* Go through the error path to wipe all confidential data now
|
2019-05-16 17:31:03 +02:00
|
|
|
|
* that the operation object is useless. */
|
2019-02-14 12:48:10 +01:00
|
|
|
|
status = PSA_ERROR_INSUFFICIENT_DATA;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (output_length == 0 && operation->capacity == 0) {
|
2019-05-16 17:31:03 +02:00
|
|
|
|
/* Edge case: this is a finished operation, and 0 bytes
|
2019-02-15 14:05:49 +01:00
|
|
|
|
* were requested. The right error in this case could
|
2018-07-12 17:12:33 +02:00
|
|
|
|
* be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
|
|
|
|
|
* INSUFFICIENT_CAPACITY, which is right for a finished
|
2019-05-16 17:31:03 +02:00
|
|
|
|
* operation, for consistency with the case when
|
2018-07-12 17:12:33 +02:00
|
|
|
|
* output_length > 0. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INSUFFICIENT_DATA;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
2019-05-16 17:31:03 +02:00
|
|
|
|
operation->capacity -= output_length;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
|
|
|
|
|
status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
|
|
|
|
|
output, output_length);
|
|
|
|
|
} else
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#endif /* BUILTIN_ALG_ANY_HKDF */
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
|
|
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
|
|
|
|
|
status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
|
|
|
|
|
kdf_alg, output,
|
|
|
|
|
output_length);
|
|
|
|
|
} else
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
|
|
|
|
|
* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
|
2022-07-29 16:00:16 +02:00
|
|
|
|
status = psa_key_derivation_tls12_ecjpake_to_pms_read(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
&operation->ctx.tls12_ecjpake_to_pms, output, output_length);
|
|
|
|
|
} else
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
|
|
|
|
|
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
2021-02-10 17:19:22 +01:00
|
|
|
|
(void) kdf_alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-02-15 14:05:49 +01:00
|
|
|
|
/* Preserve the algorithm upon errors, but clear all sensitive state.
|
2019-05-16 17:31:03 +02: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;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_derivation_abort(operation);
|
2019-05-16 17:31:03 +02:00
|
|
|
|
operation->alg = alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(output, '!', output_length);
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 00:28:23 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
|
2018-07-19 17:05:42 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
2018-07-19 17:05:42 +02:00
|
|
|
|
}
|
2021-02-10 00:28:23 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
|
2018-07-19 17:05:42 +02:00
|
|
|
|
|
2021-11-15 12:38:53 +01:00
|
|
|
|
/*
|
2023-01-11 14:50:10 +01:00
|
|
|
|
* ECC keys on a Weierstrass elliptic curve require the generation
|
|
|
|
|
* of a private key which is an integer
|
|
|
|
|
* in the range [1, N - 1], where N is the boundary of the private key domain:
|
|
|
|
|
* N is the prime p for Diffie-Hellman, or the order of the
|
|
|
|
|
* curve’s base point for ECC.
|
|
|
|
|
*
|
|
|
|
|
* Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
|
|
|
|
|
* This function generates the private key using the following process:
|
|
|
|
|
*
|
|
|
|
|
* 1. Draw a byte string of length ceiling(m/8) bytes.
|
|
|
|
|
* 2. If m is not a multiple of 8, set the most significant
|
|
|
|
|
* (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
|
|
|
|
|
* 3. Convert the string to integer k by decoding it as a big-endian byte string.
|
|
|
|
|
* 4. If k > N - 2, discard the result and return to step 1.
|
|
|
|
|
* 5. Output k + 1 as the private key.
|
|
|
|
|
*
|
|
|
|
|
* This method allows compliance to NIST standards, specifically the methods titled
|
|
|
|
|
* Key-Pair Generation by Testing Candidates in the following publications:
|
|
|
|
|
* - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
|
|
|
|
|
* Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
|
|
|
|
|
* Diffie-Hellman keys.
|
|
|
|
|
*
|
|
|
|
|
* - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
|
|
|
|
|
* Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
|
|
|
|
|
*
|
|
|
|
|
* Note: Function allocates memory for *data buffer, so given *data should be
|
|
|
|
|
* always NULL.
|
|
|
|
|
*/
|
2022-03-04 14:41:11 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2021-11-25 13:53:28 +01:00
|
|
|
|
static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
|
2019-04-18 12:53:30 +02:00
|
|
|
|
psa_key_slot_t *slot,
|
|
|
|
|
size_t bits,
|
2021-11-15 12:38:53 +01:00
|
|
|
|
psa_key_derivation_operation_t *operation,
|
2022-01-20 22:41:17 +01:00
|
|
|
|
uint8_t **data
|
|
|
|
|
)
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
2022-01-20 22:41:17 +01:00
|
|
|
|
unsigned key_out_of_range = 1;
|
2021-11-15 12:38:53 +01:00
|
|
|
|
mbedtls_mpi k;
|
|
|
|
|
mbedtls_mpi diff_N_2;
|
2022-02-21 08:17:43 +01:00
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-03-01 15:13:30 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_mpi_init(&k);
|
|
|
|
|
mbedtls_mpi_init(&diff_N_2);
|
2021-11-18 13:35:00 +01:00
|
|
|
|
|
2021-11-15 12:38:53 +01:00
|
|
|
|
psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
slot->attr.type);
|
2021-11-15 12:38:53 +01:00
|
|
|
|
mbedtls_ecp_group_id grp_id =
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_ecc_group_of_psa(curve, bits, 0);
|
2021-11-15 12:38:53 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (grp_id == MBEDTLS_ECP_DP_NONE) {
|
2022-02-21 08:17:43 +01:00
|
|
|
|
ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
|
2021-12-02 08:46:39 +01:00
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 12:38:53 +01:00
|
|
|
|
mbedtls_ecp_group ecp_group;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_ecp_group_init(&ecp_group);
|
2021-11-15 12:38:53 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
2022-01-20 22:41:17 +01:00
|
|
|
|
/* N is the boundary of the private key domain (ecp_group.N). */
|
2021-11-15 12:38:53 +01:00
|
|
|
|
/* Let m be the bit size of N. */
|
|
|
|
|
size_t m = ecp_group.nbits;
|
2021-11-09 10:46:40 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t m_bytes = PSA_BITS_TO_BYTES(m);
|
2022-01-20 22:41:17 +01:00
|
|
|
|
|
2022-02-21 08:17:43 +01:00
|
|
|
|
/* Calculate N - 2 - it will be needed later. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
2022-01-20 22:41:17 +01:00
|
|
|
|
/* Note: This function is always called with *data == NULL and it
|
|
|
|
|
* allocates memory for the data buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
*data = mbedtls_calloc(1, m_bytes);
|
|
|
|
|
if (*data == NULL) {
|
2022-02-21 08:17:43 +01:00
|
|
|
|
ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
|
2021-11-15 12:38:53 +01:00
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
2021-11-09 10:46:40 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
while (key_out_of_range) {
|
2022-01-20 22:41:17 +01:00
|
|
|
|
/* 1. Draw a byte string of length ceiling(m/8) bytes. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
|
2022-01-20 22:41:17 +01:00
|
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-11-09 10:46:40 +01:00
|
|
|
|
|
2022-01-20 22:41:17 +01:00
|
|
|
|
/* 2. If m is not a multiple of 8 */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (m % 8 != 0) {
|
2022-01-20 22:41:17 +01:00
|
|
|
|
/* Set the most significant
|
2022-02-21 08:17:43 +01:00
|
|
|
|
* (8 * ceiling(m/8) - m) bits of the first byte in
|
|
|
|
|
* the string to zero.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
|
2022-02-21 08:17:43 +01:00
|
|
|
|
(*data)[0] &= clear_bit_mask;
|
2022-01-20 22:41:17 +01:00
|
|
|
|
}
|
2021-11-09 10:46:40 +01:00
|
|
|
|
|
2022-01-20 22:41:17 +01:00
|
|
|
|
/* 3. Convert the string to integer k by decoding it as a
|
2023-01-11 14:50:10 +01:00
|
|
|
|
* big-endian byte string.
|
|
|
|
|
*/
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
|
2022-01-20 22:41:17 +01:00
|
|
|
|
|
|
|
|
|
/* 4. If k > N - 2, discard the result and return to step 1.
|
2023-01-11 14:50:10 +01:00
|
|
|
|
* Result of comparison is returned. When it indicates error
|
|
|
|
|
* then this function is called again.
|
|
|
|
|
*/
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
|
2022-01-20 22:41:17 +01:00
|
|
|
|
}
|
2021-11-09 10:46:40 +01:00
|
|
|
|
|
2021-11-15 12:38:53 +01:00
|
|
|
|
/* 5. Output k + 1 as the private key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
|
2021-11-15 12:38:53 +01:00
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (ret != 0) {
|
|
|
|
|
status = mbedtls_to_psa_error(ret);
|
|
|
|
|
}
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
mbedtls_free(*data);
|
2021-11-15 12:38:53 +01:00
|
|
|
|
*data = NULL;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_mpi_free(&k);
|
|
|
|
|
mbedtls_mpi_free(&diff_N_2);
|
|
|
|
|
return status;
|
2021-11-15 12:38:53 +01:00
|
|
|
|
}
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
|
|
|
|
/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
|
|
|
|
|
* is determined by the curve, and sets the mandatory bits accordingly. That is:
|
|
|
|
|
*
|
|
|
|
|
* - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
|
|
|
|
|
* draw a 32-byte string and process it as specified in
|
|
|
|
|
* Elliptic Curves for Security [RFC7748] §5.
|
|
|
|
|
*
|
|
|
|
|
* - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
|
|
|
|
|
* draw a 56-byte string and process it as specified in [RFC7748] §5.
|
|
|
|
|
*
|
|
|
|
|
* Note: Function allocates memory for *data buffer, so given *data should be
|
|
|
|
|
* always NULL.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
|
|
|
|
|
size_t bits,
|
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
|
uint8_t **data
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
size_t output_length;
|
2022-03-01 15:13:30 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (bits) {
|
2022-02-21 08:17:43 +01:00
|
|
|
|
case 255:
|
|
|
|
|
output_length = 32;
|
|
|
|
|
break;
|
|
|
|
|
case 448:
|
|
|
|
|
output_length = 56;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2022-02-21 08:17:43 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
*data = mbedtls_calloc(1, output_length);
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (*data == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_output_bytes(operation, *data, output_length);
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-21 08:17:43 +01:00
|
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2022-02-21 08:17:43 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (bits) {
|
2022-02-21 08:17:43 +01:00
|
|
|
|
case 255:
|
|
|
|
|
(*data)[0] &= 248;
|
|
|
|
|
(*data)[31] &= 127;
|
|
|
|
|
(*data)[31] |= 64;
|
|
|
|
|
break;
|
|
|
|
|
case 448:
|
|
|
|
|
(*data)[0] &= 252;
|
|
|
|
|
(*data)[55] |= 128;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2022-02-21 08:17:43 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-03-04 14:41:11 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
|
2021-11-09 10:46:40 +01:00
|
|
|
|
|
2019-05-03 16:44:28 +02:00
|
|
|
|
static psa_status_t psa_generate_derived_key_internal(
|
2019-04-18 12:53:30 +02:00
|
|
|
|
psa_key_slot_t *slot,
|
|
|
|
|
size_t bits,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_derivation_operation_t *operation)
|
2018-07-12 17:12:33 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t *data = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t bytes = PSA_BITS_TO_BYTES(bits);
|
2021-06-14 06:34:16 +02:00
|
|
|
|
size_t storage_size = bytes;
|
2022-03-01 15:13:30 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
2022-03-04 14:41:11 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
|
|
|
|
|
psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
|
|
|
|
|
if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
|
2021-11-25 13:53:28 +01:00
|
|
|
|
/* Weierstrass elliptic curve */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-01-20 22:41:17 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-11-25 13:53:28 +01:00
|
|
|
|
/* Montgomery elliptic curve */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-11-25 13:53:28 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-11-25 13:53:28 +01:00
|
|
|
|
}
|
2021-11-18 13:35:00 +01:00
|
|
|
|
} else
|
2022-03-04 14:41:11 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type_is_raw_bytes(slot->attr.type)) {
|
|
|
|
|
if (bits % 8 != 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
data = mbedtls_calloc(1, bytes);
|
|
|
|
|
if (data == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_key_derivation_output_bytes(operation, data, bytes);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-11-09 10:46:40 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-02-11 19:02:00 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->attr.type == PSA_KEY_TYPE_DES) {
|
|
|
|
|
psa_des_set_key_parity(data, bytes);
|
|
|
|
|
}
|
2022-03-01 14:48:05 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-11-09 10:46:40 +01:00
|
|
|
|
}
|
2020-11-28 15:14:42 +01:00
|
|
|
|
|
2020-11-28 18:06:53 +01:00
|
|
|
|
slot->attr.bits = (psa_key_bits_t) bits;
|
2020-11-28 15:54:54 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = slot->attr
|
2020-11-28 15:54:54 +01:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
|
|
|
|
|
status = psa_driver_wrapper_get_key_buffer_size(&attributes,
|
|
|
|
|
&storage_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-14 06:34:16 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-14 06:34:16 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_allocate_buffer_to_slot(slot, storage_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-14 06:34:16 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-06-14 06:34:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01: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) {
|
2020-11-28 18:06:53 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_free(data);
|
|
|
|
|
return status;
|
2018-07-12 17:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
|
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t *key)
|
2019-04-18 12:53:30 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-07-12 23:40:35 +02:00
|
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2019-09-12 19:03:13 +02:00
|
|
|
|
|
2020-11-14 12:10:32 +01:00
|
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
|
2019-09-12 19:03:13 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_get_key_bits(attributes) == 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-09-12 19:03:13 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->alg == PSA_ALG_NONE) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2021-11-16 11:32:48 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!operation->can_output_key) {
|
|
|
|
|
return PSA_ERROR_NOT_PERMITTED;
|
|
|
|
|
}
|
2019-09-24 18:21:06 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
|
|
|
|
|
&slot, &driver);
|
2019-07-24 13:44:30 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (driver != NULL) {
|
2019-07-24 13:44:30 +02:00
|
|
|
|
/* Deriving a key in a secure element is not implemented yet. */
|
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
status = psa_generate_derived_key_internal(slot,
|
|
|
|
|
attributes->core.bits,
|
|
|
|
|
operation);
|
|
|
|
|
}
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
status = psa_finish_key_creation(slot, driver, key);
|
|
|
|
|
}
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_fail_key_creation(slot, driver);
|
2019-04-18 12:53:30 +02:00
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2019-04-18 12:53:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 17:12:33 +02:00
|
|
|
|
|
|
|
|
|
|
2018-07-12 17:17:20 +02:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Key derivation */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#if defined(AT_LEAST_ONE_BUILTIN_KDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
|
2019-01-07 22:59:38 +01: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 05:37:36 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF(kdf_alg)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif
|
2022-06-06 08:33:45 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-06-06 08:33:45 +02:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#endif
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return 0;
|
2021-04-29 21:10:00 +02:00
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
|
2021-04-29 21:18:14 +02:00
|
|
|
|
{
|
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_hash_setup(&operation, alg);
|
|
|
|
|
psa_hash_abort(&operation);
|
|
|
|
|
return status;
|
2021-04-29 21:18:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 21:10:00 +02:00
|
|
|
|
static psa_status_t psa_key_derivation_setup_kdf(
|
|
|
|
|
psa_key_derivation_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t kdf_alg)
|
2021-04-29 21:10:00 +02:00
|
|
|
|
{
|
|
|
|
|
/* Make sure that operation->ctx is properly zero-initialised. (Macro
|
|
|
|
|
* initialisers for this union leave some bytes unspecified.) */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(&operation->ctx, 0, sizeof(operation->ctx));
|
2021-04-29 21:10:00 +02:00
|
|
|
|
|
|
|
|
|
/* Make sure that kdf_alg is a supported key derivation algorithm. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!is_kdf_alg_supported(kdf_alg)) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2021-04-29 21:10:00 +02:00
|
|
|
|
|
2022-07-29 16:00:16 +02:00
|
|
|
|
/* All currently supported key derivation algorithms (apart from
|
2022-09-16 12:22:44 +02:00
|
|
|
|
* ecjpake to pms) are based on a hash algorithm. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
|
|
|
|
|
size_t hash_size = PSA_HASH_LENGTH(hash_alg);
|
|
|
|
|
if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
|
|
|
|
|
if (hash_size == 0) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2021-04-29 21:18:14 +02:00
|
|
|
|
|
2022-07-29 16:00:16 +02:00
|
|
|
|
/* Make sure that hash_alg is a supported hash algorithm. Otherwise
|
|
|
|
|
* we might fail later, which is somewhat unfriendly and potentially
|
|
|
|
|
* risk-prone. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = psa_hash_try_support(hash_alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
|
2022-07-29 16:00:16 +02:00
|
|
|
|
}
|
2021-04-29 21:18:14 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
|
|
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
|
|
|
|
|
!(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
2022-09-16 18:24:52 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
|
2022-09-26 16:50:22 +02:00
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
|
|
|
|
|
(kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
|
2022-05-10 13:53:33 +02:00
|
|
|
|
operation->capacity = hash_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else
|
2022-09-26 16:50:22 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
|
|
|
|
|
MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
operation->capacity = 255 * hash_size;
|
|
|
|
|
return PSA_SUCCESS;
|
2019-01-16 15:53:06 +01:00
|
|
|
|
}
|
2021-04-29 21:34:33 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
|
2021-04-29 21:34:33 +02:00
|
|
|
|
{
|
|
|
|
|
#if defined(PSA_WANT_ALG_ECDH)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (alg == PSA_ALG_ECDH) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2021-04-29 21:34:33 +02:00
|
|
|
|
#endif
|
|
|
|
|
(void) alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-04-29 21:34:33 +02:00
|
|
|
|
}
|
2022-12-16 01:12:12 +01:00
|
|
|
|
|
|
|
|
|
static int psa_key_derivation_allows_free_form_secret_input(
|
|
|
|
|
psa_algorithm_t kdf_alg)
|
|
|
|
|
{
|
|
|
|
|
#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
|
|
|
|
|
if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
(void) kdf_alg;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* AT_LEAST_ONE_BUILTIN_KDF */
|
2019-01-16 15:53:06 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
|
|
|
|
|
psa_algorithm_t alg)
|
2019-01-16 15:53:06 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (operation->alg != 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2019-01-16 15:53:06 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
} else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#if defined(AT_LEAST_ONE_BUILTIN_KDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
|
|
|
|
|
psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
|
|
|
|
|
status = psa_key_agreement_try_support(ka_alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-12-16 01:12:12 +01:00
|
|
|
|
if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_setup_kdf(operation, kdf_alg);
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#endif /* AT_LEAST_ONE_BUILTIN_KDF */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#if defined(AT_LEAST_ONE_BUILTIN_KDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_setup_kdf(operation, alg);
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-02-15 12:14:32 +01:00
|
|
|
|
#endif /* AT_LEAST_ONE_BUILTIN_KDF */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-01-16 15:53:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2019-05-16 17:31:03 +02:00
|
|
|
|
operation->alg = alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
return status;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
|
|
|
|
|
psa_algorithm_t kdf_alg,
|
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
size_t data_length)
|
2019-01-07 22:59:38 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
|
2019-01-07 22:59:38 +01:00
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (step) {
|
2019-05-16 16:05:19 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SALT:
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->state != HKDF_STATE_INIT) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
} else {
|
|
|
|
|
status = psa_key_derivation_start_hmac(&hkdf->hmac,
|
|
|
|
|
hash_alg,
|
|
|
|
|
data, data_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2021-04-29 19:11:25 +02:00
|
|
|
|
hkdf->state = HKDF_STATE_STARTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2021-04-29 19:11:25 +02:00
|
|
|
|
}
|
2019-05-16 16:05:19 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
|
2022-05-19 10:34:37 +02:00
|
|
|
|
/* We shouldn't be in different state as HKDF_EXPAND only allows
|
|
|
|
|
* two inputs: SECRET (this case) and INFO which does not modify
|
|
|
|
|
* the state. It could happen only if the hkdf
|
|
|
|
|
* object was corrupted. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->state != HKDF_STATE_INIT) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2022-05-10 13:53:33 +02:00
|
|
|
|
|
2022-05-19 10:34:37 +02:00
|
|
|
|
/* Allow only input that fits expected prk size */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length != PSA_HASH_LENGTH(hash_alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2022-05-10 13:53:33 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(hkdf->prk, data, data_length);
|
|
|
|
|
} else
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
|
2022-05-10 13:53:33 +02:00
|
|
|
|
{
|
2022-06-03 16:00:25 +02:00
|
|
|
|
/* HKDF: If no salt was provided, use an empty salt.
|
|
|
|
|
* HKDF-EXTRACT: salt is mandatory. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->state == HKDF_STATE_INIT) {
|
2022-06-03 16:00:25 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2022-06-03 16:00:25 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_start_hmac(&hkdf->hmac,
|
|
|
|
|
hash_alg,
|
|
|
|
|
NULL, 0);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-05-10 13:53:33 +02:00
|
|
|
|
hkdf->state = HKDF_STATE_STARTED;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->state != HKDF_STATE_STARTED) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
status = psa_mac_update(&hkdf->hmac,
|
|
|
|
|
data, data_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
status = psa_mac_sign_finish(&hkdf->hmac,
|
|
|
|
|
hkdf->prk,
|
|
|
|
|
sizeof(hkdf->prk),
|
|
|
|
|
&data_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
2022-05-10 13:53:33 +02:00
|
|
|
|
|
2019-04-12 15:11:49 +02:00
|
|
|
|
hkdf->state = HKDF_STATE_KEYED;
|
2022-05-19 11:45:20 +02:00
|
|
|
|
hkdf->block_number = 0;
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
|
2022-05-19 11:45:20 +02:00
|
|
|
|
/* The only block of output is the PRK. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
|
2022-05-19 11:45:20 +02:00
|
|
|
|
hkdf->offset_in_block = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
|
2022-05-19 11:45:20 +02:00
|
|
|
|
{
|
|
|
|
|
/* Block 0 is empty, and the next block will be
|
|
|
|
|
* generated by psa_key_derivation_hkdf_read(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
|
2022-05-19 11:45:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-05-16 16:05:19 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_INFO:
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2022-06-03 16:12:27 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
|
|
|
|
|
hkdf->state == HKDF_STATE_INIT) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2022-06-03 14:05:07 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (hkdf->state == HKDF_STATE_OUTPUT) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
if (hkdf->info_set) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2019-01-07 22:59:38 +01:00
|
|
|
|
hkdf->info_length = data_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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);
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
hkdf->info_set = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#endif /* BUILTIN_ALG_ANY_HKDF */
|
2019-01-07 22:59:38 +01:00
|
|
|
|
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
|
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2023-01-11 14:50:10 +01: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)
|
2019-06-13 10:05:41 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2019-06-13 10:05:41 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length != 0) {
|
|
|
|
|
prf->seed = mbedtls_calloc(1, data_length);
|
|
|
|
|
if (prf->seed == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2019-06-13 10:05:41 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(prf->seed, data, data_length);
|
2019-07-04 10:11:38 +02:00
|
|
|
|
prf->seed_length = data_length;
|
|
|
|
|
}
|
2019-06-13 10:05:41 +02:00
|
|
|
|
|
2020-12-13 14:55:14 +01:00
|
|
|
|
prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
|
2019-06-13 10:05:41 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-06-13 10:05:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
size_t data_length)
|
2019-01-07 22:59:38 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
|
|
|
|
|
prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2019-06-13 15:26:34 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length != 0) {
|
|
|
|
|
prf->secret = mbedtls_calloc(1, data_length);
|
|
|
|
|
if (prf->secret == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2021-04-29 19:32:25 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(prf->secret, data, data_length);
|
2021-04-29 19:32:25 +02:00
|
|
|
|
prf->secret_length = data_length;
|
|
|
|
|
}
|
2019-06-13 15:26:34 +02:00
|
|
|
|
|
2020-12-13 14:55:14 +01:00
|
|
|
|
prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
|
2019-06-13 15:26:34 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-06-13 15:26:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01: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)
|
2019-06-13 10:15:47 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2019-01-07 22:59:38 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length != 0) {
|
|
|
|
|
prf->label = mbedtls_calloc(1, data_length);
|
|
|
|
|
if (prf->label == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2019-06-13 10:15:47 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(prf->label, data, data_length);
|
2019-07-04 10:11:38 +02:00
|
|
|
|
prf->label_length = data_length;
|
2019-01-16 15:53:06 +01:00
|
|
|
|
}
|
2019-06-13 10:15:47 +02:00
|
|
|
|
|
2020-12-13 14:55:14 +01:00
|
|
|
|
prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
|
2019-06-13 10:15:47 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-06-13 10:15:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
size_t data_length)
|
2019-06-11 16:30:30 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (step) {
|
2019-06-13 10:05:41 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SEED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_tls12_prf_set_seed(prf, data, data_length);
|
2019-06-13 15:26:34 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_tls12_prf_set_key(prf, data, data_length);
|
2019-06-13 10:15:47 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_LABEL:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_tls12_prf_set_label(prf, data, data_length);
|
2019-06-11 16:30:30 +02:00
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-06-11 16:30:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01: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,
|
|
|
|
|
const uint8_t *data,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t data_length)
|
2020-11-17 07:08:34 +01:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
|
|
|
|
|
4 + data_length + prf->other_secret_length :
|
|
|
|
|
4 + 2 * data_length);
|
2020-11-17 07:08:34 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t *pms = mbedtls_calloc(1, pms_len);
|
|
|
|
|
if (pms == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2022-04-07 14:17:13 +02:00
|
|
|
|
uint8_t *cur = pms;
|
|
|
|
|
|
|
|
|
|
/* pure-PSK:
|
|
|
|
|
* Quoting RFC 4279, Section 2:
|
2020-11-17 07:08:34 +01:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
2022-04-07 14:17:13 +02:00
|
|
|
|
*
|
|
|
|
|
* mixed-PSK:
|
|
|
|
|
* In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
|
|
|
|
|
* follows: concatenate a uint16 with the length of the other secret,
|
|
|
|
|
* the other secret itself, uint16 with the length of PSK, and the
|
|
|
|
|
* PSK itself.
|
|
|
|
|
* For details please check:
|
|
|
|
|
* - RFC 4279, Section 4 for the definition of RSA-PSK,
|
|
|
|
|
* - RFC 4279, Section 3 for the definition of DHE-PSK,
|
|
|
|
|
* - RFC 5489 for the definition of ECDHE-PSK.
|
2020-11-17 07:08:34 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
|
|
|
|
|
*cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
|
|
|
|
|
*cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
|
|
|
|
|
if (prf->other_secret_length != 0) {
|
|
|
|
|
memcpy(cur, prf->other_secret, prf->other_secret_length);
|
|
|
|
|
mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
|
2022-04-12 12:08:01 +02:00
|
|
|
|
cur += prf->other_secret_length;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
|
|
|
|
*cur++ = MBEDTLS_BYTE_1(data_length);
|
|
|
|
|
*cur++ = MBEDTLS_BYTE_0(data_length);
|
|
|
|
|
memset(cur, 0, data_length);
|
2022-04-07 14:17:13 +02:00
|
|
|
|
cur += data_length;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
*cur++ = MBEDTLS_BYTE_1(data_length);
|
|
|
|
|
*cur++ = MBEDTLS_BYTE_0(data_length);
|
|
|
|
|
memcpy(cur, data, data_length);
|
2020-11-17 07:08:34 +01:00
|
|
|
|
cur += data_length;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_tls12_prf_set_key(prf, pms, cur - pms);
|
2020-11-17 07:08:34 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(pms, pms_len);
|
|
|
|
|
mbedtls_free(pms);
|
|
|
|
|
return status;
|
2020-11-17 07:08:34 +01:00
|
|
|
|
}
|
2019-06-17 09:44:03 +02:00
|
|
|
|
|
2022-04-19 13:53:28 +02:00
|
|
|
|
static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
|
|
|
|
|
psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
|
const uint8_t *data,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t data_length)
|
2022-04-19 13:53:28 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2022-04-19 13:53:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length != 0) {
|
|
|
|
|
prf->other_secret = mbedtls_calloc(1, data_length);
|
|
|
|
|
if (prf->other_secret == NULL) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
}
|
2022-04-19 13:53:28 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(prf->other_secret, data, data_length);
|
2022-04-19 13:53:28 +02:00
|
|
|
|
prf->other_secret_length = data_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else {
|
2022-04-19 13:53:28 +02:00
|
|
|
|
prf->other_secret_length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2022-04-19 13:53:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 09:44:03 +02:00
|
|
|
|
static psa_status_t psa_tls12_prf_psk_to_ms_input(
|
|
|
|
|
psa_tls12_prf_key_derivation_t *prf,
|
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
|
const uint8_t *data,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t data_length)
|
2019-06-17 09:44:03 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (step) {
|
2022-04-19 13:53:28 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_tls12_prf_psk_to_ms_set_key(prf,
|
|
|
|
|
data, data_length);
|
2022-04-19 13:53:28 +02:00
|
|
|
|
break;
|
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_tls12_prf_psk_to_ms_set_other_key(prf,
|
|
|
|
|
data,
|
|
|
|
|
data_length);
|
2022-04-19 13:53:28 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_tls12_prf_input(prf, step, data, data_length);
|
2022-04-19 13:53:28 +02:00
|
|
|
|
break;
|
2019-06-17 09:44:03 +02:00
|
|
|
|
|
2022-04-19 13:53:28 +02:00
|
|
|
|
}
|
2019-06-17 09:44:03 +02:00
|
|
|
|
}
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2019-01-07 22:59:38 +01:00
|
|
|
|
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
|
|
|
|
static psa_status_t psa_tls12_ecjpake_to_pms_input(
|
|
|
|
|
psa_tls12_ecjpake_to_pms_t *ecjpake,
|
2022-09-16 12:22:44 +02:00
|
|
|
|
psa_key_derivation_step_t step,
|
2022-07-29 16:00:16 +02:00
|
|
|
|
const uint8_t *data,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t data_length)
|
2022-07-29 16:00:16 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
|
|
|
|
|
step != PSA_KEY_DERIVATION_INPUT_SECRET) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2022-09-26 16:49:16 +02:00
|
|
|
|
}
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
|
|
|
|
/* Check if the passed point is in an uncompressed form */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (data[0] != 0x04) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
|
|
|
|
/* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
|
2022-07-29 16:00:16 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2022-07-29 16:00:16 +02:00
|
|
|
|
}
|
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
|
2019-09-24 16:21:10 +02: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 18:13:17 +02:00
|
|
|
|
static int psa_key_derivation_check_input_type(
|
|
|
|
|
psa_key_derivation_step_t step,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_type_t key_type)
|
2019-09-23 18:13:17 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (step) {
|
2019-09-23 18:13:17 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type == PSA_KEY_TYPE_DERIVE) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
if (key_type == PSA_KEY_TYPE_NONE) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2019-09-23 18:13:17 +02:00
|
|
|
|
break;
|
2022-04-07 15:39:01 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type == PSA_KEY_TYPE_DERIVE) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
if (key_type == PSA_KEY_TYPE_NONE) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2022-04-07 15:39:01 +02:00
|
|
|
|
break;
|
2019-09-23 18:13:17 +02:00
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_LABEL:
|
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SALT:
|
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_INFO:
|
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SEED:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type == PSA_KEY_TYPE_RAW_DATA) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
if (key_type == PSA_KEY_TYPE_NONE) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2019-09-23 18:13:17 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2019-09-23 18:13:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 16:54:46 +02:00
|
|
|
|
static psa_status_t psa_key_derivation_input_internal(
|
2019-05-16 17:31:03 +02:00
|
|
|
|
psa_key_derivation_operation_t *operation,
|
2019-01-08 10:31:27 +01:00
|
|
|
|
psa_key_derivation_step_t step,
|
2019-09-23 18:13:17 +02:00
|
|
|
|
psa_key_type_t key_type,
|
2019-01-08 10:31:27 +01:00
|
|
|
|
const uint8_t *data,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t data_length)
|
2019-01-07 22:59:38 +01:00
|
|
|
|
{
|
2019-09-23 19:22:55 +02:00
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
|
2019-09-23 19:22:55 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_check_input_type(step, key_type);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-09-23 18:13:17 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-09-23 18:13:17 +02:00
|
|
|
|
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#if defined(BUILTIN_ALG_ANY_HKDF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
|
|
|
|
|
status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
|
|
|
|
|
step, data, data_length);
|
|
|
|
|
} else
|
2022-06-10 12:59:51 +02:00
|
|
|
|
#endif /* BUILTIN_ALG_ANY_HKDF */
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
|
|
|
|
|
status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
|
|
|
|
|
step, data, data_length);
|
|
|
|
|
} else
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
|
|
|
|
|
status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
|
|
|
|
|
step, data, data_length);
|
|
|
|
|
} else
|
2020-11-17 07:08:34 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
|
2022-07-29 16:00:16 +02:00
|
|
|
|
status = psa_tls12_ecjpake_to_pms_input(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
&operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
|
|
|
|
|
} else
|
2022-07-29 16:00:16 +02:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
|
2019-01-07 22:59:38 +01:00
|
|
|
|
{
|
2019-05-16 17:31:03 +02:00
|
|
|
|
/* This can't happen unless the operation object was not initialized */
|
2021-02-10 17:19:22 +01:00
|
|
|
|
(void) data;
|
|
|
|
|
(void) data_length;
|
|
|
|
|
(void) kdf_alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 18:13:17 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_key_derivation_abort(operation);
|
|
|
|
|
}
|
|
|
|
|
return status;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 12:35:55 +02: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,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
size_t data_length)
|
2019-01-08 10:31:27 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_key_derivation_input_internal(operation, step,
|
|
|
|
|
PSA_KEY_TYPE_NONE,
|
|
|
|
|
data, data_length);
|
2019-01-08 10:31:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 12:35:55 +02:00
|
|
|
|
psa_status_t psa_key_derivation_input_key(
|
|
|
|
|
psa_key_derivation_operation_t *operation,
|
|
|
|
|
psa_key_derivation_step_t step,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_svc_key_id_t key)
|
2019-01-07 22:59:38 +01:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2019-09-24 18:21:06 +02:00
|
|
|
|
|
2020-11-14 16:35:34 +01:00
|
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_key_derivation_abort(operation);
|
|
|
|
|
return status;
|
2019-09-23 18:17:40 +02:00
|
|
|
|
}
|
2019-09-24 18:21:06 +02:00
|
|
|
|
|
|
|
|
|
/* Passing a key object as a SECRET input unlocks the permission
|
|
|
|
|
* to output to a key object. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
|
2019-09-24 18:21:06 +02:00
|
|
|
|
operation->can_output_key = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-09-24 18:21:06 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_input_internal(operation,
|
|
|
|
|
step, slot->attr.type,
|
|
|
|
|
slot->key.data,
|
|
|
|
|
slot->key.bytes);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2019-01-07 22:59:38 +01:00
|
|
|
|
}
|
2018-07-12 17:17:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-18 12:01:02 +02:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Key agreement */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
|
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
|
size_t key_buffer_size,
|
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
|
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 17:50:04 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2019-04-11 21:23:21 +02:00
|
|
|
|
case PSA_ALG_ECDH:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
|
|
|
|
|
key_buffer_size, alg,
|
|
|
|
|
peer_key, peer_key_length,
|
|
|
|
|
shared_secret,
|
|
|
|
|
shared_secret_size,
|
|
|
|
|
shared_secret_length);
|
2020-11-10 17:50:04 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
|
2019-04-11 21:23:21 +02:00
|
|
|
|
default:
|
2022-10-13 18:21:01 +02:00
|
|
|
|
(void) attributes;
|
|
|
|
|
(void) key_buffer;
|
|
|
|
|
(void) key_buffer_size;
|
2019-04-11 21:23:21 +02:00
|
|
|
|
(void) peer_key;
|
|
|
|
|
(void) peer_key_length;
|
|
|
|
|
(void) shared_secret;
|
|
|
|
|
(void) shared_secret_size;
|
|
|
|
|
(void) shared_secret_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2019-04-11 21:23:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 18:21:01 +02:00
|
|
|
|
/** Internal function for raw key agreement
|
|
|
|
|
* Calls the driver wrapper which will hand off key agreement task
|
2022-10-14 17:41:40 +02:00
|
|
|
|
* to the driver's implementation if a driver is present.
|
|
|
|
|
* Fallback specified in the driver wrapper is built-in raw key agreement
|
2022-10-13 18:21:01 +02:00
|
|
|
|
* (psa_key_agreement_raw_builtin).
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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)
|
2022-10-13 18:21:01 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2022-10-13 18:21:01 +02:00
|
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = {
|
2023-01-11 14:50:10 +01:00
|
|
|
|
.core = private_key->attr
|
2022-10-13 18:21:01 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return psa_driver_wrapper_key_agreement(&attributes,
|
|
|
|
|
private_key->key.data,
|
|
|
|
|
private_key->key.bytes, alg,
|
|
|
|
|
peer_key, peer_key_length,
|
|
|
|
|
shared_secret,
|
|
|
|
|
shared_secret_size,
|
|
|
|
|
shared_secret_length);
|
2022-10-13 18:21:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
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 15:28:51 +02:00
|
|
|
|
/* Note that if this function fails, you must call psa_key_derivation_abort()
|
2018-11-16 16:05:06 +01:00
|
|
|
|
* to potentially free embedded data structures and wipe confidential data.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
|
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
|
psa_key_slot_t *private_key,
|
|
|
|
|
const uint8_t *peer_key,
|
|
|
|
|
size_t peer_key_length)
|
2018-09-18 12:01:02 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
2022-11-22 12:10:34 +01:00
|
|
|
|
uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
|
2018-09-18 12:01:02 +02:00
|
|
|
|
size_t shared_secret_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
|
2018-09-18 12:01:02 +02:00
|
|
|
|
|
|
|
|
|
/* Step 1: run the secret agreement algorithm to generate the shared
|
|
|
|
|
* secret. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_agreement_raw_internal(ka_alg,
|
|
|
|
|
private_key,
|
|
|
|
|
peer_key, peer_key_length,
|
|
|
|
|
shared_secret,
|
|
|
|
|
sizeof(shared_secret),
|
|
|
|
|
&shared_secret_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2018-09-18 12:01:02 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2018-09-18 12:01:02 +02:00
|
|
|
|
|
|
|
|
|
/* Step 2: set up the key derivation to generate key material from
|
2019-09-23 18:13:17 +02:00
|
|
|
|
* the shared secret. A shared secret is permitted wherever a key
|
|
|
|
|
* of type DERIVE is permitted. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_derivation_input_internal(operation, step,
|
|
|
|
|
PSA_KEY_TYPE_DERIVE,
|
|
|
|
|
shared_secret,
|
|
|
|
|
shared_secret_length);
|
2018-09-18 12:01:02 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(shared_secret, shared_secret_length);
|
|
|
|
|
return status;
|
2018-09-18 12:01:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
|
|
|
|
|
psa_key_derivation_step_t step,
|
|
|
|
|
mbedtls_svc_key_id_t private_key,
|
|
|
|
|
const uint8_t *peer_key,
|
|
|
|
|
size_t peer_key_length)
|
2018-09-18 12:01:02 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2018-12-04 17:12:32 +01:00
|
|
|
|
psa_key_slot_t *slot;
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2020-11-14 16:35:34 +01:00
|
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
status = psa_key_agreement_internal(operation, step,
|
|
|
|
|
slot,
|
|
|
|
|
peer_key, peer_key_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_key_derivation_abort(operation);
|
|
|
|
|
} else {
|
2020-10-15 17:07:12 +02:00
|
|
|
|
/* If a private key has been added as SECRET, we allow the derived
|
|
|
|
|
* key material to be used as a key in PSA Crypto. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
|
2020-10-15 17:07:12 +02:00
|
|
|
|
operation->can_output_key = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-10-15 17:07:12 +02:00
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2018-09-18 12:01:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
|
|
|
|
|
mbedtls_svc_key_id_t private_key,
|
|
|
|
|
const uint8_t *peer_key,
|
|
|
|
|
size_t peer_key_length,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_size,
|
|
|
|
|
size_t *output_length)
|
2019-04-11 21:23:21 +02:00
|
|
|
|
{
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-11-14 16:35:34 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-10-22 15:24:49 +02:00
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-04-11 21:23:21 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
|
2019-04-11 21:23:21 +02:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2020-11-14 16:35:34 +01:00
|
|
|
|
status = psa_get_and_lock_transparent_key_slot_with_policy(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-04-11 21:23:21 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-04-11 21:23:21 +02:00
|
|
|
|
|
2022-04-14 00:17:15 +02:00
|
|
|
|
/* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
|
|
|
|
|
* for the output size. The PSA specification only guarantees that this
|
|
|
|
|
* function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
|
|
|
|
|
* but it might be nice to allow smaller buffers if the output fits.
|
|
|
|
|
* At the time of writing this comment, with only ECDH implemented,
|
|
|
|
|
* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
|
|
|
|
|
* If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
|
|
|
|
|
* be exact for it as well. */
|
|
|
|
|
size_t expected_length =
|
2023-01-11 14:50:10 +01:00
|
|
|
|
PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
|
|
|
|
|
if (output_size < expected_length) {
|
2022-04-14 00:17:15 +02:00
|
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_key_agreement_raw_internal(alg, slot,
|
|
|
|
|
peer_key, peer_key_length,
|
|
|
|
|
output, output_size,
|
|
|
|
|
output_length);
|
2019-04-11 21:23:21 +02:00
|
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-04-11 21:23:21 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_generate_random(output, output_size);
|
2019-04-11 21:23:21 +02:00
|
|
|
|
*output_length = output_size;
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2019-04-11 21:23:21 +02:00
|
|
|
|
}
|
2018-09-18 12:01:02 +02:00
|
|
|
|
|
|
|
|
|
|
2020-11-13 17:02:26 +01:00
|
|
|
|
|
2018-07-12 17:12:33 +02:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Random generation */
|
2018-06-19 22:00:52 +02:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2020-11-13 17:02:26 +01:00
|
|
|
|
/** Initialize the PSA random generator.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
|
2020-11-13 17:02:26 +01:00
|
|
|
|
{
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(rng, 0, sizeof(*rng));
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
|
|
|
|
|
2020-11-13 17:02:26 +01:00
|
|
|
|
/* Set default configuration if
|
|
|
|
|
* mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (rng->entropy_init == NULL) {
|
2020-11-13 17:02:26 +01:00
|
|
|
|
rng->entropy_init = mbedtls_entropy_init;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (rng->entropy_free == NULL) {
|
2020-11-13 17:02:26 +01:00
|
|
|
|
rng->entropy_free = mbedtls_entropy_free;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-11-13 17:02:26 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
rng->entropy_init(&rng->entropy);
|
2020-11-13 17:02:26 +01:00
|
|
|
|
#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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_entropy_add_source(&rng->entropy,
|
|
|
|
|
mbedtls_nv_seed_poll, NULL,
|
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
2020-11-13 17:02:26 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 17:02:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Deinitialize the PSA random generator.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
|
2020-11-13 17:02:26 +01:00
|
|
|
|
{
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
memset(rng, 0, sizeof(*rng));
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
|
|
|
|
|
rng->entropy_free(&rng->entropy);
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 17:02:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Seed the PSA random generator.
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
|
2020-11-13 17:02:26 +01:00
|
|
|
|
{
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
|
/* Do nothing: the external RNG seeds itself. */
|
|
|
|
|
(void) rng;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 17:02:26 +01:00
|
|
|
|
const unsigned char drbg_seed[] = "PSA";
|
2023-01-11 14:50:10 +01:00
|
|
|
|
int ret = mbedtls_psa_drbg_seed(&rng->entropy,
|
|
|
|
|
drbg_seed, sizeof(drbg_seed) - 1);
|
|
|
|
|
return mbedtls_to_psa_error(ret);
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2020-11-13 17:02:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_generate_random(uint8_t *output,
|
|
|
|
|
size_t output_size)
|
2018-06-19 22:00:52 +02:00
|
|
|
|
{
|
2018-09-06 15:24:41 +02:00
|
|
|
|
GUARD_MODULE_INITIALIZED;
|
|
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
|
|
|
|
|
|
size_t output_length = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
|
|
|
|
|
output, output_size,
|
|
|
|
|
&output_length);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-11-13 18:47:18 +01:00
|
|
|
|
/* Breaking up a request into smaller chunks is currently not supported
|
2022-12-04 18:19:59 +01:00
|
|
|
|
* for the external RNG interface. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (output_length != output_size) {
|
|
|
|
|
return PSA_ERROR_INSUFFICIENT_ENTROPY;
|
|
|
|
|
}
|
|
|
|
|
return PSA_SUCCESS;
|
2020-11-13 18:47:18 +01:00
|
|
|
|
|
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
while (output_size > 0) {
|
2021-01-04 21:01:07 +01:00
|
|
|
|
size_t request_size =
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
|
|
|
|
|
MBEDTLS_PSA_RANDOM_MAX_REQUEST :
|
|
|
|
|
output_size);
|
|
|
|
|
int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
|
output, request_size);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
return mbedtls_to_psa_error(ret);
|
|
|
|
|
}
|
2021-01-04 21:01:07 +01:00
|
|
|
|
output_size -= request_size;
|
|
|
|
|
output += request_size;
|
2019-08-07 13:49:00 +02:00
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2018-06-19 22:00:52 +02:00
|
|
|
|
}
|
2019-08-07 13:49:00 +02:00
|
|
|
|
|
2020-12-14 15:33:44 +01:00
|
|
|
|
/* Wrapper function allowing the classic API to use the PSA RNG.
|
2021-01-05 16:03:55 +01: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 15:33:44 +01:00
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
|
int mbedtls_psa_get_random(void *p_rng,
|
|
|
|
|
unsigned char *output,
|
|
|
|
|
size_t output_size)
|
2020-12-14 15:33:44 +01:00
|
|
|
|
{
|
2021-01-05 16:03:55 +01: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 15:33:44 +01:00
|
|
|
|
(void) p_rng;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
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 22:00:52 +02:00
|
|
|
|
}
|
2020-12-14 15:33:44 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2018-06-19 22:00:52 +02:00
|
|
|
|
|
2019-02-25 11:04:06 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
2021-03-09 20:11:19 +01:00
|
|
|
|
#include "entropy_poll.h"
|
2019-02-25 11:04:06 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
|
|
|
|
|
size_t seed_size)
|
2018-11-19 10:53:02 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (global_data.initialized) {
|
|
|
|
|
return PSA_ERROR_NOT_PERMITTED;
|
|
|
|
|
}
|
2018-11-19 10:53:55 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01: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;
|
|
|
|
|
}
|
2018-11-19 10:53:55 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return mbedtls_psa_storage_inject_entropy(seed, seed_size);
|
2018-11-19 10:53:02 +01:00
|
|
|
|
}
|
2019-02-25 11:04:06 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
|
2018-11-19 10:53:02 +01:00
|
|
|
|
|
2021-02-08 16:10:05 +01:00
|
|
|
|
/** Validate the key type and size for key generation
|
2020-10-05 09:42:02 +02:00
|
|
|
|
*
|
2021-02-08 16:10:05 +01:00
|
|
|
|
* \param type The key type
|
|
|
|
|
* \param bits The number of bits of the key
|
2020-10-05 09:42:02 +02:00
|
|
|
|
*
|
|
|
|
|
* \retval #PSA_SUCCESS
|
2021-02-08 16:10:05 +01:00
|
|
|
|
* The key type and size are valid.
|
2020-10-05 09:42:02 +02: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 16:10:05 +01:00
|
|
|
|
static psa_status_t psa_validate_key_type_and_size_for_key_generation(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_key_type_t type, size_t bits)
|
2020-10-05 09:42:02 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type_is_raw_bytes(type)) {
|
|
|
|
|
status = psa_validate_unstructured_key_bit_size(type, bits);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
} else
|
2020-12-07 11:07:24 +01:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
|
2023-01-11 14:50:10 +01: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;
|
|
|
|
|
}
|
2020-10-05 09:42:02 +02:00
|
|
|
|
|
|
|
|
|
/* Accept only byte-aligned keys, for the same reasons as
|
|
|
|
|
* in psa_import_rsa_key(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (bits % 8 != 0) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
} else
|
2020-12-07 11:07:24 +01:00
|
|
|
|
#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
|
2020-10-05 09:42:02 +02:00
|
|
|
|
|
2020-12-07 11:07:24 +01:00
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
|
2021-02-16 09:01:16 +01:00
|
|
|
|
/* To avoid empty block, return successfully here. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
} else
|
2020-12-07 11:07:24 +01:00
|
|
|
|
#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
|
2020-10-05 09:42:02 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-10-05 09:42:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2020-10-05 09:42:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 10:30:40 +02:00
|
|
|
|
psa_status_t psa_generate_key_internal(
|
2020-10-02 20:02:04 +02:00
|
|
|
|
const psa_key_attributes_t *attributes,
|
2023-01-11 14:50:10 +01:00
|
|
|
|
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
|
2018-06-19 22:00:52 +02:00
|
|
|
|
{
|
2020-10-02 20:11:59 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-10-02 20:02:04 +02:00
|
|
|
|
psa_key_type_t type = attributes->core.type;
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if ((attributes->domain_parameters == NULL) &&
|
|
|
|
|
(attributes->domain_parameters_size != 0)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2018-07-06 17:01:38 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (key_type_is_raw_bytes(type)) {
|
|
|
|
|
status = psa_generate_random(key_buffer, key_buffer_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2020-07-24 22:04:59 +02:00
|
|
|
|
|
2021-02-11 19:02:00 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (type == PSA_KEY_TYPE_DES) {
|
|
|
|
|
psa_des_set_key_parity(key_buffer, key_buffer_size);
|
|
|
|
|
}
|
2021-02-12 16:08:46 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
} else
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
psa: Support RSA signature without MBEDTLS_GENPRIME
On space-constrained platforms, it is a useful configuration to be able
to import/export and perform RSA key pair operations, but to exclude RSA
key generation, potentially saving flash space. It is not possible to
express this with the PSA_WANT_ configuration system at the present
time. However, in previous versions of Mbed TLS (v2.24.0 and earlier) it
was possible to configure a software PSA implementation which was
capable of making RSA signatures but not capable of generating RSA keys.
To do this, one unset MBEDTLS_GENPRIME.
Since the addition of MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR, this
expressivity was lost. Expressing that you wanted to work with RSA key
pairs forced you to include the ability to generate key pairs as well.
Change psa_crypto_rsa.c to only call mbedtls_rsa_gen_key() if
MBEDTLS_GENPRIME is also set. This restores the configuration behavior
present in Mbed TLS v2.24.0 and earlier versions.
It left as a future exercise to add the ability to PSA to be able to
express a desire for a software or accelerator configuration that
includes RSA key pair operations, like signature, but excludes key pair
generation.
Without this change, linker errors will occur when attempts to call,
which doesn't exist when MBEDTLS_GENPRIME is unset.
psa_crypto_rsa.c.obj: in function `rsa_generate_key':
psa_crypto_rsa.c:320: undefined reference to `mbedtls_rsa_gen_key'
Fixes #4512
Signed-off-by: Jaeden Amero <jaeden.amero@arm.com>
2021-05-14 09:34:32 +02:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
|
|
|
|
|
defined(MBEDTLS_GENPRIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
|
|
|
|
|
return mbedtls_psa_rsa_generate_key(attributes,
|
|
|
|
|
key_buffer,
|
|
|
|
|
key_buffer_size,
|
|
|
|
|
key_buffer_length);
|
|
|
|
|
} else
|
psa: Support RSA signature without MBEDTLS_GENPRIME
On space-constrained platforms, it is a useful configuration to be able
to import/export and perform RSA key pair operations, but to exclude RSA
key generation, potentially saving flash space. It is not possible to
express this with the PSA_WANT_ configuration system at the present
time. However, in previous versions of Mbed TLS (v2.24.0 and earlier) it
was possible to configure a software PSA implementation which was
capable of making RSA signatures but not capable of generating RSA keys.
To do this, one unset MBEDTLS_GENPRIME.
Since the addition of MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR, this
expressivity was lost. Expressing that you wanted to work with RSA key
pairs forced you to include the ability to generate key pairs as well.
Change psa_crypto_rsa.c to only call mbedtls_rsa_gen_key() if
MBEDTLS_GENPRIME is also set. This restores the configuration behavior
present in Mbed TLS v2.24.0 and earlier versions.
It left as a future exercise to add the ability to PSA to be able to
express a desire for a software or accelerator configuration that
includes RSA key pair operations, like signature, but excludes key pair
generation.
Without this change, linker errors will occur when attempts to call,
which doesn't exist when MBEDTLS_GENPRIME is unset.
psa_crypto_rsa.c.obj: in function `rsa_generate_key':
psa_crypto_rsa.c:320: undefined reference to `mbedtls_rsa_gen_key'
Fixes #4512
Signed-off-by: Jaeden Amero <jaeden.amero@arm.com>
2021-05-14 09:34:32 +02:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
|
|
|
|
|
* defined(MBEDTLS_GENPRIME) */
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
|
|
|
|
|
return mbedtls_psa_ecp_generate_key(attributes,
|
|
|
|
|
key_buffer,
|
|
|
|
|
key_buffer_size,
|
|
|
|
|
key_buffer_length);
|
|
|
|
|
} else
|
2020-11-04 21:28:15 +01:00
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
|
2020-08-05 15:43:42 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
(void) key_buffer_length;
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2020-08-05 15:43:42 +02:00
|
|
|
|
}
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2019-04-18 12:53:30 +02:00
|
|
|
|
}
|
2018-11-07 17:05:30 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
|
|
|
|
|
mbedtls_svc_key_id_t *key)
|
2019-04-18 12:53:30 +02:00
|
|
|
|
{
|
|
|
|
|
psa_status_t status;
|
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2019-07-12 23:40:35 +02:00
|
|
|
|
psa_se_drv_table_entry_t *driver = NULL;
|
2020-10-05 10:02:26 +02:00
|
|
|
|
size_t key_buffer_size;
|
2019-08-06 18:36:36 +02:00
|
|
|
|
|
2020-11-14 12:10:32 +01:00
|
|
|
|
*key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
|
|
2019-09-12 19:03:13 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (psa_get_key_bits(attributes) == 0) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2019-09-12 19:03:13 +02:00
|
|
|
|
|
2021-10-07 11:08:56 +02:00
|
|
|
|
/* Reject any attempt to create a public key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
2021-10-07 11:08:56 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
|
|
|
|
|
&slot, &driver);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-08-06 18:36:36 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-08-06 18:36:36 +02:00
|
|
|
|
|
2020-10-13 08:32:21 +02:00
|
|
|
|
/* In the case of a transparent key or an opaque key stored in local
|
2021-09-08 12:06:05 +02:00
|
|
|
|
* storage ( thus not in the case of generating a key in a secure element
|
|
|
|
|
* with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
|
|
|
|
|
* buffer to hold the generated key material. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (slot->key.data == NULL) {
|
|
|
|
|
if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
|
|
|
|
|
PSA_KEY_LOCATION_LOCAL_STORAGE) {
|
2021-02-08 16:10:05 +01:00
|
|
|
|
status = psa_validate_key_type_and_size_for_key_generation(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes->core.type, attributes->core.bits);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-02-08 16:10:05 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2021-02-08 16:10:05 +01:00
|
|
|
|
|
|
|
|
|
key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes->core.type,
|
|
|
|
|
attributes->core.bits);
|
|
|
|
|
} else {
|
2020-10-13 08:32:21 +02:00
|
|
|
|
status = psa_driver_wrapper_get_key_buffer_size(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
attributes, &key_buffer_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-02-08 16:10:05 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-10-13 08:32:21 +02:00
|
|
|
|
}
|
2020-10-05 10:02:26 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-10-13 08:32:21 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2020-10-13 08:32:21 +02:00
|
|
|
|
}
|
2020-10-05 10:02:26 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_driver_wrapper_generate_key(attributes,
|
|
|
|
|
slot->key.data, slot->key.bytes, &slot->key.bytes);
|
2019-08-06 18:36:36 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_remove_key_data_from_memory(slot);
|
|
|
|
|
}
|
2020-10-05 10:02:26 +02:00
|
|
|
|
|
2019-08-06 18:36:36 +02:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
status = psa_finish_key_creation(slot, driver, key);
|
|
|
|
|
}
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_fail_key_creation(slot, driver);
|
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return status;
|
2018-06-19 22:00:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
|
/****************************************************************/
|
|
|
|
|
/* Module setup */
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
2018-11-20 21:59:56 +01:00
|
|
|
|
psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
void (* entropy_init)(mbedtls_entropy_context *ctx),
|
|
|
|
|
void (* entropy_free)(mbedtls_entropy_context *ctx))
|
2018-11-20 21:59:56 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (global_data.rng_state != RNG_NOT_INITIALIZED) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2020-11-13 17:02:26 +01:00
|
|
|
|
global_data.rng.entropy_init = entropy_init;
|
|
|
|
|
global_data.rng.entropy_free = entropy_free;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2018-11-20 21:59:56 +01:00
|
|
|
|
}
|
2020-11-13 18:47:18 +01:00
|
|
|
|
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
|
2018-11-20 21:59:56 +01:00
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
void mbedtls_psa_crypto_free(void)
|
2018-01-27 23:32:46 +01:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_wipe_all_key_slots();
|
|
|
|
|
if (global_data.rng_state != RNG_NOT_INITIALIZED) {
|
|
|
|
|
mbedtls_psa_random_free(&global_data.rng);
|
2018-11-15 15:48:15 +01:00
|
|
|
|
}
|
2018-11-20 21:42:52 +01:00
|
|
|
|
/* Wipe all remaining data, including configuration.
|
|
|
|
|
* In particular, this sets all state indicator to the value
|
|
|
|
|
* indicating "uninitialized". */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_platform_zeroize(&global_data, sizeof(global_data));
|
2021-04-10 16:57:30 +02:00
|
|
|
|
|
|
|
|
|
/* Terminate drivers */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_driver_wrapper_free();
|
2018-01-27 23:32:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 17:52:59 +02: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(
|
2023-01-11 14:50:10 +01:00
|
|
|
|
const psa_crypto_transaction_t *transaction)
|
2019-07-25 17:52:59 +02:00
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
|
switch (transaction->unknown.type) {
|
2019-07-25 17:52:59 +02:00
|
|
|
|
case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
|
|
|
|
|
case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
/* TODO - fall through to the failure case until this
|
|
|
|
|
* is implemented.
|
|
|
|
|
* https://github.com/ARMmbed/mbed-crypto/issues/218
|
|
|
|
|
*/
|
2019-07-25 17:52:59 +02:00
|
|
|
|
default:
|
|
|
|
|
/* We found an unsupported transaction in the storage.
|
|
|
|
|
* We don't know what state the storage is in. Give up. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
2019-07-25 17:52:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
psa_status_t psa_crypto_init(void)
|
2018-01-27 23:32:46 +01:00
|
|
|
|
{
|
2018-12-10 16:29:04 +01:00
|
|
|
|
psa_status_t status;
|
2018-01-27 23:32:46 +01:00
|
|
|
|
|
2018-11-20 21:42:52 +01:00
|
|
|
|
/* Double initialization is explicitly allowed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (global_data.initialized != 0) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2018-01-27 23:32:46 +01:00
|
|
|
|
|
2023-03-09 11:17:43 +01:00
|
|
|
|
/* Init drivers */
|
|
|
|
|
status = psa_driver_wrapper_init();
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
global_data.drivers_initialized = 1;
|
|
|
|
|
|
2020-11-13 17:02:26 +01:00
|
|
|
|
/* Initialize and seed the random generator. */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
mbedtls_psa_random_init(&global_data.rng);
|
2018-11-20 21:42:52 +01:00
|
|
|
|
global_data.rng_state = RNG_INITIALIZED;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = mbedtls_psa_random_seed(&global_data.rng);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2018-01-27 23:32:46 +01:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2018-11-20 21:42:52 +01:00
|
|
|
|
global_data.rng_state = RNG_SEEDED;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_initialize_key_slots();
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-10-01 15:22:29 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
2019-10-01 15:22:29 +02:00
|
|
|
|
|
2019-07-24 15:56:31 +02:00
|
|
|
|
#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
|
2023-01-11 14:50:10 +01:00
|
|
|
|
status = psa_crypto_load_transaction();
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
|
status = psa_crypto_recover_transaction(&psa_crypto_transaction);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-07-25 17:52:59 +02:00
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
status = psa_crypto_stop_transaction();
|
|
|
|
|
} else if (status == PSA_ERROR_DOES_NOT_EXIST) {
|
2019-07-22 19:30:34 +02:00
|
|
|
|
/* There's no transaction to complete. It's all good. */
|
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
|
}
|
2019-07-24 15:56:31 +02:00
|
|
|
|
#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
|
2019-07-22 19:30:34 +02:00
|
|
|
|
|
2018-11-20 21:42:52 +01:00
|
|
|
|
/* All done. */
|
2018-03-07 14:16:44 +01:00
|
|
|
|
global_data.initialized = 1;
|
|
|
|
|
|
2018-01-27 23:32:46 +01:00
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
mbedtls_psa_crypto_free();
|
|
|
|
|
}
|
|
|
|
|
return status;
|
2018-01-27 23:32:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 16:21:33 +01:00
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_password_len(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
|
|
|
|
size_t *password_len)
|
|
|
|
|
{
|
|
|
|
|
if (inputs->password_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*password_len = inputs->password_len;
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_password(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
|
|
|
|
uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
|
|
|
|
|
{
|
|
|
|
|
if (inputs->password_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer_size < inputs->password_len) {
|
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(buffer, inputs->password, inputs->password_len);
|
|
|
|
|
*buffer_length = inputs->password_len;
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_role(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
|
|
|
|
psa_pake_role_t *role)
|
|
|
|
|
{
|
|
|
|
|
if (inputs->role == PSA_PAKE_ROLE_NONE) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*role = inputs->role;
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 14:38:58 +01:00
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_user_len(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
|
|
|
|
size_t *user_len)
|
|
|
|
|
{
|
|
|
|
|
if (inputs->user_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*user_len = inputs->user_len;
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_user(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
2023-03-14 11:49:36 +01:00
|
|
|
|
uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
|
2023-02-28 14:38:58 +01:00
|
|
|
|
{
|
|
|
|
|
if (inputs->user_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 11:49:36 +01:00
|
|
|
|
if (user_id_size < inputs->user_len) {
|
2023-02-28 14:38:58 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 11:49:36 +01:00
|
|
|
|
memcpy(user_id, inputs->user, inputs->user_len);
|
|
|
|
|
*user_id_len = inputs->user_len;
|
2023-02-28 14:38:58 +01:00
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_peer_len(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
|
|
|
|
size_t *peer_len)
|
|
|
|
|
{
|
|
|
|
|
if (inputs->peer_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*peer_len = inputs->peer_len;
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_peer(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
2023-03-14 11:49:36 +01:00
|
|
|
|
uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
|
2023-02-28 14:38:58 +01:00
|
|
|
|
{
|
|
|
|
|
if (inputs->peer_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 11:49:36 +01:00
|
|
|
|
if (peer_id_size < inputs->peer_len) {
|
2023-02-28 14:38:58 +01:00
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 11:49:36 +01:00
|
|
|
|
memcpy(peer_id, inputs->peer, inputs->peer_len);
|
|
|
|
|
*peer_id_length = inputs->peer_len;
|
2023-02-28 14:38:58 +01:00
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 16:21:33 +01:00
|
|
|
|
psa_status_t psa_crypto_driver_pake_get_cipher_suite(
|
|
|
|
|
const psa_crypto_driver_pake_inputs_t *inputs,
|
|
|
|
|
psa_pake_cipher_suite_t *cipher_suite)
|
|
|
|
|
{
|
|
|
|
|
if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*cipher_suite = inputs->cipher_suite;
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 18:21:20 +02:00
|
|
|
|
psa_status_t psa_pake_setup(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
const psa_pake_cipher_suite_t *cipher_suite)
|
|
|
|
|
{
|
2023-02-08 09:12:42 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2023-01-26 10:35:02 +01:00
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 19:51:24 +01:00
|
|
|
|
if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
|
2022-12-07 11:04:51 +01:00
|
|
|
|
PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
|
|
|
|
|
|
2022-12-21 12:54:46 +01:00
|
|
|
|
operation->alg = cipher_suite->algorithm;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
operation->data.inputs.cipher_suite = *cipher_suite;
|
|
|
|
|
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
if (operation->alg == PSA_ALG_JPAKE) {
|
2022-12-22 13:34:47 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
2023-01-26 08:46:37 +01:00
|
|
|
|
&operation->computation_stage.jpake;
|
2022-12-22 13:34:47 +01:00
|
|
|
|
|
2022-12-21 12:54:46 +01:00
|
|
|
|
computation_stage->state = PSA_PAKE_STATE_SETUP;
|
|
|
|
|
computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
|
|
|
|
|
computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
|
|
|
|
|
computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
|
2023-02-20 13:32:22 +01:00
|
|
|
|
} else
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2023-02-20 13:32:22 +01:00
|
|
|
|
{
|
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 10:35:02 +01:00
|
|
|
|
operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
|
|
|
|
|
|
2022-12-07 11:04:51 +01:00
|
|
|
|
return PSA_SUCCESS;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_pake_set_password_key(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
mbedtls_svc_key_id_t password)
|
|
|
|
|
{
|
2022-09-15 11:29:46 +02:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-11-22 14:05:12 +01:00
|
|
|
|
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
psa_key_slot_t *slot = NULL;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
|
2022-12-07 11:04:51 +01:00
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 14:05:12 +01:00
|
|
|
|
status = psa_get_and_lock_key_slot_with_policy(password, &slot,
|
|
|
|
|
PSA_KEY_USAGE_DERIVE,
|
2023-01-26 10:46:05 +01:00
|
|
|
|
operation->alg);
|
2022-09-15 11:29:46 +02:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 14:05:12 +01:00
|
|
|
|
psa_key_attributes_t attributes = {
|
|
|
|
|
.core = slot->attr
|
|
|
|
|
};
|
2022-09-15 11:29:46 +02:00
|
|
|
|
|
2022-12-07 11:04:51 +01:00
|
|
|
|
psa_key_type_t type = psa_get_key_type(&attributes);
|
|
|
|
|
|
|
|
|
|
if (type != PSA_KEY_TYPE_PASSWORD &&
|
|
|
|
|
type != PSA_KEY_TYPE_PASSWORD_HASH) {
|
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
|
|
|
|
|
if (operation->data.inputs.password == NULL) {
|
2022-12-21 12:54:46 +01:00
|
|
|
|
status = PSA_ERROR_INSUFFICIENT_MEMORY;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
2022-09-15 11:29:46 +02:00
|
|
|
|
|
2022-12-07 11:04:51 +01:00
|
|
|
|
memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
|
|
|
|
|
operation->data.inputs.password_len = slot->key.bytes;
|
2023-01-26 15:06:09 +01:00
|
|
|
|
operation->data.inputs.attributes = attributes;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
}
|
2023-02-08 09:12:42 +01:00
|
|
|
|
unlock_status = psa_unlock_key_slot(slot);
|
2023-02-27 10:20:06 +01:00
|
|
|
|
return (status == PSA_SUCCESS) ? unlock_status : status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_pake_set_user(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
const uint8_t *user_id,
|
|
|
|
|
size_t user_id_len)
|
|
|
|
|
{
|
2023-02-08 09:12:42 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
|
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:04:51 +01:00
|
|
|
|
if (user_id_len == 0) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 09:54:45 +01:00
|
|
|
|
if (operation->data.inputs.user_len != 0) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 13:39:52 +01:00
|
|
|
|
/* Allow only "client" or "server" values (temporary restriction). */
|
2023-03-13 16:06:09 +01:00
|
|
|
|
if ((user_id_len != sizeof(jpake_server_id) ||
|
|
|
|
|
memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
|
|
|
|
|
(user_id_len != sizeof(jpake_client_id) ||
|
|
|
|
|
memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 09:54:45 +01:00
|
|
|
|
operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
|
|
|
|
|
if (operation->data.inputs.user == NULL) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 09:54:45 +01:00
|
|
|
|
memcpy(operation->data.inputs.user, user_id, user_id_len);
|
|
|
|
|
operation->data.inputs.user_len = user_id_len;
|
2023-02-28 12:34:03 +01:00
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_pake_set_peer(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
const uint8_t *peer_id,
|
|
|
|
|
size_t peer_id_len)
|
|
|
|
|
{
|
2023-02-08 09:12:42 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
|
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (peer_id_len == 0) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 09:54:45 +01:00
|
|
|
|
if (operation->data.inputs.peer_len != 0) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 13:39:52 +01:00
|
|
|
|
/* Allow only "client" or "server" values (temporary restriction). */
|
2023-03-13 16:06:09 +01:00
|
|
|
|
if ((peer_id_len != sizeof(jpake_server_id) ||
|
|
|
|
|
memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
|
|
|
|
|
(peer_id_len != sizeof(jpake_client_id) ||
|
|
|
|
|
memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 09:54:45 +01:00
|
|
|
|
operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
|
|
|
|
|
if (operation->data.inputs.peer == NULL) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 09:54:45 +01:00
|
|
|
|
memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
|
|
|
|
|
operation->data.inputs.peer_len = peer_id_len;
|
2023-02-28 12:34:03 +01:00
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_pake_set_role(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
psa_pake_role_t role)
|
|
|
|
|
{
|
2023-02-08 09:12:42 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
2022-12-07 11:04:51 +01:00
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 14:11:51 +01:00
|
|
|
|
switch (operation->alg) {
|
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
|
|
|
|
case PSA_ALG_JPAKE:
|
|
|
|
|
if (role == PSA_PAKE_ROLE_NONE) {
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
default:
|
|
|
|
|
(void) role;
|
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
2023-02-08 09:12:42 +01:00
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 12:05:38 +01:00
|
|
|
|
/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2023-02-17 14:30:50 +01:00
|
|
|
|
static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
|
2023-01-26 08:46:37 +01:00
|
|
|
|
psa_jpake_computation_stage_t *stage)
|
2023-01-17 12:05:38 +01:00
|
|
|
|
{
|
2023-01-26 08:46:37 +01:00
|
|
|
|
switch (stage->state) {
|
2023-01-17 12:05:38 +01:00
|
|
|
|
case PSA_PAKE_OUTPUT_X1_X2:
|
|
|
|
|
case PSA_PAKE_INPUT_X1_X2:
|
2023-01-26 08:46:37 +01:00
|
|
|
|
switch (stage->sequence) {
|
2023-01-17 12:05:38 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
|
|
|
|
return PSA_JPAKE_X1_STEP_KEY_SHARE;
|
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
|
|
|
|
return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
|
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
|
|
|
|
return PSA_JPAKE_X1_STEP_ZK_PROOF;
|
|
|
|
|
case PSA_PAKE_X2_STEP_KEY_SHARE:
|
|
|
|
|
return PSA_JPAKE_X2_STEP_KEY_SHARE;
|
|
|
|
|
case PSA_PAKE_X2_STEP_ZK_PUBLIC:
|
|
|
|
|
return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
|
|
|
|
|
case PSA_PAKE_X2_STEP_ZK_PROOF:
|
|
|
|
|
return PSA_JPAKE_X2_STEP_ZK_PROOF;
|
|
|
|
|
default:
|
|
|
|
|
return PSA_JPAKE_STEP_INVALID;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PSA_PAKE_OUTPUT_X2S:
|
2023-01-26 08:46:37 +01:00
|
|
|
|
switch (stage->sequence) {
|
2023-01-17 12:05:38 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
|
|
|
|
return PSA_JPAKE_X2S_STEP_KEY_SHARE;
|
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
|
|
|
|
return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
|
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
|
|
|
|
return PSA_JPAKE_X2S_STEP_ZK_PROOF;
|
2023-03-01 12:21:26 +01:00
|
|
|
|
default:
|
2023-01-17 12:05:38 +01:00
|
|
|
|
return PSA_JPAKE_STEP_INVALID;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PSA_PAKE_INPUT_X4S:
|
2023-01-26 08:46:37 +01:00
|
|
|
|
switch (stage->sequence) {
|
2023-01-17 12:05:38 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
|
|
|
|
return PSA_JPAKE_X4S_STEP_KEY_SHARE;
|
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
|
|
|
|
return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
|
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
|
|
|
|
return PSA_JPAKE_X4S_STEP_ZK_PROOF;
|
2023-03-01 12:21:26 +01:00
|
|
|
|
default:
|
2023-01-17 12:05:38 +01:00
|
|
|
|
return PSA_JPAKE_STEP_INVALID;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return PSA_JPAKE_STEP_INVALID;
|
|
|
|
|
}
|
|
|
|
|
return PSA_JPAKE_STEP_INVALID;
|
|
|
|
|
}
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2023-01-17 12:05:38 +01:00
|
|
|
|
|
2022-12-22 11:19:22 +01:00
|
|
|
|
static psa_status_t psa_pake_complete_inputs(
|
|
|
|
|
psa_pake_operation_t *operation)
|
|
|
|
|
{
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-17 16:34:52 +01:00
|
|
|
|
/* Create copy of the inputs on stack as inputs share memory
|
|
|
|
|
with the driver context which will be setup by the driver. */
|
|
|
|
|
psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
|
2022-12-22 11:19:22 +01:00
|
|
|
|
|
2023-03-13 16:06:09 +01:00
|
|
|
|
if (inputs.password_len == 0) {
|
2022-12-22 11:19:22 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 12:34:03 +01:00
|
|
|
|
if (operation->alg == PSA_ALG_JPAKE) {
|
2023-03-13 16:06:09 +01:00
|
|
|
|
if (inputs.user_len == 0 || inputs.peer_len == 0) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
|
|
|
|
|
memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
inputs.role = PSA_PAKE_ROLE_CLIENT;
|
|
|
|
|
} else
|
2023-03-13 16:06:09 +01:00
|
|
|
|
if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
|
|
|
|
|
memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
|
2023-02-28 12:34:03 +01:00
|
|
|
|
inputs.role = PSA_PAKE_ROLE_SERVER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
|
|
|
|
|
inputs.role != PSA_PAKE_ROLE_SERVER) {
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2023-02-14 20:09:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 16:34:52 +01:00
|
|
|
|
/* Clear driver context */
|
|
|
|
|
mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
|
|
|
|
|
|
|
|
|
|
status = psa_driver_wrapper_pake_setup(operation, &inputs);
|
2022-12-22 11:19:22 +01:00
|
|
|
|
|
|
|
|
|
/* Driver is responsible for creating its own copy of the password. */
|
2023-01-31 19:51:24 +01:00
|
|
|
|
mbedtls_platform_zeroize(inputs.password, inputs.password_len);
|
|
|
|
|
mbedtls_free(inputs.password);
|
2022-12-22 11:19:22 +01:00
|
|
|
|
|
2023-02-28 12:34:03 +01:00
|
|
|
|
/* User and peer are translated to role. */
|
|
|
|
|
mbedtls_free(inputs.user);
|
|
|
|
|
mbedtls_free(inputs.peer);
|
|
|
|
|
|
2022-12-22 11:19:22 +01:00
|
|
|
|
if (status == PSA_SUCCESS) {
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-22 11:19:22 +01:00
|
|
|
|
if (operation->alg == PSA_ALG_JPAKE) {
|
2023-02-24 08:39:04 +01:00
|
|
|
|
operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
|
2023-01-31 19:51:24 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
|
|
|
|
&operation->computation_stage.jpake;
|
2022-12-22 11:19:22 +01:00
|
|
|
|
computation_stage->state = PSA_PAKE_STATE_READY;
|
|
|
|
|
computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
|
|
|
|
|
computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
|
|
|
|
|
computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
|
2023-02-20 13:32:22 +01:00
|
|
|
|
} else
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2023-02-20 13:32:22 +01:00
|
|
|
|
{
|
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2022-12-22 11:19:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
static psa_status_t psa_jpake_output_prologue(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
psa_pake_step_t step)
|
|
|
|
|
{
|
|
|
|
|
if (step != PSA_PAKE_STEP_KEY_SHARE &&
|
|
|
|
|
step != PSA_PAKE_STEP_ZK_PUBLIC &&
|
|
|
|
|
step != PSA_PAKE_STEP_ZK_PROOF) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
|
|
|
|
&operation->computation_stage.jpake;
|
|
|
|
|
|
|
|
|
|
if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (computation_stage->state != PSA_PAKE_STATE_READY &&
|
|
|
|
|
computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
|
|
|
|
|
computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
if (computation_stage->state == PSA_PAKE_STATE_READY) {
|
|
|
|
|
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
2022-12-21 12:54:46 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
switch (computation_stage->output_step) {
|
|
|
|
|
case PSA_PAKE_STEP_X1_X2:
|
|
|
|
|
computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
|
|
|
|
|
break;
|
|
|
|
|
case PSA_PAKE_STEP_X2S:
|
|
|
|
|
computation_stage->state = PSA_PAKE_OUTPUT_X2S;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if step matches current sequence */
|
|
|
|
|
switch (computation_stage->sequence) {
|
|
|
|
|
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
|
|
|
|
case PSA_PAKE_X2_STEP_KEY_SHARE:
|
2022-12-21 12:54:46 +01:00
|
|
|
|
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2023-02-21 19:10:36 +01:00
|
|
|
|
break;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
|
|
|
|
case PSA_PAKE_X2_STEP_ZK_PUBLIC:
|
|
|
|
|
if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
2023-02-21 19:10:36 +01:00
|
|
|
|
break;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
|
|
|
|
case PSA_PAKE_X2_STEP_ZK_PROOF:
|
|
|
|
|
if (step != PSA_PAKE_STEP_ZK_PROOF) {
|
2023-02-20 13:32:22 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2023-02-21 19:10:36 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static psa_status_t psa_jpake_output_epilogue(
|
|
|
|
|
psa_pake_operation_t *operation)
|
|
|
|
|
{
|
2023-02-21 19:10:36 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
|
|
|
|
&operation->computation_stage.jpake;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
|
2023-02-23 17:28:23 +01:00
|
|
|
|
computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
|
2023-02-21 19:10:36 +01:00
|
|
|
|
(computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
|
2023-02-23 17:28:23 +01:00
|
|
|
|
computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
|
2023-02-21 19:10:36 +01:00
|
|
|
|
computation_stage->state = PSA_PAKE_STATE_READY;
|
|
|
|
|
computation_stage->output_step++;
|
|
|
|
|
computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
|
|
|
|
|
} else {
|
|
|
|
|
computation_stage->sequence++;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2022-06-01 18:21:20 +02:00
|
|
|
|
psa_status_t psa_pake_output(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
psa_pake_step_t step,
|
|
|
|
|
uint8_t *output,
|
|
|
|
|
size_t output_size,
|
|
|
|
|
size_t *output_length)
|
|
|
|
|
{
|
2022-12-07 11:04:51 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-03-07 16:26:37 +01:00
|
|
|
|
psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
|
2023-01-31 19:51:24 +01:00
|
|
|
|
*output_length = 0;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
|
|
|
|
|
if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
2022-12-22 11:19:22 +01:00
|
|
|
|
status = psa_pake_complete_inputs(operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 19:51:24 +01:00
|
|
|
|
if (output_size == 0) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 12:54:46 +01:00
|
|
|
|
switch (operation->alg) {
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
case PSA_ALG_JPAKE:
|
|
|
|
|
status = psa_jpake_output_prologue(operation, step);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
2023-03-07 16:26:37 +01:00
|
|
|
|
driver_step = convert_jpake_computation_stage_to_driver_step(
|
|
|
|
|
&operation->computation_stage.jpake);
|
2022-12-21 12:54:46 +01:00
|
|
|
|
break;
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-12-21 12:54:46 +01:00
|
|
|
|
default:
|
2023-02-20 13:32:22 +01:00
|
|
|
|
(void) step;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 16:26:37 +01:00
|
|
|
|
status = psa_driver_wrapper_pake_output(operation, driver_step,
|
|
|
|
|
output, output_size, output_length);
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (operation->alg) {
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
case PSA_ALG_JPAKE:
|
|
|
|
|
status = psa_jpake_output_epilogue(operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-12-21 12:54:46 +01:00
|
|
|
|
default:
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 09:12:42 +01:00
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
static psa_status_t psa_jpake_input_prologue(
|
|
|
|
|
psa_pake_operation_t *operation,
|
2023-03-07 16:26:37 +01:00
|
|
|
|
psa_pake_step_t step)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
{
|
|
|
|
|
if (step != PSA_PAKE_STEP_KEY_SHARE &&
|
|
|
|
|
step != PSA_PAKE_STEP_ZK_PUBLIC &&
|
|
|
|
|
step != PSA_PAKE_STEP_ZK_PROOF) {
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
|
|
|
|
&operation->computation_stage.jpake;
|
|
|
|
|
|
|
|
|
|
if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (computation_stage->state != PSA_PAKE_STATE_READY &&
|
|
|
|
|
computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
|
|
|
|
|
computation_stage->state != PSA_PAKE_INPUT_X4S) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (computation_stage->state == PSA_PAKE_STATE_READY) {
|
|
|
|
|
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
2022-12-21 12:54:46 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
switch (computation_stage->input_step) {
|
|
|
|
|
case PSA_PAKE_STEP_X1_X2:
|
|
|
|
|
computation_stage->state = PSA_PAKE_INPUT_X1_X2;
|
|
|
|
|
break;
|
|
|
|
|
case PSA_PAKE_STEP_X2S:
|
|
|
|
|
computation_stage->state = PSA_PAKE_INPUT_X4S;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
|
|
|
|
|
}
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
/* Check if step matches current sequence */
|
|
|
|
|
switch (computation_stage->sequence) {
|
|
|
|
|
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
|
|
|
|
case PSA_PAKE_X2_STEP_KEY_SHARE:
|
2022-12-21 12:54:46 +01:00
|
|
|
|
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
|
}
|
2023-02-21 19:10:36 +01:00
|
|
|
|
break;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
|
|
|
|
case PSA_PAKE_X2_STEP_ZK_PUBLIC:
|
|
|
|
|
if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
2023-02-21 19:10:36 +01:00
|
|
|
|
break;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
|
|
|
|
case PSA_PAKE_X2_STEP_ZK_PROOF:
|
|
|
|
|
if (step != PSA_PAKE_STEP_ZK_PROOF) {
|
2023-02-20 13:32:22 +01:00
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2023-02-21 19:10:36 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static psa_status_t psa_jpake_input_epilogue(
|
|
|
|
|
psa_pake_operation_t *operation)
|
|
|
|
|
{
|
2023-02-21 19:10:36 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
|
|
|
|
&operation->computation_stage.jpake;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-02-21 19:10:36 +01:00
|
|
|
|
if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
|
2023-02-23 17:28:23 +01:00
|
|
|
|
computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
|
2023-02-21 19:10:36 +01:00
|
|
|
|
(computation_stage->state == PSA_PAKE_INPUT_X4S &&
|
2023-02-23 17:28:23 +01:00
|
|
|
|
computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
|
2023-02-21 19:10:36 +01:00
|
|
|
|
computation_stage->state = PSA_PAKE_STATE_READY;
|
|
|
|
|
computation_stage->input_step++;
|
|
|
|
|
computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
|
|
|
|
|
} else {
|
|
|
|
|
computation_stage->sequence++;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
}
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2022-06-01 18:21:20 +02:00
|
|
|
|
psa_status_t psa_pake_input(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
psa_pake_step_t step,
|
|
|
|
|
const uint8_t *input,
|
|
|
|
|
size_t input_length)
|
|
|
|
|
{
|
2022-12-07 11:04:51 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-03-07 16:26:37 +01:00
|
|
|
|
psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
|
|
|
|
|
if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
2022-12-22 11:19:22 +01:00
|
|
|
|
status = psa_pake_complete_inputs(operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-12-07 11:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 16:26:37 +01:00
|
|
|
|
if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 12:54:46 +01:00
|
|
|
|
switch (operation->alg) {
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
case PSA_ALG_JPAKE:
|
2023-03-07 16:26:37 +01:00
|
|
|
|
status = psa_jpake_input_prologue(operation, step);
|
2022-12-21 12:54:46 +01:00
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
2023-03-07 16:26:37 +01:00
|
|
|
|
driver_step = convert_jpake_computation_stage_to_driver_step(
|
|
|
|
|
&operation->computation_stage.jpake);
|
2022-12-21 12:54:46 +01:00
|
|
|
|
break;
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-12-21 12:54:46 +01:00
|
|
|
|
default:
|
2023-02-20 13:32:22 +01:00
|
|
|
|
(void) step;
|
2023-02-27 10:20:06 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 16:26:37 +01:00
|
|
|
|
status = psa_driver_wrapper_pake_input(operation, driver_step,
|
|
|
|
|
input, input_length);
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (operation->alg) {
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
case PSA_ALG_JPAKE:
|
|
|
|
|
status = psa_jpake_input_epilogue(operation);
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-12-21 12:54:46 +01:00
|
|
|
|
default:
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 09:12:42 +01:00
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
|
exit:
|
2023-02-27 10:20:06 +01:00
|
|
|
|
psa_pake_abort(operation);
|
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_pake_get_implicit_key(
|
|
|
|
|
psa_pake_operation_t *operation,
|
|
|
|
|
psa_key_derivation_operation_t *output)
|
|
|
|
|
{
|
2023-01-31 19:51:24 +01:00
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-02-08 09:12:42 +01:00
|
|
|
|
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-02-17 14:30:50 +01:00
|
|
|
|
uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
|
2022-11-29 14:53:13 +01:00
|
|
|
|
size_t shared_key_len = 0;
|
|
|
|
|
|
2023-01-31 19:51:24 +01:00
|
|
|
|
if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 13:00:57 +01:00
|
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-12-21 12:54:46 +01:00
|
|
|
|
if (operation->alg == PSA_ALG_JPAKE) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
psa_jpake_computation_stage_t *computation_stage =
|
2023-02-23 17:28:23 +01:00
|
|
|
|
&operation->computation_stage.jpake;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
|
|
|
|
|
computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
2023-02-20 13:32:22 +01:00
|
|
|
|
} else
|
2023-02-27 10:20:06 +01:00
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2023-02-20 13:32:22 +01:00
|
|
|
|
{
|
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
|
goto exit;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 14:53:13 +01:00
|
|
|
|
status = psa_driver_wrapper_pake_get_implicit_key(operation,
|
|
|
|
|
shared_key,
|
2023-02-19 22:55:33 +01:00
|
|
|
|
sizeof(shared_key),
|
2022-11-29 14:53:13 +01:00
|
|
|
|
&shared_key_len);
|
|
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-08 09:12:42 +01:00
|
|
|
|
goto exit;
|
2022-11-29 14:53:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = psa_key_derivation_input_bytes(output,
|
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
|
shared_key,
|
|
|
|
|
shared_key_len);
|
|
|
|
|
|
2023-02-27 10:20:06 +01:00
|
|
|
|
mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
|
2023-02-08 09:12:42 +01:00
|
|
|
|
exit:
|
|
|
|
|
abort_status = psa_pake_abort(operation);
|
|
|
|
|
return status == PSA_SUCCESS ? abort_status : status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psa_status_t psa_pake_abort(
|
|
|
|
|
psa_pake_operation_t *operation)
|
|
|
|
|
{
|
2023-01-31 19:59:20 +01:00
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
2022-12-21 12:54:46 +01:00
|
|
|
|
|
2023-01-31 19:59:20 +01:00
|
|
|
|
if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
|
2022-12-22 13:34:47 +01:00
|
|
|
|
status = psa_driver_wrapper_pake_abort(operation);
|
2022-09-15 11:29:46 +02:00
|
|
|
|
}
|
2022-12-22 13:34:47 +01:00
|
|
|
|
|
2023-02-28 12:34:03 +01:00
|
|
|
|
if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
|
|
|
|
|
if (operation->data.inputs.password != NULL) {
|
|
|
|
|
mbedtls_platform_zeroize(operation->data.inputs.password,
|
2023-03-06 14:21:44 +01:00
|
|
|
|
operation->data.inputs.password_len);
|
2023-02-28 12:34:03 +01:00
|
|
|
|
mbedtls_free(operation->data.inputs.password);
|
|
|
|
|
}
|
|
|
|
|
if (operation->data.inputs.user != NULL) {
|
|
|
|
|
mbedtls_free(operation->data.inputs.user);
|
|
|
|
|
}
|
|
|
|
|
if (operation->data.inputs.peer != NULL) {
|
|
|
|
|
mbedtls_free(operation->data.inputs.peer);
|
|
|
|
|
}
|
2022-12-22 13:34:47 +01:00
|
|
|
|
}
|
2023-01-31 19:59:20 +01:00
|
|
|
|
memset(operation, 0, sizeof(psa_pake_operation_t));
|
2022-12-22 13:34:47 +01:00
|
|
|
|
|
2023-01-31 19:59:20 +01:00
|
|
|
|
return status;
|
2022-06-01 18:21:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 23:32:46 +01:00
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|