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" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 43 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 44 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 45 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | #define mbedtls_calloc calloc |
| 51 | #define mbedtls_free free |
| 52 | #endif |
| 53 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 54 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 56 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 57 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 58 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/blowfish.h" |
| 60 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 61 | #include "mbedtls/chacha20.h" |
| 62 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/cipher.h" |
| 64 | #include "mbedtls/ccm.h" |
| 65 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 67 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 68 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 69 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/error.h" |
| 71 | #include "mbedtls/gcm.h" |
| 72 | #include "mbedtls/md2.h" |
| 73 | #include "mbedtls/md4.h" |
| 74 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 75 | #include "mbedtls/md.h" |
| 76 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 78 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 79 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 80 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 82 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/sha1.h" |
| 84 | #include "mbedtls/sha256.h" |
| 85 | #include "mbedtls/sha512.h" |
| 86 | #include "mbedtls/xtea.h" |
| 87 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 88 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 89 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 90 | /* constant-time buffer comparison */ |
| 91 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 92 | { |
| 93 | size_t i; |
| 94 | unsigned char diff = 0; |
| 95 | |
| 96 | for( i = 0; i < n; i++ ) |
| 97 | diff |= a[i] ^ b[i]; |
| 98 | |
| 99 | return( diff ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 104 | /****************************************************************/ |
| 105 | /* Global data, support functions and library management */ |
| 106 | /****************************************************************/ |
| 107 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 109 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 110 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 113 | /* Values for psa_global_data_t::rng_state */ |
| 114 | #define RNG_NOT_INITIALIZED 0 |
| 115 | #define RNG_INITIALIZED 1 |
| 116 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 118 | typedef struct |
| 119 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 120 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; |
| 124 | |
| 125 | static psa_global_data_t global_data; |
| 126 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 127 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 128 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 129 | &global_data.rng.drbg; |
| 130 | #endif |
| 131 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 132 | #define GUARD_MODULE_INITIALIZED \ |
| 133 | if( global_data.initialized == 0 ) \ |
| 134 | return( PSA_ERROR_BAD_STATE ); |
| 135 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 136 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 137 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 138 | /* Mbed TLS error codes can combine a high-level error code and a |
| 139 | * low-level error code. The low-level error usually reflects the |
| 140 | * root cause better, so dispatch on that preferably. */ |
| 141 | int low_level_ret = - ( -ret & 0x007f ); |
| 142 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 143 | { |
| 144 | case 0: |
| 145 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 146 | |
| 147 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 148 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 149 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 150 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 151 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 152 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 153 | |
| 154 | case MBEDTLS_ERR_ARC4_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_MD2_HW_ACCEL_FAILED: |
| 267 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 268 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 269 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 270 | |
| 271 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 272 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 273 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 275 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 276 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 277 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 278 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 279 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 280 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 281 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 283 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 284 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 286 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 288 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 289 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 290 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 292 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 294 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 295 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 296 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 297 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 298 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 299 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 300 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 301 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 302 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 303 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 304 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 306 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 307 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 309 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 311 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 312 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 313 | return( PSA_ERROR_NOT_PERMITTED ); |
| 314 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 315 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 316 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 317 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 318 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 319 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 320 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 321 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 322 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 323 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 324 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 325 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 326 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 327 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 328 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 329 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 330 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 331 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 332 | |
| 333 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 335 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 336 | return( PSA_ERROR_INVALID_PADDING ); |
| 337 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 338 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 339 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 341 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 342 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 343 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 344 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 345 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 346 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 347 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 348 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 349 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 350 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 352 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 353 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 354 | |
| 355 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 356 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 357 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 358 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 359 | |
| 360 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 361 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 362 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 363 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 364 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 365 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 366 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 367 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 368 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 369 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 370 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 371 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 372 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 373 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 374 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 375 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 376 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 377 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 378 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 379 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 380 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 381 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 382 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 383 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 384 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 385 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 386 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 390 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 391 | |
| 392 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 393 | /****************************************************************/ |
| 394 | /* Key management */ |
| 395 | /****************************************************************/ |
| 396 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 397 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 398 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 399 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 400 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 401 | } |
| 402 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 403 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 404 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 405 | * current test driver in key_management.c is using this function |
| 406 | * when accelerators are used for ECC key pair and public key. |
| 407 | * Once that dependency is resolved these guards can be removed. |
| 408 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 409 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 410 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 411 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 412 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 413 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 414 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 415 | { |
| 416 | switch( curve ) |
| 417 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 418 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 419 | switch( byte_length ) |
| 420 | { |
| 421 | case PSA_BITS_TO_BYTES( 192 ): |
| 422 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 423 | case PSA_BITS_TO_BYTES( 224 ): |
| 424 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 425 | case PSA_BITS_TO_BYTES( 256 ): |
| 426 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 427 | case PSA_BITS_TO_BYTES( 384 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 429 | case PSA_BITS_TO_BYTES( 521 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 431 | default: |
| 432 | return( MBEDTLS_ECP_DP_NONE ); |
| 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_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | switch( byte_length ) |
| 438 | { |
| 439 | case PSA_BITS_TO_BYTES( 256 ): |
| 440 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 441 | case PSA_BITS_TO_BYTES( 384 ): |
| 442 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 443 | case PSA_BITS_TO_BYTES( 512 ): |
| 444 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 445 | default: |
| 446 | return( MBEDTLS_ECP_DP_NONE ); |
| 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_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 451 | switch( byte_length ) |
| 452 | { |
| 453 | case PSA_BITS_TO_BYTES( 255 ): |
| 454 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 455 | case PSA_BITS_TO_BYTES( 448 ): |
| 456 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 457 | default: |
| 458 | return( MBEDTLS_ECP_DP_NONE ); |
| 459 | } |
| 460 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 461 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 462 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 463 | switch( byte_length ) |
| 464 | { |
| 465 | case PSA_BITS_TO_BYTES( 192 ): |
| 466 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 467 | case PSA_BITS_TO_BYTES( 224 ): |
| 468 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 469 | case PSA_BITS_TO_BYTES( 256 ): |
| 470 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 471 | default: |
| 472 | return( MBEDTLS_ECP_DP_NONE ); |
| 473 | } |
| 474 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 475 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 476 | default: |
| 477 | return( MBEDTLS_ECP_DP_NONE ); |
| 478 | } |
| 479 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 480 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 481 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 482 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 483 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 484 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 485 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 486 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | { |
| 488 | /* Check that the bit size is acceptable for the key type */ |
| 489 | switch( type ) |
| 490 | { |
| 491 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 493 | case PSA_KEY_TYPE_DERIVE: |
| 494 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | #if defined(MBEDTLS_AES_C) |
| 496 | case PSA_KEY_TYPE_AES: |
| 497 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 498 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 499 | break; |
| 500 | #endif |
| 501 | #if defined(MBEDTLS_CAMELLIA_C) |
| 502 | case PSA_KEY_TYPE_CAMELLIA: |
| 503 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 504 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 505 | break; |
| 506 | #endif |
| 507 | #if defined(MBEDTLS_DES_C) |
| 508 | case PSA_KEY_TYPE_DES: |
| 509 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 510 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 511 | break; |
| 512 | #endif |
| 513 | #if defined(MBEDTLS_ARC4_C) |
| 514 | case PSA_KEY_TYPE_ARC4: |
| 515 | if( bits < 8 || bits > 2048 ) |
| 516 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 517 | break; |
| 518 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 519 | #if defined(MBEDTLS_CHACHA20_C) |
| 520 | case PSA_KEY_TYPE_CHACHA20: |
| 521 | if( bits != 256 ) |
| 522 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 523 | break; |
| 524 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 525 | default: |
| 526 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 527 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 528 | if( bits % 8 != 0 ) |
| 529 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 530 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 531 | return( PSA_SUCCESS ); |
| 532 | } |
| 533 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 534 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 535 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 536 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 537 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 538 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 539 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 540 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 541 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 542 | * that are not a multiple of 8) well. For example, there is only |
| 543 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 544 | * way to return the exact bit size of a key. |
| 545 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 546 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 547 | const mbedtls_rsa_context *rsa ) |
| 548 | { |
| 549 | mbedtls_mpi n; |
| 550 | psa_status_t status; |
| 551 | mbedtls_mpi_init( &n ); |
| 552 | status = mbedtls_to_psa_error( |
| 553 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 554 | if( status == PSA_SUCCESS ) |
| 555 | { |
| 556 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 557 | status = PSA_ERROR_NOT_SUPPORTED; |
| 558 | } |
| 559 | mbedtls_mpi_free( &n ); |
| 560 | return( status ); |
| 561 | } |
| 562 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 563 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 564 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 565 | * \param[in] type The type of key contained in \p data. |
| 566 | * \param[in] data The buffer from which to load the representation. |
| 567 | * \param[in] data_length The size in bytes of \p data. |
| 568 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 569 | * The caller is responsible for freeing both the |
| 570 | * contents of the context and the context itself |
| 571 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 572 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 573 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 574 | const uint8_t *data, |
| 575 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 576 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 577 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 578 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 579 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 580 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 581 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 582 | |
| 583 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 584 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 585 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 586 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 587 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 588 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 589 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 590 | if( status != PSA_SUCCESS ) |
| 591 | goto exit; |
| 592 | |
| 593 | /* We have something that the pkparse module recognizes. If it is a |
| 594 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 595 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 596 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 597 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 598 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 599 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 600 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 601 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 602 | * supports non-byte-aligned key sizes, but not well. For example, |
| 603 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 604 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 605 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 606 | { |
| 607 | status = PSA_ERROR_NOT_SUPPORTED; |
| 608 | goto exit; |
| 609 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 610 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 611 | if( status != PSA_SUCCESS ) |
| 612 | goto exit; |
| 613 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 614 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 615 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 616 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 617 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 618 | |
| 619 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 620 | mbedtls_pk_free( &ctx ); |
| 621 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 622 | } |
| 623 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 624 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 625 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 626 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 627 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 628 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 629 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 630 | |
| 631 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 632 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 633 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 634 | /** Export an RSA key to export representation |
| 635 | * |
| 636 | * \param[in] type The type of key (public/private) to export |
| 637 | * \param[in] rsa The internal RSA representation from which to export |
| 638 | * \param[out] data The buffer to export to |
| 639 | * \param[in] data_size The length of the buffer to export to |
| 640 | * \param[out] data_length The amount of bytes written to \p data |
| 641 | */ |
| 642 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 643 | mbedtls_rsa_context *rsa, |
| 644 | uint8_t *data, |
| 645 | size_t data_size, |
| 646 | size_t *data_length ) |
| 647 | { |
| 648 | #if defined(MBEDTLS_PK_WRITE_C) |
| 649 | int ret; |
| 650 | mbedtls_pk_context pk; |
| 651 | uint8_t *pos = data + data_size; |
| 652 | |
| 653 | mbedtls_pk_init( &pk ); |
| 654 | pk.pk_info = &mbedtls_rsa_info; |
| 655 | pk.pk_ctx = rsa; |
| 656 | |
| 657 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 658 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 659 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 660 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 661 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 662 | else |
| 663 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 664 | |
| 665 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 666 | { |
| 667 | /* Clean up in case pk_write failed halfway through. */ |
| 668 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 669 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 670 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 671 | |
| 672 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 673 | * Move the data to the beginning and erase remaining data |
| 674 | * at the original location. */ |
| 675 | if( 2 * (size_t) ret <= data_size ) |
| 676 | { |
| 677 | memcpy( data, data + data_size - ret, ret ); |
| 678 | memset( data + data_size - ret, 0, ret ); |
| 679 | } |
| 680 | else if( (size_t) ret < data_size ) |
| 681 | { |
| 682 | memmove( data, data + data_size - ret, ret ); |
| 683 | memset( data + ret, 0, data_size - ret ); |
| 684 | } |
| 685 | |
| 686 | *data_length = ret; |
| 687 | return( PSA_SUCCESS ); |
| 688 | #else |
| 689 | (void) type; |
| 690 | (void) rsa; |
| 691 | (void) data; |
| 692 | (void) data_size; |
| 693 | (void) data_length; |
| 694 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 695 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 696 | } |
| 697 | |
| 698 | /** Import an RSA key from import representation to a slot |
| 699 | * |
| 700 | * \param[in,out] slot The slot where to store the export representation to |
| 701 | * \param[in] data The buffer containing the import representation |
| 702 | * \param[in] data_length The amount of bytes in \p data |
| 703 | */ |
| 704 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 705 | const uint8_t *data, |
| 706 | size_t data_length ) |
| 707 | { |
| 708 | psa_status_t status; |
| 709 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 710 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 711 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 712 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 713 | status = psa_load_rsa_representation( slot->attr.type, |
| 714 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 715 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 716 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 717 | if( status != PSA_SUCCESS ) |
| 718 | goto exit; |
| 719 | |
| 720 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 721 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 722 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 723 | /* Re-export the data to PSA export format, such that we can store export |
| 724 | * representation in the key slot. Export representation in case of RSA is |
| 725 | * the smallest representation that's allowed as input, so a straight-up |
| 726 | * allocation of the same size as the input buffer will be large enough. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 727 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 728 | if( output == NULL ) |
| 729 | { |
| 730 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 731 | goto exit; |
| 732 | } |
| 733 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 734 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 735 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 736 | output, |
| 737 | data_length, |
| 738 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 739 | exit: |
| 740 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 741 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 742 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 743 | |
| 744 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 745 | if( status != PSA_SUCCESS ) |
| 746 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 747 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 748 | return( status ); |
| 749 | } |
| 750 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 751 | /* On success, store the allocated export-formatted key. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 752 | slot->key.data = output; |
| 753 | slot->key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 754 | |
| 755 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 756 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 757 | |
| 758 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 759 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 760 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 761 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 762 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 763 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 764 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \ |
| 765 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 766 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 767 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 768 | * \param[in] type The type of key contained in \p data. |
| 769 | * \param[in] data The buffer from which to load the representation. |
| 770 | * \param[in] data_length The size in bytes of \p data. |
| 771 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 772 | * The caller is responsible for freeing both the |
| 773 | * contents of the context and the context itself |
| 774 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 775 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 776 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 777 | const uint8_t *data, |
| 778 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 779 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 780 | { |
| 781 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 782 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 783 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 784 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 785 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 786 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 787 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 788 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 789 | /* A Weierstrass public key is represented as: |
| 790 | * - The byte 0x04; |
| 791 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 792 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 793 | * So its data length is 2m+1 where m is the curve size in bits. |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 794 | */ |
| 795 | if( ( data_length & 1 ) == 0 ) |
| 796 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 797 | curve_size = data_length / 2; |
| 798 | |
| 799 | /* Montgomery public keys are represented in compressed format, meaning |
| 800 | * their curve_size is equal to the amount of input. */ |
| 801 | |
| 802 | /* Private keys are represented in uncompressed private random integer |
| 803 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 806 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 807 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 808 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 809 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 810 | mbedtls_ecp_keypair_init( ecp ); |
| 811 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 812 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 813 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 814 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 815 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 816 | { |
| 817 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 818 | goto exit; |
| 819 | } |
| 820 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 821 | status = mbedtls_to_psa_error( |
| 822 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 823 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 824 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 825 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 826 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 827 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 828 | { |
| 829 | /* Load the public value. */ |
| 830 | status = mbedtls_to_psa_error( |
| 831 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 832 | data, |
| 833 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 834 | if( status != PSA_SUCCESS ) |
| 835 | goto exit; |
| 836 | |
| 837 | /* Check that the point is on the curve. */ |
| 838 | status = mbedtls_to_psa_error( |
| 839 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 840 | if( status != PSA_SUCCESS ) |
| 841 | goto exit; |
| 842 | } |
| 843 | else |
| 844 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 845 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 846 | status = mbedtls_to_psa_error( |
| 847 | mbedtls_ecp_read_key( ecp->grp.id, |
| 848 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 849 | data, |
| 850 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 851 | if( status != PSA_SUCCESS ) |
| 852 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 853 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 854 | |
| 855 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 856 | exit: |
| 857 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 858 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 859 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 860 | mbedtls_free( ecp ); |
| 861 | } |
| 862 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 863 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 864 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 865 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 866 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 867 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 868 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || |
| 869 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 870 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 871 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 872 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 873 | /** Export an ECP key to export representation |
| 874 | * |
| 875 | * \param[in] type The type of key (public/private) to export |
| 876 | * \param[in] ecp The internal ECP representation from which to export |
| 877 | * \param[out] data The buffer to export to |
| 878 | * \param[in] data_size The length of the buffer to export to |
| 879 | * \param[out] data_length The amount of bytes written to \p data |
| 880 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 881 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 882 | mbedtls_ecp_keypair *ecp, |
| 883 | uint8_t *data, |
| 884 | size_t data_size, |
| 885 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 886 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 887 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 888 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 889 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 890 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 891 | /* Check whether the public part is loaded */ |
| 892 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 893 | { |
| 894 | /* Calculate the public key */ |
| 895 | status = mbedtls_to_psa_error( |
| 896 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 897 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 898 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 899 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 902 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 903 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 904 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 905 | data_length, |
| 906 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 907 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 908 | if( status != PSA_SUCCESS ) |
| 909 | memset( data, 0, data_size ); |
| 910 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 911 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 912 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 913 | else |
| 914 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 915 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 916 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 917 | |
| 918 | status = mbedtls_to_psa_error( |
| 919 | mbedtls_ecp_write_key( ecp, |
| 920 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 921 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 922 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 923 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 924 | else |
| 925 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 926 | |
| 927 | return( status ); |
| 928 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 929 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 930 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 931 | /** Import an ECP key from import representation to a slot |
| 932 | * |
| 933 | * \param[in,out] slot The slot where to store the export representation to |
| 934 | * \param[in] data The buffer containing the import representation |
| 935 | * \param[in] data_length The amount of bytes in \p data |
| 936 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 937 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 938 | const uint8_t *data, |
| 939 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 940 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 941 | psa_status_t status; |
| 942 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 943 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 944 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 945 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 946 | status = psa_load_ecp_representation( slot->attr.type, |
| 947 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 948 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 949 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 950 | if( status != PSA_SUCCESS ) |
| 951 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 952 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 953 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 954 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 955 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 956 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 957 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 958 | /* Re-export the data to PSA export format. There is currently no support |
| 959 | * for other input formats then the export format, so this is a 1-1 |
| 960 | * copy operation. */ |
| 961 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 962 | if( output == NULL ) |
| 963 | { |
| 964 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 965 | goto exit; |
| 966 | } |
| 967 | |
| 968 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 969 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 970 | output, |
| 971 | data_length, |
| 972 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 973 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 974 | /* Always free the PK object (will also free contained ECP context) */ |
| 975 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 976 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 977 | |
| 978 | /* Free the allocated buffer only on error. */ |
| 979 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 980 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 981 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 982 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 983 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 984 | |
| 985 | /* On success, store the allocated export-formatted key. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 986 | slot->key.data = output; |
| 987 | slot->key.bytes = data_length; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 988 | |
| 989 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 990 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 991 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 992 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 993 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 994 | /** Return the size of the key in the given slot, in bits. |
| 995 | * |
| 996 | * \param[in] slot A key slot. |
| 997 | * |
| 998 | * \return The key size in bits, read from the metadata in the slot. |
| 999 | */ |
| 1000 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 1001 | { |
| 1002 | return( slot->attr.bits ); |
| 1003 | } |
| 1004 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1005 | /** Try to allocate a buffer to an empty key slot. |
| 1006 | * |
| 1007 | * \param[in,out] slot Key slot to attach buffer to. |
| 1008 | * \param[in] buffer_length Requested size of the buffer. |
| 1009 | * |
| 1010 | * \retval #PSA_SUCCESS |
| 1011 | * The buffer has been successfully allocated. |
| 1012 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1013 | * Not enough memory was available for allocation. |
| 1014 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1015 | * Trying to allocate a buffer to a non-empty key slot. |
| 1016 | */ |
| 1017 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 1018 | size_t buffer_length ) |
| 1019 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1020 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1021 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1022 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1023 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 1024 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1025 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1026 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1027 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1028 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1031 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 1032 | const uint8_t* data, |
| 1033 | size_t data_length ) |
| 1034 | { |
| 1035 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 1036 | data_length ); |
| 1037 | if( status != PSA_SUCCESS ) |
| 1038 | return( status ); |
| 1039 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1040 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1041 | return( PSA_SUCCESS ); |
| 1042 | } |
| 1043 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1044 | /** Import key data into a slot. |
| 1045 | * |
| 1046 | * `slot->type` must have been set previously. |
| 1047 | * This function assumes that the slot does not contain any key material yet. |
| 1048 | * On failure, the slot content is unchanged. |
| 1049 | * |
| 1050 | * Persistent storage is not affected. |
| 1051 | * |
| 1052 | * \param[in,out] slot The key slot to import data into. |
| 1053 | * Its `type` field must have previously been set to |
| 1054 | * the desired key type. |
| 1055 | * It must not contain any key material yet. |
| 1056 | * \param[in] data Buffer containing the key material to parse and import. |
| 1057 | * \param data_length Size of \p data in bytes. |
| 1058 | * |
| 1059 | * \retval #PSA_SUCCESS |
| 1060 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1061 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1062 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1063 | */ |
| 1064 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 1065 | const uint8_t *data, |
| 1066 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1067 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1068 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1069 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1070 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1071 | /* zero-length keys are never supported. */ |
| 1072 | if( data_length == 0 ) |
| 1073 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1074 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1075 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1076 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1077 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1078 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1079 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 1080 | if( data_length > SIZE_MAX / 8 ) |
| 1081 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1082 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 1083 | /* Enforce a size limit, and in particular ensure that the bit |
| 1084 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1085 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 1086 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1087 | |
| 1088 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1089 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1090 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1093 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1094 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1095 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1096 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1097 | /* Write the actual key size to the slot. |
| 1098 | * psa_start_key_creation() wrote the size declared by the |
| 1099 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1100 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1101 | |
| 1102 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1103 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1104 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1105 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1106 | /* Try validation through accelerators first. */ |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1107 | psa_key_attributes_t attributes = { |
| 1108 | .core = slot->attr |
| 1109 | }; |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 1110 | |
| 1111 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
| 1112 | if( status != PSA_SUCCESS ) |
| 1113 | return( status ); |
| 1114 | |
| 1115 | bit_size = slot->attr.bits; |
| 1116 | status = psa_driver_wrapper_import_key( &attributes, |
| 1117 | data, data_length, |
| 1118 | slot->key.data, |
| 1119 | slot->key.bytes, |
| 1120 | &slot->key.bytes, |
| 1121 | &bit_size ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1122 | if( status == PSA_SUCCESS ) |
| 1123 | { |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 1124 | if( slot->attr.bits == 0 ) |
| 1125 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 1126 | else if( bit_size != slot->attr.bits ) |
| 1127 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1128 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1129 | return( PSA_SUCCESS ); |
| 1130 | } |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 1131 | else |
| 1132 | { |
| 1133 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1134 | return( status ); |
| 1135 | } |
| 1136 | |
| 1137 | mbedtls_platform_zeroize( slot->key.data, data_length ); |
| 1138 | mbedtls_free( slot->key.data ); |
| 1139 | slot->key.data = NULL; |
| 1140 | slot->key.bytes = 0; |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1141 | |
| 1142 | /* Key format is not supported by any accelerator, try software fallback |
| 1143 | * if present. */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1144 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1145 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1146 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1147 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1148 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 1149 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1150 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1151 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1152 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1153 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1154 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1155 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1156 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1157 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1158 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1159 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1160 | |
| 1161 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 1162 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1163 | } |
| 1164 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1165 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1166 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1167 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1168 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1171 | /** Calculate the intersection of two algorithm usage policies. |
| 1172 | * |
| 1173 | * Return 0 (which allows no operation) on incompatibility. |
| 1174 | */ |
| 1175 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1176 | psa_algorithm_t alg1, |
| 1177 | psa_algorithm_t alg2 ) |
| 1178 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1179 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1180 | if( alg1 == alg2 ) |
| 1181 | return( alg1 ); |
| 1182 | /* If the policies are from the same hash-and-sign family, check |
| 1183 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1184 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1185 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1186 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1187 | { |
| 1188 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1189 | return( alg2 ); |
| 1190 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1191 | return( alg1 ); |
| 1192 | } |
| 1193 | /* If the policies are incompatible, allow nothing. */ |
| 1194 | return( 0 ); |
| 1195 | } |
| 1196 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1197 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1198 | psa_algorithm_t requested_alg ) |
| 1199 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1200 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1201 | if( requested_alg == policy_alg ) |
| 1202 | return( 1 ); |
| 1203 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1204 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1205 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1206 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1207 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1208 | { |
| 1209 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1210 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1211 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1212 | /* 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] | 1213 | * a key derivation with that key agreement should also be allowed. This |
| 1214 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1215 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1216 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1217 | { |
| 1218 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1219 | policy_alg ); |
| 1220 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1221 | /* If it isn't permitted, it's forbidden. */ |
| 1222 | return( 0 ); |
| 1223 | } |
| 1224 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1225 | /** Test whether a policy permits an algorithm. |
| 1226 | * |
| 1227 | * The caller must test usage flags separately. |
| 1228 | */ |
| 1229 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1230 | psa_algorithm_t alg ) |
| 1231 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1232 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1233 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1236 | /** Restrict a key policy based on a constraint. |
| 1237 | * |
| 1238 | * \param[in,out] policy The policy to restrict. |
| 1239 | * \param[in] constraint The policy constraint to apply. |
| 1240 | * |
| 1241 | * \retval #PSA_SUCCESS |
| 1242 | * \c *policy contains the intersection of the original value of |
| 1243 | * \c *policy and \c *constraint. |
| 1244 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1245 | * \c *policy and \c *constraint are incompatible. |
| 1246 | * \c *policy is unchanged. |
| 1247 | */ |
| 1248 | static psa_status_t psa_restrict_key_policy( |
| 1249 | psa_key_policy_t *policy, |
| 1250 | const psa_key_policy_t *constraint ) |
| 1251 | { |
| 1252 | psa_algorithm_t intersection_alg = |
| 1253 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1254 | psa_algorithm_t intersection_alg2 = |
| 1255 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1256 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1257 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1258 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1260 | policy->usage &= constraint->usage; |
| 1261 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1262 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1263 | return( PSA_SUCCESS ); |
| 1264 | } |
| 1265 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1266 | /** Get the description of a key given its identifier and policy constraints |
| 1267 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1268 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1269 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1270 | * nonzero, the key must allow operations with this algorithm. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1271 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1272 | * In case of a persistent key, the function loads the description of the key |
| 1273 | * into a key slot if not already done. |
| 1274 | * |
| 1275 | * On success, the returned key slot is locked. It is the responsibility of |
| 1276 | * 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] | 1277 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1278 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1279 | mbedtls_svc_key_id_t key, |
| 1280 | psa_key_slot_t **p_slot, |
| 1281 | psa_key_usage_t usage, |
| 1282 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1283 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1284 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1285 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1286 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1287 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1288 | if( status != PSA_SUCCESS ) |
| 1289 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1290 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1291 | |
| 1292 | /* Enforce that usage policy for the key slot contains all the flags |
| 1293 | * required by the usage parameter. There is one exception: public |
| 1294 | * keys can always be exported, so we treat public key objects as |
| 1295 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1296 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1297 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1298 | |
| 1299 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1300 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1301 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1302 | |
| 1303 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1304 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1305 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1306 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1307 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1308 | |
| 1309 | error: |
| 1310 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1311 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1312 | |
| 1313 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1314 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1315 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1316 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1317 | * |
| 1318 | * A transparent key is a key for which the key material is directly |
| 1319 | * available, as opposed to a key in a secure element. |
| 1320 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1321 | * This is a temporary function to use instead of |
| 1322 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1323 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1324 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1325 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1326 | * 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] | 1327 | */ |
| 1328 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1329 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1330 | mbedtls_svc_key_id_t key, |
| 1331 | psa_key_slot_t **p_slot, |
| 1332 | psa_key_usage_t usage, |
| 1333 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1334 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1335 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1336 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1337 | if( status != PSA_SUCCESS ) |
| 1338 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1340 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1341 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1342 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1343 | *p_slot = NULL; |
| 1344 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1345 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1346 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1347 | return( PSA_SUCCESS ); |
| 1348 | } |
| 1349 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1350 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1351 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1352 | 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] | 1353 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1354 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1355 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1356 | 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] | 1357 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1358 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1359 | * initialized slot, so we can just free it. */ |
| 1360 | if( slot->key.data != NULL ) |
| 1361 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1362 | |
| 1363 | mbedtls_free( slot->key.data ); |
| 1364 | slot->key.data = NULL; |
| 1365 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1366 | |
| 1367 | return( PSA_SUCCESS ); |
| 1368 | } |
| 1369 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1370 | /** Completely wipe a slot in memory, including its policy. |
| 1371 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1372 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1373 | { |
| 1374 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * 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] | 1378 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1379 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1380 | * 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] | 1381 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1382 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1383 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1384 | { |
| 1385 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1386 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1387 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1388 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1389 | } |
| 1390 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1391 | /* Multipart operations may still be using the key. This is safe |
| 1392 | * because all multipart operation objects are independent from |
| 1393 | * the key slot: if they need to access the key after the setup |
| 1394 | * phase, they have a copy of the key. Note that this means that |
| 1395 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1396 | /* At this point, key material and other type-specific content has |
| 1397 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1398 | * zeroize because the metadata is not particularly sensitive. */ |
| 1399 | memset( slot, 0, sizeof( *slot ) ); |
| 1400 | return( status ); |
| 1401 | } |
| 1402 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1403 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1404 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1405 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1406 | psa_status_t status; /* status of the last operation */ |
| 1407 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1408 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1409 | psa_se_drv_table_entry_t *driver; |
| 1410 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1411 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1412 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1413 | return( PSA_SUCCESS ); |
| 1414 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1415 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1416 | * 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] | 1417 | * key, this will load the key description from persistent memory if not |
| 1418 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1419 | * the key is operated by an SE or not and this information is needed by |
| 1420 | * the current implementation. |
| 1421 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1422 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1423 | if( status != PSA_SUCCESS ) |
| 1424 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1425 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1426 | /* |
| 1427 | * If the key slot containing the key description is under access by the |
| 1428 | * library (apart from the present access), the key cannot be destroyed |
| 1429 | * yet. For the time being, just return in error. Eventually (to be |
| 1430 | * implemented), the key should be destroyed when all accesses have |
| 1431 | * stopped. |
| 1432 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1433 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1434 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1435 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1436 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1437 | } |
| 1438 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1439 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1440 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1441 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1442 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1443 | /* For a key in a secure element, we need to do three things: |
| 1444 | * remove the key file in internal storage, destroy the |
| 1445 | * key inside the secure element, and update the driver's |
| 1446 | * persistent data. Start a transaction that will encompass these |
| 1447 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1448 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1449 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1450 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1451 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1452 | status = psa_crypto_save_transaction( ); |
| 1453 | if( status != PSA_SUCCESS ) |
| 1454 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1455 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1456 | /* We should still try to destroy the key in the secure |
| 1457 | * element and the key metadata in storage. This is especially |
| 1458 | * important if the error is that the storage is full. |
| 1459 | * But how to do it exactly without risking an inconsistent |
| 1460 | * state after a reset? |
| 1461 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1462 | */ |
| 1463 | overall_status = status; |
| 1464 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1465 | } |
| 1466 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1467 | status = psa_destroy_se_key( driver, |
| 1468 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1469 | if( overall_status == PSA_SUCCESS ) |
| 1470 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1471 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1472 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1473 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1474 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1475 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1476 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1477 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1478 | if( overall_status == PSA_SUCCESS ) |
| 1479 | overall_status = status; |
| 1480 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1481 | /* TODO: other slots may have a copy of the same key. We should |
| 1482 | * invalidate them. |
| 1483 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1484 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1485 | } |
| 1486 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1487 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1488 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1489 | if( driver != NULL ) |
| 1490 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1491 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1492 | if( overall_status == PSA_SUCCESS ) |
| 1493 | overall_status = status; |
| 1494 | status = psa_crypto_stop_transaction( ); |
| 1495 | if( overall_status == PSA_SUCCESS ) |
| 1496 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1497 | } |
| 1498 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1499 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1500 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1501 | exit: |
| 1502 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1503 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1504 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1505 | if( overall_status == PSA_SUCCESS ) |
| 1506 | overall_status = status; |
| 1507 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1508 | } |
| 1509 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1510 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1511 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1512 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1513 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1516 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1517 | psa_key_type_t type, |
| 1518 | const uint8_t *data, |
| 1519 | size_t data_length ) |
| 1520 | { |
| 1521 | uint8_t *copy = NULL; |
| 1522 | |
| 1523 | if( data_length != 0 ) |
| 1524 | { |
| 1525 | copy = mbedtls_calloc( 1, data_length ); |
| 1526 | if( copy == NULL ) |
| 1527 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1528 | memcpy( copy, data, data_length ); |
| 1529 | } |
| 1530 | /* After this point, this function is guaranteed to succeed, so it |
| 1531 | * can start modifying `*attributes`. */ |
| 1532 | |
| 1533 | if( attributes->domain_parameters != NULL ) |
| 1534 | { |
| 1535 | mbedtls_free( attributes->domain_parameters ); |
| 1536 | attributes->domain_parameters = NULL; |
| 1537 | attributes->domain_parameters_size = 0; |
| 1538 | } |
| 1539 | |
| 1540 | attributes->domain_parameters = copy; |
| 1541 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1542 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1543 | return( PSA_SUCCESS ); |
| 1544 | } |
| 1545 | |
| 1546 | psa_status_t psa_get_key_domain_parameters( |
| 1547 | const psa_key_attributes_t *attributes, |
| 1548 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1549 | { |
| 1550 | if( attributes->domain_parameters_size > data_size ) |
| 1551 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1552 | *data_length = attributes->domain_parameters_size; |
| 1553 | if( attributes->domain_parameters_size != 0 ) |
| 1554 | memcpy( data, attributes->domain_parameters, |
| 1555 | attributes->domain_parameters_size ); |
| 1556 | return( PSA_SUCCESS ); |
| 1557 | } |
| 1558 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1559 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1560 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1561 | static psa_status_t psa_get_rsa_public_exponent( |
| 1562 | const mbedtls_rsa_context *rsa, |
| 1563 | psa_key_attributes_t *attributes ) |
| 1564 | { |
| 1565 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1566 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1567 | uint8_t *buffer = NULL; |
| 1568 | size_t buflen; |
| 1569 | mbedtls_mpi_init( &mpi ); |
| 1570 | |
| 1571 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1572 | if( ret != 0 ) |
| 1573 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1574 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1575 | { |
| 1576 | /* It's the default value, which is reported as an empty string, |
| 1577 | * so there's nothing to do. */ |
| 1578 | goto exit; |
| 1579 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1580 | |
| 1581 | buflen = mbedtls_mpi_size( &mpi ); |
| 1582 | buffer = mbedtls_calloc( 1, buflen ); |
| 1583 | if( buffer == NULL ) |
| 1584 | { |
| 1585 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1586 | goto exit; |
| 1587 | } |
| 1588 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1589 | if( ret != 0 ) |
| 1590 | goto exit; |
| 1591 | attributes->domain_parameters = buffer; |
| 1592 | attributes->domain_parameters_size = buflen; |
| 1593 | |
| 1594 | exit: |
| 1595 | mbedtls_mpi_free( &mpi ); |
| 1596 | if( ret != 0 ) |
| 1597 | mbedtls_free( buffer ); |
| 1598 | return( mbedtls_to_psa_error( ret ) ); |
| 1599 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1600 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1601 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1602 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1603 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1604 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1605 | 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] | 1606 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1607 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1608 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1609 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1610 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1611 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1612 | psa_reset_key_attributes( attributes ); |
| 1613 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1614 | 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] | 1615 | if( status != PSA_SUCCESS ) |
| 1616 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1617 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1618 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1619 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1620 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1621 | |
| 1622 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1623 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1624 | psa_set_key_slot_number( attributes, |
| 1625 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1626 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1627 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1628 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1629 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1630 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1631 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1632 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1633 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1634 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1635 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1636 | * is not yet implemented. |
| 1637 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1638 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1639 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1640 | break; |
| 1641 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1642 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1643 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1644 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1645 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1646 | slot->key.data, |
| 1647 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1648 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1649 | if( status != PSA_SUCCESS ) |
| 1650 | break; |
| 1651 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1652 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1653 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1654 | mbedtls_rsa_free( rsa ); |
| 1655 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1656 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1657 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1658 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1659 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1660 | default: |
| 1661 | /* Nothing else to do. */ |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | if( status != PSA_SUCCESS ) |
| 1666 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1667 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1668 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1669 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1670 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1673 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1674 | psa_status_t psa_get_key_slot_number( |
| 1675 | const psa_key_attributes_t *attributes, |
| 1676 | psa_key_slot_number_t *slot_number ) |
| 1677 | { |
| 1678 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1679 | { |
| 1680 | *slot_number = attributes->slot_number; |
| 1681 | return( PSA_SUCCESS ); |
| 1682 | } |
| 1683 | else |
| 1684 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1685 | } |
| 1686 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1687 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1688 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1689 | uint8_t *data, |
| 1690 | size_t data_size, |
| 1691 | size_t *data_length ) |
| 1692 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1693 | if( slot->key.bytes > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1694 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1695 | memcpy( data, slot->key.data, slot->key.bytes ); |
| 1696 | memset( data + slot->key.bytes, 0, |
| 1697 | data_size - slot->key.bytes ); |
| 1698 | *data_length = slot->key.bytes; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1699 | return( PSA_SUCCESS ); |
| 1700 | } |
| 1701 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1702 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1703 | uint8_t *data, |
| 1704 | size_t data_size, |
| 1705 | size_t *data_length, |
| 1706 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1707 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1708 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1709 | const psa_drv_se_t *drv; |
| 1710 | psa_drv_se_context_t *drv_context; |
| 1711 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1712 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1713 | *data_length = 0; |
| 1714 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1715 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1716 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1717 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1718 | /* Reject a zero-length output buffer now, since this can never be a |
| 1719 | * valid key representation. This way we know that data must be a valid |
| 1720 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1721 | if( data_size == 0 ) |
| 1722 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1723 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1724 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1725 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1726 | { |
| 1727 | psa_drv_se_export_key_t method; |
| 1728 | if( drv->key_management == NULL ) |
| 1729 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1730 | method = ( export_public_key ? |
| 1731 | drv->key_management->p_export_public : |
| 1732 | drv->key_management->p_export ); |
| 1733 | if( method == NULL ) |
| 1734 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1735 | return( method( drv_context, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1736 | psa_key_slot_get_slot_number( slot ), |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1737 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1738 | } |
| 1739 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1740 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1741 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1742 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1743 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1744 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1745 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1746 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1747 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1748 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1749 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1750 | /* Exporting public -> public */ |
| 1751 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1752 | } |
| 1753 | else if( !export_public_key ) |
| 1754 | { |
| 1755 | /* Exporting private -> private */ |
| 1756 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1757 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1758 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1759 | /* Need to export the public part of a private key, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1760 | * so conversion is needed. Try the accelerators first. */ |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame^] | 1761 | psa_key_attributes_t attributes = { |
| 1762 | .core = slot->attr |
| 1763 | }; |
| 1764 | psa_status_t status = psa_driver_wrapper_export_public_key( |
| 1765 | &attributes, slot->key.data, slot->key.bytes, |
| 1766 | data, data_size, data_length ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1767 | |
| 1768 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 1769 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 1770 | return( status ); |
| 1771 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1772 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1773 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1774 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1775 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1776 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1777 | status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1778 | slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1779 | slot->key.data, |
| 1780 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1781 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1782 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1783 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1784 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1785 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1786 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1787 | data, |
| 1788 | data_size, |
| 1789 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1790 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1791 | mbedtls_rsa_free( rsa ); |
| 1792 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1793 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1794 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1795 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1796 | /* We don't know how to convert a private RSA key to public. */ |
| 1797 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1798 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1799 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1800 | } |
| 1801 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1802 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1803 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1804 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1805 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1806 | status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1807 | slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1808 | slot->key.data, |
| 1809 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1810 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1811 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1812 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1813 | |
| 1814 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1815 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1816 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1817 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1818 | data, |
| 1819 | data_size, |
| 1820 | data_length ); |
| 1821 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1822 | mbedtls_ecp_keypair_free( ecp ); |
| 1823 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1824 | return( status ); |
| 1825 | #else |
| 1826 | /* 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] | 1827 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1828 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1829 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1830 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1831 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1832 | else |
| 1833 | { |
| 1834 | /* This shouldn't happen in the reference implementation, but |
| 1835 | it is valid for a special-purpose implementation to omit |
| 1836 | support for exporting certain key types. */ |
| 1837 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1838 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1839 | } |
| 1840 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1841 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1842 | uint8_t *data, |
| 1843 | size_t data_size, |
| 1844 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1845 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1846 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1847 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1848 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1849 | |
| 1850 | /* Set the key to empty now, so that even when there are errors, we always |
| 1851 | * set data_length to a value between 0 and data_size. On error, setting |
| 1852 | * the key to empty is a good choice because an empty key representation is |
| 1853 | * unlikely to be accepted anywhere. */ |
| 1854 | *data_length = 0; |
| 1855 | |
| 1856 | /* Export requires the EXPORT flag. There is an exception for public keys, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1857 | * which don't require any flag, but |
| 1858 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1859 | */ |
| 1860 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1861 | PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1862 | if( status != PSA_SUCCESS ) |
| 1863 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1864 | |
| 1865 | status = psa_internal_export_key( slot, data, data_size, data_length, 0 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1866 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1867 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1868 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1869 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1870 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1871 | 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] | 1872 | uint8_t *data, |
| 1873 | size_t data_size, |
| 1874 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1875 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1876 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1877 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1878 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1879 | |
| 1880 | /* Set the key to empty now, so that even when there are errors, we always |
| 1881 | * set data_length to a value between 0 and data_size. On error, setting |
| 1882 | * the key to empty is a good choice because an empty key representation is |
| 1883 | * unlikely to be accepted anywhere. */ |
| 1884 | *data_length = 0; |
| 1885 | |
| 1886 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1887 | 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] | 1888 | if( status != PSA_SUCCESS ) |
| 1889 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1890 | |
| 1891 | status = psa_internal_export_key( slot, data, data_size, data_length, 1 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1892 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1893 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1894 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1895 | } |
| 1896 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1897 | #if defined(static_assert) |
| 1898 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1899 | "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] | 1900 | 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] | 1901 | "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] | 1902 | 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] | 1903 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1904 | #endif |
| 1905 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1906 | /** Validate that a key policy is internally well-formed. |
| 1907 | * |
| 1908 | * This function only rejects invalid policies. It does not validate the |
| 1909 | * consistency of the policy with respect to other attributes of the key |
| 1910 | * such as the key type. |
| 1911 | */ |
| 1912 | 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] | 1913 | { |
| 1914 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1915 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1916 | PSA_KEY_USAGE_ENCRYPT | |
| 1917 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1918 | PSA_KEY_USAGE_SIGN_HASH | |
| 1919 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1920 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1921 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1922 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1923 | return( PSA_SUCCESS ); |
| 1924 | } |
| 1925 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1926 | /** Validate the internal consistency of key attributes. |
| 1927 | * |
| 1928 | * This function only rejects invalid attribute values. If does not |
| 1929 | * validate the consistency of the attributes with any key data that may |
| 1930 | * be involved in the creation of the key. |
| 1931 | * |
| 1932 | * Call this function early in the key creation process. |
| 1933 | * |
| 1934 | * \param[in] attributes Key attributes for the new key. |
| 1935 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1936 | * NULL for a transparent key. |
| 1937 | * |
| 1938 | */ |
| 1939 | static psa_status_t psa_validate_key_attributes( |
| 1940 | const psa_key_attributes_t *attributes, |
| 1941 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1942 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1943 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1944 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1945 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1946 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1947 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1948 | if( status != PSA_SUCCESS ) |
| 1949 | return( status ); |
| 1950 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1951 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1952 | if( status != PSA_SUCCESS ) |
| 1953 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1954 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1955 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1956 | { |
| 1957 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1958 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1959 | } |
| 1960 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1961 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1962 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1963 | if( status != PSA_SUCCESS ) |
| 1964 | return( status ); |
| 1965 | } |
| 1966 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1967 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1968 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1969 | return( status ); |
| 1970 | |
| 1971 | /* Refuse to create overly large keys. |
| 1972 | * Note that this doesn't trigger on import if the attributes don't |
| 1973 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1974 | * psa_import_key() needs its own checks. */ |
| 1975 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1976 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1977 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1978 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1979 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1980 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1981 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1982 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1983 | return( PSA_SUCCESS ); |
| 1984 | } |
| 1985 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1986 | /** Prepare a key slot to receive key material. |
| 1987 | * |
| 1988 | * This function allocates a key slot and sets its metadata. |
| 1989 | * |
| 1990 | * If this function fails, call psa_fail_key_creation(). |
| 1991 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1992 | * This function is intended to be used as follows: |
| 1993 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1994 | * it with the specified attributes, and in case of a volatile key assign it |
| 1995 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1996 | * -# Populate the slot with the key material. |
| 1997 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1998 | * In case of failure at any step, stop the sequence and call |
| 1999 | * psa_fail_key_creation(). |
| 2000 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2001 | * On success, the key slot is locked. It is the responsibility of the caller |
| 2002 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2003 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2004 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2005 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2006 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 2007 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 2008 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2009 | * |
| 2010 | * \retval #PSA_SUCCESS |
| 2011 | * The key slot is ready to receive key material. |
| 2012 | * \return If this function fails, the key slot is an invalid state. |
| 2013 | * 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] | 2014 | */ |
| 2015 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2016 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2017 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2018 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2019 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2020 | { |
| 2021 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 2022 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2023 | psa_key_slot_t *slot; |
| 2024 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2025 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2026 | *p_drv = NULL; |
| 2027 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2028 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 2029 | if( status != PSA_SUCCESS ) |
| 2030 | return( status ); |
| 2031 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2032 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2033 | if( status != PSA_SUCCESS ) |
| 2034 | return( status ); |
| 2035 | slot = *p_slot; |
| 2036 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2037 | /* We're storing the declared bit-size of the key. It's up to each |
| 2038 | * creation mechanism to verify that this information is correct. |
| 2039 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2040 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2041 | * is optional (import, copy). In case of a volatile key, assign it the |
| 2042 | * volatile key identifier associated to the slot returned to contain its |
| 2043 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2044 | |
| 2045 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2046 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 2047 | { |
| 2048 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 2049 | slot->attr.id = volatile_key_id; |
| 2050 | #else |
| 2051 | slot->attr.id.key_id = volatile_key_id; |
| 2052 | #endif |
| 2053 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 2054 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2055 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2056 | * external-only flags, query `attributes`. Thanks to the check |
| 2057 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 2058 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2059 | * may have set. */ |
| 2060 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2061 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2062 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2063 | /* 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] | 2064 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2065 | * create the key file in internal storage, create the |
| 2066 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2067 | * persistent data. This is done by starting a transaction that will |
| 2068 | * encompass these three actions. |
| 2069 | * For registering a volatile key, we just need to find an appropriate |
| 2070 | * slot number inside the SE. Since the key is designated volatile, creating |
| 2071 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2072 | /* The first thing to do is to find a slot number for the new key. |
| 2073 | * We save the slot number in persistent storage as part of the |
| 2074 | * transaction data. It will be needed to recover if the power |
| 2075 | * fails during the key creation process, to clean up on the secure |
| 2076 | * element side after restarting. Obtaining a slot number from the |
| 2077 | * secure element driver updates its persistent state, but we do not yet |
| 2078 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2079 | * 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] | 2080 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2081 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2082 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 2083 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2084 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2085 | if( status != PSA_SUCCESS ) |
| 2086 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2087 | |
| 2088 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2089 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2090 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 2091 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2092 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2093 | psa_crypto_transaction.key.id = slot->attr.id; |
| 2094 | status = psa_crypto_save_transaction( ); |
| 2095 | if( status != PSA_SUCCESS ) |
| 2096 | { |
| 2097 | (void) psa_crypto_stop_transaction( ); |
| 2098 | return( status ); |
| 2099 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2100 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2101 | |
| 2102 | status = psa_copy_key_material_into_slot( |
| 2103 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2104 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2105 | |
| 2106 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2107 | { |
| 2108 | /* Key registration only makes sense with a secure element. */ |
| 2109 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2110 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2111 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2112 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2113 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2114 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2115 | |
| 2116 | /** Finalize the creation of a key once its key material has been set. |
| 2117 | * |
| 2118 | * This entails writing the key to persistent storage. |
| 2119 | * |
| 2120 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2121 | * See the documentation of psa_start_key_creation() for the intended use |
| 2122 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2123 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2124 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 2125 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 2126 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2127 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2128 | * \param[in,out] slot Pointer to the slot with key material. |
| 2129 | * \param[in] driver The secure element driver for the key, |
| 2130 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2131 | * \param[out] key On success, identifier of the key. Note that the |
| 2132 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2133 | * |
| 2134 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2135 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2136 | * \return If this function fails, the key slot is an invalid state. |
| 2137 | * 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] | 2138 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2139 | static psa_status_t psa_finish_key_creation( |
| 2140 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2141 | psa_se_drv_table_entry_t *driver, |
| 2142 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2143 | { |
| 2144 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2145 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2146 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2147 | |
| 2148 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2149 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2150 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2151 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2152 | if( driver != NULL ) |
| 2153 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2154 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2155 | psa_key_slot_number_t slot_number = |
| 2156 | psa_key_slot_get_slot_number( slot ) ; |
| 2157 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2158 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2159 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2160 | sizeof( data.slot_number ), |
| 2161 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2162 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2163 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2164 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2165 | (uint8_t*) &data, |
| 2166 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2167 | } |
| 2168 | else |
| 2169 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2170 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2171 | /* Key material is saved in export representation in the slot, so |
| 2172 | * just pass the slot buffer for storage. */ |
| 2173 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2174 | slot->key.data, |
| 2175 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2176 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2177 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2178 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2179 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2180 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2181 | /* Finish the transaction for a key creation. This does not |
| 2182 | * happen when registering an existing key. Detect this case |
| 2183 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2184 | * creation of a persistent key in a secure element requires a transaction, |
| 2185 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2186 | if( driver != NULL && |
| 2187 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2188 | { |
| 2189 | status = psa_save_se_persistent_data( driver ); |
| 2190 | if( status != PSA_SUCCESS ) |
| 2191 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2192 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2193 | return( status ); |
| 2194 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2195 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2196 | } |
| 2197 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2198 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2199 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2200 | { |
| 2201 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2202 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2203 | if( status != PSA_SUCCESS ) |
| 2204 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2205 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2206 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2207 | return( status ); |
| 2208 | } |
| 2209 | |
| 2210 | /** Abort the creation of a key. |
| 2211 | * |
| 2212 | * You may call this function after calling psa_start_key_creation(), |
| 2213 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2214 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2215 | * See the documentation of psa_start_key_creation() for the intended use |
| 2216 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2217 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2218 | * \param[in,out] slot Pointer to the slot with key material. |
| 2219 | * \param[in] driver The secure element driver for the key, |
| 2220 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2221 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2222 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2223 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2224 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2225 | (void) driver; |
| 2226 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2227 | if( slot == NULL ) |
| 2228 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2229 | |
| 2230 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2231 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2232 | * element, and the failure happened later (when saving metadata |
| 2233 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2234 | * element. |
| 2235 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2236 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2237 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2238 | /* Abort the ongoing transaction if any (there may not be one if |
| 2239 | * the creation process failed before starting one, or if the |
| 2240 | * key creation is a registration of a key in a secure element). |
| 2241 | * Earlier functions must already have done what it takes to undo any |
| 2242 | * partial creation. All that's left is to update the transaction data |
| 2243 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2244 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2245 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2246 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2247 | psa_wipe_key_slot( slot ); |
| 2248 | } |
| 2249 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2250 | /** Validate optional attributes during key creation. |
| 2251 | * |
| 2252 | * Some key attributes are optional during key creation. If they are |
| 2253 | * specified in the attributes structure, check that they are consistent |
| 2254 | * with the data in the slot. |
| 2255 | * |
| 2256 | * This function should be called near the end of key creation, after |
| 2257 | * the slot in memory is fully populated but before saving persistent data. |
| 2258 | */ |
| 2259 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2260 | const psa_key_slot_t *slot, |
| 2261 | const psa_key_attributes_t *attributes ) |
| 2262 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2263 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2264 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2265 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2267 | } |
| 2268 | |
| 2269 | if( attributes->domain_parameters_size != 0 ) |
| 2270 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2271 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2272 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2273 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2274 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2275 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2276 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2277 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2278 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2279 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2280 | slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2281 | slot->key.data, |
| 2282 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2283 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2284 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2285 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2286 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2287 | mbedtls_mpi_init( &actual ); |
| 2288 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2289 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2290 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2291 | mbedtls_rsa_free( rsa ); |
| 2292 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2293 | if( ret != 0 ) |
| 2294 | goto rsa_exit; |
| 2295 | ret = mbedtls_mpi_read_binary( &required, |
| 2296 | attributes->domain_parameters, |
| 2297 | attributes->domain_parameters_size ); |
| 2298 | if( ret != 0 ) |
| 2299 | goto rsa_exit; |
| 2300 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2301 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2302 | rsa_exit: |
| 2303 | mbedtls_mpi_free( &actual ); |
| 2304 | mbedtls_mpi_free( &required ); |
| 2305 | if( ret != 0) |
| 2306 | return( mbedtls_to_psa_error( ret ) ); |
| 2307 | } |
| 2308 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2309 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2310 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2311 | { |
| 2312 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2313 | } |
| 2314 | } |
| 2315 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2316 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2317 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2318 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2319 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2320 | } |
| 2321 | |
| 2322 | return( PSA_SUCCESS ); |
| 2323 | } |
| 2324 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2325 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2326 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2327 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2328 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2329 | { |
| 2330 | psa_status_t status; |
| 2331 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2332 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2333 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2334 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2335 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2336 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2337 | * This also rejects any key which might be encoded as an empty string, |
| 2338 | * which is never valid. */ |
| 2339 | if( data_length == 0 ) |
| 2340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2341 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2342 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2343 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2344 | if( status != PSA_SUCCESS ) |
| 2345 | goto exit; |
| 2346 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2347 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2348 | if( driver != NULL ) |
| 2349 | { |
| 2350 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2351 | /* The driver should set the number of key bits, however in |
| 2352 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2353 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2354 | if( drv->key_management == NULL || |
| 2355 | drv->key_management->p_import == NULL ) |
| 2356 | { |
| 2357 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2358 | goto exit; |
| 2359 | } |
| 2360 | status = drv->key_management->p_import( |
| 2361 | psa_get_se_driver_context( driver ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2362 | psa_key_slot_get_slot_number( slot ), |
| 2363 | attributes, data, data_length, &bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2364 | if( status != PSA_SUCCESS ) |
| 2365 | goto exit; |
| 2366 | if( bits > PSA_MAX_KEY_BITS ) |
| 2367 | { |
| 2368 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2369 | goto exit; |
| 2370 | } |
| 2371 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2372 | } |
| 2373 | else |
| 2374 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2375 | if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) ) |
| 2376 | { |
| 2377 | /* Importing a key with external lifetime through the driver wrapper |
| 2378 | * interface is not yet supported. Return as if this was an invalid |
| 2379 | * lifetime. */ |
| 2380 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2381 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2382 | } |
| 2383 | else |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2384 | { |
| 2385 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2386 | if( status != PSA_SUCCESS ) |
| 2387 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2388 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2389 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2390 | if( status != PSA_SUCCESS ) |
| 2391 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2392 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2393 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2394 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2395 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2396 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2397 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2398 | return( status ); |
| 2399 | } |
| 2400 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2401 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2402 | psa_status_t mbedtls_psa_register_se_key( |
| 2403 | const psa_key_attributes_t *attributes ) |
| 2404 | { |
| 2405 | psa_status_t status; |
| 2406 | psa_key_slot_t *slot = NULL; |
| 2407 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2409 | |
| 2410 | /* Leaving attributes unspecified is not currently supported. |
| 2411 | * It could make sense to query the key type and size from the |
| 2412 | * secure element, but not all secure elements support this |
| 2413 | * and the driver HAL doesn't currently support it. */ |
| 2414 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2415 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2416 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2417 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2418 | |
| 2419 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2420 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2421 | if( status != PSA_SUCCESS ) |
| 2422 | goto exit; |
| 2423 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2424 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2425 | |
| 2426 | exit: |
| 2427 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2428 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2429 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2430 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2431 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2432 | return( status ); |
| 2433 | } |
| 2434 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2435 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2436 | 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] | 2437 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2438 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2439 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2440 | source->key.data, |
| 2441 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2442 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2443 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2444 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2445 | target->attr.type = source->attr.type; |
| 2446 | target->attr.bits = source->attr.bits; |
| 2447 | |
| 2448 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2449 | } |
| 2450 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2451 | 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] | 2452 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2453 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2454 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2455 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2456 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2457 | psa_key_slot_t *source_slot = NULL; |
| 2458 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2459 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2460 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2461 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2462 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2463 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2464 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2465 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2466 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2467 | goto exit; |
| 2468 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2469 | status = psa_validate_optional_attributes( source_slot, |
| 2470 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2471 | if( status != PSA_SUCCESS ) |
| 2472 | goto exit; |
| 2473 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2474 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2475 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2476 | if( status != PSA_SUCCESS ) |
| 2477 | goto exit; |
| 2478 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2479 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2480 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2481 | if( status != PSA_SUCCESS ) |
| 2482 | goto exit; |
| 2483 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2484 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2485 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2486 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2487 | /* Copying to a secure element is not implemented yet. */ |
| 2488 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2489 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2490 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2491 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2492 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2493 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2494 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2495 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2496 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2497 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2498 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2499 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2500 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2501 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2502 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2503 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2504 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2507 | |
| 2508 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2509 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2510 | /* Message digests */ |
| 2511 | /****************************************************************/ |
| 2512 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2513 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2514 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2515 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2516 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2517 | static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2518 | { |
| 2519 | switch( alg ) |
| 2520 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2521 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2522 | case PSA_ALG_MD2: |
| 2523 | return( &mbedtls_md2_info ); |
| 2524 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2525 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2526 | case PSA_ALG_MD4: |
| 2527 | return( &mbedtls_md4_info ); |
| 2528 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2529 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2530 | case PSA_ALG_MD5: |
| 2531 | return( &mbedtls_md5_info ); |
| 2532 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2533 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2534 | case PSA_ALG_RIPEMD160: |
| 2535 | return( &mbedtls_ripemd160_info ); |
| 2536 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2537 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2538 | case PSA_ALG_SHA_1: |
| 2539 | return( &mbedtls_sha1_info ); |
| 2540 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2541 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2542 | case PSA_ALG_SHA_224: |
| 2543 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2544 | #endif |
| 2545 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2546 | case PSA_ALG_SHA_256: |
| 2547 | return( &mbedtls_sha256_info ); |
| 2548 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2549 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2550 | case PSA_ALG_SHA_384: |
| 2551 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2552 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2553 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2554 | case PSA_ALG_SHA_512: |
| 2555 | return( &mbedtls_sha512_info ); |
| 2556 | #endif |
| 2557 | default: |
| 2558 | return( NULL ); |
| 2559 | } |
| 2560 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2561 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2562 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2563 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2564 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2565 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2566 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2567 | { |
| 2568 | switch( operation->alg ) |
| 2569 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2570 | case 0: |
| 2571 | /* The object has (apparently) been initialized but it is not |
| 2572 | * in use. It's ok to call abort on such an object, and there's |
| 2573 | * nothing to do. */ |
| 2574 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2575 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2576 | case PSA_ALG_MD2: |
| 2577 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2578 | break; |
| 2579 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2580 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2581 | case PSA_ALG_MD4: |
| 2582 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2583 | break; |
| 2584 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2585 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2586 | case PSA_ALG_MD5: |
| 2587 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2588 | break; |
| 2589 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2590 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2591 | case PSA_ALG_RIPEMD160: |
| 2592 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2593 | break; |
| 2594 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2595 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2596 | case PSA_ALG_SHA_1: |
| 2597 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2598 | break; |
| 2599 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2600 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2601 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2602 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2603 | break; |
| 2604 | #endif |
| 2605 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2606 | case PSA_ALG_SHA_256: |
| 2607 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2608 | break; |
| 2609 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2610 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2611 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2612 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2613 | break; |
| 2614 | #endif |
| 2615 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2616 | case PSA_ALG_SHA_512: |
| 2617 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2618 | break; |
| 2619 | #endif |
| 2620 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2621 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2622 | } |
| 2623 | operation->alg = 0; |
| 2624 | return( PSA_SUCCESS ); |
| 2625 | } |
| 2626 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2627 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2628 | psa_algorithm_t alg ) |
| 2629 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2630 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2631 | |
| 2632 | /* A context must be freshly initialized before it can be set up. */ |
| 2633 | if( operation->alg != 0 ) |
| 2634 | { |
| 2635 | return( PSA_ERROR_BAD_STATE ); |
| 2636 | } |
| 2637 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2638 | switch( alg ) |
| 2639 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2640 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2641 | case PSA_ALG_MD2: |
| 2642 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2643 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2644 | break; |
| 2645 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2646 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2647 | case PSA_ALG_MD4: |
| 2648 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2649 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2650 | break; |
| 2651 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2652 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2653 | case PSA_ALG_MD5: |
| 2654 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2655 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2656 | break; |
| 2657 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2658 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2659 | case PSA_ALG_RIPEMD160: |
| 2660 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2661 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2662 | break; |
| 2663 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2664 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2665 | case PSA_ALG_SHA_1: |
| 2666 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2667 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2668 | break; |
| 2669 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2670 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2671 | case PSA_ALG_SHA_224: |
| 2672 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2673 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2674 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2675 | #endif |
| 2676 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2677 | case PSA_ALG_SHA_256: |
| 2678 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2679 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2680 | break; |
| 2681 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2682 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2683 | case PSA_ALG_SHA_384: |
| 2684 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2685 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2686 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2687 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2688 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2689 | case PSA_ALG_SHA_512: |
| 2690 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2691 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2692 | break; |
| 2693 | #endif |
| 2694 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2695 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2696 | PSA_ERROR_NOT_SUPPORTED : |
| 2697 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2698 | } |
| 2699 | if( ret == 0 ) |
| 2700 | operation->alg = alg; |
| 2701 | else |
| 2702 | psa_hash_abort( operation ); |
| 2703 | return( mbedtls_to_psa_error( ret ) ); |
| 2704 | } |
| 2705 | |
| 2706 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2707 | const uint8_t *input, |
| 2708 | size_t input_length ) |
| 2709 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2710 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2711 | |
| 2712 | /* Don't require hash implementations to behave correctly on a |
| 2713 | * zero-length input, which may have an invalid pointer. */ |
| 2714 | if( input_length == 0 ) |
| 2715 | return( PSA_SUCCESS ); |
| 2716 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2717 | switch( operation->alg ) |
| 2718 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2719 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2720 | case PSA_ALG_MD2: |
| 2721 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2722 | input, input_length ); |
| 2723 | break; |
| 2724 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2725 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2726 | case PSA_ALG_MD4: |
| 2727 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2728 | input, input_length ); |
| 2729 | break; |
| 2730 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2731 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2732 | case PSA_ALG_MD5: |
| 2733 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2734 | input, input_length ); |
| 2735 | break; |
| 2736 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2737 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2738 | case PSA_ALG_RIPEMD160: |
| 2739 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2740 | input, input_length ); |
| 2741 | break; |
| 2742 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2743 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2744 | case PSA_ALG_SHA_1: |
| 2745 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2746 | input, input_length ); |
| 2747 | break; |
| 2748 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2749 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2750 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2751 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2752 | input, input_length ); |
| 2753 | break; |
| 2754 | #endif |
| 2755 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2756 | case PSA_ALG_SHA_256: |
| 2757 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2758 | input, input_length ); |
| 2759 | break; |
| 2760 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2761 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2762 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2763 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2764 | input, input_length ); |
| 2765 | break; |
| 2766 | #endif |
| 2767 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2768 | case PSA_ALG_SHA_512: |
| 2769 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2770 | input, input_length ); |
| 2771 | break; |
| 2772 | #endif |
| 2773 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2774 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2775 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2776 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2777 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2778 | if( ret != 0 ) |
| 2779 | psa_hash_abort( operation ); |
| 2780 | return( mbedtls_to_psa_error( ret ) ); |
| 2781 | } |
| 2782 | |
| 2783 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2784 | uint8_t *hash, |
| 2785 | size_t hash_size, |
| 2786 | size_t *hash_length ) |
| 2787 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2788 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2789 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2790 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2791 | |
| 2792 | /* Fill the output buffer with something that isn't a valid hash |
| 2793 | * (barring an attack on the hash and deliberately-crafted input), |
| 2794 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2795 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2796 | /* If hash_size is 0 then hash may be NULL and then the |
| 2797 | * call to memset would have undefined behavior. */ |
| 2798 | if( hash_size != 0 ) |
| 2799 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2800 | |
| 2801 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2802 | { |
| 2803 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2804 | goto exit; |
| 2805 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2806 | |
| 2807 | switch( operation->alg ) |
| 2808 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2809 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2810 | case PSA_ALG_MD2: |
| 2811 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2812 | break; |
| 2813 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2814 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2815 | case PSA_ALG_MD4: |
| 2816 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2817 | break; |
| 2818 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2819 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2820 | case PSA_ALG_MD5: |
| 2821 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2822 | break; |
| 2823 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2824 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2825 | case PSA_ALG_RIPEMD160: |
| 2826 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2827 | break; |
| 2828 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2829 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2830 | case PSA_ALG_SHA_1: |
| 2831 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2832 | break; |
| 2833 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2834 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2835 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2836 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2837 | break; |
| 2838 | #endif |
| 2839 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2840 | case PSA_ALG_SHA_256: |
| 2841 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2842 | break; |
| 2843 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2844 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2845 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2846 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2847 | break; |
| 2848 | #endif |
| 2849 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2850 | case PSA_ALG_SHA_512: |
| 2851 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2852 | break; |
| 2853 | #endif |
| 2854 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2855 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2856 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2857 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2858 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2859 | exit: |
| 2860 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2861 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2862 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2863 | return( psa_hash_abort( operation ) ); |
| 2864 | } |
| 2865 | else |
| 2866 | { |
| 2867 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2868 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2869 | } |
| 2870 | } |
| 2871 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2872 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2873 | const uint8_t *hash, |
| 2874 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2875 | { |
| 2876 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2877 | size_t actual_hash_length; |
| 2878 | psa_status_t status = psa_hash_finish( operation, |
| 2879 | actual_hash, sizeof( actual_hash ), |
| 2880 | &actual_hash_length ); |
| 2881 | if( status != PSA_SUCCESS ) |
| 2882 | return( status ); |
| 2883 | if( actual_hash_length != hash_length ) |
| 2884 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2885 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2886 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2887 | return( PSA_SUCCESS ); |
| 2888 | } |
| 2889 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2890 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2891 | const uint8_t *input, size_t input_length, |
| 2892 | uint8_t *hash, size_t hash_size, |
| 2893 | size_t *hash_length ) |
| 2894 | { |
| 2895 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2896 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2897 | |
| 2898 | *hash_length = hash_size; |
| 2899 | status = psa_hash_setup( &operation, alg ); |
| 2900 | if( status != PSA_SUCCESS ) |
| 2901 | goto exit; |
| 2902 | status = psa_hash_update( &operation, input, input_length ); |
| 2903 | if( status != PSA_SUCCESS ) |
| 2904 | goto exit; |
| 2905 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2906 | if( status != PSA_SUCCESS ) |
| 2907 | goto exit; |
| 2908 | |
| 2909 | exit: |
| 2910 | if( status == PSA_SUCCESS ) |
| 2911 | status = psa_hash_abort( &operation ); |
| 2912 | else |
| 2913 | psa_hash_abort( &operation ); |
| 2914 | return( status ); |
| 2915 | } |
| 2916 | |
| 2917 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2918 | const uint8_t *input, size_t input_length, |
| 2919 | const uint8_t *hash, size_t hash_length ) |
| 2920 | { |
| 2921 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2922 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2923 | |
| 2924 | status = psa_hash_setup( &operation, alg ); |
| 2925 | if( status != PSA_SUCCESS ) |
| 2926 | goto exit; |
| 2927 | status = psa_hash_update( &operation, input, input_length ); |
| 2928 | if( status != PSA_SUCCESS ) |
| 2929 | goto exit; |
| 2930 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2931 | if( status != PSA_SUCCESS ) |
| 2932 | goto exit; |
| 2933 | |
| 2934 | exit: |
| 2935 | if( status == PSA_SUCCESS ) |
| 2936 | status = psa_hash_abort( &operation ); |
| 2937 | else |
| 2938 | psa_hash_abort( &operation ); |
| 2939 | return( status ); |
| 2940 | } |
| 2941 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2942 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2943 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2944 | { |
| 2945 | if( target_operation->alg != 0 ) |
| 2946 | return( PSA_ERROR_BAD_STATE ); |
| 2947 | |
| 2948 | switch( source_operation->alg ) |
| 2949 | { |
| 2950 | case 0: |
| 2951 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2952 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2953 | case PSA_ALG_MD2: |
| 2954 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2955 | &source_operation->ctx.md2 ); |
| 2956 | break; |
| 2957 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2958 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2959 | case PSA_ALG_MD4: |
| 2960 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2961 | &source_operation->ctx.md4 ); |
| 2962 | break; |
| 2963 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2964 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2965 | case PSA_ALG_MD5: |
| 2966 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2967 | &source_operation->ctx.md5 ); |
| 2968 | break; |
| 2969 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2970 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2971 | case PSA_ALG_RIPEMD160: |
| 2972 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2973 | &source_operation->ctx.ripemd160 ); |
| 2974 | break; |
| 2975 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2976 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2977 | case PSA_ALG_SHA_1: |
| 2978 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2979 | &source_operation->ctx.sha1 ); |
| 2980 | break; |
| 2981 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2982 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2983 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2984 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2985 | &source_operation->ctx.sha256 ); |
| 2986 | break; |
| 2987 | #endif |
| 2988 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2989 | case PSA_ALG_SHA_256: |
| 2990 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2991 | &source_operation->ctx.sha256 ); |
| 2992 | break; |
| 2993 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2994 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2995 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2996 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2997 | &source_operation->ctx.sha512 ); |
| 2998 | break; |
| 2999 | #endif |
| 3000 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3001 | case PSA_ALG_SHA_512: |
| 3002 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 3003 | &source_operation->ctx.sha512 ); |
| 3004 | break; |
| 3005 | #endif |
| 3006 | default: |
| 3007 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3008 | } |
| 3009 | |
| 3010 | target_operation->alg = source_operation->alg; |
| 3011 | return( PSA_SUCCESS ); |
| 3012 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3013 | |
| 3014 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3015 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3016 | /* MAC */ |
| 3017 | /****************************************************************/ |
| 3018 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 3019 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3020 | psa_algorithm_t alg, |
| 3021 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3022 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3023 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3024 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3025 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3026 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3027 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3028 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3029 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3030 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3031 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 3032 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 3033 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3034 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 3035 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3036 | mode = MBEDTLS_MODE_STREAM; |
| 3037 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3038 | case PSA_ALG_CTR: |
| 3039 | mode = MBEDTLS_MODE_CTR; |
| 3040 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3041 | case PSA_ALG_CFB: |
| 3042 | mode = MBEDTLS_MODE_CFB; |
| 3043 | break; |
| 3044 | case PSA_ALG_OFB: |
| 3045 | mode = MBEDTLS_MODE_OFB; |
| 3046 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3047 | case PSA_ALG_ECB_NO_PADDING: |
| 3048 | mode = MBEDTLS_MODE_ECB; |
| 3049 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3050 | case PSA_ALG_CBC_NO_PADDING: |
| 3051 | mode = MBEDTLS_MODE_CBC; |
| 3052 | break; |
| 3053 | case PSA_ALG_CBC_PKCS7: |
| 3054 | mode = MBEDTLS_MODE_CBC; |
| 3055 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3056 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3057 | mode = MBEDTLS_MODE_CCM; |
| 3058 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3059 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3060 | mode = MBEDTLS_MODE_GCM; |
| 3061 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3062 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3063 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 3064 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3065 | default: |
| 3066 | return( NULL ); |
| 3067 | } |
| 3068 | } |
| 3069 | else if( alg == PSA_ALG_CMAC ) |
| 3070 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3071 | else |
| 3072 | return( NULL ); |
| 3073 | |
| 3074 | switch( key_type ) |
| 3075 | { |
| 3076 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3077 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3078 | break; |
| 3079 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3080 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 3081 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3082 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3083 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3084 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3085 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3086 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 3087 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 3088 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 3089 | if( key_bits == 128 ) |
| 3090 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3091 | break; |
| 3092 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3093 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3094 | break; |
| 3095 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3096 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3097 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3098 | case PSA_KEY_TYPE_CHACHA20: |
| 3099 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 3100 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3101 | default: |
| 3102 | return( NULL ); |
| 3103 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3104 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3105 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3106 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3107 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 3108 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3111 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3112 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3113 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3114 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3115 | { |
| 3116 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3117 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3118 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3119 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3120 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3121 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3122 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3123 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3124 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3125 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3126 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3127 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3128 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3129 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3130 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3131 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3132 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3133 | return( 128 ); |
| 3134 | default: |
| 3135 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3136 | } |
| 3137 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3138 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 3139 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3140 | /* Initialize the MAC operation structure. Once this function has been |
| 3141 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3142 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3143 | psa_algorithm_t alg ) |
| 3144 | { |
| 3145 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3146 | |
| 3147 | operation->alg = alg; |
| 3148 | operation->key_set = 0; |
| 3149 | operation->iv_set = 0; |
| 3150 | operation->iv_required = 0; |
| 3151 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3152 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3153 | |
| 3154 | #if defined(MBEDTLS_CMAC_C) |
| 3155 | if( alg == PSA_ALG_CMAC ) |
| 3156 | { |
| 3157 | operation->iv_required = 0; |
| 3158 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3159 | status = PSA_SUCCESS; |
| 3160 | } |
| 3161 | else |
| 3162 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3163 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3165 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3166 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3167 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3168 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3169 | } |
| 3170 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3171 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3172 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3173 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3174 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | if( status != PSA_SUCCESS ) |
| 3178 | memset( operation, 0, sizeof( *operation ) ); |
| 3179 | return( status ); |
| 3180 | } |
| 3181 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3182 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3183 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3184 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3185 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3186 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3187 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3188 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3189 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3190 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3191 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3192 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3193 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3194 | /* The object has (apparently) been initialized but it is not |
| 3195 | * in use. It's ok to call abort on such an object, and there's |
| 3196 | * nothing to do. */ |
| 3197 | return( PSA_SUCCESS ); |
| 3198 | } |
| 3199 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3200 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3201 | if( operation->alg == PSA_ALG_CMAC ) |
| 3202 | { |
| 3203 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3204 | } |
| 3205 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3206 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3207 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3208 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3209 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3210 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3211 | } |
| 3212 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3213 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3214 | { |
| 3215 | /* Sanity check (shouldn't happen: operation->alg should |
| 3216 | * always have been initialized to a valid value). */ |
| 3217 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3218 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3219 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3220 | operation->alg = 0; |
| 3221 | operation->key_set = 0; |
| 3222 | operation->iv_set = 0; |
| 3223 | operation->iv_required = 0; |
| 3224 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3225 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3226 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3227 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3228 | |
| 3229 | bad_state: |
| 3230 | /* If abort is called on an uninitialized object, we can't trust |
| 3231 | * anything. Wipe the object in case it contains confidential data. |
| 3232 | * This may result in a memory leak if a pointer gets overwritten, |
| 3233 | * but it's too late to do anything about this. */ |
| 3234 | memset( operation, 0, sizeof( *operation ) ); |
| 3235 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3236 | } |
| 3237 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3238 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3239 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3240 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3241 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3242 | const mbedtls_cipher_info_t *cipher_info ) |
| 3243 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3244 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3245 | |
| 3246 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3247 | |
| 3248 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3249 | if( ret != 0 ) |
| 3250 | return( ret ); |
| 3251 | |
| 3252 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3253 | slot->key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3254 | key_bits ); |
| 3255 | return( ret ); |
| 3256 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3257 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3258 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3259 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3260 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3261 | const uint8_t *key, |
| 3262 | size_t key_length, |
| 3263 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3264 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3265 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3266 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3267 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3268 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3269 | psa_status_t status; |
| 3270 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3271 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3272 | * overflow below. This should never trigger if the hash algorithm |
| 3273 | * is implemented correctly. */ |
| 3274 | /* The size checks against the ipad and opad buffers cannot be written |
| 3275 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3276 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3277 | if( block_size > sizeof( ipad ) ) |
| 3278 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3279 | if( block_size > sizeof( hmac->opad ) ) |
| 3280 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3281 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3282 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3283 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3284 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3285 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3286 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3287 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3288 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3289 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3290 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3291 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3292 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3293 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3294 | * an invalid pointer which would make the behavior undefined. */ |
| 3295 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3296 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3297 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3298 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3299 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3300 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3301 | ipad[i] ^= 0x36; |
| 3302 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3303 | |
| 3304 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3305 | * and filling the rest of opad with the requisite constant. */ |
| 3306 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3307 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3308 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3309 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3310 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3311 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3312 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3313 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3314 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3315 | |
| 3316 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3317 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3318 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3319 | return( status ); |
| 3320 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3321 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3322 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3323 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3324 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3325 | psa_algorithm_t alg, |
| 3326 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3327 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3328 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3329 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3330 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3331 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3332 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3333 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3334 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3335 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3336 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3337 | /* A context must be freshly initialized before it can be set up. */ |
| 3338 | if( operation->alg != 0 ) |
| 3339 | { |
| 3340 | return( PSA_ERROR_BAD_STATE ); |
| 3341 | } |
| 3342 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3343 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3344 | if( status != PSA_SUCCESS ) |
| 3345 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3346 | if( is_sign ) |
| 3347 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3348 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3349 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3350 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3351 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3352 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3353 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3354 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3355 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3356 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3357 | { |
| 3358 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3359 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3360 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3361 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3362 | if( cipher_info == NULL ) |
| 3363 | { |
| 3364 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3365 | goto exit; |
| 3366 | } |
| 3367 | operation->mac_size = cipher_info->block_size; |
| 3368 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3369 | status = mbedtls_to_psa_error( ret ); |
| 3370 | } |
| 3371 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3372 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3373 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3374 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3375 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3376 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3377 | if( hash_alg == 0 ) |
| 3378 | { |
| 3379 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3380 | goto exit; |
| 3381 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3382 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3383 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3384 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3385 | if( operation->mac_size == 0 || |
| 3386 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3387 | { |
| 3388 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3389 | goto exit; |
| 3390 | } |
| 3391 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3392 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3393 | { |
| 3394 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3395 | goto exit; |
| 3396 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3397 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3398 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3399 | slot->key.data, |
| 3400 | slot->key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3401 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3402 | } |
| 3403 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3404 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3405 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3406 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3407 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3408 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3409 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3410 | if( truncated == 0 ) |
| 3411 | { |
| 3412 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3413 | } |
| 3414 | else if( truncated < 4 ) |
| 3415 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3416 | /* A very short MAC is too short for security since it can be |
| 3417 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3418 | * so we make this our minimum, even though 32 bits is still |
| 3419 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3420 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3421 | } |
| 3422 | else if( truncated > operation->mac_size ) |
| 3423 | { |
| 3424 | /* It's impossible to "truncate" to a larger length. */ |
| 3425 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3426 | } |
| 3427 | else |
| 3428 | operation->mac_size = truncated; |
| 3429 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3430 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3431 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3432 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3433 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3434 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3435 | else |
| 3436 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3437 | operation->key_set = 1; |
| 3438 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3439 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3440 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3441 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3442 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3443 | } |
| 3444 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3445 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3446 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3447 | psa_algorithm_t alg ) |
| 3448 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3449 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3453 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3454 | psa_algorithm_t alg ) |
| 3455 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3456 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3457 | } |
| 3458 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3459 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3460 | const uint8_t *input, |
| 3461 | size_t input_length ) |
| 3462 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3463 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3464 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3465 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3466 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3467 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3468 | operation->has_input = 1; |
| 3469 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3470 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3471 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3472 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3473 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3474 | input, input_length ); |
| 3475 | status = mbedtls_to_psa_error( ret ); |
| 3476 | } |
| 3477 | else |
| 3478 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3479 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3480 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3481 | { |
| 3482 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3483 | input_length ); |
| 3484 | } |
| 3485 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3486 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3487 | { |
| 3488 | /* This shouldn't happen if `operation` was initialized by |
| 3489 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3490 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3491 | } |
| 3492 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3493 | if( status != PSA_SUCCESS ) |
| 3494 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3495 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3496 | } |
| 3497 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3498 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3499 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3500 | uint8_t *mac, |
| 3501 | size_t mac_size ) |
| 3502 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3503 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3504 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3505 | size_t hash_size = 0; |
| 3506 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3507 | psa_status_t status; |
| 3508 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3509 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3510 | if( status != PSA_SUCCESS ) |
| 3511 | return( status ); |
| 3512 | /* From here on, tmp needs to be wiped. */ |
| 3513 | |
| 3514 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3515 | if( status != PSA_SUCCESS ) |
| 3516 | goto exit; |
| 3517 | |
| 3518 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3519 | if( status != PSA_SUCCESS ) |
| 3520 | goto exit; |
| 3521 | |
| 3522 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3523 | if( status != PSA_SUCCESS ) |
| 3524 | goto exit; |
| 3525 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3526 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3527 | if( status != PSA_SUCCESS ) |
| 3528 | goto exit; |
| 3529 | |
| 3530 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3531 | |
| 3532 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3533 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3534 | return( status ); |
| 3535 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3536 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3537 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3538 | 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] | 3539 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3540 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3541 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3542 | if( ! operation->key_set ) |
| 3543 | return( PSA_ERROR_BAD_STATE ); |
| 3544 | if( operation->iv_required && ! operation->iv_set ) |
| 3545 | return( PSA_ERROR_BAD_STATE ); |
| 3546 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3547 | if( mac_size < operation->mac_size ) |
| 3548 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3549 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3550 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3551 | if( operation->alg == PSA_ALG_CMAC ) |
| 3552 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3553 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3554 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3555 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3556 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3557 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3558 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3559 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3560 | else |
| 3561 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3562 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3563 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3564 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3565 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3566 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3567 | } |
| 3568 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3569 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3570 | { |
| 3571 | /* This shouldn't happen if `operation` was initialized by |
| 3572 | * a setup function. */ |
| 3573 | return( PSA_ERROR_BAD_STATE ); |
| 3574 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3577 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3578 | uint8_t *mac, |
| 3579 | size_t mac_size, |
| 3580 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3581 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3582 | psa_status_t status; |
| 3583 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3584 | if( operation->alg == 0 ) |
| 3585 | { |
| 3586 | return( PSA_ERROR_BAD_STATE ); |
| 3587 | } |
| 3588 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3589 | /* Fill the output buffer with something that isn't a valid mac |
| 3590 | * (barring an attack on the mac and deliberately-crafted input), |
| 3591 | * in case the caller doesn't check the return status properly. */ |
| 3592 | *mac_length = mac_size; |
| 3593 | /* If mac_size is 0 then mac may be NULL and then the |
| 3594 | * call to memset would have undefined behavior. */ |
| 3595 | if( mac_size != 0 ) |
| 3596 | memset( mac, '!', mac_size ); |
| 3597 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3598 | if( ! operation->is_sign ) |
| 3599 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3600 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3601 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3602 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3603 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3604 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3605 | if( status == PSA_SUCCESS ) |
| 3606 | { |
| 3607 | status = psa_mac_abort( operation ); |
| 3608 | if( status == PSA_SUCCESS ) |
| 3609 | *mac_length = operation->mac_size; |
| 3610 | else |
| 3611 | memset( mac, '!', mac_size ); |
| 3612 | } |
| 3613 | else |
| 3614 | psa_mac_abort( operation ); |
| 3615 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3616 | } |
| 3617 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3618 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3619 | const uint8_t *mac, |
| 3620 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3621 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3622 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3623 | psa_status_t status; |
| 3624 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3625 | if( operation->alg == 0 ) |
| 3626 | { |
| 3627 | return( PSA_ERROR_BAD_STATE ); |
| 3628 | } |
| 3629 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3630 | if( operation->is_sign ) |
| 3631 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3632 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3633 | } |
| 3634 | if( operation->mac_size != mac_length ) |
| 3635 | { |
| 3636 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3637 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3638 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3639 | |
| 3640 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3641 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3642 | if( status != PSA_SUCCESS ) |
| 3643 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3644 | |
| 3645 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3646 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3647 | |
| 3648 | cleanup: |
| 3649 | if( status == PSA_SUCCESS ) |
| 3650 | status = psa_mac_abort( operation ); |
| 3651 | else |
| 3652 | psa_mac_abort( operation ); |
| 3653 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3654 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3655 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3656 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3660 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3661 | /****************************************************************/ |
| 3662 | /* Asymmetric cryptography */ |
| 3663 | /****************************************************************/ |
| 3664 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3665 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3666 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3667 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3668 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3669 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3670 | size_t hash_length, |
| 3671 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3672 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3673 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3674 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3675 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3676 | |
| 3677 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3678 | * parameters. Validate that it fits so that we don't risk an |
| 3679 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3680 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3681 | if( hash_length > UINT_MAX ) |
| 3682 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3683 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3684 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3685 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3686 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3687 | * must be correct. */ |
| 3688 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3689 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3690 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3691 | if( md_info == NULL ) |
| 3692 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3693 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3694 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3695 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3696 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3697 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3698 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3699 | /* PSS requires a hash internally. */ |
| 3700 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3701 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3702 | if( md_info == NULL ) |
| 3703 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3704 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3705 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3706 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3707 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3708 | } |
| 3709 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3710 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3711 | psa_algorithm_t alg, |
| 3712 | const uint8_t *hash, |
| 3713 | size_t hash_length, |
| 3714 | uint8_t *signature, |
| 3715 | size_t signature_size, |
| 3716 | size_t *signature_length ) |
| 3717 | { |
| 3718 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3719 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3720 | mbedtls_md_type_t md_alg; |
| 3721 | |
| 3722 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3723 | if( status != PSA_SUCCESS ) |
| 3724 | return( status ); |
| 3725 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3726 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3727 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3728 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3729 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3730 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3731 | { |
| 3732 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3733 | MBEDTLS_MD_NONE ); |
| 3734 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3735 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3736 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3737 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3738 | md_alg, |
| 3739 | (unsigned int) hash_length, |
| 3740 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3741 | signature ); |
| 3742 | } |
| 3743 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3744 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3745 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3746 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3747 | { |
| 3748 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3749 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3750 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3751 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3752 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3753 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3754 | (unsigned int) hash_length, |
| 3755 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3756 | signature ); |
| 3757 | } |
| 3758 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3759 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3760 | { |
| 3761 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3762 | } |
| 3763 | |
| 3764 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3765 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3766 | return( mbedtls_to_psa_error( ret ) ); |
| 3767 | } |
| 3768 | |
| 3769 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3770 | psa_algorithm_t alg, |
| 3771 | const uint8_t *hash, |
| 3772 | size_t hash_length, |
| 3773 | const uint8_t *signature, |
| 3774 | size_t signature_length ) |
| 3775 | { |
| 3776 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3777 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3778 | mbedtls_md_type_t md_alg; |
| 3779 | |
| 3780 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3781 | if( status != PSA_SUCCESS ) |
| 3782 | return( status ); |
| 3783 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3784 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3785 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3786 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3787 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3788 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3789 | { |
| 3790 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3791 | MBEDTLS_MD_NONE ); |
| 3792 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3793 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3794 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3795 | MBEDTLS_RSA_PUBLIC, |
| 3796 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3797 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3798 | hash, |
| 3799 | signature ); |
| 3800 | } |
| 3801 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3802 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3803 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3804 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3805 | { |
| 3806 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3807 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3808 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3809 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3810 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3811 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3812 | (unsigned int) hash_length, |
| 3813 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3814 | signature ); |
| 3815 | } |
| 3816 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3817 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3818 | { |
| 3819 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3820 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3821 | |
| 3822 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3823 | * the rest of the signature is invalid". This has little use in |
| 3824 | * practice and PSA doesn't report this distinction. */ |
| 3825 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3826 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3827 | return( mbedtls_to_psa_error( ret ) ); |
| 3828 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3829 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3830 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3831 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3832 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3833 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3834 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3835 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3836 | * (even though these functions don't modify it). */ |
| 3837 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3838 | psa_algorithm_t alg, |
| 3839 | const uint8_t *hash, |
| 3840 | size_t hash_length, |
| 3841 | uint8_t *signature, |
| 3842 | size_t signature_size, |
| 3843 | size_t *signature_length ) |
| 3844 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3845 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3846 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3847 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3848 | mbedtls_mpi_init( &r ); |
| 3849 | mbedtls_mpi_init( &s ); |
| 3850 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3851 | if( signature_size < 2 * curve_bytes ) |
| 3852 | { |
| 3853 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3854 | goto cleanup; |
| 3855 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3856 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3857 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3858 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3859 | { |
| 3860 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3861 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3862 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3863 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3864 | &ecp->d, hash, |
| 3865 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3866 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3867 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3868 | } |
| 3869 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3870 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3871 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3872 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3873 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3874 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3875 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3876 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3877 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3878 | |
| 3879 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3880 | signature, |
| 3881 | curve_bytes ) ); |
| 3882 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3883 | signature + curve_bytes, |
| 3884 | curve_bytes ) ); |
| 3885 | |
| 3886 | cleanup: |
| 3887 | mbedtls_mpi_free( &r ); |
| 3888 | mbedtls_mpi_free( &s ); |
| 3889 | if( ret == 0 ) |
| 3890 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3891 | return( mbedtls_to_psa_error( ret ) ); |
| 3892 | } |
| 3893 | |
| 3894 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3895 | const uint8_t *hash, |
| 3896 | size_t hash_length, |
| 3897 | const uint8_t *signature, |
| 3898 | size_t signature_length ) |
| 3899 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3900 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3901 | mbedtls_mpi r, s; |
| 3902 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3903 | mbedtls_mpi_init( &r ); |
| 3904 | mbedtls_mpi_init( &s ); |
| 3905 | |
| 3906 | if( signature_length != 2 * curve_bytes ) |
| 3907 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3908 | |
| 3909 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3910 | signature, |
| 3911 | curve_bytes ) ); |
| 3912 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3913 | signature + curve_bytes, |
| 3914 | curve_bytes ) ); |
| 3915 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3916 | /* Check whether the public part is loaded. If not, load it. */ |
| 3917 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3918 | { |
| 3919 | MBEDTLS_MPI_CHK( |
| 3920 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3921 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3922 | } |
| 3923 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3924 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3925 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3926 | |
| 3927 | cleanup: |
| 3928 | mbedtls_mpi_free( &r ); |
| 3929 | mbedtls_mpi_free( &s ); |
| 3930 | return( mbedtls_to_psa_error( ret ) ); |
| 3931 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3932 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3933 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3934 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3935 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3936 | psa_algorithm_t alg, |
| 3937 | const uint8_t *hash, |
| 3938 | size_t hash_length, |
| 3939 | uint8_t *signature, |
| 3940 | size_t signature_size, |
| 3941 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3942 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3943 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3944 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3945 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3946 | |
| 3947 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3948 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3949 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3950 | * buffer can in principle be empty since it doesn't actually have |
| 3951 | * to be a hash.) */ |
| 3952 | if( signature_size == 0 ) |
| 3953 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3954 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3955 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3956 | PSA_KEY_USAGE_SIGN_HASH, |
| 3957 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3958 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3959 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3960 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3961 | { |
| 3962 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3963 | goto exit; |
| 3964 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3965 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3966 | /* Try any of the available accelerators first */ |
| 3967 | status = psa_driver_wrapper_sign_hash( slot, |
| 3968 | alg, |
| 3969 | hash, |
| 3970 | hash_length, |
| 3971 | signature, |
| 3972 | signature_size, |
| 3973 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3974 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3975 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3976 | goto exit; |
| 3977 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3978 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3979 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3980 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3981 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3982 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3983 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3984 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3985 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3986 | slot->key.data, |
| 3987 | slot->key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3988 | &rsa ); |
| 3989 | if( status != PSA_SUCCESS ) |
| 3990 | goto exit; |
| 3991 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3992 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3993 | alg, |
| 3994 | hash, hash_length, |
| 3995 | signature, signature_size, |
| 3996 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3997 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3998 | mbedtls_rsa_free( rsa ); |
| 3999 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4000 | } |
| 4001 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4002 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4003 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4004 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4005 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4006 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4007 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4008 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 4009 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4010 | PSA_ALG_IS_ECDSA( alg ) |
| 4011 | #else |
| 4012 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 4013 | #endif |
| 4014 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4015 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4016 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4017 | status = psa_load_ecp_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4018 | slot->key.data, |
| 4019 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4020 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4021 | if( status != PSA_SUCCESS ) |
| 4022 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4023 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4024 | alg, |
| 4025 | hash, hash_length, |
| 4026 | signature, signature_size, |
| 4027 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4028 | mbedtls_ecp_keypair_free( ecp ); |
| 4029 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4030 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4031 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4032 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4033 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4034 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4035 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4036 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4037 | } |
| 4038 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4039 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4040 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4041 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4042 | |
| 4043 | exit: |
| 4044 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 4045 | * the trailing part on success) with something that isn't a valid mac |
| 4046 | * (barring an attack on the mac and deliberately-crafted input), |
| 4047 | * in case the caller doesn't check the return status properly. */ |
| 4048 | if( status == PSA_SUCCESS ) |
| 4049 | memset( signature + *signature_length, '!', |
| 4050 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 4051 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4052 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4053 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 4054 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4055 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4056 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4057 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4058 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4059 | } |
| 4060 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4061 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4062 | psa_algorithm_t alg, |
| 4063 | const uint8_t *hash, |
| 4064 | size_t hash_length, |
| 4065 | const uint8_t *signature, |
| 4066 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4067 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4068 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4069 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4070 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 4071 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4072 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 4073 | PSA_KEY_USAGE_VERIFY_HASH, |
| 4074 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4075 | if( status != PSA_SUCCESS ) |
| 4076 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4077 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4078 | /* Try any of the available accelerators first */ |
| 4079 | status = psa_driver_wrapper_verify_hash( slot, |
| 4080 | alg, |
| 4081 | hash, |
| 4082 | hash_length, |
| 4083 | signature, |
| 4084 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 4085 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4086 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4087 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4088 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4089 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 4090 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4091 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4092 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4093 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4094 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4095 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4096 | slot->key.data, |
| 4097 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4098 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4099 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4100 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4101 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4102 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4103 | alg, |
| 4104 | hash, hash_length, |
| 4105 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4106 | mbedtls_rsa_free( rsa ); |
| 4107 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4108 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4109 | } |
| 4110 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4111 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4112 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4113 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4114 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4115 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4116 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4117 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4118 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4119 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4120 | status = psa_load_ecp_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4121 | slot->key.data, |
| 4122 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4123 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4124 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4125 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4126 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4127 | hash, hash_length, |
| 4128 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4129 | mbedtls_ecp_keypair_free( ecp ); |
| 4130 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4131 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4132 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4133 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4134 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4135 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4136 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4137 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4138 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4139 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4140 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4141 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4142 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4143 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4144 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4145 | |
| 4146 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4147 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4148 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4149 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4150 | } |
| 4151 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4152 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4153 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4154 | mbedtls_rsa_context *rsa ) |
| 4155 | { |
| 4156 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4157 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4158 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4159 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4160 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4161 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4162 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4163 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4164 | psa_algorithm_t alg, |
| 4165 | const uint8_t *input, |
| 4166 | size_t input_length, |
| 4167 | const uint8_t *salt, |
| 4168 | size_t salt_length, |
| 4169 | uint8_t *output, |
| 4170 | size_t output_size, |
| 4171 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4172 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4173 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4174 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4175 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4176 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4177 | (void) input; |
| 4178 | (void) input_length; |
| 4179 | (void) salt; |
| 4180 | (void) output; |
| 4181 | (void) output_size; |
| 4182 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4183 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4184 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4185 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4186 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4187 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4188 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4189 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4190 | if( status != PSA_SUCCESS ) |
| 4191 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4192 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4193 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4194 | { |
| 4195 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4196 | goto exit; |
| 4197 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4198 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4199 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4200 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4201 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4202 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4203 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4204 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4205 | slot->key.data, |
| 4206 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4207 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4208 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4209 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4210 | |
| 4211 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4212 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4213 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4214 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4215 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4216 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4217 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4218 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4219 | status = mbedtls_to_psa_error( |
| 4220 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4221 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4222 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4223 | MBEDTLS_RSA_PUBLIC, |
| 4224 | input_length, |
| 4225 | input, |
| 4226 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4227 | } |
| 4228 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4229 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4230 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4231 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4232 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4233 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4234 | status = mbedtls_to_psa_error( |
| 4235 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4236 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4237 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4238 | MBEDTLS_RSA_PUBLIC, |
| 4239 | salt, salt_length, |
| 4240 | input_length, |
| 4241 | input, |
| 4242 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4243 | } |
| 4244 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4245 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4246 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4247 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4248 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4249 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4250 | rsa_exit: |
| 4251 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4252 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4253 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4254 | mbedtls_rsa_free( rsa ); |
| 4255 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4256 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4257 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4258 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4259 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4260 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4261 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4262 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4263 | |
| 4264 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4265 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4266 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4267 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4268 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4269 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4270 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4271 | psa_algorithm_t alg, |
| 4272 | const uint8_t *input, |
| 4273 | size_t input_length, |
| 4274 | const uint8_t *salt, |
| 4275 | size_t salt_length, |
| 4276 | uint8_t *output, |
| 4277 | size_t output_size, |
| 4278 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4279 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4280 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4281 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4282 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4283 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4284 | (void) input; |
| 4285 | (void) input_length; |
| 4286 | (void) salt; |
| 4287 | (void) output; |
| 4288 | (void) output_size; |
| 4289 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4290 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4291 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4292 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4294 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4295 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4296 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4297 | if( status != PSA_SUCCESS ) |
| 4298 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4299 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4300 | { |
| 4301 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4302 | goto exit; |
| 4303 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4304 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4306 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4307 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4308 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4309 | mbedtls_rsa_context *rsa = NULL; |
| 4310 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4311 | slot->key.data, |
| 4312 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4313 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4314 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4315 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4316 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4317 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4318 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4319 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4320 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4321 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4322 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4323 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4324 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4325 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4326 | status = mbedtls_to_psa_error( |
| 4327 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4328 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4329 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4330 | MBEDTLS_RSA_PRIVATE, |
| 4331 | output_length, |
| 4332 | input, |
| 4333 | output, |
| 4334 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4335 | } |
| 4336 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4337 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4338 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4339 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4340 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4341 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4342 | status = mbedtls_to_psa_error( |
| 4343 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4344 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4345 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4346 | MBEDTLS_RSA_PRIVATE, |
| 4347 | salt, salt_length, |
| 4348 | output_length, |
| 4349 | input, |
| 4350 | output, |
| 4351 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4352 | } |
| 4353 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4354 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4355 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4356 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4357 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4358 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4359 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4360 | mbedtls_rsa_free( rsa ); |
| 4361 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4362 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4363 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4364 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4365 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4366 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4367 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4368 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4369 | |
| 4370 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4371 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4372 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4373 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4374 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4375 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4376 | |
| 4377 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4378 | /****************************************************************/ |
| 4379 | /* Symmetric cryptography */ |
| 4380 | /****************************************************************/ |
| 4381 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4382 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4383 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4384 | psa_algorithm_t alg, |
| 4385 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4386 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4387 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4388 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4389 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4390 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4391 | size_t key_bits; |
| 4392 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4393 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4394 | PSA_KEY_USAGE_ENCRYPT : |
| 4395 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4396 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4397 | /* A context must be freshly initialized before it can be set up. */ |
| 4398 | if( operation->alg != 0 ) |
| 4399 | return( PSA_ERROR_BAD_STATE ); |
| 4400 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4401 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4402 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4403 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4404 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4405 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4406 | 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] | 4407 | if( status != PSA_SUCCESS ) |
| 4408 | goto exit; |
| 4409 | |
| 4410 | /* Initialize the operation struct members, except for alg. The alg member |
| 4411 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4412 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4413 | operation->key_set = 0; |
| 4414 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4415 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4416 | operation->iv_size = 0; |
| 4417 | operation->block_size = 0; |
| 4418 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4419 | operation->iv_required = 0; |
| 4420 | else |
| 4421 | operation->iv_required = 1; |
| 4422 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4423 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4424 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4425 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4426 | slot, |
| 4427 | alg ); |
| 4428 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4429 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4430 | slot, |
| 4431 | alg ); |
| 4432 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4433 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4434 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4435 | /* Once the driver context is initialised, it needs to be freed using |
| 4436 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4437 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4438 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4439 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4440 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4441 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4442 | goto exit; |
| 4443 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4444 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4445 | * available for the given algorithm & key. */ |
| 4446 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4447 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4448 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4449 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4450 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4451 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4452 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4453 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4454 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4455 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4456 | 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] | 4457 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4458 | { |
| 4459 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4460 | goto exit; |
| 4461 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4462 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4463 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4464 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4465 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4466 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4467 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4468 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4469 | { |
| 4470 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4471 | uint8_t keys[24]; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4472 | memcpy( keys, slot->key.data, 16 ); |
| 4473 | memcpy( keys + 16, slot->key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4474 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4475 | keys, |
| 4476 | 192, cipher_operation ); |
| 4477 | } |
| 4478 | else |
| 4479 | #endif |
| 4480 | { |
| 4481 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4482 | slot->key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4483 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4484 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4485 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4486 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4487 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4488 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4489 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4490 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4491 | case PSA_ALG_CBC_NO_PADDING: |
| 4492 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4493 | MBEDTLS_PADDING_NONE ); |
| 4494 | break; |
| 4495 | case PSA_ALG_CBC_PKCS7: |
| 4496 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4497 | MBEDTLS_PADDING_PKCS7 ); |
| 4498 | break; |
| 4499 | default: |
| 4500 | /* The algorithm doesn't involve padding. */ |
| 4501 | ret = 0; |
| 4502 | break; |
| 4503 | } |
| 4504 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4505 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4506 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4507 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4508 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4509 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4510 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4511 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4512 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4513 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4514 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4515 | #if defined(MBEDTLS_CHACHA20_C) |
| 4516 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4517 | 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] | 4518 | operation->iv_size = 12; |
| 4519 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4520 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4521 | status = PSA_SUCCESS; |
| 4522 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4523 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4524 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4525 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4526 | if( status == PSA_SUCCESS ) |
| 4527 | { |
| 4528 | /* Update operation flags for both driver and software implementations */ |
| 4529 | operation->key_set = 1; |
| 4530 | } |
| 4531 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4532 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4533 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4534 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4535 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4536 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4537 | } |
| 4538 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4539 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4540 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4541 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4542 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4543 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4544 | } |
| 4545 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4546 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4547 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4548 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4549 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4550 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4551 | } |
| 4552 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4553 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4554 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4555 | size_t iv_size, |
| 4556 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4557 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4558 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4559 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4560 | if( operation->iv_set || ! operation->iv_required ) |
| 4561 | { |
| 4562 | return( PSA_ERROR_BAD_STATE ); |
| 4563 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4564 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4565 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4566 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4567 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4568 | iv, |
| 4569 | iv_size, |
| 4570 | iv_length ); |
| 4571 | goto exit; |
| 4572 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4573 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4574 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4575 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4576 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4577 | goto exit; |
| 4578 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4579 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4580 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4581 | if( ret != 0 ) |
| 4582 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4583 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4584 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4585 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4586 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4587 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4588 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4589 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4590 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4591 | if( status == PSA_SUCCESS ) |
| 4592 | operation->iv_set = 1; |
| 4593 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4594 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4595 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4596 | } |
| 4597 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4598 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4599 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4600 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4601 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4602 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4603 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4604 | if( operation->iv_set || ! operation->iv_required ) |
| 4605 | { |
| 4606 | return( PSA_ERROR_BAD_STATE ); |
| 4607 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4608 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4609 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4610 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4611 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4612 | iv, |
| 4613 | iv_length ); |
| 4614 | goto exit; |
| 4615 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4616 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4617 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4618 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4619 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4620 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4621 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4622 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4623 | status = mbedtls_to_psa_error( ret ); |
| 4624 | exit: |
| 4625 | if( status == PSA_SUCCESS ) |
| 4626 | operation->iv_set = 1; |
| 4627 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4628 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4629 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4630 | } |
| 4631 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4632 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4633 | * manual processing, since the PSA API is defined as being able to process |
| 4634 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4635 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4636 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4637 | mbedtls_cipher_context_t *ctx, |
| 4638 | const uint8_t *input, |
| 4639 | size_t input_length, |
| 4640 | uint8_t *output, |
| 4641 | size_t output_size, |
| 4642 | size_t *output_length ) |
| 4643 | { |
| 4644 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4645 | size_t block_size = ctx->cipher_info->block_size; |
| 4646 | size_t internal_output_length = 0; |
| 4647 | *output_length = 0; |
| 4648 | |
| 4649 | if( input_length == 0 ) |
| 4650 | { |
| 4651 | status = PSA_SUCCESS; |
| 4652 | goto exit; |
| 4653 | } |
| 4654 | |
| 4655 | if( ctx->unprocessed_len > 0 ) |
| 4656 | { |
| 4657 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4658 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4659 | |
| 4660 | if( input_length < bytes_to_copy ) |
| 4661 | bytes_to_copy = input_length; |
| 4662 | |
| 4663 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4664 | input, bytes_to_copy ); |
| 4665 | input_length -= bytes_to_copy; |
| 4666 | input += bytes_to_copy; |
| 4667 | ctx->unprocessed_len += bytes_to_copy; |
| 4668 | |
| 4669 | if( ctx->unprocessed_len == block_size ) |
| 4670 | { |
| 4671 | status = mbedtls_to_psa_error( |
| 4672 | mbedtls_cipher_update( ctx, |
| 4673 | ctx->unprocessed_data, |
| 4674 | block_size, |
| 4675 | output, &internal_output_length ) ); |
| 4676 | |
| 4677 | if( status != PSA_SUCCESS ) |
| 4678 | goto exit; |
| 4679 | |
| 4680 | output += internal_output_length; |
| 4681 | output_size -= internal_output_length; |
| 4682 | *output_length += internal_output_length; |
| 4683 | ctx->unprocessed_len = 0; |
| 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | while( input_length >= block_size ) |
| 4688 | { |
| 4689 | /* Run all full blocks we have, one by one */ |
| 4690 | status = mbedtls_to_psa_error( |
| 4691 | mbedtls_cipher_update( ctx, input, |
| 4692 | block_size, |
| 4693 | output, &internal_output_length ) ); |
| 4694 | |
| 4695 | if( status != PSA_SUCCESS ) |
| 4696 | goto exit; |
| 4697 | |
| 4698 | input_length -= block_size; |
| 4699 | input += block_size; |
| 4700 | |
| 4701 | output += internal_output_length; |
| 4702 | output_size -= internal_output_length; |
| 4703 | *output_length += internal_output_length; |
| 4704 | } |
| 4705 | |
| 4706 | if( input_length > 0 ) |
| 4707 | { |
| 4708 | /* Save unprocessed bytes for later processing */ |
| 4709 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4710 | input, input_length ); |
| 4711 | ctx->unprocessed_len += input_length; |
| 4712 | } |
| 4713 | |
| 4714 | status = PSA_SUCCESS; |
| 4715 | |
| 4716 | exit: |
| 4717 | return( status ); |
| 4718 | } |
| 4719 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4720 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4721 | const uint8_t *input, |
| 4722 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4723 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4724 | size_t output_size, |
| 4725 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4726 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4727 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4728 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4729 | if( operation->alg == 0 ) |
| 4730 | { |
| 4731 | return( PSA_ERROR_BAD_STATE ); |
| 4732 | } |
| 4733 | if( operation->iv_required && ! operation->iv_set ) |
| 4734 | { |
| 4735 | return( PSA_ERROR_BAD_STATE ); |
| 4736 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4737 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4738 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4739 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4740 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4741 | input, |
| 4742 | input_length, |
| 4743 | output, |
| 4744 | output_size, |
| 4745 | output_length ); |
| 4746 | goto exit; |
| 4747 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4748 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4749 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4750 | { |
| 4751 | /* Take the unprocessed partial block left over from previous |
| 4752 | * update calls, if any, plus the input to this call. Remove |
| 4753 | * the last partial block, if any. You get the data that will be |
| 4754 | * output in this call. */ |
| 4755 | expected_output_size = |
| 4756 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4757 | / operation->block_size * operation->block_size; |
| 4758 | } |
| 4759 | else |
| 4760 | { |
| 4761 | expected_output_size = input_length; |
| 4762 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4763 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4764 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4765 | { |
| 4766 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4767 | goto exit; |
| 4768 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4769 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4770 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4771 | { |
| 4772 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4773 | * process a single block at a time in ECB mode. Abstract away that |
| 4774 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4775 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4776 | input, |
| 4777 | input_length, |
| 4778 | output, |
| 4779 | output_size, |
| 4780 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4781 | } |
| 4782 | else |
| 4783 | { |
| 4784 | status = mbedtls_to_psa_error( |
| 4785 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4786 | input_length, output, output_length ) ); |
| 4787 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4788 | exit: |
| 4789 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4790 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4791 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4792 | } |
| 4793 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4794 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4795 | uint8_t *output, |
| 4796 | size_t output_size, |
| 4797 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4798 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4799 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4800 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4801 | if( operation->alg == 0 ) |
| 4802 | { |
| 4803 | return( PSA_ERROR_BAD_STATE ); |
| 4804 | } |
| 4805 | if( operation->iv_required && ! operation->iv_set ) |
| 4806 | { |
| 4807 | return( PSA_ERROR_BAD_STATE ); |
| 4808 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4809 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4810 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4811 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4812 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4813 | output, |
| 4814 | output_size, |
| 4815 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4816 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4817 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4818 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4819 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4820 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4821 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4822 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4823 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4824 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4825 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4826 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4827 | } |
| 4828 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4829 | status = mbedtls_to_psa_error( |
| 4830 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4831 | temp_output_buffer, |
| 4832 | output_length ) ); |
| 4833 | if( status != PSA_SUCCESS ) |
| 4834 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4835 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4836 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4837 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4838 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4839 | memcpy( output, temp_output_buffer, *output_length ); |
| 4840 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4841 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4842 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4843 | exit: |
| 4844 | if( operation->mbedtls_in_use == 1 ) |
| 4845 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4846 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4847 | if( status == PSA_SUCCESS ) |
| 4848 | return( psa_cipher_abort( operation ) ); |
| 4849 | else |
| 4850 | { |
| 4851 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4852 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4853 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4854 | return( status ); |
| 4855 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4856 | } |
| 4857 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4858 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4859 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4860 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4861 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4862 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4863 | * in use. It's ok to call abort on such an object, and there's |
| 4864 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4865 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4866 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4868 | /* Sanity check (shouldn't happen: operation->alg should |
| 4869 | * always have been initialized to a valid value). */ |
| 4870 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4871 | return( PSA_ERROR_BAD_STATE ); |
| 4872 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4873 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4874 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4875 | else |
| 4876 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4877 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4878 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4879 | operation->key_set = 0; |
| 4880 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4881 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4882 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4883 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4884 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4885 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4886 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4887 | } |
| 4888 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4889 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4890 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4891 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4892 | /****************************************************************/ |
| 4893 | /* AEAD */ |
| 4894 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4895 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4896 | typedef struct |
| 4897 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4898 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4899 | const mbedtls_cipher_info_t *cipher_info; |
| 4900 | union |
| 4901 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4902 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4903 | #if defined(MBEDTLS_CCM_C) |
| 4904 | mbedtls_ccm_context ccm; |
| 4905 | #endif /* MBEDTLS_CCM_C */ |
| 4906 | #if defined(MBEDTLS_GCM_C) |
| 4907 | mbedtls_gcm_context gcm; |
| 4908 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4909 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4910 | mbedtls_chachapoly_context chachapoly; |
| 4911 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4912 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4913 | psa_algorithm_t core_alg; |
| 4914 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4915 | uint8_t tag_length; |
| 4916 | } aead_operation_t; |
| 4917 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4918 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4919 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4920 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4921 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4922 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4923 | { |
| 4924 | #if defined(MBEDTLS_CCM_C) |
| 4925 | case PSA_ALG_CCM: |
| 4926 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4927 | break; |
| 4928 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4929 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4930 | case PSA_ALG_GCM: |
| 4931 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4932 | break; |
| 4933 | #endif /* MBEDTLS_GCM_C */ |
| 4934 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4935 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4936 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4940 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4941 | psa_key_usage_t usage, |
| 4942 | psa_algorithm_t alg ) |
| 4943 | { |
| 4944 | psa_status_t status; |
| 4945 | size_t key_bits; |
| 4946 | mbedtls_cipher_id_t cipher_id; |
| 4947 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4948 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4949 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4950 | if( status != PSA_SUCCESS ) |
| 4951 | return( status ); |
| 4952 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4953 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4954 | |
| 4955 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4956 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4957 | &cipher_id ); |
| 4958 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4959 | { |
| 4960 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4961 | goto cleanup; |
| 4962 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4963 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4964 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4965 | { |
| 4966 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4967 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4968 | operation->core_alg = PSA_ALG_CCM; |
| 4969 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4970 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4971 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4972 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4973 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4974 | { |
| 4975 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4976 | goto cleanup; |
| 4977 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4978 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4979 | status = mbedtls_to_psa_error( |
| 4980 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4981 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4982 | (unsigned int) key_bits ) ); |
| 4983 | if( status != 0 ) |
| 4984 | goto cleanup; |
| 4985 | break; |
| 4986 | #endif /* MBEDTLS_CCM_C */ |
| 4987 | |
| 4988 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4989 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4990 | operation->core_alg = PSA_ALG_GCM; |
| 4991 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4992 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4993 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4994 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4995 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4996 | { |
| 4997 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4998 | goto cleanup; |
| 4999 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5000 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 5001 | status = mbedtls_to_psa_error( |
| 5002 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5003 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5004 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5005 | if( status != 0 ) |
| 5006 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5007 | break; |
| 5008 | #endif /* MBEDTLS_GCM_C */ |
| 5009 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5010 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5011 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 5012 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 5013 | operation->full_tag_length = 16; |
| 5014 | /* We only support the default tag length. */ |
| 5015 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5016 | { |
| 5017 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5018 | goto cleanup; |
| 5019 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5020 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 5021 | status = mbedtls_to_psa_error( |
| 5022 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5023 | operation->slot->key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5024 | if( status != 0 ) |
| 5025 | goto cleanup; |
| 5026 | break; |
| 5027 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 5028 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5029 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5030 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5031 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5032 | } |
| 5033 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5034 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 5035 | { |
| 5036 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5037 | goto cleanup; |
| 5038 | } |
| 5039 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5040 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5041 | return( PSA_SUCCESS ); |
| 5042 | |
| 5043 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5044 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5045 | return( status ); |
| 5046 | } |
| 5047 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5048 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5049 | psa_algorithm_t alg, |
| 5050 | const uint8_t *nonce, |
| 5051 | size_t nonce_length, |
| 5052 | const uint8_t *additional_data, |
| 5053 | size_t additional_data_length, |
| 5054 | const uint8_t *plaintext, |
| 5055 | size_t plaintext_length, |
| 5056 | uint8_t *ciphertext, |
| 5057 | size_t ciphertext_size, |
| 5058 | size_t *ciphertext_length ) |
| 5059 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5060 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5061 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 5062 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5063 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 5064 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 5065 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5066 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5067 | if( status != PSA_SUCCESS ) |
| 5068 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5069 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5070 | /* For all currently supported modes, the tag is at the end of the |
| 5071 | * ciphertext. */ |
| 5072 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 5073 | { |
| 5074 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5075 | goto exit; |
| 5076 | } |
| 5077 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5078 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5079 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5080 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5081 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5082 | status = mbedtls_to_psa_error( |
| 5083 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 5084 | MBEDTLS_GCM_ENCRYPT, |
| 5085 | plaintext_length, |
| 5086 | nonce, nonce_length, |
| 5087 | additional_data, additional_data_length, |
| 5088 | plaintext, ciphertext, |
| 5089 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5090 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5091 | else |
| 5092 | #endif /* MBEDTLS_GCM_C */ |
| 5093 | #if defined(MBEDTLS_CCM_C) |
| 5094 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5095 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5096 | status = mbedtls_to_psa_error( |
| 5097 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 5098 | plaintext_length, |
| 5099 | nonce, nonce_length, |
| 5100 | additional_data, |
| 5101 | additional_data_length, |
| 5102 | plaintext, ciphertext, |
| 5103 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5104 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5105 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5106 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5107 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5108 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5109 | { |
| 5110 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5111 | { |
| 5112 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5113 | goto exit; |
| 5114 | } |
| 5115 | status = mbedtls_to_psa_error( |
| 5116 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 5117 | plaintext_length, |
| 5118 | nonce, |
| 5119 | additional_data, |
| 5120 | additional_data_length, |
| 5121 | plaintext, |
| 5122 | ciphertext, |
| 5123 | tag ) ); |
| 5124 | } |
| 5125 | else |
| 5126 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5127 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5128 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5129 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5130 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5131 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 5132 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5133 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5134 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5135 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5136 | if( status == PSA_SUCCESS ) |
| 5137 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 5138 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5139 | } |
| 5140 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5141 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 5142 | * followed by the tag. Return the length of the part preceding the tag in |
| 5143 | * *plaintext_length. This is the size of the plaintext in modes where |
| 5144 | * the encrypted data has the same size as the plaintext, such as |
| 5145 | * CCM and GCM. */ |
| 5146 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 5147 | const uint8_t *ciphertext, |
| 5148 | size_t ciphertext_length, |
| 5149 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5150 | const uint8_t **p_tag ) |
| 5151 | { |
| 5152 | size_t payload_length; |
| 5153 | if( tag_length > ciphertext_length ) |
| 5154 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5155 | payload_length = ciphertext_length - tag_length; |
| 5156 | if( payload_length > plaintext_size ) |
| 5157 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5158 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5159 | return( PSA_SUCCESS ); |
| 5160 | } |
| 5161 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5162 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5163 | psa_algorithm_t alg, |
| 5164 | const uint8_t *nonce, |
| 5165 | size_t nonce_length, |
| 5166 | const uint8_t *additional_data, |
| 5167 | size_t additional_data_length, |
| 5168 | const uint8_t *ciphertext, |
| 5169 | size_t ciphertext_length, |
| 5170 | uint8_t *plaintext, |
| 5171 | size_t plaintext_size, |
| 5172 | size_t *plaintext_length ) |
| 5173 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5174 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5175 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5176 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5177 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5178 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5179 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5180 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5181 | if( status != PSA_SUCCESS ) |
| 5182 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5183 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5184 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5185 | ciphertext, ciphertext_length, |
| 5186 | plaintext_size, &tag ); |
| 5187 | if( status != PSA_SUCCESS ) |
| 5188 | goto exit; |
| 5189 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5190 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5191 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5192 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5193 | status = mbedtls_to_psa_error( |
| 5194 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5195 | ciphertext_length - operation.tag_length, |
| 5196 | nonce, nonce_length, |
| 5197 | additional_data, |
| 5198 | additional_data_length, |
| 5199 | tag, operation.tag_length, |
| 5200 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5201 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5202 | else |
| 5203 | #endif /* MBEDTLS_GCM_C */ |
| 5204 | #if defined(MBEDTLS_CCM_C) |
| 5205 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5206 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5207 | status = mbedtls_to_psa_error( |
| 5208 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5209 | ciphertext_length - operation.tag_length, |
| 5210 | nonce, nonce_length, |
| 5211 | additional_data, |
| 5212 | additional_data_length, |
| 5213 | ciphertext, plaintext, |
| 5214 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5215 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5216 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5217 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5218 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5219 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5220 | { |
| 5221 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5222 | { |
| 5223 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5224 | goto exit; |
| 5225 | } |
| 5226 | status = mbedtls_to_psa_error( |
| 5227 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5228 | ciphertext_length - operation.tag_length, |
| 5229 | nonce, |
| 5230 | additional_data, |
| 5231 | additional_data_length, |
| 5232 | tag, |
| 5233 | ciphertext, |
| 5234 | plaintext ) ); |
| 5235 | } |
| 5236 | else |
| 5237 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5238 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5239 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5240 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5241 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5242 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5243 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5244 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5245 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5246 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5247 | if( status == PSA_SUCCESS ) |
| 5248 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5249 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5250 | } |
| 5251 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5252 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5253 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5254 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5255 | /* Generators */ |
| 5256 | /****************************************************************/ |
| 5257 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5258 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 5259 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5260 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5261 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 5262 | #endif |
| 5263 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5264 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5265 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5266 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5267 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5268 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5269 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5270 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5271 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5272 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5273 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5274 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5275 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5276 | } |
| 5277 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5278 | 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] | 5279 | { |
| 5280 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5281 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5282 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5283 | { |
| 5284 | /* The object has (apparently) been initialized but it is not |
| 5285 | * in use. It's ok to call abort on such an object, and there's |
| 5286 | * nothing to do. */ |
| 5287 | } |
| 5288 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5289 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5290 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5291 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5292 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5293 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5294 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5295 | else |
| 5296 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5297 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5298 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5299 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5300 | /* 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] | 5301 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5302 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5303 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5304 | { |
| 5305 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5306 | operation->ctx.tls12_prf.seed_length ); |
| 5307 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5308 | } |
| 5309 | |
| 5310 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5311 | { |
| 5312 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5313 | operation->ctx.tls12_prf.label_length ); |
| 5314 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5315 | } |
| 5316 | |
| 5317 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5318 | |
| 5319 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5320 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5321 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5322 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5323 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5324 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5325 | { |
| 5326 | status = PSA_ERROR_BAD_STATE; |
| 5327 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5328 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5329 | return( status ); |
| 5330 | } |
| 5331 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5332 | 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] | 5333 | size_t *capacity) |
| 5334 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5335 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5336 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5337 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5338 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5341 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5342 | return( PSA_SUCCESS ); |
| 5343 | } |
| 5344 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5345 | 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] | 5346 | size_t capacity ) |
| 5347 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5348 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5349 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5350 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5351 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5352 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5353 | return( PSA_SUCCESS ); |
| 5354 | } |
| 5355 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5356 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5357 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5358 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5359 | 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] | 5360 | psa_algorithm_t hash_alg, |
| 5361 | uint8_t *output, |
| 5362 | size_t output_length ) |
| 5363 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5364 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5365 | psa_status_t status; |
| 5366 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5367 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5368 | return( PSA_ERROR_BAD_STATE ); |
| 5369 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5370 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5371 | while( output_length != 0 ) |
| 5372 | { |
| 5373 | /* Copy what remains of the current block */ |
| 5374 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5375 | if( n > output_length ) |
| 5376 | n = (uint8_t) output_length; |
| 5377 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5378 | output += n; |
| 5379 | output_length -= n; |
| 5380 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5381 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5382 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5383 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5384 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5385 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5386 | * object was corrupted or if this function is called directly |
| 5387 | * inside the library. */ |
| 5388 | if( hkdf->block_number == 0xff ) |
| 5389 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5390 | |
| 5391 | /* We need a new block */ |
| 5392 | ++hkdf->block_number; |
| 5393 | hkdf->offset_in_block = 0; |
| 5394 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5395 | hkdf->prk, hash_length, |
| 5396 | hash_alg ); |
| 5397 | if( status != PSA_SUCCESS ) |
| 5398 | return( status ); |
| 5399 | if( hkdf->block_number != 1 ) |
| 5400 | { |
| 5401 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5402 | hkdf->output_block, |
| 5403 | hash_length ); |
| 5404 | if( status != PSA_SUCCESS ) |
| 5405 | return( status ); |
| 5406 | } |
| 5407 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5408 | hkdf->info, |
| 5409 | hkdf->info_length ); |
| 5410 | if( status != PSA_SUCCESS ) |
| 5411 | return( status ); |
| 5412 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5413 | &hkdf->block_number, 1 ); |
| 5414 | if( status != PSA_SUCCESS ) |
| 5415 | return( status ); |
| 5416 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5417 | hkdf->output_block, |
| 5418 | sizeof( hkdf->output_block ) ); |
| 5419 | if( status != PSA_SUCCESS ) |
| 5420 | return( status ); |
| 5421 | } |
| 5422 | |
| 5423 | return( PSA_SUCCESS ); |
| 5424 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5425 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5426 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5427 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5428 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5429 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5430 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5431 | psa_algorithm_t alg ) |
| 5432 | { |
| 5433 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5434 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5435 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5436 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5437 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5438 | /* We can't be wanting more output after block 0xff, otherwise |
| 5439 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5440 | * prevented this call. It could happen only if the operation |
| 5441 | * object was corrupted or if this function is called directly |
| 5442 | * inside the library. */ |
| 5443 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5444 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5445 | |
| 5446 | /* We need a new block */ |
| 5447 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5448 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5449 | |
| 5450 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5451 | * |
| 5452 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5453 | * |
| 5454 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5455 | * HMAC_hash(secret, A(2) + seed) + |
| 5456 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5457 | * |
| 5458 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5459 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5460 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5461 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5462 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5463 | * is currently extracted as `output_block` and where i is |
| 5464 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5465 | */ |
| 5466 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5467 | /* Save the hash context before using it, to preserve the hash state with |
| 5468 | * only the inner padding in it. We need this, because inner padding depends |
| 5469 | * on the key (secret in the RFC's terminology). */ |
| 5470 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5471 | if( status != PSA_SUCCESS ) |
| 5472 | goto cleanup; |
| 5473 | |
| 5474 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5475 | if( tls12_prf->block_number == 1 ) |
| 5476 | { |
| 5477 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5478 | * the variable seed and in this instance means it in the context of the |
| 5479 | * P_hash function, where seed = label + seed.) */ |
| 5480 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5481 | tls12_prf->label, tls12_prf->label_length ); |
| 5482 | if( status != PSA_SUCCESS ) |
| 5483 | goto cleanup; |
| 5484 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5485 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5486 | if( status != PSA_SUCCESS ) |
| 5487 | goto cleanup; |
| 5488 | } |
| 5489 | else |
| 5490 | { |
| 5491 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5492 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5493 | tls12_prf->Ai, hash_length ); |
| 5494 | if( status != PSA_SUCCESS ) |
| 5495 | goto cleanup; |
| 5496 | } |
| 5497 | |
| 5498 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5499 | tls12_prf->Ai, hash_length ); |
| 5500 | if( status != PSA_SUCCESS ) |
| 5501 | goto cleanup; |
| 5502 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5503 | if( status != PSA_SUCCESS ) |
| 5504 | goto cleanup; |
| 5505 | |
| 5506 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5507 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5508 | tls12_prf->Ai, hash_length ); |
| 5509 | if( status != PSA_SUCCESS ) |
| 5510 | goto cleanup; |
| 5511 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5512 | tls12_prf->label, tls12_prf->label_length ); |
| 5513 | if( status != PSA_SUCCESS ) |
| 5514 | goto cleanup; |
| 5515 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5516 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5517 | if( status != PSA_SUCCESS ) |
| 5518 | goto cleanup; |
| 5519 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5520 | tls12_prf->output_block, hash_length ); |
| 5521 | if( status != PSA_SUCCESS ) |
| 5522 | goto cleanup; |
| 5523 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5524 | if( status != PSA_SUCCESS ) |
| 5525 | goto cleanup; |
| 5526 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5527 | |
| 5528 | cleanup: |
| 5529 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5530 | cleanup_status = psa_hash_abort( &backup ); |
| 5531 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5532 | status = cleanup_status; |
| 5533 | |
| 5534 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5535 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5536 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5537 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5538 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5539 | psa_algorithm_t alg, |
| 5540 | uint8_t *output, |
| 5541 | size_t output_length ) |
| 5542 | { |
| 5543 | 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] | 5544 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5545 | psa_status_t status; |
| 5546 | uint8_t offset, length; |
| 5547 | |
| 5548 | while( output_length != 0 ) |
| 5549 | { |
| 5550 | /* Check if we have fully processed the current block. */ |
| 5551 | if( tls12_prf->left_in_block == 0 ) |
| 5552 | { |
| 5553 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5554 | alg ); |
| 5555 | if( status != PSA_SUCCESS ) |
| 5556 | return( status ); |
| 5557 | |
| 5558 | continue; |
| 5559 | } |
| 5560 | |
| 5561 | if( tls12_prf->left_in_block > output_length ) |
| 5562 | length = (uint8_t) output_length; |
| 5563 | else |
| 5564 | length = tls12_prf->left_in_block; |
| 5565 | |
| 5566 | offset = hash_length - tls12_prf->left_in_block; |
| 5567 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5568 | output += length; |
| 5569 | output_length -= length; |
| 5570 | tls12_prf->left_in_block -= length; |
| 5571 | } |
| 5572 | |
| 5573 | return( PSA_SUCCESS ); |
| 5574 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5575 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5576 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5577 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5578 | psa_status_t psa_key_derivation_output_bytes( |
| 5579 | psa_key_derivation_operation_t *operation, |
| 5580 | uint8_t *output, |
| 5581 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5582 | { |
| 5583 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5584 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5585 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5586 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5587 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5588 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5589 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5592 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5593 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5594 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5595 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5596 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5597 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5598 | goto exit; |
| 5599 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5600 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5601 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5602 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5603 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5604 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5605 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5606 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5607 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5608 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5609 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5610 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5611 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5612 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5613 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5614 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5615 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5616 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5617 | output, output_length ); |
| 5618 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5619 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5620 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5621 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5622 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5623 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5624 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5625 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5626 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5627 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5628 | output_length ); |
| 5629 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5630 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5631 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5632 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5633 | { |
| 5634 | return( PSA_ERROR_BAD_STATE ); |
| 5635 | } |
| 5636 | |
| 5637 | exit: |
| 5638 | if( status != PSA_SUCCESS ) |
| 5639 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5640 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5641 | * This allows us to differentiate between exhausted operations and |
| 5642 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5643 | * operations. */ |
| 5644 | psa_algorithm_t alg = operation->alg; |
| 5645 | psa_key_derivation_abort( operation ); |
| 5646 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5647 | memset( output, '!', output_length ); |
| 5648 | } |
| 5649 | return( status ); |
| 5650 | } |
| 5651 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5652 | #if defined(MBEDTLS_DES_C) |
| 5653 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5654 | { |
| 5655 | if( data_size >= 8 ) |
| 5656 | mbedtls_des_key_set_parity( data ); |
| 5657 | if( data_size >= 16 ) |
| 5658 | mbedtls_des_key_set_parity( data + 8 ); |
| 5659 | if( data_size >= 24 ) |
| 5660 | mbedtls_des_key_set_parity( data + 16 ); |
| 5661 | } |
| 5662 | #endif /* MBEDTLS_DES_C */ |
| 5663 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5664 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5665 | psa_key_slot_t *slot, |
| 5666 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5667 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5668 | { |
| 5669 | uint8_t *data = NULL; |
| 5670 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5671 | psa_status_t status; |
| 5672 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5673 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5674 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5675 | if( bits % 8 != 0 ) |
| 5676 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5677 | data = mbedtls_calloc( 1, bytes ); |
| 5678 | if( data == NULL ) |
| 5679 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5680 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5681 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5682 | if( status != PSA_SUCCESS ) |
| 5683 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5684 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5685 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5686 | psa_des_set_key_parity( data, bytes ); |
| 5687 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5688 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5689 | |
| 5690 | exit: |
| 5691 | mbedtls_free( data ); |
| 5692 | return( status ); |
| 5693 | } |
| 5694 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5695 | 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] | 5696 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5697 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5698 | { |
| 5699 | psa_status_t status; |
| 5700 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5701 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5702 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5703 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5704 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5705 | /* Reject any attempt to create a zero-length key so that we don't |
| 5706 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5707 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5708 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5709 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5710 | if( ! operation->can_output_key ) |
| 5711 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5712 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5713 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5714 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5715 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5716 | if( driver != NULL ) |
| 5717 | { |
| 5718 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5719 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5720 | } |
| 5721 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5722 | if( status == PSA_SUCCESS ) |
| 5723 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5724 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5725 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5726 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5727 | } |
| 5728 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5729 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5730 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5731 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5732 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5733 | return( status ); |
| 5734 | } |
| 5735 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5736 | |
| 5737 | |
| 5738 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5739 | /* Key derivation */ |
| 5740 | /****************************************************************/ |
| 5741 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5742 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5743 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5744 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5745 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5746 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5747 | int is_kdf_alg_supported; |
| 5748 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5749 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5750 | * initialisers for this union leave some bytes unspecified.) */ |
| 5751 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5752 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5753 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5754 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5755 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5756 | is_kdf_alg_supported = 1; |
| 5757 | else |
| 5758 | #endif |
| 5759 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5760 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5761 | is_kdf_alg_supported = 1; |
| 5762 | else |
| 5763 | #endif |
| 5764 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5765 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5766 | is_kdf_alg_supported = 1; |
| 5767 | else |
| 5768 | #endif |
| 5769 | is_kdf_alg_supported = 0; |
| 5770 | |
| 5771 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5772 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5773 | 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] | 5774 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5775 | if( hash_size == 0 ) |
| 5776 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5777 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5778 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5779 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5780 | { |
| 5781 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5782 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5783 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5784 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5785 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5786 | |
| 5787 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5788 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5789 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5791 | 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] | 5792 | psa_algorithm_t alg ) |
| 5793 | { |
| 5794 | psa_status_t status; |
| 5795 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5796 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5797 | return( PSA_ERROR_BAD_STATE ); |
| 5798 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5799 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5800 | return( PSA_ERROR_INVALID_ARGUMENT ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5801 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5802 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5803 | { |
| 5804 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5805 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5806 | } |
| 5807 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5808 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5809 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5810 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5811 | #endif |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5812 | else |
| 5813 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5814 | |
| 5815 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5816 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5817 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5818 | } |
| 5819 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5820 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5821 | 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] | 5822 | psa_algorithm_t hash_alg, |
| 5823 | psa_key_derivation_step_t step, |
| 5824 | const uint8_t *data, |
| 5825 | size_t data_length ) |
| 5826 | { |
| 5827 | psa_status_t status; |
| 5828 | switch( step ) |
| 5829 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5830 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5831 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5832 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5833 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5834 | data, data_length, |
| 5835 | hash_alg ); |
| 5836 | if( status != PSA_SUCCESS ) |
| 5837 | return( status ); |
| 5838 | hkdf->state = HKDF_STATE_STARTED; |
| 5839 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5840 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5841 | /* If no salt was provided, use an empty salt. */ |
| 5842 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5843 | { |
| 5844 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5845 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5846 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5847 | if( status != PSA_SUCCESS ) |
| 5848 | return( status ); |
| 5849 | hkdf->state = HKDF_STATE_STARTED; |
| 5850 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5851 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5852 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5853 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5854 | data, data_length ); |
| 5855 | if( status != PSA_SUCCESS ) |
| 5856 | return( status ); |
| 5857 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5858 | hkdf->prk, |
| 5859 | sizeof( hkdf->prk ) ); |
| 5860 | if( status != PSA_SUCCESS ) |
| 5861 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5862 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5863 | hkdf->block_number = 0; |
| 5864 | hkdf->state = HKDF_STATE_KEYED; |
| 5865 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5866 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5867 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5868 | return( PSA_ERROR_BAD_STATE ); |
| 5869 | if( hkdf->info_set ) |
| 5870 | return( PSA_ERROR_BAD_STATE ); |
| 5871 | hkdf->info_length = data_length; |
| 5872 | if( data_length != 0 ) |
| 5873 | { |
| 5874 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5875 | if( hkdf->info == NULL ) |
| 5876 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5877 | memcpy( hkdf->info, data, data_length ); |
| 5878 | } |
| 5879 | hkdf->info_set = 1; |
| 5880 | return( PSA_SUCCESS ); |
| 5881 | default: |
| 5882 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5883 | } |
| 5884 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5885 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5886 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5887 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5888 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5889 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5890 | const uint8_t *data, |
| 5891 | size_t data_length ) |
| 5892 | { |
| 5893 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5894 | return( PSA_ERROR_BAD_STATE ); |
| 5895 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5896 | if( data_length != 0 ) |
| 5897 | { |
| 5898 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5899 | if( prf->seed == NULL ) |
| 5900 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5901 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5902 | memcpy( prf->seed, data, data_length ); |
| 5903 | prf->seed_length = data_length; |
| 5904 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5905 | |
| 5906 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5907 | |
| 5908 | return( PSA_SUCCESS ); |
| 5909 | } |
| 5910 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5911 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5912 | psa_algorithm_t hash_alg, |
| 5913 | const uint8_t *data, |
| 5914 | size_t data_length ) |
| 5915 | { |
| 5916 | psa_status_t status; |
| 5917 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5918 | return( PSA_ERROR_BAD_STATE ); |
| 5919 | |
| 5920 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5921 | if( status != PSA_SUCCESS ) |
| 5922 | return( status ); |
| 5923 | |
| 5924 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5925 | |
| 5926 | return( PSA_SUCCESS ); |
| 5927 | } |
| 5928 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5929 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5930 | const uint8_t *data, |
| 5931 | size_t data_length ) |
| 5932 | { |
| 5933 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5934 | return( PSA_ERROR_BAD_STATE ); |
| 5935 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5936 | if( data_length != 0 ) |
| 5937 | { |
| 5938 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5939 | if( prf->label == NULL ) |
| 5940 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5941 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5942 | memcpy( prf->label, data, data_length ); |
| 5943 | prf->label_length = data_length; |
| 5944 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5945 | |
| 5946 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5947 | |
| 5948 | return( PSA_SUCCESS ); |
| 5949 | } |
| 5950 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5951 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5952 | psa_algorithm_t hash_alg, |
| 5953 | psa_key_derivation_step_t step, |
| 5954 | const uint8_t *data, |
| 5955 | size_t data_length ) |
| 5956 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5957 | switch( step ) |
| 5958 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5959 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5960 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5961 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5962 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5963 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5964 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5965 | default: |
| 5966 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5967 | } |
| 5968 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5969 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5970 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5971 | |
| 5972 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5973 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5974 | psa_tls12_prf_key_derivation_t *prf, |
| 5975 | psa_algorithm_t hash_alg, |
| 5976 | const uint8_t *data, |
| 5977 | size_t data_length ) |
| 5978 | { |
| 5979 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5980 | 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] | 5981 | uint8_t *cur = pms; |
| 5982 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5983 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5984 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5985 | |
| 5986 | /* Quoting RFC 4279, Section 2: |
| 5987 | * |
| 5988 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5989 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5990 | * uint16 with the value N, and the PSK itself. |
| 5991 | */ |
| 5992 | |
| 5993 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5994 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5995 | memset( cur, 0, data_length ); |
| 5996 | cur += data_length; |
| 5997 | *cur++ = pms[0]; |
| 5998 | *cur++ = pms[1]; |
| 5999 | memcpy( cur, data, data_length ); |
| 6000 | cur += data_length; |
| 6001 | |
| 6002 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 6003 | |
| 6004 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 6005 | return( status ); |
| 6006 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6007 | |
| 6008 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6009 | psa_tls12_prf_key_derivation_t *prf, |
| 6010 | psa_algorithm_t hash_alg, |
| 6011 | psa_key_derivation_step_t step, |
| 6012 | const uint8_t *data, |
| 6013 | size_t data_length ) |
| 6014 | { |
| 6015 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 6016 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6017 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 6018 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 6019 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6020 | |
| 6021 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 6022 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6023 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6024 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6025 | /** Check whether the given key type is acceptable for the given |
| 6026 | * input step of a key derivation. |
| 6027 | * |
| 6028 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6029 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6030 | * Both secret and non-secret inputs can alternatively have the type |
| 6031 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6032 | * that the input was passed as a buffer rather than via a key object. |
| 6033 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6034 | static int psa_key_derivation_check_input_type( |
| 6035 | psa_key_derivation_step_t step, |
| 6036 | psa_key_type_t key_type ) |
| 6037 | { |
| 6038 | switch( step ) |
| 6039 | { |
| 6040 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6041 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 6042 | return( PSA_SUCCESS ); |
| 6043 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6044 | return( PSA_SUCCESS ); |
| 6045 | break; |
| 6046 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6047 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6048 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6049 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6050 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 6051 | return( PSA_SUCCESS ); |
| 6052 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6053 | return( PSA_SUCCESS ); |
| 6054 | break; |
| 6055 | } |
| 6056 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6057 | } |
| 6058 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6059 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6060 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6061 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6062 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6063 | const uint8_t *data, |
| 6064 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6065 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6066 | psa_status_t status; |
| 6067 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 6068 | |
| 6069 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6070 | if( status != PSA_SUCCESS ) |
| 6071 | goto exit; |
| 6072 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 6073 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6074 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6075 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6076 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6077 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6078 | step, data, data_length ); |
| 6079 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6080 | else |
| 6081 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 6082 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 6083 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6084 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6085 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 6086 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6087 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6088 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6089 | else |
| 6090 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6091 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6092 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6093 | { |
| 6094 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 6095 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6096 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6097 | } |
| 6098 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6099 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6100 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6101 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6102 | return( PSA_ERROR_BAD_STATE ); |
| 6103 | } |
| 6104 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6105 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6106 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6107 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6108 | return( status ); |
| 6109 | } |
| 6110 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6111 | psa_status_t psa_key_derivation_input_bytes( |
| 6112 | psa_key_derivation_operation_t *operation, |
| 6113 | psa_key_derivation_step_t step, |
| 6114 | const uint8_t *data, |
| 6115 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6116 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6117 | return( psa_key_derivation_input_internal( operation, step, |
| 6118 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 6119 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6120 | } |
| 6121 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6122 | psa_status_t psa_key_derivation_input_key( |
| 6123 | psa_key_derivation_operation_t *operation, |
| 6124 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6125 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6126 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6127 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6128 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6129 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6130 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6131 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6132 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6133 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6134 | { |
| 6135 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6136 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6137 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6138 | |
| 6139 | /* Passing a key object as a SECRET input unlocks the permission |
| 6140 | * to output to a key object. */ |
| 6141 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6142 | operation->can_output_key = 1; |
| 6143 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6144 | status = psa_key_derivation_input_internal( operation, |
| 6145 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6146 | slot->key.data, |
| 6147 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6148 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6149 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6150 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6151 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6152 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6153 | |
| 6154 | |
| 6155 | |
| 6156 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6157 | /* Key agreement */ |
| 6158 | /****************************************************************/ |
| 6159 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6160 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6161 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 6162 | size_t peer_key_length, |
| 6163 | const mbedtls_ecp_keypair *our_key, |
| 6164 | uint8_t *shared_secret, |
| 6165 | size_t shared_secret_size, |
| 6166 | size_t *shared_secret_length ) |
| 6167 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 6168 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6169 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6170 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6171 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6172 | 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] | 6173 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6174 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6175 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 6176 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6177 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6178 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6179 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6180 | goto exit; |
| 6181 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6182 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6183 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6184 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6185 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6186 | status = mbedtls_to_psa_error( |
| 6187 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 6188 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6189 | goto exit; |
| 6190 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6191 | status = mbedtls_to_psa_error( |
| 6192 | mbedtls_ecdh_calc_secret( &ecdh, |
| 6193 | shared_secret_length, |
| 6194 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6195 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6196 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6197 | if( status != PSA_SUCCESS ) |
| 6198 | goto exit; |
| 6199 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 6200 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6201 | |
| 6202 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 6203 | if( status != PSA_SUCCESS ) |
| 6204 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6205 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6206 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6207 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6208 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6209 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6210 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6211 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6212 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6213 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6214 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6215 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6216 | psa_key_slot_t *private_key, |
| 6217 | const uint8_t *peer_key, |
| 6218 | size_t peer_key_length, |
| 6219 | uint8_t *shared_secret, |
| 6220 | size_t shared_secret_size, |
| 6221 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6222 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6223 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6224 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6225 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6226 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6227 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6228 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6229 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6230 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6231 | private_key->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6232 | private_key->key.data, |
| 6233 | private_key->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6234 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6235 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6236 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6237 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6238 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6239 | shared_secret, shared_secret_size, |
| 6240 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6241 | mbedtls_ecp_keypair_free( ecp ); |
| 6242 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6243 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6244 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6245 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6246 | (void) private_key; |
| 6247 | (void) peer_key; |
| 6248 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6249 | (void) shared_secret; |
| 6250 | (void) shared_secret_size; |
| 6251 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6252 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6253 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6254 | } |
| 6255 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6256 | /* 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] | 6257 | * to potentially free embedded data structures and wipe confidential data. |
| 6258 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6259 | 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] | 6260 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6261 | psa_key_slot_t *private_key, |
| 6262 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6263 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6264 | { |
| 6265 | psa_status_t status; |
| 6266 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6267 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6268 | 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] | 6269 | |
| 6270 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6271 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6272 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6273 | private_key, |
| 6274 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6275 | shared_secret, |
| 6276 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6277 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6278 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6279 | goto exit; |
| 6280 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6281 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6282 | * the shared secret. A shared secret is permitted wherever a key |
| 6283 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6284 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6285 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6286 | shared_secret, |
| 6287 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6288 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6289 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6290 | return( status ); |
| 6291 | } |
| 6292 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6293 | 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] | 6294 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6295 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6296 | const uint8_t *peer_key, |
| 6297 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6298 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6299 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6300 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6301 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6302 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6303 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6304 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6305 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6306 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6307 | if( status != PSA_SUCCESS ) |
| 6308 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6309 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6310 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6311 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6312 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6313 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6314 | else |
| 6315 | { |
| 6316 | /* If a private key has been added as SECRET, we allow the derived |
| 6317 | * key material to be used as a key in PSA Crypto. */ |
| 6318 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6319 | operation->can_output_key = 1; |
| 6320 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6321 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6322 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6323 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6324 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6325 | } |
| 6326 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6327 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6328 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6329 | const uint8_t *peer_key, |
| 6330 | size_t peer_key_length, |
| 6331 | uint8_t *output, |
| 6332 | size_t output_size, |
| 6333 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6334 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6335 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6336 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6337 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6338 | |
| 6339 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6340 | { |
| 6341 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6342 | goto exit; |
| 6343 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6344 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6345 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6346 | if( status != PSA_SUCCESS ) |
| 6347 | goto exit; |
| 6348 | |
| 6349 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6350 | peer_key, peer_key_length, |
| 6351 | output, output_size, |
| 6352 | output_length ); |
| 6353 | |
| 6354 | exit: |
| 6355 | if( status != PSA_SUCCESS ) |
| 6356 | { |
| 6357 | /* If an error happens and is not handled properly, the output |
| 6358 | * may be used as a key to protect sensitive data. Arrange for such |
| 6359 | * a key to be random, which is likely to result in decryption or |
| 6360 | * verification errors. This is better than filling the buffer with |
| 6361 | * some constant data such as zeros, which would result in the data |
| 6362 | * being protected with a reproducible, easily knowable key. |
| 6363 | */ |
| 6364 | psa_generate_random( output, output_size ); |
| 6365 | *output_length = output_size; |
| 6366 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6367 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6368 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6369 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6370 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6371 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6372 | |
| 6373 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6374 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6375 | /****************************************************************/ |
| 6376 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6377 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6378 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6379 | /** Initialize the PSA random generator. |
| 6380 | */ |
| 6381 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6382 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6383 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6384 | memset( rng, 0, sizeof( *rng ) ); |
| 6385 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6386 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6387 | /* Set default configuration if |
| 6388 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6389 | if( rng->entropy_init == NULL ) |
| 6390 | rng->entropy_init = mbedtls_entropy_init; |
| 6391 | if( rng->entropy_free == NULL ) |
| 6392 | rng->entropy_free = mbedtls_entropy_free; |
| 6393 | |
| 6394 | rng->entropy_init( &rng->entropy ); |
| 6395 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6396 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6397 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6398 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6399 | mbedtls_entropy_add_source( &rng->entropy, |
| 6400 | mbedtls_nv_seed_poll, NULL, |
| 6401 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6402 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6403 | #endif |
| 6404 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6405 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6406 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | /** Deinitialize the PSA random generator. |
| 6410 | */ |
| 6411 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6412 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6413 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6414 | memset( rng, 0, sizeof( *rng ) ); |
| 6415 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6416 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6417 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6418 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6419 | } |
| 6420 | |
| 6421 | /** Seed the PSA random generator. |
| 6422 | */ |
| 6423 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6424 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6425 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6426 | /* Do nothing: the external RNG seeds itself. */ |
| 6427 | (void) rng; |
| 6428 | return( PSA_SUCCESS ); |
| 6429 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6430 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6431 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6432 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6433 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6434 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6435 | } |
| 6436 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6437 | psa_status_t psa_generate_random( uint8_t *output, |
| 6438 | size_t output_size ) |
| 6439 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6440 | GUARD_MODULE_INITIALIZED; |
| 6441 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6442 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6443 | |
| 6444 | size_t output_length = 0; |
| 6445 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6446 | output, output_size, |
| 6447 | &output_length ); |
| 6448 | if( status != PSA_SUCCESS ) |
| 6449 | return( status ); |
| 6450 | /* Breaking up a request into smaller chunks is currently not supported |
| 6451 | * for the extrernal RNG interface. */ |
| 6452 | if( output_length != output_size ) |
| 6453 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6454 | return( PSA_SUCCESS ); |
| 6455 | |
| 6456 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6457 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6458 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6459 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6460 | size_t request_size = |
| 6461 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6462 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6463 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6464 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6465 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6466 | if( ret != 0 ) |
| 6467 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6468 | output_size -= request_size; |
| 6469 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6470 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6471 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6472 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6473 | } |
| 6474 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6475 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6476 | * |
| 6477 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6478 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6479 | * PSA API doesn't support passing an explicit state. |
| 6480 | * |
| 6481 | * In the non-external case, psa_generate_random() calls an |
| 6482 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6483 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6484 | * instead of doing this back-and-forth between the PSA API and the |
| 6485 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6486 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6487 | */ |
| 6488 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6489 | int mbedtls_psa_get_random( void *p_rng, |
| 6490 | unsigned char *output, |
| 6491 | size_t output_size ) |
| 6492 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6493 | /* This function takes a pointer to the RNG state because that's what |
| 6494 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6495 | * its own state internally and doesn't let the caller access that state. |
| 6496 | * So we just ignore the state parameter, and in practice we'll pass |
| 6497 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6498 | (void) p_rng; |
| 6499 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6500 | if( status == PSA_SUCCESS ) |
| 6501 | return( 0 ); |
| 6502 | else |
| 6503 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6504 | } |
| 6505 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6506 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6507 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6508 | #include "mbedtls/entropy_poll.h" |
| 6509 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6510 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6511 | size_t seed_size ) |
| 6512 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6513 | if( global_data.initialized ) |
| 6514 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6515 | |
| 6516 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6517 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6518 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6519 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6520 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6521 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6522 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6523 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6524 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6525 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6526 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6527 | size_t domain_parameters_size, |
| 6528 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6529 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6530 | size_t i; |
| 6531 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6532 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6533 | if( domain_parameters_size == 0 ) |
| 6534 | { |
| 6535 | *exponent = 65537; |
| 6536 | return( PSA_SUCCESS ); |
| 6537 | } |
| 6538 | |
| 6539 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6540 | * support values that fit in a 32-bit integer, which is larger than |
| 6541 | * int on just about every platform anyway. */ |
| 6542 | if( domain_parameters_size > sizeof( acc ) ) |
| 6543 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6544 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6545 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6546 | if( acc > INT_MAX ) |
| 6547 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6548 | *exponent = acc; |
| 6549 | return( PSA_SUCCESS ); |
| 6550 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6551 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6552 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6553 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6554 | psa_key_slot_t *slot, size_t bits, |
| 6555 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6556 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6557 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6558 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6559 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6560 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6561 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6562 | if( key_type_is_raw_bytes( type ) ) |
| 6563 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6564 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6565 | |
| 6566 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6567 | if( status != PSA_SUCCESS ) |
| 6568 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6569 | |
| 6570 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6571 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6572 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6573 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6574 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6575 | status = psa_generate_random( slot->key.data, |
| 6576 | slot->key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6577 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6578 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6579 | |
| 6580 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6581 | #if defined(MBEDTLS_DES_C) |
| 6582 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6583 | psa_des_set_key_parity( slot->key.data, |
| 6584 | slot->key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6585 | #endif /* MBEDTLS_DES_C */ |
| 6586 | } |
| 6587 | else |
| 6588 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6589 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6590 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6591 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6592 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6593 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6594 | int exponent; |
| 6595 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6596 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6597 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6598 | /* Accept only byte-aligned keys, for the same reasons as |
| 6599 | * in psa_import_rsa_key(). */ |
| 6600 | if( bits % 8 != 0 ) |
| 6601 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6602 | status = psa_read_rsa_exponent( domain_parameters, |
| 6603 | domain_parameters_size, |
| 6604 | &exponent ); |
| 6605 | if( status != PSA_SUCCESS ) |
| 6606 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6607 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6608 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6609 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6610 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6611 | (unsigned int) bits, |
| 6612 | exponent ); |
| 6613 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6614 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6615 | |
| 6616 | /* Make sure to always have an export representation available */ |
| 6617 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6618 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6619 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6620 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6621 | { |
| 6622 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6623 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6624 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6625 | |
| 6626 | status = psa_export_rsa_key( type, |
| 6627 | &rsa, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6628 | slot->key.data, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6629 | bytes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6630 | &slot->key.bytes ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6631 | mbedtls_rsa_free( &rsa ); |
| 6632 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6633 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6634 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6635 | } |
| 6636 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6637 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6638 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6639 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6640 | 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] | 6641 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6642 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6643 | mbedtls_ecp_group_id grp_id = |
| 6644 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6645 | const mbedtls_ecp_curve_info *curve_info = |
| 6646 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6647 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6648 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6649 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6650 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6651 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6652 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6653 | mbedtls_ecp_keypair_init( &ecp ); |
| 6654 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6655 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6656 | MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6657 | if( ret != 0 ) |
| 6658 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6659 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6660 | return( mbedtls_to_psa_error( ret ) ); |
| 6661 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6662 | |
| 6663 | |
| 6664 | /* Make sure to always have an export representation available */ |
| 6665 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6666 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6667 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6668 | { |
| 6669 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6670 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6671 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6672 | |
| 6673 | status = mbedtls_to_psa_error( |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6674 | mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6675 | |
| 6676 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6677 | if( status != PSA_SUCCESS ) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6678 | memset( slot->key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6679 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6680 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6681 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6682 | } |
| 6683 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6684 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6685 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6686 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6687 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6688 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6689 | return( PSA_SUCCESS ); |
| 6690 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6691 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6692 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6693 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6694 | { |
| 6695 | psa_status_t status; |
| 6696 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6697 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6698 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6699 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6700 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6701 | /* Reject any attempt to create a zero-length key so that we don't |
| 6702 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6703 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6704 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6705 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6706 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6707 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6708 | if( status != PSA_SUCCESS ) |
| 6709 | goto exit; |
| 6710 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6711 | status = psa_driver_wrapper_generate_key( attributes, |
| 6712 | slot ); |
| 6713 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6714 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6715 | goto exit; |
| 6716 | |
| 6717 | status = psa_generate_key_internal( |
| 6718 | slot, attributes->core.bits, |
| 6719 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6720 | |
| 6721 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6722 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6723 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6724 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6725 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6726 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6727 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6728 | } |
| 6729 | |
| 6730 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6731 | |
| 6732 | /****************************************************************/ |
| 6733 | /* Module setup */ |
| 6734 | /****************************************************************/ |
| 6735 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6736 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6737 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6738 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6739 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6740 | { |
| 6741 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6742 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6743 | global_data.rng.entropy_init = entropy_init; |
| 6744 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6745 | return( PSA_SUCCESS ); |
| 6746 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6747 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6748 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6749 | void mbedtls_psa_crypto_free( void ) |
| 6750 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6751 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6752 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6753 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6754 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6755 | } |
| 6756 | /* Wipe all remaining data, including configuration. |
| 6757 | * In particular, this sets all state indicator to the value |
| 6758 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6759 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6760 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6761 | /* Unregister all secure element drivers, so that we restart from |
| 6762 | * a pristine state. */ |
| 6763 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6764 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6765 | } |
| 6766 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6767 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6768 | /** Recover a transaction that was interrupted by a power failure. |
| 6769 | * |
| 6770 | * This function is called during initialization, before psa_crypto_init() |
| 6771 | * returns. If this function returns a failure status, the initialization |
| 6772 | * fails. |
| 6773 | */ |
| 6774 | static psa_status_t psa_crypto_recover_transaction( |
| 6775 | const psa_crypto_transaction_t *transaction ) |
| 6776 | { |
| 6777 | switch( transaction->unknown.type ) |
| 6778 | { |
| 6779 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6780 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6781 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6782 | * is implemented. |
| 6783 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6784 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6785 | default: |
| 6786 | /* We found an unsupported transaction in the storage. |
| 6787 | * We don't know what state the storage is in. Give up. */ |
| 6788 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6789 | } |
| 6790 | } |
| 6791 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6792 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6793 | psa_status_t psa_crypto_init( void ) |
| 6794 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6795 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6796 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6797 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6798 | if( global_data.initialized != 0 ) |
| 6799 | return( PSA_SUCCESS ); |
| 6800 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6801 | /* Initialize and seed the random generator. */ |
| 6802 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6803 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6804 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6805 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6806 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6807 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6808 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6809 | status = psa_initialize_key_slots( ); |
| 6810 | if( status != PSA_SUCCESS ) |
| 6811 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6812 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6813 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6814 | status = psa_init_all_se_drivers( ); |
| 6815 | if( status != PSA_SUCCESS ) |
| 6816 | goto exit; |
| 6817 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6818 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6819 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6820 | status = psa_crypto_load_transaction( ); |
| 6821 | if( status == PSA_SUCCESS ) |
| 6822 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6823 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6824 | if( status != PSA_SUCCESS ) |
| 6825 | goto exit; |
| 6826 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6827 | } |
| 6828 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6829 | { |
| 6830 | /* There's no transaction to complete. It's all good. */ |
| 6831 | status = PSA_SUCCESS; |
| 6832 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6833 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6834 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6835 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6836 | global_data.initialized = 1; |
| 6837 | |
| 6838 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6839 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6840 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6841 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |