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 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 157 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 158 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 159 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 160 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 161 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 163 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 164 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 165 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 166 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 167 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 168 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 169 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 171 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 172 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 174 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 175 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 176 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 177 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 178 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 179 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 180 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 181 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 182 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 184 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 185 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 186 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 187 | |
| 188 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 189 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 190 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 191 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 192 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 193 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 194 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 195 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 196 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 197 | |
| 198 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 199 | return( PSA_ERROR_BAD_STATE ); |
| 200 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 201 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 202 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 204 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 205 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 206 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 207 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 208 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 209 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 210 | return( PSA_ERROR_INVALID_PADDING ); |
| 211 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 212 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 213 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 214 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 215 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 216 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 217 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 218 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 219 | |
| 220 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 221 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 222 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 223 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 224 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 225 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 226 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 227 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 228 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 229 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 230 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 231 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 232 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 233 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 234 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 235 | |
| 236 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 237 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 238 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 239 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 240 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 241 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 242 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 243 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 244 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 245 | |
| 246 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 247 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 248 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 250 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 251 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 252 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 253 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 254 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 255 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 256 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 258 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 259 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 260 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 261 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 262 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 263 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 264 | #endif |
| 265 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 266 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 267 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 268 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 269 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 271 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 272 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 273 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 274 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 276 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 277 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 281 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 282 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 283 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 284 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 285 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 287 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 289 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 290 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 291 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 292 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 293 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 294 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 295 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 296 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 297 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 298 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 299 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 300 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 301 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 302 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 303 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 304 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 305 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 306 | return( PSA_ERROR_NOT_PERMITTED ); |
| 307 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 309 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 310 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 311 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 312 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 313 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 314 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 315 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 316 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 317 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 318 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 319 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 320 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 321 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 322 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 323 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 324 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 325 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 326 | return( PSA_ERROR_INVALID_PADDING ); |
| 327 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 328 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 329 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 330 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 331 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 332 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 333 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 334 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 335 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 336 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 337 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 338 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 339 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 340 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 341 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 342 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 343 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 344 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 345 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 346 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 347 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 348 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 349 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 350 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 351 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 352 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 353 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 354 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 355 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 356 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 357 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 358 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 359 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 360 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 361 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 362 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 363 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 364 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 365 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 366 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 367 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 368 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 369 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 370 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 371 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 375 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 376 | |
| 377 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 378 | /****************************************************************/ |
| 379 | /* Key management */ |
| 380 | /****************************************************************/ |
| 381 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 382 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 383 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 384 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 385 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 386 | } |
| 387 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 388 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 389 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 390 | * current test driver in key_management.c is using this function |
| 391 | * when accelerators are used for ECC key pair and public key. |
| 392 | * Once that dependency is resolved these guards can be removed. |
| 393 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 394 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 395 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 396 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 397 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 398 | 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] | 399 | size_t bits, |
| 400 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 401 | { |
| 402 | switch( curve ) |
| 403 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 404 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 405 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 407 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 409 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 410 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 413 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 414 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 417 | case 528: |
| 418 | if( bits_is_sloppy ) |
| 419 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 420 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 421 | } |
| 422 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 424 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 425 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 427 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 428 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 429 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 430 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 431 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 432 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 433 | } |
| 434 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 435 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 436 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 437 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 438 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 439 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 440 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 441 | case 256: |
| 442 | if( bits_is_sloppy ) |
| 443 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 444 | break; |
| 445 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 446 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 447 | } |
| 448 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 450 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 451 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 452 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 453 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 454 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 455 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 456 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 457 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 458 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 459 | } |
| 460 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 461 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 462 | |
| 463 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 464 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 465 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 466 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 467 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 468 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 469 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 470 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 471 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 472 | { |
| 473 | /* Check that the bit size is acceptable for the key type */ |
| 474 | switch( type ) |
| 475 | { |
| 476 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 477 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 478 | case PSA_KEY_TYPE_DERIVE: |
| 479 | break; |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 480 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 481 | case PSA_KEY_TYPE_AES: |
| 482 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 484 | break; |
| 485 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 486 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | case PSA_KEY_TYPE_CAMELLIA: |
| 488 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 489 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 490 | break; |
| 491 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 492 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 493 | case PSA_KEY_TYPE_DES: |
| 494 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 495 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 496 | break; |
| 497 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 498 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 499 | case PSA_KEY_TYPE_ARC4: |
| 500 | if( bits < 8 || bits > 2048 ) |
| 501 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 502 | break; |
| 503 | #endif |
David Brown | 1bfe4d7 | 2021-02-16 12:54:35 -0700 | [diff] [blame] | 504 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 505 | case PSA_KEY_TYPE_CHACHA20: |
| 506 | if( bits != 256 ) |
| 507 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 508 | break; |
| 509 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 510 | default: |
| 511 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 512 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 513 | if( bits % 8 != 0 ) |
| 514 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 515 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 516 | return( PSA_SUCCESS ); |
| 517 | } |
| 518 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 519 | /** Return the size of the key in the given slot, in bits. |
| 520 | * |
| 521 | * \param[in] slot A key slot. |
| 522 | * |
| 523 | * \return The key size in bits, read from the metadata in the slot. |
| 524 | */ |
| 525 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 526 | { |
| 527 | return( slot->attr.bits ); |
| 528 | } |
| 529 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 530 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 531 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 532 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 533 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 534 | * |
| 535 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 536 | * \param[in] key_type The key type of the key to be used with the |
| 537 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 538 | * |
| 539 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 540 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 541 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 542 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 543 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 544 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 545 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 546 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 547 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 548 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 549 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 550 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 551 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 552 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 553 | |
| 554 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 555 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 556 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 557 | * key. */ |
| 558 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 559 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 560 | { |
| 561 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 562 | * the block length (larger than 1) for block ciphers. */ |
| 563 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 564 | return( PSA_SUCCESS ); |
| 565 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 566 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 567 | |
| 568 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 571 | /** Try to allocate a buffer to an empty key slot. |
| 572 | * |
| 573 | * \param[in,out] slot Key slot to attach buffer to. |
| 574 | * \param[in] buffer_length Requested size of the buffer. |
| 575 | * |
| 576 | * \retval #PSA_SUCCESS |
| 577 | * The buffer has been successfully allocated. |
| 578 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 579 | * Not enough memory was available for allocation. |
| 580 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 581 | * Trying to allocate a buffer to a non-empty key slot. |
| 582 | */ |
| 583 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 584 | size_t buffer_length ) |
| 585 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 586 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 587 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 588 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 589 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 590 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 591 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 592 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 593 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 594 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 597 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 598 | const uint8_t* data, |
| 599 | size_t data_length ) |
| 600 | { |
| 601 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 602 | data_length ); |
| 603 | if( status != PSA_SUCCESS ) |
| 604 | return( status ); |
| 605 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 606 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 607 | return( PSA_SUCCESS ); |
| 608 | } |
| 609 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 610 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 611 | const psa_key_attributes_t *attributes, |
| 612 | const uint8_t *data, size_t data_length, |
| 613 | uint8_t *key_buffer, size_t key_buffer_size, |
| 614 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 615 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 616 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 617 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 618 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 619 | /* zero-length keys are never supported. */ |
| 620 | if( data_length == 0 ) |
| 621 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 622 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 623 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 624 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 625 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 626 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 627 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 628 | if( data_length > SIZE_MAX / 8 ) |
| 629 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 630 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 631 | /* Enforce a size limit, and in particular ensure that the bit |
| 632 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 633 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 634 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 635 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 636 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 637 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 638 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 639 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 640 | /* Copy the key material. */ |
| 641 | memcpy( key_buffer, data, data_length ); |
| 642 | *key_buffer_length = data_length; |
| 643 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 644 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 645 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 646 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 647 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 648 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 649 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 650 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 651 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 652 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 653 | return( mbedtls_psa_ecp_import_key( attributes, |
| 654 | data, data_length, |
| 655 | key_buffer, key_buffer_size, |
| 656 | key_buffer_length, |
| 657 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 658 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 659 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 660 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 661 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 662 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 663 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 664 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 665 | return( mbedtls_psa_rsa_import_key( attributes, |
| 666 | data, data_length, |
| 667 | key_buffer, key_buffer_size, |
| 668 | key_buffer_length, |
| 669 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 670 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 671 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 672 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 673 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 674 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 675 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 678 | /** Calculate the intersection of two algorithm usage policies. |
| 679 | * |
| 680 | * Return 0 (which allows no operation) on incompatibility. |
| 681 | */ |
| 682 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 683 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 684 | psa_algorithm_t alg1, |
| 685 | psa_algorithm_t alg2 ) |
| 686 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 687 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 688 | if( alg1 == alg2 ) |
| 689 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 690 | /* If the policies are from the same hash-and-sign family, check |
| 691 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 692 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 693 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 694 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 695 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 697 | return( alg2 ); |
| 698 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 699 | return( alg1 ); |
| 700 | } |
| 701 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 702 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 703 | * restrictive tag length. */ |
| 704 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 705 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 706 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 707 | { |
| 708 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 709 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 710 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 711 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 712 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 713 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 714 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 715 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 716 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 717 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 718 | } |
| 719 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 720 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 721 | ( alg1_len <= alg2_len ) ) |
| 722 | { |
| 723 | return( alg2 ); |
| 724 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 725 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 726 | ( alg2_len <= alg1_len ) ) |
| 727 | { |
| 728 | return( alg1 ); |
| 729 | } |
| 730 | } |
| 731 | /* If the policies are from the same MAC family, check whether one |
| 732 | * of them is a minimum-MAC-length policy. Calculate the most |
| 733 | * restrictive tag length. */ |
| 734 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 735 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 736 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 737 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 738 | /* Validate the combination of key type and algorithm. Since the base |
| 739 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 740 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 741 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 742 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 743 | /* Get the (exact or at-least) output lengths for both sides of the |
| 744 | * requested intersection. None of the currently supported algorithms |
| 745 | * have an output length dependent on the actual key size, so setting it |
| 746 | * to a bogus value of 0 is currently OK. |
| 747 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 748 | * Note that for at-least-this-length wildcard algorithms, the output |
| 749 | * length is set to the shortest allowed length, which allows us to |
| 750 | * calculate the most restrictive tag length for the intersection. */ |
| 751 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 752 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 753 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 754 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 755 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 756 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 757 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 758 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 759 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 760 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 761 | |
| 762 | /* If only one is an at-least-this-length policy, the intersection would |
| 763 | * be the other (fixed-length) policy as long as said fixed length is |
| 764 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 765 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 766 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 767 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 768 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 769 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 770 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 771 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 772 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 773 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 774 | /* If none of them are wildcards, check whether they define the same tag |
| 775 | * length. This is still possible here when one is default-length and |
| 776 | * the other specific-length. Ensure to always return the |
| 777 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 778 | if( alg1_len == alg2_len ) |
| 779 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 780 | } |
| 781 | /* If the policies are incompatible, allow nothing. */ |
| 782 | return( 0 ); |
| 783 | } |
| 784 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 785 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 786 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 787 | psa_algorithm_t requested_alg ) |
| 788 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 789 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 790 | if( requested_alg == policy_alg ) |
| 791 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 792 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 793 | * and requested_alg is the same hash-and-sign family with any hash, |
| 794 | * then requested_alg is compliant with policy_alg. */ |
| 795 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 796 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 797 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 798 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 799 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 800 | } |
| 801 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 802 | * the requested algorithm, check the requested tag length to be |
| 803 | * equal-length or longer than the wildcard-specified length. */ |
| 804 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 805 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 806 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 807 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 808 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 809 | { |
| 810 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 811 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 812 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 813 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 814 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 815 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 816 | PSA_ALG_IS_MAC( requested_alg ) && |
| 817 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 818 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 819 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 820 | /* Validate the combination of key type and algorithm. Since the policy |
| 821 | * and requested algorithms are the same, we only need this once. */ |
| 822 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 823 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 824 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 825 | /* Get both the requested output length for the algorithm which is to be |
| 826 | * verified, and the default output length for the base algorithm. |
| 827 | * Note that none of the currently supported algorithms have an output |
| 828 | * length dependent on actual key size, so setting it to a bogus value |
| 829 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 830 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 831 | key_type, 0, requested_alg ); |
| 832 | size_t default_output_length = PSA_MAC_LENGTH( |
| 833 | key_type, 0, |
| 834 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 835 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 836 | /* If the policy is default-length, only allow an algorithm with |
| 837 | * a declared exact-length matching the default. */ |
| 838 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 839 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 840 | |
| 841 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 842 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 843 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 844 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 845 | { |
| 846 | return( 1 ); |
| 847 | } |
| 848 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 849 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 850 | * check for the requested MAC length to be equal to or longer than the |
| 851 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 852 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 853 | { |
| 854 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 855 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 856 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 857 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 858 | /* 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] | 859 | * a key derivation with that key agreement should also be allowed. This |
| 860 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 861 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 862 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 863 | { |
| 864 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 865 | policy_alg ); |
| 866 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 867 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 868 | return( 0 ); |
| 869 | } |
| 870 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 871 | /** Test whether a policy permits an algorithm. |
| 872 | * |
| 873 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 874 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 875 | * \note This function requires providing the key type for which the policy is |
| 876 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 877 | * have different properties depending on what kind of cipher it is |
| 878 | * combined with. |
| 879 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 880 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 881 | * allowed by the \p policy. |
| 882 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 883 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 884 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 885 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 886 | static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy, |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 887 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 888 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 889 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 890 | /* '0' is not a valid algorithm */ |
| 891 | if( alg == 0 ) |
| 892 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 893 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 894 | /* A requested algorithm cannot be a wildcard. */ |
| 895 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 896 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 897 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 898 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 899 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 900 | return( PSA_SUCCESS ); |
| 901 | else |
| 902 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 905 | /** Restrict a key policy based on a constraint. |
| 906 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 907 | * \note This function requires providing the key type for which the policy is |
| 908 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 909 | * have different properties depending on what kind of cipher it is |
| 910 | * combined with. |
| 911 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 912 | * \param[in] key_type The key type for which to restrict the policy |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 913 | * \param[in,out] policy The policy to restrict. |
| 914 | * \param[in] constraint The policy constraint to apply. |
| 915 | * |
| 916 | * \retval #PSA_SUCCESS |
| 917 | * \c *policy contains the intersection of the original value of |
| 918 | * \c *policy and \c *constraint. |
| 919 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 920 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 921 | * \c *policy is unchanged. |
| 922 | */ |
| 923 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 924 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 925 | psa_key_policy_t *policy, |
| 926 | const psa_key_policy_t *constraint ) |
| 927 | { |
| 928 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 929 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 930 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 931 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 932 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 933 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 934 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 935 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 936 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 937 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 938 | policy->usage &= constraint->usage; |
| 939 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 940 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 941 | return( PSA_SUCCESS ); |
| 942 | } |
| 943 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 944 | /** Get the description of a key given its identifier and policy constraints |
| 945 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 946 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 947 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 948 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 949 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 950 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 951 | * In case of a persistent key, the function loads the description of the key |
| 952 | * into a key slot if not already done. |
| 953 | * |
| 954 | * On success, the returned key slot is locked. It is the responsibility of |
| 955 | * 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] | 956 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 957 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 958 | mbedtls_svc_key_id_t key, |
| 959 | psa_key_slot_t **p_slot, |
| 960 | psa_key_usage_t usage, |
| 961 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 962 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 963 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 964 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 965 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 966 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 967 | if( status != PSA_SUCCESS ) |
| 968 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 969 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 970 | |
| 971 | /* Enforce that usage policy for the key slot contains all the flags |
| 972 | * required by the usage parameter. There is one exception: public |
| 973 | * keys can always be exported, so we treat public key objects as |
| 974 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 975 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 976 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 977 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 978 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 979 | { |
| 980 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 981 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 982 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 983 | |
| 984 | /* Enforce that the usage policy permits the requested algortihm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 985 | if( alg != 0 ) |
| 986 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 987 | status = psa_key_policy_permits( &slot->attr.policy, |
| 988 | slot->attr.type, |
| 989 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 990 | if( status != PSA_SUCCESS ) |
| 991 | goto error; |
| 992 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 993 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 994 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 995 | |
| 996 | error: |
| 997 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 998 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 999 | |
| 1000 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1001 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1002 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1003 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1004 | * |
| 1005 | * A transparent key is a key for which the key material is directly |
| 1006 | * available, as opposed to a key in a secure element. |
| 1007 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1008 | * This is a temporary function to use instead of |
| 1009 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1010 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1011 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1012 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1013 | * 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] | 1014 | */ |
| 1015 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1016 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1017 | mbedtls_svc_key_id_t key, |
| 1018 | psa_key_slot_t **p_slot, |
| 1019 | psa_key_usage_t usage, |
| 1020 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1021 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1022 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1023 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1024 | if( status != PSA_SUCCESS ) |
| 1025 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1026 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1027 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1028 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1029 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1030 | *p_slot = NULL; |
| 1031 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1032 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1033 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1034 | return( PSA_SUCCESS ); |
| 1035 | } |
| 1036 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1037 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1038 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1039 | 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] | 1040 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1041 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1042 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1043 | 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] | 1044 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1045 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1046 | * initialized slot, so we can just free it. */ |
| 1047 | if( slot->key.data != NULL ) |
| 1048 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1049 | |
| 1050 | mbedtls_free( slot->key.data ); |
| 1051 | slot->key.data = NULL; |
| 1052 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1053 | |
| 1054 | return( PSA_SUCCESS ); |
| 1055 | } |
| 1056 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1057 | /** Completely wipe a slot in memory, including its policy. |
| 1058 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1059 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1060 | { |
| 1061 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1062 | |
| 1063 | /* |
| 1064 | * As the return error code may not be handled in case of multiple errors, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1065 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1066 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1067 | * part of the execution of a test suite this will stop the test suite |
Ronald Cron | 4640c15 | 2020-11-13 10:11:01 +0100 | [diff] [blame] | 1068 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1069 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1070 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1071 | { |
| 1072 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1073 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1074 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1075 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1076 | } |
| 1077 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1078 | /* Multipart operations may still be using the key. This is safe |
| 1079 | * because all multipart operation objects are independent from |
| 1080 | * the key slot: if they need to access the key after the setup |
| 1081 | * phase, they have a copy of the key. Note that this means that |
| 1082 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1083 | /* At this point, key material and other type-specific content has |
| 1084 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1085 | * zeroize because the metadata is not particularly sensitive. */ |
| 1086 | memset( slot, 0, sizeof( *slot ) ); |
| 1087 | return( status ); |
| 1088 | } |
| 1089 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1090 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1091 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1092 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1093 | psa_status_t status; /* status of the last operation */ |
| 1094 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1095 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1096 | psa_se_drv_table_entry_t *driver; |
| 1097 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1098 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1099 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1100 | return( PSA_SUCCESS ); |
| 1101 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1102 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1103 | * Get the description of the key in a key slot. In case of a persistent |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1104 | * key, this will load the key description from persistent memory if not |
| 1105 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1106 | * the key is operated by an SE or not and this information is needed by |
| 1107 | * the current implementation. |
| 1108 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1109 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1110 | if( status != PSA_SUCCESS ) |
| 1111 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1112 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1113 | /* |
| 1114 | * If the key slot containing the key description is under access by the |
| 1115 | * library (apart from the present access), the key cannot be destroyed |
| 1116 | * yet. For the time being, just return in error. Eventually (to be |
| 1117 | * implemented), the key should be destroyed when all accesses have |
| 1118 | * stopped. |
| 1119 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1120 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1121 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1122 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1123 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1124 | } |
| 1125 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1126 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1127 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1128 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1129 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1130 | /* For a key in a secure element, we need to do three things: |
| 1131 | * remove the key file in internal storage, destroy the |
| 1132 | * key inside the secure element, and update the driver's |
| 1133 | * persistent data. Start a transaction that will encompass these |
| 1134 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1135 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1136 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1137 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1138 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1139 | status = psa_crypto_save_transaction( ); |
| 1140 | if( status != PSA_SUCCESS ) |
| 1141 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1142 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1143 | /* We should still try to destroy the key in the secure |
| 1144 | * element and the key metadata in storage. This is especially |
| 1145 | * important if the error is that the storage is full. |
| 1146 | * But how to do it exactly without risking an inconsistent |
| 1147 | * state after a reset? |
| 1148 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1149 | */ |
| 1150 | overall_status = status; |
| 1151 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1154 | status = psa_destroy_se_key( driver, |
| 1155 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1156 | if( overall_status == PSA_SUCCESS ) |
| 1157 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1158 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1159 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1160 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1161 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1162 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1163 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1164 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1165 | if( overall_status == PSA_SUCCESS ) |
| 1166 | overall_status = status; |
| 1167 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1168 | /* TODO: other slots may have a copy of the same key. We should |
| 1169 | * invalidate them. |
| 1170 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1171 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1172 | } |
| 1173 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1174 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1175 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1176 | if( driver != NULL ) |
| 1177 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1178 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1179 | if( overall_status == PSA_SUCCESS ) |
| 1180 | overall_status = status; |
| 1181 | status = psa_crypto_stop_transaction( ); |
| 1182 | if( overall_status == PSA_SUCCESS ) |
| 1183 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1184 | } |
| 1185 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1186 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1187 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1188 | exit: |
| 1189 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1190 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1191 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1192 | if( overall_status == PSA_SUCCESS ) |
| 1193 | overall_status = status; |
| 1194 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1197 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1198 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1199 | static psa_status_t psa_get_rsa_public_exponent( |
| 1200 | const mbedtls_rsa_context *rsa, |
| 1201 | psa_key_attributes_t *attributes ) |
| 1202 | { |
| 1203 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1204 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1205 | uint8_t *buffer = NULL; |
| 1206 | size_t buflen; |
| 1207 | mbedtls_mpi_init( &mpi ); |
| 1208 | |
| 1209 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1210 | if( ret != 0 ) |
| 1211 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1212 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1213 | { |
| 1214 | /* It's the default value, which is reported as an empty string, |
| 1215 | * so there's nothing to do. */ |
| 1216 | goto exit; |
| 1217 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1218 | |
| 1219 | buflen = mbedtls_mpi_size( &mpi ); |
| 1220 | buffer = mbedtls_calloc( 1, buflen ); |
| 1221 | if( buffer == NULL ) |
| 1222 | { |
| 1223 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1224 | goto exit; |
| 1225 | } |
| 1226 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1227 | if( ret != 0 ) |
| 1228 | goto exit; |
| 1229 | attributes->domain_parameters = buffer; |
| 1230 | attributes->domain_parameters_size = buflen; |
| 1231 | |
| 1232 | exit: |
| 1233 | mbedtls_mpi_free( &mpi ); |
| 1234 | if( ret != 0 ) |
| 1235 | mbedtls_free( buffer ); |
| 1236 | return( mbedtls_to_psa_error( ret ) ); |
| 1237 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1238 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1239 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1240 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1241 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1242 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1243 | 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] | 1244 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1245 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1246 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1247 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1248 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1249 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1250 | psa_reset_key_attributes( attributes ); |
| 1251 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1252 | 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] | 1253 | if( status != PSA_SUCCESS ) |
| 1254 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1255 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1256 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1257 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1258 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1259 | |
| 1260 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1261 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1262 | psa_set_key_slot_number( attributes, |
| 1263 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1264 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1265 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1266 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1267 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1268 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1269 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1270 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1271 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1272 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1273 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1274 | * is not yet implemented. |
| 1275 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1276 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1277 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1278 | break; |
| 1279 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1280 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1281 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1282 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1283 | status = mbedtls_psa_rsa_load_representation( |
| 1284 | slot->attr.type, |
| 1285 | slot->key.data, |
| 1286 | slot->key.bytes, |
| 1287 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1288 | if( status != PSA_SUCCESS ) |
| 1289 | break; |
| 1290 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1291 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1292 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1293 | mbedtls_rsa_free( rsa ); |
| 1294 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1295 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1296 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1297 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1298 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1299 | default: |
| 1300 | /* Nothing else to do. */ |
| 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | if( status != PSA_SUCCESS ) |
| 1305 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1306 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1307 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1308 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1309 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1312 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1313 | psa_status_t psa_get_key_slot_number( |
| 1314 | const psa_key_attributes_t *attributes, |
| 1315 | psa_key_slot_number_t *slot_number ) |
| 1316 | { |
| 1317 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1318 | { |
| 1319 | *slot_number = attributes->slot_number; |
| 1320 | return( PSA_SUCCESS ); |
| 1321 | } |
| 1322 | else |
| 1323 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1324 | } |
| 1325 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1326 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1327 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1328 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1329 | uint8_t *data, |
| 1330 | size_t data_size, |
| 1331 | size_t *data_length ) |
| 1332 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1333 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1334 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1335 | memcpy( data, key_buffer, key_buffer_size ); |
| 1336 | memset( data + key_buffer_size, 0, |
| 1337 | data_size - key_buffer_size ); |
| 1338 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1339 | return( PSA_SUCCESS ); |
| 1340 | } |
| 1341 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1342 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1343 | const psa_key_attributes_t *attributes, |
| 1344 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1345 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1346 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1347 | psa_key_type_t type = attributes->core.type; |
| 1348 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1349 | if( key_type_is_raw_bytes( type ) || |
| 1350 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1351 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1352 | { |
| 1353 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1354 | key_buffer, key_buffer_size, |
| 1355 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1356 | } |
| 1357 | else |
| 1358 | { |
| 1359 | /* This shouldn't happen in the reference implementation, but |
| 1360 | it is valid for a special-purpose implementation to omit |
| 1361 | support for exporting certain key types. */ |
| 1362 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1367 | uint8_t *data, |
| 1368 | size_t data_size, |
| 1369 | size_t *data_length ) |
| 1370 | { |
| 1371 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1372 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1373 | psa_key_slot_t *slot; |
| 1374 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1375 | /* Reject a zero-length output buffer now, since this can never be a |
| 1376 | * valid key representation. This way we know that data must be a valid |
| 1377 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1378 | if( data_size == 0 ) |
| 1379 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1380 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1381 | /* Set the key to empty now, so that even when there are errors, we always |
| 1382 | * set data_length to a value between 0 and data_size. On error, setting |
| 1383 | * the key to empty is a good choice because an empty key representation is |
| 1384 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1385 | *data_length = 0; |
| 1386 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1387 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1388 | * which don't require any flag, but |
| 1389 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1390 | */ |
| 1391 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1392 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1393 | if( status != PSA_SUCCESS ) |
| 1394 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1395 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1396 | psa_key_attributes_t attributes = { |
| 1397 | .core = slot->attr |
| 1398 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1399 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1400 | slot->key.data, slot->key.bytes, |
| 1401 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1402 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1403 | unlock_status = psa_unlock_key_slot( slot ); |
| 1404 | |
| 1405 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1406 | } |
| 1407 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1408 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1409 | const psa_key_attributes_t *attributes, |
| 1410 | const uint8_t *key_buffer, |
| 1411 | size_t key_buffer_size, |
| 1412 | uint8_t *data, |
| 1413 | size_t data_size, |
| 1414 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1415 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1416 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1417 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1418 | 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] | 1419 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1420 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1421 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1422 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1423 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1424 | key_buffer, key_buffer_size, |
| 1425 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1426 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1427 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1428 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1429 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1430 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1431 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1432 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1433 | key_buffer, |
| 1434 | key_buffer_size, |
| 1435 | data, |
| 1436 | data_size, |
| 1437 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1438 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1439 | /* We don't know how to convert a private RSA key to public. */ |
| 1440 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1441 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1442 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1443 | } |
| 1444 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1445 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1446 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1447 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1448 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1449 | key_buffer, |
| 1450 | key_buffer_size, |
| 1451 | data, |
| 1452 | data_size, |
| 1453 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1454 | #else |
| 1455 | /* 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] | 1456 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1457 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1458 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1459 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1460 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1461 | else |
| 1462 | { |
| 1463 | /* This shouldn't happen in the reference implementation, but |
| 1464 | it is valid for a special-purpose implementation to omit |
| 1465 | support for exporting certain key types. */ |
| 1466 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1467 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1468 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1469 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1470 | 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] | 1471 | uint8_t *data, |
| 1472 | size_t data_size, |
| 1473 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1474 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1475 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1476 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1477 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1478 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1479 | /* Reject a zero-length output buffer now, since this can never be a |
| 1480 | * valid key representation. This way we know that data must be a valid |
| 1481 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1482 | if( data_size == 0 ) |
| 1483 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1484 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1485 | /* Set the key to empty now, so that even when there are errors, we always |
| 1486 | * set data_length to a value between 0 and data_size. On error, setting |
| 1487 | * the key to empty is a good choice because an empty key representation is |
| 1488 | * unlikely to be accepted anywhere. */ |
| 1489 | *data_length = 0; |
| 1490 | |
| 1491 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1492 | 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] | 1493 | if( status != PSA_SUCCESS ) |
| 1494 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1495 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1496 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1497 | { |
| 1498 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1499 | goto exit; |
| 1500 | } |
| 1501 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1502 | psa_key_attributes_t attributes = { |
| 1503 | .core = slot->attr |
| 1504 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1505 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1506 | &attributes, slot->key.data, slot->key.bytes, |
| 1507 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1508 | |
| 1509 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1510 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1511 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1512 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1513 | } |
| 1514 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1515 | #if defined(static_assert) |
| 1516 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1517 | "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] | 1518 | 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] | 1519 | "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] | 1520 | 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] | 1521 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1522 | #endif |
| 1523 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1524 | /** Validate that a key policy is internally well-formed. |
| 1525 | * |
| 1526 | * This function only rejects invalid policies. It does not validate the |
| 1527 | * consistency of the policy with respect to other attributes of the key |
| 1528 | * such as the key type. |
| 1529 | */ |
| 1530 | 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] | 1531 | { |
| 1532 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1533 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1534 | PSA_KEY_USAGE_ENCRYPT | |
| 1535 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1536 | PSA_KEY_USAGE_SIGN_HASH | |
| 1537 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1538 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1539 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1540 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1541 | return( PSA_SUCCESS ); |
| 1542 | } |
| 1543 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1544 | /** Validate the internal consistency of key attributes. |
| 1545 | * |
| 1546 | * This function only rejects invalid attribute values. If does not |
| 1547 | * validate the consistency of the attributes with any key data that may |
| 1548 | * be involved in the creation of the key. |
| 1549 | * |
| 1550 | * Call this function early in the key creation process. |
| 1551 | * |
| 1552 | * \param[in] attributes Key attributes for the new key. |
| 1553 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1554 | * NULL for a transparent key. |
| 1555 | * |
| 1556 | */ |
| 1557 | static psa_status_t psa_validate_key_attributes( |
| 1558 | const psa_key_attributes_t *attributes, |
| 1559 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1560 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1561 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1562 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1563 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1564 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1565 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1566 | if( status != PSA_SUCCESS ) |
| 1567 | return( status ); |
| 1568 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1569 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1570 | if( status != PSA_SUCCESS ) |
| 1571 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1572 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1573 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1574 | { |
| 1575 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1576 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1577 | } |
| 1578 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1579 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1580 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1581 | if( status != PSA_SUCCESS ) |
| 1582 | return( status ); |
| 1583 | } |
| 1584 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1585 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1586 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1587 | return( status ); |
| 1588 | |
| 1589 | /* Refuse to create overly large keys. |
| 1590 | * Note that this doesn't trigger on import if the attributes don't |
| 1591 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1592 | * psa_import_key() needs its own checks. */ |
| 1593 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1594 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1595 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1596 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1597 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1598 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1599 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1600 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1601 | return( PSA_SUCCESS ); |
| 1602 | } |
| 1603 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1604 | /** Prepare a key slot to receive key material. |
| 1605 | * |
| 1606 | * This function allocates a key slot and sets its metadata. |
| 1607 | * |
| 1608 | * If this function fails, call psa_fail_key_creation(). |
| 1609 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1610 | * This function is intended to be used as follows: |
| 1611 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1612 | * it with the specified attributes, and in case of a volatile key assign it |
| 1613 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1614 | * -# Populate the slot with the key material. |
| 1615 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1616 | * In case of failure at any step, stop the sequence and call |
| 1617 | * psa_fail_key_creation(). |
| 1618 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1619 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1620 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1621 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1622 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1623 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1624 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1625 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1626 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1627 | * |
| 1628 | * \retval #PSA_SUCCESS |
| 1629 | * The key slot is ready to receive key material. |
| 1630 | * \return If this function fails, the key slot is an invalid state. |
| 1631 | * 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] | 1632 | */ |
| 1633 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1634 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1635 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1636 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1637 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1638 | { |
| 1639 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1640 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | psa_key_slot_t *slot; |
| 1642 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1643 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1644 | *p_drv = NULL; |
| 1645 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1646 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1647 | if( status != PSA_SUCCESS ) |
| 1648 | return( status ); |
| 1649 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1650 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1651 | if( status != PSA_SUCCESS ) |
| 1652 | return( status ); |
| 1653 | slot = *p_slot; |
| 1654 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1655 | /* We're storing the declared bit-size of the key. It's up to each |
| 1656 | * creation mechanism to verify that this information is correct. |
| 1657 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1658 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1659 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1660 | * volatile key identifier associated to the slot returned to contain its |
| 1661 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1662 | |
| 1663 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1664 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1665 | { |
| 1666 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1667 | slot->attr.id = volatile_key_id; |
| 1668 | #else |
| 1669 | slot->attr.id.key_id = volatile_key_id; |
| 1670 | #endif |
| 1671 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1672 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1673 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1674 | * external-only flags, query `attributes`. Thanks to the check |
| 1675 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1676 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1677 | * may have set. */ |
| 1678 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1679 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1680 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1681 | /* 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] | 1682 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1683 | * create the key file in internal storage, create the |
| 1684 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1685 | * persistent data. This is done by starting a transaction that will |
| 1686 | * encompass these three actions. |
| 1687 | * For registering a volatile key, we just need to find an appropriate |
| 1688 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1689 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1690 | /* The first thing to do is to find a slot number for the new key. |
| 1691 | * We save the slot number in persistent storage as part of the |
| 1692 | * transaction data. It will be needed to recover if the power |
| 1693 | * fails during the key creation process, to clean up on the secure |
| 1694 | * element side after restarting. Obtaining a slot number from the |
| 1695 | * secure element driver updates its persistent state, but we do not yet |
| 1696 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1697 | * 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] | 1698 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1699 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1700 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1701 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1702 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1703 | if( status != PSA_SUCCESS ) |
| 1704 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1705 | |
| 1706 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1707 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1708 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1709 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1710 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1711 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1712 | status = psa_crypto_save_transaction( ); |
| 1713 | if( status != PSA_SUCCESS ) |
| 1714 | { |
| 1715 | (void) psa_crypto_stop_transaction( ); |
| 1716 | return( status ); |
| 1717 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1718 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1719 | |
| 1720 | status = psa_copy_key_material_into_slot( |
| 1721 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1722 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1723 | |
| 1724 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1725 | { |
| 1726 | /* Key registration only makes sense with a secure element. */ |
| 1727 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1728 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1729 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1730 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1731 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1732 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1733 | |
| 1734 | /** Finalize the creation of a key once its key material has been set. |
| 1735 | * |
| 1736 | * This entails writing the key to persistent storage. |
| 1737 | * |
| 1738 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1739 | * See the documentation of psa_start_key_creation() for the intended use |
| 1740 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1741 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1742 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1743 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1744 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1745 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1746 | * \param[in,out] slot Pointer to the slot with key material. |
| 1747 | * \param[in] driver The secure element driver for the key, |
| 1748 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1749 | * \param[out] key On success, identifier of the key. Note that the |
| 1750 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1751 | * |
| 1752 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1753 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1754 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1755 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1756 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1757 | * \retval #PSA_ERROR_DATA_INVALID |
| 1758 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1759 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1760 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1761 | * \return If this function fails, the key slot is an invalid state. |
| 1762 | * 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] | 1763 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1764 | static psa_status_t psa_finish_key_creation( |
| 1765 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1766 | psa_se_drv_table_entry_t *driver, |
| 1767 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1768 | { |
| 1769 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1770 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1771 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1772 | |
| 1773 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1774 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1775 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1776 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1777 | if( driver != NULL ) |
| 1778 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1779 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1780 | psa_key_slot_number_t slot_number = |
| 1781 | psa_key_slot_get_slot_number( slot ) ; |
| 1782 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1783 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1784 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1785 | sizeof( data.slot_number ), |
| 1786 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1787 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1788 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1789 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1790 | (uint8_t*) &data, |
| 1791 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1792 | } |
| 1793 | else |
| 1794 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1795 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1796 | /* Key material is saved in export representation in the slot, so |
| 1797 | * just pass the slot buffer for storage. */ |
| 1798 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1799 | slot->key.data, |
| 1800 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1801 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1802 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1803 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1804 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1806 | /* Finish the transaction for a key creation. This does not |
| 1807 | * happen when registering an existing key. Detect this case |
| 1808 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1809 | * creation of a persistent key in a secure element requires a transaction, |
| 1810 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1811 | if( driver != NULL && |
| 1812 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1813 | { |
| 1814 | status = psa_save_se_persistent_data( driver ); |
| 1815 | if( status != PSA_SUCCESS ) |
| 1816 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1817 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1818 | return( status ); |
| 1819 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1820 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1821 | } |
| 1822 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1823 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1824 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1825 | { |
| 1826 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1827 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1828 | if( status != PSA_SUCCESS ) |
| 1829 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1830 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1831 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1832 | return( status ); |
| 1833 | } |
| 1834 | |
| 1835 | /** Abort the creation of a key. |
| 1836 | * |
| 1837 | * You may call this function after calling psa_start_key_creation(), |
| 1838 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1839 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1840 | * See the documentation of psa_start_key_creation() for the intended use |
| 1841 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1842 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1843 | * \param[in,out] slot Pointer to the slot with key material. |
| 1844 | * \param[in] driver The secure element driver for the key, |
| 1845 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1846 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1847 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1848 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1849 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1850 | (void) driver; |
| 1851 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1852 | if( slot == NULL ) |
| 1853 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1854 | |
| 1855 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1856 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1857 | * element, and the failure happened later (when saving metadata |
| 1858 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1859 | * element. |
| 1860 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1861 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1862 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1863 | /* Abort the ongoing transaction if any (there may not be one if |
| 1864 | * the creation process failed before starting one, or if the |
| 1865 | * key creation is a registration of a key in a secure element). |
| 1866 | * Earlier functions must already have done what it takes to undo any |
| 1867 | * partial creation. All that's left is to update the transaction data |
| 1868 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1869 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1870 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1871 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1872 | psa_wipe_key_slot( slot ); |
| 1873 | } |
| 1874 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1875 | /** Validate optional attributes during key creation. |
| 1876 | * |
| 1877 | * Some key attributes are optional during key creation. If they are |
| 1878 | * specified in the attributes structure, check that they are consistent |
| 1879 | * with the data in the slot. |
| 1880 | * |
| 1881 | * This function should be called near the end of key creation, after |
| 1882 | * the slot in memory is fully populated but before saving persistent data. |
| 1883 | */ |
| 1884 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1885 | const psa_key_slot_t *slot, |
| 1886 | const psa_key_attributes_t *attributes ) |
| 1887 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1888 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1889 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1890 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1891 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1892 | } |
| 1893 | |
| 1894 | if( attributes->domain_parameters_size != 0 ) |
| 1895 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1896 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1897 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1898 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1899 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1900 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1901 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1902 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1903 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1904 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1905 | slot->attr.type, |
| 1906 | slot->key.data, |
| 1907 | slot->key.bytes, |
| 1908 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1909 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1910 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1911 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1912 | mbedtls_mpi_init( &actual ); |
| 1913 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1914 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1915 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1916 | mbedtls_rsa_free( rsa ); |
| 1917 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1918 | if( ret != 0 ) |
| 1919 | goto rsa_exit; |
| 1920 | ret = mbedtls_mpi_read_binary( &required, |
| 1921 | attributes->domain_parameters, |
| 1922 | attributes->domain_parameters_size ); |
| 1923 | if( ret != 0 ) |
| 1924 | goto rsa_exit; |
| 1925 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1926 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1927 | rsa_exit: |
| 1928 | mbedtls_mpi_free( &actual ); |
| 1929 | mbedtls_mpi_free( &required ); |
| 1930 | if( ret != 0) |
| 1931 | return( mbedtls_to_psa_error( ret ) ); |
| 1932 | } |
| 1933 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1934 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1935 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1936 | { |
| 1937 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1938 | } |
| 1939 | } |
| 1940 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1941 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1942 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1943 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1944 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1945 | } |
| 1946 | |
| 1947 | return( PSA_SUCCESS ); |
| 1948 | } |
| 1949 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1950 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1951 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1952 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1953 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | { |
| 1955 | psa_status_t status; |
| 1956 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1957 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1958 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1959 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1960 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1961 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1962 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1963 | * This also rejects any key which might be encoded as an empty string, |
| 1964 | * which is never valid. */ |
| 1965 | if( data_length == 0 ) |
| 1966 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1967 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1968 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1969 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1970 | if( status != PSA_SUCCESS ) |
| 1971 | goto exit; |
| 1972 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1973 | /* In the case of a transparent key or an opaque key stored in local |
| 1974 | * storage (thus not in the case of generating a key in a secure element |
| 1975 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1976 | * hold the generated key material. */ |
| 1977 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1978 | { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1979 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1980 | if( status != PSA_SUCCESS ) |
| 1981 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1982 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1983 | |
| 1984 | bits = slot->attr.bits; |
| 1985 | status = psa_driver_wrapper_import_key( attributes, |
| 1986 | data, data_length, |
| 1987 | slot->key.data, |
| 1988 | slot->key.bytes, |
| 1989 | &slot->key.bytes, &bits ); |
| 1990 | if( status != PSA_SUCCESS ) |
| 1991 | goto exit; |
| 1992 | |
| 1993 | if( slot->attr.bits == 0 ) |
| 1994 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1995 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1996 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1997 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1998 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1999 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2000 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2001 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2002 | if( status != PSA_SUCCESS ) |
| 2003 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2004 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2005 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2006 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2007 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2008 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2009 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2010 | return( status ); |
| 2011 | } |
| 2012 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2013 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2014 | psa_status_t mbedtls_psa_register_se_key( |
| 2015 | const psa_key_attributes_t *attributes ) |
| 2016 | { |
| 2017 | psa_status_t status; |
| 2018 | psa_key_slot_t *slot = NULL; |
| 2019 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2020 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2021 | |
| 2022 | /* Leaving attributes unspecified is not currently supported. |
| 2023 | * It could make sense to query the key type and size from the |
| 2024 | * secure element, but not all secure elements support this |
| 2025 | * and the driver HAL doesn't currently support it. */ |
| 2026 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2027 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2028 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2029 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2030 | |
| 2031 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2032 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2033 | if( status != PSA_SUCCESS ) |
| 2034 | goto exit; |
| 2035 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2036 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2037 | |
| 2038 | exit: |
| 2039 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2040 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2041 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2042 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2043 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2044 | return( status ); |
| 2045 | } |
| 2046 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2047 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2048 | 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] | 2049 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2050 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2051 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2052 | source->key.data, |
| 2053 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2054 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2055 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2056 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2057 | target->attr.type = source->attr.type; |
| 2058 | target->attr.bits = source->attr.bits; |
| 2059 | |
| 2060 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2061 | } |
| 2062 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2063 | 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] | 2064 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2065 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2066 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2067 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2068 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2069 | psa_key_slot_t *source_slot = NULL; |
| 2070 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2071 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2072 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2073 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2074 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2075 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2076 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2077 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2078 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2079 | goto exit; |
| 2080 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2081 | status = psa_validate_optional_attributes( source_slot, |
| 2082 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2083 | if( status != PSA_SUCCESS ) |
| 2084 | goto exit; |
| 2085 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2086 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2087 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2088 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2089 | if( status != PSA_SUCCESS ) |
| 2090 | goto exit; |
| 2091 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2092 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2093 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2094 | if( status != PSA_SUCCESS ) |
| 2095 | goto exit; |
| 2096 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2097 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2098 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2099 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2100 | /* Copying to a secure element is not implemented yet. */ |
| 2101 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2102 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2103 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2104 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2105 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2106 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2107 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2108 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2109 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2110 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2111 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2112 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2113 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2114 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2115 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2116 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2117 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2120 | |
| 2121 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2122 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2123 | /* Message digests */ |
| 2124 | /****************************************************************/ |
| 2125 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2126 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2127 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2128 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2129 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Ronald Cron | 36f641b | 2021-02-16 17:20:43 +0100 | [diff] [blame] | 2130 | 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] | 2131 | { |
| 2132 | switch( alg ) |
| 2133 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2134 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2135 | case PSA_ALG_MD2: |
| 2136 | return( &mbedtls_md2_info ); |
| 2137 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2138 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2139 | case PSA_ALG_MD4: |
| 2140 | return( &mbedtls_md4_info ); |
| 2141 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2142 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2143 | case PSA_ALG_MD5: |
| 2144 | return( &mbedtls_md5_info ); |
| 2145 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2146 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2147 | case PSA_ALG_RIPEMD160: |
| 2148 | return( &mbedtls_ripemd160_info ); |
| 2149 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2150 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2151 | case PSA_ALG_SHA_1: |
| 2152 | return( &mbedtls_sha1_info ); |
| 2153 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2154 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2155 | case PSA_ALG_SHA_224: |
| 2156 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2157 | #endif |
| 2158 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2159 | case PSA_ALG_SHA_256: |
| 2160 | return( &mbedtls_sha256_info ); |
| 2161 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2162 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2163 | case PSA_ALG_SHA_384: |
| 2164 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2165 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2166 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2167 | case PSA_ALG_SHA_512: |
| 2168 | return( &mbedtls_sha512_info ); |
| 2169 | #endif |
| 2170 | default: |
| 2171 | return( NULL ); |
| 2172 | } |
| 2173 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2174 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2175 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2176 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2177 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2178 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2179 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2180 | { |
| 2181 | switch( operation->alg ) |
| 2182 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2183 | case 0: |
| 2184 | /* The object has (apparently) been initialized but it is not |
| 2185 | * in use. It's ok to call abort on such an object, and there's |
| 2186 | * nothing to do. */ |
| 2187 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2188 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | case PSA_ALG_MD2: |
| 2190 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2191 | break; |
| 2192 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2193 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | case PSA_ALG_MD4: |
| 2195 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2196 | break; |
| 2197 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2198 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2199 | case PSA_ALG_MD5: |
| 2200 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2201 | break; |
| 2202 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2203 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2204 | case PSA_ALG_RIPEMD160: |
| 2205 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2206 | break; |
| 2207 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2208 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2209 | case PSA_ALG_SHA_1: |
| 2210 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2211 | break; |
| 2212 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2213 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2214 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2215 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2216 | break; |
| 2217 | #endif |
| 2218 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2219 | case PSA_ALG_SHA_256: |
| 2220 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2221 | break; |
| 2222 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2224 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2225 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2226 | break; |
| 2227 | #endif |
| 2228 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2229 | case PSA_ALG_SHA_512: |
| 2230 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2231 | break; |
| 2232 | #endif |
| 2233 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2234 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2235 | } |
| 2236 | operation->alg = 0; |
| 2237 | return( PSA_SUCCESS ); |
| 2238 | } |
| 2239 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2240 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2241 | psa_algorithm_t alg ) |
| 2242 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2243 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2244 | |
| 2245 | /* A context must be freshly initialized before it can be set up. */ |
| 2246 | if( operation->alg != 0 ) |
| 2247 | { |
| 2248 | return( PSA_ERROR_BAD_STATE ); |
| 2249 | } |
| 2250 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2251 | switch( alg ) |
| 2252 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2253 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2254 | case PSA_ALG_MD2: |
| 2255 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2256 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2257 | break; |
| 2258 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2259 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2260 | case PSA_ALG_MD4: |
| 2261 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2262 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2263 | break; |
| 2264 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2265 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2266 | case PSA_ALG_MD5: |
| 2267 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2268 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2269 | break; |
| 2270 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2271 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2272 | case PSA_ALG_RIPEMD160: |
| 2273 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2274 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2275 | break; |
| 2276 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2277 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2278 | case PSA_ALG_SHA_1: |
| 2279 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2280 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2281 | break; |
| 2282 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2283 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2284 | case PSA_ALG_SHA_224: |
| 2285 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2286 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2287 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2288 | #endif |
| 2289 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2290 | case PSA_ALG_SHA_256: |
| 2291 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2292 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2293 | break; |
| 2294 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2296 | case PSA_ALG_SHA_384: |
| 2297 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2298 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2299 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2300 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2301 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2302 | case PSA_ALG_SHA_512: |
| 2303 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2304 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2305 | break; |
| 2306 | #endif |
| 2307 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2308 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2309 | PSA_ERROR_NOT_SUPPORTED : |
| 2310 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2311 | } |
| 2312 | if( ret == 0 ) |
| 2313 | operation->alg = alg; |
| 2314 | else |
| 2315 | psa_hash_abort( operation ); |
| 2316 | return( mbedtls_to_psa_error( ret ) ); |
| 2317 | } |
| 2318 | |
| 2319 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2320 | const uint8_t *input, |
| 2321 | size_t input_length ) |
| 2322 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2323 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2324 | |
| 2325 | /* Don't require hash implementations to behave correctly on a |
| 2326 | * zero-length input, which may have an invalid pointer. */ |
| 2327 | if( input_length == 0 ) |
| 2328 | return( PSA_SUCCESS ); |
| 2329 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2330 | switch( operation->alg ) |
| 2331 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2332 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2333 | case PSA_ALG_MD2: |
| 2334 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2335 | input, input_length ); |
| 2336 | break; |
| 2337 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2338 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2339 | case PSA_ALG_MD4: |
| 2340 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2341 | input, input_length ); |
| 2342 | break; |
| 2343 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2344 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2345 | case PSA_ALG_MD5: |
| 2346 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2347 | input, input_length ); |
| 2348 | break; |
| 2349 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2350 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2351 | case PSA_ALG_RIPEMD160: |
| 2352 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2353 | input, input_length ); |
| 2354 | break; |
| 2355 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2356 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2357 | case PSA_ALG_SHA_1: |
| 2358 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2359 | input, input_length ); |
| 2360 | break; |
| 2361 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2362 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2363 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2364 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2365 | input, input_length ); |
| 2366 | break; |
| 2367 | #endif |
| 2368 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2369 | case PSA_ALG_SHA_256: |
| 2370 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2371 | input, input_length ); |
| 2372 | break; |
| 2373 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2374 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2375 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2376 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2377 | input, input_length ); |
| 2378 | break; |
| 2379 | #endif |
| 2380 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2381 | case PSA_ALG_SHA_512: |
| 2382 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2383 | input, input_length ); |
| 2384 | break; |
| 2385 | #endif |
| 2386 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2387 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2388 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2389 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2390 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2391 | if( ret != 0 ) |
| 2392 | psa_hash_abort( operation ); |
| 2393 | return( mbedtls_to_psa_error( ret ) ); |
| 2394 | } |
| 2395 | |
| 2396 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2397 | uint8_t *hash, |
| 2398 | size_t hash_size, |
| 2399 | size_t *hash_length ) |
| 2400 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2401 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2402 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2403 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2404 | |
| 2405 | /* Fill the output buffer with something that isn't a valid hash |
| 2406 | * (barring an attack on the hash and deliberately-crafted input), |
| 2407 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2408 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2409 | /* If hash_size is 0 then hash may be NULL and then the |
| 2410 | * call to memset would have undefined behavior. */ |
| 2411 | if( hash_size != 0 ) |
| 2412 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2413 | |
| 2414 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2415 | { |
| 2416 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2417 | goto exit; |
| 2418 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2419 | |
| 2420 | switch( operation->alg ) |
| 2421 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2422 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2423 | case PSA_ALG_MD2: |
| 2424 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2425 | break; |
| 2426 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2427 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2428 | case PSA_ALG_MD4: |
| 2429 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2430 | break; |
| 2431 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2432 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2433 | case PSA_ALG_MD5: |
| 2434 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2435 | break; |
| 2436 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2437 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2438 | case PSA_ALG_RIPEMD160: |
| 2439 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2440 | break; |
| 2441 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2442 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2443 | case PSA_ALG_SHA_1: |
| 2444 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2445 | break; |
| 2446 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2447 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2448 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2449 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2450 | break; |
| 2451 | #endif |
| 2452 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2453 | case PSA_ALG_SHA_256: |
| 2454 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2455 | break; |
| 2456 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2457 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2458 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2459 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2460 | break; |
| 2461 | #endif |
| 2462 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2463 | case PSA_ALG_SHA_512: |
| 2464 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2465 | break; |
| 2466 | #endif |
| 2467 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2468 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2469 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2470 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2471 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2472 | exit: |
| 2473 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2474 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2475 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2476 | return( psa_hash_abort( operation ) ); |
| 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2481 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2485 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2486 | const uint8_t *hash, |
| 2487 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2488 | { |
| 2489 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2490 | size_t actual_hash_length; |
| 2491 | psa_status_t status = psa_hash_finish( operation, |
| 2492 | actual_hash, sizeof( actual_hash ), |
| 2493 | &actual_hash_length ); |
| 2494 | if( status != PSA_SUCCESS ) |
| 2495 | return( status ); |
| 2496 | if( actual_hash_length != hash_length ) |
| 2497 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2498 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2499 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2500 | return( PSA_SUCCESS ); |
| 2501 | } |
| 2502 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2503 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2504 | const uint8_t *input, size_t input_length, |
| 2505 | uint8_t *hash, size_t hash_size, |
| 2506 | size_t *hash_length ) |
| 2507 | { |
| 2508 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2509 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2510 | |
| 2511 | *hash_length = hash_size; |
| 2512 | status = psa_hash_setup( &operation, alg ); |
| 2513 | if( status != PSA_SUCCESS ) |
| 2514 | goto exit; |
| 2515 | status = psa_hash_update( &operation, input, input_length ); |
| 2516 | if( status != PSA_SUCCESS ) |
| 2517 | goto exit; |
| 2518 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2519 | if( status != PSA_SUCCESS ) |
| 2520 | goto exit; |
| 2521 | |
| 2522 | exit: |
| 2523 | if( status == PSA_SUCCESS ) |
| 2524 | status = psa_hash_abort( &operation ); |
| 2525 | else |
| 2526 | psa_hash_abort( &operation ); |
| 2527 | return( status ); |
| 2528 | } |
| 2529 | |
| 2530 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2531 | const uint8_t *input, size_t input_length, |
| 2532 | const uint8_t *hash, size_t hash_length ) |
| 2533 | { |
| 2534 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2535 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2536 | |
| 2537 | status = psa_hash_setup( &operation, alg ); |
| 2538 | if( status != PSA_SUCCESS ) |
| 2539 | goto exit; |
| 2540 | status = psa_hash_update( &operation, input, input_length ); |
| 2541 | if( status != PSA_SUCCESS ) |
| 2542 | goto exit; |
| 2543 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2544 | if( status != PSA_SUCCESS ) |
| 2545 | goto exit; |
| 2546 | |
| 2547 | exit: |
| 2548 | if( status == PSA_SUCCESS ) |
| 2549 | status = psa_hash_abort( &operation ); |
| 2550 | else |
| 2551 | psa_hash_abort( &operation ); |
| 2552 | return( status ); |
| 2553 | } |
| 2554 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2555 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2556 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2557 | { |
| 2558 | if( target_operation->alg != 0 ) |
| 2559 | return( PSA_ERROR_BAD_STATE ); |
| 2560 | |
| 2561 | switch( source_operation->alg ) |
| 2562 | { |
| 2563 | case 0: |
| 2564 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2565 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2566 | case PSA_ALG_MD2: |
| 2567 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2568 | &source_operation->ctx.md2 ); |
| 2569 | break; |
| 2570 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2571 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2572 | case PSA_ALG_MD4: |
| 2573 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2574 | &source_operation->ctx.md4 ); |
| 2575 | break; |
| 2576 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2577 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2578 | case PSA_ALG_MD5: |
| 2579 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2580 | &source_operation->ctx.md5 ); |
| 2581 | break; |
| 2582 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2583 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2584 | case PSA_ALG_RIPEMD160: |
| 2585 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2586 | &source_operation->ctx.ripemd160 ); |
| 2587 | break; |
| 2588 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2589 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2590 | case PSA_ALG_SHA_1: |
| 2591 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2592 | &source_operation->ctx.sha1 ); |
| 2593 | break; |
| 2594 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2595 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2596 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2597 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2598 | &source_operation->ctx.sha256 ); |
| 2599 | break; |
| 2600 | #endif |
| 2601 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2602 | case PSA_ALG_SHA_256: |
| 2603 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2604 | &source_operation->ctx.sha256 ); |
| 2605 | break; |
| 2606 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2607 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2608 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2609 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2610 | &source_operation->ctx.sha512 ); |
| 2611 | break; |
| 2612 | #endif |
| 2613 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2614 | case PSA_ALG_SHA_512: |
| 2615 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2616 | &source_operation->ctx.sha512 ); |
| 2617 | break; |
| 2618 | #endif |
| 2619 | default: |
| 2620 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2621 | } |
| 2622 | |
| 2623 | target_operation->alg = source_operation->alg; |
| 2624 | return( PSA_SUCCESS ); |
| 2625 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2626 | |
| 2627 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2628 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2629 | /* MAC */ |
| 2630 | /****************************************************************/ |
| 2631 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2632 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2633 | psa_algorithm_t alg, |
| 2634 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2635 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2636 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2637 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2638 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2639 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2640 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2641 | if( PSA_ALG_IS_AEAD( alg ) ) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2642 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2643 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2644 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2645 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2646 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2647 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 2648 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2649 | mode = MBEDTLS_MODE_STREAM; |
| 2650 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2651 | case PSA_ALG_CTR: |
| 2652 | mode = MBEDTLS_MODE_CTR; |
| 2653 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2654 | case PSA_ALG_CFB: |
| 2655 | mode = MBEDTLS_MODE_CFB; |
| 2656 | break; |
| 2657 | case PSA_ALG_OFB: |
| 2658 | mode = MBEDTLS_MODE_OFB; |
| 2659 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2660 | case PSA_ALG_ECB_NO_PADDING: |
| 2661 | mode = MBEDTLS_MODE_ECB; |
| 2662 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2663 | case PSA_ALG_CBC_NO_PADDING: |
| 2664 | mode = MBEDTLS_MODE_CBC; |
| 2665 | break; |
| 2666 | case PSA_ALG_CBC_PKCS7: |
| 2667 | mode = MBEDTLS_MODE_CBC; |
| 2668 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2669 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2670 | mode = MBEDTLS_MODE_CCM; |
| 2671 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2672 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2673 | mode = MBEDTLS_MODE_GCM; |
| 2674 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2675 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2676 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2677 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2678 | default: |
| 2679 | return( NULL ); |
| 2680 | } |
| 2681 | } |
| 2682 | else if( alg == PSA_ALG_CMAC ) |
| 2683 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2684 | else |
| 2685 | return( NULL ); |
| 2686 | |
| 2687 | switch( key_type ) |
| 2688 | { |
| 2689 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2690 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2691 | break; |
| 2692 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2693 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2694 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2695 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2696 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2697 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2698 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2699 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2700 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2701 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2702 | if( key_bits == 128 ) |
| 2703 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2704 | break; |
| 2705 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2706 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2707 | break; |
| 2708 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2709 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2710 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2711 | case PSA_KEY_TYPE_CHACHA20: |
| 2712 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2713 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2714 | default: |
| 2715 | return( NULL ); |
| 2716 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2717 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2718 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2719 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2720 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2721 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2722 | } |
| 2723 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2724 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2725 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2726 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2727 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2728 | { |
| 2729 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2730 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2731 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2732 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2733 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2734 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2735 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2736 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2737 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2738 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2739 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2740 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2741 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2742 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2743 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2744 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2745 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2746 | return( 128 ); |
| 2747 | default: |
| 2748 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2749 | } |
| 2750 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2751 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2752 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2753 | /* Initialize the MAC operation structure. Once this function has been |
| 2754 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2755 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2756 | psa_algorithm_t alg ) |
| 2757 | { |
| 2758 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2759 | |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2760 | operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2761 | operation->key_set = 0; |
| 2762 | operation->iv_set = 0; |
| 2763 | operation->iv_required = 0; |
| 2764 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2765 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2766 | |
| 2767 | #if defined(MBEDTLS_CMAC_C) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2768 | if( operation->alg == PSA_ALG_CMAC ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2769 | { |
| 2770 | operation->iv_required = 0; |
| 2771 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2772 | status = PSA_SUCCESS; |
| 2773 | } |
| 2774 | else |
| 2775 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2776 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2777 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2778 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2779 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2780 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2781 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2782 | } |
| 2783 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2784 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2785 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2786 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2787 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | if( status != PSA_SUCCESS ) |
| 2791 | memset( operation, 0, sizeof( *operation ) ); |
| 2792 | return( status ); |
| 2793 | } |
| 2794 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2795 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2796 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2797 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2798 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2799 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2800 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2801 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2802 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2803 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2804 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2805 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2806 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2807 | /* The object has (apparently) been initialized but it is not |
| 2808 | * in use. It's ok to call abort on such an object, and there's |
| 2809 | * nothing to do. */ |
| 2810 | return( PSA_SUCCESS ); |
| 2811 | } |
| 2812 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2813 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2814 | if( operation->alg == PSA_ALG_CMAC ) |
| 2815 | { |
| 2816 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2817 | } |
| 2818 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2819 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2820 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2821 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2822 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2823 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2824 | } |
| 2825 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2826 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2827 | { |
| 2828 | /* Sanity check (shouldn't happen: operation->alg should |
| 2829 | * always have been initialized to a valid value). */ |
| 2830 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2831 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2832 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2833 | operation->alg = 0; |
| 2834 | operation->key_set = 0; |
| 2835 | operation->iv_set = 0; |
| 2836 | operation->iv_required = 0; |
| 2837 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2838 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2839 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2840 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2841 | |
| 2842 | bad_state: |
| 2843 | /* If abort is called on an uninitialized object, we can't trust |
| 2844 | * anything. Wipe the object in case it contains confidential data. |
| 2845 | * This may result in a memory leak if a pointer gets overwritten, |
| 2846 | * but it's too late to do anything about this. */ |
| 2847 | memset( operation, 0, sizeof( *operation ) ); |
| 2848 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2849 | } |
| 2850 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2851 | #if defined(MBEDTLS_CMAC_C) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2852 | static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation, |
| 2853 | psa_key_slot_t *slot ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2854 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2855 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2856 | const mbedtls_cipher_info_t *cipher_info = |
| 2857 | mbedtls_cipher_info_from_psa( PSA_ALG_CMAC, |
| 2858 | slot->attr.type, slot->attr.bits, |
| 2859 | NULL ); |
| 2860 | if( cipher_info == NULL ) |
| 2861 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2862 | |
| 2863 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2864 | if( ret != 0 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2865 | goto exit; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2866 | |
| 2867 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2868 | slot->key.data, |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2869 | slot->attr.bits ); |
| 2870 | exit: |
| 2871 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2872 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2873 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2874 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2875 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2876 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2877 | const uint8_t *key, |
| 2878 | size_t key_length, |
| 2879 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2880 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2881 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2882 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2883 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2884 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2885 | psa_status_t status; |
| 2886 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2887 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2888 | * overflow below. This should never trigger if the hash algorithm |
| 2889 | * is implemented correctly. */ |
| 2890 | /* The size checks against the ipad and opad buffers cannot be written |
| 2891 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2892 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2893 | if( block_size > sizeof( ipad ) ) |
| 2894 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2895 | if( block_size > sizeof( hmac->opad ) ) |
| 2896 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2897 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2898 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2899 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2900 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2901 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2902 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2903 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2904 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2905 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2906 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2907 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2908 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2909 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2910 | * an invalid pointer which would make the behavior undefined. */ |
| 2911 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2912 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2913 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2914 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2915 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2916 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2917 | ipad[i] ^= 0x36; |
| 2918 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2919 | |
| 2920 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2921 | * and filling the rest of opad with the requisite constant. */ |
| 2922 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2923 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2924 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2925 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2926 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2927 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2928 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2929 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2930 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2931 | |
| 2932 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2933 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2934 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2935 | return( status ); |
| 2936 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2937 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2938 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2939 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2940 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2941 | psa_algorithm_t alg, |
| 2942 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2943 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2944 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2945 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2946 | psa_key_slot_t *slot; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2947 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2948 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2949 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2950 | /* A context must be freshly initialized before it can be set up. */ |
| 2951 | if( operation->alg != 0 ) |
| 2952 | { |
| 2953 | return( PSA_ERROR_BAD_STATE ); |
| 2954 | } |
| 2955 | |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2956 | status = psa_mac_init( operation, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2957 | if( status != PSA_SUCCESS ) |
| 2958 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2959 | if( is_sign ) |
| 2960 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2961 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2962 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2963 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2964 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2965 | goto exit; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2966 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2967 | /* Validate the combination of key type and algorithm */ |
| 2968 | status = psa_mac_key_can_do( alg, slot->attr.type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2969 | if( status != PSA_SUCCESS ) |
| 2970 | goto exit; |
| 2971 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2972 | /* Get the output length for the algorithm and key combination. None of the |
| 2973 | * currently supported algorithms have an output length dependent on actual |
| 2974 | * key size, so setting it to a bogus value is currently OK. */ |
| 2975 | operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2976 | |
| 2977 | if( operation->mac_size < 4 ) |
| 2978 | { |
| 2979 | /* A very short MAC is too short for security since it can be |
| 2980 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2981 | * so we make this our minimum, even though 32 bits is still |
| 2982 | * too small for security. */ |
| 2983 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2984 | goto exit; |
| 2985 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2986 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2987 | if( operation->mac_size > |
| 2988 | PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
| 2989 | { |
| 2990 | /* It's impossible to "truncate" to a larger length than the full length |
| 2991 | * of the algorithm. */ |
| 2992 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2993 | goto exit; |
| 2994 | } |
| 2995 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2996 | #if defined(MBEDTLS_CMAC_C) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2997 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2998 | { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2999 | status = psa_cmac_setup( operation, slot ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3000 | } |
| 3001 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3002 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3003 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3004 | if( PSA_ALG_IS_HMAC( alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3005 | { |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3006 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
Steven Cooreman | 1fb691a | 2021-03-08 12:01:55 +0100 | [diff] [blame] | 3007 | if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3008 | { |
| 3009 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3010 | goto exit; |
| 3011 | } |
| 3012 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3013 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3014 | { |
| 3015 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3016 | goto exit; |
| 3017 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3018 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3019 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3020 | slot->key.data, |
| 3021 | slot->key.bytes, |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3022 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3023 | } |
| 3024 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3025 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3026 | { |
| 3027 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3028 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3029 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3030 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3031 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3032 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3033 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3034 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3035 | else |
| 3036 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3037 | operation->key_set = 1; |
| 3038 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3039 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3040 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3041 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3042 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3043 | } |
| 3044 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3045 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3046 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3047 | psa_algorithm_t alg ) |
| 3048 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3049 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3053 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3054 | psa_algorithm_t alg ) |
| 3055 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3056 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3057 | } |
| 3058 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3059 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3060 | const uint8_t *input, |
| 3061 | size_t input_length ) |
| 3062 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3063 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3064 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3065 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3066 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3067 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3068 | operation->has_input = 1; |
| 3069 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3070 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3071 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3072 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3073 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3074 | input, input_length ); |
| 3075 | status = mbedtls_to_psa_error( ret ); |
| 3076 | } |
| 3077 | else |
| 3078 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3079 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3080 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3081 | { |
| 3082 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3083 | input_length ); |
| 3084 | } |
| 3085 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3086 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3087 | { |
| 3088 | /* This shouldn't happen if `operation` was initialized by |
| 3089 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3090 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3091 | } |
| 3092 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3093 | if( status != PSA_SUCCESS ) |
| 3094 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3095 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3096 | } |
| 3097 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3098 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3099 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3100 | uint8_t *mac, |
| 3101 | size_t mac_size ) |
| 3102 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3103 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3104 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3105 | size_t hash_size = 0; |
| 3106 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3107 | psa_status_t status; |
| 3108 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3109 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3110 | if( status != PSA_SUCCESS ) |
| 3111 | return( status ); |
| 3112 | /* From here on, tmp needs to be wiped. */ |
| 3113 | |
| 3114 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3115 | if( status != PSA_SUCCESS ) |
| 3116 | goto exit; |
| 3117 | |
| 3118 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3119 | if( status != PSA_SUCCESS ) |
| 3120 | goto exit; |
| 3121 | |
| 3122 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3123 | if( status != PSA_SUCCESS ) |
| 3124 | goto exit; |
| 3125 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3126 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3127 | if( status != PSA_SUCCESS ) |
| 3128 | goto exit; |
| 3129 | |
| 3130 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3131 | |
| 3132 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3133 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3134 | return( status ); |
| 3135 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3136 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3137 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3138 | 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] | 3139 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3140 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3141 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3142 | if( ! operation->key_set ) |
| 3143 | return( PSA_ERROR_BAD_STATE ); |
| 3144 | if( operation->iv_required && ! operation->iv_set ) |
| 3145 | return( PSA_ERROR_BAD_STATE ); |
| 3146 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3147 | if( mac_size < operation->mac_size ) |
| 3148 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3149 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3150 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3151 | if( operation->alg == PSA_ALG_CMAC ) |
| 3152 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3153 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3154 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3155 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3156 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3157 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3158 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3159 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3160 | else |
| 3161 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3162 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3163 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3164 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3165 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3166 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3167 | } |
| 3168 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3169 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3170 | { |
| 3171 | /* This shouldn't happen if `operation` was initialized by |
| 3172 | * a setup function. */ |
| 3173 | return( PSA_ERROR_BAD_STATE ); |
| 3174 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3177 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3178 | uint8_t *mac, |
| 3179 | size_t mac_size, |
| 3180 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3181 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3182 | psa_status_t status; |
| 3183 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3184 | if( operation->alg == 0 ) |
| 3185 | { |
| 3186 | return( PSA_ERROR_BAD_STATE ); |
| 3187 | } |
| 3188 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3189 | /* Fill the output buffer with something that isn't a valid mac |
| 3190 | * (barring an attack on the mac and deliberately-crafted input), |
| 3191 | * in case the caller doesn't check the return status properly. */ |
| 3192 | *mac_length = mac_size; |
| 3193 | /* If mac_size is 0 then mac may be NULL and then the |
| 3194 | * call to memset would have undefined behavior. */ |
| 3195 | if( mac_size != 0 ) |
| 3196 | memset( mac, '!', mac_size ); |
| 3197 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3198 | if( ! operation->is_sign ) |
| 3199 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3200 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3201 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3202 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3203 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3204 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3205 | if( status == PSA_SUCCESS ) |
| 3206 | { |
| 3207 | status = psa_mac_abort( operation ); |
| 3208 | if( status == PSA_SUCCESS ) |
| 3209 | *mac_length = operation->mac_size; |
| 3210 | else |
| 3211 | memset( mac, '!', mac_size ); |
| 3212 | } |
| 3213 | else |
| 3214 | psa_mac_abort( operation ); |
| 3215 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3216 | } |
| 3217 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3218 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3219 | const uint8_t *mac, |
| 3220 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3221 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3222 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3223 | psa_status_t status; |
| 3224 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3225 | if( operation->alg == 0 ) |
| 3226 | { |
| 3227 | return( PSA_ERROR_BAD_STATE ); |
| 3228 | } |
| 3229 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3230 | if( operation->is_sign ) |
| 3231 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3232 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3233 | } |
| 3234 | if( operation->mac_size != mac_length ) |
| 3235 | { |
| 3236 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3237 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3238 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3239 | |
| 3240 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3241 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3242 | if( status != PSA_SUCCESS ) |
| 3243 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3244 | |
| 3245 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3246 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3247 | |
| 3248 | cleanup: |
| 3249 | if( status == PSA_SUCCESS ) |
| 3250 | status = psa_mac_abort( operation ); |
| 3251 | else |
| 3252 | psa_mac_abort( operation ); |
| 3253 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3254 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3255 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3256 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3260 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3261 | /****************************************************************/ |
| 3262 | /* Asymmetric cryptography */ |
| 3263 | /****************************************************************/ |
| 3264 | |
Ronald Cron | 67b1eb3 | 2020-12-08 17:37:27 +0100 | [diff] [blame] | 3265 | psa_status_t psa_sign_hash_internal( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3266 | const psa_key_attributes_t *attributes, |
| 3267 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3268 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3269 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3270 | { |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3271 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3272 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 3273 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3274 | { |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 3275 | return( mbedtls_psa_rsa_sign_hash( |
| 3276 | attributes, |
| 3277 | key_buffer, key_buffer_size, |
| 3278 | alg, hash, hash_length, |
| 3279 | signature, signature_size, signature_length ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3280 | } |
| 3281 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3282 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3283 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3284 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3285 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Ronald Cron | bb9cbc7 | 2021-03-04 17:09:00 +0100 | [diff] [blame] | 3286 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3287 | { |
Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 3288 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3289 | { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 3290 | return( mbedtls_psa_ecdsa_sign_hash( |
| 3291 | attributes, |
| 3292 | key_buffer, key_buffer_size, |
| 3293 | alg, hash, hash_length, |
| 3294 | signature, signature_size, signature_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3295 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3296 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3297 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3298 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3299 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3300 | } |
| 3301 | else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3302 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3303 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3304 | { |
Ronald Cron | bb9cbc7 | 2021-03-04 17:09:00 +0100 | [diff] [blame] | 3305 | (void)attributes; |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3306 | (void)key_buffer; |
| 3307 | (void)key_buffer_size; |
| 3308 | (void)alg; |
| 3309 | (void)hash; |
| 3310 | (void)hash_length; |
| 3311 | (void)signature; |
| 3312 | (void)signature_size; |
| 3313 | (void)signature_length; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3314 | |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3315 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3316 | } |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3317 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3318 | |
| 3319 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 3320 | psa_algorithm_t alg, |
| 3321 | const uint8_t *hash, |
| 3322 | size_t hash_length, |
| 3323 | uint8_t *signature, |
| 3324 | size_t signature_size, |
| 3325 | size_t *signature_length ) |
| 3326 | { |
| 3327 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3328 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3329 | psa_key_slot_t *slot; |
| 3330 | |
| 3331 | *signature_length = signature_size; |
| 3332 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3333 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3334 | * buffer can in principle be empty since it doesn't actually have |
| 3335 | * to be a hash.) */ |
| 3336 | if( signature_size == 0 ) |
| 3337 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3338 | |
| 3339 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3340 | PSA_KEY_USAGE_SIGN_HASH, |
| 3341 | alg ); |
| 3342 | if( status != PSA_SUCCESS ) |
| 3343 | goto exit; |
| 3344 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 3345 | { |
| 3346 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3347 | goto exit; |
| 3348 | } |
| 3349 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3350 | psa_key_attributes_t attributes = { |
| 3351 | .core = slot->attr |
| 3352 | }; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3353 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3354 | status = psa_driver_wrapper_sign_hash( |
| 3355 | &attributes, slot->key.data, slot->key.bytes, |
| 3356 | alg, hash, hash_length, |
| 3357 | signature, signature_size, signature_length ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3358 | |
| 3359 | exit: |
| 3360 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3361 | * the trailing part on success) with something that isn't a valid mac |
| 3362 | * (barring an attack on the mac and deliberately-crafted input), |
| 3363 | * in case the caller doesn't check the return status properly. */ |
| 3364 | if( status == PSA_SUCCESS ) |
| 3365 | memset( signature + *signature_length, '!', |
| 3366 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3367 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3368 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3369 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3370 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3371 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3372 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3373 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3374 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3375 | } |
| 3376 | |
Ronald Cron | 67b1eb3 | 2020-12-08 17:37:27 +0100 | [diff] [blame] | 3377 | psa_status_t psa_verify_hash_internal( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3378 | const psa_key_attributes_t *attributes, |
| 3379 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3380 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3381 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3382 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3383 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3384 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 3385 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3386 | { |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 3387 | return( mbedtls_psa_rsa_verify_hash( |
| 3388 | attributes, |
| 3389 | key_buffer, key_buffer_size, |
| 3390 | alg, hash, hash_length, |
| 3391 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3392 | } |
| 3393 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3394 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3395 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 3396 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3397 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3399 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3400 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3401 | { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 3402 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3403 | attributes, |
| 3404 | key_buffer, key_buffer_size, |
| 3405 | alg, hash, hash_length, |
| 3406 | signature, signature_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3407 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3408 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3409 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3410 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3411 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3412 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3413 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3414 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3415 | else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3416 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3417 | (void)key_buffer; |
| 3418 | (void)key_buffer_size; |
| 3419 | (void)alg; |
| 3420 | (void)hash; |
| 3421 | (void)hash_length; |
| 3422 | (void)signature; |
| 3423 | (void)signature_length; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3424 | |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3425 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3426 | } |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3427 | } |
| 3428 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3429 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3430 | psa_algorithm_t alg, |
| 3431 | const uint8_t *hash, |
| 3432 | size_t hash_length, |
| 3433 | const uint8_t *signature, |
| 3434 | size_t signature_length ) |
| 3435 | { |
| 3436 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3437 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3438 | psa_key_slot_t *slot; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3439 | |
| 3440 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3441 | PSA_KEY_USAGE_VERIFY_HASH, |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3442 | alg ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3443 | if( status != PSA_SUCCESS ) |
| 3444 | return( status ); |
| 3445 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3446 | psa_key_attributes_t attributes = { |
| 3447 | .core = slot->attr |
| 3448 | }; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3449 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3450 | status = psa_driver_wrapper_verify_hash( |
| 3451 | &attributes, slot->key.data, slot->key.bytes, |
| 3452 | alg, hash, hash_length, |
| 3453 | signature, signature_length ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3454 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3455 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3456 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3457 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3458 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3459 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3460 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3461 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3462 | mbedtls_rsa_context *rsa ) |
| 3463 | { |
| 3464 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3465 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3466 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3467 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3468 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3469 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3470 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3471 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3472 | psa_algorithm_t alg, |
| 3473 | const uint8_t *input, |
| 3474 | size_t input_length, |
| 3475 | const uint8_t *salt, |
| 3476 | size_t salt_length, |
| 3477 | uint8_t *output, |
| 3478 | size_t output_size, |
| 3479 | size_t *output_length ) |
| 3480 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3481 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3482 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3483 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3484 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3485 | (void) input; |
| 3486 | (void) input_length; |
| 3487 | (void) salt; |
| 3488 | (void) output; |
| 3489 | (void) output_size; |
| 3490 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3491 | *output_length = 0; |
| 3492 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3493 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3494 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3495 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3496 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3497 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3498 | if( status != PSA_SUCCESS ) |
| 3499 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3500 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3501 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3502 | { |
| 3503 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3504 | goto exit; |
| 3505 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3506 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3507 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3508 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3509 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3510 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3511 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3512 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3513 | slot->key.data, |
| 3514 | slot->key.bytes, |
| 3515 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3516 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3517 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3518 | |
| 3519 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3520 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3521 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3522 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3523 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3524 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3525 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
| 3526 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3527 | status = mbedtls_to_psa_error( |
| 3528 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3529 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3530 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3531 | MBEDTLS_RSA_PUBLIC, |
| 3532 | input_length, |
| 3533 | input, |
| 3534 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3535 | } |
| 3536 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3537 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3538 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3539 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
| 3540 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3541 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3542 | status = mbedtls_to_psa_error( |
| 3543 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3544 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3545 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3546 | MBEDTLS_RSA_PUBLIC, |
| 3547 | salt, salt_length, |
| 3548 | input_length, |
| 3549 | input, |
| 3550 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3551 | } |
| 3552 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3553 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3554 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3555 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3556 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3557 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3558 | rsa_exit: |
| 3559 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3560 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3561 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3562 | mbedtls_rsa_free( rsa ); |
| 3563 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3564 | } |
| 3565 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3566 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3567 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3568 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3569 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3570 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3571 | |
| 3572 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3573 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3574 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3575 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3576 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3577 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3578 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3579 | psa_algorithm_t alg, |
| 3580 | const uint8_t *input, |
| 3581 | size_t input_length, |
| 3582 | const uint8_t *salt, |
| 3583 | size_t salt_length, |
| 3584 | uint8_t *output, |
| 3585 | size_t output_size, |
| 3586 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3587 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3588 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3589 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3590 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3591 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3592 | (void) input; |
| 3593 | (void) input_length; |
| 3594 | (void) salt; |
| 3595 | (void) output; |
| 3596 | (void) output_size; |
| 3597 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3598 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3599 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3600 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3601 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3602 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3603 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3604 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3605 | if( status != PSA_SUCCESS ) |
| 3606 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3607 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3608 | { |
| 3609 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3610 | goto exit; |
| 3611 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3612 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3613 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3614 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3615 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3616 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3617 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3618 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3619 | slot->key.data, |
| 3620 | slot->key.bytes, |
| 3621 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3622 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3623 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3624 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3625 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3626 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3627 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3628 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3629 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3630 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3631 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3632 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3633 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3634 | status = mbedtls_to_psa_error( |
| 3635 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3636 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3637 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3638 | MBEDTLS_RSA_PRIVATE, |
| 3639 | output_length, |
| 3640 | input, |
| 3641 | output, |
| 3642 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3643 | } |
| 3644 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3645 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3646 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3647 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3648 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3649 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3650 | status = mbedtls_to_psa_error( |
| 3651 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3652 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3653 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3654 | MBEDTLS_RSA_PRIVATE, |
| 3655 | salt, salt_length, |
| 3656 | output_length, |
| 3657 | input, |
| 3658 | output, |
| 3659 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3660 | } |
| 3661 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3662 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3663 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3664 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3665 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3666 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3667 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3668 | mbedtls_rsa_free( rsa ); |
| 3669 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3670 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3671 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3672 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3673 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3674 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3675 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3676 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3677 | |
| 3678 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3679 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3680 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3681 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3682 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3683 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3684 | |
| 3685 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3686 | /****************************************************************/ |
| 3687 | /* Symmetric cryptography */ |
| 3688 | /****************************************************************/ |
| 3689 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3690 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3691 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3692 | psa_algorithm_t alg, |
| 3693 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3694 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3695 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3696 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3697 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3698 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3699 | size_t key_bits; |
| 3700 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3701 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3702 | PSA_KEY_USAGE_ENCRYPT : |
| 3703 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3704 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3705 | /* A context must be freshly initialized before it can be set up. */ |
| 3706 | if( operation->alg != 0 ) |
| 3707 | return( PSA_ERROR_BAD_STATE ); |
| 3708 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3709 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 3710 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3711 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3712 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3713 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3714 | 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] | 3715 | if( status != PSA_SUCCESS ) |
| 3716 | goto exit; |
| 3717 | |
| 3718 | /* Initialize the operation struct members, except for alg. The alg member |
| 3719 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 3720 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3721 | operation->key_set = 0; |
| 3722 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3723 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3724 | operation->iv_size = 0; |
| 3725 | operation->block_size = 0; |
| 3726 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3727 | operation->iv_required = 0; |
| 3728 | else |
| 3729 | operation->iv_required = 1; |
| 3730 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3731 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 3732 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 3733 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 3734 | slot, |
| 3735 | alg ); |
| 3736 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 3737 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 3738 | slot, |
| 3739 | alg ); |
| 3740 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3741 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 3742 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3743 | /* Once the driver context is initialised, it needs to be freed using |
| 3744 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 3745 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 3746 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3747 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3748 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3749 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 3750 | goto exit; |
| 3751 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3752 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3753 | * available for the given algorithm & key. */ |
| 3754 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3755 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3756 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3757 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 3758 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 3759 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3760 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3761 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3762 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3763 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3764 | 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] | 3765 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3766 | { |
| 3767 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3768 | goto exit; |
| 3769 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3770 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3771 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3772 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3773 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3774 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 3775 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3776 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3777 | { |
| 3778 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3779 | uint8_t keys[24]; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3780 | memcpy( keys, slot->key.data, 16 ); |
| 3781 | memcpy( keys + 16, slot->key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3782 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3783 | keys, |
| 3784 | 192, cipher_operation ); |
| 3785 | } |
| 3786 | else |
| 3787 | #endif |
| 3788 | { |
| 3789 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3790 | slot->key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3791 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3792 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3793 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3794 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3795 | |
David Brown | 63ca260 | 2021-01-26 11:51:12 -0700 | [diff] [blame] | 3796 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 3797 | defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3798 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3799 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3800 | case PSA_ALG_CBC_NO_PADDING: |
| 3801 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3802 | MBEDTLS_PADDING_NONE ); |
| 3803 | break; |
| 3804 | case PSA_ALG_CBC_PKCS7: |
| 3805 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3806 | MBEDTLS_PADDING_PKCS7 ); |
| 3807 | break; |
| 3808 | default: |
| 3809 | /* The algorithm doesn't involve padding. */ |
| 3810 | ret = 0; |
| 3811 | break; |
| 3812 | } |
| 3813 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3814 | goto exit; |
David Brown | 288a96e | 2021-02-09 16:27:55 -0700 | [diff] [blame] | 3815 | #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] | 3816 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3817 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3818 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3819 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 3820 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3821 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3822 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3823 | } |
David Brown | 1bfe4d7 | 2021-02-16 12:54:35 -0700 | [diff] [blame] | 3824 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3825 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 3826 | 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] | 3827 | operation->iv_size = 12; |
| 3828 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3829 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3830 | status = PSA_SUCCESS; |
| 3831 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3832 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3833 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3834 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3835 | if( status == PSA_SUCCESS ) |
| 3836 | { |
| 3837 | /* Update operation flags for both driver and software implementations */ |
| 3838 | operation->key_set = 1; |
| 3839 | } |
| 3840 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3841 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3842 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3843 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3844 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3845 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3846 | } |
| 3847 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3848 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3849 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3850 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3851 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3852 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3853 | } |
| 3854 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3855 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3856 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3857 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3858 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3859 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3862 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3863 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3864 | size_t iv_size, |
| 3865 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3866 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3867 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3868 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3869 | if( operation->iv_set || ! operation->iv_required ) |
| 3870 | { |
| 3871 | return( PSA_ERROR_BAD_STATE ); |
| 3872 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3873 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3874 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 3875 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 3876 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 3877 | iv, |
| 3878 | iv_size, |
| 3879 | iv_length ); |
| 3880 | goto exit; |
| 3881 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3882 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3883 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3884 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3885 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3886 | goto exit; |
| 3887 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3888 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3889 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3890 | if( ret != 0 ) |
| 3891 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3892 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3893 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3894 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3895 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3896 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3897 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3898 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3899 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3900 | if( status == PSA_SUCCESS ) |
| 3901 | operation->iv_set = 1; |
| 3902 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3903 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3904 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3907 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3908 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3909 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3910 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3911 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3912 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3913 | if( operation->iv_set || ! operation->iv_required ) |
| 3914 | { |
| 3915 | return( PSA_ERROR_BAD_STATE ); |
| 3916 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3917 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3918 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 3919 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 3920 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 3921 | iv, |
| 3922 | iv_length ); |
| 3923 | goto exit; |
| 3924 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3925 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3926 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3927 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3928 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3929 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3930 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3931 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3932 | status = mbedtls_to_psa_error( ret ); |
| 3933 | exit: |
| 3934 | if( status == PSA_SUCCESS ) |
| 3935 | operation->iv_set = 1; |
| 3936 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3937 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3938 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3939 | } |
| 3940 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3941 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 3942 | * manual processing, since the PSA API is defined as being able to process |
| 3943 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 3944 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 3945 | static psa_status_t psa_cipher_update_ecb_internal( |
| 3946 | mbedtls_cipher_context_t *ctx, |
| 3947 | const uint8_t *input, |
| 3948 | size_t input_length, |
| 3949 | uint8_t *output, |
| 3950 | size_t output_size, |
| 3951 | size_t *output_length ) |
| 3952 | { |
| 3953 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3954 | size_t block_size = ctx->cipher_info->block_size; |
| 3955 | size_t internal_output_length = 0; |
| 3956 | *output_length = 0; |
| 3957 | |
| 3958 | if( input_length == 0 ) |
| 3959 | { |
| 3960 | status = PSA_SUCCESS; |
| 3961 | goto exit; |
| 3962 | } |
| 3963 | |
| 3964 | if( ctx->unprocessed_len > 0 ) |
| 3965 | { |
| 3966 | /* Fill up to block size, and run the block if there's a full one. */ |
| 3967 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 3968 | |
| 3969 | if( input_length < bytes_to_copy ) |
| 3970 | bytes_to_copy = input_length; |
| 3971 | |
| 3972 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 3973 | input, bytes_to_copy ); |
| 3974 | input_length -= bytes_to_copy; |
| 3975 | input += bytes_to_copy; |
| 3976 | ctx->unprocessed_len += bytes_to_copy; |
| 3977 | |
| 3978 | if( ctx->unprocessed_len == block_size ) |
| 3979 | { |
| 3980 | status = mbedtls_to_psa_error( |
| 3981 | mbedtls_cipher_update( ctx, |
| 3982 | ctx->unprocessed_data, |
| 3983 | block_size, |
| 3984 | output, &internal_output_length ) ); |
| 3985 | |
| 3986 | if( status != PSA_SUCCESS ) |
| 3987 | goto exit; |
| 3988 | |
| 3989 | output += internal_output_length; |
| 3990 | output_size -= internal_output_length; |
| 3991 | *output_length += internal_output_length; |
| 3992 | ctx->unprocessed_len = 0; |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | while( input_length >= block_size ) |
| 3997 | { |
| 3998 | /* Run all full blocks we have, one by one */ |
| 3999 | status = mbedtls_to_psa_error( |
| 4000 | mbedtls_cipher_update( ctx, input, |
| 4001 | block_size, |
| 4002 | output, &internal_output_length ) ); |
| 4003 | |
| 4004 | if( status != PSA_SUCCESS ) |
| 4005 | goto exit; |
| 4006 | |
| 4007 | input_length -= block_size; |
| 4008 | input += block_size; |
| 4009 | |
| 4010 | output += internal_output_length; |
| 4011 | output_size -= internal_output_length; |
| 4012 | *output_length += internal_output_length; |
| 4013 | } |
| 4014 | |
| 4015 | if( input_length > 0 ) |
| 4016 | { |
| 4017 | /* Save unprocessed bytes for later processing */ |
| 4018 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4019 | input, input_length ); |
| 4020 | ctx->unprocessed_len += input_length; |
| 4021 | } |
| 4022 | |
| 4023 | status = PSA_SUCCESS; |
| 4024 | |
| 4025 | exit: |
| 4026 | return( status ); |
| 4027 | } |
| 4028 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4029 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4030 | const uint8_t *input, |
| 4031 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4032 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4033 | size_t output_size, |
| 4034 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4035 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4036 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4037 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4038 | if( operation->alg == 0 ) |
| 4039 | { |
| 4040 | return( PSA_ERROR_BAD_STATE ); |
| 4041 | } |
| 4042 | if( operation->iv_required && ! operation->iv_set ) |
| 4043 | { |
| 4044 | return( PSA_ERROR_BAD_STATE ); |
| 4045 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4046 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4047 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4048 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4049 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4050 | input, |
| 4051 | input_length, |
| 4052 | output, |
| 4053 | output_size, |
| 4054 | output_length ); |
| 4055 | goto exit; |
| 4056 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4057 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4058 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4059 | { |
| 4060 | /* Take the unprocessed partial block left over from previous |
| 4061 | * update calls, if any, plus the input to this call. Remove |
| 4062 | * the last partial block, if any. You get the data that will be |
| 4063 | * output in this call. */ |
| 4064 | expected_output_size = |
| 4065 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4066 | / operation->block_size * operation->block_size; |
| 4067 | } |
| 4068 | else |
| 4069 | { |
| 4070 | expected_output_size = input_length; |
| 4071 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4072 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4073 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4074 | { |
| 4075 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4076 | goto exit; |
| 4077 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4078 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4079 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4080 | { |
| 4081 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4082 | * process a single block at a time in ECB mode. Abstract away that |
| 4083 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4084 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4085 | input, |
| 4086 | input_length, |
| 4087 | output, |
| 4088 | output_size, |
| 4089 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4090 | } |
| 4091 | else |
| 4092 | { |
| 4093 | status = mbedtls_to_psa_error( |
| 4094 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4095 | input_length, output, output_length ) ); |
| 4096 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4097 | exit: |
| 4098 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4099 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4100 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4101 | } |
| 4102 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4103 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4104 | uint8_t *output, |
| 4105 | size_t output_size, |
| 4106 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4107 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4108 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4109 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4110 | if( operation->alg == 0 ) |
| 4111 | { |
| 4112 | return( PSA_ERROR_BAD_STATE ); |
| 4113 | } |
| 4114 | if( operation->iv_required && ! operation->iv_set ) |
| 4115 | { |
| 4116 | return( PSA_ERROR_BAD_STATE ); |
| 4117 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4118 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4119 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4120 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4121 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4122 | output, |
| 4123 | output_size, |
| 4124 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4125 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4126 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4127 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4128 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4129 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4130 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4131 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4132 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4133 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4134 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4135 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4136 | } |
| 4137 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4138 | status = mbedtls_to_psa_error( |
| 4139 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4140 | temp_output_buffer, |
| 4141 | output_length ) ); |
| 4142 | if( status != PSA_SUCCESS ) |
| 4143 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4144 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4145 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4146 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4147 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4148 | memcpy( output, temp_output_buffer, *output_length ); |
| 4149 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4150 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4151 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4152 | exit: |
| 4153 | if( operation->mbedtls_in_use == 1 ) |
| 4154 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4155 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4156 | if( status == PSA_SUCCESS ) |
| 4157 | return( psa_cipher_abort( operation ) ); |
| 4158 | else |
| 4159 | { |
| 4160 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4161 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4162 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4163 | return( status ); |
| 4164 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4165 | } |
| 4166 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4167 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4168 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4169 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4170 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4171 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4172 | * in use. It's ok to call abort on such an object, and there's |
| 4173 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4174 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4175 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4176 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4177 | /* Sanity check (shouldn't happen: operation->alg should |
| 4178 | * always have been initialized to a valid value). */ |
| 4179 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4180 | return( PSA_ERROR_BAD_STATE ); |
| 4181 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4182 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4183 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4184 | else |
| 4185 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4186 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4187 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4188 | operation->key_set = 0; |
| 4189 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4190 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4191 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4192 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4193 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4194 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4195 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4196 | } |
| 4197 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4198 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4199 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4200 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4201 | /****************************************************************/ |
| 4202 | /* AEAD */ |
| 4203 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4204 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4205 | typedef struct |
| 4206 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4207 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4208 | const mbedtls_cipher_info_t *cipher_info; |
| 4209 | union |
| 4210 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4211 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4212 | #if defined(MBEDTLS_CCM_C) |
| 4213 | mbedtls_ccm_context ccm; |
| 4214 | #endif /* MBEDTLS_CCM_C */ |
| 4215 | #if defined(MBEDTLS_GCM_C) |
| 4216 | mbedtls_gcm_context gcm; |
| 4217 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4218 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4219 | mbedtls_chachapoly_context chachapoly; |
| 4220 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4221 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4222 | psa_algorithm_t core_alg; |
| 4223 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4224 | uint8_t tag_length; |
| 4225 | } aead_operation_t; |
| 4226 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4227 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4228 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4229 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4230 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4231 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4232 | { |
| 4233 | #if defined(MBEDTLS_CCM_C) |
| 4234 | case PSA_ALG_CCM: |
| 4235 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4236 | break; |
| 4237 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4238 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4239 | case PSA_ALG_GCM: |
| 4240 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4241 | break; |
| 4242 | #endif /* MBEDTLS_GCM_C */ |
| 4243 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4244 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4245 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4249 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4250 | psa_key_usage_t usage, |
| 4251 | psa_algorithm_t alg ) |
| 4252 | { |
| 4253 | psa_status_t status; |
| 4254 | size_t key_bits; |
| 4255 | mbedtls_cipher_id_t cipher_id; |
| 4256 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4257 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4258 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4259 | if( status != PSA_SUCCESS ) |
| 4260 | return( status ); |
| 4261 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4262 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4263 | |
| 4264 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4265 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4266 | &cipher_id ); |
| 4267 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4268 | { |
| 4269 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4270 | goto cleanup; |
| 4271 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4272 | |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4273 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4274 | { |
| 4275 | #if defined(MBEDTLS_CCM_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4276 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4277 | operation->core_alg = PSA_ALG_CCM; |
| 4278 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4279 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4280 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4281 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4282 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4283 | { |
| 4284 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4285 | goto cleanup; |
| 4286 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4287 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4288 | status = mbedtls_to_psa_error( |
| 4289 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4290 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4291 | (unsigned int) key_bits ) ); |
| 4292 | if( status != 0 ) |
| 4293 | goto cleanup; |
| 4294 | break; |
| 4295 | #endif /* MBEDTLS_CCM_C */ |
| 4296 | |
| 4297 | #if defined(MBEDTLS_GCM_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4298 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4299 | operation->core_alg = PSA_ALG_GCM; |
| 4300 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4301 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4302 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4303 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4304 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4305 | { |
| 4306 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4307 | goto cleanup; |
| 4308 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4309 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4310 | status = mbedtls_to_psa_error( |
| 4311 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4312 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4313 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4314 | if( status != 0 ) |
| 4315 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4316 | break; |
| 4317 | #endif /* MBEDTLS_GCM_C */ |
| 4318 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4319 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4320 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4321 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4322 | operation->full_tag_length = 16; |
| 4323 | /* We only support the default tag length. */ |
| 4324 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4325 | { |
| 4326 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4327 | goto cleanup; |
| 4328 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4329 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4330 | status = mbedtls_to_psa_error( |
| 4331 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4332 | operation->slot->key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4333 | if( status != 0 ) |
| 4334 | goto cleanup; |
| 4335 | break; |
| 4336 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4337 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4338 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4339 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4340 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4341 | } |
| 4342 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4343 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4344 | { |
| 4345 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4346 | goto cleanup; |
| 4347 | } |
| 4348 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4349 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4350 | return( PSA_SUCCESS ); |
| 4351 | |
| 4352 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4353 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4354 | return( status ); |
| 4355 | } |
| 4356 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4357 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4358 | psa_algorithm_t alg, |
| 4359 | const uint8_t *nonce, |
| 4360 | size_t nonce_length, |
| 4361 | const uint8_t *additional_data, |
| 4362 | size_t additional_data_length, |
| 4363 | const uint8_t *plaintext, |
| 4364 | size_t plaintext_length, |
| 4365 | uint8_t *ciphertext, |
| 4366 | size_t ciphertext_size, |
| 4367 | size_t *ciphertext_length ) |
| 4368 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4369 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4370 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4371 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4372 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4373 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4374 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4375 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4376 | if( status != PSA_SUCCESS ) |
| 4377 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4378 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4379 | /* For all currently supported modes, the tag is at the end of the |
| 4380 | * ciphertext. */ |
| 4381 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4382 | { |
| 4383 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4384 | goto exit; |
| 4385 | } |
| 4386 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4387 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4388 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4389 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4390 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4391 | status = mbedtls_to_psa_error( |
| 4392 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4393 | MBEDTLS_GCM_ENCRYPT, |
| 4394 | plaintext_length, |
| 4395 | nonce, nonce_length, |
| 4396 | additional_data, additional_data_length, |
| 4397 | plaintext, ciphertext, |
| 4398 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4399 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4400 | else |
| 4401 | #endif /* MBEDTLS_GCM_C */ |
| 4402 | #if defined(MBEDTLS_CCM_C) |
| 4403 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4404 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4405 | status = mbedtls_to_psa_error( |
| 4406 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4407 | plaintext_length, |
| 4408 | nonce, nonce_length, |
| 4409 | additional_data, |
| 4410 | additional_data_length, |
| 4411 | plaintext, ciphertext, |
| 4412 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4413 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4414 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4415 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4416 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4417 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4418 | { |
| 4419 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4420 | { |
| 4421 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4422 | goto exit; |
| 4423 | } |
| 4424 | status = mbedtls_to_psa_error( |
| 4425 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4426 | plaintext_length, |
| 4427 | nonce, |
| 4428 | additional_data, |
| 4429 | additional_data_length, |
| 4430 | plaintext, |
| 4431 | ciphertext, |
| 4432 | tag ) ); |
| 4433 | } |
| 4434 | else |
| 4435 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4436 | { |
Steven Cooreman | 7196fef | 2021-02-10 17:13:28 +0100 | [diff] [blame] | 4437 | (void) tag; |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4438 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4439 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4440 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4441 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4442 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4443 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4444 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4445 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4446 | if( status == PSA_SUCCESS ) |
| 4447 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4448 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4449 | } |
| 4450 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4451 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4452 | * followed by the tag. Return the length of the part preceding the tag in |
| 4453 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4454 | * the encrypted data has the same size as the plaintext, such as |
| 4455 | * CCM and GCM. */ |
| 4456 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4457 | const uint8_t *ciphertext, |
| 4458 | size_t ciphertext_length, |
| 4459 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4460 | const uint8_t **p_tag ) |
| 4461 | { |
| 4462 | size_t payload_length; |
| 4463 | if( tag_length > ciphertext_length ) |
| 4464 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4465 | payload_length = ciphertext_length - tag_length; |
| 4466 | if( payload_length > plaintext_size ) |
| 4467 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4468 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4469 | return( PSA_SUCCESS ); |
| 4470 | } |
| 4471 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4472 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4473 | psa_algorithm_t alg, |
| 4474 | const uint8_t *nonce, |
| 4475 | size_t nonce_length, |
| 4476 | const uint8_t *additional_data, |
| 4477 | size_t additional_data_length, |
| 4478 | const uint8_t *ciphertext, |
| 4479 | size_t ciphertext_length, |
| 4480 | uint8_t *plaintext, |
| 4481 | size_t plaintext_size, |
| 4482 | size_t *plaintext_length ) |
| 4483 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4484 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4485 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4486 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4487 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4488 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4489 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4490 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4491 | if( status != PSA_SUCCESS ) |
| 4492 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4493 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4494 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4495 | ciphertext, ciphertext_length, |
| 4496 | plaintext_size, &tag ); |
| 4497 | if( status != PSA_SUCCESS ) |
| 4498 | goto exit; |
| 4499 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4500 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4501 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4502 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4503 | status = mbedtls_to_psa_error( |
| 4504 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4505 | ciphertext_length - operation.tag_length, |
| 4506 | nonce, nonce_length, |
| 4507 | additional_data, |
| 4508 | additional_data_length, |
| 4509 | tag, operation.tag_length, |
| 4510 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4511 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4512 | else |
| 4513 | #endif /* MBEDTLS_GCM_C */ |
| 4514 | #if defined(MBEDTLS_CCM_C) |
| 4515 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4516 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4517 | status = mbedtls_to_psa_error( |
| 4518 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4519 | ciphertext_length - operation.tag_length, |
| 4520 | nonce, nonce_length, |
| 4521 | additional_data, |
| 4522 | additional_data_length, |
| 4523 | ciphertext, plaintext, |
| 4524 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4525 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4526 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4527 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4528 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4529 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4530 | { |
| 4531 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4532 | { |
| 4533 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4534 | goto exit; |
| 4535 | } |
| 4536 | status = mbedtls_to_psa_error( |
| 4537 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4538 | ciphertext_length - operation.tag_length, |
| 4539 | nonce, |
| 4540 | additional_data, |
| 4541 | additional_data_length, |
| 4542 | tag, |
| 4543 | ciphertext, |
| 4544 | plaintext ) ); |
| 4545 | } |
| 4546 | else |
| 4547 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4548 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4549 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4550 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4551 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4552 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4553 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4554 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4555 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4556 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4557 | if( status == PSA_SUCCESS ) |
| 4558 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4559 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4560 | } |
| 4561 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4562 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4563 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4564 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4565 | /* Generators */ |
| 4566 | /****************************************************************/ |
| 4567 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4568 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4569 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4570 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4571 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 4572 | #endif |
| 4573 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4574 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4575 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4576 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4577 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4578 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4579 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4580 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4581 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4582 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4583 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4584 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4585 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4586 | } |
| 4587 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4588 | 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] | 4589 | { |
| 4590 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4591 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4592 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4593 | { |
| 4594 | /* The object has (apparently) been initialized but it is not |
| 4595 | * in use. It's ok to call abort on such an object, and there's |
| 4596 | * nothing to do. */ |
| 4597 | } |
| 4598 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4599 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4600 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4601 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4602 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4603 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4604 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4605 | else |
| 4606 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4607 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4608 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4609 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4610 | /* 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] | 4611 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4612 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4613 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4614 | { |
| 4615 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4616 | operation->ctx.tls12_prf.seed_length ); |
| 4617 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4618 | } |
| 4619 | |
| 4620 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4621 | { |
| 4622 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4623 | operation->ctx.tls12_prf.label_length ); |
| 4624 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4625 | } |
| 4626 | |
| 4627 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4628 | |
| 4629 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4630 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4631 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4632 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4633 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4634 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4635 | { |
| 4636 | status = PSA_ERROR_BAD_STATE; |
| 4637 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4638 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4639 | return( status ); |
| 4640 | } |
| 4641 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4642 | 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] | 4643 | size_t *capacity) |
| 4644 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4645 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4646 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4647 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4648 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4651 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4652 | return( PSA_SUCCESS ); |
| 4653 | } |
| 4654 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | 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] | 4656 | size_t capacity ) |
| 4657 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4658 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4659 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4660 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4661 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4663 | return( PSA_SUCCESS ); |
| 4664 | } |
| 4665 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4666 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4667 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4668 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4669 | 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] | 4670 | psa_algorithm_t hash_alg, |
| 4671 | uint8_t *output, |
| 4672 | size_t output_length ) |
| 4673 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4674 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4675 | psa_status_t status; |
| 4676 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4677 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4678 | return( PSA_ERROR_BAD_STATE ); |
| 4679 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4680 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4681 | while( output_length != 0 ) |
| 4682 | { |
| 4683 | /* Copy what remains of the current block */ |
| 4684 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4685 | if( n > output_length ) |
| 4686 | n = (uint8_t) output_length; |
| 4687 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4688 | output += n; |
| 4689 | output_length -= n; |
| 4690 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4691 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4692 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4693 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4694 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4695 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4696 | * object was corrupted or if this function is called directly |
| 4697 | * inside the library. */ |
| 4698 | if( hkdf->block_number == 0xff ) |
| 4699 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4700 | |
| 4701 | /* We need a new block */ |
| 4702 | ++hkdf->block_number; |
| 4703 | hkdf->offset_in_block = 0; |
| 4704 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4705 | hkdf->prk, hash_length, |
| 4706 | hash_alg ); |
| 4707 | if( status != PSA_SUCCESS ) |
| 4708 | return( status ); |
| 4709 | if( hkdf->block_number != 1 ) |
| 4710 | { |
| 4711 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4712 | hkdf->output_block, |
| 4713 | hash_length ); |
| 4714 | if( status != PSA_SUCCESS ) |
| 4715 | return( status ); |
| 4716 | } |
| 4717 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4718 | hkdf->info, |
| 4719 | hkdf->info_length ); |
| 4720 | if( status != PSA_SUCCESS ) |
| 4721 | return( status ); |
| 4722 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4723 | &hkdf->block_number, 1 ); |
| 4724 | if( status != PSA_SUCCESS ) |
| 4725 | return( status ); |
| 4726 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4727 | hkdf->output_block, |
| 4728 | sizeof( hkdf->output_block ) ); |
| 4729 | if( status != PSA_SUCCESS ) |
| 4730 | return( status ); |
| 4731 | } |
| 4732 | |
| 4733 | return( PSA_SUCCESS ); |
| 4734 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4735 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4736 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4737 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4738 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4739 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4740 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4741 | psa_algorithm_t alg ) |
| 4742 | { |
| 4743 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4744 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4745 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4746 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4747 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4748 | /* We can't be wanting more output after block 0xff, otherwise |
| 4749 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4750 | * prevented this call. It could happen only if the operation |
| 4751 | * object was corrupted or if this function is called directly |
| 4752 | * inside the library. */ |
| 4753 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4754 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4755 | |
| 4756 | /* We need a new block */ |
| 4757 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4758 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4759 | |
| 4760 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4761 | * |
| 4762 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4763 | * |
| 4764 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4765 | * HMAC_hash(secret, A(2) + seed) + |
| 4766 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4767 | * |
| 4768 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4769 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4770 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4771 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4772 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4773 | * is currently extracted as `output_block` and where i is |
| 4774 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4775 | */ |
| 4776 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4777 | /* Save the hash context before using it, to preserve the hash state with |
| 4778 | * only the inner padding in it. We need this, because inner padding depends |
| 4779 | * on the key (secret in the RFC's terminology). */ |
| 4780 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4781 | if( status != PSA_SUCCESS ) |
| 4782 | goto cleanup; |
| 4783 | |
| 4784 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4785 | if( tls12_prf->block_number == 1 ) |
| 4786 | { |
| 4787 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4788 | * the variable seed and in this instance means it in the context of the |
| 4789 | * P_hash function, where seed = label + seed.) */ |
| 4790 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4791 | tls12_prf->label, tls12_prf->label_length ); |
| 4792 | if( status != PSA_SUCCESS ) |
| 4793 | goto cleanup; |
| 4794 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4795 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4796 | if( status != PSA_SUCCESS ) |
| 4797 | goto cleanup; |
| 4798 | } |
| 4799 | else |
| 4800 | { |
| 4801 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4802 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4803 | tls12_prf->Ai, hash_length ); |
| 4804 | if( status != PSA_SUCCESS ) |
| 4805 | goto cleanup; |
| 4806 | } |
| 4807 | |
| 4808 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4809 | tls12_prf->Ai, hash_length ); |
| 4810 | if( status != PSA_SUCCESS ) |
| 4811 | goto cleanup; |
| 4812 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4813 | if( status != PSA_SUCCESS ) |
| 4814 | goto cleanup; |
| 4815 | |
| 4816 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4817 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4818 | tls12_prf->Ai, hash_length ); |
| 4819 | if( status != PSA_SUCCESS ) |
| 4820 | goto cleanup; |
| 4821 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4822 | tls12_prf->label, tls12_prf->label_length ); |
| 4823 | if( status != PSA_SUCCESS ) |
| 4824 | goto cleanup; |
| 4825 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4826 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4827 | if( status != PSA_SUCCESS ) |
| 4828 | goto cleanup; |
| 4829 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4830 | tls12_prf->output_block, hash_length ); |
| 4831 | if( status != PSA_SUCCESS ) |
| 4832 | goto cleanup; |
| 4833 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4834 | if( status != PSA_SUCCESS ) |
| 4835 | goto cleanup; |
| 4836 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4837 | |
| 4838 | cleanup: |
| 4839 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4840 | cleanup_status = psa_hash_abort( &backup ); |
| 4841 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4842 | status = cleanup_status; |
| 4843 | |
| 4844 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4845 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4846 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4847 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4848 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4849 | psa_algorithm_t alg, |
| 4850 | uint8_t *output, |
| 4851 | size_t output_length ) |
| 4852 | { |
| 4853 | 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] | 4854 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4855 | psa_status_t status; |
| 4856 | uint8_t offset, length; |
| 4857 | |
| 4858 | while( output_length != 0 ) |
| 4859 | { |
| 4860 | /* Check if we have fully processed the current block. */ |
| 4861 | if( tls12_prf->left_in_block == 0 ) |
| 4862 | { |
| 4863 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4864 | alg ); |
| 4865 | if( status != PSA_SUCCESS ) |
| 4866 | return( status ); |
| 4867 | |
| 4868 | continue; |
| 4869 | } |
| 4870 | |
| 4871 | if( tls12_prf->left_in_block > output_length ) |
| 4872 | length = (uint8_t) output_length; |
| 4873 | else |
| 4874 | length = tls12_prf->left_in_block; |
| 4875 | |
| 4876 | offset = hash_length - tls12_prf->left_in_block; |
| 4877 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4878 | output += length; |
| 4879 | output_length -= length; |
| 4880 | tls12_prf->left_in_block -= length; |
| 4881 | } |
| 4882 | |
| 4883 | return( PSA_SUCCESS ); |
| 4884 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4885 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4886 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4887 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4888 | psa_status_t psa_key_derivation_output_bytes( |
| 4889 | psa_key_derivation_operation_t *operation, |
| 4890 | uint8_t *output, |
| 4891 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4892 | { |
| 4893 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4894 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4895 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4896 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4897 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4898 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4899 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4902 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4903 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4904 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4905 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4906 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4907 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4908 | goto exit; |
| 4909 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4910 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4911 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4912 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4913 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4914 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4915 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4917 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4918 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4919 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4920 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4921 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4922 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4923 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4924 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4925 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4926 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4927 | output, output_length ); |
| 4928 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4929 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4930 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4931 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4932 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4933 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4934 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4935 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4936 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4937 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4938 | output_length ); |
| 4939 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4940 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4941 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4942 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4943 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4944 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4945 | return( PSA_ERROR_BAD_STATE ); |
| 4946 | } |
| 4947 | |
| 4948 | exit: |
| 4949 | if( status != PSA_SUCCESS ) |
| 4950 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4951 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4952 | * This allows us to differentiate between exhausted operations and |
| 4953 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4954 | * operations. */ |
| 4955 | psa_algorithm_t alg = operation->alg; |
| 4956 | psa_key_derivation_abort( operation ); |
| 4957 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4958 | memset( output, '!', output_length ); |
| 4959 | } |
| 4960 | return( status ); |
| 4961 | } |
| 4962 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4963 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4964 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4965 | { |
| 4966 | if( data_size >= 8 ) |
| 4967 | mbedtls_des_key_set_parity( data ); |
| 4968 | if( data_size >= 16 ) |
| 4969 | mbedtls_des_key_set_parity( data + 8 ); |
| 4970 | if( data_size >= 24 ) |
| 4971 | mbedtls_des_key_set_parity( data + 16 ); |
| 4972 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4973 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4974 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4975 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4976 | psa_key_slot_t *slot, |
| 4977 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4979 | { |
| 4980 | uint8_t *data = NULL; |
| 4981 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4982 | psa_status_t status; |
| 4983 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4984 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4985 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4986 | if( bits % 8 != 0 ) |
| 4987 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4988 | data = mbedtls_calloc( 1, bytes ); |
| 4989 | if( data == NULL ) |
| 4990 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4991 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4992 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4993 | if( status != PSA_SUCCESS ) |
| 4994 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4995 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4996 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4997 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4998 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4999 | |
| 5000 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5001 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 5002 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5003 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5004 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5005 | psa_key_attributes_t attributes = { |
| 5006 | .core = slot->attr |
| 5007 | }; |
| 5008 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5009 | status = psa_driver_wrapper_import_key( &attributes, |
| 5010 | data, bytes, |
| 5011 | slot->key.data, |
| 5012 | slot->key.bytes, |
| 5013 | &slot->key.bytes, &bits ); |
| 5014 | if( bits != slot->attr.bits ) |
| 5015 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5016 | |
| 5017 | exit: |
| 5018 | mbedtls_free( data ); |
| 5019 | return( status ); |
| 5020 | } |
| 5021 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5022 | 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] | 5023 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5024 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5025 | { |
| 5026 | psa_status_t status; |
| 5027 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5028 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5029 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5030 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5031 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5032 | /* Reject any attempt to create a zero-length key so that we don't |
| 5033 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5034 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5035 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5036 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5037 | if( ! operation->can_output_key ) |
| 5038 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5039 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5040 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5041 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5042 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5043 | if( driver != NULL ) |
| 5044 | { |
| 5045 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5046 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5047 | } |
| 5048 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5049 | if( status == PSA_SUCCESS ) |
| 5050 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5051 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5052 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5053 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5054 | } |
| 5055 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5056 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5057 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5058 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5059 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5060 | return( status ); |
| 5061 | } |
| 5062 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5063 | |
| 5064 | |
| 5065 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5066 | /* Key derivation */ |
| 5067 | /****************************************************************/ |
| 5068 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5069 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5070 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5071 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5072 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5073 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5074 | int is_kdf_alg_supported; |
| 5075 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5076 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5077 | * initialisers for this union leave some bytes unspecified.) */ |
| 5078 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5079 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5080 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5081 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5082 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5083 | is_kdf_alg_supported = 1; |
| 5084 | else |
| 5085 | #endif |
| 5086 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5087 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5088 | is_kdf_alg_supported = 1; |
| 5089 | else |
| 5090 | #endif |
| 5091 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5092 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5093 | is_kdf_alg_supported = 1; |
| 5094 | else |
| 5095 | #endif |
| 5096 | is_kdf_alg_supported = 0; |
| 5097 | |
| 5098 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5099 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5100 | 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] | 5101 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5102 | if( hash_size == 0 ) |
| 5103 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5104 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5105 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5106 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5107 | { |
| 5108 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5109 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5110 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5111 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5112 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5113 | |
| 5114 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5115 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5116 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5117 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5118 | 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] | 5119 | psa_algorithm_t alg ) |
| 5120 | { |
| 5121 | psa_status_t status; |
| 5122 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5123 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5124 | return( PSA_ERROR_BAD_STATE ); |
| 5125 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5126 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5127 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5128 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5129 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5130 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5131 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5132 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5133 | #else |
| 5134 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5135 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5136 | } |
| 5137 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5138 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5139 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5140 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5141 | #else |
| 5142 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5143 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5144 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5145 | else |
| 5146 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5147 | |
| 5148 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5149 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5150 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5151 | } |
| 5152 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5153 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5154 | 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] | 5155 | psa_algorithm_t hash_alg, |
| 5156 | psa_key_derivation_step_t step, |
| 5157 | const uint8_t *data, |
| 5158 | size_t data_length ) |
| 5159 | { |
| 5160 | psa_status_t status; |
| 5161 | switch( step ) |
| 5162 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5163 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5164 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5165 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5166 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5167 | data, data_length, |
| 5168 | hash_alg ); |
| 5169 | if( status != PSA_SUCCESS ) |
| 5170 | return( status ); |
| 5171 | hkdf->state = HKDF_STATE_STARTED; |
| 5172 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5173 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5174 | /* If no salt was provided, use an empty salt. */ |
| 5175 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5176 | { |
| 5177 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5178 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5179 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5180 | if( status != PSA_SUCCESS ) |
| 5181 | return( status ); |
| 5182 | hkdf->state = HKDF_STATE_STARTED; |
| 5183 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5184 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5185 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5186 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5187 | data, data_length ); |
| 5188 | if( status != PSA_SUCCESS ) |
| 5189 | return( status ); |
| 5190 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5191 | hkdf->prk, |
| 5192 | sizeof( hkdf->prk ) ); |
| 5193 | if( status != PSA_SUCCESS ) |
| 5194 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5195 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5196 | hkdf->block_number = 0; |
| 5197 | hkdf->state = HKDF_STATE_KEYED; |
| 5198 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5199 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5200 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5201 | return( PSA_ERROR_BAD_STATE ); |
| 5202 | if( hkdf->info_set ) |
| 5203 | return( PSA_ERROR_BAD_STATE ); |
| 5204 | hkdf->info_length = data_length; |
| 5205 | if( data_length != 0 ) |
| 5206 | { |
| 5207 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5208 | if( hkdf->info == NULL ) |
| 5209 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5210 | memcpy( hkdf->info, data, data_length ); |
| 5211 | } |
| 5212 | hkdf->info_set = 1; |
| 5213 | return( PSA_SUCCESS ); |
| 5214 | default: |
| 5215 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5216 | } |
| 5217 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5218 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5219 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5220 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5221 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5222 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5223 | const uint8_t *data, |
| 5224 | size_t data_length ) |
| 5225 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5226 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5227 | return( PSA_ERROR_BAD_STATE ); |
| 5228 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5229 | if( data_length != 0 ) |
| 5230 | { |
| 5231 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5232 | if( prf->seed == NULL ) |
| 5233 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5234 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5235 | memcpy( prf->seed, data, data_length ); |
| 5236 | prf->seed_length = data_length; |
| 5237 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5238 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5239 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5240 | |
| 5241 | return( PSA_SUCCESS ); |
| 5242 | } |
| 5243 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5244 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5245 | psa_algorithm_t hash_alg, |
| 5246 | const uint8_t *data, |
| 5247 | size_t data_length ) |
| 5248 | { |
| 5249 | psa_status_t status; |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5250 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5251 | return( PSA_ERROR_BAD_STATE ); |
| 5252 | |
| 5253 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5254 | if( status != PSA_SUCCESS ) |
| 5255 | return( status ); |
| 5256 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5257 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5258 | |
| 5259 | return( PSA_SUCCESS ); |
| 5260 | } |
| 5261 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5262 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5263 | const uint8_t *data, |
| 5264 | size_t data_length ) |
| 5265 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5266 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5267 | return( PSA_ERROR_BAD_STATE ); |
| 5268 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5269 | if( data_length != 0 ) |
| 5270 | { |
| 5271 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5272 | if( prf->label == NULL ) |
| 5273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5274 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5275 | memcpy( prf->label, data, data_length ); |
| 5276 | prf->label_length = data_length; |
| 5277 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5278 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5279 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5280 | |
| 5281 | return( PSA_SUCCESS ); |
| 5282 | } |
| 5283 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5284 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5285 | psa_algorithm_t hash_alg, |
| 5286 | psa_key_derivation_step_t step, |
| 5287 | const uint8_t *data, |
| 5288 | size_t data_length ) |
| 5289 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5290 | switch( step ) |
| 5291 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5292 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5293 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5294 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5295 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5296 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5297 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5298 | default: |
| 5299 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5300 | } |
| 5301 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5302 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5303 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5304 | |
| 5305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5306 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5307 | psa_tls12_prf_key_derivation_t *prf, |
| 5308 | psa_algorithm_t hash_alg, |
| 5309 | const uint8_t *data, |
| 5310 | size_t data_length ) |
| 5311 | { |
| 5312 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5313 | 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] | 5314 | uint8_t *cur = pms; |
| 5315 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5316 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5317 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5318 | |
| 5319 | /* Quoting RFC 4279, Section 2: |
| 5320 | * |
| 5321 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5322 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5323 | * uint16 with the value N, and the PSK itself. |
| 5324 | */ |
| 5325 | |
| 5326 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5327 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5328 | memset( cur, 0, data_length ); |
| 5329 | cur += data_length; |
| 5330 | *cur++ = pms[0]; |
| 5331 | *cur++ = pms[1]; |
| 5332 | memcpy( cur, data, data_length ); |
| 5333 | cur += data_length; |
| 5334 | |
| 5335 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5336 | |
| 5337 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5338 | return( status ); |
| 5339 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5340 | |
| 5341 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5342 | psa_tls12_prf_key_derivation_t *prf, |
| 5343 | psa_algorithm_t hash_alg, |
| 5344 | psa_key_derivation_step_t step, |
| 5345 | const uint8_t *data, |
| 5346 | size_t data_length ) |
| 5347 | { |
| 5348 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5349 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5350 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5351 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5352 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5353 | |
| 5354 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5355 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5356 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5357 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5358 | /** Check whether the given key type is acceptable for the given |
| 5359 | * input step of a key derivation. |
| 5360 | * |
| 5361 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5362 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5363 | * Both secret and non-secret inputs can alternatively have the type |
| 5364 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5365 | * that the input was passed as a buffer rather than via a key object. |
| 5366 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5367 | static int psa_key_derivation_check_input_type( |
| 5368 | psa_key_derivation_step_t step, |
| 5369 | psa_key_type_t key_type ) |
| 5370 | { |
| 5371 | switch( step ) |
| 5372 | { |
| 5373 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5374 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5375 | return( PSA_SUCCESS ); |
| 5376 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5377 | return( PSA_SUCCESS ); |
| 5378 | break; |
| 5379 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5380 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5381 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5382 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5383 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5384 | return( PSA_SUCCESS ); |
| 5385 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5386 | return( PSA_SUCCESS ); |
| 5387 | break; |
| 5388 | } |
| 5389 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5390 | } |
| 5391 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5392 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5393 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5394 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5395 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5396 | const uint8_t *data, |
| 5397 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5398 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5399 | psa_status_t status; |
| 5400 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5401 | |
| 5402 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5403 | if( status != PSA_SUCCESS ) |
| 5404 | goto exit; |
| 5405 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5406 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5407 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5408 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5409 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5410 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5411 | step, data, data_length ); |
| 5412 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5413 | else |
| 5414 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5415 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5416 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5417 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5418 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5419 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5420 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5421 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5422 | else |
| 5423 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5424 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5425 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5426 | { |
| 5427 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5428 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5429 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5430 | } |
| 5431 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5432 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5433 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5434 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5435 | (void) data; |
| 5436 | (void) data_length; |
| 5437 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5438 | return( PSA_ERROR_BAD_STATE ); |
| 5439 | } |
| 5440 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5441 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5442 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5443 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5444 | return( status ); |
| 5445 | } |
| 5446 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5447 | psa_status_t psa_key_derivation_input_bytes( |
| 5448 | psa_key_derivation_operation_t *operation, |
| 5449 | psa_key_derivation_step_t step, |
| 5450 | const uint8_t *data, |
| 5451 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5452 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5453 | return( psa_key_derivation_input_internal( operation, step, |
| 5454 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5455 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5458 | psa_status_t psa_key_derivation_input_key( |
| 5459 | psa_key_derivation_operation_t *operation, |
| 5460 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5461 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5462 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5463 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5464 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5465 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5466 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5467 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5468 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5469 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5470 | { |
| 5471 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5472 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5473 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5474 | |
| 5475 | /* Passing a key object as a SECRET input unlocks the permission |
| 5476 | * to output to a key object. */ |
| 5477 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5478 | operation->can_output_key = 1; |
| 5479 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5480 | status = psa_key_derivation_input_internal( operation, |
| 5481 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5482 | slot->key.data, |
| 5483 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5484 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5485 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5486 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5487 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5488 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5489 | |
| 5490 | |
| 5491 | |
| 5492 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5493 | /* Key agreement */ |
| 5494 | /****************************************************************/ |
| 5495 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5496 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5497 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5498 | size_t peer_key_length, |
| 5499 | const mbedtls_ecp_keypair *our_key, |
| 5500 | uint8_t *shared_secret, |
| 5501 | size_t shared_secret_size, |
| 5502 | size_t *shared_secret_length ) |
| 5503 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5504 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5505 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5506 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5507 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5508 | 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] | 5509 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5510 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5511 | status = mbedtls_psa_ecp_load_representation( |
| 5512 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5513 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5514 | peer_key, |
| 5515 | peer_key_length, |
| 5516 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5517 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5518 | goto exit; |
| 5519 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5520 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5521 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5522 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5523 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5524 | status = mbedtls_to_psa_error( |
| 5525 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5526 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5527 | goto exit; |
| 5528 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5529 | status = mbedtls_to_psa_error( |
| 5530 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5531 | shared_secret_length, |
| 5532 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5533 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5534 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5535 | if( status != PSA_SUCCESS ) |
| 5536 | goto exit; |
| 5537 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5538 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5539 | |
| 5540 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5541 | if( status != PSA_SUCCESS ) |
| 5542 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5543 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5544 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5545 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5546 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5547 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5548 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5549 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5550 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5551 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5552 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5553 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5554 | psa_key_slot_t *private_key, |
| 5555 | const uint8_t *peer_key, |
| 5556 | size_t peer_key_length, |
| 5557 | uint8_t *shared_secret, |
| 5558 | size_t shared_secret_size, |
| 5559 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5560 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5561 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5562 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5563 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5564 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5565 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5566 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5567 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5568 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 5569 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5570 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5571 | private_key->key.data, |
| 5572 | private_key->key.bytes, |
| 5573 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5574 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5575 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5576 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5577 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5578 | shared_secret, shared_secret_size, |
| 5579 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5580 | mbedtls_ecp_keypair_free( ecp ); |
| 5581 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5582 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5583 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5584 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5585 | (void) private_key; |
| 5586 | (void) peer_key; |
| 5587 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5588 | (void) shared_secret; |
| 5589 | (void) shared_secret_size; |
| 5590 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5591 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5592 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5593 | } |
| 5594 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5595 | /* 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] | 5596 | * to potentially free embedded data structures and wipe confidential data. |
| 5597 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5598 | 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] | 5599 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5600 | psa_key_slot_t *private_key, |
| 5601 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5602 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5603 | { |
| 5604 | psa_status_t status; |
| 5605 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5606 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5607 | 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] | 5608 | |
| 5609 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5610 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5611 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5612 | private_key, |
| 5613 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5614 | shared_secret, |
| 5615 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5616 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5617 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5618 | goto exit; |
| 5619 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5620 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5621 | * the shared secret. A shared secret is permitted wherever a key |
| 5622 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5623 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5624 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5625 | shared_secret, |
| 5626 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5627 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5628 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5629 | return( status ); |
| 5630 | } |
| 5631 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5632 | 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] | 5633 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5634 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5635 | const uint8_t *peer_key, |
| 5636 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5637 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5638 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5639 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5640 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5641 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5642 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5643 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5644 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5645 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5646 | if( status != PSA_SUCCESS ) |
| 5647 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5648 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5649 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5650 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5651 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5652 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5653 | else |
| 5654 | { |
| 5655 | /* If a private key has been added as SECRET, we allow the derived |
| 5656 | * key material to be used as a key in PSA Crypto. */ |
| 5657 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5658 | operation->can_output_key = 1; |
| 5659 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5660 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5661 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5662 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5663 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5664 | } |
| 5665 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5666 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5667 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5668 | const uint8_t *peer_key, |
| 5669 | size_t peer_key_length, |
| 5670 | uint8_t *output, |
| 5671 | size_t output_size, |
| 5672 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5673 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5674 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5675 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5676 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5677 | |
| 5678 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5679 | { |
| 5680 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5681 | goto exit; |
| 5682 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5683 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5684 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5685 | if( status != PSA_SUCCESS ) |
| 5686 | goto exit; |
| 5687 | |
| 5688 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5689 | peer_key, peer_key_length, |
| 5690 | output, output_size, |
| 5691 | output_length ); |
| 5692 | |
| 5693 | exit: |
| 5694 | if( status != PSA_SUCCESS ) |
| 5695 | { |
| 5696 | /* If an error happens and is not handled properly, the output |
| 5697 | * may be used as a key to protect sensitive data. Arrange for such |
| 5698 | * a key to be random, which is likely to result in decryption or |
| 5699 | * verification errors. This is better than filling the buffer with |
| 5700 | * some constant data such as zeros, which would result in the data |
| 5701 | * being protected with a reproducible, easily knowable key. |
| 5702 | */ |
| 5703 | psa_generate_random( output, output_size ); |
| 5704 | *output_length = output_size; |
| 5705 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5706 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5707 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5708 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5709 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5710 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5711 | |
| 5712 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5713 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5714 | /****************************************************************/ |
| 5715 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5716 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5717 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5718 | /** Initialize the PSA random generator. |
| 5719 | */ |
| 5720 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5721 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5722 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5723 | memset( rng, 0, sizeof( *rng ) ); |
| 5724 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5725 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5726 | /* Set default configuration if |
| 5727 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5728 | if( rng->entropy_init == NULL ) |
| 5729 | rng->entropy_init = mbedtls_entropy_init; |
| 5730 | if( rng->entropy_free == NULL ) |
| 5731 | rng->entropy_free = mbedtls_entropy_free; |
| 5732 | |
| 5733 | rng->entropy_init( &rng->entropy ); |
| 5734 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5735 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5736 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5737 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5738 | mbedtls_entropy_add_source( &rng->entropy, |
| 5739 | mbedtls_nv_seed_poll, NULL, |
| 5740 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5741 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5742 | #endif |
| 5743 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5744 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5745 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | /** Deinitialize the PSA random generator. |
| 5749 | */ |
| 5750 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 5751 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5752 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5753 | memset( rng, 0, sizeof( *rng ) ); |
| 5754 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5755 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5756 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5757 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | /** Seed the PSA random generator. |
| 5761 | */ |
| 5762 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5763 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5764 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5765 | /* Do nothing: the external RNG seeds itself. */ |
| 5766 | (void) rng; |
| 5767 | return( PSA_SUCCESS ); |
| 5768 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5769 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5770 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5771 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5772 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5773 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5774 | } |
| 5775 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5776 | psa_status_t psa_generate_random( uint8_t *output, |
| 5777 | size_t output_size ) |
| 5778 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5779 | GUARD_MODULE_INITIALIZED; |
| 5780 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5781 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5782 | |
| 5783 | size_t output_length = 0; |
| 5784 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 5785 | output, output_size, |
| 5786 | &output_length ); |
| 5787 | if( status != PSA_SUCCESS ) |
| 5788 | return( status ); |
| 5789 | /* Breaking up a request into smaller chunks is currently not supported |
| 5790 | * for the extrernal RNG interface. */ |
| 5791 | if( output_length != output_size ) |
| 5792 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 5793 | return( PSA_SUCCESS ); |
| 5794 | |
| 5795 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5796 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5797 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5798 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5799 | size_t request_size = |
| 5800 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5801 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5802 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5803 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 5804 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5805 | if( ret != 0 ) |
| 5806 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5807 | output_size -= request_size; |
| 5808 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5809 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5810 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5811 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5812 | } |
| 5813 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5814 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5815 | * |
| 5816 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5817 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5818 | * PSA API doesn't support passing an explicit state. |
| 5819 | * |
| 5820 | * In the non-external case, psa_generate_random() calls an |
| 5821 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5822 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5823 | * instead of doing this back-and-forth between the PSA API and the |
| 5824 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5825 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5826 | */ |
| 5827 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5828 | int mbedtls_psa_get_random( void *p_rng, |
| 5829 | unsigned char *output, |
| 5830 | size_t output_size ) |
| 5831 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5832 | /* This function takes a pointer to the RNG state because that's what |
| 5833 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5834 | * its own state internally and doesn't let the caller access that state. |
| 5835 | * So we just ignore the state parameter, and in practice we'll pass |
| 5836 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5837 | (void) p_rng; |
| 5838 | psa_status_t status = psa_generate_random( output, output_size ); |
| 5839 | if( status == PSA_SUCCESS ) |
| 5840 | return( 0 ); |
| 5841 | else |
| 5842 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 5843 | } |
| 5844 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5845 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5846 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5847 | #include "mbedtls/entropy_poll.h" |
| 5848 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5849 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5850 | size_t seed_size ) |
| 5851 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5852 | if( global_data.initialized ) |
| 5853 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5854 | |
| 5855 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5856 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5857 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5858 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5859 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5860 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5861 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5862 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5863 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5864 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5865 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5866 | * \param type The key type |
| 5867 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5868 | * |
| 5869 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5870 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5871 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5872 | * The size in bits of the key is not valid. |
| 5873 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5874 | * The type and/or the size in bits of the key or the combination of |
| 5875 | * the two is not supported. |
| 5876 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5877 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 5878 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5879 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5880 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5881 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5882 | if( key_type_is_raw_bytes( type ) ) |
| 5883 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5884 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5885 | if( status != PSA_SUCCESS ) |
| 5886 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5887 | } |
| 5888 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5889 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5890 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5891 | { |
| 5892 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5893 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5894 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5895 | /* Accept only byte-aligned keys, for the same reasons as |
| 5896 | * in psa_import_rsa_key(). */ |
| 5897 | if( bits % 8 != 0 ) |
| 5898 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5899 | } |
| 5900 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5901 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5902 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5903 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5904 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 5905 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 5906 | /* To avoid empty block, return successfully here. */ |
| 5907 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5908 | } |
| 5909 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5910 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5911 | { |
| 5912 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5913 | } |
| 5914 | |
| 5915 | return( PSA_SUCCESS ); |
| 5916 | } |
| 5917 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5918 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5919 | const psa_key_attributes_t *attributes, |
| 5920 | 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] | 5921 | { |
| 5922 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5923 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5924 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5925 | if( ( attributes->domain_parameters == NULL ) && |
| 5926 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5927 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5928 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5929 | if( key_type_is_raw_bytes( type ) ) |
| 5930 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5931 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5932 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5933 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5934 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5935 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5936 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5937 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5938 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5939 | } |
| 5940 | else |
| 5941 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5942 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5943 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5944 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 5945 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 5946 | key_buffer, |
| 5947 | key_buffer_size, |
| 5948 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5949 | } |
| 5950 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5951 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5952 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5953 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5954 | 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] | 5955 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 5956 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 5957 | key_buffer, |
| 5958 | key_buffer_size, |
| 5959 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5960 | } |
| 5961 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5962 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5963 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5964 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5965 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5966 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5967 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5968 | return( PSA_SUCCESS ); |
| 5969 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5970 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5971 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5972 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5973 | { |
| 5974 | psa_status_t status; |
| 5975 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5976 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5977 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5978 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5979 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5980 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5981 | /* Reject any attempt to create a zero-length key so that we don't |
| 5982 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5983 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5984 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5985 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5986 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 5987 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5988 | if( status != PSA_SUCCESS ) |
| 5989 | goto exit; |
| 5990 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5991 | /* In the case of a transparent key or an opaque key stored in local |
| 5992 | * storage (thus not in the case of generating a key in a secure element |
| 5993 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5994 | * hold the generated key material. */ |
| 5995 | if( slot->key.data == NULL ) |
| 5996 | { |
| 5997 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 5998 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 5999 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6000 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6001 | attributes->core.type, attributes->core.bits ); |
| 6002 | if( status != PSA_SUCCESS ) |
| 6003 | goto exit; |
| 6004 | |
| 6005 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6006 | attributes->core.type, |
| 6007 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6008 | } |
| 6009 | else |
| 6010 | { |
| 6011 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6012 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6013 | if( status != PSA_SUCCESS ) |
| 6014 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6015 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6016 | |
| 6017 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6018 | if( status != PSA_SUCCESS ) |
| 6019 | goto exit; |
| 6020 | } |
| 6021 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6022 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6023 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6024 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6025 | if( status != PSA_SUCCESS ) |
| 6026 | psa_remove_key_data_from_memory( slot ); |
| 6027 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6028 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6029 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6030 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6031 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6032 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6033 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6034 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6035 | } |
| 6036 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6037 | /****************************************************************/ |
| 6038 | /* Module setup */ |
| 6039 | /****************************************************************/ |
| 6040 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6041 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6042 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6043 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6044 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6045 | { |
| 6046 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6047 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6048 | global_data.rng.entropy_init = entropy_init; |
| 6049 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6050 | return( PSA_SUCCESS ); |
| 6051 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6052 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6053 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6054 | void mbedtls_psa_crypto_free( void ) |
| 6055 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6056 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6057 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6058 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6059 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6060 | } |
| 6061 | /* Wipe all remaining data, including configuration. |
| 6062 | * In particular, this sets all state indicator to the value |
| 6063 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6064 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6065 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6066 | /* Unregister all secure element drivers, so that we restart from |
| 6067 | * a pristine state. */ |
| 6068 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6069 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6070 | } |
| 6071 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6072 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6073 | /** Recover a transaction that was interrupted by a power failure. |
| 6074 | * |
| 6075 | * This function is called during initialization, before psa_crypto_init() |
| 6076 | * returns. If this function returns a failure status, the initialization |
| 6077 | * fails. |
| 6078 | */ |
| 6079 | static psa_status_t psa_crypto_recover_transaction( |
| 6080 | const psa_crypto_transaction_t *transaction ) |
| 6081 | { |
| 6082 | switch( transaction->unknown.type ) |
| 6083 | { |
| 6084 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6085 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6086 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6087 | * is implemented. |
| 6088 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6089 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6090 | default: |
| 6091 | /* We found an unsupported transaction in the storage. |
| 6092 | * 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] | 6093 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6094 | } |
| 6095 | } |
| 6096 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6097 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6098 | psa_status_t psa_crypto_init( void ) |
| 6099 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6100 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6101 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6102 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6103 | if( global_data.initialized != 0 ) |
| 6104 | return( PSA_SUCCESS ); |
| 6105 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6106 | /* Initialize and seed the random generator. */ |
| 6107 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6108 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6109 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6110 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6111 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6112 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6113 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6114 | status = psa_initialize_key_slots( ); |
| 6115 | if( status != PSA_SUCCESS ) |
| 6116 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6117 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6118 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6119 | status = psa_init_all_se_drivers( ); |
| 6120 | if( status != PSA_SUCCESS ) |
| 6121 | goto exit; |
| 6122 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6123 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6124 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6125 | status = psa_crypto_load_transaction( ); |
| 6126 | if( status == PSA_SUCCESS ) |
| 6127 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6128 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6129 | if( status != PSA_SUCCESS ) |
| 6130 | goto exit; |
| 6131 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6132 | } |
| 6133 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6134 | { |
| 6135 | /* There's no transaction to complete. It's all good. */ |
| 6136 | status = PSA_SUCCESS; |
| 6137 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6138 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6139 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6140 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6141 | global_data.initialized = 1; |
| 6142 | |
| 6143 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6144 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6145 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6146 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |