Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 29 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | #include "psa/crypto.h" |
| 31 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 32 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 35 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 36 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 37 | #include "psa_crypto_hash.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 38 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 39 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 41 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 42 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 43 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 44 | /* Include internal declarations that are useful for implementing persistently |
| 45 | * stored keys. */ |
| 46 | #include "psa_crypto_storage.h" |
| 47 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 48 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 49 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 50 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 53 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 55 | #define mbedtls_calloc calloc |
| 56 | #define mbedtls_free free |
| 57 | #endif |
| 58 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 59 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 61 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 62 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 63 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/blowfish.h" |
| 65 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 66 | #include "mbedtls/chacha20.h" |
| 67 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/cipher.h" |
| 69 | #include "mbedtls/ccm.h" |
| 70 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 71 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 72 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 73 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 74 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 75 | #include "mbedtls/error.h" |
| 76 | #include "mbedtls/gcm.h" |
| 77 | #include "mbedtls/md2.h" |
| 78 | #include "mbedtls/md4.h" |
| 79 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 80 | #include "mbedtls/md.h" |
| 81 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 82 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 83 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 84 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 85 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 86 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 87 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 88 | #include "mbedtls/sha1.h" |
| 89 | #include "mbedtls/sha256.h" |
| 90 | #include "mbedtls/sha512.h" |
| 91 | #include "mbedtls/xtea.h" |
| 92 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 93 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 94 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 95 | /* constant-time buffer comparison */ |
| 96 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 97 | { |
| 98 | size_t i; |
| 99 | unsigned char diff = 0; |
| 100 | |
| 101 | for( i = 0; i < n; i++ ) |
| 102 | diff |= a[i] ^ b[i]; |
| 103 | |
| 104 | return( diff ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 109 | /****************************************************************/ |
| 110 | /* Global data, support functions and library management */ |
| 111 | /****************************************************************/ |
| 112 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 113 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 114 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 115 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 118 | /* Values for psa_global_data_t::rng_state */ |
| 119 | #define RNG_NOT_INITIALIZED 0 |
| 120 | #define RNG_INITIALIZED 1 |
| 121 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 122 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 123 | typedef struct |
| 124 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 125 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 126 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 127 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 128 | } psa_global_data_t; |
| 129 | |
| 130 | static psa_global_data_t global_data; |
| 131 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 132 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 133 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 134 | &global_data.rng.drbg; |
| 135 | #endif |
| 136 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 137 | #define GUARD_MODULE_INITIALIZED \ |
| 138 | if( global_data.initialized == 0 ) \ |
| 139 | return( PSA_ERROR_BAD_STATE ); |
| 140 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 141 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 142 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 143 | /* Mbed TLS error codes can combine a high-level error code and a |
| 144 | * low-level error code. The low-level error usually reflects the |
| 145 | * root cause better, so dispatch on that preferably. */ |
| 146 | int low_level_ret = - ( -ret & 0x007f ); |
| 147 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 148 | { |
| 149 | case 0: |
| 150 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 151 | |
| 152 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 153 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 154 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 155 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 156 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 157 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 158 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 159 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 160 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 161 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 162 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 163 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 164 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 165 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 166 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 167 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 168 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 169 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 171 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 173 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 174 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 175 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 176 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 177 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 178 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 179 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 180 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 181 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 182 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 183 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 184 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 185 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 186 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 187 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 188 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 189 | |
| 190 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 191 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 192 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 193 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 194 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 195 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 196 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 197 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 198 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 199 | |
| 200 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 201 | return( PSA_ERROR_BAD_STATE ); |
| 202 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 203 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 204 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 205 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 206 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 207 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 208 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 209 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 210 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 211 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 212 | return( PSA_ERROR_INVALID_PADDING ); |
| 213 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 214 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 215 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 216 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 217 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 218 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 219 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 220 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 221 | |
| 222 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 223 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 224 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 225 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 226 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 227 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 228 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 230 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 231 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 232 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 233 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 234 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 235 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 236 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | |
| 238 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 239 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 240 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 241 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 242 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 243 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 244 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 245 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 246 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 247 | |
| 248 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 249 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 250 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 252 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 253 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 254 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 255 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 256 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 257 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 258 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 259 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 260 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 261 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 262 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 263 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 264 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 265 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 266 | #endif |
| 267 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 268 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 269 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 270 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 271 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 272 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 274 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 275 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 277 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 278 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 279 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 281 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 283 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 284 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 285 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 287 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 289 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 292 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 293 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 294 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 295 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 296 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 297 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 298 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 299 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 300 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 301 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 302 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 303 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 304 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 305 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 306 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 307 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 308 | return( PSA_ERROR_NOT_PERMITTED ); |
| 309 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 310 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 311 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 312 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 313 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 314 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 315 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 316 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 317 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 318 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 319 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 320 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 321 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 322 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 323 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 324 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 325 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 326 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 327 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 328 | return( PSA_ERROR_INVALID_PADDING ); |
| 329 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 330 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 331 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 333 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 334 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 335 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 336 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 337 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 338 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 339 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 340 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 341 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 342 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 343 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 344 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 345 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 346 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 347 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 348 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 349 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 350 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 351 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 352 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 353 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 355 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 356 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 357 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 358 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 359 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 360 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 361 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 362 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 363 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 364 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 365 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 366 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 367 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 368 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 369 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 370 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 371 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 372 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 373 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 377 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 378 | |
| 379 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 380 | /****************************************************************/ |
| 381 | /* Key management */ |
| 382 | /****************************************************************/ |
| 383 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 384 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 385 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 386 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 387 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 388 | } |
| 389 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 390 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 391 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 392 | * current test driver in key_management.c is using this function |
| 393 | * when accelerators are used for ECC key pair and public key. |
| 394 | * Once that dependency is resolved these guards can be removed. |
| 395 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 396 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 397 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 398 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 399 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 400 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 401 | size_t bits, |
| 402 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 403 | { |
| 404 | switch( curve ) |
| 405 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 406 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 407 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 409 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 410 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 411 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 412 | #endif |
| 413 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 414 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 415 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 416 | #endif |
| 417 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 418 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 419 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 420 | #endif |
| 421 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 422 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 424 | #endif |
| 425 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 426 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 427 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 428 | case 528: |
| 429 | if( bits_is_sloppy ) |
| 430 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 431 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 432 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 433 | } |
| 434 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 435 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 436 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 437 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 438 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 439 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 440 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 441 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 442 | #endif |
| 443 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 444 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 445 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 446 | #endif |
| 447 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 448 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 450 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 451 | } |
| 452 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 453 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 454 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 455 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 456 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 457 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 458 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 459 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 460 | case 256: |
| 461 | if( bits_is_sloppy ) |
| 462 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 463 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 464 | #endif |
| 465 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 466 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 467 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 468 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 469 | } |
| 470 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 471 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 472 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 473 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 474 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 475 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 476 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 477 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 478 | #endif |
| 479 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 480 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 481 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 482 | #endif |
| 483 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 484 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 485 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 486 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 487 | } |
| 488 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 489 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 490 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 491 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 492 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 493 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 494 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 495 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 496 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 497 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 498 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 499 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 500 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 501 | { |
| 502 | /* Check that the bit size is acceptable for the key type */ |
| 503 | switch( type ) |
| 504 | { |
| 505 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 506 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 507 | case PSA_KEY_TYPE_DERIVE: |
| 508 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 509 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 510 | case PSA_KEY_TYPE_AES: |
| 511 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 512 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 513 | break; |
| 514 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 515 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 516 | case PSA_KEY_TYPE_CAMELLIA: |
| 517 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 519 | break; |
| 520 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 521 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 522 | case PSA_KEY_TYPE_DES: |
| 523 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 525 | break; |
| 526 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 527 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 528 | case PSA_KEY_TYPE_ARC4: |
| 529 | if( bits < 8 || bits > 2048 ) |
| 530 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 531 | break; |
| 532 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 533 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 534 | case PSA_KEY_TYPE_CHACHA20: |
| 535 | if( bits != 256 ) |
| 536 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 537 | break; |
| 538 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 539 | default: |
| 540 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 541 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 542 | if( bits % 8 != 0 ) |
| 543 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 544 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 545 | return( PSA_SUCCESS ); |
| 546 | } |
| 547 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 548 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 549 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 550 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 551 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 552 | * |
| 553 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 554 | * \param[in] key_type The key type of the key to be used with the |
| 555 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 556 | * |
| 557 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 558 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 559 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 560 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 561 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 562 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 563 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 564 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 565 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 566 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 567 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 568 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 569 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 570 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 571 | |
| 572 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 573 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 574 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 575 | * key. */ |
| 576 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 577 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 578 | { |
| 579 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 580 | * the block length (larger than 1) for block ciphers. */ |
| 581 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 582 | return( PSA_SUCCESS ); |
| 583 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 584 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 585 | |
| 586 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 589 | /** Try to allocate a buffer to an empty key slot. |
| 590 | * |
| 591 | * \param[in,out] slot Key slot to attach buffer to. |
| 592 | * \param[in] buffer_length Requested size of the buffer. |
| 593 | * |
| 594 | * \retval #PSA_SUCCESS |
| 595 | * The buffer has been successfully allocated. |
| 596 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 597 | * Not enough memory was available for allocation. |
| 598 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 599 | * Trying to allocate a buffer to a non-empty key slot. |
| 600 | */ |
| 601 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 602 | size_t buffer_length ) |
| 603 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 604 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 605 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 606 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 607 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 608 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 609 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 610 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 611 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 612 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 615 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 616 | const uint8_t* data, |
| 617 | size_t data_length ) |
| 618 | { |
| 619 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 620 | data_length ); |
| 621 | if( status != PSA_SUCCESS ) |
| 622 | return( status ); |
| 623 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 624 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 625 | return( PSA_SUCCESS ); |
| 626 | } |
| 627 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 628 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 629 | const psa_key_attributes_t *attributes, |
| 630 | const uint8_t *data, size_t data_length, |
| 631 | uint8_t *key_buffer, size_t key_buffer_size, |
| 632 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 633 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 634 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 635 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 636 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 637 | /* zero-length keys are never supported. */ |
| 638 | if( data_length == 0 ) |
| 639 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 640 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 641 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 642 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 643 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 644 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 645 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 646 | if( data_length > SIZE_MAX / 8 ) |
| 647 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 648 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 649 | /* Enforce a size limit, and in particular ensure that the bit |
| 650 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 651 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 652 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 653 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 654 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 655 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 656 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 657 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 658 | /* Copy the key material. */ |
| 659 | memcpy( key_buffer, data, data_length ); |
| 660 | *key_buffer_length = data_length; |
| 661 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 662 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 663 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 664 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 665 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 666 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 667 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 668 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 669 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 670 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 671 | return( mbedtls_psa_ecp_import_key( attributes, |
| 672 | data, data_length, |
| 673 | key_buffer, key_buffer_size, |
| 674 | key_buffer_length, |
| 675 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 676 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 677 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 678 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 679 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 680 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 681 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 682 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 683 | return( mbedtls_psa_rsa_import_key( attributes, |
| 684 | data, data_length, |
| 685 | key_buffer, key_buffer_size, |
| 686 | key_buffer_length, |
| 687 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 688 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 689 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 690 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 691 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 692 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 693 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 696 | /** Calculate the intersection of two algorithm usage policies. |
| 697 | * |
| 698 | * Return 0 (which allows no operation) on incompatibility. |
| 699 | */ |
| 700 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 701 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 702 | psa_algorithm_t alg1, |
| 703 | psa_algorithm_t alg2 ) |
| 704 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 705 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 706 | if( alg1 == alg2 ) |
| 707 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 708 | /* If the policies are from the same hash-and-sign family, check |
| 709 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 710 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 711 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 712 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 713 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 714 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 715 | return( alg2 ); |
| 716 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 717 | return( alg1 ); |
| 718 | } |
| 719 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 720 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 721 | * restrictive tag length. */ |
| 722 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 723 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 724 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 725 | { |
| 726 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 727 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 728 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 729 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 730 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 731 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 732 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 733 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 734 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 735 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 736 | } |
| 737 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 738 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 739 | ( alg1_len <= alg2_len ) ) |
| 740 | { |
| 741 | return( alg2 ); |
| 742 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 743 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 744 | ( alg2_len <= alg1_len ) ) |
| 745 | { |
| 746 | return( alg1 ); |
| 747 | } |
| 748 | } |
| 749 | /* If the policies are from the same MAC family, check whether one |
| 750 | * of them is a minimum-MAC-length policy. Calculate the most |
| 751 | * restrictive tag length. */ |
| 752 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 753 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 754 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 755 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 756 | /* Validate the combination of key type and algorithm. Since the base |
| 757 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 758 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 759 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 760 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 761 | /* Get the (exact or at-least) output lengths for both sides of the |
| 762 | * requested intersection. None of the currently supported algorithms |
| 763 | * have an output length dependent on the actual key size, so setting it |
| 764 | * to a bogus value of 0 is currently OK. |
| 765 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 766 | * Note that for at-least-this-length wildcard algorithms, the output |
| 767 | * length is set to the shortest allowed length, which allows us to |
| 768 | * calculate the most restrictive tag length for the intersection. */ |
| 769 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 770 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 771 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 772 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 773 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 774 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 775 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 776 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 777 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 778 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 779 | |
| 780 | /* If only one is an at-least-this-length policy, the intersection would |
| 781 | * be the other (fixed-length) policy as long as said fixed length is |
| 782 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 783 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 784 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 785 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 786 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 787 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 788 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 789 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 790 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 791 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 792 | /* If none of them are wildcards, check whether they define the same tag |
| 793 | * length. This is still possible here when one is default-length and |
| 794 | * the other specific-length. Ensure to always return the |
| 795 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 796 | if( alg1_len == alg2_len ) |
| 797 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 798 | } |
| 799 | /* If the policies are incompatible, allow nothing. */ |
| 800 | return( 0 ); |
| 801 | } |
| 802 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 803 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 804 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 805 | psa_algorithm_t requested_alg ) |
| 806 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 807 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 808 | if( requested_alg == policy_alg ) |
| 809 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 810 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 811 | * and requested_alg is the same hash-and-sign family with any hash, |
| 812 | * then requested_alg is compliant with policy_alg. */ |
| 813 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 814 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 815 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 816 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 817 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 818 | } |
| 819 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 820 | * the requested algorithm, check the requested tag length to be |
| 821 | * equal-length or longer than the wildcard-specified length. */ |
| 822 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 823 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 824 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 825 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 826 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 827 | { |
| 828 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 829 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 830 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 831 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 832 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 833 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 834 | PSA_ALG_IS_MAC( requested_alg ) && |
| 835 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 836 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 837 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 838 | /* Validate the combination of key type and algorithm. Since the policy |
| 839 | * and requested algorithms are the same, we only need this once. */ |
| 840 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 841 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 842 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 843 | /* Get both the requested output length for the algorithm which is to be |
| 844 | * verified, and the default output length for the base algorithm. |
| 845 | * Note that none of the currently supported algorithms have an output |
| 846 | * length dependent on actual key size, so setting it to a bogus value |
| 847 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 848 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 849 | key_type, 0, requested_alg ); |
| 850 | size_t default_output_length = PSA_MAC_LENGTH( |
| 851 | key_type, 0, |
| 852 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 853 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 854 | /* If the policy is default-length, only allow an algorithm with |
| 855 | * a declared exact-length matching the default. */ |
| 856 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 857 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 858 | |
| 859 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 860 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 861 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 862 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 863 | { |
| 864 | return( 1 ); |
| 865 | } |
| 866 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 867 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 868 | * check for the requested MAC length to be equal to or longer than the |
| 869 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 870 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 871 | { |
| 872 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 873 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 874 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 875 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 876 | /* If policy_alg is a generic key agreement operation, then using it for |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 877 | * a key derivation with that key agreement should also be allowed. This |
| 878 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 879 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 880 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 881 | { |
| 882 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 883 | policy_alg ); |
| 884 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 885 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 886 | return( 0 ); |
| 887 | } |
| 888 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 889 | /** Test whether a policy permits an algorithm. |
| 890 | * |
| 891 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 892 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 893 | * \note This function requires providing the key type for which the policy is |
| 894 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 895 | * have different properties depending on what kind of cipher it is |
| 896 | * combined with. |
| 897 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 898 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 899 | * allowed by the \p policy. |
| 900 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 901 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 902 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 903 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 904 | static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy, |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 905 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 906 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 907 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 908 | /* '0' is not a valid algorithm */ |
| 909 | if( alg == 0 ) |
| 910 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 911 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 912 | /* A requested algorithm cannot be a wildcard. */ |
| 913 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 914 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 915 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 916 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 917 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 918 | return( PSA_SUCCESS ); |
| 919 | else |
| 920 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 923 | /** Restrict a key policy based on a constraint. |
| 924 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 925 | * \note This function requires providing the key type for which the policy is |
| 926 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 927 | * have different properties depending on what kind of cipher it is |
| 928 | * combined with. |
| 929 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 930 | * \param[in] key_type The key type for which to restrict the policy |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 931 | * \param[in,out] policy The policy to restrict. |
| 932 | * \param[in] constraint The policy constraint to apply. |
| 933 | * |
| 934 | * \retval #PSA_SUCCESS |
| 935 | * \c *policy contains the intersection of the original value of |
| 936 | * \c *policy and \c *constraint. |
| 937 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 938 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 939 | * \c *policy is unchanged. |
| 940 | */ |
| 941 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 942 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 943 | psa_key_policy_t *policy, |
| 944 | const psa_key_policy_t *constraint ) |
| 945 | { |
| 946 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 947 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 948 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 949 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 950 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 951 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 952 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 953 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 954 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 955 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 956 | policy->usage &= constraint->usage; |
| 957 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 958 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 959 | return( PSA_SUCCESS ); |
| 960 | } |
| 961 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 962 | /** Get the description of a key given its identifier and policy constraints |
| 963 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 964 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 965 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 966 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 967 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 968 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 969 | * In case of a persistent key, the function loads the description of the key |
| 970 | * into a key slot if not already done. |
| 971 | * |
| 972 | * On success, the returned key slot is locked. It is the responsibility of |
| 973 | * the caller to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 974 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 975 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 976 | mbedtls_svc_key_id_t key, |
| 977 | psa_key_slot_t **p_slot, |
| 978 | psa_key_usage_t usage, |
| 979 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 980 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 981 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 982 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 983 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 984 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 985 | if( status != PSA_SUCCESS ) |
| 986 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 987 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 988 | |
| 989 | /* Enforce that usage policy for the key slot contains all the flags |
| 990 | * required by the usage parameter. There is one exception: public |
| 991 | * keys can always be exported, so we treat public key objects as |
| 992 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 993 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 994 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 995 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 996 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 997 | { |
| 998 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 999 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1000 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1001 | |
| 1002 | /* Enforce that the usage policy permits the requested algortihm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1003 | if( alg != 0 ) |
| 1004 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1005 | status = psa_key_policy_permits( &slot->attr.policy, |
| 1006 | slot->attr.type, |
| 1007 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1008 | if( status != PSA_SUCCESS ) |
| 1009 | goto error; |
| 1010 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1011 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1012 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1013 | |
| 1014 | error: |
| 1015 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1016 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1017 | |
| 1018 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1019 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1020 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1021 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1022 | * |
| 1023 | * A transparent key is a key for which the key material is directly |
| 1024 | * available, as opposed to a key in a secure element. |
| 1025 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1026 | * This is a temporary function to use instead of |
| 1027 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1028 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1029 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1030 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1031 | * caller to unlock the key slot when it does not access it anymore. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1032 | */ |
| 1033 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1034 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1035 | mbedtls_svc_key_id_t key, |
| 1036 | psa_key_slot_t **p_slot, |
| 1037 | psa_key_usage_t usage, |
| 1038 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1039 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1040 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1041 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1042 | if( status != PSA_SUCCESS ) |
| 1043 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1044 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1045 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1046 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1047 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1048 | *p_slot = NULL; |
| 1049 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1050 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1051 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1052 | return( PSA_SUCCESS ); |
| 1053 | } |
| 1054 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1055 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1056 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1057 | psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1058 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1059 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1060 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1061 | static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1062 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1063 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1064 | * initialized slot, so we can just free it. */ |
| 1065 | if( slot->key.data != NULL ) |
| 1066 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1067 | |
| 1068 | mbedtls_free( slot->key.data ); |
| 1069 | slot->key.data = NULL; |
| 1070 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1071 | |
| 1072 | return( PSA_SUCCESS ); |
| 1073 | } |
| 1074 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1075 | /** Completely wipe a slot in memory, including its policy. |
| 1076 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1077 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1078 | { |
| 1079 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1080 | |
| 1081 | /* |
| 1082 | * As the return error code may not be handled in case of multiple errors, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1083 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1084 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1085 | * part of the execution of a test suite this will stop the test suite |
Ronald Cron | 4640c15 | 2020-11-13 10:11:01 +0100 | [diff] [blame] | 1086 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1087 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1088 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1089 | { |
| 1090 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1091 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1092 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1093 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1094 | } |
| 1095 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1096 | /* Multipart operations may still be using the key. This is safe |
| 1097 | * because all multipart operation objects are independent from |
| 1098 | * the key slot: if they need to access the key after the setup |
| 1099 | * phase, they have a copy of the key. Note that this means that |
| 1100 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1101 | /* At this point, key material and other type-specific content has |
| 1102 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1103 | * zeroize because the metadata is not particularly sensitive. */ |
| 1104 | memset( slot, 0, sizeof( *slot ) ); |
| 1105 | return( status ); |
| 1106 | } |
| 1107 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1108 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1109 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1110 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1111 | psa_status_t status; /* status of the last operation */ |
| 1112 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1113 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1114 | psa_se_drv_table_entry_t *driver; |
| 1115 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1116 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1117 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1118 | return( PSA_SUCCESS ); |
| 1119 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1120 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1121 | * Get the description of the key in a key slot. In case of a persistent |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1122 | * key, this will load the key description from persistent memory if not |
| 1123 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1124 | * the key is operated by an SE or not and this information is needed by |
| 1125 | * the current implementation. |
| 1126 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1127 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1128 | if( status != PSA_SUCCESS ) |
| 1129 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1130 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1131 | /* |
| 1132 | * If the key slot containing the key description is under access by the |
| 1133 | * library (apart from the present access), the key cannot be destroyed |
| 1134 | * yet. For the time being, just return in error. Eventually (to be |
| 1135 | * implemented), the key should be destroyed when all accesses have |
| 1136 | * stopped. |
| 1137 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1138 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1139 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1140 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1141 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1142 | } |
| 1143 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1144 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1145 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1146 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1147 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1148 | /* For a key in a secure element, we need to do three things: |
| 1149 | * remove the key file in internal storage, destroy the |
| 1150 | * key inside the secure element, and update the driver's |
| 1151 | * persistent data. Start a transaction that will encompass these |
| 1152 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1153 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1154 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1155 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1156 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1157 | status = psa_crypto_save_transaction( ); |
| 1158 | if( status != PSA_SUCCESS ) |
| 1159 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1160 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1161 | /* We should still try to destroy the key in the secure |
| 1162 | * element and the key metadata in storage. This is especially |
| 1163 | * important if the error is that the storage is full. |
| 1164 | * But how to do it exactly without risking an inconsistent |
| 1165 | * state after a reset? |
| 1166 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1167 | */ |
| 1168 | overall_status = status; |
| 1169 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1172 | status = psa_destroy_se_key( driver, |
| 1173 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1174 | if( overall_status == PSA_SUCCESS ) |
| 1175 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1176 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1177 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1178 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1179 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1180 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1181 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1182 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1183 | if( overall_status == PSA_SUCCESS ) |
| 1184 | overall_status = status; |
| 1185 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1186 | /* TODO: other slots may have a copy of the same key. We should |
| 1187 | * invalidate them. |
| 1188 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1189 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1190 | } |
| 1191 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1192 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1193 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1194 | if( driver != NULL ) |
| 1195 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1196 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1197 | if( overall_status == PSA_SUCCESS ) |
| 1198 | overall_status = status; |
| 1199 | status = psa_crypto_stop_transaction( ); |
| 1200 | if( overall_status == PSA_SUCCESS ) |
| 1201 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1202 | } |
| 1203 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1204 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1205 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1206 | exit: |
| 1207 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1208 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1209 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1210 | if( overall_status == PSA_SUCCESS ) |
| 1211 | overall_status = status; |
| 1212 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1215 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1216 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1217 | static psa_status_t psa_get_rsa_public_exponent( |
| 1218 | const mbedtls_rsa_context *rsa, |
| 1219 | psa_key_attributes_t *attributes ) |
| 1220 | { |
| 1221 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1222 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1223 | uint8_t *buffer = NULL; |
| 1224 | size_t buflen; |
| 1225 | mbedtls_mpi_init( &mpi ); |
| 1226 | |
| 1227 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1228 | if( ret != 0 ) |
| 1229 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1230 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1231 | { |
| 1232 | /* It's the default value, which is reported as an empty string, |
| 1233 | * so there's nothing to do. */ |
| 1234 | goto exit; |
| 1235 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1236 | |
| 1237 | buflen = mbedtls_mpi_size( &mpi ); |
| 1238 | buffer = mbedtls_calloc( 1, buflen ); |
| 1239 | if( buffer == NULL ) |
| 1240 | { |
| 1241 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1242 | goto exit; |
| 1243 | } |
| 1244 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1245 | if( ret != 0 ) |
| 1246 | goto exit; |
| 1247 | attributes->domain_parameters = buffer; |
| 1248 | attributes->domain_parameters_size = buflen; |
| 1249 | |
| 1250 | exit: |
| 1251 | mbedtls_mpi_free( &mpi ); |
| 1252 | if( ret != 0 ) |
| 1253 | mbedtls_free( buffer ); |
| 1254 | return( mbedtls_to_psa_error( ret ) ); |
| 1255 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1256 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1257 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1258 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1259 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1260 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1261 | psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1262 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1263 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1264 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1265 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1266 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1267 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1268 | psa_reset_key_attributes( attributes ); |
| 1269 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1270 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1271 | if( status != PSA_SUCCESS ) |
| 1272 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1273 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1274 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1275 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1276 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1277 | |
| 1278 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1279 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1280 | psa_set_key_slot_number( attributes, |
| 1281 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1282 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1283 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1284 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1285 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1286 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1287 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1288 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1289 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1290 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1291 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1292 | * is not yet implemented. |
| 1293 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1294 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1295 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1296 | break; |
| 1297 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1298 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1299 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1300 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1301 | status = mbedtls_psa_rsa_load_representation( |
| 1302 | slot->attr.type, |
| 1303 | slot->key.data, |
| 1304 | slot->key.bytes, |
| 1305 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1306 | if( status != PSA_SUCCESS ) |
| 1307 | break; |
| 1308 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1309 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1310 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1311 | mbedtls_rsa_free( rsa ); |
| 1312 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1313 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1314 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1315 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1316 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1317 | default: |
| 1318 | /* Nothing else to do. */ |
| 1319 | break; |
| 1320 | } |
| 1321 | |
| 1322 | if( status != PSA_SUCCESS ) |
| 1323 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1324 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1325 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1326 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1327 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1330 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1331 | psa_status_t psa_get_key_slot_number( |
| 1332 | const psa_key_attributes_t *attributes, |
| 1333 | psa_key_slot_number_t *slot_number ) |
| 1334 | { |
| 1335 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1336 | { |
| 1337 | *slot_number = attributes->slot_number; |
| 1338 | return( PSA_SUCCESS ); |
| 1339 | } |
| 1340 | else |
| 1341 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1342 | } |
| 1343 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1344 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1345 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1346 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1347 | uint8_t *data, |
| 1348 | size_t data_size, |
| 1349 | size_t *data_length ) |
| 1350 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1351 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1352 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1353 | memcpy( data, key_buffer, key_buffer_size ); |
| 1354 | memset( data + key_buffer_size, 0, |
| 1355 | data_size - key_buffer_size ); |
| 1356 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1357 | return( PSA_SUCCESS ); |
| 1358 | } |
| 1359 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1360 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1361 | const psa_key_attributes_t *attributes, |
| 1362 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1363 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1364 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1365 | psa_key_type_t type = attributes->core.type; |
| 1366 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1367 | if( key_type_is_raw_bytes( type ) || |
| 1368 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1369 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1370 | { |
| 1371 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1372 | key_buffer, key_buffer_size, |
| 1373 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1374 | } |
| 1375 | else |
| 1376 | { |
| 1377 | /* This shouldn't happen in the reference implementation, but |
| 1378 | it is valid for a special-purpose implementation to omit |
| 1379 | support for exporting certain key types. */ |
| 1380 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1385 | uint8_t *data, |
| 1386 | size_t data_size, |
| 1387 | size_t *data_length ) |
| 1388 | { |
| 1389 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1390 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1391 | psa_key_slot_t *slot; |
| 1392 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1393 | /* Reject a zero-length output buffer now, since this can never be a |
| 1394 | * valid key representation. This way we know that data must be a valid |
| 1395 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1396 | if( data_size == 0 ) |
| 1397 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1398 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1399 | /* Set the key to empty now, so that even when there are errors, we always |
| 1400 | * set data_length to a value between 0 and data_size. On error, setting |
| 1401 | * the key to empty is a good choice because an empty key representation is |
| 1402 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1403 | *data_length = 0; |
| 1404 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1405 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1406 | * which don't require any flag, but |
| 1407 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1408 | */ |
| 1409 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1410 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1411 | if( status != PSA_SUCCESS ) |
| 1412 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1413 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1414 | psa_key_attributes_t attributes = { |
| 1415 | .core = slot->attr |
| 1416 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1417 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1418 | slot->key.data, slot->key.bytes, |
| 1419 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1420 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1421 | unlock_status = psa_unlock_key_slot( slot ); |
| 1422 | |
| 1423 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1424 | } |
| 1425 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1426 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1427 | const psa_key_attributes_t *attributes, |
| 1428 | const uint8_t *key_buffer, |
| 1429 | size_t key_buffer_size, |
| 1430 | uint8_t *data, |
| 1431 | size_t data_size, |
| 1432 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1433 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1434 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1435 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1436 | if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1437 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1438 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1439 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1440 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1441 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1442 | key_buffer, key_buffer_size, |
| 1443 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1444 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1445 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1446 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1447 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1448 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1449 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1450 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1451 | key_buffer, |
| 1452 | key_buffer_size, |
| 1453 | data, |
| 1454 | data_size, |
| 1455 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1456 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1457 | /* We don't know how to convert a private RSA key to public. */ |
| 1458 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1459 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1460 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1461 | } |
| 1462 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1463 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1464 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1465 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1466 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1467 | key_buffer, |
| 1468 | key_buffer_size, |
| 1469 | data, |
| 1470 | data_size, |
| 1471 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1472 | #else |
| 1473 | /* We don't know how to convert a private ECC key to public */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1474 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1475 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1476 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1477 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1478 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1479 | else |
| 1480 | { |
| 1481 | /* This shouldn't happen in the reference implementation, but |
| 1482 | it is valid for a special-purpose implementation to omit |
| 1483 | support for exporting certain key types. */ |
| 1484 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1485 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1486 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1487 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1488 | psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1489 | uint8_t *data, |
| 1490 | size_t data_size, |
| 1491 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1492 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1493 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1494 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1495 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1496 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1497 | /* Reject a zero-length output buffer now, since this can never be a |
| 1498 | * valid key representation. This way we know that data must be a valid |
| 1499 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1500 | if( data_size == 0 ) |
| 1501 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1502 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1503 | /* Set the key to empty now, so that even when there are errors, we always |
| 1504 | * set data_length to a value between 0 and data_size. On error, setting |
| 1505 | * the key to empty is a good choice because an empty key representation is |
| 1506 | * unlikely to be accepted anywhere. */ |
| 1507 | *data_length = 0; |
| 1508 | |
| 1509 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1510 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1511 | if( status != PSA_SUCCESS ) |
| 1512 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1513 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1514 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1515 | { |
| 1516 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1517 | goto exit; |
| 1518 | } |
| 1519 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1520 | psa_key_attributes_t attributes = { |
| 1521 | .core = slot->attr |
| 1522 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1523 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1524 | &attributes, slot->key.data, slot->key.bytes, |
| 1525 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1526 | |
| 1527 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1528 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1529 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1530 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1531 | } |
| 1532 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1533 | #if defined(static_assert) |
| 1534 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1535 | "One or more key attribute flag is listed as both external-only and dual-use" ); |
Gilles Peskine | 5a68056 | 2019-08-05 17:32:13 +0200 | [diff] [blame] | 1536 | static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
Gilles Peskine | 094dac1 | 2019-08-07 18:19:46 +0200 | [diff] [blame] | 1537 | "One or more key attribute flag is listed as both internal-only and dual-use" ); |
Gilles Peskine | 5a68056 | 2019-08-05 17:32:13 +0200 | [diff] [blame] | 1538 | static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0, |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1539 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1540 | #endif |
| 1541 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1542 | /** Validate that a key policy is internally well-formed. |
| 1543 | * |
| 1544 | * This function only rejects invalid policies. It does not validate the |
| 1545 | * consistency of the policy with respect to other attributes of the key |
| 1546 | * such as the key type. |
| 1547 | */ |
| 1548 | static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1549 | { |
| 1550 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1551 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1552 | PSA_KEY_USAGE_ENCRYPT | |
| 1553 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1554 | PSA_KEY_USAGE_SIGN_HASH | |
| 1555 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1556 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1557 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1558 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1559 | return( PSA_SUCCESS ); |
| 1560 | } |
| 1561 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1562 | /** Validate the internal consistency of key attributes. |
| 1563 | * |
| 1564 | * This function only rejects invalid attribute values. If does not |
| 1565 | * validate the consistency of the attributes with any key data that may |
| 1566 | * be involved in the creation of the key. |
| 1567 | * |
| 1568 | * Call this function early in the key creation process. |
| 1569 | * |
| 1570 | * \param[in] attributes Key attributes for the new key. |
| 1571 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1572 | * NULL for a transparent key. |
| 1573 | * |
| 1574 | */ |
| 1575 | static psa_status_t psa_validate_key_attributes( |
| 1576 | const psa_key_attributes_t *attributes, |
| 1577 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1578 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1579 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1580 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1581 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1582 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1583 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1584 | if( status != PSA_SUCCESS ) |
| 1585 | return( status ); |
| 1586 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1587 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1588 | if( status != PSA_SUCCESS ) |
| 1589 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1590 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1591 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1592 | { |
| 1593 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1594 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1595 | } |
| 1596 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1597 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1598 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1599 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1602 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1603 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1604 | return( status ); |
| 1605 | |
| 1606 | /* Refuse to create overly large keys. |
| 1607 | * Note that this doesn't trigger on import if the attributes don't |
| 1608 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1609 | * psa_import_key() needs its own checks. */ |
| 1610 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1611 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1612 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1613 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1614 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1615 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1616 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1617 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1618 | return( PSA_SUCCESS ); |
| 1619 | } |
| 1620 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1621 | /** Prepare a key slot to receive key material. |
| 1622 | * |
| 1623 | * This function allocates a key slot and sets its metadata. |
| 1624 | * |
| 1625 | * If this function fails, call psa_fail_key_creation(). |
| 1626 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1627 | * This function is intended to be used as follows: |
| 1628 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1629 | * it with the specified attributes, and in case of a volatile key assign it |
| 1630 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1631 | * -# Populate the slot with the key material. |
| 1632 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1633 | * In case of failure at any step, stop the sequence and call |
| 1634 | * psa_fail_key_creation(). |
| 1635 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1636 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1637 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1638 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1639 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1640 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1641 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1642 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1643 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1644 | * |
| 1645 | * \retval #PSA_SUCCESS |
| 1646 | * The key slot is ready to receive key material. |
| 1647 | * \return If this function fails, the key slot is an invalid state. |
| 1648 | * You must call psa_fail_key_creation() to wipe and free the slot. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1649 | */ |
| 1650 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1651 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1652 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1653 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1654 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1655 | { |
| 1656 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1657 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1658 | psa_key_slot_t *slot; |
| 1659 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1660 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1661 | *p_drv = NULL; |
| 1662 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1663 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1664 | if( status != PSA_SUCCESS ) |
| 1665 | return( status ); |
| 1666 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1667 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1668 | if( status != PSA_SUCCESS ) |
| 1669 | return( status ); |
| 1670 | slot = *p_slot; |
| 1671 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1672 | /* We're storing the declared bit-size of the key. It's up to each |
| 1673 | * creation mechanism to verify that this information is correct. |
| 1674 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1675 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1676 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1677 | * volatile key identifier associated to the slot returned to contain its |
| 1678 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1679 | |
| 1680 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1681 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1682 | { |
| 1683 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1684 | slot->attr.id = volatile_key_id; |
| 1685 | #else |
| 1686 | slot->attr.id.key_id = volatile_key_id; |
| 1687 | #endif |
| 1688 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1689 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1690 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1691 | * external-only flags, query `attributes`. Thanks to the check |
| 1692 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1693 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1694 | * may have set. */ |
| 1695 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1696 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1697 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1698 | /* For a key in a secure element, we need to do three things |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1699 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1700 | * create the key file in internal storage, create the |
| 1701 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1702 | * persistent data. This is done by starting a transaction that will |
| 1703 | * encompass these three actions. |
| 1704 | * For registering a volatile key, we just need to find an appropriate |
| 1705 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1706 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1707 | /* The first thing to do is to find a slot number for the new key. |
| 1708 | * We save the slot number in persistent storage as part of the |
| 1709 | * transaction data. It will be needed to recover if the power |
| 1710 | * fails during the key creation process, to clean up on the secure |
| 1711 | * element side after restarting. Obtaining a slot number from the |
| 1712 | * secure element driver updates its persistent state, but we do not yet |
| 1713 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1714 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1715 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1716 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1717 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1718 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1719 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1720 | if( status != PSA_SUCCESS ) |
| 1721 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1722 | |
| 1723 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1724 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1725 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1726 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1727 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1728 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1729 | status = psa_crypto_save_transaction( ); |
| 1730 | if( status != PSA_SUCCESS ) |
| 1731 | { |
| 1732 | (void) psa_crypto_stop_transaction( ); |
| 1733 | return( status ); |
| 1734 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1735 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1736 | |
| 1737 | status = psa_copy_key_material_into_slot( |
| 1738 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1739 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1740 | |
| 1741 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1742 | { |
| 1743 | /* Key registration only makes sense with a secure element. */ |
| 1744 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1745 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1746 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1747 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1748 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1749 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1750 | |
| 1751 | /** Finalize the creation of a key once its key material has been set. |
| 1752 | * |
| 1753 | * This entails writing the key to persistent storage. |
| 1754 | * |
| 1755 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1756 | * See the documentation of psa_start_key_creation() for the intended use |
| 1757 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1758 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1759 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1760 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1761 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1762 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1763 | * \param[in,out] slot Pointer to the slot with key material. |
| 1764 | * \param[in] driver The secure element driver for the key, |
| 1765 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1766 | * \param[out] key On success, identifier of the key. Note that the |
| 1767 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1768 | * |
| 1769 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1770 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1771 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1772 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1773 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1774 | * \retval #PSA_ERROR_DATA_INVALID |
| 1775 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1776 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1777 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1778 | * \return If this function fails, the key slot is an invalid state. |
| 1779 | * You must call psa_fail_key_creation() to wipe and free the slot. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1780 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1781 | static psa_status_t psa_finish_key_creation( |
| 1782 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1783 | psa_se_drv_table_entry_t *driver, |
| 1784 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1785 | { |
| 1786 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1787 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1788 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1789 | |
| 1790 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1791 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1792 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1793 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1794 | if( driver != NULL ) |
| 1795 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1796 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1797 | psa_key_slot_number_t slot_number = |
| 1798 | psa_key_slot_get_slot_number( slot ) ; |
| 1799 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1800 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1801 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1802 | sizeof( data.slot_number ), |
| 1803 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1804 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1805 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1806 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1807 | (uint8_t*) &data, |
| 1808 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1809 | } |
| 1810 | else |
| 1811 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1812 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1813 | /* Key material is saved in export representation in the slot, so |
| 1814 | * just pass the slot buffer for storage. */ |
| 1815 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1816 | slot->key.data, |
| 1817 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1818 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1819 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1820 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1821 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1822 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1823 | /* Finish the transaction for a key creation. This does not |
| 1824 | * happen when registering an existing key. Detect this case |
| 1825 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1826 | * creation of a persistent key in a secure element requires a transaction, |
| 1827 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1828 | if( driver != NULL && |
| 1829 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1830 | { |
| 1831 | status = psa_save_se_persistent_data( driver ); |
| 1832 | if( status != PSA_SUCCESS ) |
| 1833 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1834 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1835 | return( status ); |
| 1836 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1837 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1838 | } |
| 1839 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1840 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1841 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1842 | { |
| 1843 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1844 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1845 | if( status != PSA_SUCCESS ) |
| 1846 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1847 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1848 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1849 | return( status ); |
| 1850 | } |
| 1851 | |
| 1852 | /** Abort the creation of a key. |
| 1853 | * |
| 1854 | * You may call this function after calling psa_start_key_creation(), |
| 1855 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1856 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1857 | * See the documentation of psa_start_key_creation() for the intended use |
| 1858 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1859 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1860 | * \param[in,out] slot Pointer to the slot with key material. |
| 1861 | * \param[in] driver The secure element driver for the key, |
| 1862 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1863 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1864 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1865 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1866 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1867 | (void) driver; |
| 1868 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1869 | if( slot == NULL ) |
| 1870 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1871 | |
| 1872 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1873 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1874 | * element, and the failure happened later (when saving metadata |
| 1875 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1876 | * element. |
| 1877 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1878 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1879 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1880 | /* Abort the ongoing transaction if any (there may not be one if |
| 1881 | * the creation process failed before starting one, or if the |
| 1882 | * key creation is a registration of a key in a secure element). |
| 1883 | * Earlier functions must already have done what it takes to undo any |
| 1884 | * partial creation. All that's left is to update the transaction data |
| 1885 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1886 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1887 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1888 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1889 | psa_wipe_key_slot( slot ); |
| 1890 | } |
| 1891 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1892 | /** Validate optional attributes during key creation. |
| 1893 | * |
| 1894 | * Some key attributes are optional during key creation. If they are |
| 1895 | * specified in the attributes structure, check that they are consistent |
| 1896 | * with the data in the slot. |
| 1897 | * |
| 1898 | * This function should be called near the end of key creation, after |
| 1899 | * the slot in memory is fully populated but before saving persistent data. |
| 1900 | */ |
| 1901 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1902 | const psa_key_slot_t *slot, |
| 1903 | const psa_key_attributes_t *attributes ) |
| 1904 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1905 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1906 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1907 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1908 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1909 | } |
| 1910 | |
| 1911 | if( attributes->domain_parameters_size != 0 ) |
| 1912 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1913 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1914 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1915 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1916 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1917 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1918 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1919 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1920 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1921 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1922 | slot->attr.type, |
| 1923 | slot->key.data, |
| 1924 | slot->key.bytes, |
| 1925 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1926 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1927 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1929 | mbedtls_mpi_init( &actual ); |
| 1930 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1931 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1932 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1933 | mbedtls_rsa_free( rsa ); |
| 1934 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1935 | if( ret != 0 ) |
| 1936 | goto rsa_exit; |
| 1937 | ret = mbedtls_mpi_read_binary( &required, |
| 1938 | attributes->domain_parameters, |
| 1939 | attributes->domain_parameters_size ); |
| 1940 | if( ret != 0 ) |
| 1941 | goto rsa_exit; |
| 1942 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1943 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1944 | rsa_exit: |
| 1945 | mbedtls_mpi_free( &actual ); |
| 1946 | mbedtls_mpi_free( &required ); |
| 1947 | if( ret != 0) |
| 1948 | return( mbedtls_to_psa_error( ret ) ); |
| 1949 | } |
| 1950 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1951 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1952 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1953 | { |
| 1954 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1955 | } |
| 1956 | } |
| 1957 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1958 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1959 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1960 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1961 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1962 | } |
| 1963 | |
| 1964 | return( PSA_SUCCESS ); |
| 1965 | } |
| 1966 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1967 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1968 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1969 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1970 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1971 | { |
| 1972 | psa_status_t status; |
| 1973 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1974 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1975 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1976 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1977 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1978 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1979 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1980 | * This also rejects any key which might be encoded as an empty string, |
| 1981 | * which is never valid. */ |
| 1982 | if( data_length == 0 ) |
| 1983 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1984 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1985 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1986 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1987 | if( status != PSA_SUCCESS ) |
| 1988 | goto exit; |
| 1989 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1990 | /* In the case of a transparent key or an opaque key stored in local |
| 1991 | * storage (thus not in the case of generating a key in a secure element |
| 1992 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1993 | * hold the generated key material. */ |
| 1994 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1995 | { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1996 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1997 | if( status != PSA_SUCCESS ) |
| 1998 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1999 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2000 | |
| 2001 | bits = slot->attr.bits; |
| 2002 | status = psa_driver_wrapper_import_key( attributes, |
| 2003 | data, data_length, |
| 2004 | slot->key.data, |
| 2005 | slot->key.bytes, |
| 2006 | &slot->key.bytes, &bits ); |
| 2007 | if( status != PSA_SUCCESS ) |
| 2008 | goto exit; |
| 2009 | |
| 2010 | if( slot->attr.bits == 0 ) |
| 2011 | slot->attr.bits = (psa_key_bits_t) bits; |
| 2012 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2013 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2014 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2015 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2016 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2017 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2018 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2019 | if( status != PSA_SUCCESS ) |
| 2020 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2021 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2022 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2023 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2024 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2025 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2026 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2027 | return( status ); |
| 2028 | } |
| 2029 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2030 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2031 | psa_status_t mbedtls_psa_register_se_key( |
| 2032 | const psa_key_attributes_t *attributes ) |
| 2033 | { |
| 2034 | psa_status_t status; |
| 2035 | psa_key_slot_t *slot = NULL; |
| 2036 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2037 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2038 | |
| 2039 | /* Leaving attributes unspecified is not currently supported. |
| 2040 | * It could make sense to query the key type and size from the |
| 2041 | * secure element, but not all secure elements support this |
| 2042 | * and the driver HAL doesn't currently support it. */ |
| 2043 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2044 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2045 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2046 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2047 | |
| 2048 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2049 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2050 | if( status != PSA_SUCCESS ) |
| 2051 | goto exit; |
| 2052 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2053 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2054 | |
| 2055 | exit: |
| 2056 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2057 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2058 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2059 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2060 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2061 | return( status ); |
| 2062 | } |
| 2063 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2064 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2065 | static psa_status_t psa_copy_key_material( const psa_key_slot_t *source, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2066 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2067 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2068 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2069 | source->key.data, |
| 2070 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2071 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2072 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2073 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2074 | target->attr.type = source->attr.type; |
| 2075 | target->attr.bits = source->attr.bits; |
| 2076 | |
| 2077 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2080 | psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2081 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2082 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2083 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2084 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2085 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2086 | psa_key_slot_t *source_slot = NULL; |
| 2087 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2088 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2089 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2090 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2091 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2092 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2093 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2094 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2095 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2096 | goto exit; |
| 2097 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2098 | status = psa_validate_optional_attributes( source_slot, |
| 2099 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2100 | if( status != PSA_SUCCESS ) |
| 2101 | goto exit; |
| 2102 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2103 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2104 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2105 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2106 | if( status != PSA_SUCCESS ) |
| 2107 | goto exit; |
| 2108 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2109 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2110 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2111 | if( status != PSA_SUCCESS ) |
| 2112 | goto exit; |
| 2113 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2114 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2115 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2116 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2117 | /* Copying to a secure element is not implemented yet. */ |
| 2118 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2119 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2120 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2121 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2122 | |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2123 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2124 | { |
| 2125 | /* |
| 2126 | * Copying through an opaque driver is not implemented yet, consider |
| 2127 | * a lifetime with an external location as an invalid parameter for |
| 2128 | * now. |
| 2129 | */ |
| 2130 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2131 | goto exit; |
| 2132 | } |
| 2133 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2134 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2135 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2136 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2137 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2138 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2139 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2140 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2141 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2142 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2143 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2144 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2145 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2148 | |
| 2149 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2150 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2151 | /* Message digests */ |
| 2152 | /****************************************************************/ |
| 2153 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2154 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2155 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2156 | /* Aborting a non-active operation is allowed */ |
| 2157 | if( operation->id == 0 ) |
| 2158 | return( PSA_SUCCESS ); |
| 2159 | |
| 2160 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2161 | operation->id = 0; |
| 2162 | |
| 2163 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2166 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2167 | psa_algorithm_t alg ) |
| 2168 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2169 | /* A context must be freshly initialized before it can be set up. */ |
| 2170 | if( operation->id != 0 ) |
| 2171 | return( PSA_ERROR_BAD_STATE ); |
| 2172 | |
| 2173 | if( !PSA_ALG_IS_HASH( alg ) ) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2174 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2175 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2176 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2177 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2178 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2179 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2180 | return( psa_driver_wrapper_hash_setup( operation, alg ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2184 | const uint8_t *input, |
| 2185 | size_t input_length ) |
| 2186 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2187 | if( operation->id == 0 ) |
| 2188 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2189 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2190 | /* Don't require hash implementations to behave correctly on a |
| 2191 | * zero-length input, which may have an invalid pointer. */ |
| 2192 | if( input_length == 0 ) |
| 2193 | return( PSA_SUCCESS ); |
| 2194 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2195 | psa_status_t status = psa_driver_wrapper_hash_update( operation, |
| 2196 | input, input_length ); |
| 2197 | if( status != PSA_SUCCESS ) |
| 2198 | psa_hash_abort( operation ); |
| 2199 | |
| 2200 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2204 | uint8_t *hash, |
| 2205 | size_t hash_size, |
| 2206 | size_t *hash_length ) |
| 2207 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2208 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2209 | if( operation->id == 0 ) |
| 2210 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2211 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2212 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2213 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2214 | psa_hash_abort( operation ); |
| 2215 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2218 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2219 | const uint8_t *hash, |
| 2220 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2221 | { |
| 2222 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2223 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2224 | psa_status_t status = psa_hash_finish( |
| 2225 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2226 | actual_hash, sizeof( actual_hash ), |
| 2227 | &actual_hash_length ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2228 | if( status != PSA_SUCCESS ) |
| 2229 | return( status ); |
| 2230 | if( actual_hash_length != hash_length ) |
| 2231 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2232 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2233 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2234 | return( PSA_SUCCESS ); |
| 2235 | } |
| 2236 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2237 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2238 | const uint8_t *input, size_t input_length, |
| 2239 | uint8_t *hash, size_t hash_size, |
| 2240 | size_t *hash_length ) |
| 2241 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2242 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2243 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2244 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2245 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2246 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2247 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2251 | const uint8_t *input, size_t input_length, |
| 2252 | const uint8_t *hash, size_t hash_length ) |
| 2253 | { |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2254 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2255 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2256 | |
| 2257 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2258 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2259 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2260 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2261 | alg, input, input_length, |
| 2262 | actual_hash, sizeof(actual_hash), |
| 2263 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2264 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2265 | return( status ); |
| 2266 | if( actual_hash_length != hash_length ) |
| 2267 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2268 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2269 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2270 | return( PSA_SUCCESS ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2273 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2274 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2275 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2276 | if( source_operation->id == 0 || |
| 2277 | target_operation->id != 0 ) |
| 2278 | { |
| 2279 | return( PSA_ERROR_BAD_STATE ); |
| 2280 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2281 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2282 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2283 | target_operation ); |
| 2284 | if( status != PSA_SUCCESS ) |
| 2285 | psa_hash_abort( target_operation ); |
| 2286 | |
| 2287 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2288 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2289 | |
| 2290 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2291 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2292 | /* MAC */ |
| 2293 | /****************************************************************/ |
| 2294 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2296 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2297 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2298 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2299 | { |
| 2300 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2301 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2302 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2303 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2304 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2305 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2306 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2307 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2308 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2309 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2310 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2311 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2312 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2313 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2314 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2315 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2316 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2317 | return( 128 ); |
| 2318 | default: |
| 2319 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2320 | } |
| 2321 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2322 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2323 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2324 | /* Initialize the MAC operation structure. Once this function has been |
| 2325 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2326 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2327 | psa_algorithm_t alg ) |
| 2328 | { |
| 2329 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2330 | |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2331 | operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2332 | operation->key_set = 0; |
| 2333 | operation->iv_set = 0; |
| 2334 | operation->iv_required = 0; |
| 2335 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2336 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2337 | |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2338 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2339 | if( operation->alg == PSA_ALG_CMAC ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2340 | { |
| 2341 | operation->iv_required = 0; |
| 2342 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2343 | status = PSA_SUCCESS; |
| 2344 | } |
| 2345 | else |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2346 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2347 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2348 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2349 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2350 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2351 | operation->ctx.hmac.alg = 0; |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2352 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2353 | } |
| 2354 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2355 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2356 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2357 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2358 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | if( status != PSA_SUCCESS ) |
| 2362 | memset( operation, 0, sizeof( *operation ) ); |
| 2363 | return( status ); |
| 2364 | } |
| 2365 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2366 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2367 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2368 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2369 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2370 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2371 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2372 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2373 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2374 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2375 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2376 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2377 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2378 | /* The object has (apparently) been initialized but it is not |
| 2379 | * in use. It's ok to call abort on such an object, and there's |
| 2380 | * nothing to do. */ |
| 2381 | return( PSA_SUCCESS ); |
| 2382 | } |
| 2383 | else |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2384 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2385 | if( operation->alg == PSA_ALG_CMAC ) |
| 2386 | { |
| 2387 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2388 | } |
| 2389 | else |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2390 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2391 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2392 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2393 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2394 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2395 | } |
| 2396 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2397 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2398 | { |
| 2399 | /* Sanity check (shouldn't happen: operation->alg should |
| 2400 | * always have been initialized to a valid value). */ |
| 2401 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2402 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2403 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2404 | operation->alg = 0; |
| 2405 | operation->key_set = 0; |
| 2406 | operation->iv_set = 0; |
| 2407 | operation->iv_required = 0; |
| 2408 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2409 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2410 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2411 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2412 | |
| 2413 | bad_state: |
| 2414 | /* If abort is called on an uninitialized object, we can't trust |
| 2415 | * anything. Wipe the object in case it contains confidential data. |
| 2416 | * This may result in a memory leak if a pointer gets overwritten, |
| 2417 | * but it's too late to do anything about this. */ |
| 2418 | memset( operation, 0, sizeof( *operation ) ); |
| 2419 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2422 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2423 | static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation, |
| 2424 | psa_key_slot_t *slot ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2425 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2426 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2427 | const mbedtls_cipher_info_t *cipher_info = |
| 2428 | mbedtls_cipher_info_from_psa( PSA_ALG_CMAC, |
| 2429 | slot->attr.type, slot->attr.bits, |
| 2430 | NULL ); |
| 2431 | if( cipher_info == NULL ) |
| 2432 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2433 | |
| 2434 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2435 | if( ret != 0 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2436 | goto exit; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2437 | |
| 2438 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2439 | slot->key.data, |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2440 | slot->attr.bits ); |
| 2441 | exit: |
| 2442 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2443 | } |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2444 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2445 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2446 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2447 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2448 | const uint8_t *key, |
| 2449 | size_t key_length, |
| 2450 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2451 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2452 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2453 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2454 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2455 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2456 | psa_status_t status; |
| 2457 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2458 | hmac->alg = hash_alg; |
| 2459 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2460 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2461 | * overflow below. This should never trigger if the hash algorithm |
| 2462 | * is implemented correctly. */ |
| 2463 | /* The size checks against the ipad and opad buffers cannot be written |
| 2464 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2465 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2466 | if( block_size > sizeof( ipad ) ) |
| 2467 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2468 | if( block_size > sizeof( hmac->opad ) ) |
| 2469 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2470 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2471 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2472 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2473 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2474 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2475 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2476 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2477 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2478 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2479 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2480 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2481 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2482 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2483 | * an invalid pointer which would make the behavior undefined. */ |
| 2484 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2485 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2486 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2487 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2488 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2489 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2490 | ipad[i] ^= 0x36; |
| 2491 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2492 | |
| 2493 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2494 | * and filling the rest of opad with the requisite constant. */ |
| 2495 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2496 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2497 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2498 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2499 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2500 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2501 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2502 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2503 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2504 | |
| 2505 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2506 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2507 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2508 | return( status ); |
| 2509 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2510 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2511 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2512 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2513 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2514 | psa_algorithm_t alg, |
| 2515 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2516 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2517 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2518 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2519 | psa_key_slot_t *slot; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2520 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2521 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2522 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2523 | /* A context must be freshly initialized before it can be set up. */ |
| 2524 | if( operation->alg != 0 ) |
| 2525 | { |
| 2526 | return( PSA_ERROR_BAD_STATE ); |
| 2527 | } |
| 2528 | |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2529 | status = psa_mac_init( operation, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2530 | if( status != PSA_SUCCESS ) |
| 2531 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2532 | if( is_sign ) |
| 2533 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2534 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2535 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2536 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2537 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2538 | goto exit; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2539 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2540 | /* Validate the combination of key type and algorithm */ |
| 2541 | status = psa_mac_key_can_do( alg, slot->attr.type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2542 | if( status != PSA_SUCCESS ) |
| 2543 | goto exit; |
| 2544 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2545 | /* Get the output length for the algorithm and key combination. None of the |
| 2546 | * currently supported algorithms have an output length dependent on actual |
| 2547 | * key size, so setting it to a bogus value is currently OK. */ |
| 2548 | operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2549 | |
| 2550 | if( operation->mac_size < 4 ) |
| 2551 | { |
| 2552 | /* A very short MAC is too short for security since it can be |
| 2553 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2554 | * so we make this our minimum, even though 32 bits is still |
| 2555 | * too small for security. */ |
| 2556 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2557 | goto exit; |
| 2558 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2559 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2560 | if( operation->mac_size > |
| 2561 | PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
| 2562 | { |
| 2563 | /* It's impossible to "truncate" to a larger length than the full length |
| 2564 | * of the algorithm. */ |
| 2565 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2566 | goto exit; |
| 2567 | } |
| 2568 | |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2569 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2570 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2571 | { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2572 | status = psa_cmac_setup( operation, slot ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2573 | } |
| 2574 | else |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2575 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2576 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2577 | if( PSA_ALG_IS_HMAC( alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2578 | { |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2579 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
Steven Cooreman | 1fb691a | 2021-03-08 12:01:55 +0100 | [diff] [blame] | 2580 | if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2581 | { |
| 2582 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2583 | goto exit; |
| 2584 | } |
| 2585 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2586 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2587 | { |
| 2588 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2589 | goto exit; |
| 2590 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2591 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2592 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2593 | slot->key.data, |
| 2594 | slot->key.bytes, |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2595 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2596 | } |
| 2597 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2598 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2599 | { |
| 2600 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2601 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2602 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2603 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2604 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2605 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2606 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2607 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2608 | else |
| 2609 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2610 | operation->key_set = 1; |
| 2611 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2612 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2613 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2614 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2615 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2616 | } |
| 2617 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2618 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2619 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2620 | psa_algorithm_t alg ) |
| 2621 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2622 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2626 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2627 | psa_algorithm_t alg ) |
| 2628 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2629 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2632 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2633 | const uint8_t *input, |
| 2634 | size_t input_length ) |
| 2635 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2636 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2637 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2638 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2639 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2640 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2641 | operation->has_input = 1; |
| 2642 | |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2643 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2644 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2645 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2646 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2647 | input, input_length ); |
| 2648 | status = mbedtls_to_psa_error( ret ); |
| 2649 | } |
| 2650 | else |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2651 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2652 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2653 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2654 | { |
| 2655 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2656 | input_length ); |
| 2657 | } |
| 2658 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2659 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2660 | { |
| 2661 | /* This shouldn't happen if `operation` was initialized by |
| 2662 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2663 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2664 | } |
| 2665 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2666 | if( status != PSA_SUCCESS ) |
| 2667 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2668 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2671 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2672 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2673 | uint8_t *mac, |
| 2674 | size_t mac_size ) |
| 2675 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2676 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2677 | psa_algorithm_t hash_alg = hmac->alg; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2678 | size_t hash_size = 0; |
| 2679 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2680 | psa_status_t status; |
| 2681 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2682 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2683 | if( status != PSA_SUCCESS ) |
| 2684 | return( status ); |
| 2685 | /* From here on, tmp needs to be wiped. */ |
| 2686 | |
| 2687 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2688 | if( status != PSA_SUCCESS ) |
| 2689 | goto exit; |
| 2690 | |
| 2691 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2692 | if( status != PSA_SUCCESS ) |
| 2693 | goto exit; |
| 2694 | |
| 2695 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2696 | if( status != PSA_SUCCESS ) |
| 2697 | goto exit; |
| 2698 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2699 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2700 | if( status != PSA_SUCCESS ) |
| 2701 | goto exit; |
| 2702 | |
| 2703 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2704 | |
| 2705 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2706 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2707 | return( status ); |
| 2708 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2709 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2710 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2711 | static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2712 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2713 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2714 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2715 | if( ! operation->key_set ) |
| 2716 | return( PSA_ERROR_BAD_STATE ); |
| 2717 | if( operation->iv_required && ! operation->iv_set ) |
| 2718 | return( PSA_ERROR_BAD_STATE ); |
| 2719 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2720 | if( mac_size < operation->mac_size ) |
| 2721 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2722 | |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2723 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2724 | if( operation->alg == PSA_ALG_CMAC ) |
| 2725 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2726 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2727 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2728 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2729 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2730 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2731 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2732 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2733 | else |
Ronald Cron | 3d47181 | 2021-03-18 13:40:31 +0100 | [diff] [blame] | 2734 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2735 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2736 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2737 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2738 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2739 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2740 | } |
| 2741 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2742 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2743 | { |
| 2744 | /* This shouldn't happen if `operation` was initialized by |
| 2745 | * a setup function. */ |
| 2746 | return( PSA_ERROR_BAD_STATE ); |
| 2747 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2750 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2751 | uint8_t *mac, |
| 2752 | size_t mac_size, |
| 2753 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2754 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2755 | psa_status_t status; |
| 2756 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2757 | if( operation->alg == 0 ) |
| 2758 | { |
| 2759 | return( PSA_ERROR_BAD_STATE ); |
| 2760 | } |
| 2761 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2762 | /* Fill the output buffer with something that isn't a valid mac |
| 2763 | * (barring an attack on the mac and deliberately-crafted input), |
| 2764 | * in case the caller doesn't check the return status properly. */ |
| 2765 | *mac_length = mac_size; |
| 2766 | /* If mac_size is 0 then mac may be NULL and then the |
| 2767 | * call to memset would have undefined behavior. */ |
| 2768 | if( mac_size != 0 ) |
| 2769 | memset( mac, '!', mac_size ); |
| 2770 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2771 | if( ! operation->is_sign ) |
| 2772 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2773 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2774 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2775 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2776 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2777 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2778 | if( status == PSA_SUCCESS ) |
| 2779 | { |
| 2780 | status = psa_mac_abort( operation ); |
| 2781 | if( status == PSA_SUCCESS ) |
| 2782 | *mac_length = operation->mac_size; |
| 2783 | else |
| 2784 | memset( mac, '!', mac_size ); |
| 2785 | } |
| 2786 | else |
| 2787 | psa_mac_abort( operation ); |
| 2788 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2791 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2792 | const uint8_t *mac, |
| 2793 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2794 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2795 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2796 | psa_status_t status; |
| 2797 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2798 | if( operation->alg == 0 ) |
| 2799 | { |
| 2800 | return( PSA_ERROR_BAD_STATE ); |
| 2801 | } |
| 2802 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2803 | if( operation->is_sign ) |
| 2804 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2805 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2806 | } |
| 2807 | if( operation->mac_size != mac_length ) |
| 2808 | { |
| 2809 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2810 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2811 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2812 | |
| 2813 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2814 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 2815 | if( status != PSA_SUCCESS ) |
| 2816 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2817 | |
| 2818 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2819 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2820 | |
| 2821 | cleanup: |
| 2822 | if( status == PSA_SUCCESS ) |
| 2823 | status = psa_mac_abort( operation ); |
| 2824 | else |
| 2825 | psa_mac_abort( operation ); |
| 2826 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2827 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2828 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2829 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2833 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2834 | /****************************************************************/ |
| 2835 | /* Asymmetric cryptography */ |
| 2836 | /****************************************************************/ |
| 2837 | |
Ronald Cron | 67b1eb3 | 2020-12-08 17:37:27 +0100 | [diff] [blame] | 2838 | psa_status_t psa_sign_hash_internal( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2839 | const psa_key_attributes_t *attributes, |
| 2840 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2841 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2842 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2843 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2844 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2845 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2846 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2847 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2848 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2849 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2850 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2851 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2852 | attributes, |
| 2853 | key_buffer, key_buffer_size, |
| 2854 | alg, hash, hash_length, |
| 2855 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2856 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2857 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2858 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2859 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2860 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2861 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2862 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2863 | } |
| 2864 | else |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2865 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
| 2866 | { |
| 2867 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2868 | { |
| 2869 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2870 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2871 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2872 | attributes, |
| 2873 | key_buffer, key_buffer_size, |
| 2874 | alg, hash, hash_length, |
| 2875 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2876 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2877 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2878 | } |
| 2879 | else |
| 2880 | { |
| 2881 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2882 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2883 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2884 | |
| 2885 | (void)key_buffer; |
| 2886 | (void)key_buffer_size; |
| 2887 | (void)hash; |
| 2888 | (void)hash_length; |
| 2889 | (void)signature; |
| 2890 | (void)signature_size; |
| 2891 | (void)signature_length; |
| 2892 | |
| 2893 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2894 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2895 | |
| 2896 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2897 | psa_algorithm_t alg, |
| 2898 | const uint8_t *hash, |
| 2899 | size_t hash_length, |
| 2900 | uint8_t *signature, |
| 2901 | size_t signature_size, |
| 2902 | size_t *signature_length ) |
| 2903 | { |
| 2904 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2905 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2906 | psa_key_slot_t *slot; |
| 2907 | |
| 2908 | *signature_length = signature_size; |
| 2909 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 2910 | * that signature must be a valid pointer. (On the other hand, the hash |
| 2911 | * buffer can in principle be empty since it doesn't actually have |
| 2912 | * to be a hash.) */ |
| 2913 | if( signature_size == 0 ) |
| 2914 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2915 | |
| 2916 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 2917 | PSA_KEY_USAGE_SIGN_HASH, |
| 2918 | alg ); |
| 2919 | if( status != PSA_SUCCESS ) |
| 2920 | goto exit; |
| 2921 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2922 | { |
| 2923 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2924 | goto exit; |
| 2925 | } |
| 2926 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 2927 | psa_key_attributes_t attributes = { |
| 2928 | .core = slot->attr |
| 2929 | }; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2930 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 2931 | status = psa_driver_wrapper_sign_hash( |
| 2932 | &attributes, slot->key.data, slot->key.bytes, |
| 2933 | alg, hash, hash_length, |
| 2934 | signature, signature_size, signature_length ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2935 | |
| 2936 | exit: |
| 2937 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2938 | * the trailing part on success) with something that isn't a valid mac |
| 2939 | * (barring an attack on the mac and deliberately-crafted input), |
| 2940 | * in case the caller doesn't check the return status properly. */ |
| 2941 | if( status == PSA_SUCCESS ) |
| 2942 | memset( signature + *signature_length, '!', |
| 2943 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 2944 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2945 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2946 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2947 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2948 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2949 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2950 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2951 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2952 | } |
| 2953 | |
Ronald Cron | 67b1eb3 | 2020-12-08 17:37:27 +0100 | [diff] [blame] | 2954 | psa_status_t psa_verify_hash_internal( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2955 | const psa_key_attributes_t *attributes, |
| 2956 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2957 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2958 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2959 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2960 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2961 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2962 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2963 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2964 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2965 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2966 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2967 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2968 | attributes, |
| 2969 | key_buffer, key_buffer_size, |
| 2970 | alg, hash, hash_length, |
| 2971 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2972 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2973 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2974 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2975 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2976 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2977 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2978 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2979 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2980 | else |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2981 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2982 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2983 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2984 | { |
| 2985 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2986 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2987 | return( mbedtls_psa_ecdsa_verify_hash( |
| 2988 | attributes, |
| 2989 | key_buffer, key_buffer_size, |
| 2990 | alg, hash, hash_length, |
| 2991 | signature, signature_length ) ); |
| 2992 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2993 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 2994 | } |
| 2995 | else |
| 2996 | { |
| 2997 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2998 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2999 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3000 | |
| 3001 | (void)key_buffer; |
| 3002 | (void)key_buffer_size; |
| 3003 | (void)hash; |
| 3004 | (void)hash_length; |
| 3005 | (void)signature; |
| 3006 | (void)signature_length; |
| 3007 | |
| 3008 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3009 | } |
| 3010 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3011 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3012 | psa_algorithm_t alg, |
| 3013 | const uint8_t *hash, |
| 3014 | size_t hash_length, |
| 3015 | const uint8_t *signature, |
| 3016 | size_t signature_length ) |
| 3017 | { |
| 3018 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3019 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3020 | psa_key_slot_t *slot; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3021 | |
| 3022 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3023 | PSA_KEY_USAGE_VERIFY_HASH, |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3024 | alg ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3025 | if( status != PSA_SUCCESS ) |
| 3026 | return( status ); |
| 3027 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3028 | psa_key_attributes_t attributes = { |
| 3029 | .core = slot->attr |
| 3030 | }; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3031 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3032 | status = psa_driver_wrapper_verify_hash( |
| 3033 | &attributes, slot->key.data, slot->key.bytes, |
| 3034 | alg, hash, hash_length, |
| 3035 | signature, signature_length ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3036 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3037 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3038 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3039 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3040 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3041 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3042 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3043 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3044 | mbedtls_rsa_context *rsa ) |
| 3045 | { |
| 3046 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3047 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3048 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3049 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3050 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3051 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3052 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3053 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3054 | psa_algorithm_t alg, |
| 3055 | const uint8_t *input, |
| 3056 | size_t input_length, |
| 3057 | const uint8_t *salt, |
| 3058 | size_t salt_length, |
| 3059 | uint8_t *output, |
| 3060 | size_t output_size, |
| 3061 | size_t *output_length ) |
| 3062 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3063 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3064 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3065 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3066 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3067 | (void) input; |
| 3068 | (void) input_length; |
| 3069 | (void) salt; |
| 3070 | (void) output; |
| 3071 | (void) output_size; |
| 3072 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3073 | *output_length = 0; |
| 3074 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3075 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3076 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3077 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3078 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3079 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3080 | if( status != PSA_SUCCESS ) |
| 3081 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3082 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3083 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3084 | { |
| 3085 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3086 | goto exit; |
| 3087 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3088 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3089 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3090 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3091 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3092 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3093 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3094 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3095 | slot->key.data, |
| 3096 | slot->key.bytes, |
| 3097 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3098 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3099 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3100 | |
| 3101 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3102 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3103 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3104 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3105 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3106 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3107 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
| 3108 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3109 | status = mbedtls_to_psa_error( |
| 3110 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3111 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3112 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3113 | MBEDTLS_RSA_PUBLIC, |
| 3114 | input_length, |
| 3115 | input, |
| 3116 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3117 | } |
| 3118 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3119 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3120 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3121 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
| 3122 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3123 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3124 | status = mbedtls_to_psa_error( |
| 3125 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3126 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3127 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3128 | MBEDTLS_RSA_PUBLIC, |
| 3129 | salt, salt_length, |
| 3130 | input_length, |
| 3131 | input, |
| 3132 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3133 | } |
| 3134 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3135 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3136 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3137 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3138 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3139 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3140 | rsa_exit: |
| 3141 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3142 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3143 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3144 | mbedtls_rsa_free( rsa ); |
| 3145 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3146 | } |
| 3147 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3148 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3149 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3150 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3151 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3152 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3153 | |
| 3154 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3155 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3156 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3157 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3158 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3159 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3160 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3161 | psa_algorithm_t alg, |
| 3162 | const uint8_t *input, |
| 3163 | size_t input_length, |
| 3164 | const uint8_t *salt, |
| 3165 | size_t salt_length, |
| 3166 | uint8_t *output, |
| 3167 | size_t output_size, |
| 3168 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3169 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3170 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3171 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3172 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3173 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3174 | (void) input; |
| 3175 | (void) input_length; |
| 3176 | (void) salt; |
| 3177 | (void) output; |
| 3178 | (void) output_size; |
| 3179 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3180 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3181 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3182 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3183 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3184 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3185 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3186 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3187 | if( status != PSA_SUCCESS ) |
| 3188 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3189 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3190 | { |
| 3191 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3192 | goto exit; |
| 3193 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3194 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3195 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3196 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3197 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3198 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3199 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3200 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3201 | slot->key.data, |
| 3202 | slot->key.bytes, |
| 3203 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3204 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3205 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3206 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3207 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3208 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3209 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3210 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3211 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3212 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3213 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3214 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3215 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3216 | status = mbedtls_to_psa_error( |
| 3217 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3218 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3219 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3220 | MBEDTLS_RSA_PRIVATE, |
| 3221 | output_length, |
| 3222 | input, |
| 3223 | output, |
| 3224 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3225 | } |
| 3226 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3227 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3228 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3229 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3230 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3231 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3232 | status = mbedtls_to_psa_error( |
| 3233 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3234 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3235 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3236 | MBEDTLS_RSA_PRIVATE, |
| 3237 | salt, salt_length, |
| 3238 | output_length, |
| 3239 | input, |
| 3240 | output, |
| 3241 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3242 | } |
| 3243 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3244 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3245 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3246 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3247 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3248 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3249 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3250 | mbedtls_rsa_free( rsa ); |
| 3251 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3252 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3253 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3254 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3255 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3256 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3257 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3258 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3259 | |
| 3260 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3261 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3262 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3263 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3264 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3265 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3266 | |
| 3267 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3268 | /****************************************************************/ |
| 3269 | /* Symmetric cryptography */ |
| 3270 | /****************************************************************/ |
| 3271 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3272 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3273 | mbedtls_svc_key_id_t key, |
| 3274 | psa_algorithm_t alg, |
| 3275 | mbedtls_operation_t cipher_operation ) |
| 3276 | { |
| 3277 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3278 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3279 | psa_key_slot_t *slot; |
| 3280 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3281 | PSA_KEY_USAGE_ENCRYPT : |
| 3282 | PSA_KEY_USAGE_DECRYPT ); |
| 3283 | |
| 3284 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3285 | if( operation->id != 0 ) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3286 | return( PSA_ERROR_BAD_STATE ); |
| 3287 | |
| 3288 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 3289 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3291 | |
| 3292 | /* Fetch key material from key storage. */ |
| 3293 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3294 | if( status != PSA_SUCCESS ) |
| 3295 | goto exit; |
| 3296 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3297 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3298 | * is used to indicate to psa_cipher_abort that there are resources to free, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3299 | * so we only set it (in the driver wrapper) after resources have been |
| 3300 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3301 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3302 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3303 | operation->iv_required = 0; |
| 3304 | else |
| 3305 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3306 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3307 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3308 | psa_key_attributes_t attributes = { |
| 3309 | .core = slot->attr |
| 3310 | }; |
| 3311 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3312 | /* Try doing the operation through a driver before using software fallback. */ |
| 3313 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3314 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3315 | &attributes, |
| 3316 | slot->key.data, |
| 3317 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3318 | alg ); |
| 3319 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3320 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3321 | &attributes, |
| 3322 | slot->key.data, |
| 3323 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3324 | alg ); |
| 3325 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3326 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3327 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3328 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3329 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3330 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3331 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3332 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3333 | } |
| 3334 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3335 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3336 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3337 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3338 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3339 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3340 | } |
| 3341 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3342 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3343 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3344 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3345 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3346 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3347 | } |
| 3348 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3349 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3350 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3351 | size_t iv_size, |
| 3352 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3353 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3354 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3355 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3356 | *iv_length = 0; |
| 3357 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3358 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3359 | { |
| 3360 | return( PSA_ERROR_BAD_STATE ); |
| 3361 | } |
| 3362 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3363 | if( operation->iv_set || ! operation->iv_required ) |
| 3364 | { |
| 3365 | return( PSA_ERROR_BAD_STATE ); |
| 3366 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3367 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3368 | if( iv_size < operation->default_iv_length ) |
| 3369 | { |
| 3370 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3371 | goto exit; |
| 3372 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3373 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3374 | status = psa_generate_random( iv, operation->default_iv_length ); |
| 3375 | if( status != PSA_SUCCESS ) |
| 3376 | goto exit; |
| 3377 | |
| 3378 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3379 | iv, |
| 3380 | operation->default_iv_length ); |
| 3381 | |
| 3382 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3383 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3384 | { |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3385 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3386 | *iv_length = operation->default_iv_length; |
| 3387 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3388 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3389 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3390 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3391 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3392 | } |
| 3393 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3394 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3395 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3396 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3397 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3398 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3399 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3400 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3401 | return( PSA_ERROR_BAD_STATE ); |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3402 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3403 | if( operation->iv_set || ! operation->iv_required ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3404 | return( PSA_ERROR_BAD_STATE ); |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3405 | |
| 3406 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3407 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3408 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3409 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3410 | iv, |
| 3411 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3412 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3413 | if( status == PSA_SUCCESS ) |
| 3414 | operation->iv_set = 1; |
| 3415 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3416 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3417 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3418 | } |
| 3419 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3420 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3421 | const uint8_t *input, |
| 3422 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3423 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3424 | size_t output_size, |
| 3425 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3426 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3427 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3428 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3429 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3430 | { |
| 3431 | return( PSA_ERROR_BAD_STATE ); |
| 3432 | } |
| 3433 | if( operation->iv_required && ! operation->iv_set ) |
| 3434 | { |
| 3435 | return( PSA_ERROR_BAD_STATE ); |
| 3436 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3437 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3438 | status = psa_driver_wrapper_cipher_update( operation, |
| 3439 | input, |
| 3440 | input_length, |
| 3441 | output, |
| 3442 | output_size, |
| 3443 | output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3444 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3445 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3446 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3447 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3448 | } |
| 3449 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3450 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3451 | uint8_t *output, |
| 3452 | size_t output_size, |
| 3453 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3454 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3455 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3456 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3457 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3458 | { |
| 3459 | return( PSA_ERROR_BAD_STATE ); |
| 3460 | } |
| 3461 | if( operation->iv_required && ! operation->iv_set ) |
| 3462 | { |
| 3463 | return( PSA_ERROR_BAD_STATE ); |
| 3464 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3465 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3466 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3467 | output, |
| 3468 | output_size, |
| 3469 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3470 | if( status == PSA_SUCCESS ) |
| 3471 | return( psa_cipher_abort( operation ) ); |
| 3472 | else |
| 3473 | { |
| 3474 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3475 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3476 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3477 | return( status ); |
| 3478 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3481 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3482 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3483 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3484 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3485 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3486 | * in use. It's ok to call abort on such an object, and there's |
| 3487 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3488 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3489 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3490 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3491 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3492 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3493 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3494 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3495 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3496 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3497 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3498 | } |
| 3499 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3500 | /****************************************************************/ |
| 3501 | /* AEAD */ |
| 3502 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3503 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3504 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3505 | psa_algorithm_t alg, |
| 3506 | const uint8_t *nonce, |
| 3507 | size_t nonce_length, |
| 3508 | const uint8_t *additional_data, |
| 3509 | size_t additional_data_length, |
| 3510 | const uint8_t *plaintext, |
| 3511 | size_t plaintext_length, |
| 3512 | uint8_t *ciphertext, |
| 3513 | size_t ciphertext_size, |
| 3514 | size_t *ciphertext_length ) |
| 3515 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3516 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3517 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3518 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3519 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3520 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3521 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3522 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3523 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3524 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3525 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3526 | if( status != PSA_SUCCESS ) |
| 3527 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3528 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3529 | psa_key_attributes_t attributes = { |
| 3530 | .core = slot->attr |
| 3531 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3532 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3533 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3534 | &attributes, slot->key.data, slot->key.bytes, |
| 3535 | alg, |
| 3536 | nonce, nonce_length, |
| 3537 | additional_data, additional_data_length, |
| 3538 | plaintext, plaintext_length, |
| 3539 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3540 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3541 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3542 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3543 | |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3544 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3545 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3546 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3547 | } |
| 3548 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3549 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3550 | psa_algorithm_t alg, |
| 3551 | const uint8_t *nonce, |
| 3552 | size_t nonce_length, |
| 3553 | const uint8_t *additional_data, |
| 3554 | size_t additional_data_length, |
| 3555 | const uint8_t *ciphertext, |
| 3556 | size_t ciphertext_length, |
| 3557 | uint8_t *plaintext, |
| 3558 | size_t plaintext_size, |
| 3559 | size_t *plaintext_length ) |
| 3560 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3561 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3562 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3563 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3564 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3565 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3566 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3567 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3568 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3569 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3570 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3571 | if( status != PSA_SUCCESS ) |
| 3572 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3573 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3574 | psa_key_attributes_t attributes = { |
| 3575 | .core = slot->attr |
| 3576 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3577 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3578 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3579 | &attributes, slot->key.data, slot->key.bytes, |
| 3580 | alg, |
| 3581 | nonce, nonce_length, |
| 3582 | additional_data, additional_data_length, |
| 3583 | ciphertext, ciphertext_length, |
| 3584 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3585 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3586 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3587 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3588 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3589 | psa_unlock_key_slot( slot ); |
| 3590 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3591 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3592 | } |
| 3593 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3594 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3595 | /* Generators */ |
| 3596 | /****************************************************************/ |
| 3597 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3598 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3599 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3600 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3601 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 3602 | #endif |
| 3603 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3604 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 3605 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 3606 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 3607 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 3608 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3609 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3610 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3611 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3612 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 3613 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3614 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3615 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3616 | } |
| 3617 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3618 | psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3619 | { |
| 3620 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3621 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3622 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3623 | { |
| 3624 | /* The object has (apparently) been initialized but it is not |
| 3625 | * in use. It's ok to call abort on such an object, and there's |
| 3626 | * nothing to do. */ |
| 3627 | } |
| 3628 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3629 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3630 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3631 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3632 | mbedtls_free( operation->ctx.hkdf.info ); |
| 3633 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3634 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3635 | else |
| 3636 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3637 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3638 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3639 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3640 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3641 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3642 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3643 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 3644 | { |
| 3645 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 3646 | operation->ctx.tls12_prf.seed_length ); |
| 3647 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 3648 | } |
| 3649 | |
| 3650 | if( operation->ctx.tls12_prf.label != NULL ) |
| 3651 | { |
| 3652 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 3653 | operation->ctx.tls12_prf.label_length ); |
| 3654 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 3655 | } |
| 3656 | |
| 3657 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 3658 | |
| 3659 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3660 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3661 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3662 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3663 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 3664 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3665 | { |
| 3666 | status = PSA_ERROR_BAD_STATE; |
| 3667 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 3668 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3669 | return( status ); |
| 3670 | } |
| 3671 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3672 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3673 | size_t *capacity) |
| 3674 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3675 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3676 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3677 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3678 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3681 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3682 | return( PSA_SUCCESS ); |
| 3683 | } |
| 3684 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3685 | psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3686 | size_t capacity ) |
| 3687 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3688 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3689 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3690 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3691 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3692 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3693 | return( PSA_SUCCESS ); |
| 3694 | } |
| 3695 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3696 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3697 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3698 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3699 | static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3700 | psa_algorithm_t hash_alg, |
| 3701 | uint8_t *output, |
| 3702 | size_t output_length ) |
| 3703 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3704 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3705 | psa_status_t status; |
| 3706 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3707 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 3708 | return( PSA_ERROR_BAD_STATE ); |
| 3709 | hkdf->state = HKDF_STATE_OUTPUT; |
| 3710 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3711 | while( output_length != 0 ) |
| 3712 | { |
| 3713 | /* Copy what remains of the current block */ |
| 3714 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 3715 | if( n > output_length ) |
| 3716 | n = (uint8_t) output_length; |
| 3717 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 3718 | output += n; |
| 3719 | output_length -= n; |
| 3720 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3721 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3722 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3723 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 3724 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3725 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3726 | * object was corrupted or if this function is called directly |
| 3727 | * inside the library. */ |
| 3728 | if( hkdf->block_number == 0xff ) |
| 3729 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3730 | |
| 3731 | /* We need a new block */ |
| 3732 | ++hkdf->block_number; |
| 3733 | hkdf->offset_in_block = 0; |
| 3734 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 3735 | hkdf->prk, hash_length, |
| 3736 | hash_alg ); |
| 3737 | if( status != PSA_SUCCESS ) |
| 3738 | return( status ); |
| 3739 | if( hkdf->block_number != 1 ) |
| 3740 | { |
| 3741 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3742 | hkdf->output_block, |
| 3743 | hash_length ); |
| 3744 | if( status != PSA_SUCCESS ) |
| 3745 | return( status ); |
| 3746 | } |
| 3747 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3748 | hkdf->info, |
| 3749 | hkdf->info_length ); |
| 3750 | if( status != PSA_SUCCESS ) |
| 3751 | return( status ); |
| 3752 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3753 | &hkdf->block_number, 1 ); |
| 3754 | if( status != PSA_SUCCESS ) |
| 3755 | return( status ); |
| 3756 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 3757 | hkdf->output_block, |
| 3758 | sizeof( hkdf->output_block ) ); |
| 3759 | if( status != PSA_SUCCESS ) |
| 3760 | return( status ); |
| 3761 | } |
| 3762 | |
| 3763 | return( PSA_SUCCESS ); |
| 3764 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3765 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3766 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3767 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3768 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3769 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 3770 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 3771 | psa_algorithm_t alg ) |
| 3772 | { |
| 3773 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3774 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3775 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 3776 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3777 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3778 | /* We can't be wanting more output after block 0xff, otherwise |
| 3779 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 3780 | * prevented this call. It could happen only if the operation |
| 3781 | * object was corrupted or if this function is called directly |
| 3782 | * inside the library. */ |
| 3783 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 3784 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3785 | |
| 3786 | /* We need a new block */ |
| 3787 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3788 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3789 | |
| 3790 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 3791 | * |
| 3792 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 3793 | * |
| 3794 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 3795 | * HMAC_hash(secret, A(2) + seed) + |
| 3796 | * HMAC_hash(secret, A(3) + seed) + ... |
| 3797 | * |
| 3798 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3799 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3800 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3801 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3802 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 3803 | * is currently extracted as `output_block` and where i is |
| 3804 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3805 | */ |
| 3806 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3807 | /* Save the hash context before using it, to preserve the hash state with |
| 3808 | * only the inner padding in it. We need this, because inner padding depends |
| 3809 | * on the key (secret in the RFC's terminology). */ |
| 3810 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 3811 | if( status != PSA_SUCCESS ) |
| 3812 | goto cleanup; |
| 3813 | |
| 3814 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 3815 | if( tls12_prf->block_number == 1 ) |
| 3816 | { |
| 3817 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 3818 | * the variable seed and in this instance means it in the context of the |
| 3819 | * P_hash function, where seed = label + seed.) */ |
| 3820 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 3821 | tls12_prf->label, tls12_prf->label_length ); |
| 3822 | if( status != PSA_SUCCESS ) |
| 3823 | goto cleanup; |
| 3824 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 3825 | tls12_prf->seed, tls12_prf->seed_length ); |
| 3826 | if( status != PSA_SUCCESS ) |
| 3827 | goto cleanup; |
| 3828 | } |
| 3829 | else |
| 3830 | { |
| 3831 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 3832 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 3833 | tls12_prf->Ai, hash_length ); |
| 3834 | if( status != PSA_SUCCESS ) |
| 3835 | goto cleanup; |
| 3836 | } |
| 3837 | |
| 3838 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 3839 | tls12_prf->Ai, hash_length ); |
| 3840 | if( status != PSA_SUCCESS ) |
| 3841 | goto cleanup; |
| 3842 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 3843 | if( status != PSA_SUCCESS ) |
| 3844 | goto cleanup; |
| 3845 | |
| 3846 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 3847 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 3848 | tls12_prf->Ai, hash_length ); |
| 3849 | if( status != PSA_SUCCESS ) |
| 3850 | goto cleanup; |
| 3851 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 3852 | tls12_prf->label, tls12_prf->label_length ); |
| 3853 | if( status != PSA_SUCCESS ) |
| 3854 | goto cleanup; |
| 3855 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 3856 | tls12_prf->seed, tls12_prf->seed_length ); |
| 3857 | if( status != PSA_SUCCESS ) |
| 3858 | goto cleanup; |
| 3859 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 3860 | tls12_prf->output_block, hash_length ); |
| 3861 | if( status != PSA_SUCCESS ) |
| 3862 | goto cleanup; |
| 3863 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 3864 | if( status != PSA_SUCCESS ) |
| 3865 | goto cleanup; |
| 3866 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3867 | |
| 3868 | cleanup: |
| 3869 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3870 | cleanup_status = psa_hash_abort( &backup ); |
| 3871 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 3872 | status = cleanup_status; |
| 3873 | |
| 3874 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3875 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3876 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3877 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 3878 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 3879 | psa_algorithm_t alg, |
| 3880 | uint8_t *output, |
| 3881 | size_t output_length ) |
| 3882 | { |
| 3883 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3884 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3885 | psa_status_t status; |
| 3886 | uint8_t offset, length; |
| 3887 | |
| 3888 | while( output_length != 0 ) |
| 3889 | { |
| 3890 | /* Check if we have fully processed the current block. */ |
| 3891 | if( tls12_prf->left_in_block == 0 ) |
| 3892 | { |
| 3893 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 3894 | alg ); |
| 3895 | if( status != PSA_SUCCESS ) |
| 3896 | return( status ); |
| 3897 | |
| 3898 | continue; |
| 3899 | } |
| 3900 | |
| 3901 | if( tls12_prf->left_in_block > output_length ) |
| 3902 | length = (uint8_t) output_length; |
| 3903 | else |
| 3904 | length = tls12_prf->left_in_block; |
| 3905 | |
| 3906 | offset = hash_length - tls12_prf->left_in_block; |
| 3907 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 3908 | output += length; |
| 3909 | output_length -= length; |
| 3910 | tls12_prf->left_in_block -= length; |
| 3911 | } |
| 3912 | |
| 3913 | return( PSA_SUCCESS ); |
| 3914 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3915 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 3916 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3917 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 3918 | psa_status_t psa_key_derivation_output_bytes( |
| 3919 | psa_key_derivation_operation_t *operation, |
| 3920 | uint8_t *output, |
| 3921 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3922 | { |
| 3923 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3924 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3925 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3926 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3927 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3928 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3929 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3932 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3933 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3934 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3935 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3936 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3937 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3938 | goto exit; |
| 3939 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3940 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3941 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3942 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3943 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3944 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 3945 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3946 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3947 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3948 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3949 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3950 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3951 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3952 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3953 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3954 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3955 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3956 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3957 | output, output_length ); |
| 3958 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 3959 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3960 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3961 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3962 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 3963 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3964 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3965 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3966 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3967 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3968 | output_length ); |
| 3969 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3970 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3971 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 3972 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3973 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 3974 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3975 | return( PSA_ERROR_BAD_STATE ); |
| 3976 | } |
| 3977 | |
| 3978 | exit: |
| 3979 | if( status != PSA_SUCCESS ) |
| 3980 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3981 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3982 | * This allows us to differentiate between exhausted operations and |
| 3983 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 3984 | * operations. */ |
| 3985 | psa_algorithm_t alg = operation->alg; |
| 3986 | psa_key_derivation_abort( operation ); |
| 3987 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3988 | memset( output, '!', output_length ); |
| 3989 | } |
| 3990 | return( status ); |
| 3991 | } |
| 3992 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 3993 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3994 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 3995 | { |
| 3996 | if( data_size >= 8 ) |
| 3997 | mbedtls_des_key_set_parity( data ); |
| 3998 | if( data_size >= 16 ) |
| 3999 | mbedtls_des_key_set_parity( data + 8 ); |
| 4000 | if( data_size >= 24 ) |
| 4001 | mbedtls_des_key_set_parity( data + 16 ); |
| 4002 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4003 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4004 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4005 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4006 | psa_key_slot_t *slot, |
| 4007 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4008 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4009 | { |
| 4010 | uint8_t *data = NULL; |
| 4011 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4012 | psa_status_t status; |
| 4013 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4014 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4015 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4016 | if( bits % 8 != 0 ) |
| 4017 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4018 | data = mbedtls_calloc( 1, bytes ); |
| 4019 | if( data == NULL ) |
| 4020 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4021 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4022 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4023 | if( status != PSA_SUCCESS ) |
| 4024 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4025 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4026 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4027 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4028 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4029 | |
| 4030 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 4031 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4032 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4033 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4034 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4035 | psa_key_attributes_t attributes = { |
| 4036 | .core = slot->attr |
| 4037 | }; |
| 4038 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4039 | status = psa_driver_wrapper_import_key( &attributes, |
| 4040 | data, bytes, |
| 4041 | slot->key.data, |
| 4042 | slot->key.bytes, |
| 4043 | &slot->key.bytes, &bits ); |
| 4044 | if( bits != slot->attr.bits ) |
| 4045 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4046 | |
| 4047 | exit: |
| 4048 | mbedtls_free( data ); |
| 4049 | return( status ); |
| 4050 | } |
| 4051 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4052 | psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4053 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4054 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4055 | { |
| 4056 | psa_status_t status; |
| 4057 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4058 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4059 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4060 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4061 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4062 | /* Reject any attempt to create a zero-length key so that we don't |
| 4063 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4064 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4065 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4066 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4067 | if( ! operation->can_output_key ) |
| 4068 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4069 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4070 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 4071 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4072 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4073 | if( driver != NULL ) |
| 4074 | { |
| 4075 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4076 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4077 | } |
| 4078 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4079 | if( status == PSA_SUCCESS ) |
| 4080 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4081 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4082 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4083 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4084 | } |
| 4085 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4086 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4087 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4088 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4089 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4090 | return( status ); |
| 4091 | } |
| 4092 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4093 | |
| 4094 | |
| 4095 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4096 | /* Key derivation */ |
| 4097 | /****************************************************************/ |
| 4098 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4099 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4100 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4101 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4102 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4103 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4104 | int is_kdf_alg_supported; |
| 4105 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4106 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4107 | * initialisers for this union leave some bytes unspecified.) */ |
| 4108 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4109 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4110 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4111 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4112 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 4113 | is_kdf_alg_supported = 1; |
| 4114 | else |
| 4115 | #endif |
| 4116 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4117 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 4118 | is_kdf_alg_supported = 1; |
| 4119 | else |
| 4120 | #endif |
| 4121 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4122 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 4123 | is_kdf_alg_supported = 1; |
| 4124 | else |
| 4125 | #endif |
| 4126 | is_kdf_alg_supported = 0; |
| 4127 | |
| 4128 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4129 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4130 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4131 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4132 | if( hash_size == 0 ) |
| 4133 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4134 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4135 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4136 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4137 | { |
| 4138 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4139 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4140 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4141 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4142 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4143 | |
| 4144 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4145 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4146 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4147 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4148 | psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4149 | psa_algorithm_t alg ) |
| 4150 | { |
| 4151 | psa_status_t status; |
| 4152 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4153 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4154 | return( PSA_ERROR_BAD_STATE ); |
| 4155 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4156 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4157 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4158 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4159 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4160 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4161 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4162 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4163 | #else |
| 4164 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4165 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4166 | } |
| 4167 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4168 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4169 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4170 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4171 | #else |
| 4172 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4173 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4174 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4175 | else |
| 4176 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4177 | |
| 4178 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4179 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4180 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4183 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4184 | static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4185 | psa_algorithm_t hash_alg, |
| 4186 | psa_key_derivation_step_t step, |
| 4187 | const uint8_t *data, |
| 4188 | size_t data_length ) |
| 4189 | { |
| 4190 | psa_status_t status; |
| 4191 | switch( step ) |
| 4192 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4193 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4194 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4195 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4196 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4197 | data, data_length, |
| 4198 | hash_alg ); |
| 4199 | if( status != PSA_SUCCESS ) |
| 4200 | return( status ); |
| 4201 | hkdf->state = HKDF_STATE_STARTED; |
| 4202 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4203 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4204 | /* If no salt was provided, use an empty salt. */ |
| 4205 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4206 | { |
| 4207 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4208 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4209 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4210 | if( status != PSA_SUCCESS ) |
| 4211 | return( status ); |
| 4212 | hkdf->state = HKDF_STATE_STARTED; |
| 4213 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4214 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4215 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4216 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4217 | data, data_length ); |
| 4218 | if( status != PSA_SUCCESS ) |
| 4219 | return( status ); |
| 4220 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4221 | hkdf->prk, |
| 4222 | sizeof( hkdf->prk ) ); |
| 4223 | if( status != PSA_SUCCESS ) |
| 4224 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4225 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4226 | hkdf->block_number = 0; |
| 4227 | hkdf->state = HKDF_STATE_KEYED; |
| 4228 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4229 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4230 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4231 | return( PSA_ERROR_BAD_STATE ); |
| 4232 | if( hkdf->info_set ) |
| 4233 | return( PSA_ERROR_BAD_STATE ); |
| 4234 | hkdf->info_length = data_length; |
| 4235 | if( data_length != 0 ) |
| 4236 | { |
| 4237 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4238 | if( hkdf->info == NULL ) |
| 4239 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4240 | memcpy( hkdf->info, data, data_length ); |
| 4241 | } |
| 4242 | hkdf->info_set = 1; |
| 4243 | return( PSA_SUCCESS ); |
| 4244 | default: |
| 4245 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4246 | } |
| 4247 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4248 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4249 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4250 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4251 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4252 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4253 | const uint8_t *data, |
| 4254 | size_t data_length ) |
| 4255 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4256 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4257 | return( PSA_ERROR_BAD_STATE ); |
| 4258 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4259 | if( data_length != 0 ) |
| 4260 | { |
| 4261 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4262 | if( prf->seed == NULL ) |
| 4263 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4264 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4265 | memcpy( prf->seed, data, data_length ); |
| 4266 | prf->seed_length = data_length; |
| 4267 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4268 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4269 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4270 | |
| 4271 | return( PSA_SUCCESS ); |
| 4272 | } |
| 4273 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4274 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 4275 | psa_algorithm_t hash_alg, |
| 4276 | const uint8_t *data, |
| 4277 | size_t data_length ) |
| 4278 | { |
| 4279 | psa_status_t status; |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4280 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4281 | return( PSA_ERROR_BAD_STATE ); |
| 4282 | |
| 4283 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 4284 | if( status != PSA_SUCCESS ) |
| 4285 | return( status ); |
| 4286 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4287 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4288 | |
| 4289 | return( PSA_SUCCESS ); |
| 4290 | } |
| 4291 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4292 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 4293 | const uint8_t *data, |
| 4294 | size_t data_length ) |
| 4295 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4296 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4297 | return( PSA_ERROR_BAD_STATE ); |
| 4298 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4299 | if( data_length != 0 ) |
| 4300 | { |
| 4301 | prf->label = mbedtls_calloc( 1, data_length ); |
| 4302 | if( prf->label == NULL ) |
| 4303 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4304 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4305 | memcpy( prf->label, data, data_length ); |
| 4306 | prf->label_length = data_length; |
| 4307 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4308 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4309 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4310 | |
| 4311 | return( PSA_SUCCESS ); |
| 4312 | } |
| 4313 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4314 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 4315 | psa_algorithm_t hash_alg, |
| 4316 | psa_key_derivation_step_t step, |
| 4317 | const uint8_t *data, |
| 4318 | size_t data_length ) |
| 4319 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4320 | switch( step ) |
| 4321 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4322 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 4323 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4324 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4325 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4326 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4327 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4328 | default: |
| 4329 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4330 | } |
| 4331 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4332 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4333 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 4334 | |
| 4335 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4336 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4337 | psa_tls12_prf_key_derivation_t *prf, |
| 4338 | psa_algorithm_t hash_alg, |
| 4339 | const uint8_t *data, |
| 4340 | size_t data_length ) |
| 4341 | { |
| 4342 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4343 | uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ]; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4344 | uint8_t *cur = pms; |
| 4345 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4346 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4347 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4348 | |
| 4349 | /* Quoting RFC 4279, Section 2: |
| 4350 | * |
| 4351 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4352 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4353 | * uint16 with the value N, and the PSK itself. |
| 4354 | */ |
| 4355 | |
| 4356 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 4357 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 4358 | memset( cur, 0, data_length ); |
| 4359 | cur += data_length; |
| 4360 | *cur++ = pms[0]; |
| 4361 | *cur++ = pms[1]; |
| 4362 | memcpy( cur, data, data_length ); |
| 4363 | cur += data_length; |
| 4364 | |
| 4365 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 4366 | |
| 4367 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 4368 | return( status ); |
| 4369 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4370 | |
| 4371 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4372 | psa_tls12_prf_key_derivation_t *prf, |
| 4373 | psa_algorithm_t hash_alg, |
| 4374 | psa_key_derivation_step_t step, |
| 4375 | const uint8_t *data, |
| 4376 | size_t data_length ) |
| 4377 | { |
| 4378 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4379 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4380 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 4381 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4382 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4383 | |
| 4384 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 4385 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4386 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4387 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4388 | /** Check whether the given key type is acceptable for the given |
| 4389 | * input step of a key derivation. |
| 4390 | * |
| 4391 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4392 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4393 | * Both secret and non-secret inputs can alternatively have the type |
| 4394 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4395 | * that the input was passed as a buffer rather than via a key object. |
| 4396 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4397 | static int psa_key_derivation_check_input_type( |
| 4398 | psa_key_derivation_step_t step, |
| 4399 | psa_key_type_t key_type ) |
| 4400 | { |
| 4401 | switch( step ) |
| 4402 | { |
| 4403 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4404 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 4405 | return( PSA_SUCCESS ); |
| 4406 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4407 | return( PSA_SUCCESS ); |
| 4408 | break; |
| 4409 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4410 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4411 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4412 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4413 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 4414 | return( PSA_SUCCESS ); |
| 4415 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4416 | return( PSA_SUCCESS ); |
| 4417 | break; |
| 4418 | } |
| 4419 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4420 | } |
| 4421 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4422 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4423 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4424 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4425 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4426 | const uint8_t *data, |
| 4427 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4428 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4429 | psa_status_t status; |
| 4430 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 4431 | |
| 4432 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4433 | if( status != PSA_SUCCESS ) |
| 4434 | goto exit; |
| 4435 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4436 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4437 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4438 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4439 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4440 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4441 | step, data, data_length ); |
| 4442 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4443 | else |
| 4444 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4445 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4446 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4447 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4448 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 4449 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 4450 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4451 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4452 | else |
| 4453 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4454 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4455 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4456 | { |
| 4457 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 4458 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 4459 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4460 | } |
| 4461 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4462 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4463 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4464 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4465 | (void) data; |
| 4466 | (void) data_length; |
| 4467 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4468 | return( PSA_ERROR_BAD_STATE ); |
| 4469 | } |
| 4470 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4471 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4472 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4473 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4474 | return( status ); |
| 4475 | } |
| 4476 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4477 | psa_status_t psa_key_derivation_input_bytes( |
| 4478 | psa_key_derivation_operation_t *operation, |
| 4479 | psa_key_derivation_step_t step, |
| 4480 | const uint8_t *data, |
| 4481 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4482 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4483 | return( psa_key_derivation_input_internal( operation, step, |
| 4484 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 4485 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4486 | } |
| 4487 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4488 | psa_status_t psa_key_derivation_input_key( |
| 4489 | psa_key_derivation_operation_t *operation, |
| 4490 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4491 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4492 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4493 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4494 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4495 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4496 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4497 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4498 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4499 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4500 | { |
| 4501 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4502 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4503 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4504 | |
| 4505 | /* Passing a key object as a SECRET input unlocks the permission |
| 4506 | * to output to a key object. */ |
| 4507 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4508 | operation->can_output_key = 1; |
| 4509 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4510 | status = psa_key_derivation_input_internal( operation, |
| 4511 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4512 | slot->key.data, |
| 4513 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4514 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4515 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4516 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4517 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4518 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4519 | |
| 4520 | |
| 4521 | |
| 4522 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4523 | /* Key agreement */ |
| 4524 | /****************************************************************/ |
| 4525 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4526 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4527 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 4528 | size_t peer_key_length, |
| 4529 | const mbedtls_ecp_keypair *our_key, |
| 4530 | uint8_t *shared_secret, |
| 4531 | size_t shared_secret_size, |
| 4532 | size_t *shared_secret_length ) |
| 4533 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 4534 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4535 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4536 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4537 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 4538 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4539 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4540 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4541 | status = mbedtls_psa_ecp_load_representation( |
| 4542 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4543 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4544 | peer_key, |
| 4545 | peer_key_length, |
| 4546 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4547 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4548 | goto exit; |
| 4549 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4550 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4551 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4552 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4553 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4554 | status = mbedtls_to_psa_error( |
| 4555 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 4556 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4557 | goto exit; |
| 4558 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4559 | status = mbedtls_to_psa_error( |
| 4560 | mbedtls_ecdh_calc_secret( &ecdh, |
| 4561 | shared_secret_length, |
| 4562 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4563 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4564 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4565 | if( status != PSA_SUCCESS ) |
| 4566 | goto exit; |
| 4567 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 4568 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4569 | |
| 4570 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 4571 | if( status != PSA_SUCCESS ) |
| 4572 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4573 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4574 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 4575 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4576 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4577 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4578 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4579 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4580 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4581 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4582 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4583 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 4584 | psa_key_slot_t *private_key, |
| 4585 | const uint8_t *peer_key, |
| 4586 | size_t peer_key_length, |
| 4587 | uint8_t *shared_secret, |
| 4588 | size_t shared_secret_size, |
| 4589 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4590 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4591 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4592 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4593 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4594 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4595 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4596 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4597 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4598 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 4599 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4600 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4601 | private_key->key.data, |
| 4602 | private_key->key.bytes, |
| 4603 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4604 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4605 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4606 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4607 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4608 | shared_secret, shared_secret_size, |
| 4609 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4610 | mbedtls_ecp_keypair_free( ecp ); |
| 4611 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4612 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4613 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4614 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4615 | (void) private_key; |
| 4616 | (void) peer_key; |
| 4617 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4618 | (void) shared_secret; |
| 4619 | (void) shared_secret_size; |
| 4620 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4621 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4622 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4623 | } |
| 4624 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4625 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4626 | * to potentially free embedded data structures and wipe confidential data. |
| 4627 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4628 | static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4629 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4630 | psa_key_slot_t *private_key, |
| 4631 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4632 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4633 | { |
| 4634 | psa_status_t status; |
| 4635 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4636 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4637 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4638 | |
| 4639 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4640 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4641 | status = psa_key_agreement_raw_internal( ka_alg, |
| 4642 | private_key, |
| 4643 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4644 | shared_secret, |
| 4645 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4646 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4647 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4648 | goto exit; |
| 4649 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4650 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4651 | * the shared secret. A shared secret is permitted wherever a key |
| 4652 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4653 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4654 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4655 | shared_secret, |
| 4656 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4657 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4658 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4659 | return( status ); |
| 4660 | } |
| 4661 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4663 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4664 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4665 | const uint8_t *peer_key, |
| 4666 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4667 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4668 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4669 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4670 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4671 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4672 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4673 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4674 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4675 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4676 | if( status != PSA_SUCCESS ) |
| 4677 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4678 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4679 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4680 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4681 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4682 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4683 | else |
| 4684 | { |
| 4685 | /* If a private key has been added as SECRET, we allow the derived |
| 4686 | * key material to be used as a key in PSA Crypto. */ |
| 4687 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4688 | operation->can_output_key = 1; |
| 4689 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4690 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4691 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4692 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4693 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4694 | } |
| 4695 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4696 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4697 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4698 | const uint8_t *peer_key, |
| 4699 | size_t peer_key_length, |
| 4700 | uint8_t *output, |
| 4701 | size_t output_size, |
| 4702 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4703 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4704 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4705 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4706 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4707 | |
| 4708 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 4709 | { |
| 4710 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4711 | goto exit; |
| 4712 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4713 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4714 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4715 | if( status != PSA_SUCCESS ) |
| 4716 | goto exit; |
| 4717 | |
| 4718 | status = psa_key_agreement_raw_internal( alg, slot, |
| 4719 | peer_key, peer_key_length, |
| 4720 | output, output_size, |
| 4721 | output_length ); |
| 4722 | |
| 4723 | exit: |
| 4724 | if( status != PSA_SUCCESS ) |
| 4725 | { |
| 4726 | /* If an error happens and is not handled properly, the output |
| 4727 | * may be used as a key to protect sensitive data. Arrange for such |
| 4728 | * a key to be random, which is likely to result in decryption or |
| 4729 | * verification errors. This is better than filling the buffer with |
| 4730 | * some constant data such as zeros, which would result in the data |
| 4731 | * being protected with a reproducible, easily knowable key. |
| 4732 | */ |
| 4733 | psa_generate_random( output, output_size ); |
| 4734 | *output_length = output_size; |
| 4735 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4736 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4737 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4738 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4739 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4740 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4741 | |
| 4742 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4743 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4744 | /****************************************************************/ |
| 4745 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4746 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4747 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4748 | /** Initialize the PSA random generator. |
| 4749 | */ |
| 4750 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 4751 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4752 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4753 | memset( rng, 0, sizeof( *rng ) ); |
| 4754 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4755 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4756 | /* Set default configuration if |
| 4757 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 4758 | if( rng->entropy_init == NULL ) |
| 4759 | rng->entropy_init = mbedtls_entropy_init; |
| 4760 | if( rng->entropy_free == NULL ) |
| 4761 | rng->entropy_free = mbedtls_entropy_free; |
| 4762 | |
| 4763 | rng->entropy_init( &rng->entropy ); |
| 4764 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 4765 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 4766 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 4767 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 4768 | mbedtls_entropy_add_source( &rng->entropy, |
| 4769 | mbedtls_nv_seed_poll, NULL, |
| 4770 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 4771 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 4772 | #endif |
| 4773 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4774 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4775 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | /** Deinitialize the PSA random generator. |
| 4779 | */ |
| 4780 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 4781 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4782 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4783 | memset( rng, 0, sizeof( *rng ) ); |
| 4784 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4785 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4786 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4787 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | /** Seed the PSA random generator. |
| 4791 | */ |
| 4792 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 4793 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4794 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4795 | /* Do nothing: the external RNG seeds itself. */ |
| 4796 | (void) rng; |
| 4797 | return( PSA_SUCCESS ); |
| 4798 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4799 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4800 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 4801 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4802 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4803 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4806 | psa_status_t psa_generate_random( uint8_t *output, |
| 4807 | size_t output_size ) |
| 4808 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4809 | GUARD_MODULE_INITIALIZED; |
| 4810 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4811 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4812 | |
| 4813 | size_t output_length = 0; |
| 4814 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 4815 | output, output_size, |
| 4816 | &output_length ); |
| 4817 | if( status != PSA_SUCCESS ) |
| 4818 | return( status ); |
| 4819 | /* Breaking up a request into smaller chunks is currently not supported |
| 4820 | * for the extrernal RNG interface. */ |
| 4821 | if( output_length != output_size ) |
| 4822 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 4823 | return( PSA_SUCCESS ); |
| 4824 | |
| 4825 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4826 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 4827 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 4828 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 4829 | size_t request_size = |
| 4830 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 4831 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 4832 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 4833 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 4834 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 4835 | if( ret != 0 ) |
| 4836 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 4837 | output_size -= request_size; |
| 4838 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 4839 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 4840 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4841 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4844 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 4845 | * |
| 4846 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 4847 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 4848 | * PSA API doesn't support passing an explicit state. |
| 4849 | * |
| 4850 | * In the non-external case, psa_generate_random() calls an |
| 4851 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 4852 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 4853 | * instead of doing this back-and-forth between the PSA API and the |
| 4854 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 4855 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4856 | */ |
| 4857 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4858 | int mbedtls_psa_get_random( void *p_rng, |
| 4859 | unsigned char *output, |
| 4860 | size_t output_size ) |
| 4861 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 4862 | /* This function takes a pointer to the RNG state because that's what |
| 4863 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 4864 | * its own state internally and doesn't let the caller access that state. |
| 4865 | * So we just ignore the state parameter, and in practice we'll pass |
| 4866 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4867 | (void) p_rng; |
| 4868 | psa_status_t status = psa_generate_random( output, output_size ); |
| 4869 | if( status == PSA_SUCCESS ) |
| 4870 | return( 0 ); |
| 4871 | else |
| 4872 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 4873 | } |
| 4874 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4875 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 4876 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 4877 | #include "mbedtls/entropy_poll.h" |
| 4878 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 4879 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4880 | size_t seed_size ) |
| 4881 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4882 | if( global_data.initialized ) |
| 4883 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 4884 | |
| 4885 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 4886 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 4887 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 4888 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4889 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 4890 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4891 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 4892 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4893 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4894 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4895 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4896 | * \param type The key type |
| 4897 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4898 | * |
| 4899 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4900 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4901 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 4902 | * The size in bits of the key is not valid. |
| 4903 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 4904 | * The type and/or the size in bits of the key or the combination of |
| 4905 | * the two is not supported. |
| 4906 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4907 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 4908 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4909 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 4910 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4911 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4912 | if( key_type_is_raw_bytes( type ) ) |
| 4913 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 4914 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4915 | if( status != PSA_SUCCESS ) |
| 4916 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4917 | } |
| 4918 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 4919 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4920 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 4921 | { |
| 4922 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 4923 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 4924 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4925 | /* Accept only byte-aligned keys, for the same reasons as |
| 4926 | * in psa_import_rsa_key(). */ |
| 4927 | if( bits % 8 != 0 ) |
| 4928 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4929 | } |
| 4930 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 4931 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4932 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 4933 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4934 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 4935 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 4936 | /* To avoid empty block, return successfully here. */ |
| 4937 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4938 | } |
| 4939 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 4940 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4941 | { |
| 4942 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4943 | } |
| 4944 | |
| 4945 | return( PSA_SUCCESS ); |
| 4946 | } |
| 4947 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 4948 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4949 | const psa_key_attributes_t *attributes, |
| 4950 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4951 | { |
| 4952 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4953 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4954 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4955 | if( ( attributes->domain_parameters == NULL ) && |
| 4956 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4957 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4958 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4959 | if( key_type_is_raw_bytes( type ) ) |
| 4960 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4961 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4962 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4963 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 4964 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4965 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4966 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4967 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4968 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4969 | } |
| 4970 | else |
| 4971 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4972 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 4973 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4974 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 4975 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 4976 | key_buffer, |
| 4977 | key_buffer_size, |
| 4978 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4979 | } |
| 4980 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4981 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4982 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4983 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 4984 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4985 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 4986 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 4987 | key_buffer, |
| 4988 | key_buffer_size, |
| 4989 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4990 | } |
| 4991 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4992 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4993 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4994 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4995 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4996 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4997 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4998 | return( PSA_SUCCESS ); |
| 4999 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5000 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5001 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5002 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5003 | { |
| 5004 | psa_status_t status; |
| 5005 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5006 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5007 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5008 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5009 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5010 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5011 | /* Reject any attempt to create a zero-length key so that we don't |
| 5012 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5013 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5014 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5015 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5016 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 5017 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5018 | if( status != PSA_SUCCESS ) |
| 5019 | goto exit; |
| 5020 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5021 | /* In the case of a transparent key or an opaque key stored in local |
| 5022 | * storage (thus not in the case of generating a key in a secure element |
| 5023 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5024 | * hold the generated key material. */ |
| 5025 | if( slot->key.data == NULL ) |
| 5026 | { |
| 5027 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 5028 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 5029 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5030 | status = psa_validate_key_type_and_size_for_key_generation( |
| 5031 | attributes->core.type, attributes->core.bits ); |
| 5032 | if( status != PSA_SUCCESS ) |
| 5033 | goto exit; |
| 5034 | |
| 5035 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 5036 | attributes->core.type, |
| 5037 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5038 | } |
| 5039 | else |
| 5040 | { |
| 5041 | status = psa_driver_wrapper_get_key_buffer_size( |
| 5042 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5043 | if( status != PSA_SUCCESS ) |
| 5044 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5045 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5046 | |
| 5047 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 5048 | if( status != PSA_SUCCESS ) |
| 5049 | goto exit; |
| 5050 | } |
| 5051 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 5052 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5053 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5054 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5055 | if( status != PSA_SUCCESS ) |
| 5056 | psa_remove_key_data_from_memory( slot ); |
| 5057 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5058 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5059 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5060 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5061 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5062 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5063 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5064 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5065 | } |
| 5066 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5067 | /****************************************************************/ |
| 5068 | /* Module setup */ |
| 5069 | /****************************************************************/ |
| 5070 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5071 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5072 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5073 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5074 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5075 | { |
| 5076 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5077 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5078 | global_data.rng.entropy_init = entropy_init; |
| 5079 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5080 | return( PSA_SUCCESS ); |
| 5081 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5082 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5083 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5084 | void mbedtls_psa_crypto_free( void ) |
| 5085 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5086 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5087 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5088 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5089 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5090 | } |
| 5091 | /* Wipe all remaining data, including configuration. |
| 5092 | * In particular, this sets all state indicator to the value |
| 5093 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5094 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5095 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5096 | /* Unregister all secure element drivers, so that we restart from |
| 5097 | * a pristine state. */ |
| 5098 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5099 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5100 | } |
| 5101 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5102 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5103 | /** Recover a transaction that was interrupted by a power failure. |
| 5104 | * |
| 5105 | * This function is called during initialization, before psa_crypto_init() |
| 5106 | * returns. If this function returns a failure status, the initialization |
| 5107 | * fails. |
| 5108 | */ |
| 5109 | static psa_status_t psa_crypto_recover_transaction( |
| 5110 | const psa_crypto_transaction_t *transaction ) |
| 5111 | { |
| 5112 | switch( transaction->unknown.type ) |
| 5113 | { |
| 5114 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5115 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5116 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5117 | * is implemented. |
| 5118 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5119 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5120 | default: |
| 5121 | /* We found an unsupported transaction in the storage. |
| 5122 | * We don't know what state the storage is in. Give up. */ |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 5123 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5127 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5128 | psa_status_t psa_crypto_init( void ) |
| 5129 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5130 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5131 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5132 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5133 | if( global_data.initialized != 0 ) |
| 5134 | return( PSA_SUCCESS ); |
| 5135 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5136 | /* Initialize and seed the random generator. */ |
| 5137 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5138 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5139 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5140 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5141 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5142 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5143 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5144 | status = psa_initialize_key_slots( ); |
| 5145 | if( status != PSA_SUCCESS ) |
| 5146 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5147 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5148 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5149 | status = psa_init_all_se_drivers( ); |
| 5150 | if( status != PSA_SUCCESS ) |
| 5151 | goto exit; |
| 5152 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5153 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5154 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5155 | status = psa_crypto_load_transaction( ); |
| 5156 | if( status == PSA_SUCCESS ) |
| 5157 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5158 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5159 | if( status != PSA_SUCCESS ) |
| 5160 | goto exit; |
| 5161 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5162 | } |
| 5163 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5164 | { |
| 5165 | /* There's no transaction to complete. It's all good. */ |
| 5166 | status = PSA_SUCCESS; |
| 5167 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5168 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5169 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5170 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5171 | global_data.initialized = 1; |
| 5172 | |
| 5173 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5174 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5175 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5176 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |