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 | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 54 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 55 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 57 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 58 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 59 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/blowfish.h" |
| 61 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 62 | #include "mbedtls/chacha20.h" |
| 63 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/cipher.h" |
| 65 | #include "mbedtls/ccm.h" |
| 66 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 67 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 68 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 69 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 70 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 71 | #include "mbedtls/error.h" |
| 72 | #include "mbedtls/gcm.h" |
| 73 | #include "mbedtls/md2.h" |
| 74 | #include "mbedtls/md4.h" |
| 75 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 76 | #include "mbedtls/md.h" |
| 77 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 78 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 79 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 80 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 81 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 82 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 83 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 84 | #include "mbedtls/sha1.h" |
| 85 | #include "mbedtls/sha256.h" |
| 86 | #include "mbedtls/sha512.h" |
| 87 | #include "mbedtls/xtea.h" |
| 88 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 89 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 90 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 91 | /****************************************************************/ |
| 92 | /* Global data, support functions and library management */ |
| 93 | /****************************************************************/ |
| 94 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 95 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 96 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 97 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 100 | /* Values for psa_global_data_t::rng_state */ |
| 101 | #define RNG_NOT_INITIALIZED 0 |
| 102 | #define RNG_INITIALIZED 1 |
| 103 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 104 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 105 | typedef struct |
| 106 | { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 107 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 108 | unsigned rng_state : 2; |
Gilles Peskine | b8006a6 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 109 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 110 | } psa_global_data_t; |
| 111 | |
| 112 | static psa_global_data_t global_data; |
| 113 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 114 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 115 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 116 | &global_data.rng.drbg; |
| 117 | #endif |
| 118 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 119 | #define GUARD_MODULE_INITIALIZED \ |
| 120 | if( global_data.initialized == 0 ) \ |
| 121 | return( PSA_ERROR_BAD_STATE ); |
| 122 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 123 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 124 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 125 | /* Mbed TLS error codes can combine a high-level error code and a |
| 126 | * low-level error code. The low-level error usually reflects the |
| 127 | * root cause better, so dispatch on that preferably. */ |
| 128 | int low_level_ret = - ( -ret & 0x007f ); |
| 129 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 130 | { |
| 131 | case 0: |
| 132 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 133 | |
| 134 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 135 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 136 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 137 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 138 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 139 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 140 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 141 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 142 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 143 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 144 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 145 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 146 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 147 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 148 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 149 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 150 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 151 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 152 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 153 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 154 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 155 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 156 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 157 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 158 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 159 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 160 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 161 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 162 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 163 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 164 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 165 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 166 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 167 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 168 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 169 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 170 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 171 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 172 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 173 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 174 | |
| 175 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 176 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 177 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 178 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 179 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 180 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 181 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 182 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 183 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 184 | |
| 185 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 186 | return( PSA_ERROR_BAD_STATE ); |
| 187 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 188 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 189 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 190 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 191 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 192 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 193 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 194 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 195 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 196 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 197 | return( PSA_ERROR_INVALID_PADDING ); |
| 198 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 200 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 201 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 202 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 203 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 204 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 205 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 206 | |
| 207 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 208 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 209 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 210 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 211 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 212 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 213 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 214 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 215 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 216 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 217 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 218 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 219 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 220 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 221 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 222 | |
| 223 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 225 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 226 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 227 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 228 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 229 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 230 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 231 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 232 | |
| 233 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 234 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 235 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 238 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 239 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 240 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 241 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 242 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 243 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 244 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 245 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 246 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 247 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 248 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 249 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 250 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 251 | #endif |
| 252 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 253 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 254 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 255 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 256 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 257 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 259 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 260 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 261 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 262 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 263 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 264 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 265 | return( PSA_ERROR_STORAGE_FAILURE ); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 266 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 267 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 268 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 269 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 270 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 271 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 272 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 273 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 275 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 276 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 277 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 281 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 283 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 284 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 285 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 287 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 288 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 289 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 292 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 293 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 294 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 295 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 296 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 297 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 298 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 299 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 300 | return( PSA_ERROR_NOT_PERMITTED ); |
| 301 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 302 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 303 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 304 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 305 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 306 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 307 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 308 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 309 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 310 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 311 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 312 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 313 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 314 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 315 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 316 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 317 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 318 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 319 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 321 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 322 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 323 | return( PSA_ERROR_INVALID_PADDING ); |
| 324 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 325 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 326 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 327 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 328 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 329 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 330 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 331 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 332 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 333 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 334 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 335 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 336 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 337 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 338 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 339 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 340 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 341 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 342 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 343 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 344 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 345 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 346 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 347 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 348 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 349 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 350 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 351 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 352 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 353 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 355 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 356 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 357 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 358 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 359 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 360 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 361 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 362 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 363 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 364 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 365 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 366 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 367 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 368 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 369 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 370 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 371 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 372 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 373 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 377 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 378 | |
| 379 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 380 | /****************************************************************/ |
| 381 | /* Key management */ |
| 382 | /****************************************************************/ |
| 383 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 384 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 385 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 386 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 387 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 388 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 389 | 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] | 390 | size_t bits, |
| 391 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 392 | { |
| 393 | switch( curve ) |
| 394 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 395 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 396 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 397 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 398 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 399 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 400 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 401 | #endif |
| 402 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 403 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 404 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 405 | #endif |
| 406 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 407 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 409 | #endif |
| 410 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 413 | #endif |
| 414 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 417 | case 528: |
| 418 | if( bits_is_sloppy ) |
| 419 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 420 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 421 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 422 | } |
| 423 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 425 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 426 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 427 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 428 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 429 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 430 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 431 | #endif |
| 432 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 433 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 434 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 435 | #endif |
| 436 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 437 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 438 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 439 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 440 | } |
| 441 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 442 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 443 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 444 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 445 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 446 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 447 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 448 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 449 | case 256: |
| 450 | if( bits_is_sloppy ) |
| 451 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 452 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 453 | #endif |
| 454 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 455 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 456 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 457 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 458 | } |
| 459 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 460 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 461 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 462 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 463 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 464 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 465 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 466 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 467 | #endif |
| 468 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 469 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 470 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 471 | #endif |
| 472 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 473 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 474 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 475 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 476 | } |
| 477 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 478 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 479 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 480 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 481 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 482 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 483 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 484 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 485 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 486 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 487 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 488 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 489 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 490 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 491 | { |
| 492 | /* Check that the bit size is acceptable for the key type */ |
| 493 | switch( type ) |
| 494 | { |
| 495 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 496 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 497 | case PSA_KEY_TYPE_DERIVE: |
| 498 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 499 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 500 | case PSA_KEY_TYPE_AES: |
| 501 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 502 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 503 | break; |
| 504 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 505 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 506 | case PSA_KEY_TYPE_ARIA: |
| 507 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 508 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 509 | break; |
| 510 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 511 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 512 | case PSA_KEY_TYPE_CAMELLIA: |
| 513 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 514 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 515 | break; |
| 516 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 517 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 518 | case PSA_KEY_TYPE_DES: |
| 519 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 520 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 521 | break; |
| 522 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 523 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 524 | case PSA_KEY_TYPE_ARC4: |
| 525 | if( bits < 8 || bits > 2048 ) |
| 526 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 527 | break; |
| 528 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 529 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 530 | case PSA_KEY_TYPE_CHACHA20: |
| 531 | if( bits != 256 ) |
| 532 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 533 | break; |
| 534 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 535 | default: |
| 536 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 537 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 538 | if( bits % 8 != 0 ) |
| 539 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 540 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 541 | return( PSA_SUCCESS ); |
| 542 | } |
| 543 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 544 | /** 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] | 545 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 546 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 547 | * 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] | 548 | * |
| 549 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 550 | * \param[in] key_type The key type of the key to be used with the |
| 551 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 552 | * |
| 553 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 554 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 555 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 556 | * 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] | 557 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 558 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 559 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 560 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 561 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 562 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 563 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 564 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 565 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 566 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 567 | |
| 568 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 569 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 570 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 571 | * key. */ |
| 572 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 573 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 574 | { |
| 575 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 576 | * the block length (larger than 1) for block ciphers. */ |
| 577 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 578 | return( PSA_SUCCESS ); |
| 579 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 580 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 581 | |
| 582 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 585 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 586 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 587 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 588 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 589 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 590 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 591 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 592 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 593 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
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.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 596 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 599 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 600 | const uint8_t* data, |
| 601 | size_t data_length ) |
| 602 | { |
| 603 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 604 | data_length ); |
| 605 | if( status != PSA_SUCCESS ) |
| 606 | return( status ); |
| 607 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 608 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 609 | return( PSA_SUCCESS ); |
| 610 | } |
| 611 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 612 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 613 | const psa_key_attributes_t *attributes, |
| 614 | const uint8_t *data, size_t data_length, |
| 615 | uint8_t *key_buffer, size_t key_buffer_size, |
| 616 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 617 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 618 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 619 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 620 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 621 | /* zero-length keys are never supported. */ |
| 622 | if( data_length == 0 ) |
| 623 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 624 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 625 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 626 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 627 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 628 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 629 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 630 | if( data_length > SIZE_MAX / 8 ) |
| 631 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 632 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 633 | /* Enforce a size limit, and in particular ensure that the bit |
| 634 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 635 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 636 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 637 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 638 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 639 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 640 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 641 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 642 | /* Copy the key material. */ |
| 643 | memcpy( key_buffer, data, data_length ); |
| 644 | *key_buffer_length = data_length; |
| 645 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 646 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 647 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 648 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 649 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 650 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 651 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 652 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 653 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 654 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 655 | return( mbedtls_psa_ecp_import_key( attributes, |
| 656 | data, data_length, |
| 657 | key_buffer, key_buffer_size, |
| 658 | key_buffer_length, |
| 659 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 660 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 661 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 662 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 663 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 664 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 665 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 666 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 667 | return( mbedtls_psa_rsa_import_key( attributes, |
| 668 | data, data_length, |
| 669 | key_buffer, key_buffer_size, |
| 670 | key_buffer_length, |
| 671 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 672 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 673 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 674 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 675 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 676 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 677 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 680 | /** Calculate the intersection of two algorithm usage policies. |
| 681 | * |
| 682 | * Return 0 (which allows no operation) on incompatibility. |
| 683 | */ |
| 684 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 685 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 686 | psa_algorithm_t alg1, |
| 687 | psa_algorithm_t alg2 ) |
| 688 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 689 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 690 | if( alg1 == alg2 ) |
| 691 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 692 | /* If the policies are from the same hash-and-sign family, check |
| 693 | * 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] | 694 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 695 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 697 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 698 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 699 | return( alg2 ); |
| 700 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 701 | return( alg1 ); |
| 702 | } |
| 703 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 704 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 705 | * restrictive tag length. */ |
| 706 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 707 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 708 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 709 | { |
| 710 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 711 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 712 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 713 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 714 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 715 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 716 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 717 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 718 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 719 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 720 | } |
| 721 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 722 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 723 | ( alg1_len <= alg2_len ) ) |
| 724 | { |
| 725 | return( alg2 ); |
| 726 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 727 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 728 | ( alg2_len <= alg1_len ) ) |
| 729 | { |
| 730 | return( alg1 ); |
| 731 | } |
| 732 | } |
| 733 | /* If the policies are from the same MAC family, check whether one |
| 734 | * of them is a minimum-MAC-length policy. Calculate the most |
| 735 | * restrictive tag length. */ |
| 736 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 737 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 738 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 739 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 740 | /* Validate the combination of key type and algorithm. Since the base |
| 741 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 742 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 743 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 744 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 745 | /* Get the (exact or at-least) output lengths for both sides of the |
| 746 | * requested intersection. None of the currently supported algorithms |
| 747 | * have an output length dependent on the actual key size, so setting it |
| 748 | * to a bogus value of 0 is currently OK. |
| 749 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 750 | * Note that for at-least-this-length wildcard algorithms, the output |
| 751 | * length is set to the shortest allowed length, which allows us to |
| 752 | * calculate the most restrictive tag length for the intersection. */ |
| 753 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 754 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 755 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 756 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 757 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 758 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 759 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 760 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 761 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 762 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 763 | |
| 764 | /* If only one is an at-least-this-length policy, the intersection would |
| 765 | * be the other (fixed-length) policy as long as said fixed length is |
| 766 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 767 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 768 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 769 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 770 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 771 | if( ( alg2 & 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( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 774 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 775 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 776 | /* If none of them are wildcards, check whether they define the same tag |
| 777 | * length. This is still possible here when one is default-length and |
| 778 | * the other specific-length. Ensure to always return the |
| 779 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 780 | if( alg1_len == alg2_len ) |
| 781 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 782 | } |
| 783 | /* If the policies are incompatible, allow nothing. */ |
| 784 | return( 0 ); |
| 785 | } |
| 786 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 787 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 788 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 789 | psa_algorithm_t requested_alg ) |
| 790 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 791 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 792 | if( requested_alg == policy_alg ) |
| 793 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 794 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 795 | * and requested_alg is the same hash-and-sign family with any hash, |
| 796 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 797 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 798 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 799 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 800 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 801 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 802 | } |
| 803 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 804 | * the requested algorithm, check the requested tag length to be |
| 805 | * equal-length or longer than the wildcard-specified length. */ |
| 806 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 807 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 808 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 809 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 810 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 811 | { |
| 812 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 813 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 814 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 815 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 816 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 817 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 818 | PSA_ALG_IS_MAC( requested_alg ) && |
| 819 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 820 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 821 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 822 | /* Validate the combination of key type and algorithm. Since the policy |
| 823 | * and requested algorithms are the same, we only need this once. */ |
| 824 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 825 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 826 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 827 | /* Get both the requested output length for the algorithm which is to be |
| 828 | * verified, and the default output length for the base algorithm. |
| 829 | * Note that none of the currently supported algorithms have an output |
| 830 | * length dependent on actual key size, so setting it to a bogus value |
| 831 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 832 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 833 | key_type, 0, requested_alg ); |
| 834 | size_t default_output_length = PSA_MAC_LENGTH( |
| 835 | key_type, 0, |
| 836 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 837 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 838 | /* If the policy is default-length, only allow an algorithm with |
| 839 | * a declared exact-length matching the default. */ |
| 840 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 841 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 842 | |
| 843 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 844 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 845 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 846 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 847 | { |
| 848 | return( 1 ); |
| 849 | } |
| 850 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 851 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 852 | * check for the requested MAC length to be equal to or longer than the |
| 853 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 854 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 855 | { |
| 856 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 857 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 858 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 859 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 860 | /* 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] | 861 | * a key derivation with that key agreement should also be allowed. This |
| 862 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 863 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 864 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 865 | { |
| 866 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 867 | policy_alg ); |
| 868 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 869 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 870 | return( 0 ); |
| 871 | } |
| 872 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 873 | /** Test whether a policy permits an algorithm. |
| 874 | * |
| 875 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 876 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 877 | * \note This function requires providing the key type for which the policy is |
| 878 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 879 | * have different properties depending on what kind of cipher it is |
| 880 | * combined with. |
| 881 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 882 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 883 | * allowed by the \p policy. |
| 884 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 885 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 886 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 887 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 888 | 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] | 889 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 890 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 891 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 892 | /* '0' is not a valid algorithm */ |
| 893 | if( alg == 0 ) |
| 894 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 895 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 896 | /* A requested algorithm cannot be a wildcard. */ |
| 897 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 898 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 899 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 900 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 901 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 902 | return( PSA_SUCCESS ); |
| 903 | else |
| 904 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 907 | /** Restrict a key policy based on a constraint. |
| 908 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 909 | * \note This function requires providing the key type for which the policy is |
| 910 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 911 | * have different properties depending on what kind of cipher it is |
| 912 | * combined with. |
| 913 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 914 | * \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] | 915 | * \param[in,out] policy The policy to restrict. |
| 916 | * \param[in] constraint The policy constraint to apply. |
| 917 | * |
| 918 | * \retval #PSA_SUCCESS |
| 919 | * \c *policy contains the intersection of the original value of |
| 920 | * \c *policy and \c *constraint. |
| 921 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 922 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 923 | * \c *policy is unchanged. |
| 924 | */ |
| 925 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 926 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 927 | psa_key_policy_t *policy, |
| 928 | const psa_key_policy_t *constraint ) |
| 929 | { |
| 930 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 931 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 932 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 933 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 934 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 935 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 936 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 937 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 938 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 939 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 940 | policy->usage &= constraint->usage; |
| 941 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 942 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 943 | return( PSA_SUCCESS ); |
| 944 | } |
| 945 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 946 | /** Get the description of a key given its identifier and policy constraints |
| 947 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 948 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 949 | * 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] | 950 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 951 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 952 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 953 | * In case of a persistent key, the function loads the description of the key |
| 954 | * into a key slot if not already done. |
| 955 | * |
| 956 | * On success, the returned key slot is locked. It is the responsibility of |
| 957 | * 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] | 958 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 959 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 960 | mbedtls_svc_key_id_t key, |
| 961 | psa_key_slot_t **p_slot, |
| 962 | psa_key_usage_t usage, |
| 963 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 964 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 965 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 966 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 967 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 968 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 969 | if( status != PSA_SUCCESS ) |
| 970 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 971 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 972 | |
| 973 | /* Enforce that usage policy for the key slot contains all the flags |
| 974 | * required by the usage parameter. There is one exception: public |
| 975 | * keys can always be exported, so we treat public key objects as |
| 976 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 977 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 978 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 979 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 980 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 981 | { |
| 982 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 983 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 984 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 985 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 986 | /* Enforce that the usage policy permits the requested algorithm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 987 | if( alg != 0 ) |
| 988 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 989 | status = psa_key_policy_permits( &slot->attr.policy, |
| 990 | slot->attr.type, |
| 991 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 992 | if( status != PSA_SUCCESS ) |
| 993 | goto error; |
| 994 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 995 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 996 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 997 | |
| 998 | error: |
| 999 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1000 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1001 | |
| 1002 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1003 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1004 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1005 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1006 | * |
| 1007 | * 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] | 1008 | * available, as opposed to a key in a secure element and/or to be used |
| 1009 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1010 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1011 | * This is a temporary function that may be used instead of |
| 1012 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1013 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1014 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1015 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1016 | * 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] | 1017 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1018 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1019 | mbedtls_svc_key_id_t key, |
| 1020 | psa_key_slot_t **p_slot, |
| 1021 | psa_key_usage_t usage, |
| 1022 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1023 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1024 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1025 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1026 | if( status != PSA_SUCCESS ) |
| 1027 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1028 | |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1029 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1030 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1031 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1032 | *p_slot = NULL; |
| 1033 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1034 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1035 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1036 | return( PSA_SUCCESS ); |
| 1037 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1038 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 1039 | 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] | 1040 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1041 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1042 | * initialized slot, so we can just free it. */ |
| 1043 | if( slot->key.data != NULL ) |
| 1044 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1045 | |
| 1046 | mbedtls_free( slot->key.data ); |
| 1047 | slot->key.data = NULL; |
| 1048 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1049 | |
| 1050 | return( PSA_SUCCESS ); |
| 1051 | } |
| 1052 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1053 | /** Completely wipe a slot in memory, including its policy. |
| 1054 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1055 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1056 | { |
| 1057 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1058 | |
| 1059 | /* |
| 1060 | * 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] | 1061 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1062 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1063 | * 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] | 1064 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1065 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1066 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1067 | { |
| 1068 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1069 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1070 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1071 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1072 | } |
| 1073 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1074 | /* Multipart operations may still be using the key. This is safe |
| 1075 | * because all multipart operation objects are independent from |
| 1076 | * the key slot: if they need to access the key after the setup |
| 1077 | * phase, they have a copy of the key. Note that this means that |
| 1078 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1079 | /* At this point, key material and other type-specific content has |
| 1080 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1081 | * zeroize because the metadata is not particularly sensitive. */ |
| 1082 | memset( slot, 0, sizeof( *slot ) ); |
| 1083 | return( status ); |
| 1084 | } |
| 1085 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1086 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1087 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1088 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1089 | psa_status_t status; /* status of the last operation */ |
| 1090 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1091 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1092 | psa_se_drv_table_entry_t *driver; |
| 1093 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1094 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1095 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1096 | return( PSA_SUCCESS ); |
| 1097 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1098 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1099 | * 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] | 1100 | * key, this will load the key description from persistent memory if not |
| 1101 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1102 | * the key is operated by an SE or not and this information is needed by |
| 1103 | * the current implementation. |
| 1104 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1105 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1106 | if( status != PSA_SUCCESS ) |
| 1107 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1108 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1109 | /* |
| 1110 | * If the key slot containing the key description is under access by the |
| 1111 | * library (apart from the present access), the key cannot be destroyed |
| 1112 | * yet. For the time being, just return in error. Eventually (to be |
| 1113 | * implemented), the key should be destroyed when all accesses have |
| 1114 | * stopped. |
| 1115 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1116 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1117 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1118 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1119 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1120 | } |
| 1121 | |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1122 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1123 | { |
| 1124 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1125 | * if we attempt it, depending on whether the key is merely read-only |
| 1126 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1127 | * Just do the best we can, which is to wipe the copy in memory |
| 1128 | * (done in this function's cleanup code). */ |
| 1129 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1130 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1133 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1134 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1135 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1136 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1137 | /* For a key in a secure element, we need to do three things: |
| 1138 | * remove the key file in internal storage, destroy the |
| 1139 | * key inside the secure element, and update the driver's |
| 1140 | * persistent data. Start a transaction that will encompass these |
| 1141 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1142 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1143 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1144 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1145 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1146 | status = psa_crypto_save_transaction( ); |
| 1147 | if( status != PSA_SUCCESS ) |
| 1148 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1149 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1150 | /* We should still try to destroy the key in the secure |
| 1151 | * element and the key metadata in storage. This is especially |
| 1152 | * important if the error is that the storage is full. |
| 1153 | * But how to do it exactly without risking an inconsistent |
| 1154 | * state after a reset? |
| 1155 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1156 | */ |
| 1157 | overall_status = status; |
| 1158 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1161 | status = psa_destroy_se_key( driver, |
| 1162 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1163 | if( overall_status == PSA_SUCCESS ) |
| 1164 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1165 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1166 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1167 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1168 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1169 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1170 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1171 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1172 | if( overall_status == PSA_SUCCESS ) |
| 1173 | overall_status = status; |
| 1174 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1175 | /* TODO: other slots may have a copy of the same key. We should |
| 1176 | * invalidate them. |
| 1177 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1178 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1179 | } |
| 1180 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1181 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1182 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1183 | if( driver != NULL ) |
| 1184 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1185 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1186 | if( overall_status == PSA_SUCCESS ) |
| 1187 | overall_status = status; |
| 1188 | status = psa_crypto_stop_transaction( ); |
| 1189 | if( overall_status == PSA_SUCCESS ) |
| 1190 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1191 | } |
| 1192 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1193 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1194 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1195 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1196 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1197 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1198 | overall_status = status; |
| 1199 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1202 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1203 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1204 | static psa_status_t psa_get_rsa_public_exponent( |
| 1205 | const mbedtls_rsa_context *rsa, |
| 1206 | psa_key_attributes_t *attributes ) |
| 1207 | { |
| 1208 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1209 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1210 | uint8_t *buffer = NULL; |
| 1211 | size_t buflen; |
| 1212 | mbedtls_mpi_init( &mpi ); |
| 1213 | |
| 1214 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1215 | if( ret != 0 ) |
| 1216 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1217 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1218 | { |
| 1219 | /* It's the default value, which is reported as an empty string, |
| 1220 | * so there's nothing to do. */ |
| 1221 | goto exit; |
| 1222 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1223 | |
| 1224 | buflen = mbedtls_mpi_size( &mpi ); |
| 1225 | buffer = mbedtls_calloc( 1, buflen ); |
| 1226 | if( buffer == NULL ) |
| 1227 | { |
| 1228 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1229 | goto exit; |
| 1230 | } |
| 1231 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1232 | if( ret != 0 ) |
| 1233 | goto exit; |
| 1234 | attributes->domain_parameters = buffer; |
| 1235 | attributes->domain_parameters_size = buflen; |
| 1236 | |
| 1237 | exit: |
| 1238 | mbedtls_mpi_free( &mpi ); |
| 1239 | if( ret != 0 ) |
| 1240 | mbedtls_free( buffer ); |
| 1241 | return( mbedtls_to_psa_error( ret ) ); |
| 1242 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1243 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1244 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1245 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1246 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1247 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1248 | 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] | 1249 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1250 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1251 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1252 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1253 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1254 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1255 | psa_reset_key_attributes( attributes ); |
| 1256 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1257 | 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] | 1258 | if( status != PSA_SUCCESS ) |
| 1259 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1260 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1261 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1262 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1263 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1264 | |
| 1265 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 0518f61 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1266 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1267 | psa_set_key_slot_number( attributes, |
| 1268 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1269 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1270 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1271 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1272 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1273 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1274 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1275 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1276 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1277 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1278 | * is not yet implemented. |
| 1279 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1280 | */ |
Ronald Cron | 41e8006 | 2021-08-24 16:00:51 +0200 | [diff] [blame] | 1281 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1282 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1283 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1284 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1285 | status = mbedtls_psa_rsa_load_representation( |
| 1286 | slot->attr.type, |
| 1287 | slot->key.data, |
| 1288 | slot->key.bytes, |
| 1289 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1290 | if( status != PSA_SUCCESS ) |
| 1291 | break; |
| 1292 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1293 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1294 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1295 | mbedtls_rsa_free( rsa ); |
| 1296 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1297 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1298 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1299 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1300 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1301 | default: |
| 1302 | /* Nothing else to do. */ |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | if( status != PSA_SUCCESS ) |
| 1307 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1308 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1309 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1310 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1311 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1314 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1315 | psa_status_t psa_get_key_slot_number( |
| 1316 | const psa_key_attributes_t *attributes, |
| 1317 | psa_key_slot_number_t *slot_number ) |
| 1318 | { |
| 1319 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1320 | { |
| 1321 | *slot_number = attributes->slot_number; |
| 1322 | return( PSA_SUCCESS ); |
| 1323 | } |
| 1324 | else |
| 1325 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1326 | } |
| 1327 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1328 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1329 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1330 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1331 | uint8_t *data, |
| 1332 | size_t data_size, |
| 1333 | size_t *data_length ) |
| 1334 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1335 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1336 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1337 | memcpy( data, key_buffer, key_buffer_size ); |
| 1338 | memset( data + key_buffer_size, 0, |
| 1339 | data_size - key_buffer_size ); |
| 1340 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1341 | return( PSA_SUCCESS ); |
| 1342 | } |
| 1343 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1344 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1345 | const psa_key_attributes_t *attributes, |
| 1346 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1347 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1348 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1349 | psa_key_type_t type = attributes->core.type; |
| 1350 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1351 | if( key_type_is_raw_bytes( type ) || |
| 1352 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1353 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1354 | { |
| 1355 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1356 | key_buffer, key_buffer_size, |
| 1357 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1358 | } |
| 1359 | else |
| 1360 | { |
| 1361 | /* This shouldn't happen in the reference implementation, but |
| 1362 | it is valid for a special-purpose implementation to omit |
| 1363 | support for exporting certain key types. */ |
| 1364 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1369 | uint8_t *data, |
| 1370 | size_t data_size, |
| 1371 | size_t *data_length ) |
| 1372 | { |
| 1373 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1374 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1375 | psa_key_slot_t *slot; |
| 1376 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1377 | /* Reject a zero-length output buffer now, since this can never be a |
| 1378 | * valid key representation. This way we know that data must be a valid |
| 1379 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1380 | if( data_size == 0 ) |
| 1381 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1382 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1383 | /* Set the key to empty now, so that even when there are errors, we always |
| 1384 | * set data_length to a value between 0 and data_size. On error, setting |
| 1385 | * the key to empty is a good choice because an empty key representation is |
| 1386 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1387 | *data_length = 0; |
| 1388 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1389 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1390 | * which don't require any flag, but |
| 1391 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1392 | */ |
| 1393 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1394 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1395 | if( status != PSA_SUCCESS ) |
| 1396 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1397 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1398 | psa_key_attributes_t attributes = { |
| 1399 | .core = slot->attr |
| 1400 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1401 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1402 | slot->key.data, slot->key.bytes, |
| 1403 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1404 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1405 | unlock_status = psa_unlock_key_slot( slot ); |
| 1406 | |
| 1407 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1408 | } |
| 1409 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1410 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1411 | const psa_key_attributes_t *attributes, |
| 1412 | const uint8_t *key_buffer, |
| 1413 | size_t key_buffer_size, |
| 1414 | uint8_t *data, |
| 1415 | size_t data_size, |
| 1416 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1417 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1418 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1419 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1420 | 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] | 1421 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1422 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1423 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1424 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1425 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1426 | key_buffer, key_buffer_size, |
| 1427 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1428 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1429 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1430 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1431 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1432 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1433 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1434 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1435 | key_buffer, |
| 1436 | key_buffer_size, |
| 1437 | data, |
| 1438 | data_size, |
| 1439 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1440 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1441 | /* We don't know how to convert a private RSA key to public. */ |
| 1442 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1443 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1444 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1445 | } |
| 1446 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1447 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1448 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1449 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1450 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1451 | key_buffer, |
| 1452 | key_buffer_size, |
| 1453 | data, |
| 1454 | data_size, |
| 1455 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1456 | #else |
| 1457 | /* 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] | 1458 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1459 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1460 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1461 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1462 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1463 | else |
| 1464 | { |
| 1465 | /* This shouldn't happen in the reference implementation, but |
| 1466 | it is valid for a special-purpose implementation to omit |
| 1467 | support for exporting certain key types. */ |
| 1468 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1469 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1470 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1471 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1472 | 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] | 1473 | uint8_t *data, |
| 1474 | size_t data_size, |
| 1475 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1476 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1477 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1478 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1479 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1480 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1481 | /* Reject a zero-length output buffer now, since this can never be a |
| 1482 | * valid key representation. This way we know that data must be a valid |
| 1483 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1484 | if( data_size == 0 ) |
| 1485 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1486 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1487 | /* Set the key to empty now, so that even when there are errors, we always |
| 1488 | * set data_length to a value between 0 and data_size. On error, setting |
| 1489 | * the key to empty is a good choice because an empty key representation is |
| 1490 | * unlikely to be accepted anywhere. */ |
| 1491 | *data_length = 0; |
| 1492 | |
| 1493 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1494 | 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] | 1495 | if( status != PSA_SUCCESS ) |
| 1496 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1497 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1498 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1499 | { |
| 1500 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1501 | goto exit; |
| 1502 | } |
| 1503 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1504 | psa_key_attributes_t attributes = { |
| 1505 | .core = slot->attr |
| 1506 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1507 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1508 | &attributes, slot->key.data, slot->key.bytes, |
| 1509 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1510 | |
| 1511 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1512 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1513 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1514 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1515 | } |
| 1516 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1517 | #if defined(static_assert) |
| 1518 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1519 | "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] | 1520 | 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] | 1521 | "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] | 1522 | 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] | 1523 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1524 | #endif |
| 1525 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | /** Validate that a key policy is internally well-formed. |
| 1527 | * |
| 1528 | * This function only rejects invalid policies. It does not validate the |
| 1529 | * consistency of the policy with respect to other attributes of the key |
| 1530 | * such as the key type. |
| 1531 | */ |
| 1532 | 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] | 1533 | { |
| 1534 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1535 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1536 | PSA_KEY_USAGE_ENCRYPT | |
| 1537 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1538 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1539 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1540 | PSA_KEY_USAGE_SIGN_HASH | |
| 1541 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1542 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1543 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1544 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1545 | return( PSA_SUCCESS ); |
| 1546 | } |
| 1547 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1548 | /** Validate the internal consistency of key attributes. |
| 1549 | * |
| 1550 | * This function only rejects invalid attribute values. If does not |
| 1551 | * validate the consistency of the attributes with any key data that may |
| 1552 | * be involved in the creation of the key. |
| 1553 | * |
| 1554 | * Call this function early in the key creation process. |
| 1555 | * |
| 1556 | * \param[in] attributes Key attributes for the new key. |
| 1557 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1558 | * NULL for a transparent key. |
| 1559 | * |
| 1560 | */ |
| 1561 | static psa_status_t psa_validate_key_attributes( |
| 1562 | const psa_key_attributes_t *attributes, |
| 1563 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1564 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1565 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1566 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1567 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1568 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1569 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1570 | if( status != PSA_SUCCESS ) |
| 1571 | return( status ); |
| 1572 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1573 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1574 | if( status != PSA_SUCCESS ) |
| 1575 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1576 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1577 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1578 | { |
| 1579 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1580 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1581 | } |
| 1582 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1583 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1584 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1585 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1588 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1589 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1590 | return( status ); |
| 1591 | |
| 1592 | /* Refuse to create overly large keys. |
| 1593 | * Note that this doesn't trigger on import if the attributes don't |
| 1594 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1595 | * psa_import_key() needs its own checks. */ |
| 1596 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1597 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1598 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1599 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1600 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1601 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1602 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1603 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1604 | return( PSA_SUCCESS ); |
| 1605 | } |
| 1606 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1607 | /** Prepare a key slot to receive key material. |
| 1608 | * |
| 1609 | * This function allocates a key slot and sets its metadata. |
| 1610 | * |
| 1611 | * If this function fails, call psa_fail_key_creation(). |
| 1612 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1613 | * This function is intended to be used as follows: |
| 1614 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1615 | * it with the specified attributes, and in case of a volatile key assign it |
| 1616 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1617 | * -# Populate the slot with the key material. |
| 1618 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1619 | * In case of failure at any step, stop the sequence and call |
| 1620 | * psa_fail_key_creation(). |
| 1621 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1622 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1623 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1624 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1625 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1626 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1627 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1628 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1629 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1630 | * |
| 1631 | * \retval #PSA_SUCCESS |
| 1632 | * The key slot is ready to receive key material. |
| 1633 | * \return If this function fails, the key slot is an invalid state. |
| 1634 | * 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] | 1635 | */ |
| 1636 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1637 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1638 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1639 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1640 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | { |
| 1642 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1643 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1644 | psa_key_slot_t *slot; |
| 1645 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1646 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1647 | *p_drv = NULL; |
| 1648 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1649 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1650 | if( status != PSA_SUCCESS ) |
| 1651 | return( status ); |
| 1652 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1653 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1654 | if( status != PSA_SUCCESS ) |
| 1655 | return( status ); |
| 1656 | slot = *p_slot; |
| 1657 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1658 | /* We're storing the declared bit-size of the key. It's up to each |
| 1659 | * creation mechanism to verify that this information is correct. |
| 1660 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1661 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1662 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1663 | * volatile key identifier associated to the slot returned to contain its |
| 1664 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1665 | |
| 1666 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1667 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1668 | { |
| 1669 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1670 | slot->attr.id = volatile_key_id; |
| 1671 | #else |
| 1672 | slot->attr.id.key_id = volatile_key_id; |
| 1673 | #endif |
| 1674 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1675 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1676 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1677 | * external-only flags, query `attributes`. Thanks to the check |
| 1678 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1679 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1680 | * may have set. */ |
| 1681 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1682 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1683 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1684 | /* 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] | 1685 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1686 | * create the key file in internal storage, create the |
| 1687 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1688 | * persistent data. This is done by starting a transaction that will |
| 1689 | * encompass these three actions. |
| 1690 | * For registering a volatile key, we just need to find an appropriate |
| 1691 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1692 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1693 | /* The first thing to do is to find a slot number for the new key. |
| 1694 | * We save the slot number in persistent storage as part of the |
| 1695 | * transaction data. It will be needed to recover if the power |
| 1696 | * fails during the key creation process, to clean up on the secure |
| 1697 | * element side after restarting. Obtaining a slot number from the |
| 1698 | * secure element driver updates its persistent state, but we do not yet |
| 1699 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1700 | * 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] | 1701 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1702 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1703 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1704 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1705 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1706 | if( status != PSA_SUCCESS ) |
| 1707 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1708 | |
| 1709 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1710 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1711 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1712 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1713 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1714 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1715 | status = psa_crypto_save_transaction( ); |
| 1716 | if( status != PSA_SUCCESS ) |
| 1717 | { |
| 1718 | (void) psa_crypto_stop_transaction( ); |
| 1719 | return( status ); |
| 1720 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1721 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1722 | |
| 1723 | status = psa_copy_key_material_into_slot( |
| 1724 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1725 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1726 | |
| 1727 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1728 | { |
| 1729 | /* Key registration only makes sense with a secure element. */ |
| 1730 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1731 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1732 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1733 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1734 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1735 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1736 | |
| 1737 | /** Finalize the creation of a key once its key material has been set. |
| 1738 | * |
| 1739 | * This entails writing the key to persistent storage. |
| 1740 | * |
| 1741 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1742 | * See the documentation of psa_start_key_creation() for the intended use |
| 1743 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1744 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1745 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1746 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1747 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1748 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1749 | * \param[in,out] slot Pointer to the slot with key material. |
| 1750 | * \param[in] driver The secure element driver for the key, |
| 1751 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1752 | * \param[out] key On success, identifier of the key. Note that the |
| 1753 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1754 | * |
| 1755 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1756 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1757 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1758 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1759 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1760 | * \retval #PSA_ERROR_DATA_INVALID |
| 1761 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1762 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1763 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1764 | * \return If this function fails, the key slot is an invalid state. |
| 1765 | * 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] | 1766 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1767 | static psa_status_t psa_finish_key_creation( |
| 1768 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1769 | psa_se_drv_table_entry_t *driver, |
| 1770 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1771 | { |
| 1772 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1773 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1774 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1775 | |
| 1776 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1777 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1778 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1779 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1780 | if( driver != NULL ) |
| 1781 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1782 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1783 | psa_key_slot_number_t slot_number = |
| 1784 | psa_key_slot_get_slot_number( slot ) ; |
| 1785 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1786 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1787 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1788 | sizeof( data.slot_number ), |
| 1789 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1790 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1791 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1792 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1793 | (uint8_t*) &data, |
| 1794 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1795 | } |
| 1796 | else |
| 1797 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1798 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1799 | /* Key material is saved in export representation in the slot, so |
| 1800 | * just pass the slot buffer for storage. */ |
| 1801 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1802 | slot->key.data, |
| 1803 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1804 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1805 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1806 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1807 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1808 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1809 | /* Finish the transaction for a key creation. This does not |
| 1810 | * happen when registering an existing key. Detect this case |
| 1811 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1812 | * creation of a persistent key in a secure element requires a transaction, |
| 1813 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1814 | if( driver != NULL && |
| 1815 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1816 | { |
| 1817 | status = psa_save_se_persistent_data( driver ); |
| 1818 | if( status != PSA_SUCCESS ) |
| 1819 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1820 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1821 | return( status ); |
| 1822 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1823 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1824 | } |
| 1825 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1826 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1827 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1828 | { |
| 1829 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1830 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1831 | if( status != PSA_SUCCESS ) |
| 1832 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1833 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1834 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1835 | return( status ); |
| 1836 | } |
| 1837 | |
| 1838 | /** Abort the creation of a key. |
| 1839 | * |
| 1840 | * You may call this function after calling psa_start_key_creation(), |
| 1841 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1842 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1843 | * See the documentation of psa_start_key_creation() for the intended use |
| 1844 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1845 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1846 | * \param[in,out] slot Pointer to the slot with key material. |
| 1847 | * \param[in] driver The secure element driver for the key, |
| 1848 | * or NULL for a transparent key. |
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 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1851 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1852 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1853 | (void) driver; |
| 1854 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1855 | if( slot == NULL ) |
| 1856 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1857 | |
| 1858 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1859 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1860 | * element, and the failure happened later (when saving metadata |
| 1861 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1862 | * element. |
| 1863 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1864 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1865 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1866 | /* Abort the ongoing transaction if any (there may not be one if |
| 1867 | * the creation process failed before starting one, or if the |
| 1868 | * key creation is a registration of a key in a secure element). |
| 1869 | * Earlier functions must already have done what it takes to undo any |
| 1870 | * partial creation. All that's left is to update the transaction data |
| 1871 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1872 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1873 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1874 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1875 | psa_wipe_key_slot( slot ); |
| 1876 | } |
| 1877 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1878 | /** Validate optional attributes during key creation. |
| 1879 | * |
| 1880 | * Some key attributes are optional during key creation. If they are |
| 1881 | * specified in the attributes structure, check that they are consistent |
| 1882 | * with the data in the slot. |
| 1883 | * |
| 1884 | * This function should be called near the end of key creation, after |
| 1885 | * the slot in memory is fully populated but before saving persistent data. |
| 1886 | */ |
| 1887 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1888 | const psa_key_slot_t *slot, |
| 1889 | const psa_key_attributes_t *attributes ) |
| 1890 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1891 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1892 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1893 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1894 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1895 | } |
| 1896 | |
| 1897 | if( attributes->domain_parameters_size != 0 ) |
| 1898 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1899 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1900 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1901 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1902 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1903 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1904 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1905 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1906 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1907 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1908 | slot->attr.type, |
| 1909 | slot->key.data, |
| 1910 | slot->key.bytes, |
| 1911 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1912 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1913 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1914 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1915 | mbedtls_mpi_init( &actual ); |
| 1916 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1917 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1918 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1919 | mbedtls_rsa_free( rsa ); |
| 1920 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1921 | if( ret != 0 ) |
| 1922 | goto rsa_exit; |
| 1923 | ret = mbedtls_mpi_read_binary( &required, |
| 1924 | attributes->domain_parameters, |
| 1925 | attributes->domain_parameters_size ); |
| 1926 | if( ret != 0 ) |
| 1927 | goto rsa_exit; |
| 1928 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1929 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1930 | rsa_exit: |
| 1931 | mbedtls_mpi_free( &actual ); |
| 1932 | mbedtls_mpi_free( &required ); |
| 1933 | if( ret != 0) |
| 1934 | return( mbedtls_to_psa_error( ret ) ); |
| 1935 | } |
| 1936 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1937 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1938 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1939 | { |
| 1940 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1941 | } |
| 1942 | } |
| 1943 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1944 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1945 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1946 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1947 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1948 | } |
| 1949 | |
| 1950 | return( PSA_SUCCESS ); |
| 1951 | } |
| 1952 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1953 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1955 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1956 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1957 | { |
| 1958 | psa_status_t status; |
| 1959 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1960 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1961 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1962 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1963 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1964 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1965 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1966 | * This also rejects any key which might be encoded as an empty string, |
| 1967 | * which is never valid. */ |
| 1968 | if( data_length == 0 ) |
| 1969 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1970 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1971 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1972 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1973 | if( status != PSA_SUCCESS ) |
| 1974 | goto exit; |
| 1975 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1976 | /* In the case of a transparent key or an opaque key stored in local |
| 1977 | * storage (thus not in the case of generating a key in a secure element |
| 1978 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1979 | * hold the generated key material. */ |
| 1980 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1981 | { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1982 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1983 | if( status != PSA_SUCCESS ) |
| 1984 | goto exit; |
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 | |
| 1987 | bits = slot->attr.bits; |
| 1988 | status = psa_driver_wrapper_import_key( attributes, |
| 1989 | data, data_length, |
| 1990 | slot->key.data, |
| 1991 | slot->key.bytes, |
| 1992 | &slot->key.bytes, &bits ); |
| 1993 | if( status != PSA_SUCCESS ) |
| 1994 | goto exit; |
| 1995 | |
| 1996 | if( slot->attr.bits == 0 ) |
| 1997 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1998 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1999 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2000 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2001 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2002 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2003 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2004 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2005 | if( status != PSA_SUCCESS ) |
| 2006 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2007 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2008 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2009 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2010 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2011 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2012 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2013 | return( status ); |
| 2014 | } |
| 2015 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2016 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2017 | psa_status_t mbedtls_psa_register_se_key( |
| 2018 | const psa_key_attributes_t *attributes ) |
| 2019 | { |
| 2020 | psa_status_t status; |
| 2021 | psa_key_slot_t *slot = NULL; |
| 2022 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2023 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2024 | |
| 2025 | /* Leaving attributes unspecified is not currently supported. |
| 2026 | * It could make sense to query the key type and size from the |
| 2027 | * secure element, but not all secure elements support this |
| 2028 | * and the driver HAL doesn't currently support it. */ |
| 2029 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2030 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2031 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2032 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2033 | |
| 2034 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2035 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2036 | if( status != PSA_SUCCESS ) |
| 2037 | goto exit; |
| 2038 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2039 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2040 | |
| 2041 | exit: |
| 2042 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2043 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2044 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2045 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2046 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2047 | return( status ); |
| 2048 | } |
| 2049 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2050 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2051 | 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] | 2052 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2053 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2054 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2055 | source->key.data, |
| 2056 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2057 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2058 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2059 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2060 | target->attr.type = source->attr.type; |
| 2061 | target->attr.bits = source->attr.bits; |
| 2062 | |
| 2063 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2066 | 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] | 2067 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2068 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2069 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2070 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2071 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2072 | psa_key_slot_t *source_slot = NULL; |
| 2073 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2074 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2075 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2076 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2077 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2078 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2079 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2080 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2081 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2082 | goto exit; |
| 2083 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2084 | status = psa_validate_optional_attributes( source_slot, |
| 2085 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2086 | if( status != PSA_SUCCESS ) |
| 2087 | goto exit; |
| 2088 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2089 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2090 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2091 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2092 | if( status != PSA_SUCCESS ) |
| 2093 | goto exit; |
| 2094 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2095 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2096 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2097 | if( status != PSA_SUCCESS ) |
| 2098 | goto exit; |
| 2099 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2100 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2101 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2102 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2103 | /* Copying to a secure element is not implemented yet. */ |
| 2104 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2105 | goto exit; |
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 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2108 | |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2109 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2110 | { |
| 2111 | /* |
| 2112 | * Copying through an opaque driver is not implemented yet, consider |
| 2113 | * a lifetime with an external location as an invalid parameter for |
| 2114 | * now. |
| 2115 | */ |
| 2116 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2117 | goto exit; |
| 2118 | } |
| 2119 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2120 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2121 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2122 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2123 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2124 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2125 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2126 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2127 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2128 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2129 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2130 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2131 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2134 | |
| 2135 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2136 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2137 | /* Message digests */ |
| 2138 | /****************************************************************/ |
| 2139 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2140 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2141 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2142 | /* Aborting a non-active operation is allowed */ |
| 2143 | if( operation->id == 0 ) |
| 2144 | return( PSA_SUCCESS ); |
| 2145 | |
| 2146 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2147 | operation->id = 0; |
| 2148 | |
| 2149 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2150 | } |
| 2151 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2152 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2153 | psa_algorithm_t alg ) |
| 2154 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2155 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2156 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2157 | /* A context must be freshly initialized before it can be set up. */ |
| 2158 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2159 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2160 | status = PSA_ERROR_BAD_STATE; |
| 2161 | goto exit; |
| 2162 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2163 | |
| 2164 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2165 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2166 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2167 | goto exit; |
| 2168 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2169 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2170 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2171 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2172 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2173 | |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2174 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2175 | |
| 2176 | exit: |
| 2177 | if( status != PSA_SUCCESS ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2178 | psa_hash_abort( operation ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2179 | |
| 2180 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2184 | const uint8_t *input, |
| 2185 | size_t input_length ) |
| 2186 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2187 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2188 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2189 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2190 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2191 | status = PSA_ERROR_BAD_STATE; |
| 2192 | goto exit; |
| 2193 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2194 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2195 | /* Don't require hash implementations to behave correctly on a |
| 2196 | * zero-length input, which may have an invalid pointer. */ |
| 2197 | if( input_length == 0 ) |
| 2198 | return( PSA_SUCCESS ); |
| 2199 | |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2200 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2201 | |
| 2202 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2203 | if( status != PSA_SUCCESS ) |
| 2204 | psa_hash_abort( operation ); |
| 2205 | |
| 2206 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2210 | uint8_t *hash, |
| 2211 | size_t hash_size, |
| 2212 | size_t *hash_length ) |
| 2213 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2214 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2215 | if( operation->id == 0 ) |
| 2216 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2217 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2218 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2219 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2220 | psa_hash_abort( operation ); |
| 2221 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2224 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2225 | const uint8_t *hash, |
| 2226 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2227 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2228 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2229 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2230 | psa_status_t status = psa_hash_finish( |
| 2231 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2232 | actual_hash, sizeof( actual_hash ), |
| 2233 | &actual_hash_length ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2234 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2235 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2236 | goto exit; |
| 2237 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2238 | if( actual_hash_length != hash_length ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2239 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2240 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2241 | goto exit; |
| 2242 | } |
| 2243 | |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2244 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2245 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2246 | |
| 2247 | exit: |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2248 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2249 | if( status != PSA_SUCCESS ) |
| 2250 | psa_hash_abort(operation); |
| 2251 | |
| 2252 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2255 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2256 | const uint8_t *input, size_t input_length, |
| 2257 | uint8_t *hash, size_t hash_size, |
| 2258 | size_t *hash_length ) |
| 2259 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2260 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2261 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2263 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2264 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2265 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2269 | const uint8_t *input, size_t input_length, |
| 2270 | const uint8_t *hash, size_t hash_length ) |
| 2271 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2272 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2273 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2274 | |
| 2275 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2277 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2278 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2279 | alg, input, input_length, |
| 2280 | actual_hash, sizeof(actual_hash), |
| 2281 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2282 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2283 | goto exit; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2284 | if( actual_hash_length != hash_length ) |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2285 | { |
| 2286 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2287 | goto exit; |
| 2288 | } |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2289 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2290 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2291 | |
| 2292 | exit: |
| 2293 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
| 2294 | return( status ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2295 | } |
| 2296 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2297 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2298 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2299 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2300 | if( source_operation->id == 0 || |
| 2301 | target_operation->id != 0 ) |
| 2302 | { |
| 2303 | return( PSA_ERROR_BAD_STATE ); |
| 2304 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2305 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2306 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2307 | target_operation ); |
| 2308 | if( status != PSA_SUCCESS ) |
| 2309 | psa_hash_abort( target_operation ); |
| 2310 | |
| 2311 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2312 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2313 | |
| 2314 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2315 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2316 | /* MAC */ |
| 2317 | /****************************************************************/ |
| 2318 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2319 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2320 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2321 | /* Aborting a non-active operation is allowed */ |
| 2322 | if( operation->id == 0 ) |
| 2323 | return( PSA_SUCCESS ); |
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 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2326 | operation->mac_size = 0; |
| 2327 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2328 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2329 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2330 | return( status ); |
| 2331 | } |
| 2332 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2333 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2334 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2335 | const psa_key_attributes_t *attributes, |
| 2336 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2337 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2338 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2339 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2340 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2341 | |
Ronald Cron | 48f875e | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2342 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2343 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2344 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2345 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2346 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2347 | if( status != PSA_SUCCESS ) |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2348 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2349 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2350 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2351 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2352 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2353 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2354 | { |
| 2355 | /* A very short MAC is too short for security since it can be |
| 2356 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2357 | * so we make this our minimum, even though 32 bits is still |
| 2358 | * too small for security. */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2359 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2360 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2361 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2362 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2363 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2364 | { |
| 2365 | /* It's impossible to "truncate" to a larger length than the full length |
| 2366 | * of the algorithm. */ |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2367 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2370 | if( *mac_size > PSA_MAC_MAX_SIZE ) |
| 2371 | { |
| 2372 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2373 | * that is disabled in the compile-time configuration. The result can |
| 2374 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2375 | * configuration into account. In this case, force a return of |
| 2376 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2377 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2378 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2379 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2380 | * systematically generated tests. */ |
| 2381 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2382 | } |
| 2383 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2384 | return( PSA_SUCCESS ); |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2388 | mbedtls_svc_key_id_t key, |
| 2389 | psa_algorithm_t alg, |
| 2390 | int is_sign ) |
| 2391 | { |
| 2392 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2393 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2394 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2395 | |
| 2396 | /* A context must be freshly initialized before it can be set up. */ |
| 2397 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2398 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2399 | status = PSA_ERROR_BAD_STATE; |
| 2400 | goto exit; |
| 2401 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2402 | |
| 2403 | status = psa_get_and_lock_key_slot_with_policy( |
| 2404 | key, |
| 2405 | &slot, |
Mateusz Starzyk | dd55b25 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2406 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2407 | alg ); |
| 2408 | if( status != PSA_SUCCESS ) |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2409 | goto exit; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2410 | |
| 2411 | psa_key_attributes_t attributes = { |
| 2412 | .core = slot->attr |
| 2413 | }; |
| 2414 | |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2415 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2416 | &operation->mac_size ); |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2417 | if( status != PSA_SUCCESS ) |
| 2418 | goto exit; |
| 2419 | |
| 2420 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2421 | /* Dispatch the MAC setup call with validated input */ |
| 2422 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2423 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2424 | status = psa_driver_wrapper_mac_sign_setup( operation, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2425 | &attributes, |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2426 | slot->key.data, |
| 2427 | slot->key.bytes, |
| 2428 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2429 | } |
| 2430 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2431 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2432 | status = psa_driver_wrapper_mac_verify_setup( operation, |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2433 | &attributes, |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2434 | slot->key.data, |
| 2435 | slot->key.bytes, |
| 2436 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2437 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2438 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2439 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2440 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2441 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2442 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2443 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2444 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2445 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2446 | } |
| 2447 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2448 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2449 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2450 | psa_algorithm_t alg ) |
| 2451 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2452 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2456 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2457 | psa_algorithm_t alg ) |
| 2458 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2459 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2460 | } |
| 2461 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2462 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2463 | const uint8_t *input, |
| 2464 | size_t input_length ) |
| 2465 | { |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2466 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2467 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2468 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2469 | /* Don't require hash implementations to behave correctly on a |
| 2470 | * zero-length input, which may have an invalid pointer. */ |
| 2471 | if( input_length == 0 ) |
| 2472 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2473 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2474 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2475 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2476 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2477 | psa_mac_abort( operation ); |
| 2478 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2479 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2482 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2483 | uint8_t *mac, |
| 2484 | size_t mac_size, |
| 2485 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2486 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2487 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2488 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2489 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2490 | if( operation->id == 0 ) |
Dave Rodgman | d73e1b0 | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2491 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2492 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2493 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2494 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2495 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2496 | if( ! operation->is_sign ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2497 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2498 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2499 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2500 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2501 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2502 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2503 | * once all the error checks are done. */ |
| 2504 | if( operation->mac_size == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2505 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2506 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2507 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2508 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2509 | |
| 2510 | if( mac_size < operation->mac_size ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2511 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2512 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2513 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2514 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2515 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2516 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2517 | mac, operation->mac_size, |
| 2518 | mac_length ); |
| 2519 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2520 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2521 | /* In case of success, set the potential excess room in the output buffer |
| 2522 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2523 | * In case of error, set the output length and content to a safe default, |
| 2524 | * such that in case the caller misses an error check, the output would be |
| 2525 | * an unachievable MAC. |
| 2526 | */ |
| 2527 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2528 | { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2529 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2530 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2531 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2532 | |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2533 | if( mac_size > operation->mac_size ) |
| 2534 | memset( &mac[operation->mac_size], '!', |
| 2535 | mac_size - operation->mac_size ); |
| 2536 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2537 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2538 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2539 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2542 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2543 | const uint8_t *mac, |
| 2544 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2545 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2546 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2547 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2548 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2549 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2550 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2551 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2552 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2553 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2554 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2555 | if( operation->is_sign ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2556 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2557 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2558 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2559 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2560 | |
| 2561 | if( operation->mac_size != mac_length ) |
| 2562 | { |
| 2563 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2564 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2565 | } |
| 2566 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2567 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2568 | mac, mac_length ); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2569 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2570 | exit: |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2571 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2572 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2573 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2574 | } |
| 2575 | |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2576 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2577 | psa_algorithm_t alg, |
| 2578 | const uint8_t *input, |
| 2579 | size_t input_length, |
| 2580 | uint8_t *mac, |
| 2581 | size_t mac_size, |
| 2582 | size_t *mac_length, |
| 2583 | int is_sign ) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2584 | { |
| 2585 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2586 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2587 | psa_key_slot_t *slot; |
| 2588 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2589 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2590 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | dd55b25 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2591 | key, |
| 2592 | &slot, |
| 2593 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2594 | alg ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2595 | if( status != PSA_SUCCESS ) |
| 2596 | goto exit; |
| 2597 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2598 | psa_key_attributes_t attributes = { |
| 2599 | .core = slot->attr |
| 2600 | }; |
| 2601 | |
| 2602 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2603 | &operation_mac_size ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2604 | if( status != PSA_SUCCESS ) |
| 2605 | goto exit; |
| 2606 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2607 | if( mac_size < operation_mac_size ) |
| 2608 | { |
| 2609 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2610 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | status = psa_driver_wrapper_mac_compute( |
| 2614 | &attributes, |
| 2615 | slot->key.data, slot->key.bytes, |
| 2616 | alg, |
| 2617 | input, input_length, |
| 2618 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2619 | |
| 2620 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2621 | /* In case of success, set the potential excess room in the output buffer |
| 2622 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2623 | * In case of error, set the output length and content to a safe default, |
| 2624 | * such that in case the caller misses an error check, the output would be |
| 2625 | * an unachievable MAC. |
| 2626 | */ |
| 2627 | if( status != PSA_SUCCESS ) |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2628 | { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2629 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2630 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2631 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2632 | if( mac_size > operation_mac_size ) |
| 2633 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2634 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2635 | unlock_status = psa_unlock_key_slot( slot ); |
| 2636 | |
| 2637 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2638 | } |
| 2639 | |
Ronald Cron | ef0d8f1 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2640 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2641 | psa_algorithm_t alg, |
| 2642 | const uint8_t *input, |
| 2643 | size_t input_length, |
| 2644 | uint8_t *mac, |
| 2645 | size_t mac_size, |
| 2646 | size_t *mac_length) |
| 2647 | { |
| 2648 | return( psa_mac_compute_internal( key, alg, |
| 2649 | input, input_length, |
| 2650 | mac, mac_size, mac_length, 1 ) ); |
| 2651 | } |
| 2652 | |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2653 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2654 | psa_algorithm_t alg, |
| 2655 | const uint8_t *input, |
| 2656 | size_t input_length, |
| 2657 | const uint8_t *mac, |
| 2658 | size_t mac_length) |
| 2659 | { |
| 2660 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2661 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2662 | size_t actual_mac_length; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2663 | |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2664 | status = psa_mac_compute_internal( key, alg, |
| 2665 | input, input_length, |
| 2666 | actual_mac, sizeof( actual_mac ), |
| 2667 | &actual_mac_length, 0 ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2668 | if( status != PSA_SUCCESS ) |
| 2669 | goto exit; |
| 2670 | |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2671 | if( mac_length != actual_mac_length ) |
| 2672 | { |
| 2673 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2674 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2675 | } |
| 2676 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2677 | { |
| 2678 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2679 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2680 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2681 | |
| 2682 | exit: |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2683 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2684 | |
| 2685 | return ( status ); |
| 2686 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2687 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2688 | /****************************************************************/ |
| 2689 | /* Asymmetric cryptography */ |
| 2690 | /****************************************************************/ |
| 2691 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2692 | 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] | 2693 | psa_algorithm_t alg ) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2694 | { |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2695 | if( input_is_message ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2696 | { |
| 2697 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2698 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2699 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2700 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2701 | { |
| 2702 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2703 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2704 | } |
| 2705 | } |
| 2706 | else |
| 2707 | { |
Mateusz Starzyk | d22362c | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2708 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2709 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2710 | } |
| 2711 | |
| 2712 | return( PSA_SUCCESS ); |
| 2713 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2714 | |
| 2715 | 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] | 2716 | int input_is_message, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2717 | psa_algorithm_t alg, |
| 2718 | const uint8_t * input, |
| 2719 | size_t input_length, |
| 2720 | uint8_t * signature, |
| 2721 | size_t signature_size, |
| 2722 | size_t * signature_length ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2723 | { |
| 2724 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2725 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2726 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2727 | |
| 2728 | *signature_length = 0; |
| 2729 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2730 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2731 | if( status != PSA_SUCCESS ) |
| 2732 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2733 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2734 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2735 | * 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] | 2736 | * buffer can in principle be empty since it doesn't actually have |
| 2737 | * to be a hash.) */ |
| 2738 | if( signature_size == 0 ) |
| 2739 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2740 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2741 | status = psa_get_and_lock_key_slot_with_policy( |
| 2742 | key, &slot, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2743 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2744 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2745 | alg ); |
| 2746 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2747 | if( status != PSA_SUCCESS ) |
| 2748 | goto exit; |
| 2749 | |
| 2750 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2751 | { |
| 2752 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2753 | goto exit; |
| 2754 | } |
| 2755 | |
| 2756 | psa_key_attributes_t attributes = { |
| 2757 | .core = slot->attr |
| 2758 | }; |
| 2759 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2760 | if( input_is_message ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2761 | { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2762 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2763 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2764 | alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2765 | signature, signature_size, signature_length ); |
| 2766 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2767 | else |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2768 | { |
| 2769 | |
| 2770 | status = psa_driver_wrapper_sign_hash( |
| 2771 | &attributes, slot->key.data, slot->key.bytes, |
| 2772 | alg, input, input_length, |
| 2773 | signature, signature_size, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2776 | |
| 2777 | exit: |
| 2778 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2779 | * the trailing part on success) with something that isn't a valid signature |
| 2780 | * (barring an attack on the signature and deliberately-crafted input), |
| 2781 | * in case the caller doesn't check the return status properly. */ |
| 2782 | if( status == PSA_SUCCESS ) |
| 2783 | memset( signature + *signature_length, '!', |
| 2784 | signature_size - *signature_length ); |
| 2785 | else |
| 2786 | memset( signature, '!', signature_size ); |
| 2787 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2788 | * memset because signature may be NULL in this case. */ |
| 2789 | |
| 2790 | unlock_status = psa_unlock_key_slot( slot ); |
| 2791 | |
| 2792 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2793 | } |
| 2794 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2795 | 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] | 2796 | int input_is_message, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2797 | psa_algorithm_t alg, |
| 2798 | const uint8_t * input, |
| 2799 | size_t input_length, |
| 2800 | const uint8_t * signature, |
| 2801 | size_t signature_length ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2802 | { |
| 2803 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2804 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2805 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2806 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2807 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2808 | if( status != PSA_SUCCESS ) |
| 2809 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2810 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2811 | status = psa_get_and_lock_key_slot_with_policy( |
| 2812 | key, &slot, |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2813 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2814 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2815 | alg ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2816 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2817 | if( status != PSA_SUCCESS ) |
| 2818 | return( status ); |
| 2819 | |
| 2820 | psa_key_attributes_t attributes = { |
| 2821 | .core = slot->attr |
| 2822 | }; |
| 2823 | |
gabor-mezei-arm | c979578 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2824 | if( input_is_message ) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2825 | { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2826 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2827 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2828 | alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2829 | signature, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2830 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2831 | else |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2832 | { |
| 2833 | status = psa_driver_wrapper_verify_hash( |
| 2834 | &attributes, slot->key.data, slot->key.bytes, |
| 2835 | alg, input, input_length, |
| 2836 | signature, signature_length ); |
| 2837 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2838 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2839 | unlock_status = psa_unlock_key_slot( slot ); |
| 2840 | |
| 2841 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2842 | |
| 2843 | } |
| 2844 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2845 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2846 | const psa_key_attributes_t *attributes, |
| 2847 | const uint8_t *key_buffer, |
| 2848 | size_t key_buffer_size, |
| 2849 | psa_algorithm_t alg, |
| 2850 | const uint8_t *input, |
| 2851 | size_t input_length, |
| 2852 | uint8_t *signature, |
| 2853 | size_t signature_size, |
| 2854 | size_t *signature_length ) |
| 2855 | { |
| 2856 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2857 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2858 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2859 | { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2860 | size_t hash_length; |
| 2861 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2862 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2863 | status = psa_driver_wrapper_hash_compute( |
| 2864 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2865 | input, input_length, |
| 2866 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2867 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2868 | if( status != PSA_SUCCESS ) |
| 2869 | return status; |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2870 | |
| 2871 | return psa_driver_wrapper_sign_hash( |
| 2872 | attributes, key_buffer, key_buffer_size, |
| 2873 | alg, hash, hash_length, |
| 2874 | signature, signature_size, signature_length ); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2875 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2876 | |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2877 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2878 | } |
| 2879 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2880 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2881 | psa_algorithm_t alg, |
| 2882 | const uint8_t * input, |
| 2883 | size_t input_length, |
| 2884 | uint8_t * signature, |
| 2885 | size_t signature_size, |
| 2886 | size_t * signature_length ) |
| 2887 | { |
| 2888 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2889 | key, 1, alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2890 | signature, signature_size, signature_length ); |
| 2891 | } |
| 2892 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2893 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2894 | const psa_key_attributes_t *attributes, |
| 2895 | const uint8_t *key_buffer, |
| 2896 | size_t key_buffer_size, |
| 2897 | psa_algorithm_t alg, |
| 2898 | const uint8_t *input, |
| 2899 | size_t input_length, |
| 2900 | const uint8_t *signature, |
| 2901 | size_t signature_length ) |
| 2902 | { |
| 2903 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2904 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2905 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2906 | { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2907 | size_t hash_length; |
| 2908 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2909 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2910 | status = psa_driver_wrapper_hash_compute( |
| 2911 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2912 | input, input_length, |
| 2913 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2914 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2915 | if( status != PSA_SUCCESS ) |
| 2916 | return status; |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2917 | |
| 2918 | return psa_driver_wrapper_verify_hash( |
| 2919 | attributes, key_buffer, key_buffer_size, |
| 2920 | alg, hash, hash_length, |
| 2921 | signature, signature_length ); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2922 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2923 | |
gabor-mezei-arm | ef6f2aa | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2924 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2925 | } |
| 2926 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2927 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2928 | psa_algorithm_t alg, |
| 2929 | const uint8_t * input, |
| 2930 | size_t input_length, |
| 2931 | const uint8_t * signature, |
| 2932 | size_t signature_length ) |
| 2933 | { |
| 2934 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2935 | key, 1, alg, input, input_length, |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2936 | signature, signature_length ); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2937 | } |
| 2938 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2939 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2940 | const psa_key_attributes_t *attributes, |
| 2941 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2942 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2943 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2944 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2945 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2946 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2947 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2948 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2949 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2950 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2951 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2952 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2953 | attributes, |
| 2954 | key_buffer, key_buffer_size, |
| 2955 | alg, hash, hash_length, |
| 2956 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2957 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2958 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2959 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2960 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2961 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2962 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2963 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2964 | } |
Ronald Cron | 4bcccc6 | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2965 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2966 | { |
| 2967 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2968 | { |
| 2969 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2970 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2971 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2972 | attributes, |
| 2973 | key_buffer, key_buffer_size, |
| 2974 | alg, hash, hash_length, |
| 2975 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2976 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2977 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2978 | } |
| 2979 | else |
| 2980 | { |
| 2981 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2982 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2983 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2984 | |
| 2985 | (void)key_buffer; |
| 2986 | (void)key_buffer_size; |
| 2987 | (void)hash; |
| 2988 | (void)hash_length; |
| 2989 | (void)signature; |
| 2990 | (void)signature_size; |
| 2991 | (void)signature_length; |
| 2992 | |
| 2993 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2994 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2995 | |
| 2996 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2997 | psa_algorithm_t alg, |
| 2998 | const uint8_t *hash, |
| 2999 | size_t hash_length, |
| 3000 | uint8_t *signature, |
| 3001 | size_t signature_size, |
| 3002 | size_t *signature_length ) |
| 3003 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3004 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3005 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3006 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3007 | } |
| 3008 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3009 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3010 | const psa_key_attributes_t *attributes, |
| 3011 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3012 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3013 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3014 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 3015 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3016 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3017 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 3018 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3019 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3020 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3021 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3022 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 3023 | attributes, |
| 3024 | key_buffer, key_buffer_size, |
| 3025 | alg, hash, hash_length, |
| 3026 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3027 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3028 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3029 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3030 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3031 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3032 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3033 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3034 | } |
Ronald Cron | 4bcccc6 | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 3035 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3036 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3037 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 3038 | { |
| 3039 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3040 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3041 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3042 | attributes, |
| 3043 | key_buffer, key_buffer_size, |
| 3044 | alg, hash, hash_length, |
| 3045 | signature, signature_length ) ); |
| 3046 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3047 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3048 | } |
| 3049 | else |
| 3050 | { |
| 3051 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3052 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3053 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3054 | |
| 3055 | (void)key_buffer; |
| 3056 | (void)key_buffer_size; |
| 3057 | (void)hash; |
| 3058 | (void)hash_length; |
| 3059 | (void)signature; |
| 3060 | (void)signature_length; |
| 3061 | |
| 3062 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3063 | } |
| 3064 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3065 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3066 | psa_algorithm_t alg, |
| 3067 | const uint8_t *hash, |
| 3068 | size_t hash_length, |
| 3069 | const uint8_t *signature, |
| 3070 | size_t signature_length ) |
| 3071 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3072 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3073 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3074 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3075 | } |
| 3076 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3077 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3078 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3079 | mbedtls_rsa_context *rsa ) |
| 3080 | { |
| 3081 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3082 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3083 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3084 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3085 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3086 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3087 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3088 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3089 | psa_algorithm_t alg, |
| 3090 | const uint8_t *input, |
| 3091 | size_t input_length, |
| 3092 | const uint8_t *salt, |
| 3093 | size_t salt_length, |
| 3094 | uint8_t *output, |
| 3095 | size_t output_size, |
| 3096 | size_t *output_length ) |
| 3097 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3098 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3099 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3100 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3101 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3102 | (void) input; |
| 3103 | (void) input_length; |
| 3104 | (void) salt; |
| 3105 | (void) output; |
| 3106 | (void) output_size; |
| 3107 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3108 | *output_length = 0; |
| 3109 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3110 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3111 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3112 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3113 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3114 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3115 | if( status != PSA_SUCCESS ) |
| 3116 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3117 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3118 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3119 | { |
| 3120 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3121 | goto exit; |
| 3122 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3123 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3124 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3125 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3126 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3127 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3128 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3129 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3130 | slot->key.data, |
| 3131 | slot->key.bytes, |
| 3132 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3133 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3134 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3135 | |
| 3136 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3137 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3138 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3139 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3140 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3141 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3142 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3143 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
| 3144 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3145 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3146 | status = mbedtls_to_psa_error( |
| 3147 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3148 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3149 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3150 | MBEDTLS_RSA_PUBLIC, |
| 3151 | input_length, |
| 3152 | input, |
| 3153 | output ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3154 | #else |
| 3155 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3156 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3157 | } |
| 3158 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3159 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
| 3160 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3161 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3162 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3163 | status = mbedtls_to_psa_error( |
| 3164 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3165 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3166 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3167 | MBEDTLS_RSA_PUBLIC, |
| 3168 | salt, salt_length, |
| 3169 | input_length, |
| 3170 | input, |
| 3171 | output ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3172 | #else |
| 3173 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3174 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3175 | } |
| 3176 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3177 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3178 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3179 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3180 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3181 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3182 | rsa_exit: |
| 3183 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3184 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3185 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3186 | mbedtls_rsa_free( rsa ); |
| 3187 | mbedtls_free( rsa ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3188 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3189 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3190 | } |
| 3191 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3192 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3193 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3194 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3195 | |
| 3196 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3197 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3198 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3199 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3200 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3201 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3202 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3203 | psa_algorithm_t alg, |
| 3204 | const uint8_t *input, |
| 3205 | size_t input_length, |
| 3206 | const uint8_t *salt, |
| 3207 | size_t salt_length, |
| 3208 | uint8_t *output, |
| 3209 | size_t output_size, |
| 3210 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3211 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3212 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3213 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3214 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3215 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3216 | (void) input; |
| 3217 | (void) input_length; |
| 3218 | (void) salt; |
| 3219 | (void) output; |
| 3220 | (void) output_size; |
| 3221 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3222 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3223 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3224 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3225 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3226 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3227 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3228 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3229 | if( status != PSA_SUCCESS ) |
| 3230 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3231 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3232 | { |
| 3233 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3234 | goto exit; |
| 3235 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3236 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3237 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3238 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3239 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3240 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3241 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3242 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3243 | slot->key.data, |
| 3244 | slot->key.bytes, |
| 3245 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3246 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3247 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3248 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3249 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3250 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3251 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3252 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3253 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3254 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3255 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3256 | |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3257 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3258 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3259 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3260 | status = mbedtls_to_psa_error( |
| 3261 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3262 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3263 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3264 | MBEDTLS_RSA_PRIVATE, |
| 3265 | output_length, |
| 3266 | input, |
| 3267 | output, |
| 3268 | output_size ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3269 | #else |
| 3270 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3271 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3272 | } |
| 3273 | else |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3274 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3275 | { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3276 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3277 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3278 | status = mbedtls_to_psa_error( |
| 3279 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3280 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3281 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3282 | MBEDTLS_RSA_PRIVATE, |
| 3283 | salt, salt_length, |
| 3284 | output_length, |
| 3285 | input, |
| 3286 | output, |
| 3287 | output_size ) ); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3288 | #else |
| 3289 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3290 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3291 | } |
| 3292 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3293 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3294 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3295 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3296 | |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3297 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3298 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3299 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3300 | mbedtls_rsa_free( rsa ); |
| 3301 | mbedtls_free( rsa ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3302 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3303 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3304 | } |
| 3305 | else |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3306 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3307 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3308 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3309 | |
| 3310 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3311 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3312 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3313 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3314 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3315 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3316 | |
| 3317 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3318 | /****************************************************************/ |
| 3319 | /* Symmetric cryptography */ |
| 3320 | /****************************************************************/ |
| 3321 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3322 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3323 | mbedtls_svc_key_id_t key, |
| 3324 | psa_algorithm_t alg, |
| 3325 | mbedtls_operation_t cipher_operation ) |
| 3326 | { |
| 3327 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3328 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3329 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3330 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3331 | PSA_KEY_USAGE_ENCRYPT : |
| 3332 | PSA_KEY_USAGE_DECRYPT ); |
| 3333 | |
| 3334 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3335 | if( operation->id != 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3336 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3337 | status = PSA_ERROR_BAD_STATE; |
| 3338 | goto exit; |
| 3339 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3340 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3341 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3342 | { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3343 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3344 | goto exit; |
| 3345 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3346 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3347 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3348 | if( status != PSA_SUCCESS ) |
| 3349 | goto exit; |
| 3350 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3351 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3352 | * 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] | 3353 | * so we only set it (in the driver wrapper) after resources have been |
| 3354 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3355 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3356 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3357 | operation->iv_required = 0; |
Gilles Peskine | 4a83c10 | 2022-04-20 17:00:22 +0200 | [diff] [blame] | 3358 | else if( slot->attr.type == PSA_KEY_TYPE_ARC4 ) |
| 3359 | operation->iv_required = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3360 | else |
| 3361 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3362 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3363 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3364 | psa_key_attributes_t attributes = { |
| 3365 | .core = slot->attr |
| 3366 | }; |
| 3367 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3368 | /* Try doing the operation through a driver before using software fallback. */ |
| 3369 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3370 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3371 | &attributes, |
| 3372 | slot->key.data, |
| 3373 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3374 | alg ); |
| 3375 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3376 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3377 | &attributes, |
| 3378 | slot->key.data, |
| 3379 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3380 | alg ); |
| 3381 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3382 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3383 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3384 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3385 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3386 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3387 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3388 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3391 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3392 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3393 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3394 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3395 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3398 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3399 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3400 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3401 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3402 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3403 | } |
| 3404 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3405 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3406 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3407 | size_t iv_size, |
| 3408 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3409 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3410 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3411 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3412 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3413 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3414 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3415 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3416 | status = PSA_ERROR_BAD_STATE; |
| 3417 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3418 | } |
| 3419 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3420 | if( operation->iv_set || ! operation->iv_required ) |
| 3421 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3422 | status = PSA_ERROR_BAD_STATE; |
| 3423 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3424 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3425 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3426 | default_iv_length = operation->default_iv_length; |
| 3427 | if( iv_size < default_iv_length ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3428 | { |
| 3429 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3430 | goto exit; |
| 3431 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3432 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3433 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3434 | { |
| 3435 | status = PSA_ERROR_GENERIC_ERROR; |
| 3436 | goto exit; |
| 3437 | } |
| 3438 | |
| 3439 | status = psa_generate_random( local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3440 | if( status != PSA_SUCCESS ) |
| 3441 | goto exit; |
| 3442 | |
| 3443 | status = psa_driver_wrapper_cipher_set_iv( operation, |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3444 | local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3445 | |
| 3446 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3447 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3448 | { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3449 | memcpy( iv, local_iv, default_iv_length ); |
| 3450 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3451 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3452 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3453 | else |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3454 | { |
| 3455 | *iv_length = 0; |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3456 | psa_cipher_abort( operation ); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3457 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3458 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3459 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3460 | } |
| 3461 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3462 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3463 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3464 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3465 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3466 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3467 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3468 | if( operation->id == 0 ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3469 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3470 | status = PSA_ERROR_BAD_STATE; |
| 3471 | goto exit; |
| 3472 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3473 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3474 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3475 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3476 | status = PSA_ERROR_BAD_STATE; |
| 3477 | goto exit; |
| 3478 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3479 | |
| 3480 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | db86179 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3481 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3482 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3483 | goto exit; |
| 3484 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3485 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3486 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3487 | iv, |
| 3488 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3489 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3490 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3491 | if( status == PSA_SUCCESS ) |
| 3492 | operation->iv_set = 1; |
| 3493 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3494 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3495 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3496 | } |
| 3497 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3498 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3499 | const uint8_t *input, |
| 3500 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3501 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3502 | size_t output_size, |
| 3503 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3504 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3505 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3506 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3507 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3508 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3509 | status = PSA_ERROR_BAD_STATE; |
| 3510 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3511 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3512 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3513 | if( operation->iv_required && ! operation->iv_set ) |
| 3514 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3515 | status = PSA_ERROR_BAD_STATE; |
| 3516 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3517 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3518 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3519 | status = psa_driver_wrapper_cipher_update( operation, |
| 3520 | input, |
| 3521 | input_length, |
| 3522 | output, |
| 3523 | output_size, |
| 3524 | output_length ); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3525 | |
| 3526 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3527 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3528 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3529 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3530 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3531 | } |
| 3532 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3533 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3534 | uint8_t *output, |
| 3535 | size_t output_size, |
| 3536 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3537 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3538 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3539 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3540 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3541 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3542 | status = PSA_ERROR_BAD_STATE; |
| 3543 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3544 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3545 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3546 | if( operation->iv_required && ! operation->iv_set ) |
| 3547 | { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3548 | status = PSA_ERROR_BAD_STATE; |
| 3549 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3550 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3551 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3552 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3553 | output, |
| 3554 | output_size, |
| 3555 | output_length ); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3556 | |
| 3557 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3558 | if( status == PSA_SUCCESS ) |
| 3559 | return( psa_cipher_abort( operation ) ); |
| 3560 | else |
| 3561 | { |
| 3562 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3563 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3564 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3565 | return( status ); |
| 3566 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3569 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3570 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3571 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3572 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3573 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3574 | * in use. It's ok to call abort on such an object, and there's |
| 3575 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3576 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3577 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3578 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3579 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3580 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3581 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3582 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3583 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3584 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3585 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3586 | } |
| 3587 | |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3588 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3589 | psa_algorithm_t alg, |
| 3590 | const uint8_t *input, |
| 3591 | size_t input_length, |
| 3592 | uint8_t *output, |
| 3593 | size_t output_size, |
| 3594 | size_t *output_length ) |
| 3595 | { |
| 3596 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3597 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3598 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3599 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3600 | size_t default_iv_length = 0; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3601 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3602 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3603 | { |
| 3604 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3605 | goto exit; |
| 3606 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3607 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3608 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3609 | PSA_KEY_USAGE_ENCRYPT, |
| 3610 | alg ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3611 | if( status != PSA_SUCCESS ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3612 | goto exit; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3613 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3614 | psa_key_attributes_t attributes = { |
| 3615 | .core = slot->attr |
| 3616 | }; |
| 3617 | |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3618 | default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
| 3619 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3620 | { |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3621 | status = PSA_ERROR_GENERIC_ERROR; |
| 3622 | goto exit; |
| 3623 | } |
| 3624 | |
| 3625 | if( default_iv_length > 0 ) |
| 3626 | { |
| 3627 | if( output_size < default_iv_length ) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3628 | { |
| 3629 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3630 | goto exit; |
| 3631 | } |
| 3632 | |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3633 | status = psa_generate_random( local_iv, default_iv_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3634 | if( status != PSA_SUCCESS ) |
| 3635 | goto exit; |
| 3636 | } |
| 3637 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3638 | status = psa_driver_wrapper_cipher_encrypt( |
| 3639 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3640 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 3641 | mbedtls_buffer_offset( output, default_iv_length ), |
| 3642 | output_size - default_iv_length, output_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3643 | |
| 3644 | exit: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3645 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3646 | if( status == PSA_SUCCESS ) |
| 3647 | status = unlock_status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3648 | |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3649 | if( status == PSA_SUCCESS ) |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3650 | { |
| 3651 | if( default_iv_length > 0 ) |
| 3652 | memcpy( output, local_iv, default_iv_length ); |
| 3653 | *output_length += default_iv_length; |
| 3654 | } |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3655 | else |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3656 | *output_length = 0; |
| 3657 | |
| 3658 | return( status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3662 | psa_algorithm_t alg, |
| 3663 | const uint8_t *input, |
| 3664 | size_t input_length, |
| 3665 | uint8_t *output, |
| 3666 | size_t output_size, |
| 3667 | size_t *output_length ) |
| 3668 | { |
| 3669 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3670 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3671 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3672 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3673 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3674 | { |
| 3675 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3676 | goto exit; |
| 3677 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3678 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3679 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3680 | PSA_KEY_USAGE_DECRYPT, |
| 3681 | alg ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3682 | if( status != PSA_SUCCESS ) |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3683 | goto exit; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3684 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3685 | psa_key_attributes_t attributes = { |
| 3686 | .core = slot->attr |
| 3687 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3688 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3689 | if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) ) |
| 3690 | { |
| 3691 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3692 | goto exit; |
| 3693 | } |
| 3694 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3695 | status = psa_driver_wrapper_cipher_decrypt( |
| 3696 | &attributes, slot->key.data, slot->key.bytes, |
| 3697 | alg, input, input_length, |
| 3698 | output, output_size, output_length ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3699 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3700 | exit: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3701 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3702 | if( status == PSA_SUCCESS ) |
| 3703 | status = unlock_status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3704 | |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3705 | if( status != PSA_SUCCESS ) |
| 3706 | *output_length = 0; |
| 3707 | |
| 3708 | return( status ); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3712 | /****************************************************************/ |
| 3713 | /* AEAD */ |
| 3714 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3715 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3716 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3717 | psa_algorithm_t alg, |
| 3718 | const uint8_t *nonce, |
| 3719 | size_t nonce_length, |
| 3720 | const uint8_t *additional_data, |
| 3721 | size_t additional_data_length, |
| 3722 | const uint8_t *plaintext, |
| 3723 | size_t plaintext_length, |
| 3724 | uint8_t *ciphertext, |
| 3725 | size_t ciphertext_size, |
| 3726 | size_t *ciphertext_length ) |
| 3727 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3728 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3729 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3730 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3731 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3732 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3733 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3734 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3735 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3736 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3737 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3738 | if( status != PSA_SUCCESS ) |
| 3739 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3740 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3741 | psa_key_attributes_t attributes = { |
| 3742 | .core = slot->attr |
| 3743 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3744 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3745 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3746 | &attributes, slot->key.data, slot->key.bytes, |
| 3747 | alg, |
| 3748 | nonce, nonce_length, |
| 3749 | additional_data, additional_data_length, |
| 3750 | plaintext, plaintext_length, |
| 3751 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3752 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3753 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3754 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3755 | |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3756 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3757 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3758 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3759 | } |
| 3760 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3761 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3762 | psa_algorithm_t alg, |
| 3763 | const uint8_t *nonce, |
| 3764 | size_t nonce_length, |
| 3765 | const uint8_t *additional_data, |
| 3766 | size_t additional_data_length, |
| 3767 | const uint8_t *ciphertext, |
| 3768 | size_t ciphertext_length, |
| 3769 | uint8_t *plaintext, |
| 3770 | size_t plaintext_size, |
| 3771 | size_t *plaintext_length ) |
| 3772 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3773 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3774 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3775 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3776 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3777 | |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3778 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3779 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3780 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3781 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3782 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3783 | if( status != PSA_SUCCESS ) |
| 3784 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3785 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3786 | psa_key_attributes_t attributes = { |
| 3787 | .core = slot->attr |
| 3788 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3789 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3790 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3791 | &attributes, slot->key.data, slot->key.bytes, |
| 3792 | alg, |
| 3793 | nonce, nonce_length, |
| 3794 | additional_data, additional_data_length, |
| 3795 | ciphertext, ciphertext_length, |
| 3796 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3797 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3798 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3799 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3800 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3801 | psa_unlock_key_slot( slot ); |
| 3802 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3803 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3804 | } |
| 3805 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3806 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3807 | /* Generators */ |
| 3808 | /****************************************************************/ |
| 3809 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3810 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3811 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3812 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3813 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3814 | #endif /* At least one builtin KDF */ |
| 3815 | |
| 3816 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3817 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3818 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3819 | static psa_status_t psa_key_derivation_start_hmac( |
| 3820 | psa_mac_operation_t *operation, |
| 3821 | psa_algorithm_t hash_alg, |
| 3822 | const uint8_t *hmac_key, |
| 3823 | size_t hmac_key_length ) |
| 3824 | { |
| 3825 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3826 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3827 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 3828 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 3829 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 3830 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3831 | operation->is_sign = 1; |
| 3832 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 3833 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3834 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 3835 | &attributes, |
| 3836 | hmac_key, hmac_key_length, |
| 3837 | PSA_ALG_HMAC( hash_alg ) ); |
| 3838 | |
| 3839 | psa_reset_key_attributes( &attributes ); |
| 3840 | return( status ); |
| 3841 | } |
| 3842 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3843 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3844 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 3845 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 3846 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 3847 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 3848 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3849 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3850 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3851 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3852 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 3853 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3854 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3855 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3856 | } |
| 3857 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3858 | 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] | 3859 | { |
| 3860 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3861 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3862 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3863 | { |
| 3864 | /* The object has (apparently) been initialized but it is not |
| 3865 | * in use. It's ok to call abort on such an object, and there's |
| 3866 | * nothing to do. */ |
| 3867 | } |
| 3868 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3869 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3870 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3871 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3872 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3873 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3874 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3875 | else |
| 3876 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3877 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3878 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3879 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3880 | /* 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] | 3881 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3882 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3883 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 3884 | { |
| 3885 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 3886 | operation->ctx.tls12_prf.secret_length ); |
| 3887 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 3888 | } |
| 3889 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3890 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 3891 | { |
| 3892 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 3893 | operation->ctx.tls12_prf.seed_length ); |
| 3894 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 3895 | } |
| 3896 | |
| 3897 | if( operation->ctx.tls12_prf.label != NULL ) |
| 3898 | { |
| 3899 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 3900 | operation->ctx.tls12_prf.label_length ); |
| 3901 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 3902 | } |
| 3903 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3904 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3905 | |
| 3906 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3907 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3908 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3909 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3910 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 3911 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3912 | { |
| 3913 | status = PSA_ERROR_BAD_STATE; |
| 3914 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 3915 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3916 | return( status ); |
| 3917 | } |
| 3918 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3919 | 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] | 3920 | size_t *capacity) |
| 3921 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3922 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3923 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3924 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3925 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3928 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3929 | return( PSA_SUCCESS ); |
| 3930 | } |
| 3931 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3932 | 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] | 3933 | size_t capacity ) |
| 3934 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3935 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3936 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3937 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3938 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3939 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3940 | return( PSA_SUCCESS ); |
| 3941 | } |
| 3942 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3943 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3944 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3945 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3946 | 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] | 3947 | psa_algorithm_t hash_alg, |
| 3948 | uint8_t *output, |
| 3949 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3950 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3951 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3952 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3953 | psa_status_t status; |
| 3954 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3955 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 3956 | return( PSA_ERROR_BAD_STATE ); |
| 3957 | hkdf->state = HKDF_STATE_OUTPUT; |
| 3958 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3959 | while( output_length != 0 ) |
| 3960 | { |
| 3961 | /* Copy what remains of the current block */ |
| 3962 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 3963 | if( n > output_length ) |
| 3964 | n = (uint8_t) output_length; |
| 3965 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 3966 | output += n; |
| 3967 | output_length -= n; |
| 3968 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3969 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3970 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3971 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 3972 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3973 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3974 | * object was corrupted or if this function is called directly |
| 3975 | * inside the library. */ |
| 3976 | if( hkdf->block_number == 0xff ) |
| 3977 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3978 | |
| 3979 | /* We need a new block */ |
| 3980 | ++hkdf->block_number; |
| 3981 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3982 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3983 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 3984 | hash_alg, |
| 3985 | hkdf->prk, |
| 3986 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3987 | if( status != PSA_SUCCESS ) |
| 3988 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3989 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3990 | if( hkdf->block_number != 1 ) |
| 3991 | { |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3992 | status = psa_mac_update( &hkdf->hmac, |
| 3993 | hkdf->output_block, |
| 3994 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3995 | if( status != PSA_SUCCESS ) |
| 3996 | return( status ); |
| 3997 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3998 | status = psa_mac_update( &hkdf->hmac, |
| 3999 | hkdf->info, |
| 4000 | hkdf->info_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4001 | if( status != PSA_SUCCESS ) |
| 4002 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4003 | status = psa_mac_update( &hkdf->hmac, |
| 4004 | &hkdf->block_number, 1 ); |
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_sign_finish( &hkdf->hmac, |
| 4008 | hkdf->output_block, |
| 4009 | sizeof( hkdf->output_block ), |
| 4010 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4011 | if( status != PSA_SUCCESS ) |
| 4012 | return( status ); |
| 4013 | } |
| 4014 | |
| 4015 | return( PSA_SUCCESS ); |
| 4016 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4017 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4018 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4019 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4020 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4021 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4022 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4023 | psa_algorithm_t alg ) |
| 4024 | { |
| 4025 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4026 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4027 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4028 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4029 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4030 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4031 | /* We can't be wanting more output after block 0xff, otherwise |
| 4032 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4033 | * prevented this call. It could happen only if the operation |
| 4034 | * object was corrupted or if this function is called directly |
| 4035 | * inside the library. */ |
| 4036 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4037 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4038 | |
| 4039 | /* We need a new block */ |
| 4040 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4041 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4042 | |
| 4043 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4044 | * |
| 4045 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4046 | * |
| 4047 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4048 | * HMAC_hash(secret, A(2) + seed) + |
| 4049 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4050 | * |
| 4051 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4052 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4053 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4054 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4055 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4056 | * is currently extracted as `output_block` and where i is |
| 4057 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4058 | */ |
| 4059 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4060 | status = psa_key_derivation_start_hmac( &hmac, |
| 4061 | hash_alg, |
| 4062 | tls12_prf->secret, |
| 4063 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4064 | if( status != PSA_SUCCESS ) |
| 4065 | goto cleanup; |
| 4066 | |
| 4067 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4068 | if( tls12_prf->block_number == 1 ) |
| 4069 | { |
| 4070 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4071 | * the variable seed and in this instance means it in the context of the |
| 4072 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4073 | status = psa_mac_update( &hmac, |
| 4074 | tls12_prf->label, |
| 4075 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4076 | if( status != PSA_SUCCESS ) |
| 4077 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4078 | status = psa_mac_update( &hmac, |
| 4079 | tls12_prf->seed, |
| 4080 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4081 | if( status != PSA_SUCCESS ) |
| 4082 | goto cleanup; |
| 4083 | } |
| 4084 | else |
| 4085 | { |
| 4086 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4087 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4088 | if( status != PSA_SUCCESS ) |
| 4089 | goto cleanup; |
| 4090 | } |
| 4091 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4092 | status = psa_mac_sign_finish( &hmac, |
| 4093 | tls12_prf->Ai, hash_length, |
| 4094 | &hmac_output_length ); |
| 4095 | if( hmac_output_length != hash_length ) |
| 4096 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4097 | if( status != PSA_SUCCESS ) |
| 4098 | goto cleanup; |
| 4099 | |
| 4100 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4101 | status = psa_key_derivation_start_hmac( &hmac, |
| 4102 | hash_alg, |
| 4103 | tls12_prf->secret, |
| 4104 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4105 | if( status != PSA_SUCCESS ) |
| 4106 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4107 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4108 | if( status != PSA_SUCCESS ) |
| 4109 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4110 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4111 | if( status != PSA_SUCCESS ) |
| 4112 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4113 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4114 | if( status != PSA_SUCCESS ) |
| 4115 | goto cleanup; |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4116 | status = psa_mac_sign_finish( &hmac, |
| 4117 | tls12_prf->output_block, hash_length, |
| 4118 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4119 | if( status != PSA_SUCCESS ) |
| 4120 | goto cleanup; |
| 4121 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4122 | |
| 4123 | cleanup: |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4124 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4125 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4126 | status = cleanup_status; |
| 4127 | |
| 4128 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4129 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4130 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4131 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4132 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4133 | psa_algorithm_t alg, |
| 4134 | uint8_t *output, |
| 4135 | size_t output_length ) |
| 4136 | { |
| 4137 | 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] | 4138 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4139 | psa_status_t status; |
| 4140 | uint8_t offset, length; |
| 4141 | |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4142 | switch( tls12_prf->state ) |
| 4143 | { |
| 4144 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4145 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4146 | break; |
| 4147 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4148 | break; |
| 4149 | default: |
| 4150 | return( PSA_ERROR_BAD_STATE ); |
| 4151 | } |
| 4152 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4153 | while( output_length != 0 ) |
| 4154 | { |
| 4155 | /* Check if we have fully processed the current block. */ |
| 4156 | if( tls12_prf->left_in_block == 0 ) |
| 4157 | { |
| 4158 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4159 | alg ); |
| 4160 | if( status != PSA_SUCCESS ) |
| 4161 | return( status ); |
| 4162 | |
| 4163 | continue; |
| 4164 | } |
| 4165 | |
| 4166 | if( tls12_prf->left_in_block > output_length ) |
| 4167 | length = (uint8_t) output_length; |
| 4168 | else |
| 4169 | length = tls12_prf->left_in_block; |
| 4170 | |
| 4171 | offset = hash_length - tls12_prf->left_in_block; |
| 4172 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4173 | output += length; |
| 4174 | output_length -= length; |
| 4175 | tls12_prf->left_in_block -= length; |
| 4176 | } |
| 4177 | |
| 4178 | return( PSA_SUCCESS ); |
| 4179 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4180 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4181 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4182 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4183 | psa_status_t psa_key_derivation_output_bytes( |
| 4184 | psa_key_derivation_operation_t *operation, |
| 4185 | uint8_t *output, |
| 4186 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4187 | { |
| 4188 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4189 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4190 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4191 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4192 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4193 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4194 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4197 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4198 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4199 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4200 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4201 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4202 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4203 | goto exit; |
| 4204 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4205 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4206 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4207 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4208 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4209 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4210 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4211 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4212 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4213 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4214 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4215 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4216 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4217 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4218 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4219 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4220 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4221 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4222 | output, output_length ); |
| 4223 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4224 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4225 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4226 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4227 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4228 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4229 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4230 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4231 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4232 | kdf_alg, output, |
| 4233 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4234 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4235 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4236 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4237 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4238 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4239 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4240 | return( PSA_ERROR_BAD_STATE ); |
| 4241 | } |
| 4242 | |
| 4243 | exit: |
| 4244 | if( status != PSA_SUCCESS ) |
| 4245 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4246 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4247 | * This allows us to differentiate between exhausted operations and |
| 4248 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4249 | * operations. */ |
| 4250 | psa_algorithm_t alg = operation->alg; |
| 4251 | psa_key_derivation_abort( operation ); |
| 4252 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4253 | memset( output, '!', output_length ); |
| 4254 | } |
| 4255 | return( status ); |
| 4256 | } |
| 4257 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4258 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4259 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4260 | { |
| 4261 | if( data_size >= 8 ) |
| 4262 | mbedtls_des_key_set_parity( data ); |
| 4263 | if( data_size >= 16 ) |
| 4264 | mbedtls_des_key_set_parity( data + 8 ); |
| 4265 | if( data_size >= 24 ) |
| 4266 | mbedtls_des_key_set_parity( data + 16 ); |
| 4267 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4268 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4269 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4270 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4271 | psa_key_slot_t *slot, |
| 4272 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4273 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4274 | { |
| 4275 | uint8_t *data = NULL; |
| 4276 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4277 | psa_status_t status; |
| 4278 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4279 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4281 | if( bits % 8 != 0 ) |
| 4282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4283 | data = mbedtls_calloc( 1, bytes ); |
| 4284 | if( data == NULL ) |
| 4285 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4286 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4287 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4288 | if( status != PSA_SUCCESS ) |
| 4289 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4290 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4291 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4292 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4293 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4294 | |
| 4295 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 4296 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4297 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4298 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4299 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4300 | psa_key_attributes_t attributes = { |
| 4301 | .core = slot->attr |
| 4302 | }; |
| 4303 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4304 | status = psa_driver_wrapper_import_key( &attributes, |
| 4305 | data, bytes, |
| 4306 | slot->key.data, |
| 4307 | slot->key.bytes, |
| 4308 | &slot->key.bytes, &bits ); |
| 4309 | if( bits != slot->attr.bits ) |
| 4310 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4311 | |
| 4312 | exit: |
| 4313 | mbedtls_free( data ); |
| 4314 | return( status ); |
| 4315 | } |
| 4316 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4317 | 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] | 4318 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4319 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4320 | { |
| 4321 | psa_status_t status; |
| 4322 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4323 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4324 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4325 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4326 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4327 | /* Reject any attempt to create a zero-length key so that we don't |
| 4328 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4329 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4330 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4331 | |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4332 | if( operation->alg == PSA_ALG_NONE ) |
| 4333 | return( PSA_ERROR_BAD_STATE ); |
| 4334 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4335 | if( ! operation->can_output_key ) |
| 4336 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4337 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4338 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 4339 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4340 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4341 | if( driver != NULL ) |
| 4342 | { |
| 4343 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4344 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4345 | } |
| 4346 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4347 | if( status == PSA_SUCCESS ) |
| 4348 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4349 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4350 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4351 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4352 | } |
| 4353 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4354 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4355 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4356 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4357 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4358 | return( status ); |
| 4359 | } |
| 4360 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4361 | |
| 4362 | |
| 4363 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4364 | /* Key derivation */ |
| 4365 | /****************************************************************/ |
| 4366 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4367 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4368 | static int is_kdf_alg_supported( psa_algorithm_t kdf_alg ) |
| 4369 | { |
| 4370 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 4371 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 4372 | return( 1 ); |
| 4373 | #endif |
| 4374 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4375 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 4376 | return( 1 ); |
| 4377 | #endif |
| 4378 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4379 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 4380 | return( 1 ); |
| 4381 | #endif |
| 4382 | return( 0 ); |
| 4383 | } |
| 4384 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4385 | static psa_status_t psa_hash_try_support( psa_algorithm_t alg ) |
| 4386 | { |
| 4387 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 4388 | psa_status_t status = psa_hash_setup( &operation, alg ); |
| 4389 | psa_hash_abort( &operation ); |
| 4390 | return( status ); |
| 4391 | } |
| 4392 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4393 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4394 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4395 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4396 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4397 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4398 | * initialisers for this union leave some bytes unspecified.) */ |
| 4399 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4400 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4401 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4402 | if( ! is_kdf_alg_supported( kdf_alg ) ) |
| 4403 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4404 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4405 | /* All currently supported key derivation algorithms are based on a |
| 4406 | * hash algorithm. */ |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4407 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
| 4408 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
| 4409 | if( hash_size == 0 ) |
| 4410 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4411 | |
| 4412 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 4413 | * we might fail later, which is somewhat unfriendly and potentially |
| 4414 | * risk-prone. */ |
| 4415 | psa_status_t status = psa_hash_try_support( hash_alg ); |
| 4416 | if( status != PSA_SUCCESS ) |
| 4417 | return( status ); |
| 4418 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4419 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4420 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
| 4421 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4422 | { |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4423 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4424 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4425 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4426 | operation->capacity = 255 * hash_size; |
| 4427 | return( PSA_SUCCESS ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4428 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4429 | |
| 4430 | static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg ) |
| 4431 | { |
| 4432 | #if defined(PSA_WANT_ALG_ECDH) |
| 4433 | if( alg == PSA_ALG_ECDH ) |
| 4434 | return( PSA_SUCCESS ); |
| 4435 | #endif |
| 4436 | (void) alg; |
| 4437 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4438 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4439 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4440 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4441 | 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] | 4442 | psa_algorithm_t alg ) |
| 4443 | { |
| 4444 | psa_status_t status; |
| 4445 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4446 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4447 | return( PSA_ERROR_BAD_STATE ); |
| 4448 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4449 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4450 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4451 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4452 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4453 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4454 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4455 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ); |
| 4456 | status = psa_key_agreement_try_support( ka_alg ); |
| 4457 | if( status != PSA_SUCCESS ) |
| 4458 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4459 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4460 | #else |
| 4461 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4462 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4463 | } |
| 4464 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4465 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4466 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4467 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4468 | #else |
| 4469 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4470 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4471 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4472 | else |
| 4473 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4474 | |
| 4475 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4476 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4477 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4478 | } |
| 4479 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4480 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4481 | 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] | 4482 | psa_algorithm_t hash_alg, |
| 4483 | psa_key_derivation_step_t step, |
| 4484 | const uint8_t *data, |
| 4485 | size_t data_length ) |
| 4486 | { |
| 4487 | psa_status_t status; |
| 4488 | switch( step ) |
| 4489 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4490 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4491 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4492 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4493 | else |
| 4494 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4495 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4496 | hash_alg, |
| 4497 | data, data_length ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4498 | if( status != PSA_SUCCESS ) |
| 4499 | return( status ); |
| 4500 | hkdf->state = HKDF_STATE_STARTED; |
| 4501 | return( PSA_SUCCESS ); |
| 4502 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4503 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4504 | /* If no salt was provided, use an empty salt. */ |
| 4505 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4506 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4507 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4508 | hash_alg, |
| 4509 | NULL, 0 ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4510 | if( status != PSA_SUCCESS ) |
| 4511 | return( status ); |
| 4512 | hkdf->state = HKDF_STATE_STARTED; |
| 4513 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4514 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4515 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4516 | status = psa_mac_update( &hkdf->hmac, |
| 4517 | data, data_length ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4518 | if( status != PSA_SUCCESS ) |
| 4519 | return( status ); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4520 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4521 | hkdf->prk, |
| 4522 | sizeof( hkdf->prk ), |
| 4523 | &data_length ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4524 | if( status != PSA_SUCCESS ) |
| 4525 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4526 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4527 | hkdf->block_number = 0; |
| 4528 | hkdf->state = HKDF_STATE_KEYED; |
| 4529 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4530 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4531 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4532 | return( PSA_ERROR_BAD_STATE ); |
| 4533 | if( hkdf->info_set ) |
| 4534 | return( PSA_ERROR_BAD_STATE ); |
| 4535 | hkdf->info_length = data_length; |
| 4536 | if( data_length != 0 ) |
| 4537 | { |
| 4538 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4539 | if( hkdf->info == NULL ) |
| 4540 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4541 | memcpy( hkdf->info, data, data_length ); |
| 4542 | } |
| 4543 | hkdf->info_set = 1; |
| 4544 | return( PSA_SUCCESS ); |
| 4545 | default: |
| 4546 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4547 | } |
| 4548 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4549 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4550 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4551 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4552 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4553 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4554 | const uint8_t *data, |
| 4555 | size_t data_length ) |
| 4556 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4557 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4558 | return( PSA_ERROR_BAD_STATE ); |
| 4559 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4560 | if( data_length != 0 ) |
| 4561 | { |
| 4562 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4563 | if( prf->seed == NULL ) |
| 4564 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4565 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4566 | memcpy( prf->seed, data, data_length ); |
| 4567 | prf->seed_length = data_length; |
| 4568 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4569 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4570 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4571 | |
| 4572 | return( PSA_SUCCESS ); |
| 4573 | } |
| 4574 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4575 | 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] | 4576 | const uint8_t *data, |
| 4577 | size_t data_length ) |
| 4578 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4579 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4580 | return( PSA_ERROR_BAD_STATE ); |
| 4581 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4582 | if( data_length != 0 ) |
| 4583 | { |
| 4584 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 4585 | if( prf->secret == NULL ) |
| 4586 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4587 | |
| 4588 | memcpy( prf->secret, data, data_length ); |
| 4589 | prf->secret_length = data_length; |
| 4590 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4591 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4592 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4593 | |
| 4594 | return( PSA_SUCCESS ); |
| 4595 | } |
| 4596 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4597 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 4598 | const uint8_t *data, |
| 4599 | size_t data_length ) |
| 4600 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4601 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4602 | return( PSA_ERROR_BAD_STATE ); |
| 4603 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4604 | if( data_length != 0 ) |
| 4605 | { |
| 4606 | prf->label = mbedtls_calloc( 1, data_length ); |
| 4607 | if( prf->label == NULL ) |
| 4608 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4609 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4610 | memcpy( prf->label, data, data_length ); |
| 4611 | prf->label_length = data_length; |
| 4612 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4613 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4614 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4615 | |
| 4616 | return( PSA_SUCCESS ); |
| 4617 | } |
| 4618 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4619 | 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] | 4620 | psa_key_derivation_step_t step, |
| 4621 | const uint8_t *data, |
| 4622 | size_t data_length ) |
| 4623 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4624 | switch( step ) |
| 4625 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4626 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 4627 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4628 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4629 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4630 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4631 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4632 | default: |
| 4633 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4634 | } |
| 4635 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4636 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4637 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 4638 | |
| 4639 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4640 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4641 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4642 | const uint8_t *data, |
| 4643 | size_t data_length ) |
| 4644 | { |
| 4645 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4646 | 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] | 4647 | uint8_t *cur = pms; |
| 4648 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4649 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4650 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4651 | |
| 4652 | /* Quoting RFC 4279, Section 2: |
| 4653 | * |
| 4654 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4655 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4656 | * uint16 with the value N, and the PSK itself. |
| 4657 | */ |
| 4658 | |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 4659 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 4660 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4661 | memset( cur, 0, data_length ); |
| 4662 | cur += data_length; |
| 4663 | *cur++ = pms[0]; |
| 4664 | *cur++ = pms[1]; |
| 4665 | memcpy( cur, data, data_length ); |
| 4666 | cur += data_length; |
| 4667 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4668 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4669 | |
| 4670 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 4671 | return( status ); |
| 4672 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4673 | |
| 4674 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4675 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4676 | psa_key_derivation_step_t step, |
| 4677 | const uint8_t *data, |
| 4678 | size_t data_length ) |
| 4679 | { |
| 4680 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4681 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4682 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4683 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4684 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4685 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4686 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4687 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4688 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4689 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4690 | /** Check whether the given key type is acceptable for the given |
| 4691 | * input step of a key derivation. |
| 4692 | * |
| 4693 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4694 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4695 | * Both secret and non-secret inputs can alternatively have the type |
| 4696 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4697 | * that the input was passed as a buffer rather than via a key object. |
| 4698 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4699 | static int psa_key_derivation_check_input_type( |
| 4700 | psa_key_derivation_step_t step, |
| 4701 | psa_key_type_t key_type ) |
| 4702 | { |
| 4703 | switch( step ) |
| 4704 | { |
| 4705 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4706 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 4707 | return( PSA_SUCCESS ); |
| 4708 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4709 | return( PSA_SUCCESS ); |
| 4710 | break; |
| 4711 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4712 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4713 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4714 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4715 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 4716 | return( PSA_SUCCESS ); |
| 4717 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4718 | return( PSA_SUCCESS ); |
| 4719 | break; |
| 4720 | } |
| 4721 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4722 | } |
| 4723 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4724 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4725 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4726 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4727 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4728 | const uint8_t *data, |
| 4729 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4730 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4731 | psa_status_t status; |
| 4732 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 4733 | |
| 4734 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4735 | if( status != PSA_SUCCESS ) |
| 4736 | goto exit; |
| 4737 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4738 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4739 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4740 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4741 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4742 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4743 | step, data, data_length ); |
| 4744 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4745 | else |
| 4746 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4747 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4748 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4749 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4750 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4751 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4752 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4753 | else |
| 4754 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4755 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4756 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4757 | { |
| 4758 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4759 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4760 | } |
| 4761 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4762 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4763 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4764 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4765 | (void) data; |
| 4766 | (void) data_length; |
| 4767 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4768 | return( PSA_ERROR_BAD_STATE ); |
| 4769 | } |
| 4770 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4771 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4772 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4773 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4774 | return( status ); |
| 4775 | } |
| 4776 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4777 | psa_status_t psa_key_derivation_input_bytes( |
| 4778 | psa_key_derivation_operation_t *operation, |
| 4779 | psa_key_derivation_step_t step, |
| 4780 | const uint8_t *data, |
| 4781 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4782 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4783 | return( psa_key_derivation_input_internal( operation, step, |
| 4784 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 4785 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4786 | } |
| 4787 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4788 | psa_status_t psa_key_derivation_input_key( |
| 4789 | psa_key_derivation_operation_t *operation, |
| 4790 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4791 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4792 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4793 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4794 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4795 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4796 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4797 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4798 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4799 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4800 | { |
| 4801 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4802 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4803 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4804 | |
| 4805 | /* Passing a key object as a SECRET input unlocks the permission |
| 4806 | * to output to a key object. */ |
| 4807 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4808 | operation->can_output_key = 1; |
| 4809 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4810 | status = psa_key_derivation_input_internal( operation, |
| 4811 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4812 | slot->key.data, |
| 4813 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4814 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4815 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4816 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4817 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4818 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4819 | |
| 4820 | |
| 4821 | |
| 4822 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4823 | /* Key agreement */ |
| 4824 | /****************************************************************/ |
| 4825 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4826 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4827 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 4828 | size_t peer_key_length, |
| 4829 | const mbedtls_ecp_keypair *our_key, |
| 4830 | uint8_t *shared_secret, |
| 4831 | size_t shared_secret_size, |
| 4832 | size_t *shared_secret_length ) |
| 4833 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 4834 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4835 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4836 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4837 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 4838 | 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] | 4839 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4840 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4841 | status = mbedtls_psa_ecp_load_representation( |
| 4842 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4843 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4844 | peer_key, |
| 4845 | peer_key_length, |
| 4846 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4847 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4848 | goto exit; |
| 4849 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4850 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4851 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4852 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4853 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4854 | status = mbedtls_to_psa_error( |
| 4855 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 4856 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4857 | goto exit; |
| 4858 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4859 | status = mbedtls_to_psa_error( |
| 4860 | mbedtls_ecdh_calc_secret( &ecdh, |
| 4861 | shared_secret_length, |
| 4862 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4863 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4864 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4865 | if( status != PSA_SUCCESS ) |
| 4866 | goto exit; |
| 4867 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 4868 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4869 | |
| 4870 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 4871 | if( status != PSA_SUCCESS ) |
| 4872 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4873 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4874 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 4875 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4876 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4877 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4878 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4879 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4880 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4881 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4882 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4883 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 4884 | psa_key_slot_t *private_key, |
| 4885 | const uint8_t *peer_key, |
| 4886 | size_t peer_key_length, |
| 4887 | uint8_t *shared_secret, |
| 4888 | size_t shared_secret_size, |
| 4889 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4890 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4891 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4892 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4893 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4894 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4895 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4896 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4897 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4898 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 4899 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 4900 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4901 | private_key->key.data, |
| 4902 | private_key->key.bytes, |
| 4903 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4904 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4905 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4906 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4907 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4908 | shared_secret, shared_secret_size, |
| 4909 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4910 | mbedtls_ecp_keypair_free( ecp ); |
| 4911 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4912 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4913 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4914 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4915 | (void) private_key; |
| 4916 | (void) peer_key; |
| 4917 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4918 | (void) shared_secret; |
| 4919 | (void) shared_secret_size; |
| 4920 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4921 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4922 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4925 | /* 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] | 4926 | * to potentially free embedded data structures and wipe confidential data. |
| 4927 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4928 | 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] | 4929 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4930 | psa_key_slot_t *private_key, |
| 4931 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4932 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4933 | { |
| 4934 | psa_status_t status; |
| 4935 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4936 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4937 | 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] | 4938 | |
| 4939 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4940 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4941 | status = psa_key_agreement_raw_internal( ka_alg, |
| 4942 | private_key, |
| 4943 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4944 | shared_secret, |
| 4945 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4946 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4947 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4948 | goto exit; |
| 4949 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4950 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4951 | * the shared secret. A shared secret is permitted wherever a key |
| 4952 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4953 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4954 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4955 | shared_secret, |
| 4956 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4957 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4958 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4959 | return( status ); |
| 4960 | } |
| 4961 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4962 | 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] | 4963 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4964 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4965 | const uint8_t *peer_key, |
| 4966 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4967 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4968 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4969 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4970 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4971 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4972 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4973 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4974 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4975 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4976 | if( status != PSA_SUCCESS ) |
| 4977 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4979 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4980 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4981 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4982 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4983 | else |
| 4984 | { |
| 4985 | /* If a private key has been added as SECRET, we allow the derived |
| 4986 | * key material to be used as a key in PSA Crypto. */ |
| 4987 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4988 | operation->can_output_key = 1; |
| 4989 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4990 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4991 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4992 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4993 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4994 | } |
| 4995 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4996 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4997 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4998 | const uint8_t *peer_key, |
| 4999 | size_t peer_key_length, |
| 5000 | uint8_t *output, |
| 5001 | size_t output_size, |
| 5002 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5003 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5004 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5005 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5006 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5007 | |
| 5008 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5009 | { |
| 5010 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5011 | goto exit; |
| 5012 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5013 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5014 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5015 | if( status != PSA_SUCCESS ) |
| 5016 | goto exit; |
| 5017 | |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5018 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5019 | * for the output size. The PSA specification only guarantees that this |
| 5020 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5021 | * but it might be nice to allow smaller buffers if the output fits. |
| 5022 | * At the time of writing this comment, with only ECDH implemented, |
| 5023 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5024 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5025 | * be exact for it as well. */ |
| 5026 | size_t expected_length = |
| 5027 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits ); |
| 5028 | if( output_size < expected_length ) |
| 5029 | { |
| 5030 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5031 | goto exit; |
| 5032 | } |
| 5033 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5034 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5035 | peer_key, peer_key_length, |
| 5036 | output, output_size, |
| 5037 | output_length ); |
| 5038 | |
| 5039 | exit: |
| 5040 | if( status != PSA_SUCCESS ) |
| 5041 | { |
| 5042 | /* If an error happens and is not handled properly, the output |
| 5043 | * may be used as a key to protect sensitive data. Arrange for such |
| 5044 | * a key to be random, which is likely to result in decryption or |
| 5045 | * verification errors. This is better than filling the buffer with |
| 5046 | * some constant data such as zeros, which would result in the data |
| 5047 | * being protected with a reproducible, easily knowable key. |
| 5048 | */ |
| 5049 | psa_generate_random( output, output_size ); |
| 5050 | *output_length = output_size; |
| 5051 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5052 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5053 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5054 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5055 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5056 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5057 | |
| 5058 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5059 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5060 | /****************************************************************/ |
| 5061 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5062 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5063 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5064 | /** Initialize the PSA random generator. |
| 5065 | */ |
| 5066 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5067 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5068 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5069 | memset( rng, 0, sizeof( *rng ) ); |
| 5070 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5071 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5072 | /* Set default configuration if |
| 5073 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5074 | if( rng->entropy_init == NULL ) |
| 5075 | rng->entropy_init = mbedtls_entropy_init; |
| 5076 | if( rng->entropy_free == NULL ) |
| 5077 | rng->entropy_free = mbedtls_entropy_free; |
| 5078 | |
| 5079 | rng->entropy_init( &rng->entropy ); |
| 5080 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5081 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5082 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5083 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5084 | mbedtls_entropy_add_source( &rng->entropy, |
| 5085 | mbedtls_nv_seed_poll, NULL, |
| 5086 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5087 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5088 | #endif |
| 5089 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5090 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5091 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | /** Deinitialize the PSA random generator. |
| 5095 | */ |
| 5096 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 5097 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5098 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5099 | memset( rng, 0, sizeof( *rng ) ); |
| 5100 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5101 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5102 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5103 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | /** Seed the PSA random generator. |
| 5107 | */ |
| 5108 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5109 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5110 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5111 | /* Do nothing: the external RNG seeds itself. */ |
| 5112 | (void) rng; |
| 5113 | return( PSA_SUCCESS ); |
| 5114 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5115 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5116 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5117 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5118 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5119 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5120 | } |
| 5121 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5122 | psa_status_t psa_generate_random( uint8_t *output, |
| 5123 | size_t output_size ) |
| 5124 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5125 | GUARD_MODULE_INITIALIZED; |
| 5126 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5127 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5128 | |
| 5129 | size_t output_length = 0; |
| 5130 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 5131 | output, output_size, |
| 5132 | &output_length ); |
| 5133 | if( status != PSA_SUCCESS ) |
| 5134 | return( status ); |
| 5135 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame^] | 5136 | * for the external RNG interface. */ |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5137 | if( output_length != output_size ) |
| 5138 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 5139 | return( PSA_SUCCESS ); |
| 5140 | |
| 5141 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5142 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5143 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5144 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5145 | size_t request_size = |
| 5146 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5147 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5148 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5149 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 5150 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5151 | if( ret != 0 ) |
| 5152 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5153 | output_size -= request_size; |
| 5154 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5155 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5156 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5157 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5158 | } |
| 5159 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5160 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5161 | * |
| 5162 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5163 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5164 | * PSA API doesn't support passing an explicit state. |
| 5165 | * |
| 5166 | * In the non-external case, psa_generate_random() calls an |
| 5167 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5168 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5169 | * instead of doing this back-and-forth between the PSA API and the |
| 5170 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5171 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5172 | */ |
| 5173 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5174 | int mbedtls_psa_get_random( void *p_rng, |
| 5175 | unsigned char *output, |
| 5176 | size_t output_size ) |
| 5177 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5178 | /* This function takes a pointer to the RNG state because that's what |
| 5179 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5180 | * its own state internally and doesn't let the caller access that state. |
| 5181 | * So we just ignore the state parameter, and in practice we'll pass |
| 5182 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5183 | (void) p_rng; |
| 5184 | psa_status_t status = psa_generate_random( output, output_size ); |
| 5185 | if( status == PSA_SUCCESS ) |
| 5186 | return( 0 ); |
| 5187 | else |
| 5188 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 5189 | } |
| 5190 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5191 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5192 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5193 | #include "mbedtls/entropy_poll.h" |
| 5194 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5195 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5196 | size_t seed_size ) |
| 5197 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5198 | if( global_data.initialized ) |
| 5199 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5200 | |
| 5201 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5202 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5203 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5204 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5205 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5206 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5207 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5208 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5209 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5210 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5211 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5212 | * \param type The key type |
| 5213 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5214 | * |
| 5215 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5216 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5217 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5218 | * The size in bits of the key is not valid. |
| 5219 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5220 | * The type and/or the size in bits of the key or the combination of |
| 5221 | * the two is not supported. |
| 5222 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5223 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 5224 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5225 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5226 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5227 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5228 | if( key_type_is_raw_bytes( type ) ) |
| 5229 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5230 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5231 | if( status != PSA_SUCCESS ) |
| 5232 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5233 | } |
| 5234 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5235 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5236 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5237 | { |
| 5238 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5239 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5240 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5241 | /* Accept only byte-aligned keys, for the same reasons as |
| 5242 | * in psa_import_rsa_key(). */ |
| 5243 | if( bits % 8 != 0 ) |
| 5244 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5245 | } |
| 5246 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5247 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5248 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5249 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5250 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5251 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 5252 | /* To avoid empty block, return successfully here. */ |
| 5253 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5254 | } |
| 5255 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5256 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5257 | { |
| 5258 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5259 | } |
| 5260 | |
| 5261 | return( PSA_SUCCESS ); |
| 5262 | } |
| 5263 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5264 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5265 | const psa_key_attributes_t *attributes, |
| 5266 | 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] | 5267 | { |
| 5268 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5269 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5270 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5271 | if( ( attributes->domain_parameters == NULL ) && |
| 5272 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5273 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5274 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5275 | if( key_type_is_raw_bytes( type ) ) |
| 5276 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5277 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5278 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5279 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5280 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5281 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5282 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5283 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5284 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5285 | } |
| 5286 | else |
| 5287 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5288 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5289 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5290 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5291 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 5292 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 5293 | key_buffer, |
| 5294 | key_buffer_size, |
| 5295 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5296 | } |
| 5297 | else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5298 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5299 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5300 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5301 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5302 | 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] | 5303 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 5304 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 5305 | key_buffer, |
| 5306 | key_buffer_size, |
| 5307 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5308 | } |
| 5309 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5310 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5311 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5312 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5313 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5314 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5315 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5316 | return( PSA_SUCCESS ); |
| 5317 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5318 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5319 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5320 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5321 | { |
| 5322 | psa_status_t status; |
| 5323 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5324 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5325 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5326 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5327 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5328 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5329 | /* Reject any attempt to create a zero-length key so that we don't |
| 5330 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5331 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5333 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5334 | /* Reject any attempt to create a public key. */ |
| 5335 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 5336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5337 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5338 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 5339 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5340 | if( status != PSA_SUCCESS ) |
| 5341 | goto exit; |
| 5342 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5343 | /* In the case of a transparent key or an opaque key stored in local |
| 5344 | * storage (thus not in the case of generating a key in a secure element |
| 5345 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5346 | * hold the generated key material. */ |
| 5347 | if( slot->key.data == NULL ) |
| 5348 | { |
| 5349 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 5350 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 5351 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5352 | status = psa_validate_key_type_and_size_for_key_generation( |
| 5353 | attributes->core.type, attributes->core.bits ); |
| 5354 | if( status != PSA_SUCCESS ) |
| 5355 | goto exit; |
| 5356 | |
| 5357 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 5358 | attributes->core.type, |
| 5359 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5360 | } |
| 5361 | else |
| 5362 | { |
| 5363 | status = psa_driver_wrapper_get_key_buffer_size( |
| 5364 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5365 | if( status != PSA_SUCCESS ) |
| 5366 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5367 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5368 | |
| 5369 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 5370 | if( status != PSA_SUCCESS ) |
| 5371 | goto exit; |
| 5372 | } |
| 5373 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 5374 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5375 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5376 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5377 | if( status != PSA_SUCCESS ) |
| 5378 | psa_remove_key_data_from_memory( slot ); |
| 5379 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5380 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5381 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5382 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5383 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5384 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5385 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5386 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5387 | } |
| 5388 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5389 | /****************************************************************/ |
| 5390 | /* Module setup */ |
| 5391 | /****************************************************************/ |
| 5392 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5393 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5394 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5395 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5396 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5397 | { |
| 5398 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5399 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5400 | global_data.rng.entropy_init = entropy_init; |
| 5401 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5402 | return( PSA_SUCCESS ); |
| 5403 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5404 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5405 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5406 | void mbedtls_psa_crypto_free( void ) |
| 5407 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5408 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5409 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5410 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5411 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5412 | } |
| 5413 | /* Wipe all remaining data, including configuration. |
| 5414 | * In particular, this sets all state indicator to the value |
| 5415 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5416 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5417 | |
| 5418 | /* Terminate drivers */ |
| 5419 | psa_driver_wrapper_free( ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5420 | } |
| 5421 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5422 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5423 | /** Recover a transaction that was interrupted by a power failure. |
| 5424 | * |
| 5425 | * This function is called during initialization, before psa_crypto_init() |
| 5426 | * returns. If this function returns a failure status, the initialization |
| 5427 | * fails. |
| 5428 | */ |
| 5429 | static psa_status_t psa_crypto_recover_transaction( |
| 5430 | const psa_crypto_transaction_t *transaction ) |
| 5431 | { |
| 5432 | switch( transaction->unknown.type ) |
| 5433 | { |
| 5434 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5435 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5436 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5437 | * is implemented. |
| 5438 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5439 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5440 | default: |
| 5441 | /* We found an unsupported transaction in the storage. |
| 5442 | * 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] | 5443 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5444 | } |
| 5445 | } |
| 5446 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5447 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5448 | psa_status_t psa_crypto_init( void ) |
| 5449 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5450 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5451 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5452 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5453 | if( global_data.initialized != 0 ) |
| 5454 | return( PSA_SUCCESS ); |
| 5455 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5456 | /* Initialize and seed the random generator. */ |
| 5457 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5458 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5459 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5460 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5461 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5462 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5464 | status = psa_initialize_key_slots( ); |
| 5465 | if( status != PSA_SUCCESS ) |
| 5466 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5467 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5468 | /* Init drivers */ |
| 5469 | status = psa_driver_wrapper_init( ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5470 | if( status != PSA_SUCCESS ) |
| 5471 | goto exit; |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5472 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5473 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5474 | status = psa_crypto_load_transaction( ); |
| 5475 | if( status == PSA_SUCCESS ) |
| 5476 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5477 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5478 | if( status != PSA_SUCCESS ) |
| 5479 | goto exit; |
| 5480 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5481 | } |
| 5482 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5483 | { |
| 5484 | /* There's no transaction to complete. It's all good. */ |
| 5485 | status = PSA_SUCCESS; |
| 5486 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5487 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5488 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5489 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5490 | global_data.initialized = 1; |
| 5491 | |
| 5492 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5493 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5494 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5495 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |