Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 29 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | #include "psa/crypto.h" |
| 31 | |
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" |
| 36 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 37 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 39 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 40 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 41 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 42 | /* Include internal declarations that are useful for implementing persistently |
| 43 | * stored keys. */ |
| 44 | #include "psa_crypto_storage.h" |
| 45 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 46 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 48 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 49 | #include <stdlib.h> |
| 50 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 51 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 52 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 53 | #define mbedtls_calloc calloc |
| 54 | #define mbedtls_free free |
| 55 | #endif |
| 56 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 57 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 58 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 59 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 60 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 61 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 62 | #include "mbedtls/blowfish.h" |
| 63 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 64 | #include "mbedtls/chacha20.h" |
| 65 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/cipher.h" |
| 67 | #include "mbedtls/ccm.h" |
| 68 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 69 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 70 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 71 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 72 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 73 | #include "mbedtls/error.h" |
| 74 | #include "mbedtls/gcm.h" |
| 75 | #include "mbedtls/md2.h" |
| 76 | #include "mbedtls/md4.h" |
| 77 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 78 | #include "mbedtls/md.h" |
| 79 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 80 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 81 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 82 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 83 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 84 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 85 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 86 | #include "mbedtls/sha1.h" |
| 87 | #include "mbedtls/sha256.h" |
| 88 | #include "mbedtls/sha512.h" |
| 89 | #include "mbedtls/xtea.h" |
| 90 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 91 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 92 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 93 | /* constant-time buffer comparison */ |
| 94 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 95 | { |
| 96 | size_t i; |
| 97 | unsigned char diff = 0; |
| 98 | |
| 99 | for( i = 0; i < n; i++ ) |
| 100 | diff |= a[i] ^ b[i]; |
| 101 | |
| 102 | return( diff ); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 107 | /****************************************************************/ |
| 108 | /* Global data, support functions and library management */ |
| 109 | /****************************************************************/ |
| 110 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 111 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 112 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 113 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 116 | /* Values for psa_global_data_t::rng_state */ |
| 117 | #define RNG_NOT_INITIALIZED 0 |
| 118 | #define RNG_INITIALIZED 1 |
| 119 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 120 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 121 | typedef struct |
| 122 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 123 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 124 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 125 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 126 | } psa_global_data_t; |
| 127 | |
| 128 | static psa_global_data_t global_data; |
| 129 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 130 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 131 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 132 | &global_data.rng.drbg; |
| 133 | #endif |
| 134 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 135 | #define GUARD_MODULE_INITIALIZED \ |
| 136 | if( global_data.initialized == 0 ) \ |
| 137 | return( PSA_ERROR_BAD_STATE ); |
| 138 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 139 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 140 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 141 | /* Mbed TLS error codes can combine a high-level error code and a |
| 142 | * low-level error code. The low-level error usually reflects the |
| 143 | * root cause better, so dispatch on that preferably. */ |
| 144 | int low_level_ret = - ( -ret & 0x007f ); |
| 145 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 146 | { |
| 147 | case 0: |
| 148 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 149 | |
| 150 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 151 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 152 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 153 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 154 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 155 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 156 | |
| 157 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 158 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 159 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 160 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 161 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 162 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 163 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 164 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 165 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 166 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 167 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 168 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 169 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 170 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 172 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 174 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 175 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 177 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 178 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 179 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 180 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 181 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 182 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 183 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 184 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 185 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 186 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 187 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 188 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 189 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 190 | |
| 191 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 192 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 193 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 194 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 195 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 196 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 197 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 198 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 200 | |
| 201 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 202 | return( PSA_ERROR_BAD_STATE ); |
| 203 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 204 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 205 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 206 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 207 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 208 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 209 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 210 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 211 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 212 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 213 | return( PSA_ERROR_INVALID_PADDING ); |
| 214 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 215 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 217 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 218 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 219 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 220 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 221 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 222 | |
| 223 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 224 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 225 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 226 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 227 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 228 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 229 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 231 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 232 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 233 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 234 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 235 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 236 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 237 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 238 | |
| 239 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 240 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 241 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 242 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 243 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 244 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 245 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 246 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 247 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 248 | |
| 249 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 250 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 251 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 252 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 253 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 254 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 255 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 256 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 257 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 258 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 259 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 260 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 261 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 262 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 263 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 264 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 265 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 266 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 267 | #endif |
| 268 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 269 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 270 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 271 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 272 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 273 | |
| 274 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 275 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 276 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 278 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 279 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 280 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 281 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 282 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 283 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 284 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 285 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 286 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 287 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 289 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 292 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 293 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 294 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 295 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 296 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 297 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 298 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 299 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 300 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 301 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 302 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 303 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 304 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 305 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 307 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 308 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 309 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 310 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 311 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 312 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 313 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 314 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 315 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 316 | return( PSA_ERROR_NOT_PERMITTED ); |
| 317 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 318 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 319 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 320 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 321 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 322 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 323 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 324 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 325 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 326 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 327 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 328 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 332 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 333 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 334 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 335 | |
| 336 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 337 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 338 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 339 | return( PSA_ERROR_INVALID_PADDING ); |
| 340 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 341 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 342 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 343 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 344 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 345 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 346 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 347 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 348 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 349 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 350 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 351 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 352 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 353 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 354 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 355 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 356 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 357 | |
| 358 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 359 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 360 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 361 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 362 | |
| 363 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 364 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 365 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 366 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 367 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 368 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 369 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 370 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 371 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 372 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 373 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 374 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 375 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 376 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 377 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 378 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 379 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 380 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 381 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 382 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 383 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 384 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 385 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 386 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 387 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 388 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 389 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 393 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 394 | |
| 395 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 396 | /****************************************************************/ |
| 397 | /* Key management */ |
| 398 | /****************************************************************/ |
| 399 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 400 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 401 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 402 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 403 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 404 | } |
| 405 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 406 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 407 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 408 | * current test driver in key_management.c is using this function |
| 409 | * when accelerators are used for ECC key pair and public key. |
| 410 | * Once that dependency is resolved these guards can be removed. |
| 411 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 412 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 413 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 414 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 415 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 416 | 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] | 417 | size_t bits, |
| 418 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 419 | { |
| 420 | switch( curve ) |
| 421 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 422 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 423 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 425 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 427 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 428 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
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_SECP256R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 431 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 432 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 433 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 434 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 435 | case 528: |
| 436 | if( bits_is_sloppy ) |
| 437 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 438 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 439 | } |
| 440 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 441 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 442 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 443 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 444 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 445 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 446 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 447 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 448 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 449 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 450 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 451 | } |
| 452 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 453 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 454 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 455 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 456 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 457 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 458 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 459 | case 256: |
| 460 | if( bits_is_sloppy ) |
| 461 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 462 | break; |
| 463 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 464 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 465 | } |
| 466 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 467 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 468 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 469 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 470 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 471 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 472 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 473 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 474 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 475 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 476 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 477 | } |
| 478 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 479 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 480 | |
| 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) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 484 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 485 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 486 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 487 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 488 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 489 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 490 | { |
| 491 | /* Check that the bit size is acceptable for the key type */ |
| 492 | switch( type ) |
| 493 | { |
| 494 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 496 | case PSA_KEY_TYPE_DERIVE: |
| 497 | break; |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 498 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 499 | case PSA_KEY_TYPE_AES: |
| 500 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 501 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 502 | break; |
| 503 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 504 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 505 | case PSA_KEY_TYPE_CAMELLIA: |
| 506 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 507 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 508 | break; |
| 509 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 510 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 511 | case PSA_KEY_TYPE_DES: |
| 512 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 513 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 514 | break; |
| 515 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 516 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 517 | case PSA_KEY_TYPE_ARC4: |
| 518 | if( bits < 8 || bits > 2048 ) |
| 519 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 520 | break; |
| 521 | #endif |
David Brown | 1bfe4d7 | 2021-02-16 12:54:35 -0700 | [diff] [blame] | 522 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 523 | case PSA_KEY_TYPE_CHACHA20: |
| 524 | if( bits != 256 ) |
| 525 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 526 | break; |
| 527 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 528 | default: |
| 529 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 530 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 531 | if( bits % 8 != 0 ) |
| 532 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 533 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 534 | return( PSA_SUCCESS ); |
| 535 | } |
| 536 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 537 | /** Return the size of the key in the given slot, in bits. |
| 538 | * |
| 539 | * \param[in] slot A key slot. |
| 540 | * |
| 541 | * \return The key size in bits, read from the metadata in the slot. |
| 542 | */ |
| 543 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 544 | { |
| 545 | return( slot->attr.bits ); |
| 546 | } |
| 547 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 548 | /** Try to allocate a buffer to an empty key slot. |
| 549 | * |
| 550 | * \param[in,out] slot Key slot to attach buffer to. |
| 551 | * \param[in] buffer_length Requested size of the buffer. |
| 552 | * |
| 553 | * \retval #PSA_SUCCESS |
| 554 | * The buffer has been successfully allocated. |
| 555 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 556 | * Not enough memory was available for allocation. |
| 557 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 558 | * Trying to allocate a buffer to a non-empty key slot. |
| 559 | */ |
| 560 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 561 | size_t buffer_length ) |
| 562 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 563 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 564 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 565 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 566 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 567 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 568 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 569 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 570 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 571 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 574 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 575 | const uint8_t* data, |
| 576 | size_t data_length ) |
| 577 | { |
| 578 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 579 | data_length ); |
| 580 | if( status != PSA_SUCCESS ) |
| 581 | return( status ); |
| 582 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 583 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 584 | return( PSA_SUCCESS ); |
| 585 | } |
| 586 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 587 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 588 | const psa_key_attributes_t *attributes, |
| 589 | const uint8_t *data, size_t data_length, |
| 590 | uint8_t *key_buffer, size_t key_buffer_size, |
| 591 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 592 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 593 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 594 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 595 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 596 | /* zero-length keys are never supported. */ |
| 597 | if( data_length == 0 ) |
| 598 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 599 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 600 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 601 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 602 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 603 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 604 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 605 | if( data_length > SIZE_MAX / 8 ) |
| 606 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 607 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 608 | /* Enforce a size limit, and in particular ensure that the bit |
| 609 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 610 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 611 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 612 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 613 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 614 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 615 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 616 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 617 | /* Copy the key material. */ |
| 618 | memcpy( key_buffer, data, data_length ); |
| 619 | *key_buffer_length = data_length; |
| 620 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 621 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 622 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 623 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 624 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 625 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 626 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 627 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 628 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 629 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 630 | return( mbedtls_psa_ecp_import_key( attributes, |
| 631 | data, data_length, |
| 632 | key_buffer, key_buffer_size, |
| 633 | key_buffer_length, |
| 634 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 635 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 636 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 637 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 638 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 639 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 640 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 641 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 642 | return( mbedtls_psa_rsa_import_key( attributes, |
| 643 | data, data_length, |
| 644 | key_buffer, key_buffer_size, |
| 645 | key_buffer_length, |
| 646 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 647 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 648 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 649 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 650 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 651 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 652 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 655 | /** Calculate the intersection of two algorithm usage policies. |
| 656 | * |
| 657 | * Return 0 (which allows no operation) on incompatibility. |
| 658 | */ |
| 659 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 660 | psa_algorithm_t alg1, |
| 661 | psa_algorithm_t alg2 ) |
| 662 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 663 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 664 | if( alg1 == alg2 ) |
| 665 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 666 | /* If the policies are from the same hash-and-sign family, check |
| 667 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 668 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 669 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 670 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 671 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 672 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 673 | return( alg2 ); |
| 674 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 675 | return( alg1 ); |
| 676 | } |
| 677 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 678 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 679 | * restrictive tag length. */ |
| 680 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 681 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 682 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 683 | { |
| 684 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 685 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
| 686 | size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 687 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 688 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 689 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 690 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 691 | { |
Steven Cooreman | 5d81481 | 2021-02-18 12:11:39 +0100 | [diff] [blame] | 692 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 693 | } |
| 694 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 695 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | ( alg1_len <= alg2_len ) ) |
| 697 | { |
| 698 | return( alg2 ); |
| 699 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 700 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 701 | ( alg2_len <= alg1_len ) ) |
| 702 | { |
| 703 | return( alg1 ); |
| 704 | } |
| 705 | } |
| 706 | /* If the policies are from the same MAC family, check whether one |
| 707 | * of them is a minimum-MAC-length policy. Calculate the most |
| 708 | * restrictive tag length. */ |
| 709 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 710 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 711 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 712 | { |
| 713 | size_t alg1_len = PSA_MAC_TRUNCATED_LENGTH( alg1 ); |
| 714 | size_t alg2_len = PSA_MAC_TRUNCATED_LENGTH( alg2 ); |
| 715 | size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 716 | |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 717 | /* If both are wildcards, return most restricitve wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 718 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 719 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 720 | { |
Steven Cooreman | caad493 | 2021-02-18 11:28:17 +0100 | [diff] [blame] | 721 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 722 | } |
| 723 | /* If only one is a wildcard, return specific algorithm if compatible. |
| 724 | * Special case: specific MAC algorithm with '0' as length means full- |
| 725 | * length MAC, which is always allowed by a wildcard with the same |
| 726 | * base algorithm. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 727 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 728 | ( ( alg1_len <= alg2_len ) || |
| 729 | ( alg2 == PSA_ALG_FULL_LENGTH_MAC( alg1 ) ) ) ) |
| 730 | { |
| 731 | return( alg2 ); |
| 732 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 733 | if( ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | ( ( alg2_len <= alg1_len ) || |
| 735 | ( alg1 == PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) ) |
| 736 | { |
| 737 | return( alg1 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 738 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 739 | } |
| 740 | /* If the policies are incompatible, allow nothing. */ |
| 741 | return( 0 ); |
| 742 | } |
| 743 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 744 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 745 | psa_algorithm_t requested_alg ) |
| 746 | { |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 747 | /* A requested algorithm cannot be a wildcard. */ |
| 748 | if( PSA_ALG_IS_WILDCARD( requested_alg ) ) |
| 749 | return( 0 ); |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 750 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 751 | if( requested_alg == policy_alg ) |
| 752 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 753 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 754 | * and requested_alg is the same hash-and-sign family with any hash, |
| 755 | * then requested_alg is compliant with policy_alg. */ |
| 756 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 757 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 758 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 759 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 760 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 761 | } |
| 762 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 763 | * the requested algorithm, check the requested tag length to be |
| 764 | * equal-length or longer than the wildcard-specified length. */ |
| 765 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 766 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 767 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 768 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 769 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 770 | { |
| 771 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 772 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 773 | } |
| 774 | /* If policy_alg is a wildcard MAC algorithm of the same base as |
| 775 | * the requested algorithm, check the requested tag length to be |
| 776 | * equal-length or longer than the wildcard-specified length. */ |
| 777 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 778 | PSA_ALG_IS_MAC( requested_alg ) && |
| 779 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
| 780 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 781 | ( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 782 | { |
| 783 | /* Special case: full-length MAC is encoded with 0-length. |
| 784 | * A minimum-length policy will always allow a full-length MAC. */ |
| 785 | if( PSA_ALG_FULL_LENGTH_MAC( requested_alg ) == requested_alg ) |
| 786 | return( 1 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 787 | |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 788 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
| 789 | PSA_MAC_TRUNCATED_LENGTH( requested_alg ) ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 790 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 791 | /* 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] | 792 | * a key derivation with that key agreement should also be allowed. This |
| 793 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 794 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 795 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 796 | { |
| 797 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 798 | policy_alg ); |
| 799 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 800 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 801 | return( 0 ); |
| 802 | } |
| 803 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 804 | /** Test whether a policy permits an algorithm. |
| 805 | * |
| 806 | * The caller must test usage flags separately. |
| 807 | */ |
| 808 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 809 | psa_algorithm_t alg ) |
| 810 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 811 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 812 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 815 | /** Restrict a key policy based on a constraint. |
| 816 | * |
| 817 | * \param[in,out] policy The policy to restrict. |
| 818 | * \param[in] constraint The policy constraint to apply. |
| 819 | * |
| 820 | * \retval #PSA_SUCCESS |
| 821 | * \c *policy contains the intersection of the original value of |
| 822 | * \c *policy and \c *constraint. |
| 823 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 824 | * \c *policy and \c *constraint are incompatible. |
| 825 | * \c *policy is unchanged. |
| 826 | */ |
| 827 | static psa_status_t psa_restrict_key_policy( |
| 828 | psa_key_policy_t *policy, |
| 829 | const psa_key_policy_t *constraint ) |
| 830 | { |
| 831 | psa_algorithm_t intersection_alg = |
| 832 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 833 | psa_algorithm_t intersection_alg2 = |
| 834 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 835 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 836 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 837 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 838 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 839 | policy->usage &= constraint->usage; |
| 840 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 841 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 842 | return( PSA_SUCCESS ); |
| 843 | } |
| 844 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 845 | /** Get the description of a key given its identifier and policy constraints |
| 846 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 847 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 848 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 849 | * nonzero, the key must allow operations with this algorithm. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 850 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 851 | * In case of a persistent key, the function loads the description of the key |
| 852 | * into a key slot if not already done. |
| 853 | * |
| 854 | * On success, the returned key slot is locked. It is the responsibility of |
| 855 | * 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] | 856 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 857 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 858 | mbedtls_svc_key_id_t key, |
| 859 | psa_key_slot_t **p_slot, |
| 860 | psa_key_usage_t usage, |
| 861 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 862 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 863 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 864 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 865 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 866 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 867 | if( status != PSA_SUCCESS ) |
| 868 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 869 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 870 | |
| 871 | /* Enforce that usage policy for the key slot contains all the flags |
| 872 | * required by the usage parameter. There is one exception: public |
| 873 | * keys can always be exported, so we treat public key objects as |
| 874 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 875 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 876 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 877 | |
| 878 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 879 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 880 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 881 | |
| 882 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 883 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 884 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 885 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 886 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 887 | |
| 888 | error: |
| 889 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 890 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 891 | |
| 892 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 893 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 894 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 895 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 896 | * |
| 897 | * A transparent key is a key for which the key material is directly |
| 898 | * available, as opposed to a key in a secure element. |
| 899 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 900 | * This is a temporary function to use instead of |
| 901 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 902 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 903 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 904 | * On success, the returned key slot is locked. It is the responsibility of the |
| 905 | * 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] | 906 | */ |
| 907 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 908 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 909 | mbedtls_svc_key_id_t key, |
| 910 | psa_key_slot_t **p_slot, |
| 911 | psa_key_usage_t usage, |
| 912 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 913 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 914 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 915 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 916 | if( status != PSA_SUCCESS ) |
| 917 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 918 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 919 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 920 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 921 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 922 | *p_slot = NULL; |
| 923 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 924 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 925 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 926 | return( PSA_SUCCESS ); |
| 927 | } |
| 928 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 929 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 930 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 931 | psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 932 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 933 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 934 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 935 | static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 936 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 937 | /* Data pointer will always be either a valid pointer or NULL in an |
| 938 | * initialized slot, so we can just free it. */ |
| 939 | if( slot->key.data != NULL ) |
| 940 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 941 | |
| 942 | mbedtls_free( slot->key.data ); |
| 943 | slot->key.data = NULL; |
| 944 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 945 | |
| 946 | return( PSA_SUCCESS ); |
| 947 | } |
| 948 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 949 | /** Completely wipe a slot in memory, including its policy. |
| 950 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 951 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 952 | { |
| 953 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 954 | |
| 955 | /* |
| 956 | * 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] | 957 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 958 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 959 | * 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] | 960 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 961 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 962 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 963 | { |
| 964 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 965 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 966 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 967 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 968 | } |
| 969 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 970 | /* Multipart operations may still be using the key. This is safe |
| 971 | * because all multipart operation objects are independent from |
| 972 | * the key slot: if they need to access the key after the setup |
| 973 | * phase, they have a copy of the key. Note that this means that |
| 974 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 975 | /* At this point, key material and other type-specific content has |
| 976 | * been wiped. Clear remaining metadata. We can call memset and not |
| 977 | * zeroize because the metadata is not particularly sensitive. */ |
| 978 | memset( slot, 0, sizeof( *slot ) ); |
| 979 | return( status ); |
| 980 | } |
| 981 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 982 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 983 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 984 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 985 | psa_status_t status; /* status of the last operation */ |
| 986 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 987 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 988 | psa_se_drv_table_entry_t *driver; |
| 989 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 990 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 991 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 992 | return( PSA_SUCCESS ); |
| 993 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 994 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 995 | * 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] | 996 | * key, this will load the key description from persistent memory if not |
| 997 | * done yet. We cannot avoid this loading as without it we don't know if |
| 998 | * the key is operated by an SE or not and this information is needed by |
| 999 | * the current implementation. |
| 1000 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1001 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1002 | if( status != PSA_SUCCESS ) |
| 1003 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1004 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1005 | /* |
| 1006 | * If the key slot containing the key description is under access by the |
| 1007 | * library (apart from the present access), the key cannot be destroyed |
| 1008 | * yet. For the time being, just return in error. Eventually (to be |
| 1009 | * implemented), the key should be destroyed when all accesses have |
| 1010 | * stopped. |
| 1011 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1012 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1013 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1014 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1015 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1016 | } |
| 1017 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1018 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1019 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1020 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1021 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1022 | /* For a key in a secure element, we need to do three things: |
| 1023 | * remove the key file in internal storage, destroy the |
| 1024 | * key inside the secure element, and update the driver's |
| 1025 | * persistent data. Start a transaction that will encompass these |
| 1026 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1027 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1028 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1029 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1030 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1031 | status = psa_crypto_save_transaction( ); |
| 1032 | if( status != PSA_SUCCESS ) |
| 1033 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1034 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1035 | /* We should still try to destroy the key in the secure |
| 1036 | * element and the key metadata in storage. This is especially |
| 1037 | * important if the error is that the storage is full. |
| 1038 | * But how to do it exactly without risking an inconsistent |
| 1039 | * state after a reset? |
| 1040 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1041 | */ |
| 1042 | overall_status = status; |
| 1043 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1046 | status = psa_destroy_se_key( driver, |
| 1047 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1048 | if( overall_status == PSA_SUCCESS ) |
| 1049 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1050 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1051 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1052 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1053 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1054 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1055 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1056 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1057 | if( overall_status == PSA_SUCCESS ) |
| 1058 | overall_status = status; |
| 1059 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1060 | /* TODO: other slots may have a copy of the same key. We should |
| 1061 | * invalidate them. |
| 1062 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1063 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1064 | } |
| 1065 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1066 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1067 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1068 | if( driver != NULL ) |
| 1069 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1070 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1071 | if( overall_status == PSA_SUCCESS ) |
| 1072 | overall_status = status; |
| 1073 | status = psa_crypto_stop_transaction( ); |
| 1074 | if( overall_status == PSA_SUCCESS ) |
| 1075 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1076 | } |
| 1077 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1078 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1079 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1080 | exit: |
| 1081 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1082 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1083 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1084 | if( overall_status == PSA_SUCCESS ) |
| 1085 | overall_status = status; |
| 1086 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1089 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1090 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1091 | static psa_status_t psa_get_rsa_public_exponent( |
| 1092 | const mbedtls_rsa_context *rsa, |
| 1093 | psa_key_attributes_t *attributes ) |
| 1094 | { |
| 1095 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1096 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1097 | uint8_t *buffer = NULL; |
| 1098 | size_t buflen; |
| 1099 | mbedtls_mpi_init( &mpi ); |
| 1100 | |
| 1101 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1102 | if( ret != 0 ) |
| 1103 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1104 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1105 | { |
| 1106 | /* It's the default value, which is reported as an empty string, |
| 1107 | * so there's nothing to do. */ |
| 1108 | goto exit; |
| 1109 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1110 | |
| 1111 | buflen = mbedtls_mpi_size( &mpi ); |
| 1112 | buffer = mbedtls_calloc( 1, buflen ); |
| 1113 | if( buffer == NULL ) |
| 1114 | { |
| 1115 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1116 | goto exit; |
| 1117 | } |
| 1118 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1119 | if( ret != 0 ) |
| 1120 | goto exit; |
| 1121 | attributes->domain_parameters = buffer; |
| 1122 | attributes->domain_parameters_size = buflen; |
| 1123 | |
| 1124 | exit: |
| 1125 | mbedtls_mpi_free( &mpi ); |
| 1126 | if( ret != 0 ) |
| 1127 | mbedtls_free( buffer ); |
| 1128 | return( mbedtls_to_psa_error( ret ) ); |
| 1129 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1130 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1131 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1132 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1133 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1134 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1135 | 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] | 1136 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1137 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1138 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1139 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1140 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1141 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1142 | psa_reset_key_attributes( attributes ); |
| 1143 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1144 | 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] | 1145 | if( status != PSA_SUCCESS ) |
| 1146 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1147 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1148 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1149 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1150 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1151 | |
| 1152 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1153 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1154 | psa_set_key_slot_number( attributes, |
| 1155 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1156 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1157 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1158 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1159 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1160 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1161 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1162 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1163 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1164 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1165 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1166 | * is not yet implemented. |
| 1167 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1168 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1169 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1170 | break; |
| 1171 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1172 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1173 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1174 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1175 | status = mbedtls_psa_rsa_load_representation( |
| 1176 | slot->attr.type, |
| 1177 | slot->key.data, |
| 1178 | slot->key.bytes, |
| 1179 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1180 | if( status != PSA_SUCCESS ) |
| 1181 | break; |
| 1182 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1183 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1184 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1185 | mbedtls_rsa_free( rsa ); |
| 1186 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1187 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1188 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1189 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1190 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1191 | default: |
| 1192 | /* Nothing else to do. */ |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | if( status != PSA_SUCCESS ) |
| 1197 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1198 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1199 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1200 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1201 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1204 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1205 | psa_status_t psa_get_key_slot_number( |
| 1206 | const psa_key_attributes_t *attributes, |
| 1207 | psa_key_slot_number_t *slot_number ) |
| 1208 | { |
| 1209 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1210 | { |
| 1211 | *slot_number = attributes->slot_number; |
| 1212 | return( PSA_SUCCESS ); |
| 1213 | } |
| 1214 | else |
| 1215 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1216 | } |
| 1217 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1218 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1219 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1220 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1221 | uint8_t *data, |
| 1222 | size_t data_size, |
| 1223 | size_t *data_length ) |
| 1224 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1225 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1226 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1227 | memcpy( data, key_buffer, key_buffer_size ); |
| 1228 | memset( data + key_buffer_size, 0, |
| 1229 | data_size - key_buffer_size ); |
| 1230 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1231 | return( PSA_SUCCESS ); |
| 1232 | } |
| 1233 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1234 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1235 | const psa_key_attributes_t *attributes, |
| 1236 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1237 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1238 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1239 | psa_key_type_t type = attributes->core.type; |
| 1240 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1241 | if( key_type_is_raw_bytes( type ) || |
| 1242 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1243 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1244 | { |
| 1245 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1246 | key_buffer, key_buffer_size, |
| 1247 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1248 | } |
| 1249 | else |
| 1250 | { |
| 1251 | /* This shouldn't happen in the reference implementation, but |
| 1252 | it is valid for a special-purpose implementation to omit |
| 1253 | support for exporting certain key types. */ |
| 1254 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1259 | uint8_t *data, |
| 1260 | size_t data_size, |
| 1261 | size_t *data_length ) |
| 1262 | { |
| 1263 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1264 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1265 | psa_key_slot_t *slot; |
| 1266 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1267 | /* Reject a zero-length output buffer now, since this can never be a |
| 1268 | * valid key representation. This way we know that data must be a valid |
| 1269 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1270 | if( data_size == 0 ) |
| 1271 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1272 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1273 | /* Set the key to empty now, so that even when there are errors, we always |
| 1274 | * set data_length to a value between 0 and data_size. On error, setting |
| 1275 | * the key to empty is a good choice because an empty key representation is |
| 1276 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1277 | *data_length = 0; |
| 1278 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1279 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1280 | * which don't require any flag, but |
| 1281 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1282 | */ |
| 1283 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1284 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1285 | if( status != PSA_SUCCESS ) |
| 1286 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1287 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1288 | psa_key_attributes_t attributes = { |
| 1289 | .core = slot->attr |
| 1290 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1291 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1292 | slot->key.data, slot->key.bytes, |
| 1293 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1294 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1295 | unlock_status = psa_unlock_key_slot( slot ); |
| 1296 | |
| 1297 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1298 | } |
| 1299 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1300 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1301 | const psa_key_attributes_t *attributes, |
| 1302 | const uint8_t *key_buffer, |
| 1303 | size_t key_buffer_size, |
| 1304 | uint8_t *data, |
| 1305 | size_t data_size, |
| 1306 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1307 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1308 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1309 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1310 | 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] | 1311 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1312 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1313 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1314 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1315 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1316 | key_buffer, key_buffer_size, |
| 1317 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1318 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1319 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1320 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1321 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1322 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1323 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1324 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1325 | key_buffer, |
| 1326 | key_buffer_size, |
| 1327 | data, |
| 1328 | data_size, |
| 1329 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1330 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1331 | /* We don't know how to convert a private RSA key to public. */ |
| 1332 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1333 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1334 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1335 | } |
| 1336 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1337 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1338 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1339 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1340 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1341 | key_buffer, |
| 1342 | key_buffer_size, |
| 1343 | data, |
| 1344 | data_size, |
| 1345 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1346 | #else |
| 1347 | /* 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] | 1348 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1349 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1350 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1351 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1353 | else |
| 1354 | { |
| 1355 | /* This shouldn't happen in the reference implementation, but |
| 1356 | it is valid for a special-purpose implementation to omit |
| 1357 | support for exporting certain key types. */ |
| 1358 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1359 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1360 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1361 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1362 | 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] | 1363 | uint8_t *data, |
| 1364 | size_t data_size, |
| 1365 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1366 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1367 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1368 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1369 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1370 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1371 | /* Reject a zero-length output buffer now, since this can never be a |
| 1372 | * valid key representation. This way we know that data must be a valid |
| 1373 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1374 | if( data_size == 0 ) |
| 1375 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1376 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1377 | /* Set the key to empty now, so that even when there are errors, we always |
| 1378 | * set data_length to a value between 0 and data_size. On error, setting |
| 1379 | * the key to empty is a good choice because an empty key representation is |
| 1380 | * unlikely to be accepted anywhere. */ |
| 1381 | *data_length = 0; |
| 1382 | |
| 1383 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1384 | 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] | 1385 | if( status != PSA_SUCCESS ) |
| 1386 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1387 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1388 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1389 | { |
| 1390 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1391 | goto exit; |
| 1392 | } |
| 1393 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1394 | psa_key_attributes_t attributes = { |
| 1395 | .core = slot->attr |
| 1396 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1397 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1398 | &attributes, slot->key.data, slot->key.bytes, |
| 1399 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1400 | |
| 1401 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1402 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1403 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1404 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1405 | } |
| 1406 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1407 | #if defined(static_assert) |
| 1408 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1409 | "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] | 1410 | 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] | 1411 | "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] | 1412 | 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] | 1413 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1414 | #endif |
| 1415 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1416 | /** Validate that a key policy is internally well-formed. |
| 1417 | * |
| 1418 | * This function only rejects invalid policies. It does not validate the |
| 1419 | * consistency of the policy with respect to other attributes of the key |
| 1420 | * such as the key type. |
| 1421 | */ |
| 1422 | 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] | 1423 | { |
| 1424 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1425 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1426 | PSA_KEY_USAGE_ENCRYPT | |
| 1427 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1428 | PSA_KEY_USAGE_SIGN_HASH | |
| 1429 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1430 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1431 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1432 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1433 | return( PSA_SUCCESS ); |
| 1434 | } |
| 1435 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1436 | /** Validate the internal consistency of key attributes. |
| 1437 | * |
| 1438 | * This function only rejects invalid attribute values. If does not |
| 1439 | * validate the consistency of the attributes with any key data that may |
| 1440 | * be involved in the creation of the key. |
| 1441 | * |
| 1442 | * Call this function early in the key creation process. |
| 1443 | * |
| 1444 | * \param[in] attributes Key attributes for the new key. |
| 1445 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1446 | * NULL for a transparent key. |
| 1447 | * |
| 1448 | */ |
| 1449 | static psa_status_t psa_validate_key_attributes( |
| 1450 | const psa_key_attributes_t *attributes, |
| 1451 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1452 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1453 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1454 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1455 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1456 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1457 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1458 | if( status != PSA_SUCCESS ) |
| 1459 | return( status ); |
| 1460 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1461 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1462 | if( status != PSA_SUCCESS ) |
| 1463 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1464 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1465 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1466 | { |
| 1467 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1469 | } |
| 1470 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1471 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1472 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1473 | if( status != PSA_SUCCESS ) |
| 1474 | return( status ); |
| 1475 | } |
| 1476 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1477 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1478 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1479 | return( status ); |
| 1480 | |
| 1481 | /* Refuse to create overly large keys. |
| 1482 | * Note that this doesn't trigger on import if the attributes don't |
| 1483 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1484 | * psa_import_key() needs its own checks. */ |
| 1485 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1486 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1487 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1488 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1489 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1490 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1491 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1492 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1493 | return( PSA_SUCCESS ); |
| 1494 | } |
| 1495 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1496 | /** Prepare a key slot to receive key material. |
| 1497 | * |
| 1498 | * This function allocates a key slot and sets its metadata. |
| 1499 | * |
| 1500 | * If this function fails, call psa_fail_key_creation(). |
| 1501 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1502 | * This function is intended to be used as follows: |
| 1503 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1504 | * it with the specified attributes, and in case of a volatile key assign it |
| 1505 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1506 | * -# Populate the slot with the key material. |
| 1507 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1508 | * In case of failure at any step, stop the sequence and call |
| 1509 | * psa_fail_key_creation(). |
| 1510 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1511 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1512 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1513 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1514 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1515 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1516 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1517 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1518 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1519 | * |
| 1520 | * \retval #PSA_SUCCESS |
| 1521 | * The key slot is ready to receive key material. |
| 1522 | * \return If this function fails, the key slot is an invalid state. |
| 1523 | * 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] | 1524 | */ |
| 1525 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1526 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1527 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1528 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1529 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1530 | { |
| 1531 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1532 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1533 | psa_key_slot_t *slot; |
| 1534 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1535 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1536 | *p_drv = NULL; |
| 1537 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1538 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1539 | if( status != PSA_SUCCESS ) |
| 1540 | return( status ); |
| 1541 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1542 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1543 | if( status != PSA_SUCCESS ) |
| 1544 | return( status ); |
| 1545 | slot = *p_slot; |
| 1546 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1547 | /* We're storing the declared bit-size of the key. It's up to each |
| 1548 | * creation mechanism to verify that this information is correct. |
| 1549 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1550 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1551 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1552 | * volatile key identifier associated to the slot returned to contain its |
| 1553 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1554 | |
| 1555 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1556 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1557 | { |
| 1558 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1559 | slot->attr.id = volatile_key_id; |
| 1560 | #else |
| 1561 | slot->attr.id.key_id = volatile_key_id; |
| 1562 | #endif |
| 1563 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1564 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1565 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1566 | * external-only flags, query `attributes`. Thanks to the check |
| 1567 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1568 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1569 | * may have set. */ |
| 1570 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1571 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1572 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1573 | /* 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] | 1574 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1575 | * create the key file in internal storage, create the |
| 1576 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1577 | * persistent data. This is done by starting a transaction that will |
| 1578 | * encompass these three actions. |
| 1579 | * For registering a volatile key, we just need to find an appropriate |
| 1580 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1581 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1582 | /* The first thing to do is to find a slot number for the new key. |
| 1583 | * We save the slot number in persistent storage as part of the |
| 1584 | * transaction data. It will be needed to recover if the power |
| 1585 | * fails during the key creation process, to clean up on the secure |
| 1586 | * element side after restarting. Obtaining a slot number from the |
| 1587 | * secure element driver updates its persistent state, but we do not yet |
| 1588 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1589 | * 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] | 1590 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1591 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1592 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1593 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1594 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1595 | if( status != PSA_SUCCESS ) |
| 1596 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1597 | |
| 1598 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1599 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1600 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1601 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1602 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1603 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1604 | status = psa_crypto_save_transaction( ); |
| 1605 | if( status != PSA_SUCCESS ) |
| 1606 | { |
| 1607 | (void) psa_crypto_stop_transaction( ); |
| 1608 | return( status ); |
| 1609 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1610 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1611 | |
| 1612 | status = psa_copy_key_material_into_slot( |
| 1613 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1614 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1615 | |
| 1616 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1617 | { |
| 1618 | /* Key registration only makes sense with a secure element. */ |
| 1619 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1620 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1621 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1622 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1623 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1624 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1625 | |
| 1626 | /** Finalize the creation of a key once its key material has been set. |
| 1627 | * |
| 1628 | * This entails writing the key to persistent storage. |
| 1629 | * |
| 1630 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1631 | * See the documentation of psa_start_key_creation() for the intended use |
| 1632 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1633 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1634 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1635 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1636 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1637 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1638 | * \param[in,out] slot Pointer to the slot with key material. |
| 1639 | * \param[in] driver The secure element driver for the key, |
| 1640 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1641 | * \param[out] key On success, identifier of the key. Note that the |
| 1642 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1643 | * |
| 1644 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1645 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1646 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1647 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1648 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1649 | * \retval #PSA_ERROR_DATA_INVALID |
| 1650 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1651 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1652 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1653 | * \return If this function fails, the key slot is an invalid state. |
| 1654 | * 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] | 1655 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1656 | static psa_status_t psa_finish_key_creation( |
| 1657 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1658 | psa_se_drv_table_entry_t *driver, |
| 1659 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1660 | { |
| 1661 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1662 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1663 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1664 | |
| 1665 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1666 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1667 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1668 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1669 | if( driver != NULL ) |
| 1670 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1671 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1672 | psa_key_slot_number_t slot_number = |
| 1673 | psa_key_slot_get_slot_number( slot ) ; |
| 1674 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1675 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1676 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1677 | sizeof( data.slot_number ), |
| 1678 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1679 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1680 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1681 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1682 | (uint8_t*) &data, |
| 1683 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1684 | } |
| 1685 | else |
| 1686 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1687 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1688 | /* Key material is saved in export representation in the slot, so |
| 1689 | * just pass the slot buffer for storage. */ |
| 1690 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1691 | slot->key.data, |
| 1692 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1693 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1694 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1695 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1696 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1697 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1698 | /* Finish the transaction for a key creation. This does not |
| 1699 | * happen when registering an existing key. Detect this case |
| 1700 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1701 | * creation of a persistent key in a secure element requires a transaction, |
| 1702 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1703 | if( driver != NULL && |
| 1704 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1705 | { |
| 1706 | status = psa_save_se_persistent_data( driver ); |
| 1707 | if( status != PSA_SUCCESS ) |
| 1708 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1709 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1710 | return( status ); |
| 1711 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1712 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1713 | } |
| 1714 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1715 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1716 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1717 | { |
| 1718 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1719 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1720 | if( status != PSA_SUCCESS ) |
| 1721 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1722 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1723 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1724 | return( status ); |
| 1725 | } |
| 1726 | |
| 1727 | /** Abort the creation of a key. |
| 1728 | * |
| 1729 | * You may call this function after calling psa_start_key_creation(), |
| 1730 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1731 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1732 | * See the documentation of psa_start_key_creation() for the intended use |
| 1733 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1734 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1735 | * \param[in,out] slot Pointer to the slot with key material. |
| 1736 | * \param[in] driver The secure element driver for the key, |
| 1737 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1738 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1739 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1740 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1741 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1742 | (void) driver; |
| 1743 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1744 | if( slot == NULL ) |
| 1745 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1746 | |
| 1747 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1748 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1749 | * element, and the failure happened later (when saving metadata |
| 1750 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1751 | * element. |
| 1752 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1753 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1754 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1755 | /* Abort the ongoing transaction if any (there may not be one if |
| 1756 | * the creation process failed before starting one, or if the |
| 1757 | * key creation is a registration of a key in a secure element). |
| 1758 | * Earlier functions must already have done what it takes to undo any |
| 1759 | * partial creation. All that's left is to update the transaction data |
| 1760 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1761 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1762 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1763 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1764 | psa_wipe_key_slot( slot ); |
| 1765 | } |
| 1766 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1767 | /** Validate optional attributes during key creation. |
| 1768 | * |
| 1769 | * Some key attributes are optional during key creation. If they are |
| 1770 | * specified in the attributes structure, check that they are consistent |
| 1771 | * with the data in the slot. |
| 1772 | * |
| 1773 | * This function should be called near the end of key creation, after |
| 1774 | * the slot in memory is fully populated but before saving persistent data. |
| 1775 | */ |
| 1776 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1777 | const psa_key_slot_t *slot, |
| 1778 | const psa_key_attributes_t *attributes ) |
| 1779 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1780 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1781 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1782 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1783 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1784 | } |
| 1785 | |
| 1786 | if( attributes->domain_parameters_size != 0 ) |
| 1787 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1788 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1789 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1790 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1791 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1792 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1793 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1794 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1795 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1796 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1797 | slot->attr.type, |
| 1798 | slot->key.data, |
| 1799 | slot->key.bytes, |
| 1800 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1801 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1802 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1803 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1804 | mbedtls_mpi_init( &actual ); |
| 1805 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1806 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1807 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1808 | mbedtls_rsa_free( rsa ); |
| 1809 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1810 | if( ret != 0 ) |
| 1811 | goto rsa_exit; |
| 1812 | ret = mbedtls_mpi_read_binary( &required, |
| 1813 | attributes->domain_parameters, |
| 1814 | attributes->domain_parameters_size ); |
| 1815 | if( ret != 0 ) |
| 1816 | goto rsa_exit; |
| 1817 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1818 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1819 | rsa_exit: |
| 1820 | mbedtls_mpi_free( &actual ); |
| 1821 | mbedtls_mpi_free( &required ); |
| 1822 | if( ret != 0) |
| 1823 | return( mbedtls_to_psa_error( ret ) ); |
| 1824 | } |
| 1825 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1826 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1827 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1828 | { |
| 1829 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1830 | } |
| 1831 | } |
| 1832 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1833 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1834 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1835 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1836 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1837 | } |
| 1838 | |
| 1839 | return( PSA_SUCCESS ); |
| 1840 | } |
| 1841 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1842 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1843 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1844 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1845 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1846 | { |
| 1847 | psa_status_t status; |
| 1848 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1849 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1850 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1851 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1852 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1853 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1854 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1855 | * This also rejects any key which might be encoded as an empty string, |
| 1856 | * which is never valid. */ |
| 1857 | if( data_length == 0 ) |
| 1858 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1859 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1860 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1861 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1862 | if( status != PSA_SUCCESS ) |
| 1863 | goto exit; |
| 1864 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1865 | /* In the case of a transparent key or an opaque key stored in local |
| 1866 | * storage (thus not in the case of generating a key in a secure element |
| 1867 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1868 | * hold the generated key material. */ |
| 1869 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1870 | { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1871 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1872 | if( status != PSA_SUCCESS ) |
| 1873 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1874 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1875 | |
| 1876 | bits = slot->attr.bits; |
| 1877 | status = psa_driver_wrapper_import_key( attributes, |
| 1878 | data, data_length, |
| 1879 | slot->key.data, |
| 1880 | slot->key.bytes, |
| 1881 | &slot->key.bytes, &bits ); |
| 1882 | if( status != PSA_SUCCESS ) |
| 1883 | goto exit; |
| 1884 | |
| 1885 | if( slot->attr.bits == 0 ) |
| 1886 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1887 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1888 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1889 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1890 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1891 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1892 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1893 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1894 | if( status != PSA_SUCCESS ) |
| 1895 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1896 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1897 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1898 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1899 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1900 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1901 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1902 | return( status ); |
| 1903 | } |
| 1904 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1905 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1906 | psa_status_t mbedtls_psa_register_se_key( |
| 1907 | const psa_key_attributes_t *attributes ) |
| 1908 | { |
| 1909 | psa_status_t status; |
| 1910 | psa_key_slot_t *slot = NULL; |
| 1911 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1912 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1913 | |
| 1914 | /* Leaving attributes unspecified is not currently supported. |
| 1915 | * It could make sense to query the key type and size from the |
| 1916 | * secure element, but not all secure elements support this |
| 1917 | * and the driver HAL doesn't currently support it. */ |
| 1918 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1919 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1920 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1921 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1922 | |
| 1923 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1924 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1925 | if( status != PSA_SUCCESS ) |
| 1926 | goto exit; |
| 1927 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1928 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1929 | |
| 1930 | exit: |
| 1931 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1932 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1933 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1934 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1935 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1936 | return( status ); |
| 1937 | } |
| 1938 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1939 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1940 | 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] | 1941 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1942 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1943 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1944 | source->key.data, |
| 1945 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1946 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 1947 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1948 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 1949 | target->attr.type = source->attr.type; |
| 1950 | target->attr.bits = source->attr.bits; |
| 1951 | |
| 1952 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1953 | } |
| 1954 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1955 | 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] | 1956 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1957 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1958 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1959 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1960 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1961 | psa_key_slot_t *source_slot = NULL; |
| 1962 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1963 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1964 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1965 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1966 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1967 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1968 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 1969 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1970 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1971 | goto exit; |
| 1972 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1973 | status = psa_validate_optional_attributes( source_slot, |
| 1974 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1975 | if( status != PSA_SUCCESS ) |
| 1976 | goto exit; |
| 1977 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1978 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1979 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1980 | if( status != PSA_SUCCESS ) |
| 1981 | goto exit; |
| 1982 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1983 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 1984 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1985 | if( status != PSA_SUCCESS ) |
| 1986 | goto exit; |
| 1987 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1988 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1989 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1990 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1991 | /* Copying to a secure element is not implemented yet. */ |
| 1992 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1993 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1994 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1995 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1996 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1997 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1998 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1999 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2000 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2001 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2002 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2003 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2004 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2005 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2006 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2007 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2008 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2009 | } |
| 2010 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2011 | |
| 2012 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2013 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2014 | /* Message digests */ |
| 2015 | /****************************************************************/ |
| 2016 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2017 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2018 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2019 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2020 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2021 | static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2022 | { |
| 2023 | switch( alg ) |
| 2024 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2025 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2026 | case PSA_ALG_MD2: |
| 2027 | return( &mbedtls_md2_info ); |
| 2028 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2029 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2030 | case PSA_ALG_MD4: |
| 2031 | return( &mbedtls_md4_info ); |
| 2032 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2033 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2034 | case PSA_ALG_MD5: |
| 2035 | return( &mbedtls_md5_info ); |
| 2036 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2037 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2038 | case PSA_ALG_RIPEMD160: |
| 2039 | return( &mbedtls_ripemd160_info ); |
| 2040 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2041 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2042 | case PSA_ALG_SHA_1: |
| 2043 | return( &mbedtls_sha1_info ); |
| 2044 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2045 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2046 | case PSA_ALG_SHA_224: |
| 2047 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2048 | #endif |
| 2049 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2050 | case PSA_ALG_SHA_256: |
| 2051 | return( &mbedtls_sha256_info ); |
| 2052 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2053 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2054 | case PSA_ALG_SHA_384: |
| 2055 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2056 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2057 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2058 | case PSA_ALG_SHA_512: |
| 2059 | return( &mbedtls_sha512_info ); |
| 2060 | #endif |
| 2061 | default: |
| 2062 | return( NULL ); |
| 2063 | } |
| 2064 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2065 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2066 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2067 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2068 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2069 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2070 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2071 | { |
| 2072 | switch( operation->alg ) |
| 2073 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2074 | case 0: |
| 2075 | /* The object has (apparently) been initialized but it is not |
| 2076 | * in use. It's ok to call abort on such an object, and there's |
| 2077 | * nothing to do. */ |
| 2078 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2079 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2080 | case PSA_ALG_MD2: |
| 2081 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2082 | break; |
| 2083 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2084 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2085 | case PSA_ALG_MD4: |
| 2086 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2087 | break; |
| 2088 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2089 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2090 | case PSA_ALG_MD5: |
| 2091 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2092 | break; |
| 2093 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2094 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2095 | case PSA_ALG_RIPEMD160: |
| 2096 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2097 | break; |
| 2098 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2099 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2100 | case PSA_ALG_SHA_1: |
| 2101 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2102 | break; |
| 2103 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2104 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2105 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2106 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2107 | break; |
| 2108 | #endif |
| 2109 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2110 | case PSA_ALG_SHA_256: |
| 2111 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2112 | break; |
| 2113 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2114 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2115 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2116 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2117 | break; |
| 2118 | #endif |
| 2119 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2120 | case PSA_ALG_SHA_512: |
| 2121 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2122 | break; |
| 2123 | #endif |
| 2124 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2125 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2126 | } |
| 2127 | operation->alg = 0; |
| 2128 | return( PSA_SUCCESS ); |
| 2129 | } |
| 2130 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2131 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2132 | psa_algorithm_t alg ) |
| 2133 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2134 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2135 | |
| 2136 | /* A context must be freshly initialized before it can be set up. */ |
| 2137 | if( operation->alg != 0 ) |
| 2138 | { |
| 2139 | return( PSA_ERROR_BAD_STATE ); |
| 2140 | } |
| 2141 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2142 | switch( alg ) |
| 2143 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2144 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2145 | case PSA_ALG_MD2: |
| 2146 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2147 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2148 | break; |
| 2149 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2150 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2151 | case PSA_ALG_MD4: |
| 2152 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2153 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2154 | break; |
| 2155 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2156 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2157 | case PSA_ALG_MD5: |
| 2158 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2159 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2160 | break; |
| 2161 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2162 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2163 | case PSA_ALG_RIPEMD160: |
| 2164 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2165 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2166 | break; |
| 2167 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2168 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2169 | case PSA_ALG_SHA_1: |
| 2170 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2171 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2172 | break; |
| 2173 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2174 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2175 | case PSA_ALG_SHA_224: |
| 2176 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2177 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2178 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2179 | #endif |
| 2180 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2181 | case PSA_ALG_SHA_256: |
| 2182 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2183 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2184 | break; |
| 2185 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2186 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2187 | case PSA_ALG_SHA_384: |
| 2188 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2189 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2190 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2191 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2192 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2193 | case PSA_ALG_SHA_512: |
| 2194 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2195 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2196 | break; |
| 2197 | #endif |
| 2198 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2199 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2200 | PSA_ERROR_NOT_SUPPORTED : |
| 2201 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2202 | } |
| 2203 | if( ret == 0 ) |
| 2204 | operation->alg = alg; |
| 2205 | else |
| 2206 | psa_hash_abort( operation ); |
| 2207 | return( mbedtls_to_psa_error( ret ) ); |
| 2208 | } |
| 2209 | |
| 2210 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2211 | const uint8_t *input, |
| 2212 | size_t input_length ) |
| 2213 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2214 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2215 | |
| 2216 | /* Don't require hash implementations to behave correctly on a |
| 2217 | * zero-length input, which may have an invalid pointer. */ |
| 2218 | if( input_length == 0 ) |
| 2219 | return( PSA_SUCCESS ); |
| 2220 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2221 | switch( operation->alg ) |
| 2222 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2224 | case PSA_ALG_MD2: |
| 2225 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2226 | input, input_length ); |
| 2227 | break; |
| 2228 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2229 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2230 | case PSA_ALG_MD4: |
| 2231 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2232 | input, input_length ); |
| 2233 | break; |
| 2234 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2235 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2236 | case PSA_ALG_MD5: |
| 2237 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2238 | input, input_length ); |
| 2239 | break; |
| 2240 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2241 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2242 | case PSA_ALG_RIPEMD160: |
| 2243 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2244 | input, input_length ); |
| 2245 | break; |
| 2246 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2247 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2248 | case PSA_ALG_SHA_1: |
| 2249 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2250 | input, input_length ); |
| 2251 | break; |
| 2252 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2253 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2254 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2255 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2256 | input, input_length ); |
| 2257 | break; |
| 2258 | #endif |
| 2259 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2260 | case PSA_ALG_SHA_256: |
| 2261 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2262 | input, input_length ); |
| 2263 | break; |
| 2264 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2265 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2266 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2267 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2268 | input, input_length ); |
| 2269 | break; |
| 2270 | #endif |
| 2271 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2272 | case PSA_ALG_SHA_512: |
| 2273 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2274 | input, input_length ); |
| 2275 | break; |
| 2276 | #endif |
| 2277 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2278 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2279 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2280 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2281 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2282 | if( ret != 0 ) |
| 2283 | psa_hash_abort( operation ); |
| 2284 | return( mbedtls_to_psa_error( ret ) ); |
| 2285 | } |
| 2286 | |
| 2287 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2288 | uint8_t *hash, |
| 2289 | size_t hash_size, |
| 2290 | size_t *hash_length ) |
| 2291 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2292 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2293 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2294 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2295 | |
| 2296 | /* Fill the output buffer with something that isn't a valid hash |
| 2297 | * (barring an attack on the hash and deliberately-crafted input), |
| 2298 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2299 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2300 | /* If hash_size is 0 then hash may be NULL and then the |
| 2301 | * call to memset would have undefined behavior. */ |
| 2302 | if( hash_size != 0 ) |
| 2303 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2304 | |
| 2305 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2306 | { |
| 2307 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2308 | goto exit; |
| 2309 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2310 | |
| 2311 | switch( operation->alg ) |
| 2312 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2313 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2314 | case PSA_ALG_MD2: |
| 2315 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2316 | break; |
| 2317 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2318 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2319 | case PSA_ALG_MD4: |
| 2320 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2321 | break; |
| 2322 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2323 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2324 | case PSA_ALG_MD5: |
| 2325 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2326 | break; |
| 2327 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2328 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2329 | case PSA_ALG_RIPEMD160: |
| 2330 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2331 | break; |
| 2332 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2333 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2334 | case PSA_ALG_SHA_1: |
| 2335 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2336 | break; |
| 2337 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2338 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2339 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2340 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2341 | break; |
| 2342 | #endif |
| 2343 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2344 | case PSA_ALG_SHA_256: |
| 2345 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2346 | break; |
| 2347 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2348 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2349 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2350 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2351 | break; |
| 2352 | #endif |
| 2353 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2354 | case PSA_ALG_SHA_512: |
| 2355 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2356 | break; |
| 2357 | #endif |
| 2358 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2359 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2360 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2361 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2362 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2363 | exit: |
| 2364 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2365 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2366 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2367 | return( psa_hash_abort( operation ) ); |
| 2368 | } |
| 2369 | else |
| 2370 | { |
| 2371 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2372 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2376 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2377 | const uint8_t *hash, |
| 2378 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2379 | { |
| 2380 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2381 | size_t actual_hash_length; |
| 2382 | psa_status_t status = psa_hash_finish( operation, |
| 2383 | actual_hash, sizeof( actual_hash ), |
| 2384 | &actual_hash_length ); |
| 2385 | if( status != PSA_SUCCESS ) |
| 2386 | return( status ); |
| 2387 | if( actual_hash_length != hash_length ) |
| 2388 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2389 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2390 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2391 | return( PSA_SUCCESS ); |
| 2392 | } |
| 2393 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2394 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2395 | const uint8_t *input, size_t input_length, |
| 2396 | uint8_t *hash, size_t hash_size, |
| 2397 | size_t *hash_length ) |
| 2398 | { |
| 2399 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2400 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2401 | |
| 2402 | *hash_length = hash_size; |
| 2403 | status = psa_hash_setup( &operation, alg ); |
| 2404 | if( status != PSA_SUCCESS ) |
| 2405 | goto exit; |
| 2406 | status = psa_hash_update( &operation, input, input_length ); |
| 2407 | if( status != PSA_SUCCESS ) |
| 2408 | goto exit; |
| 2409 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2410 | if( status != PSA_SUCCESS ) |
| 2411 | goto exit; |
| 2412 | |
| 2413 | exit: |
| 2414 | if( status == PSA_SUCCESS ) |
| 2415 | status = psa_hash_abort( &operation ); |
| 2416 | else |
| 2417 | psa_hash_abort( &operation ); |
| 2418 | return( status ); |
| 2419 | } |
| 2420 | |
| 2421 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2422 | const uint8_t *input, size_t input_length, |
| 2423 | const uint8_t *hash, size_t hash_length ) |
| 2424 | { |
| 2425 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2426 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2427 | |
| 2428 | status = psa_hash_setup( &operation, alg ); |
| 2429 | if( status != PSA_SUCCESS ) |
| 2430 | goto exit; |
| 2431 | status = psa_hash_update( &operation, input, input_length ); |
| 2432 | if( status != PSA_SUCCESS ) |
| 2433 | goto exit; |
| 2434 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2435 | if( status != PSA_SUCCESS ) |
| 2436 | goto exit; |
| 2437 | |
| 2438 | exit: |
| 2439 | if( status == PSA_SUCCESS ) |
| 2440 | status = psa_hash_abort( &operation ); |
| 2441 | else |
| 2442 | psa_hash_abort( &operation ); |
| 2443 | return( status ); |
| 2444 | } |
| 2445 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2446 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2447 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2448 | { |
| 2449 | if( target_operation->alg != 0 ) |
| 2450 | return( PSA_ERROR_BAD_STATE ); |
| 2451 | |
| 2452 | switch( source_operation->alg ) |
| 2453 | { |
| 2454 | case 0: |
| 2455 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2456 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2457 | case PSA_ALG_MD2: |
| 2458 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2459 | &source_operation->ctx.md2 ); |
| 2460 | break; |
| 2461 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2462 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2463 | case PSA_ALG_MD4: |
| 2464 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2465 | &source_operation->ctx.md4 ); |
| 2466 | break; |
| 2467 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2468 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2469 | case PSA_ALG_MD5: |
| 2470 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2471 | &source_operation->ctx.md5 ); |
| 2472 | break; |
| 2473 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2474 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2475 | case PSA_ALG_RIPEMD160: |
| 2476 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2477 | &source_operation->ctx.ripemd160 ); |
| 2478 | break; |
| 2479 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2480 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2481 | case PSA_ALG_SHA_1: |
| 2482 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2483 | &source_operation->ctx.sha1 ); |
| 2484 | break; |
| 2485 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2486 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2487 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2488 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2489 | &source_operation->ctx.sha256 ); |
| 2490 | break; |
| 2491 | #endif |
| 2492 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2493 | case PSA_ALG_SHA_256: |
| 2494 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2495 | &source_operation->ctx.sha256 ); |
| 2496 | break; |
| 2497 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2498 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2499 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2500 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2501 | &source_operation->ctx.sha512 ); |
| 2502 | break; |
| 2503 | #endif |
| 2504 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2505 | case PSA_ALG_SHA_512: |
| 2506 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2507 | &source_operation->ctx.sha512 ); |
| 2508 | break; |
| 2509 | #endif |
| 2510 | default: |
| 2511 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2512 | } |
| 2513 | |
| 2514 | target_operation->alg = source_operation->alg; |
| 2515 | return( PSA_SUCCESS ); |
| 2516 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2517 | |
| 2518 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2519 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2520 | /* MAC */ |
| 2521 | /****************************************************************/ |
| 2522 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2523 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2524 | psa_algorithm_t alg, |
| 2525 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2526 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2527 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2528 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2529 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2530 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2531 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2532 | if( PSA_ALG_IS_AEAD( alg ) ) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2533 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2534 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2535 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2536 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2537 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2538 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 2539 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2540 | mode = MBEDTLS_MODE_STREAM; |
| 2541 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2542 | case PSA_ALG_CTR: |
| 2543 | mode = MBEDTLS_MODE_CTR; |
| 2544 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2545 | case PSA_ALG_CFB: |
| 2546 | mode = MBEDTLS_MODE_CFB; |
| 2547 | break; |
| 2548 | case PSA_ALG_OFB: |
| 2549 | mode = MBEDTLS_MODE_OFB; |
| 2550 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2551 | case PSA_ALG_ECB_NO_PADDING: |
| 2552 | mode = MBEDTLS_MODE_ECB; |
| 2553 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2554 | case PSA_ALG_CBC_NO_PADDING: |
| 2555 | mode = MBEDTLS_MODE_CBC; |
| 2556 | break; |
| 2557 | case PSA_ALG_CBC_PKCS7: |
| 2558 | mode = MBEDTLS_MODE_CBC; |
| 2559 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2560 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2561 | mode = MBEDTLS_MODE_CCM; |
| 2562 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2563 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2564 | mode = MBEDTLS_MODE_GCM; |
| 2565 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2566 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2567 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2568 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2569 | default: |
| 2570 | return( NULL ); |
| 2571 | } |
| 2572 | } |
| 2573 | else if( alg == PSA_ALG_CMAC ) |
| 2574 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2575 | else |
| 2576 | return( NULL ); |
| 2577 | |
| 2578 | switch( key_type ) |
| 2579 | { |
| 2580 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2581 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2582 | break; |
| 2583 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2584 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2585 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2586 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2587 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2588 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2589 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2590 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2591 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2592 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2593 | if( key_bits == 128 ) |
| 2594 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2595 | break; |
| 2596 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2597 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2598 | break; |
| 2599 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2600 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2601 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2602 | case PSA_KEY_TYPE_CHACHA20: |
| 2603 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2604 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2605 | default: |
| 2606 | return( NULL ); |
| 2607 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2608 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2609 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2610 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2611 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2612 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2613 | } |
| 2614 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2615 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2616 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2617 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2618 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2619 | { |
| 2620 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2621 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2622 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2623 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2624 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2625 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2626 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2627 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2628 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2629 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2630 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2631 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2632 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2633 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2634 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2635 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2636 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2637 | return( 128 ); |
| 2638 | default: |
| 2639 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2640 | } |
| 2641 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2642 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2643 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2644 | /* Initialize the MAC operation structure. Once this function has been |
| 2645 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2646 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2647 | psa_algorithm_t alg ) |
| 2648 | { |
| 2649 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2650 | |
| 2651 | operation->alg = alg; |
| 2652 | operation->key_set = 0; |
| 2653 | operation->iv_set = 0; |
| 2654 | operation->iv_required = 0; |
| 2655 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2656 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2657 | |
| 2658 | #if defined(MBEDTLS_CMAC_C) |
| 2659 | if( alg == PSA_ALG_CMAC ) |
| 2660 | { |
| 2661 | operation->iv_required = 0; |
| 2662 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2663 | status = PSA_SUCCESS; |
| 2664 | } |
| 2665 | else |
| 2666 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2667 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2668 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2669 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2670 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2671 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2672 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2673 | } |
| 2674 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2675 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2676 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2677 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2678 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | if( status != PSA_SUCCESS ) |
| 2682 | memset( operation, 0, sizeof( *operation ) ); |
| 2683 | return( status ); |
| 2684 | } |
| 2685 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2686 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2687 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2688 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2689 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2690 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2691 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2692 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2693 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2694 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2695 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2696 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2697 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2698 | /* The object has (apparently) been initialized but it is not |
| 2699 | * in use. It's ok to call abort on such an object, and there's |
| 2700 | * nothing to do. */ |
| 2701 | return( PSA_SUCCESS ); |
| 2702 | } |
| 2703 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2704 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2705 | if( operation->alg == PSA_ALG_CMAC ) |
| 2706 | { |
| 2707 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2708 | } |
| 2709 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2710 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2711 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2712 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2713 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2714 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2715 | } |
| 2716 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2717 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2718 | { |
| 2719 | /* Sanity check (shouldn't happen: operation->alg should |
| 2720 | * always have been initialized to a valid value). */ |
| 2721 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2722 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2723 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2724 | operation->alg = 0; |
| 2725 | operation->key_set = 0; |
| 2726 | operation->iv_set = 0; |
| 2727 | operation->iv_required = 0; |
| 2728 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2729 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2730 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2731 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2732 | |
| 2733 | bad_state: |
| 2734 | /* If abort is called on an uninitialized object, we can't trust |
| 2735 | * anything. Wipe the object in case it contains confidential data. |
| 2736 | * This may result in a memory leak if a pointer gets overwritten, |
| 2737 | * but it's too late to do anything about this. */ |
| 2738 | memset( operation, 0, sizeof( *operation ) ); |
| 2739 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2742 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2743 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2744 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2745 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2746 | const mbedtls_cipher_info_t *cipher_info ) |
| 2747 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2748 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2749 | |
| 2750 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2751 | |
| 2752 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2753 | if( ret != 0 ) |
| 2754 | return( ret ); |
| 2755 | |
| 2756 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2757 | slot->key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2758 | key_bits ); |
| 2759 | return( ret ); |
| 2760 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2761 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2762 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2763 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2764 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2765 | const uint8_t *key, |
| 2766 | size_t key_length, |
| 2767 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2768 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2769 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2770 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2771 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2772 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2773 | psa_status_t status; |
| 2774 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2775 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2776 | * overflow below. This should never trigger if the hash algorithm |
| 2777 | * is implemented correctly. */ |
| 2778 | /* The size checks against the ipad and opad buffers cannot be written |
| 2779 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2780 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2781 | if( block_size > sizeof( ipad ) ) |
| 2782 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2783 | if( block_size > sizeof( hmac->opad ) ) |
| 2784 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2785 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2786 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2787 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2788 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2789 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2790 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2791 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2792 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2793 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2794 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2795 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2796 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2797 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2798 | * an invalid pointer which would make the behavior undefined. */ |
| 2799 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2800 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2801 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2802 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2803 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2804 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2805 | ipad[i] ^= 0x36; |
| 2806 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2807 | |
| 2808 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2809 | * and filling the rest of opad with the requisite constant. */ |
| 2810 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2811 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2812 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2813 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2814 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2815 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2816 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2817 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2818 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2819 | |
| 2820 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2821 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2822 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2823 | return( status ); |
| 2824 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2825 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2826 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2827 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2828 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2829 | psa_algorithm_t alg, |
| 2830 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2831 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2832 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2833 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2834 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2835 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2836 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2837 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2838 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2839 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2840 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2841 | /* A context must be freshly initialized before it can be set up. */ |
| 2842 | if( operation->alg != 0 ) |
| 2843 | { |
| 2844 | return( PSA_ERROR_BAD_STATE ); |
| 2845 | } |
| 2846 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2847 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2848 | if( status != PSA_SUCCESS ) |
| 2849 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2850 | if( is_sign ) |
| 2851 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2852 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2853 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2854 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2855 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2856 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2857 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2858 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2859 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2860 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2861 | { |
| 2862 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2863 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2864 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2865 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2866 | if( cipher_info == NULL ) |
| 2867 | { |
| 2868 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2869 | goto exit; |
| 2870 | } |
| 2871 | operation->mac_size = cipher_info->block_size; |
| 2872 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2873 | status = mbedtls_to_psa_error( ret ); |
| 2874 | } |
| 2875 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2876 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2877 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2878 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2879 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2880 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2881 | if( hash_alg == 0 ) |
| 2882 | { |
| 2883 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2884 | goto exit; |
| 2885 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2886 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2887 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2888 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2889 | if( operation->mac_size == 0 || |
| 2890 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2891 | { |
| 2892 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2893 | goto exit; |
| 2894 | } |
| 2895 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2896 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2897 | { |
| 2898 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2899 | goto exit; |
| 2900 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2901 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2902 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2903 | slot->key.data, |
| 2904 | slot->key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2905 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2906 | } |
| 2907 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2908 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2909 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2910 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2911 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2912 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2913 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2914 | if( truncated == 0 ) |
| 2915 | { |
Steven Cooreman | ae3f13b | 2021-02-23 12:19:42 +0100 | [diff] [blame^] | 2916 | /* The "normal" case: untruncated algorithm. Re-validate the |
| 2917 | * key policy with explicit MAC length set in the algorithm |
| 2918 | * when the algorithm policy is at-least-this-length to catch |
| 2919 | * a corner case due to the default MAC length being unknown |
| 2920 | * at key loading time. */ |
| 2921 | if( PSA_ALG_IS_MAC( slot->attr.policy.alg ) && |
| 2922 | ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg ) == full_length_alg ) && |
| 2923 | ( slot->attr.policy.alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) ) |
| 2924 | { |
| 2925 | /* validate policy length */ |
| 2926 | if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg ) > operation->mac_size ) |
| 2927 | status = PSA_ERROR_NOT_PERMITTED; |
| 2928 | } |
| 2929 | |
| 2930 | if( PSA_ALG_IS_MAC( slot->attr.policy.alg2 ) && |
| 2931 | ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg2 ) == full_length_alg ) && |
| 2932 | ( slot->attr.policy.alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) ) |
| 2933 | { |
| 2934 | /* validate policy length */ |
| 2935 | if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg2 ) > operation->mac_size ) |
| 2936 | status = PSA_ERROR_NOT_PERMITTED; |
| 2937 | } |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2938 | } |
| 2939 | else if( truncated < 4 ) |
| 2940 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2941 | /* A very short MAC is too short for security since it can be |
| 2942 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2943 | * so we make this our minimum, even though 32 bits is still |
| 2944 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2945 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2946 | } |
| 2947 | else if( truncated > operation->mac_size ) |
| 2948 | { |
| 2949 | /* It's impossible to "truncate" to a larger length. */ |
| 2950 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2951 | } |
| 2952 | else |
| 2953 | operation->mac_size = truncated; |
| 2954 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2955 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2956 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2957 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2958 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2959 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2960 | else |
| 2961 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2962 | operation->key_set = 1; |
| 2963 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2964 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2965 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2966 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2967 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2968 | } |
| 2969 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2970 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2971 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2972 | psa_algorithm_t alg ) |
| 2973 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2974 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2978 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2979 | psa_algorithm_t alg ) |
| 2980 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2981 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2982 | } |
| 2983 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2984 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2985 | const uint8_t *input, |
| 2986 | size_t input_length ) |
| 2987 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2988 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2989 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2990 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2991 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2992 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2993 | operation->has_input = 1; |
| 2994 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2995 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2996 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2997 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2998 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2999 | input, input_length ); |
| 3000 | status = mbedtls_to_psa_error( ret ); |
| 3001 | } |
| 3002 | else |
| 3003 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3004 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3005 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3006 | { |
| 3007 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3008 | input_length ); |
| 3009 | } |
| 3010 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3011 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3012 | { |
| 3013 | /* This shouldn't happen if `operation` was initialized by |
| 3014 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3015 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3016 | } |
| 3017 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3018 | if( status != PSA_SUCCESS ) |
| 3019 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3020 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3021 | } |
| 3022 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3023 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3024 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3025 | uint8_t *mac, |
| 3026 | size_t mac_size ) |
| 3027 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3028 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3029 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3030 | size_t hash_size = 0; |
| 3031 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3032 | psa_status_t status; |
| 3033 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3034 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3035 | if( status != PSA_SUCCESS ) |
| 3036 | return( status ); |
| 3037 | /* From here on, tmp needs to be wiped. */ |
| 3038 | |
| 3039 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3040 | if( status != PSA_SUCCESS ) |
| 3041 | goto exit; |
| 3042 | |
| 3043 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3044 | if( status != PSA_SUCCESS ) |
| 3045 | goto exit; |
| 3046 | |
| 3047 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3048 | if( status != PSA_SUCCESS ) |
| 3049 | goto exit; |
| 3050 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3051 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3052 | if( status != PSA_SUCCESS ) |
| 3053 | goto exit; |
| 3054 | |
| 3055 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3056 | |
| 3057 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3058 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3059 | return( status ); |
| 3060 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3061 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3062 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3063 | static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3064 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3065 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3066 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3067 | if( ! operation->key_set ) |
| 3068 | return( PSA_ERROR_BAD_STATE ); |
| 3069 | if( operation->iv_required && ! operation->iv_set ) |
| 3070 | return( PSA_ERROR_BAD_STATE ); |
| 3071 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3072 | if( mac_size < operation->mac_size ) |
| 3073 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3074 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3075 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3076 | if( operation->alg == PSA_ALG_CMAC ) |
| 3077 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3078 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3079 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3080 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3081 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3082 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3083 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3084 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3085 | else |
| 3086 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3087 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3088 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3089 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3090 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3091 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3092 | } |
| 3093 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3094 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3095 | { |
| 3096 | /* This shouldn't happen if `operation` was initialized by |
| 3097 | * a setup function. */ |
| 3098 | return( PSA_ERROR_BAD_STATE ); |
| 3099 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3100 | } |
| 3101 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3102 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3103 | uint8_t *mac, |
| 3104 | size_t mac_size, |
| 3105 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3106 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3107 | psa_status_t status; |
| 3108 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3109 | if( operation->alg == 0 ) |
| 3110 | { |
| 3111 | return( PSA_ERROR_BAD_STATE ); |
| 3112 | } |
| 3113 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3114 | /* Fill the output buffer with something that isn't a valid mac |
| 3115 | * (barring an attack on the mac and deliberately-crafted input), |
| 3116 | * in case the caller doesn't check the return status properly. */ |
| 3117 | *mac_length = mac_size; |
| 3118 | /* If mac_size is 0 then mac may be NULL and then the |
| 3119 | * call to memset would have undefined behavior. */ |
| 3120 | if( mac_size != 0 ) |
| 3121 | memset( mac, '!', mac_size ); |
| 3122 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3123 | if( ! operation->is_sign ) |
| 3124 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3125 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3126 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3127 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3128 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3129 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3130 | if( status == PSA_SUCCESS ) |
| 3131 | { |
| 3132 | status = psa_mac_abort( operation ); |
| 3133 | if( status == PSA_SUCCESS ) |
| 3134 | *mac_length = operation->mac_size; |
| 3135 | else |
| 3136 | memset( mac, '!', mac_size ); |
| 3137 | } |
| 3138 | else |
| 3139 | psa_mac_abort( operation ); |
| 3140 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3143 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3144 | const uint8_t *mac, |
| 3145 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3146 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3147 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3148 | psa_status_t status; |
| 3149 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3150 | if( operation->alg == 0 ) |
| 3151 | { |
| 3152 | return( PSA_ERROR_BAD_STATE ); |
| 3153 | } |
| 3154 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3155 | if( operation->is_sign ) |
| 3156 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3157 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3158 | } |
| 3159 | if( operation->mac_size != mac_length ) |
| 3160 | { |
| 3161 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3162 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3163 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3164 | |
| 3165 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3166 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3167 | if( status != PSA_SUCCESS ) |
| 3168 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3169 | |
| 3170 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3171 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3172 | |
| 3173 | cleanup: |
| 3174 | if( status == PSA_SUCCESS ) |
| 3175 | status = psa_mac_abort( operation ); |
| 3176 | else |
| 3177 | psa_mac_abort( operation ); |
| 3178 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3179 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3180 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3181 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3185 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3186 | /****************************************************************/ |
| 3187 | /* Asymmetric cryptography */ |
| 3188 | /****************************************************************/ |
| 3189 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3190 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3191 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3192 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3193 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3194 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3195 | size_t hash_length, |
| 3196 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3197 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3198 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3199 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3200 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3201 | |
| 3202 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3203 | * parameters. Validate that it fits so that we don't risk an |
| 3204 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3205 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3206 | if( hash_length > UINT_MAX ) |
| 3207 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3208 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3209 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3210 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3211 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3212 | * must be correct. */ |
| 3213 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3214 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3215 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3216 | if( md_info == NULL ) |
| 3217 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3218 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3219 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3220 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3221 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3222 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3224 | /* PSS requires a hash internally. */ |
| 3225 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3226 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3227 | if( md_info == NULL ) |
| 3228 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3229 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3230 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3232 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3233 | } |
| 3234 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3235 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3236 | psa_algorithm_t alg, |
| 3237 | const uint8_t *hash, |
| 3238 | size_t hash_length, |
| 3239 | uint8_t *signature, |
| 3240 | size_t signature_size, |
| 3241 | size_t *signature_length ) |
| 3242 | { |
| 3243 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3244 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3245 | mbedtls_md_type_t md_alg; |
| 3246 | |
| 3247 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3248 | if( status != PSA_SUCCESS ) |
| 3249 | return( status ); |
| 3250 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3251 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3252 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3253 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3254 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3255 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3256 | { |
| 3257 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3258 | MBEDTLS_MD_NONE ); |
| 3259 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3260 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3261 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3262 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3263 | md_alg, |
| 3264 | (unsigned int) hash_length, |
| 3265 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3266 | signature ); |
| 3267 | } |
| 3268 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3269 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3270 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3271 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3272 | { |
| 3273 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3274 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3275 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3276 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3277 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3278 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3279 | (unsigned int) hash_length, |
| 3280 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3281 | signature ); |
| 3282 | } |
| 3283 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3284 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3285 | { |
| 3286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3287 | } |
| 3288 | |
| 3289 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3290 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3291 | return( mbedtls_to_psa_error( ret ) ); |
| 3292 | } |
| 3293 | |
| 3294 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3295 | psa_algorithm_t alg, |
| 3296 | const uint8_t *hash, |
| 3297 | size_t hash_length, |
| 3298 | const uint8_t *signature, |
| 3299 | size_t signature_length ) |
| 3300 | { |
| 3301 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3302 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3303 | mbedtls_md_type_t md_alg; |
| 3304 | |
| 3305 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3306 | if( status != PSA_SUCCESS ) |
| 3307 | return( status ); |
| 3308 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3309 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3310 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3311 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3312 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3313 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3314 | { |
| 3315 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3316 | MBEDTLS_MD_NONE ); |
| 3317 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3318 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3319 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3320 | MBEDTLS_RSA_PUBLIC, |
| 3321 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3322 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3323 | hash, |
| 3324 | signature ); |
| 3325 | } |
| 3326 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3327 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3328 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3329 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3330 | { |
| 3331 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3332 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3333 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3334 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3335 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3336 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3337 | (unsigned int) hash_length, |
| 3338 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3339 | signature ); |
| 3340 | } |
| 3341 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3342 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3343 | { |
| 3344 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3345 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3346 | |
| 3347 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3348 | * the rest of the signature is invalid". This has little use in |
| 3349 | * practice and PSA doesn't report this distinction. */ |
| 3350 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3351 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3352 | return( mbedtls_to_psa_error( ret ) ); |
| 3353 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3354 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3355 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3356 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3357 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3358 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3359 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3360 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3361 | * (even though these functions don't modify it). */ |
| 3362 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3363 | psa_algorithm_t alg, |
| 3364 | const uint8_t *hash, |
| 3365 | size_t hash_length, |
| 3366 | uint8_t *signature, |
| 3367 | size_t signature_size, |
| 3368 | size_t *signature_length ) |
| 3369 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3370 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3371 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3372 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3373 | mbedtls_mpi_init( &r ); |
| 3374 | mbedtls_mpi_init( &s ); |
| 3375 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3376 | if( signature_size < 2 * curve_bytes ) |
| 3377 | { |
| 3378 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3379 | goto cleanup; |
| 3380 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3381 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3382 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3383 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3384 | { |
| 3385 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3386 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3387 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3388 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3389 | &ecp->d, hash, |
| 3390 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3391 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3392 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3393 | } |
| 3394 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3395 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3396 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3397 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3398 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3399 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3400 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3401 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3402 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3403 | |
| 3404 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3405 | signature, |
| 3406 | curve_bytes ) ); |
| 3407 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3408 | signature + curve_bytes, |
| 3409 | curve_bytes ) ); |
| 3410 | |
| 3411 | cleanup: |
| 3412 | mbedtls_mpi_free( &r ); |
| 3413 | mbedtls_mpi_free( &s ); |
| 3414 | if( ret == 0 ) |
| 3415 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3416 | return( mbedtls_to_psa_error( ret ) ); |
| 3417 | } |
| 3418 | |
| 3419 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3420 | const uint8_t *hash, |
| 3421 | size_t hash_length, |
| 3422 | const uint8_t *signature, |
| 3423 | size_t signature_length ) |
| 3424 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3425 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3426 | mbedtls_mpi r, s; |
| 3427 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3428 | mbedtls_mpi_init( &r ); |
| 3429 | mbedtls_mpi_init( &s ); |
| 3430 | |
| 3431 | if( signature_length != 2 * curve_bytes ) |
| 3432 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3433 | |
| 3434 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3435 | signature, |
| 3436 | curve_bytes ) ); |
| 3437 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3438 | signature + curve_bytes, |
| 3439 | curve_bytes ) ); |
| 3440 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3441 | /* Check whether the public part is loaded. If not, load it. */ |
| 3442 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3443 | { |
| 3444 | MBEDTLS_MPI_CHK( |
| 3445 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3446 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3449 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3450 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3451 | |
| 3452 | cleanup: |
| 3453 | mbedtls_mpi_free( &r ); |
| 3454 | mbedtls_mpi_free( &s ); |
| 3455 | return( mbedtls_to_psa_error( ret ) ); |
| 3456 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3457 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3458 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3459 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3460 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3461 | psa_algorithm_t alg, |
| 3462 | const uint8_t *hash, |
| 3463 | size_t hash_length, |
| 3464 | uint8_t *signature, |
| 3465 | size_t signature_size, |
| 3466 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3467 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3468 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3469 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3470 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3471 | |
| 3472 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3473 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3474 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3475 | * buffer can in principle be empty since it doesn't actually have |
| 3476 | * to be a hash.) */ |
| 3477 | if( signature_size == 0 ) |
| 3478 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3479 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3480 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3481 | PSA_KEY_USAGE_SIGN_HASH, |
| 3482 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3483 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3484 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3485 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3486 | { |
| 3487 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3488 | goto exit; |
| 3489 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3490 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3491 | /* Try any of the available accelerators first */ |
| 3492 | status = psa_driver_wrapper_sign_hash( slot, |
| 3493 | alg, |
| 3494 | hash, |
| 3495 | hash_length, |
| 3496 | signature, |
| 3497 | signature_size, |
| 3498 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3499 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3500 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3501 | goto exit; |
| 3502 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3503 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3504 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3505 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3506 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3507 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3508 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3509 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3510 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3511 | slot->key.data, |
| 3512 | slot->key.bytes, |
| 3513 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3514 | if( status != PSA_SUCCESS ) |
| 3515 | goto exit; |
| 3516 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3517 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3518 | alg, |
| 3519 | hash, hash_length, |
| 3520 | signature, signature_size, |
| 3521 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3522 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3523 | mbedtls_rsa_free( rsa ); |
| 3524 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3525 | } |
| 3526 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3527 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3528 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3529 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3530 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3531 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3532 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3533 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3534 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3535 | PSA_ALG_IS_ECDSA( alg ) |
| 3536 | #else |
| 3537 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3538 | #endif |
| 3539 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3540 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3541 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3542 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 3543 | slot->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3544 | slot->key.data, |
| 3545 | slot->key.bytes, |
| 3546 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3547 | if( status != PSA_SUCCESS ) |
| 3548 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3549 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3550 | alg, |
| 3551 | hash, hash_length, |
| 3552 | signature, signature_size, |
| 3553 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3554 | mbedtls_ecp_keypair_free( ecp ); |
| 3555 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3556 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3557 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3558 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3559 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3560 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3561 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3562 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3563 | } |
| 3564 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3565 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3566 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3567 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3568 | |
| 3569 | exit: |
| 3570 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3571 | * the trailing part on success) with something that isn't a valid mac |
| 3572 | * (barring an attack on the mac and deliberately-crafted input), |
| 3573 | * in case the caller doesn't check the return status properly. */ |
| 3574 | if( status == PSA_SUCCESS ) |
| 3575 | memset( signature + *signature_length, '!', |
| 3576 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3577 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3578 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3579 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3580 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3581 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3582 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3583 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3584 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3585 | } |
| 3586 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3587 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3588 | psa_algorithm_t alg, |
| 3589 | const uint8_t *hash, |
| 3590 | size_t hash_length, |
| 3591 | const uint8_t *signature, |
| 3592 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3593 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3594 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3595 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3596 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3597 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3598 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3599 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3600 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3601 | if( status != PSA_SUCCESS ) |
| 3602 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3603 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3604 | /* Try any of the available accelerators first */ |
| 3605 | status = psa_driver_wrapper_verify_hash( slot, |
| 3606 | alg, |
| 3607 | hash, |
| 3608 | hash_length, |
| 3609 | signature, |
| 3610 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3611 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3612 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3613 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3614 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3615 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3616 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3617 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3618 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3619 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3620 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3621 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3622 | slot->key.data, |
| 3623 | slot->key.bytes, |
| 3624 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3625 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3626 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3627 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3628 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3629 | alg, |
| 3630 | hash, hash_length, |
| 3631 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3632 | mbedtls_rsa_free( rsa ); |
| 3633 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3634 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3635 | } |
| 3636 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3637 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3638 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3639 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3640 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3641 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3642 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3643 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3644 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3645 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3646 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 3647 | slot->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3648 | slot->key.data, |
| 3649 | slot->key.bytes, |
| 3650 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3651 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3652 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3653 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3654 | hash, hash_length, |
| 3655 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3656 | mbedtls_ecp_keypair_free( ecp ); |
| 3657 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3658 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3659 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3660 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3661 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3662 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3663 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3664 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3665 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3666 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3667 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3668 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3669 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3670 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3671 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3672 | |
| 3673 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3674 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3675 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3676 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3677 | } |
| 3678 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3679 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3680 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3681 | mbedtls_rsa_context *rsa ) |
| 3682 | { |
| 3683 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3684 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3685 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3686 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3687 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3688 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3689 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3690 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3691 | psa_algorithm_t alg, |
| 3692 | const uint8_t *input, |
| 3693 | size_t input_length, |
| 3694 | const uint8_t *salt, |
| 3695 | size_t salt_length, |
| 3696 | uint8_t *output, |
| 3697 | size_t output_size, |
| 3698 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3699 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3700 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3701 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3702 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3703 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3704 | (void) input; |
| 3705 | (void) input_length; |
| 3706 | (void) salt; |
| 3707 | (void) output; |
| 3708 | (void) output_size; |
| 3709 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3710 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3711 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3712 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3713 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3714 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3715 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3716 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3717 | if( status != PSA_SUCCESS ) |
| 3718 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3719 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3720 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3721 | { |
| 3722 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3723 | goto exit; |
| 3724 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3725 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3726 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3727 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3728 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3729 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3730 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3731 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3732 | slot->key.data, |
| 3733 | slot->key.bytes, |
| 3734 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3735 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3736 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3737 | |
| 3738 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3739 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3740 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3741 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3742 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3743 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3744 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3745 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3746 | status = mbedtls_to_psa_error( |
| 3747 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3748 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3749 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3750 | MBEDTLS_RSA_PUBLIC, |
| 3751 | input_length, |
| 3752 | input, |
| 3753 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3754 | } |
| 3755 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3756 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3757 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3758 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3759 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3760 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3761 | status = mbedtls_to_psa_error( |
| 3762 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3763 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3764 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3765 | MBEDTLS_RSA_PUBLIC, |
| 3766 | salt, salt_length, |
| 3767 | input_length, |
| 3768 | input, |
| 3769 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3770 | } |
| 3771 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3772 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3773 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3774 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3775 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3776 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3777 | rsa_exit: |
| 3778 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3779 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3780 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3781 | mbedtls_rsa_free( rsa ); |
| 3782 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3783 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3784 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3785 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3786 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3787 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3788 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3789 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3790 | |
| 3791 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3792 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3793 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3794 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3795 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3796 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3797 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3798 | psa_algorithm_t alg, |
| 3799 | const uint8_t *input, |
| 3800 | size_t input_length, |
| 3801 | const uint8_t *salt, |
| 3802 | size_t salt_length, |
| 3803 | uint8_t *output, |
| 3804 | size_t output_size, |
| 3805 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3806 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3807 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3808 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3809 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3810 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3811 | (void) input; |
| 3812 | (void) input_length; |
| 3813 | (void) salt; |
| 3814 | (void) output; |
| 3815 | (void) output_size; |
| 3816 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3817 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3818 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3819 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3820 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3821 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3822 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3823 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3824 | if( status != PSA_SUCCESS ) |
| 3825 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3826 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3827 | { |
| 3828 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3829 | goto exit; |
| 3830 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3831 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3832 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3833 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3834 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3835 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3836 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3837 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3838 | slot->key.data, |
| 3839 | slot->key.bytes, |
| 3840 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3841 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3842 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3843 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3844 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3845 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3846 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3847 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3848 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3849 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3850 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3851 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3852 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3853 | status = mbedtls_to_psa_error( |
| 3854 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3855 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3856 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3857 | MBEDTLS_RSA_PRIVATE, |
| 3858 | output_length, |
| 3859 | input, |
| 3860 | output, |
| 3861 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3862 | } |
| 3863 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3864 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3865 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3866 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3867 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3868 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3869 | status = mbedtls_to_psa_error( |
| 3870 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3871 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3872 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3873 | MBEDTLS_RSA_PRIVATE, |
| 3874 | salt, salt_length, |
| 3875 | output_length, |
| 3876 | input, |
| 3877 | output, |
| 3878 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3879 | } |
| 3880 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3881 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3882 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3883 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3884 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3885 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3886 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3887 | mbedtls_rsa_free( rsa ); |
| 3888 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3889 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3890 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3891 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3892 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3893 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3894 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3895 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3896 | |
| 3897 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3898 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3899 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3900 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3901 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3902 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3903 | |
| 3904 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3905 | /****************************************************************/ |
| 3906 | /* Symmetric cryptography */ |
| 3907 | /****************************************************************/ |
| 3908 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3909 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3910 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3911 | psa_algorithm_t alg, |
| 3912 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3913 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3914 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3915 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3916 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3917 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3918 | size_t key_bits; |
| 3919 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3920 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3921 | PSA_KEY_USAGE_ENCRYPT : |
| 3922 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3923 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3924 | /* A context must be freshly initialized before it can be set up. */ |
| 3925 | if( operation->alg != 0 ) |
| 3926 | return( PSA_ERROR_BAD_STATE ); |
| 3927 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3928 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 3929 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3930 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3931 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3932 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3933 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3934 | if( status != PSA_SUCCESS ) |
| 3935 | goto exit; |
| 3936 | |
| 3937 | /* Initialize the operation struct members, except for alg. The alg member |
| 3938 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 3939 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3940 | operation->key_set = 0; |
| 3941 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3942 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3943 | operation->iv_size = 0; |
| 3944 | operation->block_size = 0; |
| 3945 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3946 | operation->iv_required = 0; |
| 3947 | else |
| 3948 | operation->iv_required = 1; |
| 3949 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3950 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 3951 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 3952 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 3953 | slot, |
| 3954 | alg ); |
| 3955 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 3956 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 3957 | slot, |
| 3958 | alg ); |
| 3959 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3960 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 3961 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3962 | /* Once the driver context is initialised, it needs to be freed using |
| 3963 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 3964 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 3965 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3966 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3967 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3968 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 3969 | goto exit; |
| 3970 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3971 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3972 | * available for the given algorithm & key. */ |
| 3973 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3974 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3975 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3976 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 3977 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 3978 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3979 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3980 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3981 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3982 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3983 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3984 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3985 | { |
| 3986 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3987 | goto exit; |
| 3988 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3989 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3990 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3991 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3992 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3993 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 3994 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3995 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3996 | { |
| 3997 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3998 | uint8_t keys[24]; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3999 | memcpy( keys, slot->key.data, 16 ); |
| 4000 | memcpy( keys + 16, slot->key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4001 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4002 | keys, |
| 4003 | 192, cipher_operation ); |
| 4004 | } |
| 4005 | else |
| 4006 | #endif |
| 4007 | { |
| 4008 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4009 | slot->key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4010 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4011 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4012 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4013 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4014 | |
David Brown | 63ca260 | 2021-01-26 11:51:12 -0700 | [diff] [blame] | 4015 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 4016 | defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4017 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4018 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4019 | case PSA_ALG_CBC_NO_PADDING: |
| 4020 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4021 | MBEDTLS_PADDING_NONE ); |
| 4022 | break; |
| 4023 | case PSA_ALG_CBC_PKCS7: |
| 4024 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4025 | MBEDTLS_PADDING_PKCS7 ); |
| 4026 | break; |
| 4027 | default: |
| 4028 | /* The algorithm doesn't involve padding. */ |
| 4029 | ret = 0; |
| 4030 | break; |
| 4031 | } |
| 4032 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4033 | goto exit; |
David Brown | 288a96e | 2021-02-09 16:27:55 -0700 | [diff] [blame] | 4034 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */ |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4035 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4036 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4037 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4038 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4039 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4040 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4041 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4042 | } |
David Brown | 1bfe4d7 | 2021-02-16 12:54:35 -0700 | [diff] [blame] | 4043 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4044 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4045 | if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 ) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4046 | operation->iv_size = 12; |
| 4047 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4048 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4049 | status = PSA_SUCCESS; |
| 4050 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4051 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4052 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4053 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4054 | if( status == PSA_SUCCESS ) |
| 4055 | { |
| 4056 | /* Update operation flags for both driver and software implementations */ |
| 4057 | operation->key_set = 1; |
| 4058 | } |
| 4059 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4060 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4061 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4062 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4063 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4064 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4065 | } |
| 4066 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4067 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4068 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4069 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4070 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4071 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4072 | } |
| 4073 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4074 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4075 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4076 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4077 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4078 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4079 | } |
| 4080 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4081 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4082 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4083 | size_t iv_size, |
| 4084 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4085 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4086 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4087 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4088 | if( operation->iv_set || ! operation->iv_required ) |
| 4089 | { |
| 4090 | return( PSA_ERROR_BAD_STATE ); |
| 4091 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4092 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4093 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4094 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4095 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4096 | iv, |
| 4097 | iv_size, |
| 4098 | iv_length ); |
| 4099 | goto exit; |
| 4100 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4101 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4102 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4103 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4104 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4105 | goto exit; |
| 4106 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4107 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4108 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4109 | if( ret != 0 ) |
| 4110 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4111 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4112 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4113 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4114 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4115 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4116 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4117 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4118 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4119 | if( status == PSA_SUCCESS ) |
| 4120 | operation->iv_set = 1; |
| 4121 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4122 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4123 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4124 | } |
| 4125 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4126 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4127 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4128 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4129 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4130 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4131 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4132 | if( operation->iv_set || ! operation->iv_required ) |
| 4133 | { |
| 4134 | return( PSA_ERROR_BAD_STATE ); |
| 4135 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4136 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4137 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4138 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4139 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4140 | iv, |
| 4141 | iv_length ); |
| 4142 | goto exit; |
| 4143 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4144 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4145 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4146 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4147 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4148 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4149 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4150 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4151 | status = mbedtls_to_psa_error( ret ); |
| 4152 | exit: |
| 4153 | if( status == PSA_SUCCESS ) |
| 4154 | operation->iv_set = 1; |
| 4155 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4156 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4157 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4158 | } |
| 4159 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4160 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4161 | * manual processing, since the PSA API is defined as being able to process |
| 4162 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4163 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4164 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4165 | mbedtls_cipher_context_t *ctx, |
| 4166 | const uint8_t *input, |
| 4167 | size_t input_length, |
| 4168 | uint8_t *output, |
| 4169 | size_t output_size, |
| 4170 | size_t *output_length ) |
| 4171 | { |
| 4172 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4173 | size_t block_size = ctx->cipher_info->block_size; |
| 4174 | size_t internal_output_length = 0; |
| 4175 | *output_length = 0; |
| 4176 | |
| 4177 | if( input_length == 0 ) |
| 4178 | { |
| 4179 | status = PSA_SUCCESS; |
| 4180 | goto exit; |
| 4181 | } |
| 4182 | |
| 4183 | if( ctx->unprocessed_len > 0 ) |
| 4184 | { |
| 4185 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4186 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4187 | |
| 4188 | if( input_length < bytes_to_copy ) |
| 4189 | bytes_to_copy = input_length; |
| 4190 | |
| 4191 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4192 | input, bytes_to_copy ); |
| 4193 | input_length -= bytes_to_copy; |
| 4194 | input += bytes_to_copy; |
| 4195 | ctx->unprocessed_len += bytes_to_copy; |
| 4196 | |
| 4197 | if( ctx->unprocessed_len == block_size ) |
| 4198 | { |
| 4199 | status = mbedtls_to_psa_error( |
| 4200 | mbedtls_cipher_update( ctx, |
| 4201 | ctx->unprocessed_data, |
| 4202 | block_size, |
| 4203 | output, &internal_output_length ) ); |
| 4204 | |
| 4205 | if( status != PSA_SUCCESS ) |
| 4206 | goto exit; |
| 4207 | |
| 4208 | output += internal_output_length; |
| 4209 | output_size -= internal_output_length; |
| 4210 | *output_length += internal_output_length; |
| 4211 | ctx->unprocessed_len = 0; |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | while( input_length >= block_size ) |
| 4216 | { |
| 4217 | /* Run all full blocks we have, one by one */ |
| 4218 | status = mbedtls_to_psa_error( |
| 4219 | mbedtls_cipher_update( ctx, input, |
| 4220 | block_size, |
| 4221 | output, &internal_output_length ) ); |
| 4222 | |
| 4223 | if( status != PSA_SUCCESS ) |
| 4224 | goto exit; |
| 4225 | |
| 4226 | input_length -= block_size; |
| 4227 | input += block_size; |
| 4228 | |
| 4229 | output += internal_output_length; |
| 4230 | output_size -= internal_output_length; |
| 4231 | *output_length += internal_output_length; |
| 4232 | } |
| 4233 | |
| 4234 | if( input_length > 0 ) |
| 4235 | { |
| 4236 | /* Save unprocessed bytes for later processing */ |
| 4237 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4238 | input, input_length ); |
| 4239 | ctx->unprocessed_len += input_length; |
| 4240 | } |
| 4241 | |
| 4242 | status = PSA_SUCCESS; |
| 4243 | |
| 4244 | exit: |
| 4245 | return( status ); |
| 4246 | } |
| 4247 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4248 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4249 | const uint8_t *input, |
| 4250 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4251 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4252 | size_t output_size, |
| 4253 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4254 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4255 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4256 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4257 | if( operation->alg == 0 ) |
| 4258 | { |
| 4259 | return( PSA_ERROR_BAD_STATE ); |
| 4260 | } |
| 4261 | if( operation->iv_required && ! operation->iv_set ) |
| 4262 | { |
| 4263 | return( PSA_ERROR_BAD_STATE ); |
| 4264 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4265 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4266 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4267 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4268 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4269 | input, |
| 4270 | input_length, |
| 4271 | output, |
| 4272 | output_size, |
| 4273 | output_length ); |
| 4274 | goto exit; |
| 4275 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4276 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4277 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4278 | { |
| 4279 | /* Take the unprocessed partial block left over from previous |
| 4280 | * update calls, if any, plus the input to this call. Remove |
| 4281 | * the last partial block, if any. You get the data that will be |
| 4282 | * output in this call. */ |
| 4283 | expected_output_size = |
| 4284 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4285 | / operation->block_size * operation->block_size; |
| 4286 | } |
| 4287 | else |
| 4288 | { |
| 4289 | expected_output_size = input_length; |
| 4290 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4291 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4292 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4293 | { |
| 4294 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4295 | goto exit; |
| 4296 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4297 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4298 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4299 | { |
| 4300 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4301 | * process a single block at a time in ECB mode. Abstract away that |
| 4302 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4303 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4304 | input, |
| 4305 | input_length, |
| 4306 | output, |
| 4307 | output_size, |
| 4308 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4309 | } |
| 4310 | else |
| 4311 | { |
| 4312 | status = mbedtls_to_psa_error( |
| 4313 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4314 | input_length, output, output_length ) ); |
| 4315 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4316 | exit: |
| 4317 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4318 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4319 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4320 | } |
| 4321 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4322 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4323 | uint8_t *output, |
| 4324 | size_t output_size, |
| 4325 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4326 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4327 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4328 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4329 | if( operation->alg == 0 ) |
| 4330 | { |
| 4331 | return( PSA_ERROR_BAD_STATE ); |
| 4332 | } |
| 4333 | if( operation->iv_required && ! operation->iv_set ) |
| 4334 | { |
| 4335 | return( PSA_ERROR_BAD_STATE ); |
| 4336 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4337 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4338 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4339 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4340 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4341 | output, |
| 4342 | output_size, |
| 4343 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4344 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4345 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4346 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4347 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4348 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4349 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4350 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4351 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4352 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4353 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4354 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4355 | } |
| 4356 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4357 | status = mbedtls_to_psa_error( |
| 4358 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4359 | temp_output_buffer, |
| 4360 | output_length ) ); |
| 4361 | if( status != PSA_SUCCESS ) |
| 4362 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4363 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4364 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4365 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4366 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4367 | memcpy( output, temp_output_buffer, *output_length ); |
| 4368 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4369 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4370 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4371 | exit: |
| 4372 | if( operation->mbedtls_in_use == 1 ) |
| 4373 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4374 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4375 | if( status == PSA_SUCCESS ) |
| 4376 | return( psa_cipher_abort( operation ) ); |
| 4377 | else |
| 4378 | { |
| 4379 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4380 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4381 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4382 | return( status ); |
| 4383 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4384 | } |
| 4385 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4386 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4387 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4388 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4389 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4390 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4391 | * in use. It's ok to call abort on such an object, and there's |
| 4392 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4393 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4394 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4395 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4396 | /* Sanity check (shouldn't happen: operation->alg should |
| 4397 | * always have been initialized to a valid value). */ |
| 4398 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4399 | return( PSA_ERROR_BAD_STATE ); |
| 4400 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4401 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4402 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4403 | else |
| 4404 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4405 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4406 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4407 | operation->key_set = 0; |
| 4408 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4409 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4410 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4411 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4412 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4413 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4414 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4415 | } |
| 4416 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4417 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4418 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4419 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4420 | /****************************************************************/ |
| 4421 | /* AEAD */ |
| 4422 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4423 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4424 | typedef struct |
| 4425 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4426 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4427 | const mbedtls_cipher_info_t *cipher_info; |
| 4428 | union |
| 4429 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4430 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4431 | #if defined(MBEDTLS_CCM_C) |
| 4432 | mbedtls_ccm_context ccm; |
| 4433 | #endif /* MBEDTLS_CCM_C */ |
| 4434 | #if defined(MBEDTLS_GCM_C) |
| 4435 | mbedtls_gcm_context gcm; |
| 4436 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4437 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4438 | mbedtls_chachapoly_context chachapoly; |
| 4439 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4440 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4441 | psa_algorithm_t core_alg; |
| 4442 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4443 | uint8_t tag_length; |
| 4444 | } aead_operation_t; |
| 4445 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4446 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4447 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4448 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4449 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4450 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4451 | { |
| 4452 | #if defined(MBEDTLS_CCM_C) |
| 4453 | case PSA_ALG_CCM: |
| 4454 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4455 | break; |
| 4456 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4457 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4458 | case PSA_ALG_GCM: |
| 4459 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4460 | break; |
| 4461 | #endif /* MBEDTLS_GCM_C */ |
| 4462 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4463 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4464 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4468 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4469 | psa_key_usage_t usage, |
| 4470 | psa_algorithm_t alg ) |
| 4471 | { |
| 4472 | psa_status_t status; |
| 4473 | size_t key_bits; |
| 4474 | mbedtls_cipher_id_t cipher_id; |
| 4475 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4476 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4477 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4478 | if( status != PSA_SUCCESS ) |
| 4479 | return( status ); |
| 4480 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4481 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4482 | |
| 4483 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4484 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4485 | &cipher_id ); |
| 4486 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4487 | { |
| 4488 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4489 | goto cleanup; |
| 4490 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4491 | |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4492 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4493 | { |
| 4494 | #if defined(MBEDTLS_CCM_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4495 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4496 | operation->core_alg = PSA_ALG_CCM; |
| 4497 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4498 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4499 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4500 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4501 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4502 | { |
| 4503 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4504 | goto cleanup; |
| 4505 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4506 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4507 | status = mbedtls_to_psa_error( |
| 4508 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4509 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4510 | (unsigned int) key_bits ) ); |
| 4511 | if( status != 0 ) |
| 4512 | goto cleanup; |
| 4513 | break; |
| 4514 | #endif /* MBEDTLS_CCM_C */ |
| 4515 | |
| 4516 | #if defined(MBEDTLS_GCM_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4517 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4518 | operation->core_alg = PSA_ALG_GCM; |
| 4519 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4520 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4521 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4522 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4523 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4524 | { |
| 4525 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4526 | goto cleanup; |
| 4527 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4528 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4529 | status = mbedtls_to_psa_error( |
| 4530 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4531 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4532 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4533 | if( status != 0 ) |
| 4534 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4535 | break; |
| 4536 | #endif /* MBEDTLS_GCM_C */ |
| 4537 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4538 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4539 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4540 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4541 | operation->full_tag_length = 16; |
| 4542 | /* We only support the default tag length. */ |
| 4543 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4544 | { |
| 4545 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4546 | goto cleanup; |
| 4547 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4548 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4549 | status = mbedtls_to_psa_error( |
| 4550 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4551 | operation->slot->key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4552 | if( status != 0 ) |
| 4553 | goto cleanup; |
| 4554 | break; |
| 4555 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4556 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4557 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4558 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4559 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4560 | } |
| 4561 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4562 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4563 | { |
| 4564 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4565 | goto cleanup; |
| 4566 | } |
| 4567 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4568 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4569 | return( PSA_SUCCESS ); |
| 4570 | |
| 4571 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4572 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4573 | return( status ); |
| 4574 | } |
| 4575 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4576 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4577 | psa_algorithm_t alg, |
| 4578 | const uint8_t *nonce, |
| 4579 | size_t nonce_length, |
| 4580 | const uint8_t *additional_data, |
| 4581 | size_t additional_data_length, |
| 4582 | const uint8_t *plaintext, |
| 4583 | size_t plaintext_length, |
| 4584 | uint8_t *ciphertext, |
| 4585 | size_t ciphertext_size, |
| 4586 | size_t *ciphertext_length ) |
| 4587 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4588 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4589 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4590 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4591 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4592 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4593 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4594 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4595 | if( status != PSA_SUCCESS ) |
| 4596 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4597 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4598 | /* For all currently supported modes, the tag is at the end of the |
| 4599 | * ciphertext. */ |
| 4600 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4601 | { |
| 4602 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4603 | goto exit; |
| 4604 | } |
| 4605 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4606 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4607 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4608 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4609 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4610 | status = mbedtls_to_psa_error( |
| 4611 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4612 | MBEDTLS_GCM_ENCRYPT, |
| 4613 | plaintext_length, |
| 4614 | nonce, nonce_length, |
| 4615 | additional_data, additional_data_length, |
| 4616 | plaintext, ciphertext, |
| 4617 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4618 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4619 | else |
| 4620 | #endif /* MBEDTLS_GCM_C */ |
| 4621 | #if defined(MBEDTLS_CCM_C) |
| 4622 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4623 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4624 | status = mbedtls_to_psa_error( |
| 4625 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4626 | plaintext_length, |
| 4627 | nonce, nonce_length, |
| 4628 | additional_data, |
| 4629 | additional_data_length, |
| 4630 | plaintext, ciphertext, |
| 4631 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4632 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4633 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4634 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4635 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4636 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4637 | { |
| 4638 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4639 | { |
| 4640 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4641 | goto exit; |
| 4642 | } |
| 4643 | status = mbedtls_to_psa_error( |
| 4644 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4645 | plaintext_length, |
| 4646 | nonce, |
| 4647 | additional_data, |
| 4648 | additional_data_length, |
| 4649 | plaintext, |
| 4650 | ciphertext, |
| 4651 | tag ) ); |
| 4652 | } |
| 4653 | else |
| 4654 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4655 | { |
Steven Cooreman | 7196fef | 2021-02-10 17:13:28 +0100 | [diff] [blame] | 4656 | (void) tag; |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4657 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4658 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4659 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4660 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4661 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4662 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4663 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4664 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4665 | if( status == PSA_SUCCESS ) |
| 4666 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4667 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4668 | } |
| 4669 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4670 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4671 | * followed by the tag. Return the length of the part preceding the tag in |
| 4672 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4673 | * the encrypted data has the same size as the plaintext, such as |
| 4674 | * CCM and GCM. */ |
| 4675 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4676 | const uint8_t *ciphertext, |
| 4677 | size_t ciphertext_length, |
| 4678 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4679 | const uint8_t **p_tag ) |
| 4680 | { |
| 4681 | size_t payload_length; |
| 4682 | if( tag_length > ciphertext_length ) |
| 4683 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4684 | payload_length = ciphertext_length - tag_length; |
| 4685 | if( payload_length > plaintext_size ) |
| 4686 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4687 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4688 | return( PSA_SUCCESS ); |
| 4689 | } |
| 4690 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4691 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4692 | psa_algorithm_t alg, |
| 4693 | const uint8_t *nonce, |
| 4694 | size_t nonce_length, |
| 4695 | const uint8_t *additional_data, |
| 4696 | size_t additional_data_length, |
| 4697 | const uint8_t *ciphertext, |
| 4698 | size_t ciphertext_length, |
| 4699 | uint8_t *plaintext, |
| 4700 | size_t plaintext_size, |
| 4701 | size_t *plaintext_length ) |
| 4702 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4703 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4704 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4705 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4707 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4708 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4709 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4710 | if( status != PSA_SUCCESS ) |
| 4711 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4712 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4713 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4714 | ciphertext, ciphertext_length, |
| 4715 | plaintext_size, &tag ); |
| 4716 | if( status != PSA_SUCCESS ) |
| 4717 | goto exit; |
| 4718 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4719 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4720 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4721 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4722 | status = mbedtls_to_psa_error( |
| 4723 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4724 | ciphertext_length - operation.tag_length, |
| 4725 | nonce, nonce_length, |
| 4726 | additional_data, |
| 4727 | additional_data_length, |
| 4728 | tag, operation.tag_length, |
| 4729 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4730 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4731 | else |
| 4732 | #endif /* MBEDTLS_GCM_C */ |
| 4733 | #if defined(MBEDTLS_CCM_C) |
| 4734 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4735 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4736 | status = mbedtls_to_psa_error( |
| 4737 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4738 | ciphertext_length - operation.tag_length, |
| 4739 | nonce, nonce_length, |
| 4740 | additional_data, |
| 4741 | additional_data_length, |
| 4742 | ciphertext, plaintext, |
| 4743 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4744 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4745 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4746 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4747 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4748 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4749 | { |
| 4750 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4751 | { |
| 4752 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4753 | goto exit; |
| 4754 | } |
| 4755 | status = mbedtls_to_psa_error( |
| 4756 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4757 | ciphertext_length - operation.tag_length, |
| 4758 | nonce, |
| 4759 | additional_data, |
| 4760 | additional_data_length, |
| 4761 | tag, |
| 4762 | ciphertext, |
| 4763 | plaintext ) ); |
| 4764 | } |
| 4765 | else |
| 4766 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4767 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4768 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4769 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4770 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4771 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4772 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4773 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4774 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4775 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4776 | if( status == PSA_SUCCESS ) |
| 4777 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4778 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4779 | } |
| 4780 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4782 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4783 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4784 | /* Generators */ |
| 4785 | /****************************************************************/ |
| 4786 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4787 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4788 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4789 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4790 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 4791 | #endif |
| 4792 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4793 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4794 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4795 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4796 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4797 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4798 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4799 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4800 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4801 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4802 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4803 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4804 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4805 | } |
| 4806 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4807 | 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] | 4808 | { |
| 4809 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4810 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4811 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4812 | { |
| 4813 | /* The object has (apparently) been initialized but it is not |
| 4814 | * in use. It's ok to call abort on such an object, and there's |
| 4815 | * nothing to do. */ |
| 4816 | } |
| 4817 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4818 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4819 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4820 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4821 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4822 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4823 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4824 | else |
| 4825 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4826 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4827 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4828 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4829 | /* 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] | 4830 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4831 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4832 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4833 | { |
| 4834 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4835 | operation->ctx.tls12_prf.seed_length ); |
| 4836 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4837 | } |
| 4838 | |
| 4839 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4840 | { |
| 4841 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4842 | operation->ctx.tls12_prf.label_length ); |
| 4843 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4844 | } |
| 4845 | |
| 4846 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4847 | |
| 4848 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4849 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4850 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4851 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4852 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4853 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4854 | { |
| 4855 | status = PSA_ERROR_BAD_STATE; |
| 4856 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4857 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4858 | return( status ); |
| 4859 | } |
| 4860 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4861 | 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] | 4862 | size_t *capacity) |
| 4863 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4864 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4865 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4866 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4867 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4870 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4871 | return( PSA_SUCCESS ); |
| 4872 | } |
| 4873 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4874 | 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] | 4875 | size_t capacity ) |
| 4876 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4877 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4878 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4879 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4880 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4881 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4882 | return( PSA_SUCCESS ); |
| 4883 | } |
| 4884 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4885 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4886 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4887 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4888 | static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4889 | psa_algorithm_t hash_alg, |
| 4890 | uint8_t *output, |
| 4891 | size_t output_length ) |
| 4892 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4893 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4894 | psa_status_t status; |
| 4895 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4896 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4897 | return( PSA_ERROR_BAD_STATE ); |
| 4898 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4899 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4900 | while( output_length != 0 ) |
| 4901 | { |
| 4902 | /* Copy what remains of the current block */ |
| 4903 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4904 | if( n > output_length ) |
| 4905 | n = (uint8_t) output_length; |
| 4906 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4907 | output += n; |
| 4908 | output_length -= n; |
| 4909 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4910 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4911 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4912 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4913 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4914 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4915 | * object was corrupted or if this function is called directly |
| 4916 | * inside the library. */ |
| 4917 | if( hkdf->block_number == 0xff ) |
| 4918 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4919 | |
| 4920 | /* We need a new block */ |
| 4921 | ++hkdf->block_number; |
| 4922 | hkdf->offset_in_block = 0; |
| 4923 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4924 | hkdf->prk, hash_length, |
| 4925 | hash_alg ); |
| 4926 | if( status != PSA_SUCCESS ) |
| 4927 | return( status ); |
| 4928 | if( hkdf->block_number != 1 ) |
| 4929 | { |
| 4930 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4931 | hkdf->output_block, |
| 4932 | hash_length ); |
| 4933 | if( status != PSA_SUCCESS ) |
| 4934 | return( status ); |
| 4935 | } |
| 4936 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4937 | hkdf->info, |
| 4938 | hkdf->info_length ); |
| 4939 | if( status != PSA_SUCCESS ) |
| 4940 | return( status ); |
| 4941 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4942 | &hkdf->block_number, 1 ); |
| 4943 | if( status != PSA_SUCCESS ) |
| 4944 | return( status ); |
| 4945 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4946 | hkdf->output_block, |
| 4947 | sizeof( hkdf->output_block ) ); |
| 4948 | if( status != PSA_SUCCESS ) |
| 4949 | return( status ); |
| 4950 | } |
| 4951 | |
| 4952 | return( PSA_SUCCESS ); |
| 4953 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4954 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4955 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4956 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4957 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4958 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4959 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4960 | psa_algorithm_t alg ) |
| 4961 | { |
| 4962 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4963 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4964 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4965 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4966 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4967 | /* We can't be wanting more output after block 0xff, otherwise |
| 4968 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4969 | * prevented this call. It could happen only if the operation |
| 4970 | * object was corrupted or if this function is called directly |
| 4971 | * inside the library. */ |
| 4972 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4973 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4974 | |
| 4975 | /* We need a new block */ |
| 4976 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4977 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4978 | |
| 4979 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4980 | * |
| 4981 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4982 | * |
| 4983 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4984 | * HMAC_hash(secret, A(2) + seed) + |
| 4985 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4986 | * |
| 4987 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4988 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4989 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4990 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4991 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4992 | * is currently extracted as `output_block` and where i is |
| 4993 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4994 | */ |
| 4995 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4996 | /* Save the hash context before using it, to preserve the hash state with |
| 4997 | * only the inner padding in it. We need this, because inner padding depends |
| 4998 | * on the key (secret in the RFC's terminology). */ |
| 4999 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5000 | if( status != PSA_SUCCESS ) |
| 5001 | goto cleanup; |
| 5002 | |
| 5003 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5004 | if( tls12_prf->block_number == 1 ) |
| 5005 | { |
| 5006 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5007 | * the variable seed and in this instance means it in the context of the |
| 5008 | * P_hash function, where seed = label + seed.) */ |
| 5009 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5010 | tls12_prf->label, tls12_prf->label_length ); |
| 5011 | if( status != PSA_SUCCESS ) |
| 5012 | goto cleanup; |
| 5013 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5014 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5015 | if( status != PSA_SUCCESS ) |
| 5016 | goto cleanup; |
| 5017 | } |
| 5018 | else |
| 5019 | { |
| 5020 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5021 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5022 | tls12_prf->Ai, hash_length ); |
| 5023 | if( status != PSA_SUCCESS ) |
| 5024 | goto cleanup; |
| 5025 | } |
| 5026 | |
| 5027 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5028 | tls12_prf->Ai, hash_length ); |
| 5029 | if( status != PSA_SUCCESS ) |
| 5030 | goto cleanup; |
| 5031 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5032 | if( status != PSA_SUCCESS ) |
| 5033 | goto cleanup; |
| 5034 | |
| 5035 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5036 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5037 | tls12_prf->Ai, hash_length ); |
| 5038 | if( status != PSA_SUCCESS ) |
| 5039 | goto cleanup; |
| 5040 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5041 | tls12_prf->label, tls12_prf->label_length ); |
| 5042 | if( status != PSA_SUCCESS ) |
| 5043 | goto cleanup; |
| 5044 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5045 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5046 | if( status != PSA_SUCCESS ) |
| 5047 | goto cleanup; |
| 5048 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5049 | tls12_prf->output_block, hash_length ); |
| 5050 | if( status != PSA_SUCCESS ) |
| 5051 | goto cleanup; |
| 5052 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5053 | if( status != PSA_SUCCESS ) |
| 5054 | goto cleanup; |
| 5055 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5056 | |
| 5057 | cleanup: |
| 5058 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5059 | cleanup_status = psa_hash_abort( &backup ); |
| 5060 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5061 | status = cleanup_status; |
| 5062 | |
| 5063 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5064 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5065 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5066 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5067 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5068 | psa_algorithm_t alg, |
| 5069 | uint8_t *output, |
| 5070 | size_t output_length ) |
| 5071 | { |
| 5072 | 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] | 5073 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5074 | psa_status_t status; |
| 5075 | uint8_t offset, length; |
| 5076 | |
| 5077 | while( output_length != 0 ) |
| 5078 | { |
| 5079 | /* Check if we have fully processed the current block. */ |
| 5080 | if( tls12_prf->left_in_block == 0 ) |
| 5081 | { |
| 5082 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5083 | alg ); |
| 5084 | if( status != PSA_SUCCESS ) |
| 5085 | return( status ); |
| 5086 | |
| 5087 | continue; |
| 5088 | } |
| 5089 | |
| 5090 | if( tls12_prf->left_in_block > output_length ) |
| 5091 | length = (uint8_t) output_length; |
| 5092 | else |
| 5093 | length = tls12_prf->left_in_block; |
| 5094 | |
| 5095 | offset = hash_length - tls12_prf->left_in_block; |
| 5096 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5097 | output += length; |
| 5098 | output_length -= length; |
| 5099 | tls12_prf->left_in_block -= length; |
| 5100 | } |
| 5101 | |
| 5102 | return( PSA_SUCCESS ); |
| 5103 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5104 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5105 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5106 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5107 | psa_status_t psa_key_derivation_output_bytes( |
| 5108 | psa_key_derivation_operation_t *operation, |
| 5109 | uint8_t *output, |
| 5110 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5111 | { |
| 5112 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5113 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5114 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5115 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5116 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5117 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5118 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5121 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5122 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5123 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5124 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5125 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5126 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5127 | goto exit; |
| 5128 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5129 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5130 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5131 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5132 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5133 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5134 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5135 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5136 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5137 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5138 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5140 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5141 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5142 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5143 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5144 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5145 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5146 | output, output_length ); |
| 5147 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5148 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5149 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5150 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5151 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5152 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5153 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5154 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5155 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5156 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5157 | output_length ); |
| 5158 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5159 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5160 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5161 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5162 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5163 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5164 | return( PSA_ERROR_BAD_STATE ); |
| 5165 | } |
| 5166 | |
| 5167 | exit: |
| 5168 | if( status != PSA_SUCCESS ) |
| 5169 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5170 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5171 | * This allows us to differentiate between exhausted operations and |
| 5172 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5173 | * operations. */ |
| 5174 | psa_algorithm_t alg = operation->alg; |
| 5175 | psa_key_derivation_abort( operation ); |
| 5176 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5177 | memset( output, '!', output_length ); |
| 5178 | } |
| 5179 | return( status ); |
| 5180 | } |
| 5181 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5182 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5183 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5184 | { |
| 5185 | if( data_size >= 8 ) |
| 5186 | mbedtls_des_key_set_parity( data ); |
| 5187 | if( data_size >= 16 ) |
| 5188 | mbedtls_des_key_set_parity( data + 8 ); |
| 5189 | if( data_size >= 24 ) |
| 5190 | mbedtls_des_key_set_parity( data + 16 ); |
| 5191 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5192 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5193 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5194 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5195 | psa_key_slot_t *slot, |
| 5196 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5197 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5198 | { |
| 5199 | uint8_t *data = NULL; |
| 5200 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5201 | psa_status_t status; |
| 5202 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5203 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5204 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5205 | if( bits % 8 != 0 ) |
| 5206 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5207 | data = mbedtls_calloc( 1, bytes ); |
| 5208 | if( data == NULL ) |
| 5209 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5210 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5211 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5212 | if( status != PSA_SUCCESS ) |
| 5213 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5214 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5215 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5216 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5217 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5218 | |
| 5219 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5220 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 5221 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5222 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5223 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5224 | psa_key_attributes_t attributes = { |
| 5225 | .core = slot->attr |
| 5226 | }; |
| 5227 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5228 | status = psa_driver_wrapper_import_key( &attributes, |
| 5229 | data, bytes, |
| 5230 | slot->key.data, |
| 5231 | slot->key.bytes, |
| 5232 | &slot->key.bytes, &bits ); |
| 5233 | if( bits != slot->attr.bits ) |
| 5234 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5235 | |
| 5236 | exit: |
| 5237 | mbedtls_free( data ); |
| 5238 | return( status ); |
| 5239 | } |
| 5240 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5241 | 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] | 5242 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5243 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5244 | { |
| 5245 | psa_status_t status; |
| 5246 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5247 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5248 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5249 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5250 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5251 | /* Reject any attempt to create a zero-length key so that we don't |
| 5252 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5253 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5254 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5255 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5256 | if( ! operation->can_output_key ) |
| 5257 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5258 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5259 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5260 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5261 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5262 | if( driver != NULL ) |
| 5263 | { |
| 5264 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5265 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5266 | } |
| 5267 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5268 | if( status == PSA_SUCCESS ) |
| 5269 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5270 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5271 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5272 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5273 | } |
| 5274 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5275 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5276 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5277 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5278 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5279 | return( status ); |
| 5280 | } |
| 5281 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5282 | |
| 5283 | |
| 5284 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5285 | /* Key derivation */ |
| 5286 | /****************************************************************/ |
| 5287 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5288 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5289 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5290 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5291 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5292 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5293 | int is_kdf_alg_supported; |
| 5294 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5295 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5296 | * initialisers for this union leave some bytes unspecified.) */ |
| 5297 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5298 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5299 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5300 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5301 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5302 | is_kdf_alg_supported = 1; |
| 5303 | else |
| 5304 | #endif |
| 5305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5306 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5307 | is_kdf_alg_supported = 1; |
| 5308 | else |
| 5309 | #endif |
| 5310 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5311 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5312 | is_kdf_alg_supported = 1; |
| 5313 | else |
| 5314 | #endif |
| 5315 | is_kdf_alg_supported = 0; |
| 5316 | |
| 5317 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5318 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5319 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5320 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5321 | if( hash_size == 0 ) |
| 5322 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5323 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5324 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5325 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5326 | { |
| 5327 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5328 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5329 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5330 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5331 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5332 | |
| 5333 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5334 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5335 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5336 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5337 | 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] | 5338 | psa_algorithm_t alg ) |
| 5339 | { |
| 5340 | psa_status_t status; |
| 5341 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5342 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5343 | return( PSA_ERROR_BAD_STATE ); |
| 5344 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5345 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5346 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5347 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5348 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5349 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5350 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5351 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5352 | #else |
| 5353 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5354 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5355 | } |
| 5356 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5357 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5358 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5359 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5360 | #else |
| 5361 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5362 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5363 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5364 | else |
| 5365 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5366 | |
| 5367 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5368 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5369 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5370 | } |
| 5371 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5372 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5373 | 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] | 5374 | psa_algorithm_t hash_alg, |
| 5375 | psa_key_derivation_step_t step, |
| 5376 | const uint8_t *data, |
| 5377 | size_t data_length ) |
| 5378 | { |
| 5379 | psa_status_t status; |
| 5380 | switch( step ) |
| 5381 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5382 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5383 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5384 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5385 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5386 | data, data_length, |
| 5387 | hash_alg ); |
| 5388 | if( status != PSA_SUCCESS ) |
| 5389 | return( status ); |
| 5390 | hkdf->state = HKDF_STATE_STARTED; |
| 5391 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5392 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5393 | /* If no salt was provided, use an empty salt. */ |
| 5394 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5395 | { |
| 5396 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5397 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5398 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5399 | if( status != PSA_SUCCESS ) |
| 5400 | return( status ); |
| 5401 | hkdf->state = HKDF_STATE_STARTED; |
| 5402 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5403 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5404 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5405 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5406 | data, data_length ); |
| 5407 | if( status != PSA_SUCCESS ) |
| 5408 | return( status ); |
| 5409 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5410 | hkdf->prk, |
| 5411 | sizeof( hkdf->prk ) ); |
| 5412 | if( status != PSA_SUCCESS ) |
| 5413 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5414 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5415 | hkdf->block_number = 0; |
| 5416 | hkdf->state = HKDF_STATE_KEYED; |
| 5417 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5418 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5419 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5420 | return( PSA_ERROR_BAD_STATE ); |
| 5421 | if( hkdf->info_set ) |
| 5422 | return( PSA_ERROR_BAD_STATE ); |
| 5423 | hkdf->info_length = data_length; |
| 5424 | if( data_length != 0 ) |
| 5425 | { |
| 5426 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5427 | if( hkdf->info == NULL ) |
| 5428 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5429 | memcpy( hkdf->info, data, data_length ); |
| 5430 | } |
| 5431 | hkdf->info_set = 1; |
| 5432 | return( PSA_SUCCESS ); |
| 5433 | default: |
| 5434 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5435 | } |
| 5436 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5437 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5438 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5439 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5440 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5441 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5442 | const uint8_t *data, |
| 5443 | size_t data_length ) |
| 5444 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5445 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5446 | return( PSA_ERROR_BAD_STATE ); |
| 5447 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5448 | if( data_length != 0 ) |
| 5449 | { |
| 5450 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5451 | if( prf->seed == NULL ) |
| 5452 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5453 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5454 | memcpy( prf->seed, data, data_length ); |
| 5455 | prf->seed_length = data_length; |
| 5456 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5457 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5458 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5459 | |
| 5460 | return( PSA_SUCCESS ); |
| 5461 | } |
| 5462 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5463 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5464 | psa_algorithm_t hash_alg, |
| 5465 | const uint8_t *data, |
| 5466 | size_t data_length ) |
| 5467 | { |
| 5468 | psa_status_t status; |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5469 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5470 | return( PSA_ERROR_BAD_STATE ); |
| 5471 | |
| 5472 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5473 | if( status != PSA_SUCCESS ) |
| 5474 | return( status ); |
| 5475 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5476 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5477 | |
| 5478 | return( PSA_SUCCESS ); |
| 5479 | } |
| 5480 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5481 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5482 | const uint8_t *data, |
| 5483 | size_t data_length ) |
| 5484 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5485 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5486 | return( PSA_ERROR_BAD_STATE ); |
| 5487 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5488 | if( data_length != 0 ) |
| 5489 | { |
| 5490 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5491 | if( prf->label == NULL ) |
| 5492 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5493 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5494 | memcpy( prf->label, data, data_length ); |
| 5495 | prf->label_length = data_length; |
| 5496 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5497 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5498 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5499 | |
| 5500 | return( PSA_SUCCESS ); |
| 5501 | } |
| 5502 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5503 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5504 | psa_algorithm_t hash_alg, |
| 5505 | psa_key_derivation_step_t step, |
| 5506 | const uint8_t *data, |
| 5507 | size_t data_length ) |
| 5508 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5509 | switch( step ) |
| 5510 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5511 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5512 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5513 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5514 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5515 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5516 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5517 | default: |
| 5518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5519 | } |
| 5520 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5521 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5522 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5523 | |
| 5524 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5525 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5526 | psa_tls12_prf_key_derivation_t *prf, |
| 5527 | psa_algorithm_t hash_alg, |
| 5528 | const uint8_t *data, |
| 5529 | size_t data_length ) |
| 5530 | { |
| 5531 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5532 | 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] | 5533 | uint8_t *cur = pms; |
| 5534 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5535 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5536 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5537 | |
| 5538 | /* Quoting RFC 4279, Section 2: |
| 5539 | * |
| 5540 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5541 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5542 | * uint16 with the value N, and the PSK itself. |
| 5543 | */ |
| 5544 | |
| 5545 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5546 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5547 | memset( cur, 0, data_length ); |
| 5548 | cur += data_length; |
| 5549 | *cur++ = pms[0]; |
| 5550 | *cur++ = pms[1]; |
| 5551 | memcpy( cur, data, data_length ); |
| 5552 | cur += data_length; |
| 5553 | |
| 5554 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5555 | |
| 5556 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5557 | return( status ); |
| 5558 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5559 | |
| 5560 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5561 | psa_tls12_prf_key_derivation_t *prf, |
| 5562 | psa_algorithm_t hash_alg, |
| 5563 | psa_key_derivation_step_t step, |
| 5564 | const uint8_t *data, |
| 5565 | size_t data_length ) |
| 5566 | { |
| 5567 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5568 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5569 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5570 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5571 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5572 | |
| 5573 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5574 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5575 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5576 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5577 | /** Check whether the given key type is acceptable for the given |
| 5578 | * input step of a key derivation. |
| 5579 | * |
| 5580 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5581 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5582 | * Both secret and non-secret inputs can alternatively have the type |
| 5583 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5584 | * that the input was passed as a buffer rather than via a key object. |
| 5585 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5586 | static int psa_key_derivation_check_input_type( |
| 5587 | psa_key_derivation_step_t step, |
| 5588 | psa_key_type_t key_type ) |
| 5589 | { |
| 5590 | switch( step ) |
| 5591 | { |
| 5592 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5593 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5594 | return( PSA_SUCCESS ); |
| 5595 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5596 | return( PSA_SUCCESS ); |
| 5597 | break; |
| 5598 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5599 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5600 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5601 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5602 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5603 | return( PSA_SUCCESS ); |
| 5604 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5605 | return( PSA_SUCCESS ); |
| 5606 | break; |
| 5607 | } |
| 5608 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5609 | } |
| 5610 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5611 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5612 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5613 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5614 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5615 | const uint8_t *data, |
| 5616 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5617 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5618 | psa_status_t status; |
| 5619 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5620 | |
| 5621 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5622 | if( status != PSA_SUCCESS ) |
| 5623 | goto exit; |
| 5624 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5625 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5626 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5627 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5628 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5629 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5630 | step, data, data_length ); |
| 5631 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5632 | else |
| 5633 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5634 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5635 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5636 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5637 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5638 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5639 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5640 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5641 | else |
| 5642 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5643 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5644 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5645 | { |
| 5646 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5647 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5648 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5649 | } |
| 5650 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5651 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5652 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5653 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5654 | (void) data; |
| 5655 | (void) data_length; |
| 5656 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5657 | return( PSA_ERROR_BAD_STATE ); |
| 5658 | } |
| 5659 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5660 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5661 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5662 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5663 | return( status ); |
| 5664 | } |
| 5665 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5666 | psa_status_t psa_key_derivation_input_bytes( |
| 5667 | psa_key_derivation_operation_t *operation, |
| 5668 | psa_key_derivation_step_t step, |
| 5669 | const uint8_t *data, |
| 5670 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5671 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5672 | return( psa_key_derivation_input_internal( operation, step, |
| 5673 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5674 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5675 | } |
| 5676 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5677 | psa_status_t psa_key_derivation_input_key( |
| 5678 | psa_key_derivation_operation_t *operation, |
| 5679 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5680 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5681 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5682 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5683 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5684 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5685 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5686 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5687 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5688 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5689 | { |
| 5690 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5691 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5692 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5693 | |
| 5694 | /* Passing a key object as a SECRET input unlocks the permission |
| 5695 | * to output to a key object. */ |
| 5696 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5697 | operation->can_output_key = 1; |
| 5698 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5699 | status = psa_key_derivation_input_internal( operation, |
| 5700 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5701 | slot->key.data, |
| 5702 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5703 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5704 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5705 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5706 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5707 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5708 | |
| 5709 | |
| 5710 | |
| 5711 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5712 | /* Key agreement */ |
| 5713 | /****************************************************************/ |
| 5714 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5715 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5716 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5717 | size_t peer_key_length, |
| 5718 | const mbedtls_ecp_keypair *our_key, |
| 5719 | uint8_t *shared_secret, |
| 5720 | size_t shared_secret_size, |
| 5721 | size_t *shared_secret_length ) |
| 5722 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5723 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5724 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5725 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5726 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5727 | 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] | 5728 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5729 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5730 | status = mbedtls_psa_ecp_load_representation( |
| 5731 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5732 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5733 | peer_key, |
| 5734 | peer_key_length, |
| 5735 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5736 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5737 | goto exit; |
| 5738 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5739 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5740 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5741 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5742 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5743 | status = mbedtls_to_psa_error( |
| 5744 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5745 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5746 | goto exit; |
| 5747 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5748 | status = mbedtls_to_psa_error( |
| 5749 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5750 | shared_secret_length, |
| 5751 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5752 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5753 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5754 | if( status != PSA_SUCCESS ) |
| 5755 | goto exit; |
| 5756 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5757 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5758 | |
| 5759 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5760 | if( status != PSA_SUCCESS ) |
| 5761 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5762 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5763 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5764 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5765 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5766 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5767 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5768 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5769 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5770 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5771 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5772 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5773 | psa_key_slot_t *private_key, |
| 5774 | const uint8_t *peer_key, |
| 5775 | size_t peer_key_length, |
| 5776 | uint8_t *shared_secret, |
| 5777 | size_t shared_secret_size, |
| 5778 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5779 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5780 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5781 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5782 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5783 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5784 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5785 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5786 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5787 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 5788 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5789 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5790 | private_key->key.data, |
| 5791 | private_key->key.bytes, |
| 5792 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5793 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5794 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5795 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5796 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5797 | shared_secret, shared_secret_size, |
| 5798 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5799 | mbedtls_ecp_keypair_free( ecp ); |
| 5800 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5801 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5802 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5803 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5804 | (void) private_key; |
| 5805 | (void) peer_key; |
| 5806 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5807 | (void) shared_secret; |
| 5808 | (void) shared_secret_size; |
| 5809 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5810 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5811 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5812 | } |
| 5813 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5814 | /* 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] | 5815 | * to potentially free embedded data structures and wipe confidential data. |
| 5816 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5817 | 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] | 5818 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5819 | psa_key_slot_t *private_key, |
| 5820 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5821 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5822 | { |
| 5823 | psa_status_t status; |
| 5824 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5825 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5826 | 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] | 5827 | |
| 5828 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5829 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5830 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5831 | private_key, |
| 5832 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5833 | shared_secret, |
| 5834 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5835 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5836 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5837 | goto exit; |
| 5838 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5839 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5840 | * the shared secret. A shared secret is permitted wherever a key |
| 5841 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5842 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5843 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5844 | shared_secret, |
| 5845 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5846 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5847 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5848 | return( status ); |
| 5849 | } |
| 5850 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5851 | 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] | 5852 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5853 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5854 | const uint8_t *peer_key, |
| 5855 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5856 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5857 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5858 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5859 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5860 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5861 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5862 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5863 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5864 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5865 | if( status != PSA_SUCCESS ) |
| 5866 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5867 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5868 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5869 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5870 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5871 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5872 | else |
| 5873 | { |
| 5874 | /* If a private key has been added as SECRET, we allow the derived |
| 5875 | * key material to be used as a key in PSA Crypto. */ |
| 5876 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5877 | operation->can_output_key = 1; |
| 5878 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5879 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5880 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5881 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5882 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5883 | } |
| 5884 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5885 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5886 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5887 | const uint8_t *peer_key, |
| 5888 | size_t peer_key_length, |
| 5889 | uint8_t *output, |
| 5890 | size_t output_size, |
| 5891 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5892 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5893 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5894 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5895 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5896 | |
| 5897 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5898 | { |
| 5899 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5900 | goto exit; |
| 5901 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5902 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5903 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5904 | if( status != PSA_SUCCESS ) |
| 5905 | goto exit; |
| 5906 | |
| 5907 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5908 | peer_key, peer_key_length, |
| 5909 | output, output_size, |
| 5910 | output_length ); |
| 5911 | |
| 5912 | exit: |
| 5913 | if( status != PSA_SUCCESS ) |
| 5914 | { |
| 5915 | /* If an error happens and is not handled properly, the output |
| 5916 | * may be used as a key to protect sensitive data. Arrange for such |
| 5917 | * a key to be random, which is likely to result in decryption or |
| 5918 | * verification errors. This is better than filling the buffer with |
| 5919 | * some constant data such as zeros, which would result in the data |
| 5920 | * being protected with a reproducible, easily knowable key. |
| 5921 | */ |
| 5922 | psa_generate_random( output, output_size ); |
| 5923 | *output_length = output_size; |
| 5924 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5925 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5926 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5927 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5928 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5929 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5930 | |
| 5931 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5932 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5933 | /****************************************************************/ |
| 5934 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5935 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5936 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5937 | /** Initialize the PSA random generator. |
| 5938 | */ |
| 5939 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5940 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5941 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5942 | memset( rng, 0, sizeof( *rng ) ); |
| 5943 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5944 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5945 | /* Set default configuration if |
| 5946 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5947 | if( rng->entropy_init == NULL ) |
| 5948 | rng->entropy_init = mbedtls_entropy_init; |
| 5949 | if( rng->entropy_free == NULL ) |
| 5950 | rng->entropy_free = mbedtls_entropy_free; |
| 5951 | |
| 5952 | rng->entropy_init( &rng->entropy ); |
| 5953 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5954 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5955 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5956 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5957 | mbedtls_entropy_add_source( &rng->entropy, |
| 5958 | mbedtls_nv_seed_poll, NULL, |
| 5959 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5960 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5961 | #endif |
| 5962 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5963 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5964 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5965 | } |
| 5966 | |
| 5967 | /** Deinitialize the PSA random generator. |
| 5968 | */ |
| 5969 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 5970 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5971 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5972 | memset( rng, 0, sizeof( *rng ) ); |
| 5973 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5974 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5975 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5976 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | /** Seed the PSA random generator. |
| 5980 | */ |
| 5981 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5982 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5983 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5984 | /* Do nothing: the external RNG seeds itself. */ |
| 5985 | (void) rng; |
| 5986 | return( PSA_SUCCESS ); |
| 5987 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5988 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5989 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5990 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5991 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5992 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5995 | psa_status_t psa_generate_random( uint8_t *output, |
| 5996 | size_t output_size ) |
| 5997 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5998 | GUARD_MODULE_INITIALIZED; |
| 5999 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6000 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6001 | |
| 6002 | size_t output_length = 0; |
| 6003 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6004 | output, output_size, |
| 6005 | &output_length ); |
| 6006 | if( status != PSA_SUCCESS ) |
| 6007 | return( status ); |
| 6008 | /* Breaking up a request into smaller chunks is currently not supported |
| 6009 | * for the extrernal RNG interface. */ |
| 6010 | if( output_length != output_size ) |
| 6011 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6012 | return( PSA_SUCCESS ); |
| 6013 | |
| 6014 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6015 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6016 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6017 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6018 | size_t request_size = |
| 6019 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6020 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6021 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6022 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6023 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6024 | if( ret != 0 ) |
| 6025 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6026 | output_size -= request_size; |
| 6027 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6028 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6029 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6030 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6031 | } |
| 6032 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6033 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6034 | * |
| 6035 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6036 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6037 | * PSA API doesn't support passing an explicit state. |
| 6038 | * |
| 6039 | * In the non-external case, psa_generate_random() calls an |
| 6040 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6041 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6042 | * instead of doing this back-and-forth between the PSA API and the |
| 6043 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6044 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6045 | */ |
| 6046 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6047 | int mbedtls_psa_get_random( void *p_rng, |
| 6048 | unsigned char *output, |
| 6049 | size_t output_size ) |
| 6050 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6051 | /* This function takes a pointer to the RNG state because that's what |
| 6052 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6053 | * its own state internally and doesn't let the caller access that state. |
| 6054 | * So we just ignore the state parameter, and in practice we'll pass |
| 6055 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6056 | (void) p_rng; |
| 6057 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6058 | if( status == PSA_SUCCESS ) |
| 6059 | return( 0 ); |
| 6060 | else |
| 6061 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6062 | } |
| 6063 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6064 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6065 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6066 | #include "mbedtls/entropy_poll.h" |
| 6067 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6068 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6069 | size_t seed_size ) |
| 6070 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6071 | if( global_data.initialized ) |
| 6072 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6073 | |
| 6074 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6075 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6076 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6077 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6078 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6079 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6080 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6081 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6082 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6083 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6084 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6085 | * \param type The key type |
| 6086 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6087 | * |
| 6088 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6089 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6090 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6091 | * The size in bits of the key is not valid. |
| 6092 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6093 | * The type and/or the size in bits of the key or the combination of |
| 6094 | * the two is not supported. |
| 6095 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6096 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 6097 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6098 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6099 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6100 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6101 | if( key_type_is_raw_bytes( type ) ) |
| 6102 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6103 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6104 | if( status != PSA_SUCCESS ) |
| 6105 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6106 | } |
| 6107 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6108 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6109 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6110 | { |
| 6111 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6112 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6113 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6114 | /* Accept only byte-aligned keys, for the same reasons as |
| 6115 | * in psa_import_rsa_key(). */ |
| 6116 | if( bits % 8 != 0 ) |
| 6117 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6118 | } |
| 6119 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6120 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6121 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6122 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6123 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6124 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6125 | /* To avoid empty block, return successfully here. */ |
| 6126 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6127 | } |
| 6128 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6129 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6130 | { |
| 6131 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6132 | } |
| 6133 | |
| 6134 | return( PSA_SUCCESS ); |
| 6135 | } |
| 6136 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6137 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6138 | const psa_key_attributes_t *attributes, |
| 6139 | 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] | 6140 | { |
| 6141 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6142 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6143 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6144 | if( ( attributes->domain_parameters == NULL ) && |
| 6145 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6146 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6147 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6148 | if( key_type_is_raw_bytes( type ) ) |
| 6149 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6150 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6151 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6152 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6153 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6154 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6155 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6156 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6157 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6158 | } |
| 6159 | else |
| 6160 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6161 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6162 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6163 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 6164 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 6165 | key_buffer, |
| 6166 | key_buffer_size, |
| 6167 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6168 | } |
| 6169 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6170 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6171 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6172 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6173 | 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] | 6174 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 6175 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 6176 | key_buffer, |
| 6177 | key_buffer_size, |
| 6178 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6179 | } |
| 6180 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6181 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6182 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6183 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6184 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6185 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6186 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6187 | return( PSA_SUCCESS ); |
| 6188 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6189 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6190 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6191 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6192 | { |
| 6193 | psa_status_t status; |
| 6194 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6195 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6196 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6197 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6198 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6199 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6200 | /* Reject any attempt to create a zero-length key so that we don't |
| 6201 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6202 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6203 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6204 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6205 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6206 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6207 | if( status != PSA_SUCCESS ) |
| 6208 | goto exit; |
| 6209 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6210 | /* In the case of a transparent key or an opaque key stored in local |
| 6211 | * storage (thus not in the case of generating a key in a secure element |
| 6212 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 6213 | * hold the generated key material. */ |
| 6214 | if( slot->key.data == NULL ) |
| 6215 | { |
| 6216 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 6217 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 6218 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6219 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6220 | attributes->core.type, attributes->core.bits ); |
| 6221 | if( status != PSA_SUCCESS ) |
| 6222 | goto exit; |
| 6223 | |
| 6224 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6225 | attributes->core.type, |
| 6226 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6227 | } |
| 6228 | else |
| 6229 | { |
| 6230 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6231 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6232 | if( status != PSA_SUCCESS ) |
| 6233 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6234 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6235 | |
| 6236 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6237 | if( status != PSA_SUCCESS ) |
| 6238 | goto exit; |
| 6239 | } |
| 6240 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6241 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6242 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6243 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6244 | if( status != PSA_SUCCESS ) |
| 6245 | psa_remove_key_data_from_memory( slot ); |
| 6246 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6247 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6248 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6249 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6250 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6251 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6252 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6253 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6254 | } |
| 6255 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6256 | /****************************************************************/ |
| 6257 | /* Module setup */ |
| 6258 | /****************************************************************/ |
| 6259 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6260 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6261 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6262 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6263 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6264 | { |
| 6265 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6266 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6267 | global_data.rng.entropy_init = entropy_init; |
| 6268 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6269 | return( PSA_SUCCESS ); |
| 6270 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6271 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6272 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6273 | void mbedtls_psa_crypto_free( void ) |
| 6274 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6275 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6276 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6277 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6278 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6279 | } |
| 6280 | /* Wipe all remaining data, including configuration. |
| 6281 | * In particular, this sets all state indicator to the value |
| 6282 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6283 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6284 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6285 | /* Unregister all secure element drivers, so that we restart from |
| 6286 | * a pristine state. */ |
| 6287 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6288 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6289 | } |
| 6290 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6291 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6292 | /** Recover a transaction that was interrupted by a power failure. |
| 6293 | * |
| 6294 | * This function is called during initialization, before psa_crypto_init() |
| 6295 | * returns. If this function returns a failure status, the initialization |
| 6296 | * fails. |
| 6297 | */ |
| 6298 | static psa_status_t psa_crypto_recover_transaction( |
| 6299 | const psa_crypto_transaction_t *transaction ) |
| 6300 | { |
| 6301 | switch( transaction->unknown.type ) |
| 6302 | { |
| 6303 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6304 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6305 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6306 | * is implemented. |
| 6307 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6308 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6309 | default: |
| 6310 | /* We found an unsupported transaction in the storage. |
| 6311 | * 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] | 6312 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6313 | } |
| 6314 | } |
| 6315 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6316 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6317 | psa_status_t psa_crypto_init( void ) |
| 6318 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6319 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6320 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6321 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6322 | if( global_data.initialized != 0 ) |
| 6323 | return( PSA_SUCCESS ); |
| 6324 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6325 | /* Initialize and seed the random generator. */ |
| 6326 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6327 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6328 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6329 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6330 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6331 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6332 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6333 | status = psa_initialize_key_slots( ); |
| 6334 | if( status != PSA_SUCCESS ) |
| 6335 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6337 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6338 | status = psa_init_all_se_drivers( ); |
| 6339 | if( status != PSA_SUCCESS ) |
| 6340 | goto exit; |
| 6341 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6342 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6343 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6344 | status = psa_crypto_load_transaction( ); |
| 6345 | if( status == PSA_SUCCESS ) |
| 6346 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6347 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6348 | if( status != PSA_SUCCESS ) |
| 6349 | goto exit; |
| 6350 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6351 | } |
| 6352 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6353 | { |
| 6354 | /* There's no transaction to complete. It's all good. */ |
| 6355 | status = PSA_SUCCESS; |
| 6356 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6357 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6358 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6359 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6360 | global_data.initialized = 1; |
| 6361 | |
| 6362 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6363 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6364 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6365 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |