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 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 29 | #include "psa/crypto.h" |
| 30 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 31 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 33 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 35 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 36 | #include "psa_crypto_hash.h" |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 37 | #include "psa_crypto_mac.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 | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 95 | /****************************************************************/ |
| 96 | /* Global data, support functions and library management */ |
| 97 | /****************************************************************/ |
| 98 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 99 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 100 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 101 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 104 | /* Values for psa_global_data_t::rng_state */ |
| 105 | #define RNG_NOT_INITIALIZED 0 |
| 106 | #define RNG_INITIALIZED 1 |
| 107 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 108 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 109 | typedef struct |
| 110 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 111 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 112 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 113 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 114 | } psa_global_data_t; |
| 115 | |
| 116 | static psa_global_data_t global_data; |
| 117 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 118 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 119 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 120 | &global_data.rng.drbg; |
| 121 | #endif |
| 122 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 123 | #define GUARD_MODULE_INITIALIZED \ |
| 124 | if( global_data.initialized == 0 ) \ |
| 125 | return( PSA_ERROR_BAD_STATE ); |
| 126 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 127 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 128 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 129 | /* Mbed TLS error codes can combine a high-level error code and a |
| 130 | * low-level error code. The low-level error usually reflects the |
| 131 | * root cause better, so dispatch on that preferably. */ |
| 132 | int low_level_ret = - ( -ret & 0x007f ); |
| 133 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 134 | { |
| 135 | case 0: |
| 136 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 137 | |
| 138 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 139 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 140 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 141 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 142 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 143 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 144 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 145 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 146 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 147 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 148 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 149 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 150 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 151 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 152 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 153 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 154 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 155 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 156 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 157 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 158 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 159 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 160 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 162 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 163 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 165 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 166 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 167 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 168 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 169 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 170 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 172 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 173 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 175 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 176 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 177 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 178 | |
| 179 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 180 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 181 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 182 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 183 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 184 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 185 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 186 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 187 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 188 | |
| 189 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 190 | return( PSA_ERROR_BAD_STATE ); |
| 191 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 192 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 193 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 194 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 195 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 196 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 197 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 198 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 199 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 200 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 201 | return( PSA_ERROR_INVALID_PADDING ); |
| 202 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 203 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 204 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 205 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 206 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 207 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 208 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 209 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 210 | |
| 211 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 212 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 213 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 214 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 215 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 216 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 217 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 218 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 219 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 220 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 221 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 222 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 223 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 224 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 225 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 226 | |
| 227 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 228 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 229 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 230 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 231 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 232 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 233 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 234 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 235 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 236 | |
| 237 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 238 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 239 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 240 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 241 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 242 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 243 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 244 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 245 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 246 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 247 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 248 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 249 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 250 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 251 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 252 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 253 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 254 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 255 | #endif |
| 256 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 257 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 258 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 259 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 260 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 261 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 262 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 263 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 264 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 266 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 267 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 268 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 269 | return( PSA_ERROR_STORAGE_FAILURE ); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 270 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 271 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 272 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 273 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 274 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 275 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 277 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 280 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 281 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 283 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 284 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 285 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 287 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 288 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 289 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 290 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 291 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 292 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 293 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 294 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 295 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 298 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 299 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 300 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 301 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 302 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 303 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 304 | return( PSA_ERROR_NOT_PERMITTED ); |
| 305 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 307 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 308 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 309 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 311 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 312 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 313 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 314 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 315 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 316 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 317 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 318 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 319 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 320 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 321 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 322 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 323 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 324 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 325 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 326 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 327 | return( PSA_ERROR_INVALID_PADDING ); |
| 328 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 331 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 332 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 333 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 334 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 335 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 336 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 337 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 338 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 339 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 340 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 341 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 342 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 343 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 344 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 345 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 346 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 347 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 348 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 349 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 350 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 351 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 352 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 353 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 354 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 355 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 356 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 357 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 358 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 359 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 360 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 361 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 362 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 363 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 364 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 365 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 366 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 367 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 368 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 369 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 370 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 371 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 372 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 373 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 374 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 375 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 376 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 377 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 381 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 382 | |
| 383 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 384 | /****************************************************************/ |
| 385 | /* Key management */ |
| 386 | /****************************************************************/ |
| 387 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 388 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 389 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 390 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 391 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 392 | } |
| 393 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 394 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 395 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 396 | * current test driver in key_management.c is using this function |
| 397 | * when accelerators are used for ECC key pair and public key. |
| 398 | * Once that dependency is resolved these guards can be removed. |
| 399 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 400 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 401 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 402 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 403 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 404 | 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] | 405 | size_t bits, |
| 406 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 407 | { |
| 408 | switch( curve ) |
| 409 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 410 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 413 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 414 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 415 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 416 | #endif |
| 417 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 418 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 419 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 420 | #endif |
| 421 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 422 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 424 | #endif |
| 425 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 426 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 427 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 428 | #endif |
| 429 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 430 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 431 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 432 | case 528: |
| 433 | if( bits_is_sloppy ) |
| 434 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 435 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 436 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | } |
| 438 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 439 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 440 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 441 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 442 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 443 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 444 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 445 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 446 | #endif |
| 447 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 448 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 450 | #endif |
| 451 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 452 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 453 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 454 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 455 | } |
| 456 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 457 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 458 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 459 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 460 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 461 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 462 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 463 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 464 | case 256: |
| 465 | if( bits_is_sloppy ) |
| 466 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 467 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 468 | #endif |
| 469 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 470 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 471 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 472 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 473 | } |
| 474 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 475 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 476 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 477 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 478 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 479 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 480 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 481 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 482 | #endif |
| 483 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 484 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 485 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 486 | #endif |
| 487 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 488 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 489 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 490 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 491 | } |
| 492 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 493 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 494 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 495 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 496 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 497 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 498 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 499 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 500 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 501 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 502 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 503 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 504 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 505 | { |
| 506 | /* Check that the bit size is acceptable for the key type */ |
| 507 | switch( type ) |
| 508 | { |
| 509 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 510 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 511 | case PSA_KEY_TYPE_DERIVE: |
| 512 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 513 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 514 | case PSA_KEY_TYPE_AES: |
| 515 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 516 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 517 | break; |
| 518 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 519 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 520 | case PSA_KEY_TYPE_ARIA: |
| 521 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 522 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 523 | break; |
| 524 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 525 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 526 | case PSA_KEY_TYPE_CAMELLIA: |
| 527 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 528 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 529 | break; |
| 530 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 531 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 532 | case PSA_KEY_TYPE_DES: |
| 533 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 534 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 535 | break; |
| 536 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 537 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 538 | case PSA_KEY_TYPE_ARC4: |
| 539 | if( bits < 8 || bits > 2048 ) |
| 540 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 541 | break; |
| 542 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 543 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 544 | case PSA_KEY_TYPE_CHACHA20: |
| 545 | if( bits != 256 ) |
| 546 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 547 | break; |
| 548 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 549 | default: |
| 550 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 551 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 552 | if( bits % 8 != 0 ) |
| 553 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 554 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 555 | return( PSA_SUCCESS ); |
| 556 | } |
| 557 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 558 | /** 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] | 559 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 560 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 561 | * 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] | 562 | * |
| 563 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 564 | * \param[in] key_type The key type of the key to be used with the |
| 565 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 566 | * |
| 567 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 568 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 569 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 570 | * 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] | 571 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 572 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 573 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 574 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 575 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 576 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 577 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 578 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 579 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 580 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 581 | |
| 582 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 583 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 584 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 585 | * key. */ |
| 586 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 587 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 588 | { |
| 589 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 590 | * the block length (larger than 1) for block ciphers. */ |
| 591 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 592 | return( PSA_SUCCESS ); |
| 593 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 594 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 595 | |
| 596 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 599 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 600 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 601 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 602 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 603 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 604 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 605 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 606 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 607 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 608 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 609 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 610 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 613 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 614 | const uint8_t* data, |
| 615 | size_t data_length ) |
| 616 | { |
| 617 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 618 | data_length ); |
| 619 | if( status != PSA_SUCCESS ) |
| 620 | return( status ); |
| 621 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 622 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 623 | return( PSA_SUCCESS ); |
| 624 | } |
| 625 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 626 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 627 | const psa_key_attributes_t *attributes, |
| 628 | const uint8_t *data, size_t data_length, |
| 629 | uint8_t *key_buffer, size_t key_buffer_size, |
| 630 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 631 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 632 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 633 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 634 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 635 | /* zero-length keys are never supported. */ |
| 636 | if( data_length == 0 ) |
| 637 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 638 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 639 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 640 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 641 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 642 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 643 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 644 | if( data_length > SIZE_MAX / 8 ) |
| 645 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 646 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 647 | /* Enforce a size limit, and in particular ensure that the bit |
| 648 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 649 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 650 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 651 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 652 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 653 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 654 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 655 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 656 | /* Copy the key material. */ |
| 657 | memcpy( key_buffer, data, data_length ); |
| 658 | *key_buffer_length = data_length; |
| 659 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 660 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 661 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 662 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 663 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 664 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 665 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 666 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 667 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 668 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 669 | return( mbedtls_psa_ecp_import_key( attributes, |
| 670 | data, data_length, |
| 671 | key_buffer, key_buffer_size, |
| 672 | key_buffer_length, |
| 673 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 674 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 675 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 676 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 677 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 678 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 679 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 680 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 681 | return( mbedtls_psa_rsa_import_key( attributes, |
| 682 | data, data_length, |
| 683 | key_buffer, key_buffer_size, |
| 684 | key_buffer_length, |
| 685 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 686 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 687 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 688 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 689 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 690 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 691 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 694 | /** Calculate the intersection of two algorithm usage policies. |
| 695 | * |
| 696 | * Return 0 (which allows no operation) on incompatibility. |
| 697 | */ |
| 698 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 699 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 700 | psa_algorithm_t alg1, |
| 701 | psa_algorithm_t alg2 ) |
| 702 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 703 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 704 | if( alg1 == alg2 ) |
| 705 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 706 | /* If the policies are from the same hash-and-sign family, check |
| 707 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 708 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 709 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 710 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 711 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 712 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 713 | return( alg2 ); |
| 714 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 715 | return( alg1 ); |
| 716 | } |
| 717 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 718 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 719 | * restrictive tag length. */ |
| 720 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 721 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 722 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 723 | { |
| 724 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 725 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 726 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 727 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 728 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 729 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 730 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 731 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 732 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 733 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | } |
| 735 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 736 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 737 | ( alg1_len <= alg2_len ) ) |
| 738 | { |
| 739 | return( alg2 ); |
| 740 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 741 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 742 | ( alg2_len <= alg1_len ) ) |
| 743 | { |
| 744 | return( alg1 ); |
| 745 | } |
| 746 | } |
| 747 | /* If the policies are from the same MAC family, check whether one |
| 748 | * of them is a minimum-MAC-length policy. Calculate the most |
| 749 | * restrictive tag length. */ |
| 750 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 751 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 752 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 753 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 754 | /* Validate the combination of key type and algorithm. Since the base |
| 755 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 756 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 757 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 758 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 759 | /* Get the (exact or at-least) output lengths for both sides of the |
| 760 | * requested intersection. None of the currently supported algorithms |
| 761 | * have an output length dependent on the actual key size, so setting it |
| 762 | * to a bogus value of 0 is currently OK. |
| 763 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 764 | * Note that for at-least-this-length wildcard algorithms, the output |
| 765 | * length is set to the shortest allowed length, which allows us to |
| 766 | * calculate the most restrictive tag length for the intersection. */ |
| 767 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 768 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 769 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 770 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 771 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 772 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 773 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 774 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 775 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 776 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 777 | |
| 778 | /* If only one is an at-least-this-length policy, the intersection would |
| 779 | * be the other (fixed-length) policy as long as said fixed length is |
| 780 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 781 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 782 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 783 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 784 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 785 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 786 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 787 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 788 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 789 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 790 | /* If none of them are wildcards, check whether they define the same tag |
| 791 | * length. This is still possible here when one is default-length and |
| 792 | * the other specific-length. Ensure to always return the |
| 793 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 794 | if( alg1_len == alg2_len ) |
| 795 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 796 | } |
| 797 | /* If the policies are incompatible, allow nothing. */ |
| 798 | return( 0 ); |
| 799 | } |
| 800 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 801 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 802 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 803 | psa_algorithm_t requested_alg ) |
| 804 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 805 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 806 | if( requested_alg == policy_alg ) |
| 807 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 808 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 809 | * and requested_alg is the same hash-and-sign family with any hash, |
| 810 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 811 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 812 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 813 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 814 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 815 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 816 | } |
| 817 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 818 | * the requested algorithm, check the requested tag length to be |
| 819 | * equal-length or longer than the wildcard-specified length. */ |
| 820 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 821 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 822 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 823 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 824 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 825 | { |
| 826 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 827 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 828 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 829 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 830 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 831 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 832 | PSA_ALG_IS_MAC( requested_alg ) && |
| 833 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 834 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 835 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 836 | /* Validate the combination of key type and algorithm. Since the policy |
| 837 | * and requested algorithms are the same, we only need this once. */ |
| 838 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 839 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 840 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 841 | /* Get both the requested output length for the algorithm which is to be |
| 842 | * verified, and the default output length for the base algorithm. |
| 843 | * Note that none of the currently supported algorithms have an output |
| 844 | * length dependent on actual key size, so setting it to a bogus value |
| 845 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 846 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 847 | key_type, 0, requested_alg ); |
| 848 | size_t default_output_length = PSA_MAC_LENGTH( |
| 849 | key_type, 0, |
| 850 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 851 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 852 | /* If the policy is default-length, only allow an algorithm with |
| 853 | * a declared exact-length matching the default. */ |
| 854 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 855 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 856 | |
| 857 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 858 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 859 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 860 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 861 | { |
| 862 | return( 1 ); |
| 863 | } |
| 864 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 865 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 866 | * check for the requested MAC length to be equal to or longer than the |
| 867 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 868 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 869 | { |
| 870 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 871 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 872 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 873 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 874 | /* 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] | 875 | * a key derivation with that key agreement should also be allowed. This |
| 876 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 877 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 878 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 879 | { |
| 880 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 881 | policy_alg ); |
| 882 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 883 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 884 | return( 0 ); |
| 885 | } |
| 886 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 887 | /** Test whether a policy permits an algorithm. |
| 888 | * |
| 889 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 890 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 891 | * \note This function requires providing the key type for which the policy is |
| 892 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 893 | * have different properties depending on what kind of cipher it is |
| 894 | * combined with. |
| 895 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 896 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 897 | * allowed by the \p policy. |
| 898 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 899 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 900 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 901 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 902 | 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] | 903 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 904 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 905 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 906 | /* '0' is not a valid algorithm */ |
| 907 | if( alg == 0 ) |
| 908 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 909 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 910 | /* A requested algorithm cannot be a wildcard. */ |
| 911 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 912 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 913 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 914 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 915 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 916 | return( PSA_SUCCESS ); |
| 917 | else |
| 918 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 919 | } |
| 920 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 921 | /** Restrict a key policy based on a constraint. |
| 922 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 923 | * \note This function requires providing the key type for which the policy is |
| 924 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 925 | * have different properties depending on what kind of cipher it is |
| 926 | * combined with. |
| 927 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 928 | * \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] | 929 | * \param[in,out] policy The policy to restrict. |
| 930 | * \param[in] constraint The policy constraint to apply. |
| 931 | * |
| 932 | * \retval #PSA_SUCCESS |
| 933 | * \c *policy contains the intersection of the original value of |
| 934 | * \c *policy and \c *constraint. |
| 935 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 936 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 937 | * \c *policy is unchanged. |
| 938 | */ |
| 939 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 940 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 941 | psa_key_policy_t *policy, |
| 942 | const psa_key_policy_t *constraint ) |
| 943 | { |
| 944 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 945 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 946 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 947 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 948 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 949 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 950 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 951 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 952 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 953 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 954 | policy->usage &= constraint->usage; |
| 955 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 956 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 957 | return( PSA_SUCCESS ); |
| 958 | } |
| 959 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 960 | /** Get the description of a key given its identifier and policy constraints |
| 961 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 962 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 963 | * 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] | 964 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 965 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 966 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 967 | * In case of a persistent key, the function loads the description of the key |
| 968 | * into a key slot if not already done. |
| 969 | * |
| 970 | * On success, the returned key slot is locked. It is the responsibility of |
| 971 | * 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] | 972 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 973 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 974 | mbedtls_svc_key_id_t key, |
| 975 | psa_key_slot_t **p_slot, |
| 976 | psa_key_usage_t usage, |
| 977 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 978 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 979 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 980 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 981 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 982 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 983 | if( status != PSA_SUCCESS ) |
| 984 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 985 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 986 | |
| 987 | /* Enforce that usage policy for the key slot contains all the flags |
| 988 | * required by the usage parameter. There is one exception: public |
| 989 | * keys can always be exported, so we treat public key objects as |
| 990 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 991 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 992 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 993 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 994 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 995 | { |
| 996 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 997 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 998 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 999 | |
| 1000 | /* Enforce that the usage policy permits the requested algortihm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1001 | if( alg != 0 ) |
| 1002 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1003 | status = psa_key_policy_permits( &slot->attr.policy, |
| 1004 | slot->attr.type, |
| 1005 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1006 | if( status != PSA_SUCCESS ) |
| 1007 | goto error; |
| 1008 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1009 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1010 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1011 | |
| 1012 | error: |
| 1013 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1014 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1015 | |
| 1016 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1017 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1018 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1019 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1020 | * |
| 1021 | * A transparent key is a key for which the key material is directly |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1022 | * available, as opposed to a key in a secure element and/or to be used |
| 1023 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1024 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1025 | * This is a temporary function that may be used instead of |
| 1026 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1027 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1028 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1029 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1030 | * 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] | 1031 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1032 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1033 | mbedtls_svc_key_id_t key, |
| 1034 | psa_key_slot_t **p_slot, |
| 1035 | psa_key_usage_t usage, |
| 1036 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1037 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1038 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1039 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1040 | if( status != PSA_SUCCESS ) |
| 1041 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1042 | |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1043 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1044 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1045 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1046 | *p_slot = NULL; |
| 1047 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1048 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1049 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1050 | return( PSA_SUCCESS ); |
| 1051 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1052 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 1053 | 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] | 1054 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1055 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1056 | * initialized slot, so we can just free it. */ |
| 1057 | if( slot->key.data != NULL ) |
| 1058 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1059 | |
| 1060 | mbedtls_free( slot->key.data ); |
| 1061 | slot->key.data = NULL; |
| 1062 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1063 | |
| 1064 | return( PSA_SUCCESS ); |
| 1065 | } |
| 1066 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1067 | /** Completely wipe a slot in memory, including its policy. |
| 1068 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1069 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1070 | { |
| 1071 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1072 | |
| 1073 | /* |
| 1074 | * 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] | 1075 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1076 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1077 | * 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] | 1078 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1079 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1080 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1081 | { |
| 1082 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1083 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1084 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1085 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1086 | } |
| 1087 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1088 | /* Multipart operations may still be using the key. This is safe |
| 1089 | * because all multipart operation objects are independent from |
| 1090 | * the key slot: if they need to access the key after the setup |
| 1091 | * phase, they have a copy of the key. Note that this means that |
| 1092 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1093 | /* At this point, key material and other type-specific content has |
| 1094 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1095 | * zeroize because the metadata is not particularly sensitive. */ |
| 1096 | memset( slot, 0, sizeof( *slot ) ); |
| 1097 | return( status ); |
| 1098 | } |
| 1099 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1100 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1101 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1102 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1103 | psa_status_t status; /* status of the last operation */ |
| 1104 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1105 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1106 | psa_se_drv_table_entry_t *driver; |
| 1107 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1108 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1109 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1110 | return( PSA_SUCCESS ); |
| 1111 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1112 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1113 | * 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] | 1114 | * key, this will load the key description from persistent memory if not |
| 1115 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1116 | * the key is operated by an SE or not and this information is needed by |
| 1117 | * the current implementation. |
| 1118 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1119 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1120 | if( status != PSA_SUCCESS ) |
| 1121 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1122 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1123 | /* |
| 1124 | * If the key slot containing the key description is under access by the |
| 1125 | * library (apart from the present access), the key cannot be destroyed |
| 1126 | * yet. For the time being, just return in error. Eventually (to be |
| 1127 | * implemented), the key should be destroyed when all accesses have |
| 1128 | * stopped. |
| 1129 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1130 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1131 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1132 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1133 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1134 | } |
| 1135 | |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1136 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1137 | { |
| 1138 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1139 | * if we attempt it, depending on whether the key is merely read-only |
| 1140 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1141 | * Just do the best we can, which is to wipe the copy in memory |
| 1142 | * (done in this function's cleanup code). */ |
| 1143 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1144 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1147 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1148 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1149 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1150 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1151 | /* For a key in a secure element, we need to do three things: |
| 1152 | * remove the key file in internal storage, destroy the |
| 1153 | * key inside the secure element, and update the driver's |
| 1154 | * persistent data. Start a transaction that will encompass these |
| 1155 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1156 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1157 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1158 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1159 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1160 | status = psa_crypto_save_transaction( ); |
| 1161 | if( status != PSA_SUCCESS ) |
| 1162 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1163 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1164 | /* We should still try to destroy the key in the secure |
| 1165 | * element and the key metadata in storage. This is especially |
| 1166 | * important if the error is that the storage is full. |
| 1167 | * But how to do it exactly without risking an inconsistent |
| 1168 | * state after a reset? |
| 1169 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1170 | */ |
| 1171 | overall_status = status; |
| 1172 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1175 | status = psa_destroy_se_key( driver, |
| 1176 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1177 | if( overall_status == PSA_SUCCESS ) |
| 1178 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1179 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1180 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1181 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1182 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1183 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1184 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1185 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1186 | if( overall_status == PSA_SUCCESS ) |
| 1187 | overall_status = status; |
| 1188 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1189 | /* TODO: other slots may have a copy of the same key. We should |
| 1190 | * invalidate them. |
| 1191 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1192 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1193 | } |
| 1194 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1195 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1196 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1197 | if( driver != NULL ) |
| 1198 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1199 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1200 | if( overall_status == PSA_SUCCESS ) |
| 1201 | overall_status = status; |
| 1202 | status = psa_crypto_stop_transaction( ); |
| 1203 | if( overall_status == PSA_SUCCESS ) |
| 1204 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1205 | } |
| 1206 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1207 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1208 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1209 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1210 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1211 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1212 | overall_status = status; |
| 1213 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1216 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1217 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1218 | static psa_status_t psa_get_rsa_public_exponent( |
| 1219 | const mbedtls_rsa_context *rsa, |
| 1220 | psa_key_attributes_t *attributes ) |
| 1221 | { |
| 1222 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1223 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1224 | uint8_t *buffer = NULL; |
| 1225 | size_t buflen; |
| 1226 | mbedtls_mpi_init( &mpi ); |
| 1227 | |
| 1228 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1229 | if( ret != 0 ) |
| 1230 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1231 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1232 | { |
| 1233 | /* It's the default value, which is reported as an empty string, |
| 1234 | * so there's nothing to do. */ |
| 1235 | goto exit; |
| 1236 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1237 | |
| 1238 | buflen = mbedtls_mpi_size( &mpi ); |
| 1239 | buffer = mbedtls_calloc( 1, buflen ); |
| 1240 | if( buffer == NULL ) |
| 1241 | { |
| 1242 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1243 | goto exit; |
| 1244 | } |
| 1245 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1246 | if( ret != 0 ) |
| 1247 | goto exit; |
| 1248 | attributes->domain_parameters = buffer; |
| 1249 | attributes->domain_parameters_size = buflen; |
| 1250 | |
| 1251 | exit: |
| 1252 | mbedtls_mpi_free( &mpi ); |
| 1253 | if( ret != 0 ) |
| 1254 | mbedtls_free( buffer ); |
| 1255 | return( mbedtls_to_psa_error( ret ) ); |
| 1256 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1257 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1258 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1259 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1260 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1261 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1262 | 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] | 1263 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1264 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1265 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1266 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1267 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1268 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1269 | psa_reset_key_attributes( attributes ); |
| 1270 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1271 | 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] | 1272 | if( status != PSA_SUCCESS ) |
| 1273 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1274 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1275 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1276 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1277 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1278 | |
| 1279 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 0518f61 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1280 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1281 | psa_set_key_slot_number( attributes, |
| 1282 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1283 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1284 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1285 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1286 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1287 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1288 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1289 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1290 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
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 | */ |
Ronald Cron | 41e8006 | 2021-08-24 16:00:51 +0200 | [diff] [blame^] | 1295 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1296 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1297 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1298 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1299 | status = mbedtls_psa_rsa_load_representation( |
| 1300 | slot->attr.type, |
| 1301 | slot->key.data, |
| 1302 | slot->key.bytes, |
| 1303 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1304 | if( status != PSA_SUCCESS ) |
| 1305 | break; |
| 1306 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1307 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1308 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1309 | mbedtls_rsa_free( rsa ); |
| 1310 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1311 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1312 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1313 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1314 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1315 | default: |
| 1316 | /* Nothing else to do. */ |
| 1317 | break; |
| 1318 | } |
| 1319 | |
| 1320 | if( status != PSA_SUCCESS ) |
| 1321 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1322 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1323 | unlock_status = psa_unlock_key_slot( slot ); |
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 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1326 | } |
| 1327 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1328 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1329 | psa_status_t psa_get_key_slot_number( |
| 1330 | const psa_key_attributes_t *attributes, |
| 1331 | psa_key_slot_number_t *slot_number ) |
| 1332 | { |
| 1333 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1334 | { |
| 1335 | *slot_number = attributes->slot_number; |
| 1336 | return( PSA_SUCCESS ); |
| 1337 | } |
| 1338 | else |
| 1339 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1340 | } |
| 1341 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1342 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1343 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1344 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1345 | uint8_t *data, |
| 1346 | size_t data_size, |
| 1347 | size_t *data_length ) |
| 1348 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1349 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1350 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1351 | memcpy( data, key_buffer, key_buffer_size ); |
| 1352 | memset( data + key_buffer_size, 0, |
| 1353 | data_size - key_buffer_size ); |
| 1354 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1355 | return( PSA_SUCCESS ); |
| 1356 | } |
| 1357 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1358 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1359 | const psa_key_attributes_t *attributes, |
| 1360 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1361 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1362 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1363 | psa_key_type_t type = attributes->core.type; |
| 1364 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1365 | if( key_type_is_raw_bytes( type ) || |
| 1366 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1367 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1368 | { |
| 1369 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1370 | key_buffer, key_buffer_size, |
| 1371 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1372 | } |
| 1373 | else |
| 1374 | { |
| 1375 | /* This shouldn't happen in the reference implementation, but |
| 1376 | it is valid for a special-purpose implementation to omit |
| 1377 | support for exporting certain key types. */ |
| 1378 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1383 | uint8_t *data, |
| 1384 | size_t data_size, |
| 1385 | size_t *data_length ) |
| 1386 | { |
| 1387 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1388 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1389 | psa_key_slot_t *slot; |
| 1390 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1391 | /* Reject a zero-length output buffer now, since this can never be a |
| 1392 | * valid key representation. This way we know that data must be a valid |
| 1393 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1394 | if( data_size == 0 ) |
| 1395 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1396 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1397 | /* Set the key to empty now, so that even when there are errors, we always |
| 1398 | * set data_length to a value between 0 and data_size. On error, setting |
| 1399 | * the key to empty is a good choice because an empty key representation is |
| 1400 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1401 | *data_length = 0; |
| 1402 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1403 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1404 | * which don't require any flag, but |
| 1405 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1406 | */ |
| 1407 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1408 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1409 | if( status != PSA_SUCCESS ) |
| 1410 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1411 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1412 | psa_key_attributes_t attributes = { |
| 1413 | .core = slot->attr |
| 1414 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1415 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1416 | slot->key.data, slot->key.bytes, |
| 1417 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1418 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1419 | unlock_status = psa_unlock_key_slot( slot ); |
| 1420 | |
| 1421 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1422 | } |
| 1423 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1424 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1425 | const psa_key_attributes_t *attributes, |
| 1426 | const uint8_t *key_buffer, |
| 1427 | size_t key_buffer_size, |
| 1428 | uint8_t *data, |
| 1429 | size_t data_size, |
| 1430 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1431 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1432 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1433 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1434 | 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] | 1435 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1436 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1437 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1438 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1439 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1440 | key_buffer, key_buffer_size, |
| 1441 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1442 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1443 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1444 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1445 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1446 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1447 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1448 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1449 | key_buffer, |
| 1450 | key_buffer_size, |
| 1451 | data, |
| 1452 | data_size, |
| 1453 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1454 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1455 | /* We don't know how to convert a private RSA key to public. */ |
| 1456 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1457 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1458 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1459 | } |
| 1460 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1461 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1462 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1463 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1464 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1465 | key_buffer, |
| 1466 | key_buffer_size, |
| 1467 | data, |
| 1468 | data_size, |
| 1469 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1470 | #else |
| 1471 | /* 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] | 1472 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1473 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1474 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1475 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1476 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1477 | else |
| 1478 | { |
| 1479 | /* This shouldn't happen in the reference implementation, but |
| 1480 | it is valid for a special-purpose implementation to omit |
| 1481 | support for exporting certain key types. */ |
| 1482 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1483 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1484 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1485 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1486 | 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] | 1487 | uint8_t *data, |
| 1488 | size_t data_size, |
| 1489 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1490 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1491 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1492 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1493 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1494 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1495 | /* Reject a zero-length output buffer now, since this can never be a |
| 1496 | * valid key representation. This way we know that data must be a valid |
| 1497 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1498 | if( data_size == 0 ) |
| 1499 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1500 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1501 | /* Set the key to empty now, so that even when there are errors, we always |
| 1502 | * set data_length to a value between 0 and data_size. On error, setting |
| 1503 | * the key to empty is a good choice because an empty key representation is |
| 1504 | * unlikely to be accepted anywhere. */ |
| 1505 | *data_length = 0; |
| 1506 | |
| 1507 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1508 | 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] | 1509 | if( status != PSA_SUCCESS ) |
| 1510 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1511 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1512 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1513 | { |
| 1514 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1515 | goto exit; |
| 1516 | } |
| 1517 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1518 | psa_key_attributes_t attributes = { |
| 1519 | .core = slot->attr |
| 1520 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1521 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1522 | &attributes, slot->key.data, slot->key.bytes, |
| 1523 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1524 | |
| 1525 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1526 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1527 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1528 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1529 | } |
| 1530 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1531 | #if defined(static_assert) |
| 1532 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1533 | "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] | 1534 | 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] | 1535 | "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] | 1536 | 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] | 1537 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1538 | #endif |
| 1539 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1540 | /** Validate that a key policy is internally well-formed. |
| 1541 | * |
| 1542 | * This function only rejects invalid policies. It does not validate the |
| 1543 | * consistency of the policy with respect to other attributes of the key |
| 1544 | * such as the key type. |
| 1545 | */ |
| 1546 | 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] | 1547 | { |
| 1548 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1549 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1550 | PSA_KEY_USAGE_ENCRYPT | |
| 1551 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1552 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1553 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
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 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2169 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2170 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2171 | /* A context must be freshly initialized before it can be set up. */ |
| 2172 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2173 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2174 | status = PSA_ERROR_BAD_STATE; |
| 2175 | goto exit; |
| 2176 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2177 | |
| 2178 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2179 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2180 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2181 | goto exit; |
| 2182 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2183 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2184 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2185 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2186 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2187 | |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2188 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2189 | |
| 2190 | exit: |
| 2191 | if( status != PSA_SUCCESS ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2192 | psa_hash_abort( operation ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2193 | |
| 2194 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2198 | const uint8_t *input, |
| 2199 | size_t input_length ) |
| 2200 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2201 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2202 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2203 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2204 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2205 | status = PSA_ERROR_BAD_STATE; |
| 2206 | goto exit; |
| 2207 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2208 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2209 | /* Don't require hash implementations to behave correctly on a |
| 2210 | * zero-length input, which may have an invalid pointer. */ |
| 2211 | if( input_length == 0 ) |
| 2212 | return( PSA_SUCCESS ); |
| 2213 | |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2214 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2215 | |
| 2216 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2217 | if( status != PSA_SUCCESS ) |
| 2218 | psa_hash_abort( operation ); |
| 2219 | |
| 2220 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2224 | uint8_t *hash, |
| 2225 | size_t hash_size, |
| 2226 | size_t *hash_length ) |
| 2227 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2228 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2229 | if( operation->id == 0 ) |
| 2230 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2231 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2232 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2233 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2234 | psa_hash_abort( operation ); |
| 2235 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2238 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2239 | const uint8_t *hash, |
| 2240 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2241 | { |
| 2242 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2243 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2244 | psa_status_t status = psa_hash_finish( |
| 2245 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2246 | actual_hash, sizeof( actual_hash ), |
| 2247 | &actual_hash_length ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2248 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2249 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2250 | goto exit; |
| 2251 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2252 | if( actual_hash_length != hash_length ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2253 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2254 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2255 | goto exit; |
| 2256 | } |
| 2257 | |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2258 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2259 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2260 | |
| 2261 | exit: |
| 2262 | if( status != PSA_SUCCESS ) |
| 2263 | psa_hash_abort(operation); |
| 2264 | |
| 2265 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2268 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2269 | const uint8_t *input, size_t input_length, |
| 2270 | uint8_t *hash, size_t hash_size, |
| 2271 | size_t *hash_length ) |
| 2272 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2273 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2274 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2275 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2276 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2277 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2278 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2282 | const uint8_t *input, size_t input_length, |
| 2283 | const uint8_t *hash, size_t hash_length ) |
| 2284 | { |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2285 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2286 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2287 | |
| 2288 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2290 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2291 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2292 | alg, input, input_length, |
| 2293 | actual_hash, sizeof(actual_hash), |
| 2294 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2295 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2296 | return( status ); |
| 2297 | if( actual_hash_length != hash_length ) |
| 2298 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2299 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2300 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2301 | return( PSA_SUCCESS ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2302 | } |
| 2303 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2304 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2305 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2306 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2307 | if( source_operation->id == 0 || |
| 2308 | target_operation->id != 0 ) |
| 2309 | { |
| 2310 | return( PSA_ERROR_BAD_STATE ); |
| 2311 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2312 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2313 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2314 | target_operation ); |
| 2315 | if( status != PSA_SUCCESS ) |
| 2316 | psa_hash_abort( target_operation ); |
| 2317 | |
| 2318 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2319 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2320 | |
| 2321 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2322 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2323 | /* MAC */ |
| 2324 | /****************************************************************/ |
| 2325 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2326 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2327 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2328 | /* Aborting a non-active operation is allowed */ |
| 2329 | if( operation->id == 0 ) |
| 2330 | return( PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2331 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2332 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2333 | operation->mac_size = 0; |
| 2334 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2335 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2336 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2337 | return( status ); |
| 2338 | } |
| 2339 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2340 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2341 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2342 | const psa_key_attributes_t *attributes, |
| 2343 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2344 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2345 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2346 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2347 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2348 | |
Ronald Cron | 48f875e | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2349 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2350 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2351 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2352 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2353 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2354 | if( status != PSA_SUCCESS ) |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2355 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2356 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2357 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2358 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2359 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2360 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2361 | { |
| 2362 | /* A very short MAC is too short for security since it can be |
| 2363 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2364 | * so we make this our minimum, even though 32 bits is still |
| 2365 | * too small for security. */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2366 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2367 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2368 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2369 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2370 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2371 | { |
| 2372 | /* It's impossible to "truncate" to a larger length than the full length |
| 2373 | * of the algorithm. */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2374 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2377 | return( PSA_SUCCESS ); |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2381 | mbedtls_svc_key_id_t key, |
| 2382 | psa_algorithm_t alg, |
| 2383 | int is_sign ) |
| 2384 | { |
| 2385 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2386 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2387 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2388 | |
| 2389 | /* A context must be freshly initialized before it can be set up. */ |
| 2390 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2391 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2392 | status = PSA_ERROR_BAD_STATE; |
| 2393 | goto exit; |
| 2394 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2395 | |
| 2396 | status = psa_get_and_lock_key_slot_with_policy( |
| 2397 | key, |
| 2398 | &slot, |
Mateusz Starzyk | dd55b25 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2399 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2400 | alg ); |
| 2401 | if( status != PSA_SUCCESS ) |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2402 | goto exit; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2403 | |
| 2404 | psa_key_attributes_t attributes = { |
| 2405 | .core = slot->attr |
| 2406 | }; |
| 2407 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2408 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2409 | &operation->mac_size ); |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2410 | if( status != PSA_SUCCESS ) |
| 2411 | goto exit; |
| 2412 | |
| 2413 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2414 | /* Dispatch the MAC setup call with validated input */ |
| 2415 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2416 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2417 | status = psa_driver_wrapper_mac_sign_setup( operation, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2418 | &attributes, |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2419 | slot->key.data, |
| 2420 | slot->key.bytes, |
| 2421 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2422 | } |
| 2423 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2424 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2425 | status = psa_driver_wrapper_mac_verify_setup( operation, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2426 | &attributes, |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2427 | slot->key.data, |
| 2428 | slot->key.bytes, |
| 2429 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2430 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2431 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2432 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2433 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2434 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2435 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2436 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2437 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2438 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2439 | } |
| 2440 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2441 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2442 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2443 | psa_algorithm_t alg ) |
| 2444 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2445 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2449 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2450 | psa_algorithm_t alg ) |
| 2451 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2452 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2453 | } |
| 2454 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2455 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2456 | const uint8_t *input, |
| 2457 | size_t input_length ) |
| 2458 | { |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2459 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2460 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2461 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2462 | /* Don't require hash implementations to behave correctly on a |
| 2463 | * zero-length input, which may have an invalid pointer. */ |
| 2464 | if( input_length == 0 ) |
| 2465 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2466 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2467 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2468 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2469 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2470 | psa_mac_abort( operation ); |
| 2471 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2472 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2473 | } |
| 2474 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2475 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2476 | uint8_t *mac, |
| 2477 | size_t mac_size, |
| 2478 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2479 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2480 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2481 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2482 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2483 | if( operation->id == 0 ) |
Dave Rodgman | d73e1b0 | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2484 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2485 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2486 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2487 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2488 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2489 | if( ! operation->is_sign ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2490 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2491 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2492 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2493 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2494 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2495 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2496 | * once all the error checks are done. */ |
| 2497 | if( operation->mac_size == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2498 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2499 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2500 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2501 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2502 | |
| 2503 | if( mac_size < operation->mac_size ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2504 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2505 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2506 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2507 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2508 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2509 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2510 | mac, operation->mac_size, |
| 2511 | mac_length ); |
| 2512 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2513 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2514 | /* In case of success, set the potential excess room in the output buffer |
| 2515 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2516 | * In case of error, set the output length and content to a safe default, |
| 2517 | * such that in case the caller misses an error check, the output would be |
| 2518 | * an unachievable MAC. |
| 2519 | */ |
| 2520 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2521 | { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2522 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2523 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2524 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2525 | |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2526 | if( mac_size > operation->mac_size ) |
| 2527 | memset( &mac[operation->mac_size], '!', |
| 2528 | mac_size - operation->mac_size ); |
| 2529 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2530 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2531 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2532 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2535 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2536 | const uint8_t *mac, |
| 2537 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2538 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2539 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2540 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2541 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2542 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2543 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2544 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2545 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2546 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2547 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2548 | if( operation->is_sign ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2549 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2550 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2551 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2552 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2553 | |
| 2554 | if( operation->mac_size != mac_length ) |
| 2555 | { |
| 2556 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2557 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2558 | } |
| 2559 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2560 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2561 | mac, mac_length ); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2562 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2563 | exit: |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2564 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2565 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2566 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2567 | } |
| 2568 | |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2569 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2570 | psa_algorithm_t alg, |
| 2571 | const uint8_t *input, |
| 2572 | size_t input_length, |
| 2573 | uint8_t *mac, |
| 2574 | size_t mac_size, |
| 2575 | size_t *mac_length, |
| 2576 | int is_sign ) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2577 | { |
| 2578 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2579 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2580 | psa_key_slot_t *slot; |
| 2581 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2582 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2583 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | dd55b25 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2584 | key, |
| 2585 | &slot, |
| 2586 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2587 | alg ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2588 | if( status != PSA_SUCCESS ) |
| 2589 | goto exit; |
| 2590 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2591 | psa_key_attributes_t attributes = { |
| 2592 | .core = slot->attr |
| 2593 | }; |
| 2594 | |
| 2595 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2596 | &operation_mac_size ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2597 | if( status != PSA_SUCCESS ) |
| 2598 | goto exit; |
| 2599 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2600 | if( mac_size < operation_mac_size ) |
| 2601 | { |
| 2602 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2603 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | status = psa_driver_wrapper_mac_compute( |
| 2607 | &attributes, |
| 2608 | slot->key.data, slot->key.bytes, |
| 2609 | alg, |
| 2610 | input, input_length, |
| 2611 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2612 | |
| 2613 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2614 | /* In case of success, set the potential excess room in the output buffer |
| 2615 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2616 | * In case of error, set the output length and content to a safe default, |
| 2617 | * such that in case the caller misses an error check, the output would be |
| 2618 | * an unachievable MAC. |
| 2619 | */ |
| 2620 | if( status != PSA_SUCCESS ) |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2621 | { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2622 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2623 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2624 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2625 | if( mac_size > operation_mac_size ) |
| 2626 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2627 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2628 | unlock_status = psa_unlock_key_slot( slot ); |
| 2629 | |
| 2630 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2633 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2634 | psa_algorithm_t alg, |
| 2635 | const uint8_t *input, |
| 2636 | size_t input_length, |
| 2637 | uint8_t *mac, |
| 2638 | size_t mac_size, |
| 2639 | size_t *mac_length) |
| 2640 | { |
| 2641 | return( psa_mac_compute_internal( key, alg, |
| 2642 | input, input_length, |
| 2643 | mac, mac_size, mac_length, 1 ) ); |
| 2644 | } |
| 2645 | |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2646 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2647 | psa_algorithm_t alg, |
| 2648 | const uint8_t *input, |
| 2649 | size_t input_length, |
| 2650 | const uint8_t *mac, |
| 2651 | size_t mac_length) |
| 2652 | { |
| 2653 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2654 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2655 | size_t actual_mac_length; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2656 | |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2657 | status = psa_mac_compute_internal( key, alg, |
| 2658 | input, input_length, |
| 2659 | actual_mac, sizeof( actual_mac ), |
| 2660 | &actual_mac_length, 0 ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2661 | if( status != PSA_SUCCESS ) |
| 2662 | goto exit; |
| 2663 | |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2664 | if( mac_length != actual_mac_length ) |
| 2665 | { |
| 2666 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2667 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2668 | } |
| 2669 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2670 | { |
| 2671 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2672 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2673 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2674 | |
| 2675 | exit: |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2676 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2677 | |
| 2678 | return ( status ); |
| 2679 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2680 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2681 | /****************************************************************/ |
| 2682 | /* Asymmetric cryptography */ |
| 2683 | /****************************************************************/ |
| 2684 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2685 | static psa_status_t psa_sign_verify_check_alg( int input_is_message, |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2686 | psa_algorithm_t alg ) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2687 | { |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2688 | if( input_is_message ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2689 | { |
| 2690 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2691 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2692 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2693 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2694 | { |
| 2695 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2696 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2697 | } |
| 2698 | } |
| 2699 | else |
| 2700 | { |
Mateusz Starzyk | d22362c | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2701 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2702 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2703 | } |
| 2704 | |
| 2705 | return( PSA_SUCCESS ); |
| 2706 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2707 | |
| 2708 | static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2709 | int input_is_message, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2710 | psa_algorithm_t alg, |
| 2711 | const uint8_t * input, |
| 2712 | size_t input_length, |
| 2713 | uint8_t * signature, |
| 2714 | size_t signature_size, |
| 2715 | size_t * signature_length ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2716 | { |
| 2717 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2718 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2719 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2720 | |
| 2721 | *signature_length = 0; |
| 2722 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2723 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2724 | if( status != PSA_SUCCESS ) |
| 2725 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2726 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2727 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2728 | * that signature must be a valid pointer. (On the other hand, the input |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2729 | * buffer can in principle be empty since it doesn't actually have |
| 2730 | * to be a hash.) */ |
| 2731 | if( signature_size == 0 ) |
| 2732 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2733 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2734 | status = psa_get_and_lock_key_slot_with_policy( |
| 2735 | key, &slot, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2736 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2737 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2738 | alg ); |
| 2739 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2740 | if( status != PSA_SUCCESS ) |
| 2741 | goto exit; |
| 2742 | |
| 2743 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2744 | { |
| 2745 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2746 | goto exit; |
| 2747 | } |
| 2748 | |
| 2749 | psa_key_attributes_t attributes = { |
| 2750 | .core = slot->attr |
| 2751 | }; |
| 2752 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2753 | if( input_is_message ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2754 | { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2755 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2756 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2757 | alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2758 | signature, signature_size, signature_length ); |
| 2759 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2760 | else |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2761 | { |
| 2762 | |
| 2763 | status = psa_driver_wrapper_sign_hash( |
| 2764 | &attributes, slot->key.data, slot->key.bytes, |
| 2765 | alg, input, input_length, |
| 2766 | signature, signature_size, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2767 | } |
| 2768 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2769 | |
| 2770 | exit: |
| 2771 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2772 | * the trailing part on success) with something that isn't a valid signature |
| 2773 | * (barring an attack on the signature and deliberately-crafted input), |
| 2774 | * in case the caller doesn't check the return status properly. */ |
| 2775 | if( status == PSA_SUCCESS ) |
| 2776 | memset( signature + *signature_length, '!', |
| 2777 | signature_size - *signature_length ); |
| 2778 | else |
| 2779 | memset( signature, '!', signature_size ); |
| 2780 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2781 | * memset because signature may be NULL in this case. */ |
| 2782 | |
| 2783 | unlock_status = psa_unlock_key_slot( slot ); |
| 2784 | |
| 2785 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2786 | } |
| 2787 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2788 | static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2789 | int input_is_message, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2790 | psa_algorithm_t alg, |
| 2791 | const uint8_t * input, |
| 2792 | size_t input_length, |
| 2793 | const uint8_t * signature, |
| 2794 | size_t signature_length ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2795 | { |
| 2796 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2797 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2798 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2799 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2800 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2801 | if( status != PSA_SUCCESS ) |
| 2802 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2803 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2804 | status = psa_get_and_lock_key_slot_with_policy( |
| 2805 | key, &slot, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2806 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2807 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2808 | alg ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2809 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2810 | if( status != PSA_SUCCESS ) |
| 2811 | return( status ); |
| 2812 | |
| 2813 | psa_key_attributes_t attributes = { |
| 2814 | .core = slot->attr |
| 2815 | }; |
| 2816 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2817 | if( input_is_message ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2818 | { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2819 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2820 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2821 | alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2822 | signature, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2823 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2824 | else |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2825 | { |
| 2826 | status = psa_driver_wrapper_verify_hash( |
| 2827 | &attributes, slot->key.data, slot->key.bytes, |
| 2828 | alg, input, input_length, |
| 2829 | signature, signature_length ); |
| 2830 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2831 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2832 | unlock_status = psa_unlock_key_slot( slot ); |
| 2833 | |
| 2834 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2835 | |
| 2836 | } |
| 2837 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2838 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2839 | const psa_key_attributes_t *attributes, |
| 2840 | const uint8_t *key_buffer, |
| 2841 | size_t key_buffer_size, |
| 2842 | psa_algorithm_t alg, |
| 2843 | const uint8_t *input, |
| 2844 | size_t input_length, |
| 2845 | uint8_t *signature, |
| 2846 | size_t signature_size, |
| 2847 | size_t *signature_length ) |
| 2848 | { |
| 2849 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2850 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2851 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2852 | { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2853 | size_t hash_length; |
| 2854 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2855 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2856 | status = psa_driver_wrapper_hash_compute( |
| 2857 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2858 | input, input_length, |
| 2859 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2860 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2861 | if( status != PSA_SUCCESS ) |
| 2862 | return status; |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2863 | |
| 2864 | return psa_driver_wrapper_sign_hash( |
| 2865 | attributes, key_buffer, key_buffer_size, |
| 2866 | alg, hash, hash_length, |
| 2867 | signature, signature_size, signature_length ); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2868 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2869 | |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2870 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2871 | } |
| 2872 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2873 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2874 | psa_algorithm_t alg, |
| 2875 | const uint8_t * input, |
| 2876 | size_t input_length, |
| 2877 | uint8_t * signature, |
| 2878 | size_t signature_size, |
| 2879 | size_t * signature_length ) |
| 2880 | { |
| 2881 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2882 | key, 1, alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2883 | signature, signature_size, signature_length ); |
| 2884 | } |
| 2885 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2886 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2887 | const psa_key_attributes_t *attributes, |
| 2888 | const uint8_t *key_buffer, |
| 2889 | size_t key_buffer_size, |
| 2890 | psa_algorithm_t alg, |
| 2891 | const uint8_t *input, |
| 2892 | size_t input_length, |
| 2893 | const uint8_t *signature, |
| 2894 | size_t signature_length ) |
| 2895 | { |
| 2896 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2897 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2898 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2899 | { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2900 | size_t hash_length; |
| 2901 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2902 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2903 | status = psa_driver_wrapper_hash_compute( |
| 2904 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2905 | input, input_length, |
| 2906 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2907 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2908 | if( status != PSA_SUCCESS ) |
| 2909 | return status; |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2910 | |
| 2911 | return psa_driver_wrapper_verify_hash( |
| 2912 | attributes, key_buffer, key_buffer_size, |
| 2913 | alg, hash, hash_length, |
| 2914 | signature, signature_length ); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2915 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2916 | |
gabor-mezei-arm | ef6f2aa | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2917 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2918 | } |
| 2919 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2920 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2921 | psa_algorithm_t alg, |
| 2922 | const uint8_t * input, |
| 2923 | size_t input_length, |
| 2924 | const uint8_t * signature, |
| 2925 | size_t signature_length ) |
| 2926 | { |
| 2927 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2928 | key, 1, alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2929 | signature, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2930 | } |
| 2931 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2932 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2933 | const psa_key_attributes_t *attributes, |
| 2934 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2935 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2936 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2937 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2938 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2939 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2940 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2941 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2942 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2943 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2944 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2945 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2946 | attributes, |
| 2947 | key_buffer, key_buffer_size, |
| 2948 | alg, hash, hash_length, |
| 2949 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2950 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2951 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2952 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2953 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2954 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2955 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2956 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2957 | } |
| 2958 | else |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2959 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
| 2960 | { |
| 2961 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2962 | { |
| 2963 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2964 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2965 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2966 | attributes, |
| 2967 | key_buffer, key_buffer_size, |
| 2968 | alg, hash, hash_length, |
| 2969 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2970 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2971 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2972 | } |
| 2973 | else |
| 2974 | { |
| 2975 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2976 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2977 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2978 | |
| 2979 | (void)key_buffer; |
| 2980 | (void)key_buffer_size; |
| 2981 | (void)hash; |
| 2982 | (void)hash_length; |
| 2983 | (void)signature; |
| 2984 | (void)signature_size; |
| 2985 | (void)signature_length; |
| 2986 | |
| 2987 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2988 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2989 | |
| 2990 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2991 | psa_algorithm_t alg, |
| 2992 | const uint8_t *hash, |
| 2993 | size_t hash_length, |
| 2994 | uint8_t *signature, |
| 2995 | size_t signature_size, |
| 2996 | size_t *signature_length ) |
| 2997 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2998 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2999 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3000 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3001 | } |
| 3002 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3003 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3004 | const psa_key_attributes_t *attributes, |
| 3005 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3006 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3007 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3008 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 3009 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3010 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3011 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 3012 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3013 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3014 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3015 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3016 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 3017 | attributes, |
| 3018 | key_buffer, key_buffer_size, |
| 3019 | alg, hash, hash_length, |
| 3020 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3021 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3022 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3023 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3024 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3025 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3026 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3027 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3028 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3029 | else |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3030 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3031 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3032 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 3033 | { |
| 3034 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3035 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3036 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3037 | attributes, |
| 3038 | key_buffer, key_buffer_size, |
| 3039 | alg, hash, hash_length, |
| 3040 | signature, signature_length ) ); |
| 3041 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3042 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3043 | } |
| 3044 | else |
| 3045 | { |
| 3046 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3047 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3048 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3049 | |
| 3050 | (void)key_buffer; |
| 3051 | (void)key_buffer_size; |
| 3052 | (void)hash; |
| 3053 | (void)hash_length; |
| 3054 | (void)signature; |
| 3055 | (void)signature_length; |
| 3056 | |
| 3057 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3058 | } |
| 3059 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3060 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3061 | psa_algorithm_t alg, |
| 3062 | const uint8_t *hash, |
| 3063 | size_t hash_length, |
| 3064 | const uint8_t *signature, |
| 3065 | size_t signature_length ) |
| 3066 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3067 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3068 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3069 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3070 | } |
| 3071 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3072 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3073 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3074 | mbedtls_rsa_context *rsa ) |
| 3075 | { |
| 3076 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3077 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3078 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3079 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3080 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3081 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3082 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3083 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3084 | psa_algorithm_t alg, |
| 3085 | const uint8_t *input, |
| 3086 | size_t input_length, |
| 3087 | const uint8_t *salt, |
| 3088 | size_t salt_length, |
| 3089 | uint8_t *output, |
| 3090 | size_t output_size, |
| 3091 | size_t *output_length ) |
| 3092 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3093 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3094 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3095 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3096 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3097 | (void) input; |
| 3098 | (void) input_length; |
| 3099 | (void) salt; |
| 3100 | (void) output; |
| 3101 | (void) output_size; |
| 3102 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3103 | *output_length = 0; |
| 3104 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3105 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3106 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3107 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3108 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3109 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3110 | if( status != PSA_SUCCESS ) |
| 3111 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3112 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3113 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3114 | { |
| 3115 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3116 | goto exit; |
| 3117 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3118 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3119 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3120 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3121 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3122 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3123 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3124 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3125 | slot->key.data, |
| 3126 | slot->key.bytes, |
| 3127 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3128 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3129 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3130 | |
| 3131 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3132 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3133 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3134 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3135 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3136 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3137 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
| 3138 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3139 | status = mbedtls_to_psa_error( |
| 3140 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3141 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3142 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3143 | MBEDTLS_RSA_PUBLIC, |
| 3144 | input_length, |
| 3145 | input, |
| 3146 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3147 | } |
| 3148 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3149 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3150 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3151 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
| 3152 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3153 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3154 | status = mbedtls_to_psa_error( |
| 3155 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3156 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3157 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3158 | MBEDTLS_RSA_PUBLIC, |
| 3159 | salt, salt_length, |
| 3160 | input_length, |
| 3161 | input, |
| 3162 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3163 | } |
| 3164 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3165 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3166 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3167 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3168 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3169 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3170 | rsa_exit: |
| 3171 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3172 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3173 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3174 | mbedtls_rsa_free( rsa ); |
| 3175 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3176 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3177 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3178 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3179 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3180 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3181 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3182 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3183 | |
| 3184 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3185 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3186 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3187 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3188 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3189 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3190 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3191 | psa_algorithm_t alg, |
| 3192 | const uint8_t *input, |
| 3193 | size_t input_length, |
| 3194 | const uint8_t *salt, |
| 3195 | size_t salt_length, |
| 3196 | uint8_t *output, |
| 3197 | size_t output_size, |
| 3198 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3199 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3200 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3201 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3202 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3203 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3204 | (void) input; |
| 3205 | (void) input_length; |
| 3206 | (void) salt; |
| 3207 | (void) output; |
| 3208 | (void) output_size; |
| 3209 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3210 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3211 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3212 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3213 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3214 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3215 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3216 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3217 | if( status != PSA_SUCCESS ) |
| 3218 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3219 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3220 | { |
| 3221 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3222 | goto exit; |
| 3223 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3224 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3225 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3226 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3227 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3228 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3229 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3230 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3231 | slot->key.data, |
| 3232 | slot->key.bytes, |
| 3233 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3234 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3235 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3236 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3237 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3238 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3239 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3240 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3241 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3242 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3243 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3244 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
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 = mbedtls_to_psa_error( |
| 3247 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3248 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3249 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3250 | MBEDTLS_RSA_PRIVATE, |
| 3251 | output_length, |
| 3252 | input, |
| 3253 | output, |
| 3254 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3255 | } |
| 3256 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3257 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3258 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3259 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3260 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3261 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3262 | status = mbedtls_to_psa_error( |
| 3263 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3264 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3265 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3266 | MBEDTLS_RSA_PRIVATE, |
| 3267 | salt, salt_length, |
| 3268 | output_length, |
| 3269 | input, |
| 3270 | output, |
| 3271 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3272 | } |
| 3273 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3274 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3275 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3276 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3277 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3278 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3279 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3280 | mbedtls_rsa_free( rsa ); |
| 3281 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3282 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3283 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3284 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3285 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3286 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3287 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3288 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3289 | |
| 3290 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3291 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3292 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3293 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3294 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3295 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3296 | |
| 3297 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3298 | /****************************************************************/ |
| 3299 | /* Symmetric cryptography */ |
| 3300 | /****************************************************************/ |
| 3301 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3302 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3303 | mbedtls_svc_key_id_t key, |
| 3304 | psa_algorithm_t alg, |
| 3305 | mbedtls_operation_t cipher_operation ) |
| 3306 | { |
| 3307 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3308 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3309 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3310 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3311 | PSA_KEY_USAGE_ENCRYPT : |
| 3312 | PSA_KEY_USAGE_DECRYPT ); |
| 3313 | |
| 3314 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3315 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3316 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3317 | status = PSA_ERROR_BAD_STATE; |
| 3318 | goto exit; |
| 3319 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3320 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3321 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3322 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3323 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3324 | goto exit; |
| 3325 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3326 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3327 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3328 | if( status != PSA_SUCCESS ) |
| 3329 | goto exit; |
| 3330 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3331 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3332 | * 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] | 3333 | * so we only set it (in the driver wrapper) after resources have been |
| 3334 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3335 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3336 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3337 | operation->iv_required = 0; |
| 3338 | else |
| 3339 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3340 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3341 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3342 | psa_key_attributes_t attributes = { |
| 3343 | .core = slot->attr |
| 3344 | }; |
| 3345 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3346 | /* Try doing the operation through a driver before using software fallback. */ |
| 3347 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3348 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3349 | &attributes, |
| 3350 | slot->key.data, |
| 3351 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3352 | alg ); |
| 3353 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3354 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3355 | &attributes, |
| 3356 | slot->key.data, |
| 3357 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3358 | alg ); |
| 3359 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3360 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3361 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3362 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3363 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3364 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3365 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3366 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3367 | } |
| 3368 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3369 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3370 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3371 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3372 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3373 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3376 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3377 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3378 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3379 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3380 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3381 | } |
| 3382 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3383 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3384 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3385 | size_t iv_size, |
| 3386 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3387 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3388 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3389 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3390 | *iv_length = 0; |
| 3391 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3392 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3393 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3394 | status = PSA_ERROR_BAD_STATE; |
| 3395 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3398 | if( operation->iv_set || ! operation->iv_required ) |
| 3399 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3400 | status = PSA_ERROR_BAD_STATE; |
| 3401 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3402 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3403 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3404 | if( iv_size < operation->default_iv_length ) |
| 3405 | { |
| 3406 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3407 | goto exit; |
| 3408 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3409 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3410 | status = psa_generate_random( iv, operation->default_iv_length ); |
| 3411 | if( status != PSA_SUCCESS ) |
| 3412 | goto exit; |
| 3413 | |
| 3414 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3415 | iv, |
| 3416 | operation->default_iv_length ); |
| 3417 | |
| 3418 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3419 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3420 | { |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3421 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3422 | *iv_length = operation->default_iv_length; |
| 3423 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3424 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3425 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3426 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3427 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3428 | } |
| 3429 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3430 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3431 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3432 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3433 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3434 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3435 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3436 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3437 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3438 | status = PSA_ERROR_BAD_STATE; |
| 3439 | goto exit; |
| 3440 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3441 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3442 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3443 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3444 | status = PSA_ERROR_BAD_STATE; |
| 3445 | goto exit; |
| 3446 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3447 | |
| 3448 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3449 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3450 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3451 | goto exit; |
| 3452 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3453 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3454 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3455 | iv, |
| 3456 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3457 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3458 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3459 | if( status == PSA_SUCCESS ) |
| 3460 | operation->iv_set = 1; |
| 3461 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3462 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3463 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3464 | } |
| 3465 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3466 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3467 | const uint8_t *input, |
| 3468 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3469 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3470 | size_t output_size, |
| 3471 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3472 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3473 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3474 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3475 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3476 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3477 | status = PSA_ERROR_BAD_STATE; |
| 3478 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3479 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3480 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3481 | if( operation->iv_required && ! operation->iv_set ) |
| 3482 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3483 | status = PSA_ERROR_BAD_STATE; |
| 3484 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3485 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3486 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3487 | status = psa_driver_wrapper_cipher_update( operation, |
| 3488 | input, |
| 3489 | input_length, |
| 3490 | output, |
| 3491 | output_size, |
| 3492 | output_length ); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3493 | |
| 3494 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3495 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3496 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3497 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3498 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3499 | } |
| 3500 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3501 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3502 | uint8_t *output, |
| 3503 | size_t output_size, |
| 3504 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3505 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3506 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3507 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3508 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3509 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3510 | status = PSA_ERROR_BAD_STATE; |
| 3511 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3512 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3513 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3514 | if( operation->iv_required && ! operation->iv_set ) |
| 3515 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3516 | status = PSA_ERROR_BAD_STATE; |
| 3517 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3518 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3519 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3520 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3521 | output, |
| 3522 | output_size, |
| 3523 | output_length ); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3524 | |
| 3525 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3526 | if( status == PSA_SUCCESS ) |
| 3527 | return( psa_cipher_abort( operation ) ); |
| 3528 | else |
| 3529 | { |
| 3530 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3531 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3532 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3533 | return( status ); |
| 3534 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3535 | } |
| 3536 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3537 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3538 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3539 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3540 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3541 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3542 | * in use. It's ok to call abort on such an object, and there's |
| 3543 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3544 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3545 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3546 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3547 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3548 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3549 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3550 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3551 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3552 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3553 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3554 | } |
| 3555 | |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3556 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3557 | psa_algorithm_t alg, |
| 3558 | const uint8_t *input, |
| 3559 | size_t input_length, |
| 3560 | uint8_t *output, |
| 3561 | size_t output_size, |
| 3562 | size_t *output_length ) |
| 3563 | { |
| 3564 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3565 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3566 | psa_key_slot_t *slot; |
| 3567 | psa_key_type_t key_type; |
| 3568 | size_t iv_length; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3569 | |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3570 | *output_length = 0; |
| 3571 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3572 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3573 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3574 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3575 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3576 | PSA_KEY_USAGE_ENCRYPT, |
| 3577 | alg ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3578 | if( status != PSA_SUCCESS ) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3579 | return( status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3580 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3581 | psa_key_attributes_t attributes = { |
| 3582 | .core = slot->attr |
| 3583 | }; |
| 3584 | |
| 3585 | key_type = slot->attr.type; |
| 3586 | iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
| 3587 | |
| 3588 | if( iv_length > 0 ) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3589 | { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3590 | if( output_size < iv_length ) |
| 3591 | { |
| 3592 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3593 | goto exit; |
| 3594 | } |
| 3595 | |
| 3596 | status = psa_generate_random( output, iv_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3597 | if( status != PSA_SUCCESS ) |
| 3598 | goto exit; |
| 3599 | } |
| 3600 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3601 | status = psa_driver_wrapper_cipher_encrypt( |
| 3602 | &attributes, slot->key.data, slot->key.bytes, |
| 3603 | alg, input, input_length, |
| 3604 | output, output_size, output_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3605 | |
| 3606 | exit: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3607 | unlock_status = psa_unlock_key_slot( slot ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3608 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3609 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3613 | psa_algorithm_t alg, |
| 3614 | const uint8_t *input, |
| 3615 | size_t input_length, |
| 3616 | uint8_t *output, |
| 3617 | size_t output_size, |
| 3618 | size_t *output_length ) |
| 3619 | { |
| 3620 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3621 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3622 | psa_key_slot_t *slot; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3623 | |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3624 | *output_length = 0; |
| 3625 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3626 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3627 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3628 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3629 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3630 | PSA_KEY_USAGE_DECRYPT, |
| 3631 | alg ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3632 | if( status != PSA_SUCCESS ) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3633 | return( status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3634 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3635 | psa_key_attributes_t attributes = { |
| 3636 | .core = slot->attr |
| 3637 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3638 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3639 | if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) ) |
| 3640 | { |
| 3641 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3642 | goto exit; |
| 3643 | } |
| 3644 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3645 | status = psa_driver_wrapper_cipher_decrypt( |
| 3646 | &attributes, slot->key.data, slot->key.bytes, |
| 3647 | alg, input, input_length, |
| 3648 | output, output_size, output_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3649 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3650 | exit: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3651 | unlock_status = psa_unlock_key_slot( slot ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3652 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3653 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3657 | /****************************************************************/ |
| 3658 | /* AEAD */ |
| 3659 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3660 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3661 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3662 | psa_algorithm_t alg, |
| 3663 | const uint8_t *nonce, |
| 3664 | size_t nonce_length, |
| 3665 | const uint8_t *additional_data, |
| 3666 | size_t additional_data_length, |
| 3667 | const uint8_t *plaintext, |
| 3668 | size_t plaintext_length, |
| 3669 | uint8_t *ciphertext, |
| 3670 | size_t ciphertext_size, |
| 3671 | size_t *ciphertext_length ) |
| 3672 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3673 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3674 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3675 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3676 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3677 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3678 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3679 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3680 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3681 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3682 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3683 | if( status != PSA_SUCCESS ) |
| 3684 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3685 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3686 | psa_key_attributes_t attributes = { |
| 3687 | .core = slot->attr |
| 3688 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3689 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3690 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3691 | &attributes, slot->key.data, slot->key.bytes, |
| 3692 | alg, |
| 3693 | nonce, nonce_length, |
| 3694 | additional_data, additional_data_length, |
| 3695 | plaintext, plaintext_length, |
| 3696 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3697 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3698 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3699 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3700 | |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3701 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3702 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3703 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3704 | } |
| 3705 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3706 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3707 | psa_algorithm_t alg, |
| 3708 | const uint8_t *nonce, |
| 3709 | size_t nonce_length, |
| 3710 | const uint8_t *additional_data, |
| 3711 | size_t additional_data_length, |
| 3712 | const uint8_t *ciphertext, |
| 3713 | size_t ciphertext_length, |
| 3714 | uint8_t *plaintext, |
| 3715 | size_t plaintext_size, |
| 3716 | size_t *plaintext_length ) |
| 3717 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3718 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3719 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3720 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3721 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3722 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3723 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3724 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3725 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3726 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3727 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3728 | if( status != PSA_SUCCESS ) |
| 3729 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3730 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3731 | psa_key_attributes_t attributes = { |
| 3732 | .core = slot->attr |
| 3733 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3734 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3735 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3736 | &attributes, slot->key.data, slot->key.bytes, |
| 3737 | alg, |
| 3738 | nonce, nonce_length, |
| 3739 | additional_data, additional_data_length, |
| 3740 | ciphertext, ciphertext_length, |
| 3741 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3742 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3743 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3744 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3745 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3746 | psa_unlock_key_slot( slot ); |
| 3747 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3748 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3749 | } |
| 3750 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3751 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3752 | /* Generators */ |
| 3753 | /****************************************************************/ |
| 3754 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3755 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3756 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3757 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3758 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3759 | #endif /* At least one builtin KDF */ |
| 3760 | |
| 3761 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3762 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3763 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3764 | static psa_status_t psa_key_derivation_start_hmac( |
| 3765 | psa_mac_operation_t *operation, |
| 3766 | psa_algorithm_t hash_alg, |
| 3767 | const uint8_t *hmac_key, |
| 3768 | size_t hmac_key_length ) |
| 3769 | { |
| 3770 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3772 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 3773 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 3774 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 3775 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3776 | operation->is_sign = 1; |
| 3777 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 3778 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3779 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 3780 | &attributes, |
| 3781 | hmac_key, hmac_key_length, |
| 3782 | PSA_ALG_HMAC( hash_alg ) ); |
| 3783 | |
| 3784 | psa_reset_key_attributes( &attributes ); |
| 3785 | return( status ); |
| 3786 | } |
| 3787 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3788 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3789 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 3790 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 3791 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 3792 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 3793 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3794 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3795 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3796 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3797 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 3798 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3799 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3800 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3801 | } |
| 3802 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3803 | 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] | 3804 | { |
| 3805 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3806 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3807 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3808 | { |
| 3809 | /* The object has (apparently) been initialized but it is not |
| 3810 | * in use. It's ok to call abort on such an object, and there's |
| 3811 | * nothing to do. */ |
| 3812 | } |
| 3813 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3814 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3815 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3816 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3817 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3818 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3819 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3820 | else |
| 3821 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3822 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3823 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3824 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3825 | /* 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] | 3826 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3827 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3828 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 3829 | { |
| 3830 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 3831 | operation->ctx.tls12_prf.secret_length ); |
| 3832 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 3833 | } |
| 3834 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3835 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 3836 | { |
| 3837 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 3838 | operation->ctx.tls12_prf.seed_length ); |
| 3839 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 3840 | } |
| 3841 | |
| 3842 | if( operation->ctx.tls12_prf.label != NULL ) |
| 3843 | { |
| 3844 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 3845 | operation->ctx.tls12_prf.label_length ); |
| 3846 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 3847 | } |
| 3848 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3849 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3850 | |
| 3851 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3852 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3853 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3854 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3855 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 3856 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3857 | { |
| 3858 | status = PSA_ERROR_BAD_STATE; |
| 3859 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 3860 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3861 | return( status ); |
| 3862 | } |
| 3863 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3864 | 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] | 3865 | size_t *capacity) |
| 3866 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3867 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3868 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3869 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3870 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3873 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3874 | return( PSA_SUCCESS ); |
| 3875 | } |
| 3876 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3877 | 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] | 3878 | size_t capacity ) |
| 3879 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3880 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3881 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3882 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3883 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3884 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3885 | return( PSA_SUCCESS ); |
| 3886 | } |
| 3887 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3888 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3889 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3890 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3891 | static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf, |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3892 | psa_algorithm_t hash_alg, |
| 3893 | uint8_t *output, |
| 3894 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3895 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3896 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3897 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3898 | psa_status_t status; |
| 3899 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3900 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 3901 | return( PSA_ERROR_BAD_STATE ); |
| 3902 | hkdf->state = HKDF_STATE_OUTPUT; |
| 3903 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3904 | while( output_length != 0 ) |
| 3905 | { |
| 3906 | /* Copy what remains of the current block */ |
| 3907 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 3908 | if( n > output_length ) |
| 3909 | n = (uint8_t) output_length; |
| 3910 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 3911 | output += n; |
| 3912 | output_length -= n; |
| 3913 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3914 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3915 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3916 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 3917 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3918 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3919 | * object was corrupted or if this function is called directly |
| 3920 | * inside the library. */ |
| 3921 | if( hkdf->block_number == 0xff ) |
| 3922 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3923 | |
| 3924 | /* We need a new block */ |
| 3925 | ++hkdf->block_number; |
| 3926 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3927 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3928 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 3929 | hash_alg, |
| 3930 | hkdf->prk, |
| 3931 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3932 | if( status != PSA_SUCCESS ) |
| 3933 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3934 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3935 | if( hkdf->block_number != 1 ) |
| 3936 | { |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3937 | status = psa_mac_update( &hkdf->hmac, |
| 3938 | hkdf->output_block, |
| 3939 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3940 | if( status != PSA_SUCCESS ) |
| 3941 | return( status ); |
| 3942 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3943 | status = psa_mac_update( &hkdf->hmac, |
| 3944 | hkdf->info, |
| 3945 | hkdf->info_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3946 | if( status != PSA_SUCCESS ) |
| 3947 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3948 | status = psa_mac_update( &hkdf->hmac, |
| 3949 | &hkdf->block_number, 1 ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3950 | if( status != PSA_SUCCESS ) |
| 3951 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3952 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 3953 | hkdf->output_block, |
| 3954 | sizeof( hkdf->output_block ), |
| 3955 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3956 | if( status != PSA_SUCCESS ) |
| 3957 | return( status ); |
| 3958 | } |
| 3959 | |
| 3960 | return( PSA_SUCCESS ); |
| 3961 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3962 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3963 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3964 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3965 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3966 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 3967 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 3968 | psa_algorithm_t alg ) |
| 3969 | { |
| 3970 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3971 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3972 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 3973 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3974 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3975 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3976 | /* We can't be wanting more output after block 0xff, otherwise |
| 3977 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 3978 | * prevented this call. It could happen only if the operation |
| 3979 | * object was corrupted or if this function is called directly |
| 3980 | * inside the library. */ |
| 3981 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 3982 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3983 | |
| 3984 | /* We need a new block */ |
| 3985 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3986 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3987 | |
| 3988 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 3989 | * |
| 3990 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 3991 | * |
| 3992 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 3993 | * HMAC_hash(secret, A(2) + seed) + |
| 3994 | * HMAC_hash(secret, A(3) + seed) + ... |
| 3995 | * |
| 3996 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3997 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3998 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3999 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4000 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4001 | * is currently extracted as `output_block` and where i is |
| 4002 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4003 | */ |
| 4004 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4005 | status = psa_key_derivation_start_hmac( &hmac, |
| 4006 | hash_alg, |
| 4007 | tls12_prf->secret, |
| 4008 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4009 | if( status != PSA_SUCCESS ) |
| 4010 | goto cleanup; |
| 4011 | |
| 4012 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4013 | if( tls12_prf->block_number == 1 ) |
| 4014 | { |
| 4015 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4016 | * the variable seed and in this instance means it in the context of the |
| 4017 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4018 | status = psa_mac_update( &hmac, |
| 4019 | tls12_prf->label, |
| 4020 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4021 | if( status != PSA_SUCCESS ) |
| 4022 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4023 | status = psa_mac_update( &hmac, |
| 4024 | tls12_prf->seed, |
| 4025 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4026 | if( status != PSA_SUCCESS ) |
| 4027 | goto cleanup; |
| 4028 | } |
| 4029 | else |
| 4030 | { |
| 4031 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4032 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4033 | if( status != PSA_SUCCESS ) |
| 4034 | goto cleanup; |
| 4035 | } |
| 4036 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4037 | status = psa_mac_sign_finish( &hmac, |
| 4038 | tls12_prf->Ai, hash_length, |
| 4039 | &hmac_output_length ); |
| 4040 | if( hmac_output_length != hash_length ) |
| 4041 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4042 | if( status != PSA_SUCCESS ) |
| 4043 | goto cleanup; |
| 4044 | |
| 4045 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4046 | status = psa_key_derivation_start_hmac( &hmac, |
| 4047 | hash_alg, |
| 4048 | tls12_prf->secret, |
| 4049 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4050 | if( status != PSA_SUCCESS ) |
| 4051 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4052 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4053 | if( status != PSA_SUCCESS ) |
| 4054 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4055 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4056 | if( status != PSA_SUCCESS ) |
| 4057 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4058 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4059 | if( status != PSA_SUCCESS ) |
| 4060 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4061 | status = psa_mac_sign_finish( &hmac, |
| 4062 | tls12_prf->output_block, hash_length, |
| 4063 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4064 | if( status != PSA_SUCCESS ) |
| 4065 | goto cleanup; |
| 4066 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4067 | |
| 4068 | cleanup: |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4069 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4070 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4071 | status = cleanup_status; |
| 4072 | |
| 4073 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4074 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4075 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4076 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4077 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4078 | psa_algorithm_t alg, |
| 4079 | uint8_t *output, |
| 4080 | size_t output_length ) |
| 4081 | { |
| 4082 | 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] | 4083 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4084 | psa_status_t status; |
| 4085 | uint8_t offset, length; |
| 4086 | |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4087 | switch( tls12_prf->state ) |
| 4088 | { |
| 4089 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4090 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4091 | break; |
| 4092 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4093 | break; |
| 4094 | default: |
| 4095 | return( PSA_ERROR_BAD_STATE ); |
| 4096 | } |
| 4097 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4098 | while( output_length != 0 ) |
| 4099 | { |
| 4100 | /* Check if we have fully processed the current block. */ |
| 4101 | if( tls12_prf->left_in_block == 0 ) |
| 4102 | { |
| 4103 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4104 | alg ); |
| 4105 | if( status != PSA_SUCCESS ) |
| 4106 | return( status ); |
| 4107 | |
| 4108 | continue; |
| 4109 | } |
| 4110 | |
| 4111 | if( tls12_prf->left_in_block > output_length ) |
| 4112 | length = (uint8_t) output_length; |
| 4113 | else |
| 4114 | length = tls12_prf->left_in_block; |
| 4115 | |
| 4116 | offset = hash_length - tls12_prf->left_in_block; |
| 4117 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4118 | output += length; |
| 4119 | output_length -= length; |
| 4120 | tls12_prf->left_in_block -= length; |
| 4121 | } |
| 4122 | |
| 4123 | return( PSA_SUCCESS ); |
| 4124 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4125 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4126 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4127 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4128 | psa_status_t psa_key_derivation_output_bytes( |
| 4129 | psa_key_derivation_operation_t *operation, |
| 4130 | uint8_t *output, |
| 4131 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4132 | { |
| 4133 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4134 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4135 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4136 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4137 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4138 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4139 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4142 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4143 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4144 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4145 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4146 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4147 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4148 | goto exit; |
| 4149 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4150 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4151 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4152 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4153 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4154 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4155 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4156 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4157 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4158 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4159 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4160 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4161 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4162 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4163 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4164 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4165 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4166 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4167 | output, output_length ); |
| 4168 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4169 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4170 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4171 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4172 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4173 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4174 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4175 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4176 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4177 | kdf_alg, output, |
| 4178 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4179 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4180 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4181 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4182 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4183 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4184 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4185 | return( PSA_ERROR_BAD_STATE ); |
| 4186 | } |
| 4187 | |
| 4188 | exit: |
| 4189 | if( status != PSA_SUCCESS ) |
| 4190 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4191 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4192 | * This allows us to differentiate between exhausted operations and |
| 4193 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4194 | * operations. */ |
| 4195 | psa_algorithm_t alg = operation->alg; |
| 4196 | psa_key_derivation_abort( operation ); |
| 4197 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4198 | memset( output, '!', output_length ); |
| 4199 | } |
| 4200 | return( status ); |
| 4201 | } |
| 4202 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4203 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4204 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4205 | { |
| 4206 | if( data_size >= 8 ) |
| 4207 | mbedtls_des_key_set_parity( data ); |
| 4208 | if( data_size >= 16 ) |
| 4209 | mbedtls_des_key_set_parity( data + 8 ); |
| 4210 | if( data_size >= 24 ) |
| 4211 | mbedtls_des_key_set_parity( data + 16 ); |
| 4212 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4213 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4214 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4215 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4216 | psa_key_slot_t *slot, |
| 4217 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4218 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4219 | { |
| 4220 | uint8_t *data = NULL; |
| 4221 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4222 | psa_status_t status; |
| 4223 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4224 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4225 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4226 | if( bits % 8 != 0 ) |
| 4227 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4228 | data = mbedtls_calloc( 1, bytes ); |
| 4229 | if( data == NULL ) |
| 4230 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4231 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4232 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4233 | if( status != PSA_SUCCESS ) |
| 4234 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4235 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4236 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4237 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4238 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4239 | |
| 4240 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 4241 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4242 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4243 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4244 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4245 | psa_key_attributes_t attributes = { |
| 4246 | .core = slot->attr |
| 4247 | }; |
| 4248 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4249 | status = psa_driver_wrapper_import_key( &attributes, |
| 4250 | data, bytes, |
| 4251 | slot->key.data, |
| 4252 | slot->key.bytes, |
| 4253 | &slot->key.bytes, &bits ); |
| 4254 | if( bits != slot->attr.bits ) |
| 4255 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4256 | |
| 4257 | exit: |
| 4258 | mbedtls_free( data ); |
| 4259 | return( status ); |
| 4260 | } |
| 4261 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4262 | 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] | 4263 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4264 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4265 | { |
| 4266 | psa_status_t status; |
| 4267 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4268 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4269 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4270 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4271 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4272 | /* Reject any attempt to create a zero-length key so that we don't |
| 4273 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4274 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4275 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4276 | |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4277 | if( operation->alg == PSA_ALG_NONE ) |
| 4278 | return( PSA_ERROR_BAD_STATE ); |
| 4279 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4280 | if( ! operation->can_output_key ) |
| 4281 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4282 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4283 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 4284 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4285 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4286 | if( driver != NULL ) |
| 4287 | { |
| 4288 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4289 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4290 | } |
| 4291 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4292 | if( status == PSA_SUCCESS ) |
| 4293 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4294 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4295 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4296 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4297 | } |
| 4298 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4299 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4300 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4301 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4302 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4303 | return( status ); |
| 4304 | } |
| 4305 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4306 | |
| 4307 | |
| 4308 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4309 | /* Key derivation */ |
| 4310 | /****************************************************************/ |
| 4311 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4312 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4313 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4314 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4315 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4316 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4317 | int is_kdf_alg_supported; |
| 4318 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4319 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4320 | * initialisers for this union leave some bytes unspecified.) */ |
| 4321 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4322 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4323 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4324 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4325 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 4326 | is_kdf_alg_supported = 1; |
| 4327 | else |
| 4328 | #endif |
| 4329 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4330 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 4331 | is_kdf_alg_supported = 1; |
| 4332 | else |
| 4333 | #endif |
| 4334 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4335 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 4336 | is_kdf_alg_supported = 1; |
| 4337 | else |
| 4338 | #endif |
| 4339 | is_kdf_alg_supported = 0; |
| 4340 | |
| 4341 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4342 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4343 | 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] | 4344 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4345 | if( hash_size == 0 ) |
| 4346 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4347 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4348 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4349 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4350 | { |
| 4351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4352 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4353 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4354 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4355 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4356 | |
| 4357 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4358 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4359 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4360 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4361 | 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] | 4362 | psa_algorithm_t alg ) |
| 4363 | { |
| 4364 | psa_status_t status; |
| 4365 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4366 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4367 | return( PSA_ERROR_BAD_STATE ); |
| 4368 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4369 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4370 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4371 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4372 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4373 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4374 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4375 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4376 | #else |
| 4377 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4378 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4379 | } |
| 4380 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4381 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4382 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4383 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4384 | #else |
| 4385 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4386 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4387 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4388 | else |
| 4389 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4390 | |
| 4391 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4392 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4393 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4396 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4397 | 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] | 4398 | psa_algorithm_t hash_alg, |
| 4399 | psa_key_derivation_step_t step, |
| 4400 | const uint8_t *data, |
| 4401 | size_t data_length ) |
| 4402 | { |
| 4403 | psa_status_t status; |
| 4404 | switch( step ) |
| 4405 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4406 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4407 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4408 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4409 | else |
| 4410 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4411 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4412 | hash_alg, |
| 4413 | data, data_length ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4414 | if( status != PSA_SUCCESS ) |
| 4415 | return( status ); |
| 4416 | hkdf->state = HKDF_STATE_STARTED; |
| 4417 | return( PSA_SUCCESS ); |
| 4418 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4419 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4420 | /* If no salt was provided, use an empty salt. */ |
| 4421 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4422 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4423 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4424 | hash_alg, |
| 4425 | NULL, 0 ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4426 | if( status != PSA_SUCCESS ) |
| 4427 | return( status ); |
| 4428 | hkdf->state = HKDF_STATE_STARTED; |
| 4429 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4430 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4431 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4432 | status = psa_mac_update( &hkdf->hmac, |
| 4433 | data, data_length ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4434 | if( status != PSA_SUCCESS ) |
| 4435 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4436 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4437 | hkdf->prk, |
| 4438 | sizeof( hkdf->prk ), |
| 4439 | &data_length ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4440 | if( status != PSA_SUCCESS ) |
| 4441 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4442 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4443 | hkdf->block_number = 0; |
| 4444 | hkdf->state = HKDF_STATE_KEYED; |
| 4445 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4446 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4447 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4448 | return( PSA_ERROR_BAD_STATE ); |
| 4449 | if( hkdf->info_set ) |
| 4450 | return( PSA_ERROR_BAD_STATE ); |
| 4451 | hkdf->info_length = data_length; |
| 4452 | if( data_length != 0 ) |
| 4453 | { |
| 4454 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4455 | if( hkdf->info == NULL ) |
| 4456 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4457 | memcpy( hkdf->info, data, data_length ); |
| 4458 | } |
| 4459 | hkdf->info_set = 1; |
| 4460 | return( PSA_SUCCESS ); |
| 4461 | default: |
| 4462 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4463 | } |
| 4464 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4465 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4466 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4467 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4468 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4469 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4470 | const uint8_t *data, |
| 4471 | size_t data_length ) |
| 4472 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4473 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4474 | return( PSA_ERROR_BAD_STATE ); |
| 4475 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4476 | if( data_length != 0 ) |
| 4477 | { |
| 4478 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4479 | if( prf->seed == NULL ) |
| 4480 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4481 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4482 | memcpy( prf->seed, data, data_length ); |
| 4483 | prf->seed_length = data_length; |
| 4484 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4485 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4486 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4487 | |
| 4488 | return( PSA_SUCCESS ); |
| 4489 | } |
| 4490 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4491 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4492 | const uint8_t *data, |
| 4493 | size_t data_length ) |
| 4494 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4495 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4496 | return( PSA_ERROR_BAD_STATE ); |
| 4497 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4498 | if( data_length != 0 ) |
| 4499 | { |
| 4500 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 4501 | if( prf->secret == NULL ) |
| 4502 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4503 | |
| 4504 | memcpy( prf->secret, data, data_length ); |
| 4505 | prf->secret_length = data_length; |
| 4506 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4507 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4508 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4509 | |
| 4510 | return( PSA_SUCCESS ); |
| 4511 | } |
| 4512 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4513 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 4514 | const uint8_t *data, |
| 4515 | size_t data_length ) |
| 4516 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4517 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4518 | return( PSA_ERROR_BAD_STATE ); |
| 4519 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4520 | if( data_length != 0 ) |
| 4521 | { |
| 4522 | prf->label = mbedtls_calloc( 1, data_length ); |
| 4523 | if( prf->label == NULL ) |
| 4524 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4525 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4526 | memcpy( prf->label, data, data_length ); |
| 4527 | prf->label_length = data_length; |
| 4528 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4529 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4530 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4531 | |
| 4532 | return( PSA_SUCCESS ); |
| 4533 | } |
| 4534 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4535 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4536 | psa_key_derivation_step_t step, |
| 4537 | const uint8_t *data, |
| 4538 | size_t data_length ) |
| 4539 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4540 | switch( step ) |
| 4541 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4542 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 4543 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4544 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4545 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4546 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4547 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4548 | default: |
| 4549 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4550 | } |
| 4551 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4552 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4553 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 4554 | |
| 4555 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4556 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4557 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4558 | const uint8_t *data, |
| 4559 | size_t data_length ) |
| 4560 | { |
| 4561 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4562 | 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] | 4563 | uint8_t *cur = pms; |
| 4564 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4565 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4566 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4567 | |
| 4568 | /* Quoting RFC 4279, Section 2: |
| 4569 | * |
| 4570 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4571 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4572 | * uint16 with the value N, and the PSK itself. |
| 4573 | */ |
| 4574 | |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 4575 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 4576 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4577 | memset( cur, 0, data_length ); |
| 4578 | cur += data_length; |
| 4579 | *cur++ = pms[0]; |
| 4580 | *cur++ = pms[1]; |
| 4581 | memcpy( cur, data, data_length ); |
| 4582 | cur += data_length; |
| 4583 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4584 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4585 | |
| 4586 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 4587 | return( status ); |
| 4588 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4589 | |
| 4590 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4591 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4592 | psa_key_derivation_step_t step, |
| 4593 | const uint8_t *data, |
| 4594 | size_t data_length ) |
| 4595 | { |
| 4596 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4597 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4598 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4599 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4600 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4601 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4602 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4603 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4604 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4605 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4606 | /** Check whether the given key type is acceptable for the given |
| 4607 | * input step of a key derivation. |
| 4608 | * |
| 4609 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4610 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4611 | * Both secret and non-secret inputs can alternatively have the type |
| 4612 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4613 | * that the input was passed as a buffer rather than via a key object. |
| 4614 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4615 | static int psa_key_derivation_check_input_type( |
| 4616 | psa_key_derivation_step_t step, |
| 4617 | psa_key_type_t key_type ) |
| 4618 | { |
| 4619 | switch( step ) |
| 4620 | { |
| 4621 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4622 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 4623 | return( PSA_SUCCESS ); |
| 4624 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4625 | return( PSA_SUCCESS ); |
| 4626 | break; |
| 4627 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4628 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4629 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4630 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4631 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 4632 | return( PSA_SUCCESS ); |
| 4633 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4634 | return( PSA_SUCCESS ); |
| 4635 | break; |
| 4636 | } |
| 4637 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4638 | } |
| 4639 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4640 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4641 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4642 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4643 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4644 | const uint8_t *data, |
| 4645 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4646 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4647 | psa_status_t status; |
| 4648 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 4649 | |
| 4650 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4651 | if( status != PSA_SUCCESS ) |
| 4652 | goto exit; |
| 4653 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4654 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4655 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4656 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4657 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4658 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4659 | step, data, data_length ); |
| 4660 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4661 | else |
| 4662 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4663 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4664 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4665 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4666 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4667 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4668 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4669 | else |
| 4670 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4671 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4672 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4673 | { |
| 4674 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4675 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4676 | } |
| 4677 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4678 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4679 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4680 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4681 | (void) data; |
| 4682 | (void) data_length; |
| 4683 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4684 | return( PSA_ERROR_BAD_STATE ); |
| 4685 | } |
| 4686 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4687 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4688 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4689 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4690 | return( status ); |
| 4691 | } |
| 4692 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4693 | psa_status_t psa_key_derivation_input_bytes( |
| 4694 | psa_key_derivation_operation_t *operation, |
| 4695 | psa_key_derivation_step_t step, |
| 4696 | const uint8_t *data, |
| 4697 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4698 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4699 | return( psa_key_derivation_input_internal( operation, step, |
| 4700 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 4701 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4702 | } |
| 4703 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4704 | psa_status_t psa_key_derivation_input_key( |
| 4705 | psa_key_derivation_operation_t *operation, |
| 4706 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4707 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4708 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4709 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4710 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4711 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 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 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4715 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4716 | { |
| 4717 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4718 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4719 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4720 | |
| 4721 | /* Passing a key object as a SECRET input unlocks the permission |
| 4722 | * to output to a key object. */ |
| 4723 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4724 | operation->can_output_key = 1; |
| 4725 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4726 | status = psa_key_derivation_input_internal( operation, |
| 4727 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4728 | slot->key.data, |
| 4729 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4730 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4731 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4732 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4733 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4734 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4735 | |
| 4736 | |
| 4737 | |
| 4738 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4739 | /* Key agreement */ |
| 4740 | /****************************************************************/ |
| 4741 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4742 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4743 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 4744 | size_t peer_key_length, |
| 4745 | const mbedtls_ecp_keypair *our_key, |
| 4746 | uint8_t *shared_secret, |
| 4747 | size_t shared_secret_size, |
| 4748 | size_t *shared_secret_length ) |
| 4749 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 4750 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4751 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4752 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4753 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 4754 | 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] | 4755 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4756 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4757 | status = mbedtls_psa_ecp_load_representation( |
| 4758 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4759 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4760 | peer_key, |
| 4761 | peer_key_length, |
| 4762 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4763 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4764 | goto exit; |
| 4765 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4766 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4767 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4768 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4769 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4770 | status = mbedtls_to_psa_error( |
| 4771 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 4772 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4773 | goto exit; |
| 4774 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4775 | status = mbedtls_to_psa_error( |
| 4776 | mbedtls_ecdh_calc_secret( &ecdh, |
| 4777 | shared_secret_length, |
| 4778 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4779 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4780 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4781 | if( status != PSA_SUCCESS ) |
| 4782 | goto exit; |
| 4783 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 4784 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4785 | |
| 4786 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 4787 | if( status != PSA_SUCCESS ) |
| 4788 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4789 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4790 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 4791 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4792 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4793 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4794 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4795 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4797 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4798 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4799 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 4800 | psa_key_slot_t *private_key, |
| 4801 | const uint8_t *peer_key, |
| 4802 | size_t peer_key_length, |
| 4803 | uint8_t *shared_secret, |
| 4804 | size_t shared_secret_size, |
| 4805 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4806 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4807 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4808 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4809 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4810 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4811 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4812 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4813 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4814 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 4815 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4816 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4817 | private_key->key.data, |
| 4818 | private_key->key.bytes, |
| 4819 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4820 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4821 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4822 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4823 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4824 | shared_secret, shared_secret_size, |
| 4825 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4826 | mbedtls_ecp_keypair_free( ecp ); |
| 4827 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4828 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4829 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4830 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4831 | (void) private_key; |
| 4832 | (void) peer_key; |
| 4833 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4834 | (void) shared_secret; |
| 4835 | (void) shared_secret_size; |
| 4836 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4837 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4838 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4839 | } |
| 4840 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4841 | /* 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] | 4842 | * to potentially free embedded data structures and wipe confidential data. |
| 4843 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4844 | 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] | 4845 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4846 | psa_key_slot_t *private_key, |
| 4847 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4848 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4849 | { |
| 4850 | psa_status_t status; |
| 4851 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4852 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4853 | 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] | 4854 | |
| 4855 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4856 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4857 | status = psa_key_agreement_raw_internal( ka_alg, |
| 4858 | private_key, |
| 4859 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4860 | shared_secret, |
| 4861 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4862 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4863 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4864 | goto exit; |
| 4865 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4866 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4867 | * the shared secret. A shared secret is permitted wherever a key |
| 4868 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4869 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4870 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4871 | shared_secret, |
| 4872 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4873 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4874 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4875 | return( status ); |
| 4876 | } |
| 4877 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4878 | 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] | 4879 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4880 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4881 | const uint8_t *peer_key, |
| 4882 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4883 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4884 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4885 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4886 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4887 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4888 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4889 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4890 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4891 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4892 | if( status != PSA_SUCCESS ) |
| 4893 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4894 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4895 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4896 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4897 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4898 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4899 | else |
| 4900 | { |
| 4901 | /* If a private key has been added as SECRET, we allow the derived |
| 4902 | * key material to be used as a key in PSA Crypto. */ |
| 4903 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4904 | operation->can_output_key = 1; |
| 4905 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4906 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4907 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4908 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4909 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4910 | } |
| 4911 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4912 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4913 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4914 | const uint8_t *peer_key, |
| 4915 | size_t peer_key_length, |
| 4916 | uint8_t *output, |
| 4917 | size_t output_size, |
| 4918 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4919 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4920 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4921 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4922 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4923 | |
| 4924 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 4925 | { |
| 4926 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4927 | goto exit; |
| 4928 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4929 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4930 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4931 | if( status != PSA_SUCCESS ) |
| 4932 | goto exit; |
| 4933 | |
| 4934 | status = psa_key_agreement_raw_internal( alg, slot, |
| 4935 | peer_key, peer_key_length, |
| 4936 | output, output_size, |
| 4937 | output_length ); |
| 4938 | |
| 4939 | exit: |
| 4940 | if( status != PSA_SUCCESS ) |
| 4941 | { |
| 4942 | /* If an error happens and is not handled properly, the output |
| 4943 | * may be used as a key to protect sensitive data. Arrange for such |
| 4944 | * a key to be random, which is likely to result in decryption or |
| 4945 | * verification errors. This is better than filling the buffer with |
| 4946 | * some constant data such as zeros, which would result in the data |
| 4947 | * being protected with a reproducible, easily knowable key. |
| 4948 | */ |
| 4949 | psa_generate_random( output, output_size ); |
| 4950 | *output_length = output_size; |
| 4951 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4952 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4953 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4954 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4955 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4956 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4957 | |
| 4958 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4959 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4960 | /****************************************************************/ |
| 4961 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4962 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4963 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4964 | /** Initialize the PSA random generator. |
| 4965 | */ |
| 4966 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 4967 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4968 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4969 | memset( rng, 0, sizeof( *rng ) ); |
| 4970 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4971 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4972 | /* Set default configuration if |
| 4973 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 4974 | if( rng->entropy_init == NULL ) |
| 4975 | rng->entropy_init = mbedtls_entropy_init; |
| 4976 | if( rng->entropy_free == NULL ) |
| 4977 | rng->entropy_free = mbedtls_entropy_free; |
| 4978 | |
| 4979 | rng->entropy_init( &rng->entropy ); |
| 4980 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 4981 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 4982 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 4983 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 4984 | mbedtls_entropy_add_source( &rng->entropy, |
| 4985 | mbedtls_nv_seed_poll, NULL, |
| 4986 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 4987 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 4988 | #endif |
| 4989 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4990 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4991 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4992 | } |
| 4993 | |
| 4994 | /** Deinitialize the PSA random generator. |
| 4995 | */ |
| 4996 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 4997 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4998 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4999 | memset( rng, 0, sizeof( *rng ) ); |
| 5000 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5001 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5002 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5003 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | /** Seed the PSA random generator. |
| 5007 | */ |
| 5008 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5009 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5010 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5011 | /* Do nothing: the external RNG seeds itself. */ |
| 5012 | (void) rng; |
| 5013 | return( PSA_SUCCESS ); |
| 5014 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5015 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5016 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5017 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5018 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5019 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5020 | } |
| 5021 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5022 | psa_status_t psa_generate_random( uint8_t *output, |
| 5023 | size_t output_size ) |
| 5024 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5025 | GUARD_MODULE_INITIALIZED; |
| 5026 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5027 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5028 | |
| 5029 | size_t output_length = 0; |
| 5030 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 5031 | output, output_size, |
| 5032 | &output_length ); |
| 5033 | if( status != PSA_SUCCESS ) |
| 5034 | return( status ); |
| 5035 | /* Breaking up a request into smaller chunks is currently not supported |
| 5036 | * for the extrernal RNG interface. */ |
| 5037 | if( output_length != output_size ) |
| 5038 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 5039 | return( PSA_SUCCESS ); |
| 5040 | |
| 5041 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5042 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5043 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5044 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5045 | size_t request_size = |
| 5046 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5047 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5048 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5049 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 5050 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5051 | if( ret != 0 ) |
| 5052 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5053 | output_size -= request_size; |
| 5054 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5055 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5056 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5057 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5058 | } |
| 5059 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5060 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5061 | * |
| 5062 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5063 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5064 | * PSA API doesn't support passing an explicit state. |
| 5065 | * |
| 5066 | * In the non-external case, psa_generate_random() calls an |
| 5067 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5068 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5069 | * instead of doing this back-and-forth between the PSA API and the |
| 5070 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5071 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5072 | */ |
| 5073 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5074 | int mbedtls_psa_get_random( void *p_rng, |
| 5075 | unsigned char *output, |
| 5076 | size_t output_size ) |
| 5077 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5078 | /* This function takes a pointer to the RNG state because that's what |
| 5079 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5080 | * its own state internally and doesn't let the caller access that state. |
| 5081 | * So we just ignore the state parameter, and in practice we'll pass |
| 5082 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5083 | (void) p_rng; |
| 5084 | psa_status_t status = psa_generate_random( output, output_size ); |
| 5085 | if( status == PSA_SUCCESS ) |
| 5086 | return( 0 ); |
| 5087 | else |
| 5088 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 5089 | } |
| 5090 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5091 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5092 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5093 | #include "mbedtls/entropy_poll.h" |
| 5094 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5095 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5096 | size_t seed_size ) |
| 5097 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5098 | if( global_data.initialized ) |
| 5099 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5100 | |
| 5101 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5102 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5103 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5104 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5105 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5106 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5107 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5108 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5109 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5110 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5111 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5112 | * \param type The key type |
| 5113 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5114 | * |
| 5115 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5116 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5117 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5118 | * The size in bits of the key is not valid. |
| 5119 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5120 | * The type and/or the size in bits of the key or the combination of |
| 5121 | * the two is not supported. |
| 5122 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5123 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 5124 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5125 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5126 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5127 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5128 | if( key_type_is_raw_bytes( type ) ) |
| 5129 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5130 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5131 | if( status != PSA_SUCCESS ) |
| 5132 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5133 | } |
| 5134 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5135 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5136 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5137 | { |
| 5138 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5139 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5140 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5141 | /* Accept only byte-aligned keys, for the same reasons as |
| 5142 | * in psa_import_rsa_key(). */ |
| 5143 | if( bits % 8 != 0 ) |
| 5144 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5145 | } |
| 5146 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5147 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5148 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5149 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5150 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5151 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 5152 | /* To avoid empty block, return successfully here. */ |
| 5153 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5154 | } |
| 5155 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5156 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5157 | { |
| 5158 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5159 | } |
| 5160 | |
| 5161 | return( PSA_SUCCESS ); |
| 5162 | } |
| 5163 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5164 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5165 | const psa_key_attributes_t *attributes, |
| 5166 | 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] | 5167 | { |
| 5168 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5169 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5170 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5171 | if( ( attributes->domain_parameters == NULL ) && |
| 5172 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5173 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5174 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5175 | if( key_type_is_raw_bytes( type ) ) |
| 5176 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5177 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5178 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5179 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5180 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5181 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5182 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5183 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5184 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5185 | } |
| 5186 | else |
| 5187 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5188 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5189 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5190 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5191 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 5192 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 5193 | key_buffer, |
| 5194 | key_buffer_size, |
| 5195 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5196 | } |
| 5197 | else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5198 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5199 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5200 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5201 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5202 | 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] | 5203 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 5204 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 5205 | key_buffer, |
| 5206 | key_buffer_size, |
| 5207 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5208 | } |
| 5209 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5210 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5211 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5212 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5213 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5214 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5215 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5216 | return( PSA_SUCCESS ); |
| 5217 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5218 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5219 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5220 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5221 | { |
| 5222 | psa_status_t status; |
| 5223 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5224 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5225 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5226 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5227 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5228 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5229 | /* Reject any attempt to create a zero-length key so that we don't |
| 5230 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5231 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5233 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5234 | /* Reject any attempt to create a public key. */ |
| 5235 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 5236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5237 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5238 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 5239 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5240 | if( status != PSA_SUCCESS ) |
| 5241 | goto exit; |
| 5242 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5243 | /* In the case of a transparent key or an opaque key stored in local |
| 5244 | * storage (thus not in the case of generating a key in a secure element |
| 5245 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5246 | * hold the generated key material. */ |
| 5247 | if( slot->key.data == NULL ) |
| 5248 | { |
| 5249 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 5250 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 5251 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5252 | status = psa_validate_key_type_and_size_for_key_generation( |
| 5253 | attributes->core.type, attributes->core.bits ); |
| 5254 | if( status != PSA_SUCCESS ) |
| 5255 | goto exit; |
| 5256 | |
| 5257 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 5258 | attributes->core.type, |
| 5259 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5260 | } |
| 5261 | else |
| 5262 | { |
| 5263 | status = psa_driver_wrapper_get_key_buffer_size( |
| 5264 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5265 | if( status != PSA_SUCCESS ) |
| 5266 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5267 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5268 | |
| 5269 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 5270 | if( status != PSA_SUCCESS ) |
| 5271 | goto exit; |
| 5272 | } |
| 5273 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 5274 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5275 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5276 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5277 | if( status != PSA_SUCCESS ) |
| 5278 | psa_remove_key_data_from_memory( slot ); |
| 5279 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5280 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5281 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5282 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5283 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5284 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5285 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5286 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5287 | } |
| 5288 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5289 | /****************************************************************/ |
| 5290 | /* Module setup */ |
| 5291 | /****************************************************************/ |
| 5292 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5293 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5294 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5295 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5296 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5297 | { |
| 5298 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5299 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5300 | global_data.rng.entropy_init = entropy_init; |
| 5301 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5302 | return( PSA_SUCCESS ); |
| 5303 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5304 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5305 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5306 | void mbedtls_psa_crypto_free( void ) |
| 5307 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5308 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5309 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5310 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5311 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5312 | } |
| 5313 | /* Wipe all remaining data, including configuration. |
| 5314 | * In particular, this sets all state indicator to the value |
| 5315 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5316 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5317 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5318 | /* Unregister all secure element drivers, so that we restart from |
| 5319 | * a pristine state. */ |
| 5320 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5321 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5322 | } |
| 5323 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5324 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5325 | /** Recover a transaction that was interrupted by a power failure. |
| 5326 | * |
| 5327 | * This function is called during initialization, before psa_crypto_init() |
| 5328 | * returns. If this function returns a failure status, the initialization |
| 5329 | * fails. |
| 5330 | */ |
| 5331 | static psa_status_t psa_crypto_recover_transaction( |
| 5332 | const psa_crypto_transaction_t *transaction ) |
| 5333 | { |
| 5334 | switch( transaction->unknown.type ) |
| 5335 | { |
| 5336 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5337 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5338 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5339 | * is implemented. |
| 5340 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5341 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5342 | default: |
| 5343 | /* We found an unsupported transaction in the storage. |
| 5344 | * 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] | 5345 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5346 | } |
| 5347 | } |
| 5348 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5349 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5350 | psa_status_t psa_crypto_init( void ) |
| 5351 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5352 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5353 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5354 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5355 | if( global_data.initialized != 0 ) |
| 5356 | return( PSA_SUCCESS ); |
| 5357 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5358 | /* Initialize and seed the random generator. */ |
| 5359 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5360 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5361 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5362 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5363 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5364 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5365 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5366 | status = psa_initialize_key_slots( ); |
| 5367 | if( status != PSA_SUCCESS ) |
| 5368 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5369 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5370 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5371 | status = psa_init_all_se_drivers( ); |
| 5372 | if( status != PSA_SUCCESS ) |
| 5373 | goto exit; |
| 5374 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5375 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5376 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5377 | status = psa_crypto_load_transaction( ); |
| 5378 | if( status == PSA_SUCCESS ) |
| 5379 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5380 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5381 | if( status != PSA_SUCCESS ) |
| 5382 | goto exit; |
| 5383 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5384 | } |
| 5385 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5386 | { |
| 5387 | /* There's no transaction to complete. It's all good. */ |
| 5388 | status = PSA_SUCCESS; |
| 5389 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5390 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5391 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5392 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5393 | global_data.initialized = 1; |
| 5394 | |
| 5395 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5396 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5397 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5398 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |