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 | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 111 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 112 | unsigned rng_state : 2; |
Gilles Peskine | b8006a6 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 113 | mbedtls_psa_random_context_t rng; |
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 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 388 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 389 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 390 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 391 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 392 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 393 | 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] | 394 | size_t bits, |
| 395 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 396 | { |
| 397 | switch( curve ) |
| 398 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 399 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 400 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 401 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 402 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 403 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 404 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 405 | #endif |
| 406 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 407 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 409 | #endif |
| 410 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 413 | #endif |
| 414 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 417 | #endif |
| 418 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 419 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 420 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 421 | case 528: |
| 422 | if( bits_is_sloppy ) |
| 423 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 424 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 425 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | } |
| 427 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 428 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 429 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 430 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 431 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 432 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 433 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 434 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 435 | #endif |
| 436 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 437 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 438 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 439 | #endif |
| 440 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 441 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 442 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 443 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 444 | } |
| 445 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 446 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 447 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 448 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 450 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 451 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 452 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 453 | case 256: |
| 454 | if( bits_is_sloppy ) |
| 455 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 456 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 457 | #endif |
| 458 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 459 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 460 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 461 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 462 | } |
| 463 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 464 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 465 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 466 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 467 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 468 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 469 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 470 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 471 | #endif |
| 472 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 473 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 474 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 475 | #endif |
| 476 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 477 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 478 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 479 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 480 | } |
| 481 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 482 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 483 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 484 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 485 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 486 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 487 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 488 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 489 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 490 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 491 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 492 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 493 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 494 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | { |
| 496 | /* Check that the bit size is acceptable for the key type */ |
| 497 | switch( type ) |
| 498 | { |
| 499 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 500 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 501 | case PSA_KEY_TYPE_DERIVE: |
| 502 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 503 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 504 | case PSA_KEY_TYPE_AES: |
| 505 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 506 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 507 | break; |
| 508 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 509 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 510 | case PSA_KEY_TYPE_ARIA: |
| 511 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 512 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 513 | break; |
| 514 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 515 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 516 | case PSA_KEY_TYPE_CAMELLIA: |
| 517 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 519 | break; |
| 520 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 521 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 522 | case PSA_KEY_TYPE_DES: |
| 523 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 525 | break; |
| 526 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 527 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 528 | case PSA_KEY_TYPE_ARC4: |
| 529 | if( bits < 8 || bits > 2048 ) |
| 530 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 531 | break; |
| 532 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 533 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 534 | case PSA_KEY_TYPE_CHACHA20: |
| 535 | if( bits != 256 ) |
| 536 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 537 | break; |
| 538 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 539 | default: |
| 540 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 541 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 542 | if( bits % 8 != 0 ) |
| 543 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 544 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 545 | return( PSA_SUCCESS ); |
| 546 | } |
| 547 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 548 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 549 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 550 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 551 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 552 | * |
| 553 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 554 | * \param[in] key_type The key type of the key to be used with the |
| 555 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 556 | * |
| 557 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 558 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 559 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 560 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 561 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 562 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 563 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 564 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 565 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 566 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 567 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 568 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 569 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 570 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 571 | |
| 572 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 573 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 574 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 575 | * key. */ |
| 576 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 577 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 578 | { |
| 579 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 580 | * the block length (larger than 1) for block ciphers. */ |
| 581 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 582 | return( PSA_SUCCESS ); |
| 583 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 584 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 585 | |
| 586 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 589 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 590 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 591 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 592 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 593 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 594 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 595 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 596 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 597 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 598 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 599 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 600 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 603 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 604 | const uint8_t* data, |
| 605 | size_t data_length ) |
| 606 | { |
| 607 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 608 | data_length ); |
| 609 | if( status != PSA_SUCCESS ) |
| 610 | return( status ); |
| 611 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 612 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 613 | return( PSA_SUCCESS ); |
| 614 | } |
| 615 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 616 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 617 | const psa_key_attributes_t *attributes, |
| 618 | const uint8_t *data, size_t data_length, |
| 619 | uint8_t *key_buffer, size_t key_buffer_size, |
| 620 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 621 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 622 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 623 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 624 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 625 | /* zero-length keys are never supported. */ |
| 626 | if( data_length == 0 ) |
| 627 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 628 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 629 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 630 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 631 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 632 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 633 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 634 | if( data_length > SIZE_MAX / 8 ) |
| 635 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 636 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 637 | /* Enforce a size limit, and in particular ensure that the bit |
| 638 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 639 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 640 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 641 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 642 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 643 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 644 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 645 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 646 | /* Copy the key material. */ |
| 647 | memcpy( key_buffer, data, data_length ); |
| 648 | *key_buffer_length = data_length; |
| 649 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 650 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 651 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 652 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 653 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 654 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 655 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 656 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 657 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 658 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 659 | return( mbedtls_psa_ecp_import_key( attributes, |
| 660 | data, data_length, |
| 661 | key_buffer, key_buffer_size, |
| 662 | key_buffer_length, |
| 663 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 664 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 665 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 666 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 667 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 668 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 669 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 670 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 671 | return( mbedtls_psa_rsa_import_key( attributes, |
| 672 | data, data_length, |
| 673 | key_buffer, key_buffer_size, |
| 674 | key_buffer_length, |
| 675 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 676 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 677 | #endif /* 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 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 680 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 681 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 684 | /** Calculate the intersection of two algorithm usage policies. |
| 685 | * |
| 686 | * Return 0 (which allows no operation) on incompatibility. |
| 687 | */ |
| 688 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 689 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 690 | psa_algorithm_t alg1, |
| 691 | psa_algorithm_t alg2 ) |
| 692 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 693 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 694 | if( alg1 == alg2 ) |
| 695 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | /* If the policies are from the same hash-and-sign family, check |
| 697 | * 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] | 698 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 699 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 700 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 701 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 702 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 703 | return( alg2 ); |
| 704 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 705 | return( alg1 ); |
| 706 | } |
| 707 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 708 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 709 | * restrictive tag length. */ |
| 710 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 711 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 712 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 713 | { |
| 714 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 715 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 716 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 717 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 718 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 719 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 720 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 721 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 722 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 723 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 724 | } |
| 725 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 726 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 727 | ( alg1_len <= alg2_len ) ) |
| 728 | { |
| 729 | return( alg2 ); |
| 730 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 731 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 732 | ( alg2_len <= alg1_len ) ) |
| 733 | { |
| 734 | return( alg1 ); |
| 735 | } |
| 736 | } |
| 737 | /* If the policies are from the same MAC family, check whether one |
| 738 | * of them is a minimum-MAC-length policy. Calculate the most |
| 739 | * restrictive tag length. */ |
| 740 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 741 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 742 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 743 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 744 | /* Validate the combination of key type and algorithm. Since the base |
| 745 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 746 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 747 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 748 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 749 | /* Get the (exact or at-least) output lengths for both sides of the |
| 750 | * requested intersection. None of the currently supported algorithms |
| 751 | * have an output length dependent on the actual key size, so setting it |
| 752 | * to a bogus value of 0 is currently OK. |
| 753 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 754 | * Note that for at-least-this-length wildcard algorithms, the output |
| 755 | * length is set to the shortest allowed length, which allows us to |
| 756 | * calculate the most restrictive tag length for the intersection. */ |
| 757 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 758 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 759 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 760 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 761 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 762 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 763 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 764 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 765 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 766 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 767 | |
| 768 | /* If only one is an at-least-this-length policy, the intersection would |
| 769 | * be the other (fixed-length) policy as long as said fixed length is |
| 770 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 771 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 772 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 773 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 774 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 775 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 776 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 777 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 778 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 779 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 780 | /* If none of them are wildcards, check whether they define the same tag |
| 781 | * length. This is still possible here when one is default-length and |
| 782 | * the other specific-length. Ensure to always return the |
| 783 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 784 | if( alg1_len == alg2_len ) |
| 785 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 786 | } |
| 787 | /* If the policies are incompatible, allow nothing. */ |
| 788 | return( 0 ); |
| 789 | } |
| 790 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 791 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 792 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 793 | psa_algorithm_t requested_alg ) |
| 794 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 795 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 796 | if( requested_alg == policy_alg ) |
| 797 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 798 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 799 | * and requested_alg is the same hash-and-sign family with any hash, |
| 800 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 801 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 802 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 803 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 804 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 805 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 806 | } |
| 807 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 808 | * the requested algorithm, check the requested tag length to be |
| 809 | * equal-length or longer than the wildcard-specified length. */ |
| 810 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 811 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 812 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 813 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 814 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 815 | { |
| 816 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 817 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 818 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 819 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 820 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 821 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 822 | PSA_ALG_IS_MAC( requested_alg ) && |
| 823 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 824 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 825 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 826 | /* Validate the combination of key type and algorithm. Since the policy |
| 827 | * and requested algorithms are the same, we only need this once. */ |
| 828 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 829 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 830 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 831 | /* Get both the requested output length for the algorithm which is to be |
| 832 | * verified, and the default output length for the base algorithm. |
| 833 | * Note that none of the currently supported algorithms have an output |
| 834 | * length dependent on actual key size, so setting it to a bogus value |
| 835 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 836 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 837 | key_type, 0, requested_alg ); |
| 838 | size_t default_output_length = PSA_MAC_LENGTH( |
| 839 | key_type, 0, |
| 840 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 841 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 842 | /* If the policy is default-length, only allow an algorithm with |
| 843 | * a declared exact-length matching the default. */ |
| 844 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 845 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 846 | |
| 847 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 848 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 849 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 850 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 851 | { |
| 852 | return( 1 ); |
| 853 | } |
| 854 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 855 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 856 | * check for the requested MAC length to be equal to or longer than the |
| 857 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 858 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 859 | { |
| 860 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 861 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 862 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 863 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 864 | /* 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] | 865 | * a key derivation with that key agreement should also be allowed. This |
| 866 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 867 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 868 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 869 | { |
| 870 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 871 | policy_alg ); |
| 872 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 873 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 874 | return( 0 ); |
| 875 | } |
| 876 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 877 | /** Test whether a policy permits an algorithm. |
| 878 | * |
| 879 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 880 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 881 | * \note This function requires providing the key type for which the policy is |
| 882 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 883 | * have different properties depending on what kind of cipher it is |
| 884 | * combined with. |
| 885 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 886 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 887 | * allowed by the \p policy. |
| 888 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 889 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 890 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 891 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 892 | 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] | 893 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 894 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 895 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 896 | /* '0' is not a valid algorithm */ |
| 897 | if( alg == 0 ) |
| 898 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 899 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 900 | /* A requested algorithm cannot be a wildcard. */ |
| 901 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 902 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 903 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 904 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 905 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 906 | return( PSA_SUCCESS ); |
| 907 | else |
| 908 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 911 | /** Restrict a key policy based on a constraint. |
| 912 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 913 | * \note This function requires providing the key type for which the policy is |
| 914 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 915 | * have different properties depending on what kind of cipher it is |
| 916 | * combined with. |
| 917 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 918 | * \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] | 919 | * \param[in,out] policy The policy to restrict. |
| 920 | * \param[in] constraint The policy constraint to apply. |
| 921 | * |
| 922 | * \retval #PSA_SUCCESS |
| 923 | * \c *policy contains the intersection of the original value of |
| 924 | * \c *policy and \c *constraint. |
| 925 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 926 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 927 | * \c *policy is unchanged. |
| 928 | */ |
| 929 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 930 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 931 | psa_key_policy_t *policy, |
| 932 | const psa_key_policy_t *constraint ) |
| 933 | { |
| 934 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 935 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 936 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 937 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 938 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 939 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 940 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 941 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 942 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 943 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 944 | policy->usage &= constraint->usage; |
| 945 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 946 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 947 | return( PSA_SUCCESS ); |
| 948 | } |
| 949 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 950 | /** Get the description of a key given its identifier and policy constraints |
| 951 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 952 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 953 | * 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] | 954 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 955 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 956 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 957 | * In case of a persistent key, the function loads the description of the key |
| 958 | * into a key slot if not already done. |
| 959 | * |
| 960 | * On success, the returned key slot is locked. It is the responsibility of |
| 961 | * 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] | 962 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 963 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 964 | mbedtls_svc_key_id_t key, |
| 965 | psa_key_slot_t **p_slot, |
| 966 | psa_key_usage_t usage, |
| 967 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 968 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 969 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 970 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 971 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 972 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 973 | if( status != PSA_SUCCESS ) |
| 974 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 975 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 976 | |
| 977 | /* Enforce that usage policy for the key slot contains all the flags |
| 978 | * required by the usage parameter. There is one exception: public |
| 979 | * keys can always be exported, so we treat public key objects as |
| 980 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 981 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 982 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 983 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 984 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 985 | { |
| 986 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 987 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 988 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 989 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 990 | /* Enforce that the usage policy permits the requested algorithm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 991 | if( alg != 0 ) |
| 992 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 993 | status = psa_key_policy_permits( &slot->attr.policy, |
| 994 | slot->attr.type, |
| 995 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 996 | if( status != PSA_SUCCESS ) |
| 997 | goto error; |
| 998 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 999 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1000 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1001 | |
| 1002 | error: |
| 1003 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1004 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1005 | |
| 1006 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1007 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1008 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1009 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1010 | * |
| 1011 | * 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] | 1012 | * available, as opposed to a key in a secure element and/or to be used |
| 1013 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1014 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1015 | * This is a temporary function that may be used instead of |
| 1016 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1017 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1018 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1019 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1020 | * 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] | 1021 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1022 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1023 | mbedtls_svc_key_id_t key, |
| 1024 | psa_key_slot_t **p_slot, |
| 1025 | psa_key_usage_t usage, |
| 1026 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1027 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1028 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1029 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1030 | if( status != PSA_SUCCESS ) |
| 1031 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1032 | |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1033 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1034 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1035 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1036 | *p_slot = NULL; |
| 1037 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1038 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1039 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1040 | return( PSA_SUCCESS ); |
| 1041 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1042 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 1043 | 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] | 1044 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1045 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1046 | * initialized slot, so we can just free it. */ |
| 1047 | if( slot->key.data != NULL ) |
| 1048 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1049 | |
| 1050 | mbedtls_free( slot->key.data ); |
| 1051 | slot->key.data = NULL; |
| 1052 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1053 | |
| 1054 | return( PSA_SUCCESS ); |
| 1055 | } |
| 1056 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1057 | /** Completely wipe a slot in memory, including its policy. |
| 1058 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1059 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1060 | { |
| 1061 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1062 | |
| 1063 | /* |
| 1064 | * 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] | 1065 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1066 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1067 | * 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] | 1068 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1069 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1070 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1071 | { |
| 1072 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1073 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1074 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1075 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1076 | } |
| 1077 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1078 | /* Multipart operations may still be using the key. This is safe |
| 1079 | * because all multipart operation objects are independent from |
| 1080 | * the key slot: if they need to access the key after the setup |
| 1081 | * phase, they have a copy of the key. Note that this means that |
| 1082 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1083 | /* At this point, key material and other type-specific content has |
| 1084 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1085 | * zeroize because the metadata is not particularly sensitive. */ |
| 1086 | memset( slot, 0, sizeof( *slot ) ); |
| 1087 | return( status ); |
| 1088 | } |
| 1089 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1090 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1091 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1092 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1093 | psa_status_t status; /* status of the last operation */ |
| 1094 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1095 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1096 | psa_se_drv_table_entry_t *driver; |
| 1097 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1098 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1099 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1100 | return( PSA_SUCCESS ); |
| 1101 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1102 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1103 | * 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] | 1104 | * key, this will load the key description from persistent memory if not |
| 1105 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1106 | * the key is operated by an SE or not and this information is needed by |
| 1107 | * the current implementation. |
| 1108 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1109 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1110 | if( status != PSA_SUCCESS ) |
| 1111 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1112 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1113 | /* |
| 1114 | * If the key slot containing the key description is under access by the |
| 1115 | * library (apart from the present access), the key cannot be destroyed |
| 1116 | * yet. For the time being, just return in error. Eventually (to be |
| 1117 | * implemented), the key should be destroyed when all accesses have |
| 1118 | * stopped. |
| 1119 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1120 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1121 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1122 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1123 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1124 | } |
| 1125 | |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1126 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1127 | { |
| 1128 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1129 | * if we attempt it, depending on whether the key is merely read-only |
| 1130 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1131 | * Just do the best we can, which is to wipe the copy in memory |
| 1132 | * (done in this function's cleanup code). */ |
| 1133 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1134 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1137 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1138 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1139 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1140 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1141 | /* For a key in a secure element, we need to do three things: |
| 1142 | * remove the key file in internal storage, destroy the |
| 1143 | * key inside the secure element, and update the driver's |
| 1144 | * persistent data. Start a transaction that will encompass these |
| 1145 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1146 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1147 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1148 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1149 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1150 | status = psa_crypto_save_transaction( ); |
| 1151 | if( status != PSA_SUCCESS ) |
| 1152 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1153 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1154 | /* We should still try to destroy the key in the secure |
| 1155 | * element and the key metadata in storage. This is especially |
| 1156 | * important if the error is that the storage is full. |
| 1157 | * But how to do it exactly without risking an inconsistent |
| 1158 | * state after a reset? |
| 1159 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1160 | */ |
| 1161 | overall_status = status; |
| 1162 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1165 | status = psa_destroy_se_key( driver, |
| 1166 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1167 | if( overall_status == PSA_SUCCESS ) |
| 1168 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1169 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1170 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1171 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1172 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1173 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1174 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1175 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1176 | if( overall_status == PSA_SUCCESS ) |
| 1177 | overall_status = status; |
| 1178 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1179 | /* TODO: other slots may have a copy of the same key. We should |
| 1180 | * invalidate them. |
| 1181 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1182 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1183 | } |
| 1184 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1185 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1186 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1187 | if( driver != NULL ) |
| 1188 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1189 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1190 | if( overall_status == PSA_SUCCESS ) |
| 1191 | overall_status = status; |
| 1192 | status = psa_crypto_stop_transaction( ); |
| 1193 | if( overall_status == PSA_SUCCESS ) |
| 1194 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1195 | } |
| 1196 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1197 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1198 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1199 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1200 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1201 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1202 | overall_status = status; |
| 1203 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1206 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1207 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1208 | static psa_status_t psa_get_rsa_public_exponent( |
| 1209 | const mbedtls_rsa_context *rsa, |
| 1210 | psa_key_attributes_t *attributes ) |
| 1211 | { |
| 1212 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1213 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1214 | uint8_t *buffer = NULL; |
| 1215 | size_t buflen; |
| 1216 | mbedtls_mpi_init( &mpi ); |
| 1217 | |
| 1218 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1219 | if( ret != 0 ) |
| 1220 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1221 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1222 | { |
| 1223 | /* It's the default value, which is reported as an empty string, |
| 1224 | * so there's nothing to do. */ |
| 1225 | goto exit; |
| 1226 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1227 | |
| 1228 | buflen = mbedtls_mpi_size( &mpi ); |
| 1229 | buffer = mbedtls_calloc( 1, buflen ); |
| 1230 | if( buffer == NULL ) |
| 1231 | { |
| 1232 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1233 | goto exit; |
| 1234 | } |
| 1235 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1236 | if( ret != 0 ) |
| 1237 | goto exit; |
| 1238 | attributes->domain_parameters = buffer; |
| 1239 | attributes->domain_parameters_size = buflen; |
| 1240 | |
| 1241 | exit: |
| 1242 | mbedtls_mpi_free( &mpi ); |
| 1243 | if( ret != 0 ) |
| 1244 | mbedtls_free( buffer ); |
| 1245 | return( mbedtls_to_psa_error( ret ) ); |
| 1246 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1247 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1248 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1249 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1250 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1251 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1252 | 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] | 1253 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1254 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1255 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1256 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1257 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1258 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1259 | psa_reset_key_attributes( attributes ); |
| 1260 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1261 | 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] | 1262 | if( status != PSA_SUCCESS ) |
| 1263 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1264 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1265 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1266 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1267 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1268 | |
| 1269 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 0518f61 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1270 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1271 | psa_set_key_slot_number( attributes, |
| 1272 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1273 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1274 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1275 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1276 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1277 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1278 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1279 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1280 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1281 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1282 | * is not yet implemented. |
| 1283 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1284 | */ |
Ronald Cron | 41e8006 | 2021-08-24 16:00:51 +0200 | [diff] [blame] | 1285 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1286 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1287 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1288 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1289 | status = mbedtls_psa_rsa_load_representation( |
| 1290 | slot->attr.type, |
| 1291 | slot->key.data, |
| 1292 | slot->key.bytes, |
| 1293 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1294 | if( status != PSA_SUCCESS ) |
| 1295 | break; |
| 1296 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1297 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1298 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1299 | mbedtls_rsa_free( rsa ); |
| 1300 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1301 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1302 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1303 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1304 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1305 | default: |
| 1306 | /* Nothing else to do. */ |
| 1307 | break; |
| 1308 | } |
| 1309 | |
| 1310 | if( status != PSA_SUCCESS ) |
| 1311 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1312 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1313 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1314 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1315 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1318 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1319 | psa_status_t psa_get_key_slot_number( |
| 1320 | const psa_key_attributes_t *attributes, |
| 1321 | psa_key_slot_number_t *slot_number ) |
| 1322 | { |
| 1323 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1324 | { |
| 1325 | *slot_number = attributes->slot_number; |
| 1326 | return( PSA_SUCCESS ); |
| 1327 | } |
| 1328 | else |
| 1329 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1330 | } |
| 1331 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1332 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1333 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1334 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1335 | uint8_t *data, |
| 1336 | size_t data_size, |
| 1337 | size_t *data_length ) |
| 1338 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1339 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1340 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1341 | memcpy( data, key_buffer, key_buffer_size ); |
| 1342 | memset( data + key_buffer_size, 0, |
| 1343 | data_size - key_buffer_size ); |
| 1344 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1345 | return( PSA_SUCCESS ); |
| 1346 | } |
| 1347 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1348 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1349 | const psa_key_attributes_t *attributes, |
| 1350 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1351 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1353 | psa_key_type_t type = attributes->core.type; |
| 1354 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1355 | if( key_type_is_raw_bytes( type ) || |
| 1356 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1357 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1358 | { |
| 1359 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1360 | key_buffer, key_buffer_size, |
| 1361 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1362 | } |
| 1363 | else |
| 1364 | { |
| 1365 | /* This shouldn't happen in the reference implementation, but |
| 1366 | it is valid for a special-purpose implementation to omit |
| 1367 | support for exporting certain key types. */ |
| 1368 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1373 | uint8_t *data, |
| 1374 | size_t data_size, |
| 1375 | size_t *data_length ) |
| 1376 | { |
| 1377 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1378 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1379 | psa_key_slot_t *slot; |
| 1380 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1381 | /* Reject a zero-length output buffer now, since this can never be a |
| 1382 | * valid key representation. This way we know that data must be a valid |
| 1383 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1384 | if( data_size == 0 ) |
| 1385 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1386 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1387 | /* Set the key to empty now, so that even when there are errors, we always |
| 1388 | * set data_length to a value between 0 and data_size. On error, setting |
| 1389 | * the key to empty is a good choice because an empty key representation is |
| 1390 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1391 | *data_length = 0; |
| 1392 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1393 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1394 | * which don't require any flag, but |
| 1395 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1396 | */ |
| 1397 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1398 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1399 | if( status != PSA_SUCCESS ) |
| 1400 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1401 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1402 | psa_key_attributes_t attributes = { |
| 1403 | .core = slot->attr |
| 1404 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1405 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1406 | slot->key.data, slot->key.bytes, |
| 1407 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1408 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1409 | unlock_status = psa_unlock_key_slot( slot ); |
| 1410 | |
| 1411 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1412 | } |
| 1413 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1414 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1415 | const psa_key_attributes_t *attributes, |
| 1416 | const uint8_t *key_buffer, |
| 1417 | size_t key_buffer_size, |
| 1418 | uint8_t *data, |
| 1419 | size_t data_size, |
| 1420 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1421 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1422 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1423 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1424 | 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] | 1425 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1426 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1427 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1428 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1429 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1430 | key_buffer, key_buffer_size, |
| 1431 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1432 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1433 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1434 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1435 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1436 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1437 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1438 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1439 | key_buffer, |
| 1440 | key_buffer_size, |
| 1441 | data, |
| 1442 | data_size, |
| 1443 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1444 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1445 | /* We don't know how to convert a private RSA key to public. */ |
| 1446 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1447 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1448 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1449 | } |
| 1450 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1451 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1452 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1453 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1454 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1455 | key_buffer, |
| 1456 | key_buffer_size, |
| 1457 | data, |
| 1458 | data_size, |
| 1459 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1460 | #else |
| 1461 | /* 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] | 1462 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1463 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1464 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1465 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1466 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1467 | else |
| 1468 | { |
| 1469 | /* This shouldn't happen in the reference implementation, but |
| 1470 | it is valid for a special-purpose implementation to omit |
| 1471 | support for exporting certain key types. */ |
| 1472 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1473 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1474 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1475 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1476 | 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] | 1477 | uint8_t *data, |
| 1478 | size_t data_size, |
| 1479 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1480 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1481 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1482 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1483 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1484 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1485 | /* Reject a zero-length output buffer now, since this can never be a |
| 1486 | * valid key representation. This way we know that data must be a valid |
| 1487 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1488 | if( data_size == 0 ) |
| 1489 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1490 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1491 | /* Set the key to empty now, so that even when there are errors, we always |
| 1492 | * set data_length to a value between 0 and data_size. On error, setting |
| 1493 | * the key to empty is a good choice because an empty key representation is |
| 1494 | * unlikely to be accepted anywhere. */ |
| 1495 | *data_length = 0; |
| 1496 | |
| 1497 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1498 | 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] | 1499 | if( status != PSA_SUCCESS ) |
| 1500 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1501 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1502 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1503 | { |
| 1504 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1505 | goto exit; |
| 1506 | } |
| 1507 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1508 | psa_key_attributes_t attributes = { |
| 1509 | .core = slot->attr |
| 1510 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1511 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1512 | &attributes, slot->key.data, slot->key.bytes, |
| 1513 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1514 | |
| 1515 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1516 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1517 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1518 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1519 | } |
| 1520 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1521 | #if defined(static_assert) |
| 1522 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1523 | "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] | 1524 | 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] | 1525 | "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] | 1526 | 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] | 1527 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1528 | #endif |
| 1529 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1530 | /** Validate that a key policy is internally well-formed. |
| 1531 | * |
| 1532 | * This function only rejects invalid policies. It does not validate the |
| 1533 | * consistency of the policy with respect to other attributes of the key |
| 1534 | * such as the key type. |
| 1535 | */ |
| 1536 | 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] | 1537 | { |
| 1538 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1539 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1540 | PSA_KEY_USAGE_ENCRYPT | |
| 1541 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1542 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1543 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1544 | PSA_KEY_USAGE_SIGN_HASH | |
| 1545 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1546 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1547 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1548 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1549 | return( PSA_SUCCESS ); |
| 1550 | } |
| 1551 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1552 | /** Validate the internal consistency of key attributes. |
| 1553 | * |
| 1554 | * This function only rejects invalid attribute values. If does not |
| 1555 | * validate the consistency of the attributes with any key data that may |
| 1556 | * be involved in the creation of the key. |
| 1557 | * |
| 1558 | * Call this function early in the key creation process. |
| 1559 | * |
| 1560 | * \param[in] attributes Key attributes for the new key. |
| 1561 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1562 | * NULL for a transparent key. |
| 1563 | * |
| 1564 | */ |
| 1565 | static psa_status_t psa_validate_key_attributes( |
| 1566 | const psa_key_attributes_t *attributes, |
| 1567 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1568 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1569 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1570 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1571 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1572 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1573 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1574 | if( status != PSA_SUCCESS ) |
| 1575 | return( status ); |
| 1576 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1577 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1578 | if( status != PSA_SUCCESS ) |
| 1579 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1580 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1581 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1582 | { |
| 1583 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1584 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1585 | } |
| 1586 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1587 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1588 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1589 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1592 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1593 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1594 | return( status ); |
| 1595 | |
| 1596 | /* Refuse to create overly large keys. |
| 1597 | * Note that this doesn't trigger on import if the attributes don't |
| 1598 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1599 | * psa_import_key() needs its own checks. */ |
| 1600 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1601 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1602 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1603 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1604 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1605 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1606 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1607 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1608 | return( PSA_SUCCESS ); |
| 1609 | } |
| 1610 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1611 | /** Prepare a key slot to receive key material. |
| 1612 | * |
| 1613 | * This function allocates a key slot and sets its metadata. |
| 1614 | * |
| 1615 | * If this function fails, call psa_fail_key_creation(). |
| 1616 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1617 | * This function is intended to be used as follows: |
| 1618 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1619 | * it with the specified attributes, and in case of a volatile key assign it |
| 1620 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1621 | * -# Populate the slot with the key material. |
| 1622 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1623 | * In case of failure at any step, stop the sequence and call |
| 1624 | * psa_fail_key_creation(). |
| 1625 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1626 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1627 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1628 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1629 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1630 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1631 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1632 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1633 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1634 | * |
| 1635 | * \retval #PSA_SUCCESS |
| 1636 | * The key slot is ready to receive key material. |
| 1637 | * \return If this function fails, the key slot is an invalid state. |
| 1638 | * 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] | 1639 | */ |
| 1640 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1641 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1642 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1643 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1644 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | { |
| 1646 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1647 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1648 | psa_key_slot_t *slot; |
| 1649 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1650 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1651 | *p_drv = NULL; |
| 1652 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1653 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1654 | if( status != PSA_SUCCESS ) |
| 1655 | return( status ); |
| 1656 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1657 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1658 | if( status != PSA_SUCCESS ) |
| 1659 | return( status ); |
| 1660 | slot = *p_slot; |
| 1661 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1662 | /* We're storing the declared bit-size of the key. It's up to each |
| 1663 | * creation mechanism to verify that this information is correct. |
| 1664 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1665 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1666 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1667 | * volatile key identifier associated to the slot returned to contain its |
| 1668 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1669 | |
| 1670 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1671 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1672 | { |
| 1673 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1674 | slot->attr.id = volatile_key_id; |
| 1675 | #else |
| 1676 | slot->attr.id.key_id = volatile_key_id; |
| 1677 | #endif |
| 1678 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1679 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1680 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1681 | * external-only flags, query `attributes`. Thanks to the check |
| 1682 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1683 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1684 | * may have set. */ |
| 1685 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1686 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1687 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1688 | /* 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] | 1689 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1690 | * create the key file in internal storage, create the |
| 1691 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1692 | * persistent data. This is done by starting a transaction that will |
| 1693 | * encompass these three actions. |
| 1694 | * For registering a volatile key, we just need to find an appropriate |
| 1695 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1696 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1697 | /* The first thing to do is to find a slot number for the new key. |
| 1698 | * We save the slot number in persistent storage as part of the |
| 1699 | * transaction data. It will be needed to recover if the power |
| 1700 | * fails during the key creation process, to clean up on the secure |
| 1701 | * element side after restarting. Obtaining a slot number from the |
| 1702 | * secure element driver updates its persistent state, but we do not yet |
| 1703 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1704 | * 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] | 1705 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1706 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1707 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1708 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1709 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1710 | if( status != PSA_SUCCESS ) |
| 1711 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1712 | |
| 1713 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1714 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1715 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1716 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1717 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1718 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1719 | status = psa_crypto_save_transaction( ); |
| 1720 | if( status != PSA_SUCCESS ) |
| 1721 | { |
| 1722 | (void) psa_crypto_stop_transaction( ); |
| 1723 | return( status ); |
| 1724 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1725 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1726 | |
| 1727 | status = psa_copy_key_material_into_slot( |
| 1728 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1729 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1730 | |
| 1731 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1732 | { |
| 1733 | /* Key registration only makes sense with a secure element. */ |
| 1734 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1735 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1736 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1737 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1738 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1739 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1740 | |
| 1741 | /** Finalize the creation of a key once its key material has been set. |
| 1742 | * |
| 1743 | * This entails writing the key to persistent storage. |
| 1744 | * |
| 1745 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1746 | * See the documentation of psa_start_key_creation() for the intended use |
| 1747 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1748 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1749 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1750 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1751 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1752 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1753 | * \param[in,out] slot Pointer to the slot with key material. |
| 1754 | * \param[in] driver The secure element driver for the key, |
| 1755 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1756 | * \param[out] key On success, identifier of the key. Note that the |
| 1757 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1758 | * |
| 1759 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1760 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1761 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1762 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1763 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1764 | * \retval #PSA_ERROR_DATA_INVALID |
| 1765 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1766 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1767 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1768 | * \return If this function fails, the key slot is an invalid state. |
| 1769 | * 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] | 1770 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1771 | static psa_status_t psa_finish_key_creation( |
| 1772 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1773 | psa_se_drv_table_entry_t *driver, |
| 1774 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1775 | { |
| 1776 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1777 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1778 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1779 | |
| 1780 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1781 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1782 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1783 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1784 | if( driver != NULL ) |
| 1785 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1786 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1787 | psa_key_slot_number_t slot_number = |
| 1788 | psa_key_slot_get_slot_number( slot ) ; |
| 1789 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1790 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1791 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1792 | sizeof( data.slot_number ), |
| 1793 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1794 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1795 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1796 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1797 | (uint8_t*) &data, |
| 1798 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1799 | } |
| 1800 | else |
| 1801 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1802 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1803 | /* Key material is saved in export representation in the slot, so |
| 1804 | * just pass the slot buffer for storage. */ |
| 1805 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1806 | slot->key.data, |
| 1807 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1808 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1809 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1810 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1811 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1812 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1813 | /* Finish the transaction for a key creation. This does not |
| 1814 | * happen when registering an existing key. Detect this case |
| 1815 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1816 | * creation of a persistent key in a secure element requires a transaction, |
| 1817 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1818 | if( driver != NULL && |
| 1819 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1820 | { |
| 1821 | status = psa_save_se_persistent_data( driver ); |
| 1822 | if( status != PSA_SUCCESS ) |
| 1823 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1824 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1825 | return( status ); |
| 1826 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1827 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1828 | } |
| 1829 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1830 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1831 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1832 | { |
| 1833 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1834 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1835 | if( status != PSA_SUCCESS ) |
| 1836 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1837 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1838 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1839 | return( status ); |
| 1840 | } |
| 1841 | |
| 1842 | /** Abort the creation of a key. |
| 1843 | * |
| 1844 | * You may call this function after calling psa_start_key_creation(), |
| 1845 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1846 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1847 | * See the documentation of psa_start_key_creation() for the intended use |
| 1848 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1849 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1850 | * \param[in,out] slot Pointer to the slot with key material. |
| 1851 | * \param[in] driver The secure element driver for the key, |
| 1852 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1853 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1854 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1855 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1856 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1857 | (void) driver; |
| 1858 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1859 | if( slot == NULL ) |
| 1860 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1861 | |
| 1862 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1863 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1864 | * element, and the failure happened later (when saving metadata |
| 1865 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1866 | * element. |
| 1867 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1868 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1869 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1870 | /* Abort the ongoing transaction if any (there may not be one if |
| 1871 | * the creation process failed before starting one, or if the |
| 1872 | * key creation is a registration of a key in a secure element). |
| 1873 | * Earlier functions must already have done what it takes to undo any |
| 1874 | * partial creation. All that's left is to update the transaction data |
| 1875 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1876 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1877 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1878 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1879 | psa_wipe_key_slot( slot ); |
| 1880 | } |
| 1881 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1882 | /** Validate optional attributes during key creation. |
| 1883 | * |
| 1884 | * Some key attributes are optional during key creation. If they are |
| 1885 | * specified in the attributes structure, check that they are consistent |
| 1886 | * with the data in the slot. |
| 1887 | * |
| 1888 | * This function should be called near the end of key creation, after |
| 1889 | * the slot in memory is fully populated but before saving persistent data. |
| 1890 | */ |
| 1891 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1892 | const psa_key_slot_t *slot, |
| 1893 | const psa_key_attributes_t *attributes ) |
| 1894 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1895 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1896 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1897 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1898 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1899 | } |
| 1900 | |
| 1901 | if( attributes->domain_parameters_size != 0 ) |
| 1902 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1903 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1904 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1905 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1906 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1907 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1908 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1909 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1910 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1911 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1912 | slot->attr.type, |
| 1913 | slot->key.data, |
| 1914 | slot->key.bytes, |
| 1915 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1916 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1917 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1918 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1919 | mbedtls_mpi_init( &actual ); |
| 1920 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1921 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1922 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1923 | mbedtls_rsa_free( rsa ); |
| 1924 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1925 | if( ret != 0 ) |
| 1926 | goto rsa_exit; |
| 1927 | ret = mbedtls_mpi_read_binary( &required, |
| 1928 | attributes->domain_parameters, |
| 1929 | attributes->domain_parameters_size ); |
| 1930 | if( ret != 0 ) |
| 1931 | goto rsa_exit; |
| 1932 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1933 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1934 | rsa_exit: |
| 1935 | mbedtls_mpi_free( &actual ); |
| 1936 | mbedtls_mpi_free( &required ); |
| 1937 | if( ret != 0) |
| 1938 | return( mbedtls_to_psa_error( ret ) ); |
| 1939 | } |
| 1940 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1941 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1942 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1943 | { |
| 1944 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1945 | } |
| 1946 | } |
| 1947 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1948 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1949 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1950 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1951 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1952 | } |
| 1953 | |
| 1954 | return( PSA_SUCCESS ); |
| 1955 | } |
| 1956 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1957 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1958 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1959 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1960 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1961 | { |
| 1962 | psa_status_t status; |
| 1963 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1964 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1965 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1966 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1967 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1968 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1969 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1970 | * This also rejects any key which might be encoded as an empty string, |
| 1971 | * which is never valid. */ |
| 1972 | if( data_length == 0 ) |
| 1973 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1974 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1975 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1976 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1977 | if( status != PSA_SUCCESS ) |
| 1978 | goto exit; |
| 1979 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1980 | /* In the case of a transparent key or an opaque key stored in local |
| 1981 | * storage (thus not in the case of generating a key in a secure element |
| 1982 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1983 | * hold the generated key material. */ |
| 1984 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1985 | { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1986 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1987 | if( status != PSA_SUCCESS ) |
| 1988 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1989 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1990 | |
| 1991 | bits = slot->attr.bits; |
| 1992 | status = psa_driver_wrapper_import_key( attributes, |
| 1993 | data, data_length, |
| 1994 | slot->key.data, |
| 1995 | slot->key.bytes, |
| 1996 | &slot->key.bytes, &bits ); |
| 1997 | if( status != PSA_SUCCESS ) |
| 1998 | goto exit; |
| 1999 | |
| 2000 | if( slot->attr.bits == 0 ) |
| 2001 | slot->attr.bits = (psa_key_bits_t) bits; |
| 2002 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2003 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2004 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2005 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2006 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2007 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2008 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2009 | if( status != PSA_SUCCESS ) |
| 2010 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2011 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2012 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2013 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2014 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2015 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2017 | return( status ); |
| 2018 | } |
| 2019 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2020 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2021 | psa_status_t mbedtls_psa_register_se_key( |
| 2022 | const psa_key_attributes_t *attributes ) |
| 2023 | { |
| 2024 | psa_status_t status; |
| 2025 | psa_key_slot_t *slot = NULL; |
| 2026 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2027 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2028 | |
| 2029 | /* Leaving attributes unspecified is not currently supported. |
| 2030 | * It could make sense to query the key type and size from the |
| 2031 | * secure element, but not all secure elements support this |
| 2032 | * and the driver HAL doesn't currently support it. */ |
| 2033 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2034 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2035 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2036 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2037 | |
| 2038 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2039 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2040 | if( status != PSA_SUCCESS ) |
| 2041 | goto exit; |
| 2042 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2043 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2044 | |
| 2045 | exit: |
| 2046 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2047 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2048 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2049 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2050 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2051 | return( status ); |
| 2052 | } |
| 2053 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2054 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2055 | 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] | 2056 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2057 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2058 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2059 | source->key.data, |
| 2060 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2061 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2062 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2063 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2064 | target->attr.type = source->attr.type; |
| 2065 | target->attr.bits = source->attr.bits; |
| 2066 | |
| 2067 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2070 | 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] | 2071 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2072 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2073 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2074 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2075 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2076 | psa_key_slot_t *source_slot = NULL; |
| 2077 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2078 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2079 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2080 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2081 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2082 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2083 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2084 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2085 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2086 | goto exit; |
| 2087 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2088 | status = psa_validate_optional_attributes( source_slot, |
| 2089 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2090 | if( status != PSA_SUCCESS ) |
| 2091 | goto exit; |
| 2092 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2093 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2094 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2095 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2096 | if( status != PSA_SUCCESS ) |
| 2097 | goto exit; |
| 2098 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2099 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2100 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2101 | if( status != PSA_SUCCESS ) |
| 2102 | goto exit; |
| 2103 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2104 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2105 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2106 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2107 | /* Copying to a secure element is not implemented yet. */ |
| 2108 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2109 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2110 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2111 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2112 | |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2113 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2114 | { |
| 2115 | /* |
| 2116 | * Copying through an opaque driver is not implemented yet, consider |
| 2117 | * a lifetime with an external location as an invalid parameter for |
| 2118 | * now. |
| 2119 | */ |
| 2120 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2121 | goto exit; |
| 2122 | } |
| 2123 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2124 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2125 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2126 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2127 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2128 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2129 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2130 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2131 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2132 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2133 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2134 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2135 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2138 | |
| 2139 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2140 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2141 | /* Message digests */ |
| 2142 | /****************************************************************/ |
| 2143 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2144 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2145 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2146 | /* Aborting a non-active operation is allowed */ |
| 2147 | if( operation->id == 0 ) |
| 2148 | return( PSA_SUCCESS ); |
| 2149 | |
| 2150 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2151 | operation->id = 0; |
| 2152 | |
| 2153 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2154 | } |
| 2155 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2156 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2157 | psa_algorithm_t alg ) |
| 2158 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2159 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2160 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2161 | /* A context must be freshly initialized before it can be set up. */ |
| 2162 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2163 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2164 | status = PSA_ERROR_BAD_STATE; |
| 2165 | goto exit; |
| 2166 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2167 | |
| 2168 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2169 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2170 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2171 | goto exit; |
| 2172 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2173 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2174 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2175 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2176 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2177 | |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2178 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2179 | |
| 2180 | exit: |
| 2181 | if( status != PSA_SUCCESS ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2182 | psa_hash_abort( operation ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2183 | |
| 2184 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2188 | const uint8_t *input, |
| 2189 | size_t input_length ) |
| 2190 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2191 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2192 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2193 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2194 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2195 | status = PSA_ERROR_BAD_STATE; |
| 2196 | goto exit; |
| 2197 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2198 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2199 | /* Don't require hash implementations to behave correctly on a |
| 2200 | * zero-length input, which may have an invalid pointer. */ |
| 2201 | if( input_length == 0 ) |
| 2202 | return( PSA_SUCCESS ); |
| 2203 | |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2204 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2205 | |
| 2206 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2207 | if( status != PSA_SUCCESS ) |
| 2208 | psa_hash_abort( operation ); |
| 2209 | |
| 2210 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2214 | uint8_t *hash, |
| 2215 | size_t hash_size, |
| 2216 | size_t *hash_length ) |
| 2217 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2218 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2219 | if( operation->id == 0 ) |
| 2220 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2221 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2222 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2223 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2224 | psa_hash_abort( operation ); |
| 2225 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2226 | } |
| 2227 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2228 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2229 | const uint8_t *hash, |
| 2230 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2231 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2232 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2233 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2234 | psa_status_t status = psa_hash_finish( |
| 2235 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2236 | actual_hash, sizeof( actual_hash ), |
| 2237 | &actual_hash_length ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2238 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2239 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2240 | goto exit; |
| 2241 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2242 | if( actual_hash_length != hash_length ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2243 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2244 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2245 | goto exit; |
| 2246 | } |
| 2247 | |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2248 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2249 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2250 | |
| 2251 | exit: |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2252 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2253 | if( status != PSA_SUCCESS ) |
| 2254 | psa_hash_abort(operation); |
| 2255 | |
| 2256 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2259 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2260 | const uint8_t *input, size_t input_length, |
| 2261 | uint8_t *hash, size_t hash_size, |
| 2262 | size_t *hash_length ) |
| 2263 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2264 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2265 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2267 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2268 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2269 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2273 | const uint8_t *input, size_t input_length, |
| 2274 | const uint8_t *hash, size_t hash_length ) |
| 2275 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2276 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2277 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2278 | |
| 2279 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2281 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2282 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2283 | alg, input, input_length, |
| 2284 | actual_hash, sizeof(actual_hash), |
| 2285 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2286 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2287 | goto exit; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2288 | if( actual_hash_length != hash_length ) |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2289 | { |
| 2290 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2291 | goto exit; |
| 2292 | } |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2293 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2294 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2295 | |
| 2296 | exit: |
| 2297 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
| 2298 | return( status ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2301 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2302 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2303 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2304 | if( source_operation->id == 0 || |
| 2305 | target_operation->id != 0 ) |
| 2306 | { |
| 2307 | return( PSA_ERROR_BAD_STATE ); |
| 2308 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2309 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2310 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2311 | target_operation ); |
| 2312 | if( status != PSA_SUCCESS ) |
| 2313 | psa_hash_abort( target_operation ); |
| 2314 | |
| 2315 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2316 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2317 | |
| 2318 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2319 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2320 | /* MAC */ |
| 2321 | /****************************************************************/ |
| 2322 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2323 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2324 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2325 | /* Aborting a non-active operation is allowed */ |
| 2326 | if( operation->id == 0 ) |
| 2327 | return( PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2328 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2329 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2330 | operation->mac_size = 0; |
| 2331 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2332 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2333 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2334 | return( status ); |
| 2335 | } |
| 2336 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2337 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2338 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2339 | const psa_key_attributes_t *attributes, |
| 2340 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2341 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2342 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2343 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2344 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2345 | |
Ronald Cron | 48f875e | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2346 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2347 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2348 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2349 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2350 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2351 | if( status != PSA_SUCCESS ) |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2352 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2353 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2354 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2355 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2356 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2357 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2358 | { |
| 2359 | /* A very short MAC is too short for security since it can be |
| 2360 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2361 | * so we make this our minimum, even though 32 bits is still |
| 2362 | * too small for security. */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2363 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2364 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2365 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2366 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2367 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2368 | { |
| 2369 | /* It's impossible to "truncate" to a larger length than the full length |
| 2370 | * of the algorithm. */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2371 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2374 | if( *mac_size > PSA_MAC_MAX_SIZE ) |
| 2375 | { |
| 2376 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2377 | * that is disabled in the compile-time configuration. The result can |
| 2378 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2379 | * configuration into account. In this case, force a return of |
| 2380 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2381 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2382 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2383 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2384 | * systematically generated tests. */ |
| 2385 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2386 | } |
| 2387 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2388 | return( PSA_SUCCESS ); |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2392 | mbedtls_svc_key_id_t key, |
| 2393 | psa_algorithm_t alg, |
| 2394 | int is_sign ) |
| 2395 | { |
| 2396 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2397 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2398 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2399 | |
| 2400 | /* A context must be freshly initialized before it can be set up. */ |
| 2401 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2402 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2403 | status = PSA_ERROR_BAD_STATE; |
| 2404 | goto exit; |
| 2405 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2406 | |
| 2407 | status = psa_get_and_lock_key_slot_with_policy( |
| 2408 | key, |
| 2409 | &slot, |
Mateusz Starzyk | dd55b25 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2410 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2411 | alg ); |
| 2412 | if( status != PSA_SUCCESS ) |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2413 | goto exit; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2414 | |
| 2415 | psa_key_attributes_t attributes = { |
| 2416 | .core = slot->attr |
| 2417 | }; |
| 2418 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2419 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2420 | &operation->mac_size ); |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2421 | if( status != PSA_SUCCESS ) |
| 2422 | goto exit; |
| 2423 | |
| 2424 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2425 | /* Dispatch the MAC setup call with validated input */ |
| 2426 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2427 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2428 | status = psa_driver_wrapper_mac_sign_setup( operation, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2429 | &attributes, |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2430 | slot->key.data, |
| 2431 | slot->key.bytes, |
| 2432 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2433 | } |
| 2434 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2435 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2436 | status = psa_driver_wrapper_mac_verify_setup( operation, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2437 | &attributes, |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2438 | slot->key.data, |
| 2439 | slot->key.bytes, |
| 2440 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2441 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2442 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2443 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2444 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2445 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2446 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2447 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2448 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2449 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2452 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2453 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2454 | psa_algorithm_t alg ) |
| 2455 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2456 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2460 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2461 | psa_algorithm_t alg ) |
| 2462 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2463 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2464 | } |
| 2465 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2466 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2467 | const uint8_t *input, |
| 2468 | size_t input_length ) |
| 2469 | { |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2470 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2471 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2472 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2473 | /* Don't require hash implementations to behave correctly on a |
| 2474 | * zero-length input, which may have an invalid pointer. */ |
| 2475 | if( input_length == 0 ) |
| 2476 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2477 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2478 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2479 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2480 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2481 | psa_mac_abort( operation ); |
| 2482 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2483 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2486 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2487 | uint8_t *mac, |
| 2488 | size_t mac_size, |
| 2489 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2490 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2491 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2492 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2493 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2494 | if( operation->id == 0 ) |
Dave Rodgman | d73e1b0 | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2495 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2496 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2497 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2498 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2499 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2500 | if( ! operation->is_sign ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2501 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2502 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2503 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2504 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2505 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2506 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2507 | * once all the error checks are done. */ |
| 2508 | if( operation->mac_size == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2509 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2510 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2511 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2512 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2513 | |
| 2514 | if( mac_size < operation->mac_size ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2515 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2516 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2517 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2518 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2519 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2520 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2521 | mac, operation->mac_size, |
| 2522 | mac_length ); |
| 2523 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2524 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2525 | /* In case of success, set the potential excess room in the output buffer |
| 2526 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2527 | * In case of error, set the output length and content to a safe default, |
| 2528 | * such that in case the caller misses an error check, the output would be |
| 2529 | * an unachievable MAC. |
| 2530 | */ |
| 2531 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2532 | { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2533 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2534 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2535 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2536 | |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2537 | if( mac_size > operation->mac_size ) |
| 2538 | memset( &mac[operation->mac_size], '!', |
| 2539 | mac_size - operation->mac_size ); |
| 2540 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2541 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2542 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2543 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2546 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2547 | const uint8_t *mac, |
| 2548 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2549 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2550 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2551 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2552 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2553 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2554 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2555 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2556 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2557 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2558 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2559 | if( operation->is_sign ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2560 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2561 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2562 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2563 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2564 | |
| 2565 | if( operation->mac_size != mac_length ) |
| 2566 | { |
| 2567 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2568 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2569 | } |
| 2570 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2571 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2572 | mac, mac_length ); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2573 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2574 | exit: |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2575 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2576 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2577 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2578 | } |
| 2579 | |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2580 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2581 | psa_algorithm_t alg, |
| 2582 | const uint8_t *input, |
| 2583 | size_t input_length, |
| 2584 | uint8_t *mac, |
| 2585 | size_t mac_size, |
| 2586 | size_t *mac_length, |
| 2587 | int is_sign ) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2588 | { |
| 2589 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2590 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2591 | psa_key_slot_t *slot; |
| 2592 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2593 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2594 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | dd55b25 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2595 | key, |
| 2596 | &slot, |
| 2597 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2598 | alg ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2599 | if( status != PSA_SUCCESS ) |
| 2600 | goto exit; |
| 2601 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2602 | psa_key_attributes_t attributes = { |
| 2603 | .core = slot->attr |
| 2604 | }; |
| 2605 | |
| 2606 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2607 | &operation_mac_size ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2608 | if( status != PSA_SUCCESS ) |
| 2609 | goto exit; |
| 2610 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2611 | if( mac_size < operation_mac_size ) |
| 2612 | { |
| 2613 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2614 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | status = psa_driver_wrapper_mac_compute( |
| 2618 | &attributes, |
| 2619 | slot->key.data, slot->key.bytes, |
| 2620 | alg, |
| 2621 | input, input_length, |
| 2622 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2623 | |
| 2624 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2625 | /* In case of success, set the potential excess room in the output buffer |
| 2626 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2627 | * In case of error, set the output length and content to a safe default, |
| 2628 | * such that in case the caller misses an error check, the output would be |
| 2629 | * an unachievable MAC. |
| 2630 | */ |
| 2631 | if( status != PSA_SUCCESS ) |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2632 | { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2633 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2634 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2635 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2636 | if( mac_size > operation_mac_size ) |
| 2637 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2638 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2639 | unlock_status = psa_unlock_key_slot( slot ); |
| 2640 | |
| 2641 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2644 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2645 | psa_algorithm_t alg, |
| 2646 | const uint8_t *input, |
| 2647 | size_t input_length, |
| 2648 | uint8_t *mac, |
| 2649 | size_t mac_size, |
| 2650 | size_t *mac_length) |
| 2651 | { |
| 2652 | return( psa_mac_compute_internal( key, alg, |
| 2653 | input, input_length, |
| 2654 | mac, mac_size, mac_length, 1 ) ); |
| 2655 | } |
| 2656 | |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2657 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2658 | psa_algorithm_t alg, |
| 2659 | const uint8_t *input, |
| 2660 | size_t input_length, |
| 2661 | const uint8_t *mac, |
| 2662 | size_t mac_length) |
| 2663 | { |
| 2664 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2665 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2666 | size_t actual_mac_length; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2667 | |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2668 | status = psa_mac_compute_internal( key, alg, |
| 2669 | input, input_length, |
| 2670 | actual_mac, sizeof( actual_mac ), |
| 2671 | &actual_mac_length, 0 ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2672 | if( status != PSA_SUCCESS ) |
| 2673 | goto exit; |
| 2674 | |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2675 | if( mac_length != actual_mac_length ) |
| 2676 | { |
| 2677 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2678 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2679 | } |
| 2680 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2681 | { |
| 2682 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2683 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2684 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2685 | |
| 2686 | exit: |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2687 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2688 | |
| 2689 | return ( status ); |
| 2690 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2691 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2692 | /****************************************************************/ |
| 2693 | /* Asymmetric cryptography */ |
| 2694 | /****************************************************************/ |
| 2695 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2696 | 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] | 2697 | psa_algorithm_t alg ) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2698 | { |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2699 | if( input_is_message ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2700 | { |
| 2701 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2702 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2703 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2704 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2705 | { |
| 2706 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2707 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2708 | } |
| 2709 | } |
| 2710 | else |
| 2711 | { |
Mateusz Starzyk | d22362c | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2712 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2713 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2714 | } |
| 2715 | |
| 2716 | return( PSA_SUCCESS ); |
| 2717 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2718 | |
| 2719 | 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] | 2720 | int input_is_message, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2721 | psa_algorithm_t alg, |
| 2722 | const uint8_t * input, |
| 2723 | size_t input_length, |
| 2724 | uint8_t * signature, |
| 2725 | size_t signature_size, |
| 2726 | size_t * signature_length ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2727 | { |
| 2728 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2729 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2730 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2731 | |
| 2732 | *signature_length = 0; |
| 2733 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2734 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2735 | if( status != PSA_SUCCESS ) |
| 2736 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2737 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2738 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2739 | * 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] | 2740 | * buffer can in principle be empty since it doesn't actually have |
| 2741 | * to be a hash.) */ |
| 2742 | if( signature_size == 0 ) |
| 2743 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2744 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2745 | status = psa_get_and_lock_key_slot_with_policy( |
| 2746 | key, &slot, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2747 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2748 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2749 | alg ); |
| 2750 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2751 | if( status != PSA_SUCCESS ) |
| 2752 | goto exit; |
| 2753 | |
| 2754 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2755 | { |
| 2756 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2757 | goto exit; |
| 2758 | } |
| 2759 | |
| 2760 | psa_key_attributes_t attributes = { |
| 2761 | .core = slot->attr |
| 2762 | }; |
| 2763 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2764 | if( input_is_message ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2765 | { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2766 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2767 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2768 | alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2769 | signature, signature_size, signature_length ); |
| 2770 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2771 | else |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2772 | { |
| 2773 | |
| 2774 | status = psa_driver_wrapper_sign_hash( |
| 2775 | &attributes, slot->key.data, slot->key.bytes, |
| 2776 | alg, input, input_length, |
| 2777 | signature, signature_size, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2778 | } |
| 2779 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2780 | |
| 2781 | exit: |
| 2782 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2783 | * the trailing part on success) with something that isn't a valid signature |
| 2784 | * (barring an attack on the signature and deliberately-crafted input), |
| 2785 | * in case the caller doesn't check the return status properly. */ |
| 2786 | if( status == PSA_SUCCESS ) |
| 2787 | memset( signature + *signature_length, '!', |
| 2788 | signature_size - *signature_length ); |
| 2789 | else |
| 2790 | memset( signature, '!', signature_size ); |
| 2791 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2792 | * memset because signature may be NULL in this case. */ |
| 2793 | |
| 2794 | unlock_status = psa_unlock_key_slot( slot ); |
| 2795 | |
| 2796 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2797 | } |
| 2798 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2799 | 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] | 2800 | int input_is_message, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2801 | psa_algorithm_t alg, |
| 2802 | const uint8_t * input, |
| 2803 | size_t input_length, |
| 2804 | const uint8_t * signature, |
| 2805 | size_t signature_length ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2806 | { |
| 2807 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2808 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2809 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2810 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2811 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2812 | if( status != PSA_SUCCESS ) |
| 2813 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2814 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2815 | status = psa_get_and_lock_key_slot_with_policy( |
| 2816 | key, &slot, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2817 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2818 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2819 | alg ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2820 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2821 | if( status != PSA_SUCCESS ) |
| 2822 | return( status ); |
| 2823 | |
| 2824 | psa_key_attributes_t attributes = { |
| 2825 | .core = slot->attr |
| 2826 | }; |
| 2827 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2828 | if( input_is_message ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2829 | { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2830 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2831 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2832 | alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2833 | signature, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2834 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2835 | else |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2836 | { |
| 2837 | status = psa_driver_wrapper_verify_hash( |
| 2838 | &attributes, slot->key.data, slot->key.bytes, |
| 2839 | alg, input, input_length, |
| 2840 | signature, signature_length ); |
| 2841 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2842 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2843 | unlock_status = psa_unlock_key_slot( slot ); |
| 2844 | |
| 2845 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2846 | |
| 2847 | } |
| 2848 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2849 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2850 | const psa_key_attributes_t *attributes, |
| 2851 | const uint8_t *key_buffer, |
| 2852 | size_t key_buffer_size, |
| 2853 | psa_algorithm_t alg, |
| 2854 | const uint8_t *input, |
| 2855 | size_t input_length, |
| 2856 | uint8_t *signature, |
| 2857 | size_t signature_size, |
| 2858 | size_t *signature_length ) |
| 2859 | { |
| 2860 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2861 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2862 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2863 | { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2864 | size_t hash_length; |
| 2865 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2866 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2867 | status = psa_driver_wrapper_hash_compute( |
| 2868 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2869 | input, input_length, |
| 2870 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2871 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2872 | if( status != PSA_SUCCESS ) |
| 2873 | return status; |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2874 | |
| 2875 | return psa_driver_wrapper_sign_hash( |
| 2876 | attributes, key_buffer, key_buffer_size, |
| 2877 | alg, hash, hash_length, |
| 2878 | signature, signature_size, signature_length ); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2879 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2880 | |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2881 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2884 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2885 | psa_algorithm_t alg, |
| 2886 | const uint8_t * input, |
| 2887 | size_t input_length, |
| 2888 | uint8_t * signature, |
| 2889 | size_t signature_size, |
| 2890 | size_t * signature_length ) |
| 2891 | { |
| 2892 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2893 | key, 1, alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2894 | signature, signature_size, signature_length ); |
| 2895 | } |
| 2896 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2897 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2898 | const psa_key_attributes_t *attributes, |
| 2899 | const uint8_t *key_buffer, |
| 2900 | size_t key_buffer_size, |
| 2901 | psa_algorithm_t alg, |
| 2902 | const uint8_t *input, |
| 2903 | size_t input_length, |
| 2904 | const uint8_t *signature, |
| 2905 | size_t signature_length ) |
| 2906 | { |
| 2907 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2908 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2909 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2910 | { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2911 | size_t hash_length; |
| 2912 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2913 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2914 | status = psa_driver_wrapper_hash_compute( |
| 2915 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2916 | input, input_length, |
| 2917 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2918 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2919 | if( status != PSA_SUCCESS ) |
| 2920 | return status; |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2921 | |
| 2922 | return psa_driver_wrapper_verify_hash( |
| 2923 | attributes, key_buffer, key_buffer_size, |
| 2924 | alg, hash, hash_length, |
| 2925 | signature, signature_length ); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2926 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2927 | |
gabor-mezei-arm | ef6f2aa | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2928 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2929 | } |
| 2930 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2931 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2932 | psa_algorithm_t alg, |
| 2933 | const uint8_t * input, |
| 2934 | size_t input_length, |
| 2935 | const uint8_t * signature, |
| 2936 | size_t signature_length ) |
| 2937 | { |
| 2938 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2939 | key, 1, alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2940 | signature, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2941 | } |
| 2942 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2943 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2944 | const psa_key_attributes_t *attributes, |
| 2945 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2946 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2947 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2948 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2949 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2950 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2951 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2952 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2953 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2954 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2955 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2956 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2957 | attributes, |
| 2958 | key_buffer, key_buffer_size, |
| 2959 | alg, hash, hash_length, |
| 2960 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2961 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2962 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2963 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2964 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2965 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2966 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2967 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2968 | } |
Ronald Cron | 4bcccc6 | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2969 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2970 | { |
| 2971 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2972 | { |
| 2973 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2974 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2975 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2976 | attributes, |
| 2977 | key_buffer, key_buffer_size, |
| 2978 | alg, hash, hash_length, |
| 2979 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2980 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2981 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2982 | } |
| 2983 | else |
| 2984 | { |
| 2985 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2986 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2987 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2988 | |
| 2989 | (void)key_buffer; |
| 2990 | (void)key_buffer_size; |
| 2991 | (void)hash; |
| 2992 | (void)hash_length; |
| 2993 | (void)signature; |
| 2994 | (void)signature_size; |
| 2995 | (void)signature_length; |
| 2996 | |
| 2997 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2998 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2999 | |
| 3000 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 3001 | psa_algorithm_t alg, |
| 3002 | const uint8_t *hash, |
| 3003 | size_t hash_length, |
| 3004 | uint8_t *signature, |
| 3005 | size_t signature_size, |
| 3006 | size_t *signature_length ) |
| 3007 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3008 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3009 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3010 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3011 | } |
| 3012 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3013 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3014 | const psa_key_attributes_t *attributes, |
| 3015 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3016 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3017 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3018 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 3019 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3020 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3021 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 3022 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3023 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3024 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3025 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3026 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 3027 | attributes, |
| 3028 | key_buffer, key_buffer_size, |
| 3029 | alg, hash, hash_length, |
| 3030 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3031 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3032 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3033 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3034 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3035 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3036 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3037 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3038 | } |
Ronald Cron | 4bcccc6 | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 3039 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3040 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3041 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 3042 | { |
| 3043 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3044 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3045 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3046 | attributes, |
| 3047 | key_buffer, key_buffer_size, |
| 3048 | alg, hash, hash_length, |
| 3049 | signature, signature_length ) ); |
| 3050 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3051 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3052 | } |
| 3053 | else |
| 3054 | { |
| 3055 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3056 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3057 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3058 | |
| 3059 | (void)key_buffer; |
| 3060 | (void)key_buffer_size; |
| 3061 | (void)hash; |
| 3062 | (void)hash_length; |
| 3063 | (void)signature; |
| 3064 | (void)signature_length; |
| 3065 | |
| 3066 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3067 | } |
| 3068 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3069 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3070 | psa_algorithm_t alg, |
| 3071 | const uint8_t *hash, |
| 3072 | size_t hash_length, |
| 3073 | const uint8_t *signature, |
| 3074 | size_t signature_length ) |
| 3075 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3076 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3077 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3078 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3079 | } |
| 3080 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3081 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3082 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3083 | mbedtls_rsa_context *rsa ) |
| 3084 | { |
| 3085 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3086 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3087 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3088 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3089 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3090 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3091 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3092 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3093 | psa_algorithm_t alg, |
| 3094 | const uint8_t *input, |
| 3095 | size_t input_length, |
| 3096 | const uint8_t *salt, |
| 3097 | size_t salt_length, |
| 3098 | uint8_t *output, |
| 3099 | size_t output_size, |
| 3100 | size_t *output_length ) |
| 3101 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3102 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3103 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3104 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3105 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3106 | (void) input; |
| 3107 | (void) input_length; |
| 3108 | (void) salt; |
| 3109 | (void) output; |
| 3110 | (void) output_size; |
| 3111 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3112 | *output_length = 0; |
| 3113 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3114 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3115 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3116 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3117 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3118 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3119 | if( status != PSA_SUCCESS ) |
| 3120 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3121 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3122 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3123 | { |
| 3124 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3125 | goto exit; |
| 3126 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3127 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3128 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3129 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3130 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3131 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3132 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3133 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3134 | slot->key.data, |
| 3135 | slot->key.bytes, |
| 3136 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3137 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3138 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3139 | |
| 3140 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3141 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3142 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3143 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3144 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3145 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3146 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3147 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
| 3148 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3149 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3150 | status = mbedtls_to_psa_error( |
| 3151 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3152 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3153 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3154 | MBEDTLS_RSA_PUBLIC, |
| 3155 | input_length, |
| 3156 | input, |
| 3157 | output ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3158 | #else |
| 3159 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3160 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3161 | } |
| 3162 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3163 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
| 3164 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3165 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3166 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3167 | status = mbedtls_to_psa_error( |
| 3168 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3169 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3170 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3171 | MBEDTLS_RSA_PUBLIC, |
| 3172 | salt, salt_length, |
| 3173 | input_length, |
| 3174 | input, |
| 3175 | output ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3176 | #else |
| 3177 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3178 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3179 | } |
| 3180 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3181 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3182 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3183 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3184 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3185 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3186 | rsa_exit: |
| 3187 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3188 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3189 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3190 | mbedtls_rsa_free( rsa ); |
| 3191 | mbedtls_free( rsa ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3192 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3193 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3194 | } |
| 3195 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3196 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3197 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3198 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3199 | |
| 3200 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3201 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3202 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3203 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3204 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3205 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3206 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3207 | psa_algorithm_t alg, |
| 3208 | const uint8_t *input, |
| 3209 | size_t input_length, |
| 3210 | const uint8_t *salt, |
| 3211 | size_t salt_length, |
| 3212 | uint8_t *output, |
| 3213 | size_t output_size, |
| 3214 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3215 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3216 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3217 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3218 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3219 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3220 | (void) input; |
| 3221 | (void) input_length; |
| 3222 | (void) salt; |
| 3223 | (void) output; |
| 3224 | (void) output_size; |
| 3225 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3226 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3227 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3228 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3229 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3230 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3231 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3232 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3233 | if( status != PSA_SUCCESS ) |
| 3234 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3235 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3236 | { |
| 3237 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3238 | goto exit; |
| 3239 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3240 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3241 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3242 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3243 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3244 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3245 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3246 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3247 | slot->key.data, |
| 3248 | slot->key.bytes, |
| 3249 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3250 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3251 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3252 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3253 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3254 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3255 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3256 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3257 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3258 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3259 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3260 | |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3261 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3262 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3263 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3264 | status = mbedtls_to_psa_error( |
| 3265 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3266 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3267 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3268 | MBEDTLS_RSA_PRIVATE, |
| 3269 | output_length, |
| 3270 | input, |
| 3271 | output, |
| 3272 | output_size ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3273 | #else |
| 3274 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3275 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3276 | } |
| 3277 | else |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3278 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3279 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3280 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3281 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3282 | status = mbedtls_to_psa_error( |
| 3283 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3284 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3285 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3286 | MBEDTLS_RSA_PRIVATE, |
| 3287 | salt, salt_length, |
| 3288 | output_length, |
| 3289 | input, |
| 3290 | output, |
| 3291 | output_size ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3292 | #else |
| 3293 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3294 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3295 | } |
| 3296 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3297 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3298 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3299 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3300 | |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3301 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3302 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3303 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3304 | mbedtls_rsa_free( rsa ); |
| 3305 | mbedtls_free( rsa ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3306 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3307 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3308 | } |
| 3309 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3310 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3311 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3312 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3313 | |
| 3314 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3315 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3316 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3317 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3318 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3319 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3320 | |
| 3321 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3322 | /****************************************************************/ |
| 3323 | /* Symmetric cryptography */ |
| 3324 | /****************************************************************/ |
| 3325 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3326 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3327 | mbedtls_svc_key_id_t key, |
| 3328 | psa_algorithm_t alg, |
| 3329 | mbedtls_operation_t cipher_operation ) |
| 3330 | { |
| 3331 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3332 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3333 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3334 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3335 | PSA_KEY_USAGE_ENCRYPT : |
| 3336 | PSA_KEY_USAGE_DECRYPT ); |
| 3337 | |
| 3338 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3339 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3340 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3341 | status = PSA_ERROR_BAD_STATE; |
| 3342 | goto exit; |
| 3343 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3344 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3345 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3346 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3347 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3348 | goto exit; |
| 3349 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3350 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3351 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3352 | if( status != PSA_SUCCESS ) |
| 3353 | goto exit; |
| 3354 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3355 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3356 | * 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] | 3357 | * so we only set it (in the driver wrapper) after resources have been |
| 3358 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3359 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3360 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3361 | operation->iv_required = 0; |
Gilles Peskine | 4a83c10 | 2022-04-20 17:00:22 +0200 | [diff] [blame] | 3362 | else if( slot->attr.type == PSA_KEY_TYPE_ARC4 ) |
| 3363 | operation->iv_required = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3364 | else |
| 3365 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3366 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3367 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3368 | psa_key_attributes_t attributes = { |
| 3369 | .core = slot->attr |
| 3370 | }; |
| 3371 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3372 | /* Try doing the operation through a driver before using software fallback. */ |
| 3373 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3374 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3375 | &attributes, |
| 3376 | slot->key.data, |
| 3377 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3378 | alg ); |
| 3379 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3380 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3381 | &attributes, |
| 3382 | slot->key.data, |
| 3383 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3384 | alg ); |
| 3385 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3386 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3387 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3388 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3389 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3390 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3391 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3392 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3393 | } |
| 3394 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3395 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3396 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3397 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3398 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3399 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3400 | } |
| 3401 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3402 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3403 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3404 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3405 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3406 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3409 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3410 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3411 | size_t iv_size, |
| 3412 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3413 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3415 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3416 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3417 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3418 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3419 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3420 | status = PSA_ERROR_BAD_STATE; |
| 3421 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3424 | if( operation->iv_set || ! operation->iv_required ) |
| 3425 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3426 | status = PSA_ERROR_BAD_STATE; |
| 3427 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3428 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3429 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3430 | default_iv_length = operation->default_iv_length; |
| 3431 | if( iv_size < default_iv_length ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3432 | { |
| 3433 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3434 | goto exit; |
| 3435 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3436 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3437 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3438 | { |
| 3439 | status = PSA_ERROR_GENERIC_ERROR; |
| 3440 | goto exit; |
| 3441 | } |
| 3442 | |
| 3443 | status = psa_generate_random( local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3444 | if( status != PSA_SUCCESS ) |
| 3445 | goto exit; |
| 3446 | |
| 3447 | status = psa_driver_wrapper_cipher_set_iv( operation, |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3448 | local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3449 | |
| 3450 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3451 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3452 | { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3453 | memcpy( iv, local_iv, default_iv_length ); |
| 3454 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3455 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3456 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3457 | else |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3458 | { |
| 3459 | *iv_length = 0; |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3460 | psa_cipher_abort( operation ); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3461 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3462 | |
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 | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3466 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3467 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3468 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3469 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3470 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3471 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3472 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3473 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3474 | status = PSA_ERROR_BAD_STATE; |
| 3475 | goto exit; |
| 3476 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3477 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3478 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3479 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3480 | status = PSA_ERROR_BAD_STATE; |
| 3481 | goto exit; |
| 3482 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3483 | |
| 3484 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3485 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3486 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3487 | goto exit; |
| 3488 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3489 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3490 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3491 | iv, |
| 3492 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3493 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3494 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3495 | if( status == PSA_SUCCESS ) |
| 3496 | operation->iv_set = 1; |
| 3497 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3498 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3499 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3500 | } |
| 3501 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3502 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3503 | const uint8_t *input, |
| 3504 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3505 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3506 | size_t output_size, |
| 3507 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3508 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3509 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3510 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3511 | if( operation->id == 0 ) |
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 | status = PSA_ERROR_BAD_STATE; |
| 3514 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3515 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3516 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3517 | if( operation->iv_required && ! operation->iv_set ) |
| 3518 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3519 | status = PSA_ERROR_BAD_STATE; |
| 3520 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3521 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3522 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3523 | status = psa_driver_wrapper_cipher_update( operation, |
| 3524 | input, |
| 3525 | input_length, |
| 3526 | output, |
| 3527 | output_size, |
| 3528 | output_length ); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3529 | |
| 3530 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3531 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3532 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3533 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3534 | return( status ); |
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_finish( psa_cipher_operation_t *operation, |
| 3538 | uint8_t *output, |
| 3539 | size_t output_size, |
| 3540 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3541 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3542 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3543 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3544 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3545 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3546 | status = PSA_ERROR_BAD_STATE; |
| 3547 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3548 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3549 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3550 | if( operation->iv_required && ! operation->iv_set ) |
| 3551 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3552 | status = PSA_ERROR_BAD_STATE; |
| 3553 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3554 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3555 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3556 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3557 | output, |
| 3558 | output_size, |
| 3559 | output_length ); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3560 | |
| 3561 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3562 | if( status == PSA_SUCCESS ) |
| 3563 | return( psa_cipher_abort( operation ) ); |
| 3564 | else |
| 3565 | { |
| 3566 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3567 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3568 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3569 | return( status ); |
| 3570 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3571 | } |
| 3572 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3573 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3574 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3575 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3576 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3577 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3578 | * in use. It's ok to call abort on such an object, and there's |
| 3579 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3580 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3581 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3582 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3583 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3584 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3585 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3586 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3587 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3588 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3589 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3590 | } |
| 3591 | |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3592 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3593 | psa_algorithm_t alg, |
| 3594 | const uint8_t *input, |
| 3595 | size_t input_length, |
| 3596 | uint8_t *output, |
| 3597 | size_t output_size, |
| 3598 | size_t *output_length ) |
| 3599 | { |
| 3600 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3601 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3602 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3603 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3604 | size_t default_iv_length = 0; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3605 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3606 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3607 | { |
| 3608 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3609 | goto exit; |
| 3610 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3611 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3612 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3613 | PSA_KEY_USAGE_ENCRYPT, |
| 3614 | alg ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3615 | if( status != PSA_SUCCESS ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3616 | goto exit; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3617 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3618 | psa_key_attributes_t attributes = { |
| 3619 | .core = slot->attr |
| 3620 | }; |
| 3621 | |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3622 | default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
| 3623 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3624 | { |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3625 | status = PSA_ERROR_GENERIC_ERROR; |
| 3626 | goto exit; |
| 3627 | } |
| 3628 | |
| 3629 | if( default_iv_length > 0 ) |
| 3630 | { |
| 3631 | if( output_size < default_iv_length ) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3632 | { |
| 3633 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3634 | goto exit; |
| 3635 | } |
| 3636 | |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3637 | status = psa_generate_random( local_iv, default_iv_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3638 | if( status != PSA_SUCCESS ) |
| 3639 | goto exit; |
| 3640 | } |
| 3641 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3642 | status = psa_driver_wrapper_cipher_encrypt( |
| 3643 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3644 | alg, local_iv, default_iv_length, input, input_length, |
| 3645 | output + default_iv_length, output_size - default_iv_length, |
| 3646 | output_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3647 | |
| 3648 | exit: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3649 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3650 | if( status == PSA_SUCCESS ) |
| 3651 | status = unlock_status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3652 | |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3653 | if( status == PSA_SUCCESS ) |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3654 | { |
| 3655 | if( default_iv_length > 0 ) |
| 3656 | memcpy( output, local_iv, default_iv_length ); |
| 3657 | *output_length += default_iv_length; |
| 3658 | } |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3659 | else |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3660 | *output_length = 0; |
| 3661 | |
| 3662 | return( status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3666 | psa_algorithm_t alg, |
| 3667 | const uint8_t *input, |
| 3668 | size_t input_length, |
| 3669 | uint8_t *output, |
| 3670 | size_t output_size, |
| 3671 | size_t *output_length ) |
| 3672 | { |
| 3673 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3674 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3675 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3676 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3677 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3678 | { |
| 3679 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3680 | goto exit; |
| 3681 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3682 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3683 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3684 | PSA_KEY_USAGE_DECRYPT, |
| 3685 | alg ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3686 | if( status != PSA_SUCCESS ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3687 | goto exit; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3688 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3689 | psa_key_attributes_t attributes = { |
| 3690 | .core = slot->attr |
| 3691 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3692 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3693 | if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) ) |
| 3694 | { |
| 3695 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3696 | goto exit; |
| 3697 | } |
| 3698 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3699 | status = psa_driver_wrapper_cipher_decrypt( |
| 3700 | &attributes, slot->key.data, slot->key.bytes, |
| 3701 | alg, input, input_length, |
| 3702 | output, output_size, output_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3703 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3704 | exit: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3705 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3706 | if( status == PSA_SUCCESS ) |
| 3707 | status = unlock_status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3708 | |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3709 | if( status != PSA_SUCCESS ) |
| 3710 | *output_length = 0; |
| 3711 | |
| 3712 | return( status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3716 | /****************************************************************/ |
| 3717 | /* AEAD */ |
| 3718 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3719 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3720 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3721 | psa_algorithm_t alg, |
| 3722 | const uint8_t *nonce, |
| 3723 | size_t nonce_length, |
| 3724 | const uint8_t *additional_data, |
| 3725 | size_t additional_data_length, |
| 3726 | const uint8_t *plaintext, |
| 3727 | size_t plaintext_length, |
| 3728 | uint8_t *ciphertext, |
| 3729 | size_t ciphertext_size, |
| 3730 | size_t *ciphertext_length ) |
| 3731 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3732 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3733 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3734 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3735 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3736 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3737 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3738 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3739 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3740 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3741 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3742 | if( status != PSA_SUCCESS ) |
| 3743 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3744 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3745 | psa_key_attributes_t attributes = { |
| 3746 | .core = slot->attr |
| 3747 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3748 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3749 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3750 | &attributes, slot->key.data, slot->key.bytes, |
| 3751 | alg, |
| 3752 | nonce, nonce_length, |
| 3753 | additional_data, additional_data_length, |
| 3754 | plaintext, plaintext_length, |
| 3755 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3756 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3757 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3758 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3759 | |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3760 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3761 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3762 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3763 | } |
| 3764 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3765 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3766 | psa_algorithm_t alg, |
| 3767 | const uint8_t *nonce, |
| 3768 | size_t nonce_length, |
| 3769 | const uint8_t *additional_data, |
| 3770 | size_t additional_data_length, |
| 3771 | const uint8_t *ciphertext, |
| 3772 | size_t ciphertext_length, |
| 3773 | uint8_t *plaintext, |
| 3774 | size_t plaintext_size, |
| 3775 | size_t *plaintext_length ) |
| 3776 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3777 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3778 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3779 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3780 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3781 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3782 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3783 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3784 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3785 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3786 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3787 | if( status != PSA_SUCCESS ) |
| 3788 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3789 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3790 | psa_key_attributes_t attributes = { |
| 3791 | .core = slot->attr |
| 3792 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3793 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3794 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3795 | &attributes, slot->key.data, slot->key.bytes, |
| 3796 | alg, |
| 3797 | nonce, nonce_length, |
| 3798 | additional_data, additional_data_length, |
| 3799 | ciphertext, ciphertext_length, |
| 3800 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3801 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3802 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3803 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3804 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3805 | psa_unlock_key_slot( slot ); |
| 3806 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3807 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3808 | } |
| 3809 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3810 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3811 | /* Generators */ |
| 3812 | /****************************************************************/ |
| 3813 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3814 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3815 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3816 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3817 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3818 | #endif /* At least one builtin KDF */ |
| 3819 | |
| 3820 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3821 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3822 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3823 | static psa_status_t psa_key_derivation_start_hmac( |
| 3824 | psa_mac_operation_t *operation, |
| 3825 | psa_algorithm_t hash_alg, |
| 3826 | const uint8_t *hmac_key, |
| 3827 | size_t hmac_key_length ) |
| 3828 | { |
| 3829 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3830 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3831 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 3832 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 3833 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 3834 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3835 | operation->is_sign = 1; |
| 3836 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 3837 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3838 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 3839 | &attributes, |
| 3840 | hmac_key, hmac_key_length, |
| 3841 | PSA_ALG_HMAC( hash_alg ) ); |
| 3842 | |
| 3843 | psa_reset_key_attributes( &attributes ); |
| 3844 | return( status ); |
| 3845 | } |
| 3846 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3847 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3848 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 3849 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 3850 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 3851 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 3852 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3853 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3854 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3855 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3856 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 3857 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3858 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3859 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3860 | } |
| 3861 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3862 | 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] | 3863 | { |
| 3864 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3865 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3866 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3867 | { |
| 3868 | /* The object has (apparently) been initialized but it is not |
| 3869 | * in use. It's ok to call abort on such an object, and there's |
| 3870 | * nothing to do. */ |
| 3871 | } |
| 3872 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3873 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3874 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3875 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3876 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3877 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3878 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3879 | else |
| 3880 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3881 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3882 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3883 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3884 | /* 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] | 3885 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3886 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3887 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 3888 | { |
| 3889 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 3890 | operation->ctx.tls12_prf.secret_length ); |
| 3891 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 3892 | } |
| 3893 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3894 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 3895 | { |
| 3896 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 3897 | operation->ctx.tls12_prf.seed_length ); |
| 3898 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 3899 | } |
| 3900 | |
| 3901 | if( operation->ctx.tls12_prf.label != NULL ) |
| 3902 | { |
| 3903 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 3904 | operation->ctx.tls12_prf.label_length ); |
| 3905 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 3906 | } |
| 3907 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3908 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3909 | |
| 3910 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3911 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3912 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3913 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3914 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 3915 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3916 | { |
| 3917 | status = PSA_ERROR_BAD_STATE; |
| 3918 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 3919 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3920 | return( status ); |
| 3921 | } |
| 3922 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3923 | 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] | 3924 | size_t *capacity) |
| 3925 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3926 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3927 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3928 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3929 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3932 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3933 | return( PSA_SUCCESS ); |
| 3934 | } |
| 3935 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3936 | 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] | 3937 | size_t capacity ) |
| 3938 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3939 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3940 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3941 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3942 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3943 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3944 | return( PSA_SUCCESS ); |
| 3945 | } |
| 3946 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3947 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3948 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3949 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3950 | 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] | 3951 | psa_algorithm_t hash_alg, |
| 3952 | uint8_t *output, |
| 3953 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3954 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3955 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3956 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3957 | psa_status_t status; |
| 3958 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3959 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 3960 | return( PSA_ERROR_BAD_STATE ); |
| 3961 | hkdf->state = HKDF_STATE_OUTPUT; |
| 3962 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3963 | while( output_length != 0 ) |
| 3964 | { |
| 3965 | /* Copy what remains of the current block */ |
| 3966 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 3967 | if( n > output_length ) |
| 3968 | n = (uint8_t) output_length; |
| 3969 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 3970 | output += n; |
| 3971 | output_length -= n; |
| 3972 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3973 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3974 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3975 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 3976 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3977 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3978 | * object was corrupted or if this function is called directly |
| 3979 | * inside the library. */ |
| 3980 | if( hkdf->block_number == 0xff ) |
| 3981 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3982 | |
| 3983 | /* We need a new block */ |
| 3984 | ++hkdf->block_number; |
| 3985 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3986 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3987 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 3988 | hash_alg, |
| 3989 | hkdf->prk, |
| 3990 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3991 | if( status != PSA_SUCCESS ) |
| 3992 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3993 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3994 | if( hkdf->block_number != 1 ) |
| 3995 | { |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3996 | status = psa_mac_update( &hkdf->hmac, |
| 3997 | hkdf->output_block, |
| 3998 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3999 | if( status != PSA_SUCCESS ) |
| 4000 | return( status ); |
| 4001 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4002 | status = psa_mac_update( &hkdf->hmac, |
| 4003 | hkdf->info, |
| 4004 | hkdf->info_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4005 | if( status != PSA_SUCCESS ) |
| 4006 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4007 | status = psa_mac_update( &hkdf->hmac, |
| 4008 | &hkdf->block_number, 1 ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4009 | if( status != PSA_SUCCESS ) |
| 4010 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4011 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4012 | hkdf->output_block, |
| 4013 | sizeof( hkdf->output_block ), |
| 4014 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4015 | if( status != PSA_SUCCESS ) |
| 4016 | return( status ); |
| 4017 | } |
| 4018 | |
| 4019 | return( PSA_SUCCESS ); |
| 4020 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4021 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4022 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4023 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4024 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4025 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4026 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4027 | psa_algorithm_t alg ) |
| 4028 | { |
| 4029 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4030 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4031 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4032 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4033 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4034 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4035 | /* We can't be wanting more output after block 0xff, otherwise |
| 4036 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4037 | * prevented this call. It could happen only if the operation |
| 4038 | * object was corrupted or if this function is called directly |
| 4039 | * inside the library. */ |
| 4040 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4041 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4042 | |
| 4043 | /* We need a new block */ |
| 4044 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4045 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4046 | |
| 4047 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4048 | * |
| 4049 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4050 | * |
| 4051 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4052 | * HMAC_hash(secret, A(2) + seed) + |
| 4053 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4054 | * |
| 4055 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4056 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4057 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4058 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4059 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4060 | * is currently extracted as `output_block` and where i is |
| 4061 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4062 | */ |
| 4063 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4064 | status = psa_key_derivation_start_hmac( &hmac, |
| 4065 | hash_alg, |
| 4066 | tls12_prf->secret, |
| 4067 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4068 | if( status != PSA_SUCCESS ) |
| 4069 | goto cleanup; |
| 4070 | |
| 4071 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4072 | if( tls12_prf->block_number == 1 ) |
| 4073 | { |
| 4074 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4075 | * the variable seed and in this instance means it in the context of the |
| 4076 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4077 | status = psa_mac_update( &hmac, |
| 4078 | tls12_prf->label, |
| 4079 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4080 | if( status != PSA_SUCCESS ) |
| 4081 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4082 | status = psa_mac_update( &hmac, |
| 4083 | tls12_prf->seed, |
| 4084 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4085 | if( status != PSA_SUCCESS ) |
| 4086 | goto cleanup; |
| 4087 | } |
| 4088 | else |
| 4089 | { |
| 4090 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4091 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4092 | if( status != PSA_SUCCESS ) |
| 4093 | goto cleanup; |
| 4094 | } |
| 4095 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4096 | status = psa_mac_sign_finish( &hmac, |
| 4097 | tls12_prf->Ai, hash_length, |
| 4098 | &hmac_output_length ); |
| 4099 | if( hmac_output_length != hash_length ) |
| 4100 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4101 | if( status != PSA_SUCCESS ) |
| 4102 | goto cleanup; |
| 4103 | |
| 4104 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4105 | status = psa_key_derivation_start_hmac( &hmac, |
| 4106 | hash_alg, |
| 4107 | tls12_prf->secret, |
| 4108 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4109 | if( status != PSA_SUCCESS ) |
| 4110 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4111 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4112 | if( status != PSA_SUCCESS ) |
| 4113 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4114 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4115 | if( status != PSA_SUCCESS ) |
| 4116 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4117 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4118 | if( status != PSA_SUCCESS ) |
| 4119 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4120 | status = psa_mac_sign_finish( &hmac, |
| 4121 | tls12_prf->output_block, hash_length, |
| 4122 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4123 | if( status != PSA_SUCCESS ) |
| 4124 | goto cleanup; |
| 4125 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4126 | |
| 4127 | cleanup: |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4128 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4129 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4130 | status = cleanup_status; |
| 4131 | |
| 4132 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4133 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4134 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4135 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4136 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4137 | psa_algorithm_t alg, |
| 4138 | uint8_t *output, |
| 4139 | size_t output_length ) |
| 4140 | { |
| 4141 | 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] | 4142 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4143 | psa_status_t status; |
| 4144 | uint8_t offset, length; |
| 4145 | |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4146 | switch( tls12_prf->state ) |
| 4147 | { |
| 4148 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4149 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4150 | break; |
| 4151 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4152 | break; |
| 4153 | default: |
| 4154 | return( PSA_ERROR_BAD_STATE ); |
| 4155 | } |
| 4156 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4157 | while( output_length != 0 ) |
| 4158 | { |
| 4159 | /* Check if we have fully processed the current block. */ |
| 4160 | if( tls12_prf->left_in_block == 0 ) |
| 4161 | { |
| 4162 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4163 | alg ); |
| 4164 | if( status != PSA_SUCCESS ) |
| 4165 | return( status ); |
| 4166 | |
| 4167 | continue; |
| 4168 | } |
| 4169 | |
| 4170 | if( tls12_prf->left_in_block > output_length ) |
| 4171 | length = (uint8_t) output_length; |
| 4172 | else |
| 4173 | length = tls12_prf->left_in_block; |
| 4174 | |
| 4175 | offset = hash_length - tls12_prf->left_in_block; |
| 4176 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4177 | output += length; |
| 4178 | output_length -= length; |
| 4179 | tls12_prf->left_in_block -= length; |
| 4180 | } |
| 4181 | |
| 4182 | return( PSA_SUCCESS ); |
| 4183 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4184 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4185 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4186 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4187 | psa_status_t psa_key_derivation_output_bytes( |
| 4188 | psa_key_derivation_operation_t *operation, |
| 4189 | uint8_t *output, |
| 4190 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4191 | { |
| 4192 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4193 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4194 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4195 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4196 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4197 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4198 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4201 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4202 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4203 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4204 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4205 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4206 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4207 | goto exit; |
| 4208 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4209 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4210 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4211 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4212 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4213 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4214 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4215 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4216 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4217 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4218 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4219 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4220 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4221 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4222 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4223 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4224 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4225 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4226 | output, output_length ); |
| 4227 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4228 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4229 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4230 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4231 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4232 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4233 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4234 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4235 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4236 | kdf_alg, output, |
| 4237 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4238 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4239 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4240 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4241 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4242 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4243 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4244 | return( PSA_ERROR_BAD_STATE ); |
| 4245 | } |
| 4246 | |
| 4247 | exit: |
| 4248 | if( status != PSA_SUCCESS ) |
| 4249 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4250 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4251 | * This allows us to differentiate between exhausted operations and |
| 4252 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4253 | * operations. */ |
| 4254 | psa_algorithm_t alg = operation->alg; |
| 4255 | psa_key_derivation_abort( operation ); |
| 4256 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4257 | memset( output, '!', output_length ); |
| 4258 | } |
| 4259 | return( status ); |
| 4260 | } |
| 4261 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4262 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4263 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4264 | { |
| 4265 | if( data_size >= 8 ) |
| 4266 | mbedtls_des_key_set_parity( data ); |
| 4267 | if( data_size >= 16 ) |
| 4268 | mbedtls_des_key_set_parity( data + 8 ); |
| 4269 | if( data_size >= 24 ) |
| 4270 | mbedtls_des_key_set_parity( data + 16 ); |
| 4271 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4272 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4273 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4274 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4275 | psa_key_slot_t *slot, |
| 4276 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4277 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4278 | { |
| 4279 | uint8_t *data = NULL; |
| 4280 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4281 | psa_status_t status; |
| 4282 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4283 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4284 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4285 | if( bits % 8 != 0 ) |
| 4286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4287 | data = mbedtls_calloc( 1, bytes ); |
| 4288 | if( data == NULL ) |
| 4289 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4290 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4291 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4292 | if( status != PSA_SUCCESS ) |
| 4293 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4294 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4295 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4296 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4297 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4298 | |
| 4299 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 4300 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4301 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4302 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4303 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4304 | psa_key_attributes_t attributes = { |
| 4305 | .core = slot->attr |
| 4306 | }; |
| 4307 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4308 | status = psa_driver_wrapper_import_key( &attributes, |
| 4309 | data, bytes, |
| 4310 | slot->key.data, |
| 4311 | slot->key.bytes, |
| 4312 | &slot->key.bytes, &bits ); |
| 4313 | if( bits != slot->attr.bits ) |
| 4314 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4315 | |
| 4316 | exit: |
| 4317 | mbedtls_free( data ); |
| 4318 | return( status ); |
| 4319 | } |
| 4320 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4321 | 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] | 4322 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4323 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4324 | { |
| 4325 | psa_status_t status; |
| 4326 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4327 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4328 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4329 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4330 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4331 | /* Reject any attempt to create a zero-length key so that we don't |
| 4332 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4333 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4335 | |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4336 | if( operation->alg == PSA_ALG_NONE ) |
| 4337 | return( PSA_ERROR_BAD_STATE ); |
| 4338 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4339 | if( ! operation->can_output_key ) |
| 4340 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4341 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4342 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 4343 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4344 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4345 | if( driver != NULL ) |
| 4346 | { |
| 4347 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4348 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4349 | } |
| 4350 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4351 | if( status == PSA_SUCCESS ) |
| 4352 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4353 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4354 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4355 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4356 | } |
| 4357 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4358 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4359 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4360 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4361 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4362 | return( status ); |
| 4363 | } |
| 4364 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4365 | |
| 4366 | |
| 4367 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4368 | /* Key derivation */ |
| 4369 | /****************************************************************/ |
| 4370 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4371 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4372 | static int is_kdf_alg_supported( psa_algorithm_t kdf_alg ) |
| 4373 | { |
| 4374 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 4375 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 4376 | return( 1 ); |
| 4377 | #endif |
| 4378 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4379 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 4380 | return( 1 ); |
| 4381 | #endif |
| 4382 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4383 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 4384 | return( 1 ); |
| 4385 | #endif |
| 4386 | return( 0 ); |
| 4387 | } |
| 4388 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4389 | static psa_status_t psa_hash_try_support( psa_algorithm_t alg ) |
| 4390 | { |
| 4391 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 4392 | psa_status_t status = psa_hash_setup( &operation, alg ); |
| 4393 | psa_hash_abort( &operation ); |
| 4394 | return( status ); |
| 4395 | } |
| 4396 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4397 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4398 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4399 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4400 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4401 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4402 | * initialisers for this union leave some bytes unspecified.) */ |
| 4403 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4404 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4405 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4406 | if( ! is_kdf_alg_supported( kdf_alg ) ) |
| 4407 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4408 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4409 | /* All currently supported key derivation algorithms are based on a |
| 4410 | * hash algorithm. */ |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4411 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
| 4412 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
| 4413 | if( hash_size == 0 ) |
| 4414 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4415 | |
| 4416 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 4417 | * we might fail later, which is somewhat unfriendly and potentially |
| 4418 | * risk-prone. */ |
| 4419 | psa_status_t status = psa_hash_try_support( hash_alg ); |
| 4420 | if( status != PSA_SUCCESS ) |
| 4421 | return( status ); |
| 4422 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4423 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4424 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
| 4425 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4426 | { |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4427 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4428 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4429 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4430 | operation->capacity = 255 * hash_size; |
| 4431 | return( PSA_SUCCESS ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4432 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4433 | |
| 4434 | static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg ) |
| 4435 | { |
| 4436 | #if defined(PSA_WANT_ALG_ECDH) |
| 4437 | if( alg == PSA_ALG_ECDH ) |
| 4438 | return( PSA_SUCCESS ); |
| 4439 | #endif |
| 4440 | (void) alg; |
| 4441 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4442 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4443 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4444 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4445 | 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] | 4446 | psa_algorithm_t alg ) |
| 4447 | { |
| 4448 | psa_status_t status; |
| 4449 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4450 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4451 | return( PSA_ERROR_BAD_STATE ); |
| 4452 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4453 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4454 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4455 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4456 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4457 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4458 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4459 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ); |
| 4460 | status = psa_key_agreement_try_support( ka_alg ); |
| 4461 | if( status != PSA_SUCCESS ) |
| 4462 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4463 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4464 | #else |
| 4465 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4466 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4467 | } |
| 4468 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4469 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4470 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4471 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4472 | #else |
| 4473 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4474 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4475 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4476 | else |
| 4477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4478 | |
| 4479 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4480 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4481 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4482 | } |
| 4483 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4484 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4485 | 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] | 4486 | psa_algorithm_t hash_alg, |
| 4487 | psa_key_derivation_step_t step, |
| 4488 | const uint8_t *data, |
| 4489 | size_t data_length ) |
| 4490 | { |
| 4491 | psa_status_t status; |
| 4492 | switch( step ) |
| 4493 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4494 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4495 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4496 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4497 | else |
| 4498 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4499 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4500 | hash_alg, |
| 4501 | data, data_length ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4502 | if( status != PSA_SUCCESS ) |
| 4503 | return( status ); |
| 4504 | hkdf->state = HKDF_STATE_STARTED; |
| 4505 | return( PSA_SUCCESS ); |
| 4506 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4507 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4508 | /* If no salt was provided, use an empty salt. */ |
| 4509 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4510 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4511 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4512 | hash_alg, |
| 4513 | NULL, 0 ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4514 | if( status != PSA_SUCCESS ) |
| 4515 | return( status ); |
| 4516 | hkdf->state = HKDF_STATE_STARTED; |
| 4517 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4518 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4519 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4520 | status = psa_mac_update( &hkdf->hmac, |
| 4521 | data, data_length ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4522 | if( status != PSA_SUCCESS ) |
| 4523 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4524 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4525 | hkdf->prk, |
| 4526 | sizeof( hkdf->prk ), |
| 4527 | &data_length ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4528 | if( status != PSA_SUCCESS ) |
| 4529 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4530 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4531 | hkdf->block_number = 0; |
| 4532 | hkdf->state = HKDF_STATE_KEYED; |
| 4533 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4534 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4535 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4536 | return( PSA_ERROR_BAD_STATE ); |
| 4537 | if( hkdf->info_set ) |
| 4538 | return( PSA_ERROR_BAD_STATE ); |
| 4539 | hkdf->info_length = data_length; |
| 4540 | if( data_length != 0 ) |
| 4541 | { |
| 4542 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4543 | if( hkdf->info == NULL ) |
| 4544 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4545 | memcpy( hkdf->info, data, data_length ); |
| 4546 | } |
| 4547 | hkdf->info_set = 1; |
| 4548 | return( PSA_SUCCESS ); |
| 4549 | default: |
| 4550 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4551 | } |
| 4552 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4553 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4554 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4555 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4556 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4557 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4558 | const uint8_t *data, |
| 4559 | size_t data_length ) |
| 4560 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4561 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4562 | return( PSA_ERROR_BAD_STATE ); |
| 4563 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4564 | if( data_length != 0 ) |
| 4565 | { |
| 4566 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4567 | if( prf->seed == NULL ) |
| 4568 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4569 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4570 | memcpy( prf->seed, data, data_length ); |
| 4571 | prf->seed_length = data_length; |
| 4572 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4573 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4574 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4575 | |
| 4576 | return( PSA_SUCCESS ); |
| 4577 | } |
| 4578 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4579 | 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] | 4580 | const uint8_t *data, |
| 4581 | size_t data_length ) |
| 4582 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4583 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4584 | return( PSA_ERROR_BAD_STATE ); |
| 4585 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4586 | if( data_length != 0 ) |
| 4587 | { |
| 4588 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 4589 | if( prf->secret == NULL ) |
| 4590 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4591 | |
| 4592 | memcpy( prf->secret, data, data_length ); |
| 4593 | prf->secret_length = data_length; |
| 4594 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4595 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4596 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4597 | |
| 4598 | return( PSA_SUCCESS ); |
| 4599 | } |
| 4600 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4601 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 4602 | const uint8_t *data, |
| 4603 | size_t data_length ) |
| 4604 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4605 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4606 | return( PSA_ERROR_BAD_STATE ); |
| 4607 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4608 | if( data_length != 0 ) |
| 4609 | { |
| 4610 | prf->label = mbedtls_calloc( 1, data_length ); |
| 4611 | if( prf->label == NULL ) |
| 4612 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4613 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4614 | memcpy( prf->label, data, data_length ); |
| 4615 | prf->label_length = data_length; |
| 4616 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4617 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4618 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4619 | |
| 4620 | return( PSA_SUCCESS ); |
| 4621 | } |
| 4622 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4623 | 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] | 4624 | psa_key_derivation_step_t step, |
| 4625 | const uint8_t *data, |
| 4626 | size_t data_length ) |
| 4627 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4628 | switch( step ) |
| 4629 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4630 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 4631 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4632 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4633 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4634 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4635 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4636 | default: |
| 4637 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4638 | } |
| 4639 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4640 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4641 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 4642 | |
| 4643 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4644 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4645 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4646 | const uint8_t *data, |
| 4647 | size_t data_length ) |
| 4648 | { |
| 4649 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4650 | 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] | 4651 | uint8_t *cur = pms; |
| 4652 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4653 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4654 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4655 | |
| 4656 | /* Quoting RFC 4279, Section 2: |
| 4657 | * |
| 4658 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4659 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4660 | * uint16 with the value N, and the PSK itself. |
| 4661 | */ |
| 4662 | |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 4663 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 4664 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4665 | memset( cur, 0, data_length ); |
| 4666 | cur += data_length; |
| 4667 | *cur++ = pms[0]; |
| 4668 | *cur++ = pms[1]; |
| 4669 | memcpy( cur, data, data_length ); |
| 4670 | cur += data_length; |
| 4671 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4672 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4673 | |
| 4674 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 4675 | return( status ); |
| 4676 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4677 | |
| 4678 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4679 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4680 | psa_key_derivation_step_t step, |
| 4681 | const uint8_t *data, |
| 4682 | size_t data_length ) |
| 4683 | { |
| 4684 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4685 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4686 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4687 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4688 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4689 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4690 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4691 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4692 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4693 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4694 | /** Check whether the given key type is acceptable for the given |
| 4695 | * input step of a key derivation. |
| 4696 | * |
| 4697 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4698 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4699 | * Both secret and non-secret inputs can alternatively have the type |
| 4700 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4701 | * that the input was passed as a buffer rather than via a key object. |
| 4702 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4703 | static int psa_key_derivation_check_input_type( |
| 4704 | psa_key_derivation_step_t step, |
| 4705 | psa_key_type_t key_type ) |
| 4706 | { |
| 4707 | switch( step ) |
| 4708 | { |
| 4709 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4710 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 4711 | return( PSA_SUCCESS ); |
| 4712 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4713 | return( PSA_SUCCESS ); |
| 4714 | break; |
| 4715 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4716 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4717 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4718 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4719 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 4720 | return( PSA_SUCCESS ); |
| 4721 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4722 | return( PSA_SUCCESS ); |
| 4723 | break; |
| 4724 | } |
| 4725 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4726 | } |
| 4727 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4728 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4729 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4730 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4731 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4732 | const uint8_t *data, |
| 4733 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4734 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4735 | psa_status_t status; |
| 4736 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 4737 | |
| 4738 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4739 | if( status != PSA_SUCCESS ) |
| 4740 | goto exit; |
| 4741 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4742 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4743 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4744 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4745 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4746 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4747 | step, data, data_length ); |
| 4748 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4749 | else |
| 4750 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4751 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4752 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4753 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4754 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4755 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4756 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4757 | else |
| 4758 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4759 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4760 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4761 | { |
| 4762 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4763 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4764 | } |
| 4765 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4766 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4767 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4768 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4769 | (void) data; |
| 4770 | (void) data_length; |
| 4771 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4772 | return( PSA_ERROR_BAD_STATE ); |
| 4773 | } |
| 4774 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4775 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4776 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4777 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4778 | return( status ); |
| 4779 | } |
| 4780 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4781 | psa_status_t psa_key_derivation_input_bytes( |
| 4782 | psa_key_derivation_operation_t *operation, |
| 4783 | psa_key_derivation_step_t step, |
| 4784 | const uint8_t *data, |
| 4785 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4786 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4787 | return( psa_key_derivation_input_internal( operation, step, |
| 4788 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 4789 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4790 | } |
| 4791 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4792 | psa_status_t psa_key_derivation_input_key( |
| 4793 | psa_key_derivation_operation_t *operation, |
| 4794 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4795 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4796 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4797 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4798 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4799 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4800 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4801 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4802 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4803 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4804 | { |
| 4805 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4806 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4807 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4808 | |
| 4809 | /* Passing a key object as a SECRET input unlocks the permission |
| 4810 | * to output to a key object. */ |
| 4811 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4812 | operation->can_output_key = 1; |
| 4813 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4814 | status = psa_key_derivation_input_internal( operation, |
| 4815 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4816 | slot->key.data, |
| 4817 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4818 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4819 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4820 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4821 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4822 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4823 | |
| 4824 | |
| 4825 | |
| 4826 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4827 | /* Key agreement */ |
| 4828 | /****************************************************************/ |
| 4829 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4830 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4831 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 4832 | size_t peer_key_length, |
| 4833 | const mbedtls_ecp_keypair *our_key, |
| 4834 | uint8_t *shared_secret, |
| 4835 | size_t shared_secret_size, |
| 4836 | size_t *shared_secret_length ) |
| 4837 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 4838 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4839 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4840 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4841 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 4842 | 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] | 4843 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4844 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4845 | status = mbedtls_psa_ecp_load_representation( |
| 4846 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4847 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4848 | peer_key, |
| 4849 | peer_key_length, |
| 4850 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4851 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4852 | goto exit; |
| 4853 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4854 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4855 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4856 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4857 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4858 | status = mbedtls_to_psa_error( |
| 4859 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 4860 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4861 | goto exit; |
| 4862 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4863 | status = mbedtls_to_psa_error( |
| 4864 | mbedtls_ecdh_calc_secret( &ecdh, |
| 4865 | shared_secret_length, |
| 4866 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4867 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4868 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4869 | if( status != PSA_SUCCESS ) |
| 4870 | goto exit; |
| 4871 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 4872 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4873 | |
| 4874 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 4875 | if( status != PSA_SUCCESS ) |
| 4876 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4877 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4878 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 4879 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4880 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4881 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4882 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4883 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4884 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4885 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4886 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4887 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 4888 | psa_key_slot_t *private_key, |
| 4889 | const uint8_t *peer_key, |
| 4890 | size_t peer_key_length, |
| 4891 | uint8_t *shared_secret, |
| 4892 | size_t shared_secret_size, |
| 4893 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4894 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4895 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4896 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4897 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4898 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4899 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4900 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4901 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4902 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 4903 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4904 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4905 | private_key->key.data, |
| 4906 | private_key->key.bytes, |
| 4907 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4908 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4909 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4910 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4911 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4912 | shared_secret, shared_secret_size, |
| 4913 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4914 | mbedtls_ecp_keypair_free( ecp ); |
| 4915 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4916 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4917 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4918 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4919 | (void) private_key; |
| 4920 | (void) peer_key; |
| 4921 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4922 | (void) shared_secret; |
| 4923 | (void) shared_secret_size; |
| 4924 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4925 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4926 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4927 | } |
| 4928 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4929 | /* 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] | 4930 | * to potentially free embedded data structures and wipe confidential data. |
| 4931 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4932 | 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] | 4933 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4934 | psa_key_slot_t *private_key, |
| 4935 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4936 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4937 | { |
| 4938 | psa_status_t status; |
| 4939 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4940 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4941 | 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] | 4942 | |
| 4943 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4944 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4945 | status = psa_key_agreement_raw_internal( ka_alg, |
| 4946 | private_key, |
| 4947 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4948 | shared_secret, |
| 4949 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4950 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4951 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4952 | goto exit; |
| 4953 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4954 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4955 | * the shared secret. A shared secret is permitted wherever a key |
| 4956 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4957 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4958 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4959 | shared_secret, |
| 4960 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4961 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4962 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4963 | return( status ); |
| 4964 | } |
| 4965 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4966 | 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] | 4967 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4968 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4969 | const uint8_t *peer_key, |
| 4970 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4971 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4972 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4973 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4974 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4975 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4976 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4977 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4978 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4979 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4980 | if( status != PSA_SUCCESS ) |
| 4981 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4982 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4983 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4984 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4985 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4986 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4987 | else |
| 4988 | { |
| 4989 | /* If a private key has been added as SECRET, we allow the derived |
| 4990 | * key material to be used as a key in PSA Crypto. */ |
| 4991 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4992 | operation->can_output_key = 1; |
| 4993 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4994 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4995 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4996 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4997 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4998 | } |
| 4999 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5000 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5001 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5002 | const uint8_t *peer_key, |
| 5003 | size_t peer_key_length, |
| 5004 | uint8_t *output, |
| 5005 | size_t output_size, |
| 5006 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5007 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5008 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5009 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5010 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5011 | |
| 5012 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5013 | { |
| 5014 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5015 | goto exit; |
| 5016 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5017 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5018 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5019 | if( status != PSA_SUCCESS ) |
| 5020 | goto exit; |
| 5021 | |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame^] | 5022 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5023 | * for the output size. The PSA specification only guarantees that this |
| 5024 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5025 | * but it might be nice to allow smaller buffers if the output fits. |
| 5026 | * At the time of writing this comment, with only ECDH implemented, |
| 5027 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5028 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5029 | * be exact for it as well. */ |
| 5030 | size_t expected_length = |
| 5031 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits ); |
| 5032 | if( output_size < expected_length ) |
| 5033 | { |
| 5034 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5035 | goto exit; |
| 5036 | } |
| 5037 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5038 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5039 | peer_key, peer_key_length, |
| 5040 | output, output_size, |
| 5041 | output_length ); |
| 5042 | |
| 5043 | exit: |
| 5044 | if( status != PSA_SUCCESS ) |
| 5045 | { |
| 5046 | /* If an error happens and is not handled properly, the output |
| 5047 | * may be used as a key to protect sensitive data. Arrange for such |
| 5048 | * a key to be random, which is likely to result in decryption or |
| 5049 | * verification errors. This is better than filling the buffer with |
| 5050 | * some constant data such as zeros, which would result in the data |
| 5051 | * being protected with a reproducible, easily knowable key. |
| 5052 | */ |
| 5053 | psa_generate_random( output, output_size ); |
| 5054 | *output_length = output_size; |
| 5055 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5056 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5057 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5058 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5059 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5060 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5061 | |
| 5062 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5063 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5064 | /****************************************************************/ |
| 5065 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5066 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5067 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5068 | /** Initialize the PSA random generator. |
| 5069 | */ |
| 5070 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5071 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5072 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5073 | memset( rng, 0, sizeof( *rng ) ); |
| 5074 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5075 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5076 | /* Set default configuration if |
| 5077 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5078 | if( rng->entropy_init == NULL ) |
| 5079 | rng->entropy_init = mbedtls_entropy_init; |
| 5080 | if( rng->entropy_free == NULL ) |
| 5081 | rng->entropy_free = mbedtls_entropy_free; |
| 5082 | |
| 5083 | rng->entropy_init( &rng->entropy ); |
| 5084 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5085 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5086 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5087 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5088 | mbedtls_entropy_add_source( &rng->entropy, |
| 5089 | mbedtls_nv_seed_poll, NULL, |
| 5090 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5091 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5092 | #endif |
| 5093 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5094 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5095 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | /** Deinitialize the PSA random generator. |
| 5099 | */ |
| 5100 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 5101 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5102 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5103 | memset( rng, 0, sizeof( *rng ) ); |
| 5104 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5105 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5106 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5107 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | /** Seed the PSA random generator. |
| 5111 | */ |
| 5112 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5113 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5114 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5115 | /* Do nothing: the external RNG seeds itself. */ |
| 5116 | (void) rng; |
| 5117 | return( PSA_SUCCESS ); |
| 5118 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5119 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5120 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5121 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5122 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5123 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5124 | } |
| 5125 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5126 | psa_status_t psa_generate_random( uint8_t *output, |
| 5127 | size_t output_size ) |
| 5128 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5129 | GUARD_MODULE_INITIALIZED; |
| 5130 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5131 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5132 | |
| 5133 | size_t output_length = 0; |
| 5134 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 5135 | output, output_size, |
| 5136 | &output_length ); |
| 5137 | if( status != PSA_SUCCESS ) |
| 5138 | return( status ); |
| 5139 | /* Breaking up a request into smaller chunks is currently not supported |
| 5140 | * for the extrernal RNG interface. */ |
| 5141 | if( output_length != output_size ) |
| 5142 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 5143 | return( PSA_SUCCESS ); |
| 5144 | |
| 5145 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5146 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5147 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5148 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5149 | size_t request_size = |
| 5150 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5151 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5152 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5153 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 5154 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5155 | if( ret != 0 ) |
| 5156 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5157 | output_size -= request_size; |
| 5158 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5159 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5160 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5161 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5162 | } |
| 5163 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5164 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5165 | * |
| 5166 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5167 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5168 | * PSA API doesn't support passing an explicit state. |
| 5169 | * |
| 5170 | * In the non-external case, psa_generate_random() calls an |
| 5171 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5172 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5173 | * instead of doing this back-and-forth between the PSA API and the |
| 5174 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5175 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5176 | */ |
| 5177 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5178 | int mbedtls_psa_get_random( void *p_rng, |
| 5179 | unsigned char *output, |
| 5180 | size_t output_size ) |
| 5181 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5182 | /* This function takes a pointer to the RNG state because that's what |
| 5183 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5184 | * its own state internally and doesn't let the caller access that state. |
| 5185 | * So we just ignore the state parameter, and in practice we'll pass |
| 5186 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5187 | (void) p_rng; |
| 5188 | psa_status_t status = psa_generate_random( output, output_size ); |
| 5189 | if( status == PSA_SUCCESS ) |
| 5190 | return( 0 ); |
| 5191 | else |
| 5192 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 5193 | } |
| 5194 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5195 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5196 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5197 | #include "mbedtls/entropy_poll.h" |
| 5198 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5199 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5200 | size_t seed_size ) |
| 5201 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5202 | if( global_data.initialized ) |
| 5203 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5204 | |
| 5205 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5206 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5207 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5208 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5209 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5210 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5211 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5212 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5213 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5214 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5215 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5216 | * \param type The key type |
| 5217 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5218 | * |
| 5219 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5220 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5221 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5222 | * The size in bits of the key is not valid. |
| 5223 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5224 | * The type and/or the size in bits of the key or the combination of |
| 5225 | * the two is not supported. |
| 5226 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5227 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 5228 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5229 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5230 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5231 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5232 | if( key_type_is_raw_bytes( type ) ) |
| 5233 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5234 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5235 | if( status != PSA_SUCCESS ) |
| 5236 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5237 | } |
| 5238 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5239 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5240 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5241 | { |
| 5242 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5243 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5244 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5245 | /* Accept only byte-aligned keys, for the same reasons as |
| 5246 | * in psa_import_rsa_key(). */ |
| 5247 | if( bits % 8 != 0 ) |
| 5248 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5249 | } |
| 5250 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5251 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5252 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5253 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5254 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5255 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 5256 | /* To avoid empty block, return successfully here. */ |
| 5257 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5258 | } |
| 5259 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5260 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5261 | { |
| 5262 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5263 | } |
| 5264 | |
| 5265 | return( PSA_SUCCESS ); |
| 5266 | } |
| 5267 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5268 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5269 | const psa_key_attributes_t *attributes, |
| 5270 | 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] | 5271 | { |
| 5272 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5273 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5274 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5275 | if( ( attributes->domain_parameters == NULL ) && |
| 5276 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5278 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5279 | if( key_type_is_raw_bytes( type ) ) |
| 5280 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5281 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5282 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5283 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5284 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5285 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5286 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5287 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5288 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5289 | } |
| 5290 | else |
| 5291 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5292 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5293 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5294 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5295 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 5296 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 5297 | key_buffer, |
| 5298 | key_buffer_size, |
| 5299 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5300 | } |
| 5301 | else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5302 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5303 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5304 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5305 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5306 | 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] | 5307 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 5308 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 5309 | key_buffer, |
| 5310 | key_buffer_size, |
| 5311 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5312 | } |
| 5313 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5314 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5315 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5316 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5317 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5318 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5319 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5320 | return( PSA_SUCCESS ); |
| 5321 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5322 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5323 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5324 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5325 | { |
| 5326 | psa_status_t status; |
| 5327 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5328 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5329 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5330 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5331 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5332 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5333 | /* Reject any attempt to create a zero-length key so that we don't |
| 5334 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5335 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5337 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5338 | /* Reject any attempt to create a public key. */ |
| 5339 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 5340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5341 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5342 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 5343 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5344 | if( status != PSA_SUCCESS ) |
| 5345 | goto exit; |
| 5346 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5347 | /* In the case of a transparent key or an opaque key stored in local |
| 5348 | * storage (thus not in the case of generating a key in a secure element |
| 5349 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5350 | * hold the generated key material. */ |
| 5351 | if( slot->key.data == NULL ) |
| 5352 | { |
| 5353 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 5354 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 5355 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5356 | status = psa_validate_key_type_and_size_for_key_generation( |
| 5357 | attributes->core.type, attributes->core.bits ); |
| 5358 | if( status != PSA_SUCCESS ) |
| 5359 | goto exit; |
| 5360 | |
| 5361 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 5362 | attributes->core.type, |
| 5363 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5364 | } |
| 5365 | else |
| 5366 | { |
| 5367 | status = psa_driver_wrapper_get_key_buffer_size( |
| 5368 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5369 | if( status != PSA_SUCCESS ) |
| 5370 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5371 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5372 | |
| 5373 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 5374 | if( status != PSA_SUCCESS ) |
| 5375 | goto exit; |
| 5376 | } |
| 5377 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 5378 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5379 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5380 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5381 | if( status != PSA_SUCCESS ) |
| 5382 | psa_remove_key_data_from_memory( slot ); |
| 5383 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5384 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5385 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5386 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5387 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5388 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5389 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5390 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5391 | } |
| 5392 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5393 | /****************************************************************/ |
| 5394 | /* Module setup */ |
| 5395 | /****************************************************************/ |
| 5396 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5397 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5398 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5399 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5400 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5401 | { |
| 5402 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5403 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5404 | global_data.rng.entropy_init = entropy_init; |
| 5405 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5406 | return( PSA_SUCCESS ); |
| 5407 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5408 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5409 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5410 | void mbedtls_psa_crypto_free( void ) |
| 5411 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5412 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5413 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5414 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5415 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5416 | } |
| 5417 | /* Wipe all remaining data, including configuration. |
| 5418 | * In particular, this sets all state indicator to the value |
| 5419 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5420 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5421 | |
| 5422 | /* Terminate drivers */ |
| 5423 | psa_driver_wrapper_free( ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5426 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5427 | /** Recover a transaction that was interrupted by a power failure. |
| 5428 | * |
| 5429 | * This function is called during initialization, before psa_crypto_init() |
| 5430 | * returns. If this function returns a failure status, the initialization |
| 5431 | * fails. |
| 5432 | */ |
| 5433 | static psa_status_t psa_crypto_recover_transaction( |
| 5434 | const psa_crypto_transaction_t *transaction ) |
| 5435 | { |
| 5436 | switch( transaction->unknown.type ) |
| 5437 | { |
| 5438 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5439 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5440 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5441 | * is implemented. |
| 5442 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5443 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5444 | default: |
| 5445 | /* We found an unsupported transaction in the storage. |
| 5446 | * 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] | 5447 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5448 | } |
| 5449 | } |
| 5450 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5451 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5452 | psa_status_t psa_crypto_init( void ) |
| 5453 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5454 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5456 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5457 | if( global_data.initialized != 0 ) |
| 5458 | return( PSA_SUCCESS ); |
| 5459 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5460 | /* Initialize and seed the random generator. */ |
| 5461 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5462 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5463 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5464 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5465 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5466 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5467 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5468 | status = psa_initialize_key_slots( ); |
| 5469 | if( status != PSA_SUCCESS ) |
| 5470 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5471 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5472 | /* Init drivers */ |
| 5473 | status = psa_driver_wrapper_init( ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5474 | if( status != PSA_SUCCESS ) |
| 5475 | goto exit; |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5476 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5477 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5478 | status = psa_crypto_load_transaction( ); |
| 5479 | if( status == PSA_SUCCESS ) |
| 5480 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5481 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5482 | if( status != PSA_SUCCESS ) |
| 5483 | goto exit; |
| 5484 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5485 | } |
| 5486 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5487 | { |
| 5488 | /* There's no transaction to complete. It's all good. */ |
| 5489 | status = PSA_SUCCESS; |
| 5490 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5491 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5492 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5493 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5494 | global_data.initialized = 1; |
| 5495 | |
| 5496 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5497 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5498 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5499 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |