Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 29 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | #include "psa/crypto.h" |
| 31 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 33 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 35 | #include "psa_crypto_ecp.h" |
| 36 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 37 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 39 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 40 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 41 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 42 | /* Include internal declarations that are useful for implementing persistently |
| 43 | * stored keys. */ |
| 44 | #include "psa_crypto_storage.h" |
| 45 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 46 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 48 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 49 | #include <stdlib.h> |
| 50 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 51 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 52 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 53 | #define mbedtls_calloc calloc |
| 54 | #define mbedtls_free free |
| 55 | #endif |
| 56 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 57 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 58 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 59 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 60 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 61 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 62 | #include "mbedtls/blowfish.h" |
| 63 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 64 | #include "mbedtls/chacha20.h" |
| 65 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/cipher.h" |
| 67 | #include "mbedtls/ccm.h" |
| 68 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 69 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 70 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 71 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 72 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 73 | #include "mbedtls/error.h" |
| 74 | #include "mbedtls/gcm.h" |
| 75 | #include "mbedtls/md2.h" |
| 76 | #include "mbedtls/md4.h" |
| 77 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 78 | #include "mbedtls/md.h" |
| 79 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 80 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 81 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 82 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 83 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 84 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 85 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 86 | #include "mbedtls/sha1.h" |
| 87 | #include "mbedtls/sha256.h" |
| 88 | #include "mbedtls/sha512.h" |
| 89 | #include "mbedtls/xtea.h" |
| 90 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 91 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 92 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 93 | /* constant-time buffer comparison */ |
| 94 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 95 | { |
| 96 | size_t i; |
| 97 | unsigned char diff = 0; |
| 98 | |
| 99 | for( i = 0; i < n; i++ ) |
| 100 | diff |= a[i] ^ b[i]; |
| 101 | |
| 102 | return( diff ); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 107 | /****************************************************************/ |
| 108 | /* Global data, support functions and library management */ |
| 109 | /****************************************************************/ |
| 110 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 111 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 112 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 113 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 116 | /* Values for psa_global_data_t::rng_state */ |
| 117 | #define RNG_NOT_INITIALIZED 0 |
| 118 | #define RNG_INITIALIZED 1 |
| 119 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 120 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 121 | typedef struct |
| 122 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 123 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 124 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 125 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 126 | } psa_global_data_t; |
| 127 | |
| 128 | static psa_global_data_t global_data; |
| 129 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 130 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 131 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 132 | &global_data.rng.drbg; |
| 133 | #endif |
| 134 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 135 | #define GUARD_MODULE_INITIALIZED \ |
| 136 | if( global_data.initialized == 0 ) \ |
| 137 | return( PSA_ERROR_BAD_STATE ); |
| 138 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 139 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 140 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 141 | /* Mbed TLS error codes can combine a high-level error code and a |
| 142 | * low-level error code. The low-level error usually reflects the |
| 143 | * root cause better, so dispatch on that preferably. */ |
| 144 | int low_level_ret = - ( -ret & 0x007f ); |
| 145 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 146 | { |
| 147 | case 0: |
| 148 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 149 | |
| 150 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 151 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 152 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 153 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 154 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 155 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 156 | |
| 157 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 158 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 159 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 160 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 161 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 162 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 163 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 164 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 165 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 166 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 167 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 168 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 169 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 170 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 172 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 174 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 175 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 177 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 178 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 179 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 180 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 181 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 182 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 183 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 184 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 185 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 186 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 187 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 188 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 189 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 190 | |
| 191 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 192 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 193 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 194 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 195 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 196 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 197 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 198 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 200 | |
| 201 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 202 | return( PSA_ERROR_BAD_STATE ); |
| 203 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 204 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 205 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 206 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 207 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 208 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 209 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 210 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 211 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 212 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 213 | return( PSA_ERROR_INVALID_PADDING ); |
| 214 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 215 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 217 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 218 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 219 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 220 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 221 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 222 | |
| 223 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 224 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 225 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 226 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 227 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 228 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 229 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 231 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 232 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 233 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 234 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 235 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 236 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 237 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 238 | |
| 239 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 240 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 241 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 242 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 243 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 244 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 245 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 246 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 247 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 248 | |
| 249 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 250 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 251 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 252 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 253 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 254 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 255 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 256 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 257 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 258 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 259 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 260 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 261 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 262 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 263 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 264 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 265 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 266 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 267 | #endif |
| 268 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 269 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 270 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 271 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 272 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 273 | |
| 274 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 275 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 276 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 278 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 279 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 280 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 281 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 282 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 283 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 284 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 285 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 286 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 287 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 289 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 292 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 293 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 294 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 295 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 296 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 297 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 298 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 299 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 300 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 301 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 302 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 303 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 304 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 305 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 307 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 308 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 309 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 310 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 311 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 312 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 313 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 314 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 315 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 316 | return( PSA_ERROR_NOT_PERMITTED ); |
| 317 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 318 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 319 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 320 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 321 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 322 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 323 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 324 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 325 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 326 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 327 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 328 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 332 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 333 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 334 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 335 | |
| 336 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 337 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 338 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 339 | return( PSA_ERROR_INVALID_PADDING ); |
| 340 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 341 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 342 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 343 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 344 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 345 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 346 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 347 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 348 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 349 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 350 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 351 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 352 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 353 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 354 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 355 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 356 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 357 | |
| 358 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 359 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 360 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 361 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 362 | |
| 363 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 364 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 365 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 366 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 367 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 368 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 369 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 370 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 371 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 372 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 373 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 374 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 375 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 376 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 377 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 378 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 379 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 380 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 381 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 382 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 383 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 384 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 385 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 386 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 387 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 388 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 389 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 393 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 394 | |
| 395 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 396 | /****************************************************************/ |
| 397 | /* Key management */ |
| 398 | /****************************************************************/ |
| 399 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 400 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 401 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 402 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 403 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 404 | } |
| 405 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 406 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 407 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 408 | * current test driver in key_management.c is using this function |
| 409 | * when accelerators are used for ECC key pair and public key. |
| 410 | * Once that dependency is resolved these guards can be removed. |
| 411 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 412 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 413 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 414 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 415 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 416 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 417 | size_t bits, |
| 418 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 419 | { |
| 420 | switch( curve ) |
| 421 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 422 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 423 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 425 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 427 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 428 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 429 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 430 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 431 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 432 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 433 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 434 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 435 | case 528: |
| 436 | if( bits_is_sloppy ) |
| 437 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 438 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 439 | } |
| 440 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 441 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 442 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 443 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 444 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 445 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 446 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 447 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 448 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 449 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 450 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 451 | } |
| 452 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 453 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 454 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 455 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 456 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 457 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 458 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 459 | case 256: |
| 460 | if( bits_is_sloppy ) |
| 461 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 462 | break; |
| 463 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 464 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 465 | } |
| 466 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 467 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 468 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 469 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 470 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 471 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 472 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 473 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 474 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 475 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 476 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 477 | } |
| 478 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 479 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 480 | |
| 481 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 482 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 483 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 484 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 485 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 486 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 487 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 488 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 489 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 490 | { |
| 491 | /* Check that the bit size is acceptable for the key type */ |
| 492 | switch( type ) |
| 493 | { |
| 494 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 496 | case PSA_KEY_TYPE_DERIVE: |
| 497 | break; |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 498 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 499 | case PSA_KEY_TYPE_AES: |
| 500 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 501 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 502 | break; |
| 503 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 504 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 505 | case PSA_KEY_TYPE_CAMELLIA: |
| 506 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 507 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 508 | break; |
| 509 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 510 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 511 | case PSA_KEY_TYPE_DES: |
| 512 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 513 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 514 | break; |
| 515 | #endif |
David Brown | e04acc2 | 2021-01-26 11:39:16 -0700 | [diff] [blame] | 516 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 517 | case PSA_KEY_TYPE_ARC4: |
| 518 | if( bits < 8 || bits > 2048 ) |
| 519 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 520 | break; |
| 521 | #endif |
David Brown | 1bfe4d7 | 2021-02-16 12:54:35 -0700 | [diff] [blame] | 522 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 523 | case PSA_KEY_TYPE_CHACHA20: |
| 524 | if( bits != 256 ) |
| 525 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 526 | break; |
| 527 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 528 | default: |
| 529 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 530 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 531 | if( bits % 8 != 0 ) |
| 532 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 533 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 534 | return( PSA_SUCCESS ); |
| 535 | } |
| 536 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 537 | /** Return the size of the key in the given slot, in bits. |
| 538 | * |
| 539 | * \param[in] slot A key slot. |
| 540 | * |
| 541 | * \return The key size in bits, read from the metadata in the slot. |
| 542 | */ |
| 543 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 544 | { |
| 545 | return( slot->attr.bits ); |
| 546 | } |
| 547 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 548 | /** Return the output MAC length of a MAC algorithm, in bytes |
| 549 | * |
Steven Cooreman | 7d4b0d7 | 2021-03-02 21:29:45 +0100 | [diff] [blame] | 550 | * \param[in] algorithm The specific (non-wildcard) MAC algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 551 | * \param[in] key_type The key type of the key to be used with the |
| 552 | * \p algorithm. |
| 553 | * \param[out] length The calculated output length of the given MAC |
| 554 | * \p algorithm when used with a key corresponding to |
| 555 | * the given \p key_type |
| 556 | * |
| 557 | * \retval #PSA_SUCCESS |
| 558 | * The \p length has been successfully calculated |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 559 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 7d4b0d7 | 2021-03-02 21:29:45 +0100 | [diff] [blame] | 560 | * \p algorithm is not a valid, specific MAC algorithm recognized and |
| 561 | * supported by this core, or \p key_type describes a key which is |
| 562 | * inconsistent with the specified \p algorithm. |
| 563 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 564 | * \p algorithm tries to truncate the MAC to a size which would be |
| 565 | * larger than the underlying algorithm's maximum output length. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 566 | */ |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 567 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_get_mac_output_length( |
| 568 | psa_algorithm_t algorithm, |
| 569 | psa_key_type_t key_type, |
| 570 | size_t *length ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 571 | { |
Steven Cooreman | 7d4b0d7 | 2021-03-02 21:29:45 +0100 | [diff] [blame] | 572 | /* Get the default length for the algorithm and key combination. None of the |
| 573 | * currently supported algorithms have a default output length dependent on |
| 574 | * key size, so setting it to a bogus value is OK. */ |
| 575 | size_t default_length = PSA_MAC_LENGTH( key_type, 0, |
| 576 | PSA_ALG_FULL_LENGTH_MAC( algorithm ) ); |
| 577 | |
| 578 | /* PSA_MAC_LENGTH, when called on a full-length algorithm identifier, can |
| 579 | * currently return either 0 (unknown algorithm) or 1 (cipher-MAC with |
| 580 | * stream cipher) in cases where the key type / algorithm combination would |
| 581 | * be invalid. */ |
| 582 | if( default_length == 0 || default_length == 1 ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 583 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 584 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 585 | /* Output the expected (potentially truncated) length as long as it can |
Steven Cooreman | 4ff9a29 | 2021-03-03 12:07:20 +0100 | [diff] [blame^] | 586 | * actually be output by the algorithm. Truncation length of '0' means |
| 587 | * default output length of the keytype-algorithm combination. */ |
| 588 | if( PSA_MAC_TRUNCATED_LENGTH( algorithm ) == 0 ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 589 | { |
| 590 | *length = default_length; |
| 591 | return( PSA_SUCCESS ); |
| 592 | } |
| 593 | else if( PSA_MAC_TRUNCATED_LENGTH( algorithm ) <= default_length ) |
| 594 | { |
| 595 | *length = PSA_MAC_TRUNCATED_LENGTH( algorithm ); |
| 596 | return( PSA_SUCCESS ); |
| 597 | } |
| 598 | else |
| 599 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 600 | } |
| 601 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 602 | /** Try to allocate a buffer to an empty key slot. |
| 603 | * |
| 604 | * \param[in,out] slot Key slot to attach buffer to. |
| 605 | * \param[in] buffer_length Requested size of the buffer. |
| 606 | * |
| 607 | * \retval #PSA_SUCCESS |
| 608 | * The buffer has been successfully allocated. |
| 609 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 610 | * Not enough memory was available for allocation. |
| 611 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 612 | * Trying to allocate a buffer to a non-empty key slot. |
| 613 | */ |
| 614 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 615 | size_t buffer_length ) |
| 616 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 617 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 618 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 619 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 620 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 621 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 622 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 623 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 624 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 625 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 628 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 629 | const uint8_t* data, |
| 630 | size_t data_length ) |
| 631 | { |
| 632 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 633 | data_length ); |
| 634 | if( status != PSA_SUCCESS ) |
| 635 | return( status ); |
| 636 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 637 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 638 | return( PSA_SUCCESS ); |
| 639 | } |
| 640 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 641 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 642 | const psa_key_attributes_t *attributes, |
| 643 | const uint8_t *data, size_t data_length, |
| 644 | uint8_t *key_buffer, size_t key_buffer_size, |
| 645 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 646 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 647 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 648 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 649 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 650 | /* zero-length keys are never supported. */ |
| 651 | if( data_length == 0 ) |
| 652 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 653 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 654 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 655 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 656 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 657 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 658 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 659 | if( data_length > SIZE_MAX / 8 ) |
| 660 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 661 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 662 | /* Enforce a size limit, and in particular ensure that the bit |
| 663 | * size fits in its representation type. */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 664 | if( ( *bits ) > PSA_MAX_KEY_BITS ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 665 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 666 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 667 | status = validate_unstructured_key_bit_size( type, *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 668 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 669 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 670 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 671 | /* Copy the key material. */ |
| 672 | memcpy( key_buffer, data, data_length ); |
| 673 | *key_buffer_length = data_length; |
| 674 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 675 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 676 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 677 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 678 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 679 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 680 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 681 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 682 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 683 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 684 | return( mbedtls_psa_ecp_import_key( attributes, |
| 685 | data, data_length, |
| 686 | key_buffer, key_buffer_size, |
| 687 | key_buffer_length, |
| 688 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 689 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 690 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 691 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 692 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 693 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 694 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 695 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 696 | return( mbedtls_psa_rsa_import_key( attributes, |
| 697 | data, data_length, |
| 698 | key_buffer, key_buffer_size, |
| 699 | key_buffer_length, |
| 700 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 701 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 702 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 703 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 704 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 705 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 706 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 709 | /** Calculate the intersection of two algorithm usage policies. |
| 710 | * |
| 711 | * Return 0 (which allows no operation) on incompatibility. |
| 712 | */ |
| 713 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 714 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 715 | psa_algorithm_t alg1, |
| 716 | psa_algorithm_t alg2 ) |
| 717 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 718 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 719 | if( alg1 == alg2 ) |
| 720 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 721 | /* If the policies are from the same hash-and-sign family, check |
| 722 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 723 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 724 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 725 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 726 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 727 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 728 | return( alg2 ); |
| 729 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 730 | return( alg1 ); |
| 731 | } |
| 732 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 733 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | * restrictive tag length. */ |
| 735 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 736 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 737 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 738 | { |
| 739 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 740 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
| 741 | size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 742 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 743 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 744 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 745 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 746 | { |
Steven Cooreman | 5d81481 | 2021-02-18 12:11:39 +0100 | [diff] [blame] | 747 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 748 | } |
| 749 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 750 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 751 | ( alg1_len <= alg2_len ) ) |
| 752 | { |
| 753 | return( alg2 ); |
| 754 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 755 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 756 | ( alg2_len <= alg1_len ) ) |
| 757 | { |
| 758 | return( alg1 ); |
| 759 | } |
| 760 | } |
| 761 | /* If the policies are from the same MAC family, check whether one |
| 762 | * of them is a minimum-MAC-length policy. Calculate the most |
| 763 | * restrictive tag length. */ |
| 764 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 765 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 766 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 767 | { |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 768 | /* Calculate the actual requested output length for both sides. In case |
| 769 | * of at-least-this-length wildcard algorithms, the requested output |
| 770 | * length is the shortest allowed length. */ |
| 771 | size_t alg1_len = 0; |
| 772 | size_t alg2_len = 0; |
| 773 | if( PSA_SUCCESS != psa_get_mac_output_length( |
| 774 | PSA_ALG_TRUNCATED_MAC( alg1, |
| 775 | PSA_MAC_TRUNCATED_LENGTH( alg1 ) ), |
| 776 | key_type, |
| 777 | &alg1_len ) ) |
| 778 | { |
| 779 | return( 0 ); |
| 780 | } |
| 781 | if( PSA_SUCCESS != psa_get_mac_output_length( |
| 782 | PSA_ALG_TRUNCATED_MAC( alg2, |
| 783 | PSA_MAC_TRUNCATED_LENGTH( alg2 ) ), |
| 784 | key_type, |
| 785 | &alg2_len ) ) |
| 786 | { |
| 787 | return( 0 ); |
| 788 | } |
| 789 | |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 790 | size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 791 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 792 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 793 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 794 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 795 | { |
Steven Cooreman | caad493 | 2021-02-18 11:28:17 +0100 | [diff] [blame] | 796 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 797 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 798 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
| 799 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 800 | { |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 801 | if( alg1_len <= alg2_len ) |
| 802 | return( alg2 ); |
| 803 | else |
| 804 | return( 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 805 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 806 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 807 | { |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 808 | if( alg2_len <= alg1_len ) |
| 809 | return( alg1 ); |
| 810 | else |
| 811 | return( 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 812 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 813 | /* If none of them are wildcards, check whether we can match |
| 814 | * default-length with exact-length, and return exact-length in that |
| 815 | * case. */ |
| 816 | if( alg1_len == alg2_len ) |
| 817 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 818 | } |
| 819 | /* If the policies are incompatible, allow nothing. */ |
| 820 | return( 0 ); |
| 821 | } |
| 822 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 823 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 824 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 825 | psa_algorithm_t requested_alg ) |
| 826 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 827 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 828 | if( requested_alg == policy_alg ) |
| 829 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 830 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 831 | * and requested_alg is the same hash-and-sign family with any hash, |
| 832 | * then requested_alg is compliant with policy_alg. */ |
| 833 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 834 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 835 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 836 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 837 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 838 | } |
| 839 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 840 | * the requested algorithm, check the requested tag length to be |
| 841 | * equal-length or longer than the wildcard-specified length. */ |
| 842 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 843 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 844 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 845 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 846 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 847 | { |
| 848 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 849 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 850 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 851 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 852 | PSA_ALG_IS_MAC( requested_alg ) && |
| 853 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 854 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 855 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 856 | size_t actual_output_length; |
| 857 | size_t default_output_length; |
| 858 | if( PSA_SUCCESS != psa_get_mac_output_length( |
| 859 | requested_alg, |
| 860 | key_type, |
| 861 | &actual_output_length ) ) |
| 862 | { |
| 863 | return( 0 ); |
| 864 | } |
| 865 | if( PSA_SUCCESS != psa_get_mac_output_length( |
| 866 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ), |
| 867 | key_type, |
| 868 | &default_output_length ) ) |
| 869 | { |
| 870 | return( 0 ); |
| 871 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 872 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 873 | /* If the policy is default-length, only allow an algorithm with |
| 874 | * a declared exact-length matching the default. */ |
| 875 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
| 876 | return( actual_output_length == default_output_length ); |
| 877 | |
| 878 | /* If the requested algorithm is default-length, allow it if the policy |
| 879 | * is exactly the default length. */ |
| 880 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 881 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 882 | { |
| 883 | return( 1 ); |
| 884 | } |
| 885 | |
| 886 | /* If policy_alg is a wildcard MAC algorithm of the same base as |
| 887 | * the requested algorithm, check the requested tag length to be |
| 888 | * equal-length or longer than the wildcard-specified length. */ |
| 889 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 890 | { |
| 891 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
| 892 | actual_output_length ); |
| 893 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 894 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 895 | /* 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] | 896 | * a key derivation with that key agreement should also be allowed. This |
| 897 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 898 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 899 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 900 | { |
| 901 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 902 | policy_alg ); |
| 903 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 904 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 905 | return( 0 ); |
| 906 | } |
| 907 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 908 | /** Test whether a policy permits an algorithm. |
| 909 | * |
| 910 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 911 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 912 | * \note This function requires providing the key type for which the policy is |
| 913 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 914 | * have different properties depending on what kind of cipher it is |
| 915 | * combined with. |
| 916 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 917 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 918 | * allowed by the \p policy. |
| 919 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 920 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 921 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 922 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 923 | static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy, |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 924 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 925 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 926 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 927 | /* '0' is not a valid algorithm */ |
| 928 | if( alg == 0 ) |
| 929 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 930 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 931 | /* A requested algorithm cannot be a wildcard. */ |
| 932 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 933 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 934 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 935 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 936 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 937 | return( PSA_SUCCESS ); |
| 938 | else |
| 939 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 942 | /** Restrict a key policy based on a constraint. |
| 943 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 944 | * \note This function requires providing the key type for which the policy is |
| 945 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 946 | * have different properties depending on what kind of cipher it is |
| 947 | * combined with. |
| 948 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 949 | * \param[in] key_type The key type for which to restrict the policy |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 950 | * \param[in,out] policy The policy to restrict. |
| 951 | * \param[in] constraint The policy constraint to apply. |
| 952 | * |
| 953 | * \retval #PSA_SUCCESS |
| 954 | * \c *policy contains the intersection of the original value of |
| 955 | * \c *policy and \c *constraint. |
| 956 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 957 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 958 | * \c *policy is unchanged. |
| 959 | */ |
| 960 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 961 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 962 | psa_key_policy_t *policy, |
| 963 | const psa_key_policy_t *constraint ) |
| 964 | { |
| 965 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 966 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 967 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 968 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 969 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 970 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 971 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 972 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 973 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 974 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 975 | policy->usage &= constraint->usage; |
| 976 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 977 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 978 | return( PSA_SUCCESS ); |
| 979 | } |
| 980 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 981 | /** Get the description of a key given its identifier and policy constraints |
| 982 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 983 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 984 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 985 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 986 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 987 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 988 | * In case of a persistent key, the function loads the description of the key |
| 989 | * into a key slot if not already done. |
| 990 | * |
| 991 | * On success, the returned key slot is locked. It is the responsibility of |
| 992 | * 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] | 993 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 994 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 995 | mbedtls_svc_key_id_t key, |
| 996 | psa_key_slot_t **p_slot, |
| 997 | psa_key_usage_t usage, |
| 998 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 999 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1000 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1001 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1002 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1003 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1004 | if( status != PSA_SUCCESS ) |
| 1005 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1006 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1007 | |
| 1008 | /* Enforce that usage policy for the key slot contains all the flags |
| 1009 | * required by the usage parameter. There is one exception: public |
| 1010 | * keys can always be exported, so we treat public key objects as |
| 1011 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1012 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1013 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1014 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1015 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1016 | { |
| 1017 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1018 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1019 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1020 | |
| 1021 | /* Enforce that the usage policy permits the requested algortihm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1022 | if( alg != 0 ) |
| 1023 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1024 | status = psa_key_policy_permits( &slot->attr.policy, |
| 1025 | slot->attr.type, |
| 1026 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1027 | if( status != PSA_SUCCESS ) |
| 1028 | goto error; |
| 1029 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1030 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1031 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1032 | |
| 1033 | error: |
| 1034 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1035 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1036 | |
| 1037 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1038 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1039 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1040 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1041 | * |
| 1042 | * A transparent key is a key for which the key material is directly |
| 1043 | * available, as opposed to a key in a secure element. |
| 1044 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1045 | * This is a temporary function to use instead of |
| 1046 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1047 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1048 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1049 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1050 | * 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] | 1051 | */ |
| 1052 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1053 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1054 | mbedtls_svc_key_id_t key, |
| 1055 | psa_key_slot_t **p_slot, |
| 1056 | psa_key_usage_t usage, |
| 1057 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1058 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1059 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1060 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1061 | if( status != PSA_SUCCESS ) |
| 1062 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1063 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1064 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1065 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1066 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1067 | *p_slot = NULL; |
| 1068 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1069 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1070 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1071 | return( PSA_SUCCESS ); |
| 1072 | } |
| 1073 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1074 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1075 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1076 | 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] | 1077 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1078 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1079 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1080 | 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] | 1081 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1082 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1083 | * initialized slot, so we can just free it. */ |
| 1084 | if( slot->key.data != NULL ) |
| 1085 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1086 | |
| 1087 | mbedtls_free( slot->key.data ); |
| 1088 | slot->key.data = NULL; |
| 1089 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1090 | |
| 1091 | return( PSA_SUCCESS ); |
| 1092 | } |
| 1093 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1094 | /** Completely wipe a slot in memory, including its policy. |
| 1095 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1096 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1097 | { |
| 1098 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1099 | |
| 1100 | /* |
| 1101 | * 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] | 1102 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1103 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1104 | * 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] | 1105 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1106 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1107 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1108 | { |
| 1109 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1110 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1111 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1112 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1113 | } |
| 1114 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1115 | /* Multipart operations may still be using the key. This is safe |
| 1116 | * because all multipart operation objects are independent from |
| 1117 | * the key slot: if they need to access the key after the setup |
| 1118 | * phase, they have a copy of the key. Note that this means that |
| 1119 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1120 | /* At this point, key material and other type-specific content has |
| 1121 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1122 | * zeroize because the metadata is not particularly sensitive. */ |
| 1123 | memset( slot, 0, sizeof( *slot ) ); |
| 1124 | return( status ); |
| 1125 | } |
| 1126 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1127 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1128 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1129 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1130 | psa_status_t status; /* status of the last operation */ |
| 1131 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1132 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1133 | psa_se_drv_table_entry_t *driver; |
| 1134 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1135 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1136 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1137 | return( PSA_SUCCESS ); |
| 1138 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1139 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1140 | * 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] | 1141 | * key, this will load the key description from persistent memory if not |
| 1142 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1143 | * the key is operated by an SE or not and this information is needed by |
| 1144 | * the current implementation. |
| 1145 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1146 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1147 | if( status != PSA_SUCCESS ) |
| 1148 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1149 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1150 | /* |
| 1151 | * If the key slot containing the key description is under access by the |
| 1152 | * library (apart from the present access), the key cannot be destroyed |
| 1153 | * yet. For the time being, just return in error. Eventually (to be |
| 1154 | * implemented), the key should be destroyed when all accesses have |
| 1155 | * stopped. |
| 1156 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1157 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1158 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1159 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1160 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1161 | } |
| 1162 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1163 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1164 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1165 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1166 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1167 | /* For a key in a secure element, we need to do three things: |
| 1168 | * remove the key file in internal storage, destroy the |
| 1169 | * key inside the secure element, and update the driver's |
| 1170 | * persistent data. Start a transaction that will encompass these |
| 1171 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1172 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1173 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1174 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1175 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1176 | status = psa_crypto_save_transaction( ); |
| 1177 | if( status != PSA_SUCCESS ) |
| 1178 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1179 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1180 | /* We should still try to destroy the key in the secure |
| 1181 | * element and the key metadata in storage. This is especially |
| 1182 | * important if the error is that the storage is full. |
| 1183 | * But how to do it exactly without risking an inconsistent |
| 1184 | * state after a reset? |
| 1185 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1186 | */ |
| 1187 | overall_status = status; |
| 1188 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1191 | status = psa_destroy_se_key( driver, |
| 1192 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1193 | if( overall_status == PSA_SUCCESS ) |
| 1194 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1195 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1196 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1197 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1198 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1199 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1200 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1201 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1202 | if( overall_status == PSA_SUCCESS ) |
| 1203 | overall_status = status; |
| 1204 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1205 | /* TODO: other slots may have a copy of the same key. We should |
| 1206 | * invalidate them. |
| 1207 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1208 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1209 | } |
| 1210 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1211 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1212 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1213 | if( driver != NULL ) |
| 1214 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1215 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1216 | if( overall_status == PSA_SUCCESS ) |
| 1217 | overall_status = status; |
| 1218 | status = psa_crypto_stop_transaction( ); |
| 1219 | if( overall_status == PSA_SUCCESS ) |
| 1220 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1221 | } |
| 1222 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1223 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1224 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1225 | exit: |
| 1226 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1227 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1228 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1229 | if( overall_status == PSA_SUCCESS ) |
| 1230 | overall_status = status; |
| 1231 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1232 | } |
| 1233 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1234 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1235 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1236 | static psa_status_t psa_get_rsa_public_exponent( |
| 1237 | const mbedtls_rsa_context *rsa, |
| 1238 | psa_key_attributes_t *attributes ) |
| 1239 | { |
| 1240 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1241 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1242 | uint8_t *buffer = NULL; |
| 1243 | size_t buflen; |
| 1244 | mbedtls_mpi_init( &mpi ); |
| 1245 | |
| 1246 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1247 | if( ret != 0 ) |
| 1248 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1249 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1250 | { |
| 1251 | /* It's the default value, which is reported as an empty string, |
| 1252 | * so there's nothing to do. */ |
| 1253 | goto exit; |
| 1254 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1255 | |
| 1256 | buflen = mbedtls_mpi_size( &mpi ); |
| 1257 | buffer = mbedtls_calloc( 1, buflen ); |
| 1258 | if( buffer == NULL ) |
| 1259 | { |
| 1260 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1261 | goto exit; |
| 1262 | } |
| 1263 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1264 | if( ret != 0 ) |
| 1265 | goto exit; |
| 1266 | attributes->domain_parameters = buffer; |
| 1267 | attributes->domain_parameters_size = buflen; |
| 1268 | |
| 1269 | exit: |
| 1270 | mbedtls_mpi_free( &mpi ); |
| 1271 | if( ret != 0 ) |
| 1272 | mbedtls_free( buffer ); |
| 1273 | return( mbedtls_to_psa_error( ret ) ); |
| 1274 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1275 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1276 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1277 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1278 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1279 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1280 | 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] | 1281 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1282 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1283 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1284 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1285 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1286 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1287 | psa_reset_key_attributes( attributes ); |
| 1288 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1289 | 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] | 1290 | if( status != PSA_SUCCESS ) |
| 1291 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1292 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1293 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1294 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1295 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1296 | |
| 1297 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1298 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1299 | psa_set_key_slot_number( attributes, |
| 1300 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1301 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1302 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1303 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1304 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1305 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1306 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1307 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1308 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1309 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1310 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1311 | * is not yet implemented. |
| 1312 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1313 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1314 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1315 | break; |
| 1316 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1317 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1318 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1319 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1320 | status = mbedtls_psa_rsa_load_representation( |
| 1321 | slot->attr.type, |
| 1322 | slot->key.data, |
| 1323 | slot->key.bytes, |
| 1324 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1325 | if( status != PSA_SUCCESS ) |
| 1326 | break; |
| 1327 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1328 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1329 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1330 | mbedtls_rsa_free( rsa ); |
| 1331 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1332 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1333 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1334 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1335 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1336 | default: |
| 1337 | /* Nothing else to do. */ |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | if( status != PSA_SUCCESS ) |
| 1342 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1343 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1344 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1345 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1346 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1347 | } |
| 1348 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1349 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1350 | psa_status_t psa_get_key_slot_number( |
| 1351 | const psa_key_attributes_t *attributes, |
| 1352 | psa_key_slot_number_t *slot_number ) |
| 1353 | { |
| 1354 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1355 | { |
| 1356 | *slot_number = attributes->slot_number; |
| 1357 | return( PSA_SUCCESS ); |
| 1358 | } |
| 1359 | else |
| 1360 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1361 | } |
| 1362 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1363 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1364 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1365 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1366 | uint8_t *data, |
| 1367 | size_t data_size, |
| 1368 | size_t *data_length ) |
| 1369 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1370 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1371 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1372 | memcpy( data, key_buffer, key_buffer_size ); |
| 1373 | memset( data + key_buffer_size, 0, |
| 1374 | data_size - key_buffer_size ); |
| 1375 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1376 | return( PSA_SUCCESS ); |
| 1377 | } |
| 1378 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1379 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1380 | const psa_key_attributes_t *attributes, |
| 1381 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1382 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1383 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1384 | psa_key_type_t type = attributes->core.type; |
| 1385 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1386 | if( key_type_is_raw_bytes( type ) || |
| 1387 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1388 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1389 | { |
| 1390 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1391 | key_buffer, key_buffer_size, |
| 1392 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1393 | } |
| 1394 | else |
| 1395 | { |
| 1396 | /* This shouldn't happen in the reference implementation, but |
| 1397 | it is valid for a special-purpose implementation to omit |
| 1398 | support for exporting certain key types. */ |
| 1399 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1404 | uint8_t *data, |
| 1405 | size_t data_size, |
| 1406 | size_t *data_length ) |
| 1407 | { |
| 1408 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1409 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1410 | psa_key_slot_t *slot; |
| 1411 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1412 | /* Reject a zero-length output buffer now, since this can never be a |
| 1413 | * valid key representation. This way we know that data must be a valid |
| 1414 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1415 | if( data_size == 0 ) |
| 1416 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1417 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1418 | /* Set the key to empty now, so that even when there are errors, we always |
| 1419 | * set data_length to a value between 0 and data_size. On error, setting |
| 1420 | * the key to empty is a good choice because an empty key representation is |
| 1421 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1422 | *data_length = 0; |
| 1423 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1424 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1425 | * which don't require any flag, but |
| 1426 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1427 | */ |
| 1428 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1429 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1430 | if( status != PSA_SUCCESS ) |
| 1431 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1432 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1433 | psa_key_attributes_t attributes = { |
| 1434 | .core = slot->attr |
| 1435 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1436 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1437 | slot->key.data, slot->key.bytes, |
| 1438 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1439 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1440 | unlock_status = psa_unlock_key_slot( slot ); |
| 1441 | |
| 1442 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1443 | } |
| 1444 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1445 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1446 | const psa_key_attributes_t *attributes, |
| 1447 | const uint8_t *key_buffer, |
| 1448 | size_t key_buffer_size, |
| 1449 | uint8_t *data, |
| 1450 | size_t data_size, |
| 1451 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1452 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1453 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1454 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1455 | if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1456 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1457 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1458 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1459 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1460 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1461 | key_buffer, key_buffer_size, |
| 1462 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1463 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1464 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1465 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1466 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1467 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1468 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1469 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1470 | key_buffer, |
| 1471 | key_buffer_size, |
| 1472 | data, |
| 1473 | data_size, |
| 1474 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1475 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1476 | /* We don't know how to convert a private RSA key to public. */ |
| 1477 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1478 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1479 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1480 | } |
| 1481 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1482 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1483 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1484 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1485 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1486 | key_buffer, |
| 1487 | key_buffer_size, |
| 1488 | data, |
| 1489 | data_size, |
| 1490 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1491 | #else |
| 1492 | /* 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] | 1493 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1494 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1495 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1496 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1498 | else |
| 1499 | { |
| 1500 | /* This shouldn't happen in the reference implementation, but |
| 1501 | it is valid for a special-purpose implementation to omit |
| 1502 | support for exporting certain key types. */ |
| 1503 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1504 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1505 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1506 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1507 | 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] | 1508 | uint8_t *data, |
| 1509 | size_t data_size, |
| 1510 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1511 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1512 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1513 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1514 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1515 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1516 | /* Reject a zero-length output buffer now, since this can never be a |
| 1517 | * valid key representation. This way we know that data must be a valid |
| 1518 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1519 | if( data_size == 0 ) |
| 1520 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1521 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1522 | /* Set the key to empty now, so that even when there are errors, we always |
| 1523 | * set data_length to a value between 0 and data_size. On error, setting |
| 1524 | * the key to empty is a good choice because an empty key representation is |
| 1525 | * unlikely to be accepted anywhere. */ |
| 1526 | *data_length = 0; |
| 1527 | |
| 1528 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1529 | 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] | 1530 | if( status != PSA_SUCCESS ) |
| 1531 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1532 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1533 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1534 | { |
| 1535 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1536 | goto exit; |
| 1537 | } |
| 1538 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1539 | psa_key_attributes_t attributes = { |
| 1540 | .core = slot->attr |
| 1541 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1542 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1543 | &attributes, slot->key.data, slot->key.bytes, |
| 1544 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1545 | |
| 1546 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1547 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1548 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1549 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1550 | } |
| 1551 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1552 | #if defined(static_assert) |
| 1553 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1554 | "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] | 1555 | 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] | 1556 | "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] | 1557 | 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] | 1558 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1559 | #endif |
| 1560 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1561 | /** Validate that a key policy is internally well-formed. |
| 1562 | * |
| 1563 | * This function only rejects invalid policies. It does not validate the |
| 1564 | * consistency of the policy with respect to other attributes of the key |
| 1565 | * such as the key type. |
| 1566 | */ |
| 1567 | 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] | 1568 | { |
| 1569 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1570 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1571 | PSA_KEY_USAGE_ENCRYPT | |
| 1572 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1573 | PSA_KEY_USAGE_SIGN_HASH | |
| 1574 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1575 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1576 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1577 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1578 | return( PSA_SUCCESS ); |
| 1579 | } |
| 1580 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1581 | /** Validate the internal consistency of key attributes. |
| 1582 | * |
| 1583 | * This function only rejects invalid attribute values. If does not |
| 1584 | * validate the consistency of the attributes with any key data that may |
| 1585 | * be involved in the creation of the key. |
| 1586 | * |
| 1587 | * Call this function early in the key creation process. |
| 1588 | * |
| 1589 | * \param[in] attributes Key attributes for the new key. |
| 1590 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1591 | * NULL for a transparent key. |
| 1592 | * |
| 1593 | */ |
| 1594 | static psa_status_t psa_validate_key_attributes( |
| 1595 | const psa_key_attributes_t *attributes, |
| 1596 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1597 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1598 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1599 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1600 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1601 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1602 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1603 | if( status != PSA_SUCCESS ) |
| 1604 | return( status ); |
| 1605 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1606 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1607 | if( status != PSA_SUCCESS ) |
| 1608 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1609 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1610 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1611 | { |
| 1612 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1613 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1614 | } |
| 1615 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1616 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1617 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1618 | if( status != PSA_SUCCESS ) |
| 1619 | return( status ); |
| 1620 | } |
| 1621 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1622 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1623 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1624 | return( status ); |
| 1625 | |
| 1626 | /* Refuse to create overly large keys. |
| 1627 | * Note that this doesn't trigger on import if the attributes don't |
| 1628 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1629 | * psa_import_key() needs its own checks. */ |
| 1630 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1631 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1632 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1633 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1634 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1635 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1636 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1637 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1638 | return( PSA_SUCCESS ); |
| 1639 | } |
| 1640 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | /** Prepare a key slot to receive key material. |
| 1642 | * |
| 1643 | * This function allocates a key slot and sets its metadata. |
| 1644 | * |
| 1645 | * If this function fails, call psa_fail_key_creation(). |
| 1646 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1647 | * This function is intended to be used as follows: |
| 1648 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1649 | * it with the specified attributes, and in case of a volatile key assign it |
| 1650 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1651 | * -# Populate the slot with the key material. |
| 1652 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1653 | * In case of failure at any step, stop the sequence and call |
| 1654 | * psa_fail_key_creation(). |
| 1655 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1656 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1657 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1658 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1659 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1660 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1661 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1662 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1663 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1664 | * |
| 1665 | * \retval #PSA_SUCCESS |
| 1666 | * The key slot is ready to receive key material. |
| 1667 | * \return If this function fails, the key slot is an invalid state. |
| 1668 | * 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] | 1669 | */ |
| 1670 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1671 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1672 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1673 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1674 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1675 | { |
| 1676 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1677 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1678 | psa_key_slot_t *slot; |
| 1679 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1680 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1681 | *p_drv = NULL; |
| 1682 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1683 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1684 | if( status != PSA_SUCCESS ) |
| 1685 | return( status ); |
| 1686 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1687 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1688 | if( status != PSA_SUCCESS ) |
| 1689 | return( status ); |
| 1690 | slot = *p_slot; |
| 1691 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1692 | /* We're storing the declared bit-size of the key. It's up to each |
| 1693 | * creation mechanism to verify that this information is correct. |
| 1694 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1695 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1696 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1697 | * volatile key identifier associated to the slot returned to contain its |
| 1698 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1699 | |
| 1700 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1701 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1702 | { |
| 1703 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1704 | slot->attr.id = volatile_key_id; |
| 1705 | #else |
| 1706 | slot->attr.id.key_id = volatile_key_id; |
| 1707 | #endif |
| 1708 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1709 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1710 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1711 | * external-only flags, query `attributes`. Thanks to the check |
| 1712 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1713 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1714 | * may have set. */ |
| 1715 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1717 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1718 | /* 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] | 1719 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1720 | * create the key file in internal storage, create the |
| 1721 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1722 | * persistent data. This is done by starting a transaction that will |
| 1723 | * encompass these three actions. |
| 1724 | * For registering a volatile key, we just need to find an appropriate |
| 1725 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1726 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1727 | /* The first thing to do is to find a slot number for the new key. |
| 1728 | * We save the slot number in persistent storage as part of the |
| 1729 | * transaction data. It will be needed to recover if the power |
| 1730 | * fails during the key creation process, to clean up on the secure |
| 1731 | * element side after restarting. Obtaining a slot number from the |
| 1732 | * secure element driver updates its persistent state, but we do not yet |
| 1733 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1734 | * 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] | 1735 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1736 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1737 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1738 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1739 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1740 | if( status != PSA_SUCCESS ) |
| 1741 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1742 | |
| 1743 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1744 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1745 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1746 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1747 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1748 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1749 | status = psa_crypto_save_transaction( ); |
| 1750 | if( status != PSA_SUCCESS ) |
| 1751 | { |
| 1752 | (void) psa_crypto_stop_transaction( ); |
| 1753 | return( status ); |
| 1754 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1755 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1756 | |
| 1757 | status = psa_copy_key_material_into_slot( |
| 1758 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1759 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1760 | |
| 1761 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1762 | { |
| 1763 | /* Key registration only makes sense with a secure element. */ |
| 1764 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1765 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1766 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1767 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1768 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1769 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1770 | |
| 1771 | /** Finalize the creation of a key once its key material has been set. |
| 1772 | * |
| 1773 | * This entails writing the key to persistent storage. |
| 1774 | * |
| 1775 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1776 | * See the documentation of psa_start_key_creation() for the intended use |
| 1777 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1778 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1779 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1780 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1781 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1782 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1783 | * \param[in,out] slot Pointer to the slot with key material. |
| 1784 | * \param[in] driver The secure element driver for the key, |
| 1785 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1786 | * \param[out] key On success, identifier of the key. Note that the |
| 1787 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1788 | * |
| 1789 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1790 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1791 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1792 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1793 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1794 | * \retval #PSA_ERROR_DATA_INVALID |
| 1795 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1796 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1797 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1798 | * \return If this function fails, the key slot is an invalid state. |
| 1799 | * 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] | 1800 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1801 | static psa_status_t psa_finish_key_creation( |
| 1802 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1803 | psa_se_drv_table_entry_t *driver, |
| 1804 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1805 | { |
| 1806 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1807 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1808 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1809 | |
| 1810 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1811 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1812 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1813 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1814 | if( driver != NULL ) |
| 1815 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1816 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1817 | psa_key_slot_number_t slot_number = |
| 1818 | psa_key_slot_get_slot_number( slot ) ; |
| 1819 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1820 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1821 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1822 | sizeof( data.slot_number ), |
| 1823 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1824 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1825 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1826 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1827 | (uint8_t*) &data, |
| 1828 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1829 | } |
| 1830 | else |
| 1831 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1832 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1833 | /* Key material is saved in export representation in the slot, so |
| 1834 | * just pass the slot buffer for storage. */ |
| 1835 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1836 | slot->key.data, |
| 1837 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1838 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1839 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1840 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1841 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1842 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1843 | /* Finish the transaction for a key creation. This does not |
| 1844 | * happen when registering an existing key. Detect this case |
| 1845 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1846 | * creation of a persistent key in a secure element requires a transaction, |
| 1847 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1848 | if( driver != NULL && |
| 1849 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1850 | { |
| 1851 | status = psa_save_se_persistent_data( driver ); |
| 1852 | if( status != PSA_SUCCESS ) |
| 1853 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1854 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1855 | return( status ); |
| 1856 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1857 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1858 | } |
| 1859 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1860 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1861 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1862 | { |
| 1863 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1864 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1865 | if( status != PSA_SUCCESS ) |
| 1866 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1867 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1868 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1869 | return( status ); |
| 1870 | } |
| 1871 | |
| 1872 | /** Abort the creation of a key. |
| 1873 | * |
| 1874 | * You may call this function after calling psa_start_key_creation(), |
| 1875 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1876 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1877 | * See the documentation of psa_start_key_creation() for the intended use |
| 1878 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1879 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1880 | * \param[in,out] slot Pointer to the slot with key material. |
| 1881 | * \param[in] driver The secure element driver for the key, |
| 1882 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1883 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1884 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1885 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1886 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1887 | (void) driver; |
| 1888 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1889 | if( slot == NULL ) |
| 1890 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1891 | |
| 1892 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1893 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1894 | * element, and the failure happened later (when saving metadata |
| 1895 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1896 | * element. |
| 1897 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1898 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1899 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1900 | /* Abort the ongoing transaction if any (there may not be one if |
| 1901 | * the creation process failed before starting one, or if the |
| 1902 | * key creation is a registration of a key in a secure element). |
| 1903 | * Earlier functions must already have done what it takes to undo any |
| 1904 | * partial creation. All that's left is to update the transaction data |
| 1905 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1906 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1907 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1908 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1909 | psa_wipe_key_slot( slot ); |
| 1910 | } |
| 1911 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1912 | /** Validate optional attributes during key creation. |
| 1913 | * |
| 1914 | * Some key attributes are optional during key creation. If they are |
| 1915 | * specified in the attributes structure, check that they are consistent |
| 1916 | * with the data in the slot. |
| 1917 | * |
| 1918 | * This function should be called near the end of key creation, after |
| 1919 | * the slot in memory is fully populated but before saving persistent data. |
| 1920 | */ |
| 1921 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1922 | const psa_key_slot_t *slot, |
| 1923 | const psa_key_attributes_t *attributes ) |
| 1924 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1925 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1926 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1927 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1928 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1929 | } |
| 1930 | |
| 1931 | if( attributes->domain_parameters_size != 0 ) |
| 1932 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1933 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1934 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1935 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1936 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1937 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1938 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1939 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1940 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1941 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1942 | slot->attr.type, |
| 1943 | slot->key.data, |
| 1944 | slot->key.bytes, |
| 1945 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1946 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1947 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1948 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1949 | mbedtls_mpi_init( &actual ); |
| 1950 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1951 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1952 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1953 | mbedtls_rsa_free( rsa ); |
| 1954 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1955 | if( ret != 0 ) |
| 1956 | goto rsa_exit; |
| 1957 | ret = mbedtls_mpi_read_binary( &required, |
| 1958 | attributes->domain_parameters, |
| 1959 | attributes->domain_parameters_size ); |
| 1960 | if( ret != 0 ) |
| 1961 | goto rsa_exit; |
| 1962 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1963 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1964 | rsa_exit: |
| 1965 | mbedtls_mpi_free( &actual ); |
| 1966 | mbedtls_mpi_free( &required ); |
| 1967 | if( ret != 0) |
| 1968 | return( mbedtls_to_psa_error( ret ) ); |
| 1969 | } |
| 1970 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1971 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1972 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1973 | { |
| 1974 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1975 | } |
| 1976 | } |
| 1977 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1978 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1979 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1980 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1981 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1982 | } |
| 1983 | |
| 1984 | return( PSA_SUCCESS ); |
| 1985 | } |
| 1986 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1987 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1988 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1989 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1990 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1991 | { |
| 1992 | psa_status_t status; |
| 1993 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1994 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1995 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1996 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1997 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1998 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1999 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2000 | * This also rejects any key which might be encoded as an empty string, |
| 2001 | * which is never valid. */ |
| 2002 | if( data_length == 0 ) |
| 2003 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2004 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2005 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2006 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2007 | if( status != PSA_SUCCESS ) |
| 2008 | goto exit; |
| 2009 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2010 | /* In the case of a transparent key or an opaque key stored in local |
| 2011 | * storage (thus not in the case of generating a key in a secure element |
| 2012 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 2013 | * hold the generated key material. */ |
| 2014 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2015 | { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2016 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2017 | if( status != PSA_SUCCESS ) |
| 2018 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2019 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2020 | |
| 2021 | bits = slot->attr.bits; |
| 2022 | status = psa_driver_wrapper_import_key( attributes, |
| 2023 | data, data_length, |
| 2024 | slot->key.data, |
| 2025 | slot->key.bytes, |
| 2026 | &slot->key.bytes, &bits ); |
| 2027 | if( status != PSA_SUCCESS ) |
| 2028 | goto exit; |
| 2029 | |
| 2030 | if( slot->attr.bits == 0 ) |
| 2031 | slot->attr.bits = (psa_key_bits_t) bits; |
| 2032 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2033 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2034 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2035 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2036 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2037 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2038 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2039 | if( status != PSA_SUCCESS ) |
| 2040 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2041 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2042 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2043 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2044 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2045 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2046 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2047 | return( status ); |
| 2048 | } |
| 2049 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2050 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2051 | psa_status_t mbedtls_psa_register_se_key( |
| 2052 | const psa_key_attributes_t *attributes ) |
| 2053 | { |
| 2054 | psa_status_t status; |
| 2055 | psa_key_slot_t *slot = NULL; |
| 2056 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2057 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2058 | |
| 2059 | /* Leaving attributes unspecified is not currently supported. |
| 2060 | * It could make sense to query the key type and size from the |
| 2061 | * secure element, but not all secure elements support this |
| 2062 | * and the driver HAL doesn't currently support it. */ |
| 2063 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2064 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2065 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2066 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2067 | |
| 2068 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2069 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2070 | if( status != PSA_SUCCESS ) |
| 2071 | goto exit; |
| 2072 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2073 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2074 | |
| 2075 | exit: |
| 2076 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2077 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2078 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2079 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2080 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2081 | return( status ); |
| 2082 | } |
| 2083 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2084 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2085 | 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] | 2086 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2087 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2088 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2089 | source->key.data, |
| 2090 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2091 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2092 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2093 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2094 | target->attr.type = source->attr.type; |
| 2095 | target->attr.bits = source->attr.bits; |
| 2096 | |
| 2097 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2100 | 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] | 2101 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2102 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2103 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2104 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2105 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2106 | psa_key_slot_t *source_slot = NULL; |
| 2107 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2108 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2109 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2110 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2111 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2112 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2113 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2114 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2115 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2116 | goto exit; |
| 2117 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2118 | status = psa_validate_optional_attributes( source_slot, |
| 2119 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2120 | if( status != PSA_SUCCESS ) |
| 2121 | goto exit; |
| 2122 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2123 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2124 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2125 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2126 | if( status != PSA_SUCCESS ) |
| 2127 | goto exit; |
| 2128 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2129 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2130 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2131 | if( status != PSA_SUCCESS ) |
| 2132 | goto exit; |
| 2133 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2134 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2135 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2136 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2137 | /* Copying to a secure element is not implemented yet. */ |
| 2138 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2139 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2140 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2141 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2142 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2143 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2144 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2145 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2146 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2147 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2148 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2149 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2150 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2151 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2152 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2153 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2154 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2157 | |
| 2158 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2159 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2160 | /* Message digests */ |
| 2161 | /****************************************************************/ |
| 2162 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2163 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2164 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2165 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2166 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2167 | 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] | 2168 | { |
| 2169 | switch( alg ) |
| 2170 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2171 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2172 | case PSA_ALG_MD2: |
| 2173 | return( &mbedtls_md2_info ); |
| 2174 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2175 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2176 | case PSA_ALG_MD4: |
| 2177 | return( &mbedtls_md4_info ); |
| 2178 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2179 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2180 | case PSA_ALG_MD5: |
| 2181 | return( &mbedtls_md5_info ); |
| 2182 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2183 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2184 | case PSA_ALG_RIPEMD160: |
| 2185 | return( &mbedtls_ripemd160_info ); |
| 2186 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2187 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2188 | case PSA_ALG_SHA_1: |
| 2189 | return( &mbedtls_sha1_info ); |
| 2190 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2191 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2192 | case PSA_ALG_SHA_224: |
| 2193 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2194 | #endif |
| 2195 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2196 | case PSA_ALG_SHA_256: |
| 2197 | return( &mbedtls_sha256_info ); |
| 2198 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2199 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2200 | case PSA_ALG_SHA_384: |
| 2201 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2202 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2203 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2204 | case PSA_ALG_SHA_512: |
| 2205 | return( &mbedtls_sha512_info ); |
| 2206 | #endif |
| 2207 | default: |
| 2208 | return( NULL ); |
| 2209 | } |
| 2210 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2211 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2212 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2213 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2214 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2215 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2216 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2217 | { |
| 2218 | switch( operation->alg ) |
| 2219 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2220 | case 0: |
| 2221 | /* The object has (apparently) been initialized but it is not |
| 2222 | * in use. It's ok to call abort on such an object, and there's |
| 2223 | * nothing to do. */ |
| 2224 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2225 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2226 | case PSA_ALG_MD2: |
| 2227 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2228 | break; |
| 2229 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2230 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2231 | case PSA_ALG_MD4: |
| 2232 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2233 | break; |
| 2234 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2235 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2236 | case PSA_ALG_MD5: |
| 2237 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2238 | break; |
| 2239 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2240 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2241 | case PSA_ALG_RIPEMD160: |
| 2242 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2243 | break; |
| 2244 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2245 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2246 | case PSA_ALG_SHA_1: |
| 2247 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2248 | break; |
| 2249 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2250 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2251 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2252 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2253 | break; |
| 2254 | #endif |
| 2255 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2256 | case PSA_ALG_SHA_256: |
| 2257 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2258 | break; |
| 2259 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2260 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2261 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2262 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2263 | break; |
| 2264 | #endif |
| 2265 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2266 | case PSA_ALG_SHA_512: |
| 2267 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2268 | break; |
| 2269 | #endif |
| 2270 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2271 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2272 | } |
| 2273 | operation->alg = 0; |
| 2274 | return( PSA_SUCCESS ); |
| 2275 | } |
| 2276 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2277 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2278 | psa_algorithm_t alg ) |
| 2279 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2280 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2281 | |
| 2282 | /* A context must be freshly initialized before it can be set up. */ |
| 2283 | if( operation->alg != 0 ) |
| 2284 | { |
| 2285 | return( PSA_ERROR_BAD_STATE ); |
| 2286 | } |
| 2287 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2288 | switch( alg ) |
| 2289 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2290 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2291 | case PSA_ALG_MD2: |
| 2292 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2293 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2294 | break; |
| 2295 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2296 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2297 | case PSA_ALG_MD4: |
| 2298 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2299 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2300 | break; |
| 2301 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2302 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2303 | case PSA_ALG_MD5: |
| 2304 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2305 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2306 | break; |
| 2307 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2308 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2309 | case PSA_ALG_RIPEMD160: |
| 2310 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2311 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2312 | break; |
| 2313 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2314 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2315 | case PSA_ALG_SHA_1: |
| 2316 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2317 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2318 | break; |
| 2319 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2320 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2321 | case PSA_ALG_SHA_224: |
| 2322 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2323 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2324 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2325 | #endif |
| 2326 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2327 | case PSA_ALG_SHA_256: |
| 2328 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2329 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2330 | break; |
| 2331 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2332 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2333 | case PSA_ALG_SHA_384: |
| 2334 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2335 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2336 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2337 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2338 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2339 | case PSA_ALG_SHA_512: |
| 2340 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2341 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2342 | break; |
| 2343 | #endif |
| 2344 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2345 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2346 | PSA_ERROR_NOT_SUPPORTED : |
| 2347 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2348 | } |
| 2349 | if( ret == 0 ) |
| 2350 | operation->alg = alg; |
| 2351 | else |
| 2352 | psa_hash_abort( operation ); |
| 2353 | return( mbedtls_to_psa_error( ret ) ); |
| 2354 | } |
| 2355 | |
| 2356 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2357 | const uint8_t *input, |
| 2358 | size_t input_length ) |
| 2359 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2360 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2361 | |
| 2362 | /* Don't require hash implementations to behave correctly on a |
| 2363 | * zero-length input, which may have an invalid pointer. */ |
| 2364 | if( input_length == 0 ) |
| 2365 | return( PSA_SUCCESS ); |
| 2366 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2367 | switch( operation->alg ) |
| 2368 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2369 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2370 | case PSA_ALG_MD2: |
| 2371 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2372 | input, input_length ); |
| 2373 | break; |
| 2374 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2375 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2376 | case PSA_ALG_MD4: |
| 2377 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2378 | input, input_length ); |
| 2379 | break; |
| 2380 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2381 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2382 | case PSA_ALG_MD5: |
| 2383 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2384 | input, input_length ); |
| 2385 | break; |
| 2386 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2387 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2388 | case PSA_ALG_RIPEMD160: |
| 2389 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2390 | input, input_length ); |
| 2391 | break; |
| 2392 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2393 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2394 | case PSA_ALG_SHA_1: |
| 2395 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2396 | input, input_length ); |
| 2397 | break; |
| 2398 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2399 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2400 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2401 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2402 | input, input_length ); |
| 2403 | break; |
| 2404 | #endif |
| 2405 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2406 | case PSA_ALG_SHA_256: |
| 2407 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2408 | input, input_length ); |
| 2409 | break; |
| 2410 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2411 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2412 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2413 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2414 | input, input_length ); |
| 2415 | break; |
| 2416 | #endif |
| 2417 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2418 | case PSA_ALG_SHA_512: |
| 2419 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2420 | input, input_length ); |
| 2421 | break; |
| 2422 | #endif |
| 2423 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2424 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2425 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2426 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2427 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2428 | if( ret != 0 ) |
| 2429 | psa_hash_abort( operation ); |
| 2430 | return( mbedtls_to_psa_error( ret ) ); |
| 2431 | } |
| 2432 | |
| 2433 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2434 | uint8_t *hash, |
| 2435 | size_t hash_size, |
| 2436 | size_t *hash_length ) |
| 2437 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2438 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2439 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2440 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2441 | |
| 2442 | /* Fill the output buffer with something that isn't a valid hash |
| 2443 | * (barring an attack on the hash and deliberately-crafted input), |
| 2444 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2445 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2446 | /* If hash_size is 0 then hash may be NULL and then the |
| 2447 | * call to memset would have undefined behavior. */ |
| 2448 | if( hash_size != 0 ) |
| 2449 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2450 | |
| 2451 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2452 | { |
| 2453 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2454 | goto exit; |
| 2455 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2456 | |
| 2457 | switch( operation->alg ) |
| 2458 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2459 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2460 | case PSA_ALG_MD2: |
| 2461 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2462 | break; |
| 2463 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2464 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2465 | case PSA_ALG_MD4: |
| 2466 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2467 | break; |
| 2468 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2469 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2470 | case PSA_ALG_MD5: |
| 2471 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2472 | break; |
| 2473 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2474 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2475 | case PSA_ALG_RIPEMD160: |
| 2476 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2477 | break; |
| 2478 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2479 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2480 | case PSA_ALG_SHA_1: |
| 2481 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2482 | break; |
| 2483 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2484 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2485 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2486 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2487 | break; |
| 2488 | #endif |
| 2489 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2490 | case PSA_ALG_SHA_256: |
| 2491 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2492 | break; |
| 2493 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2494 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2495 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2496 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2497 | break; |
| 2498 | #endif |
| 2499 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2500 | case PSA_ALG_SHA_512: |
| 2501 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2502 | break; |
| 2503 | #endif |
| 2504 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2505 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2506 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2507 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2508 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2509 | exit: |
| 2510 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2511 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2512 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2513 | return( psa_hash_abort( operation ) ); |
| 2514 | } |
| 2515 | else |
| 2516 | { |
| 2517 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2518 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2522 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2523 | const uint8_t *hash, |
| 2524 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2525 | { |
| 2526 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2527 | size_t actual_hash_length; |
| 2528 | psa_status_t status = psa_hash_finish( operation, |
| 2529 | actual_hash, sizeof( actual_hash ), |
| 2530 | &actual_hash_length ); |
| 2531 | if( status != PSA_SUCCESS ) |
| 2532 | return( status ); |
| 2533 | if( actual_hash_length != hash_length ) |
| 2534 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2535 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2536 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2537 | return( PSA_SUCCESS ); |
| 2538 | } |
| 2539 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2540 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2541 | const uint8_t *input, size_t input_length, |
| 2542 | uint8_t *hash, size_t hash_size, |
| 2543 | size_t *hash_length ) |
| 2544 | { |
| 2545 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2546 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2547 | |
| 2548 | *hash_length = hash_size; |
| 2549 | status = psa_hash_setup( &operation, alg ); |
| 2550 | if( status != PSA_SUCCESS ) |
| 2551 | goto exit; |
| 2552 | status = psa_hash_update( &operation, input, input_length ); |
| 2553 | if( status != PSA_SUCCESS ) |
| 2554 | goto exit; |
| 2555 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2556 | if( status != PSA_SUCCESS ) |
| 2557 | goto exit; |
| 2558 | |
| 2559 | exit: |
| 2560 | if( status == PSA_SUCCESS ) |
| 2561 | status = psa_hash_abort( &operation ); |
| 2562 | else |
| 2563 | psa_hash_abort( &operation ); |
| 2564 | return( status ); |
| 2565 | } |
| 2566 | |
| 2567 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2568 | const uint8_t *input, size_t input_length, |
| 2569 | const uint8_t *hash, size_t hash_length ) |
| 2570 | { |
| 2571 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2572 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2573 | |
| 2574 | status = psa_hash_setup( &operation, alg ); |
| 2575 | if( status != PSA_SUCCESS ) |
| 2576 | goto exit; |
| 2577 | status = psa_hash_update( &operation, input, input_length ); |
| 2578 | if( status != PSA_SUCCESS ) |
| 2579 | goto exit; |
| 2580 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2581 | if( status != PSA_SUCCESS ) |
| 2582 | goto exit; |
| 2583 | |
| 2584 | exit: |
| 2585 | if( status == PSA_SUCCESS ) |
| 2586 | status = psa_hash_abort( &operation ); |
| 2587 | else |
| 2588 | psa_hash_abort( &operation ); |
| 2589 | return( status ); |
| 2590 | } |
| 2591 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2592 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2593 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2594 | { |
| 2595 | if( target_operation->alg != 0 ) |
| 2596 | return( PSA_ERROR_BAD_STATE ); |
| 2597 | |
| 2598 | switch( source_operation->alg ) |
| 2599 | { |
| 2600 | case 0: |
| 2601 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2602 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2603 | case PSA_ALG_MD2: |
| 2604 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2605 | &source_operation->ctx.md2 ); |
| 2606 | break; |
| 2607 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2608 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2609 | case PSA_ALG_MD4: |
| 2610 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2611 | &source_operation->ctx.md4 ); |
| 2612 | break; |
| 2613 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2614 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2615 | case PSA_ALG_MD5: |
| 2616 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2617 | &source_operation->ctx.md5 ); |
| 2618 | break; |
| 2619 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2620 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2621 | case PSA_ALG_RIPEMD160: |
| 2622 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2623 | &source_operation->ctx.ripemd160 ); |
| 2624 | break; |
| 2625 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2626 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2627 | case PSA_ALG_SHA_1: |
| 2628 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2629 | &source_operation->ctx.sha1 ); |
| 2630 | break; |
| 2631 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2632 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2633 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2634 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2635 | &source_operation->ctx.sha256 ); |
| 2636 | break; |
| 2637 | #endif |
| 2638 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2639 | case PSA_ALG_SHA_256: |
| 2640 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2641 | &source_operation->ctx.sha256 ); |
| 2642 | break; |
| 2643 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2644 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2645 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2646 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2647 | &source_operation->ctx.sha512 ); |
| 2648 | break; |
| 2649 | #endif |
| 2650 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2651 | case PSA_ALG_SHA_512: |
| 2652 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2653 | &source_operation->ctx.sha512 ); |
| 2654 | break; |
| 2655 | #endif |
| 2656 | default: |
| 2657 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2658 | } |
| 2659 | |
| 2660 | target_operation->alg = source_operation->alg; |
| 2661 | return( PSA_SUCCESS ); |
| 2662 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2663 | |
| 2664 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2665 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2666 | /* MAC */ |
| 2667 | /****************************************************************/ |
| 2668 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2669 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2670 | psa_algorithm_t alg, |
| 2671 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2672 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2673 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2674 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2675 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2676 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2677 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2678 | if( PSA_ALG_IS_AEAD( alg ) ) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2679 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2680 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2681 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2682 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2683 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2684 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 2685 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2686 | mode = MBEDTLS_MODE_STREAM; |
| 2687 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2688 | case PSA_ALG_CTR: |
| 2689 | mode = MBEDTLS_MODE_CTR; |
| 2690 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2691 | case PSA_ALG_CFB: |
| 2692 | mode = MBEDTLS_MODE_CFB; |
| 2693 | break; |
| 2694 | case PSA_ALG_OFB: |
| 2695 | mode = MBEDTLS_MODE_OFB; |
| 2696 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2697 | case PSA_ALG_ECB_NO_PADDING: |
| 2698 | mode = MBEDTLS_MODE_ECB; |
| 2699 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2700 | case PSA_ALG_CBC_NO_PADDING: |
| 2701 | mode = MBEDTLS_MODE_CBC; |
| 2702 | break; |
| 2703 | case PSA_ALG_CBC_PKCS7: |
| 2704 | mode = MBEDTLS_MODE_CBC; |
| 2705 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2706 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2707 | mode = MBEDTLS_MODE_CCM; |
| 2708 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2709 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2710 | mode = MBEDTLS_MODE_GCM; |
| 2711 | break; |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 2712 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2713 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2714 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2715 | default: |
| 2716 | return( NULL ); |
| 2717 | } |
| 2718 | } |
| 2719 | else if( alg == PSA_ALG_CMAC ) |
| 2720 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2721 | else |
| 2722 | return( NULL ); |
| 2723 | |
| 2724 | switch( key_type ) |
| 2725 | { |
| 2726 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2727 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2728 | break; |
| 2729 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2730 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2731 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2732 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2733 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2734 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2735 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2736 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2737 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2738 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2739 | if( key_bits == 128 ) |
| 2740 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2741 | break; |
| 2742 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2743 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2744 | break; |
| 2745 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2746 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2747 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2748 | case PSA_KEY_TYPE_CHACHA20: |
| 2749 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2750 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2751 | default: |
| 2752 | return( NULL ); |
| 2753 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2754 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2755 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2756 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2757 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2758 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2759 | } |
| 2760 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2761 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2762 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2763 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2764 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2765 | { |
| 2766 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2767 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2768 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2769 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2770 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2771 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2772 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2773 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2774 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2775 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2776 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2777 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2778 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2779 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2780 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2781 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2782 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2783 | return( 128 ); |
| 2784 | default: |
| 2785 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2786 | } |
| 2787 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2788 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2789 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2790 | /* Initialize the MAC operation structure. Once this function has been |
| 2791 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2792 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2793 | psa_algorithm_t alg ) |
| 2794 | { |
| 2795 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2796 | |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2797 | operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2798 | operation->key_set = 0; |
| 2799 | operation->iv_set = 0; |
| 2800 | operation->iv_required = 0; |
| 2801 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2802 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2803 | |
| 2804 | #if defined(MBEDTLS_CMAC_C) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2805 | if( operation->alg == PSA_ALG_CMAC ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2806 | { |
| 2807 | operation->iv_required = 0; |
| 2808 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2809 | status = PSA_SUCCESS; |
| 2810 | } |
| 2811 | else |
| 2812 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2813 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2814 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2815 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2816 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2817 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2818 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2819 | } |
| 2820 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2821 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2822 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2823 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2824 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | if( status != PSA_SUCCESS ) |
| 2828 | memset( operation, 0, sizeof( *operation ) ); |
| 2829 | return( status ); |
| 2830 | } |
| 2831 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2832 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2833 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2834 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2835 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2836 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2837 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2838 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2839 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2840 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2841 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2842 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2843 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2844 | /* The object has (apparently) been initialized but it is not |
| 2845 | * in use. It's ok to call abort on such an object, and there's |
| 2846 | * nothing to do. */ |
| 2847 | return( PSA_SUCCESS ); |
| 2848 | } |
| 2849 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2850 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2851 | if( operation->alg == PSA_ALG_CMAC ) |
| 2852 | { |
| 2853 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2854 | } |
| 2855 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2856 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2857 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2858 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2859 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2860 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2861 | } |
| 2862 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2863 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2864 | { |
| 2865 | /* Sanity check (shouldn't happen: operation->alg should |
| 2866 | * always have been initialized to a valid value). */ |
| 2867 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2868 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2869 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2870 | operation->alg = 0; |
| 2871 | operation->key_set = 0; |
| 2872 | operation->iv_set = 0; |
| 2873 | operation->iv_required = 0; |
| 2874 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2875 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2876 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2877 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2878 | |
| 2879 | bad_state: |
| 2880 | /* If abort is called on an uninitialized object, we can't trust |
| 2881 | * anything. Wipe the object in case it contains confidential data. |
| 2882 | * This may result in a memory leak if a pointer gets overwritten, |
| 2883 | * but it's too late to do anything about this. */ |
| 2884 | memset( operation, 0, sizeof( *operation ) ); |
| 2885 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2888 | #if defined(MBEDTLS_CMAC_C) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2889 | static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation, |
| 2890 | psa_key_slot_t *slot ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2891 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2892 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2893 | const mbedtls_cipher_info_t *cipher_info = |
| 2894 | mbedtls_cipher_info_from_psa( PSA_ALG_CMAC, |
| 2895 | slot->attr.type, slot->attr.bits, |
| 2896 | NULL ); |
| 2897 | if( cipher_info == NULL ) |
| 2898 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2899 | |
| 2900 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2901 | if( ret != 0 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2902 | goto exit; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2903 | |
| 2904 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2905 | slot->key.data, |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2906 | slot->attr.bits ); |
| 2907 | exit: |
| 2908 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2909 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2910 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2911 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2912 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2913 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2914 | const uint8_t *key, |
| 2915 | size_t key_length, |
| 2916 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2917 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2918 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2919 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2920 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2921 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2922 | psa_status_t status; |
| 2923 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2924 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2925 | * overflow below. This should never trigger if the hash algorithm |
| 2926 | * is implemented correctly. */ |
| 2927 | /* The size checks against the ipad and opad buffers cannot be written |
| 2928 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2929 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2930 | if( block_size > sizeof( ipad ) ) |
| 2931 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2932 | if( block_size > sizeof( hmac->opad ) ) |
| 2933 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2934 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2935 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2936 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2937 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2938 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2939 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2940 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2941 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2942 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2943 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2944 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2945 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2946 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2947 | * an invalid pointer which would make the behavior undefined. */ |
| 2948 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2949 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2950 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2951 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2952 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2953 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2954 | ipad[i] ^= 0x36; |
| 2955 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2956 | |
| 2957 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2958 | * and filling the rest of opad with the requisite constant. */ |
| 2959 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2960 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2961 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2962 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2963 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2964 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2965 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2966 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2967 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2968 | |
| 2969 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2970 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2971 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2972 | return( status ); |
| 2973 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 2974 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2975 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2976 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2977 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2978 | psa_algorithm_t alg, |
| 2979 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2980 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2981 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2982 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2983 | psa_key_slot_t *slot; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2984 | size_t output_length = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2985 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2986 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2987 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2988 | /* A context must be freshly initialized before it can be set up. */ |
| 2989 | if( operation->alg != 0 ) |
| 2990 | { |
| 2991 | return( PSA_ERROR_BAD_STATE ); |
| 2992 | } |
| 2993 | |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2994 | status = psa_mac_init( operation, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2995 | if( status != PSA_SUCCESS ) |
| 2996 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2997 | if( is_sign ) |
| 2998 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2999 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3000 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3001 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3002 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3003 | goto exit; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3004 | |
| 3005 | status = psa_get_mac_output_length( alg, slot->attr.type, |
| 3006 | &output_length ); |
| 3007 | if( status != PSA_SUCCESS ) |
| 3008 | goto exit; |
| 3009 | |
| 3010 | operation->mac_size = (uint8_t) output_length; |
| 3011 | |
| 3012 | if( operation->mac_size < 4 ) |
| 3013 | { |
| 3014 | /* A very short MAC is too short for security since it can be |
| 3015 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3016 | * so we make this our minimum, even though 32 bits is still |
| 3017 | * too small for security. */ |
| 3018 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3019 | goto exit; |
| 3020 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3021 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3022 | #if defined(MBEDTLS_CMAC_C) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3023 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3024 | { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3025 | status = psa_cmac_setup( operation, slot ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3026 | } |
| 3027 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3028 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3029 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3030 | if( PSA_ALG_IS_HMAC( alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3031 | { |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3032 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3033 | if( operation->mac_size == 0 || |
| 3034 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3035 | { |
| 3036 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3037 | goto exit; |
| 3038 | } |
| 3039 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3040 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3041 | { |
| 3042 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3043 | goto exit; |
| 3044 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3045 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3046 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3047 | slot->key.data, |
| 3048 | slot->key.bytes, |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 3049 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3050 | } |
| 3051 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3052 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3053 | { |
| 3054 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3055 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3056 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3057 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3058 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3059 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3060 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3061 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3062 | else |
| 3063 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3064 | operation->key_set = 1; |
| 3065 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3066 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3067 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3068 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3069 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3070 | } |
| 3071 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3072 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3073 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3074 | psa_algorithm_t alg ) |
| 3075 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3076 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3080 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3081 | psa_algorithm_t alg ) |
| 3082 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3083 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3084 | } |
| 3085 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3086 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3087 | const uint8_t *input, |
| 3088 | size_t input_length ) |
| 3089 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3090 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3091 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3092 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3093 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3094 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3095 | operation->has_input = 1; |
| 3096 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3097 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3098 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3099 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3100 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3101 | input, input_length ); |
| 3102 | status = mbedtls_to_psa_error( ret ); |
| 3103 | } |
| 3104 | else |
| 3105 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3106 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3107 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3108 | { |
| 3109 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3110 | input_length ); |
| 3111 | } |
| 3112 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3113 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3114 | { |
| 3115 | /* This shouldn't happen if `operation` was initialized by |
| 3116 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3117 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3118 | } |
| 3119 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3120 | if( status != PSA_SUCCESS ) |
| 3121 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3122 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3123 | } |
| 3124 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3125 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3126 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3127 | uint8_t *mac, |
| 3128 | size_t mac_size ) |
| 3129 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3130 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3131 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3132 | size_t hash_size = 0; |
| 3133 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3134 | psa_status_t status; |
| 3135 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3136 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3137 | if( status != PSA_SUCCESS ) |
| 3138 | return( status ); |
| 3139 | /* From here on, tmp needs to be wiped. */ |
| 3140 | |
| 3141 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3142 | if( status != PSA_SUCCESS ) |
| 3143 | goto exit; |
| 3144 | |
| 3145 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3146 | if( status != PSA_SUCCESS ) |
| 3147 | goto exit; |
| 3148 | |
| 3149 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3150 | if( status != PSA_SUCCESS ) |
| 3151 | goto exit; |
| 3152 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3153 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3154 | if( status != PSA_SUCCESS ) |
| 3155 | goto exit; |
| 3156 | |
| 3157 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3158 | |
| 3159 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3160 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3161 | return( status ); |
| 3162 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3163 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3164 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3165 | 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] | 3166 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3167 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3168 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3169 | if( ! operation->key_set ) |
| 3170 | return( PSA_ERROR_BAD_STATE ); |
| 3171 | if( operation->iv_required && ! operation->iv_set ) |
| 3172 | return( PSA_ERROR_BAD_STATE ); |
| 3173 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3174 | if( mac_size < operation->mac_size ) |
| 3175 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3176 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3177 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3178 | if( operation->alg == PSA_ALG_CMAC ) |
| 3179 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3180 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3181 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3182 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3183 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3184 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3185 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3186 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3187 | else |
| 3188 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3189 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3190 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3191 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3192 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3193 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3194 | } |
| 3195 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3196 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3197 | { |
| 3198 | /* This shouldn't happen if `operation` was initialized by |
| 3199 | * a setup function. */ |
| 3200 | return( PSA_ERROR_BAD_STATE ); |
| 3201 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3204 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3205 | uint8_t *mac, |
| 3206 | size_t mac_size, |
| 3207 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3208 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3209 | psa_status_t status; |
| 3210 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3211 | if( operation->alg == 0 ) |
| 3212 | { |
| 3213 | return( PSA_ERROR_BAD_STATE ); |
| 3214 | } |
| 3215 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3216 | /* Fill the output buffer with something that isn't a valid mac |
| 3217 | * (barring an attack on the mac and deliberately-crafted input), |
| 3218 | * in case the caller doesn't check the return status properly. */ |
| 3219 | *mac_length = mac_size; |
| 3220 | /* If mac_size is 0 then mac may be NULL and then the |
| 3221 | * call to memset would have undefined behavior. */ |
| 3222 | if( mac_size != 0 ) |
| 3223 | memset( mac, '!', mac_size ); |
| 3224 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3225 | if( ! operation->is_sign ) |
| 3226 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3227 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3228 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3229 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3230 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3231 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3232 | if( status == PSA_SUCCESS ) |
| 3233 | { |
| 3234 | status = psa_mac_abort( operation ); |
| 3235 | if( status == PSA_SUCCESS ) |
| 3236 | *mac_length = operation->mac_size; |
| 3237 | else |
| 3238 | memset( mac, '!', mac_size ); |
| 3239 | } |
| 3240 | else |
| 3241 | psa_mac_abort( operation ); |
| 3242 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3245 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3246 | const uint8_t *mac, |
| 3247 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3248 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3249 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3250 | psa_status_t status; |
| 3251 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3252 | if( operation->alg == 0 ) |
| 3253 | { |
| 3254 | return( PSA_ERROR_BAD_STATE ); |
| 3255 | } |
| 3256 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3257 | if( operation->is_sign ) |
| 3258 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3259 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3260 | } |
| 3261 | if( operation->mac_size != mac_length ) |
| 3262 | { |
| 3263 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3264 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3265 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3266 | |
| 3267 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3268 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3269 | if( status != PSA_SUCCESS ) |
| 3270 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3271 | |
| 3272 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3273 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3274 | |
| 3275 | cleanup: |
| 3276 | if( status == PSA_SUCCESS ) |
| 3277 | status = psa_mac_abort( operation ); |
| 3278 | else |
| 3279 | psa_mac_abort( operation ); |
| 3280 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3281 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3282 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3283 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3287 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3288 | /****************************************************************/ |
| 3289 | /* Asymmetric cryptography */ |
| 3290 | /****************************************************************/ |
| 3291 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3292 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3293 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3294 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3295 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3296 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3297 | size_t hash_length, |
| 3298 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3299 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3300 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3301 | 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] | 3302 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3303 | |
| 3304 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3305 | * parameters. Validate that it fits so that we don't risk an |
| 3306 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3307 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3308 | if( hash_length > UINT_MAX ) |
| 3309 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3310 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3311 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3312 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3313 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3314 | * must be correct. */ |
| 3315 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3316 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3317 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3318 | if( md_info == NULL ) |
| 3319 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3320 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3321 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3322 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3323 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3324 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3325 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3326 | /* PSS requires a hash internally. */ |
| 3327 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3328 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3329 | if( md_info == NULL ) |
| 3330 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3331 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3332 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3334 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3335 | } |
| 3336 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3337 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3338 | psa_algorithm_t alg, |
| 3339 | const uint8_t *hash, |
| 3340 | size_t hash_length, |
| 3341 | uint8_t *signature, |
| 3342 | size_t signature_size, |
| 3343 | size_t *signature_length ) |
| 3344 | { |
| 3345 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3346 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3347 | mbedtls_md_type_t md_alg; |
| 3348 | |
| 3349 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3350 | if( status != PSA_SUCCESS ) |
| 3351 | return( status ); |
| 3352 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3353 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3354 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3355 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3356 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3357 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3358 | { |
| 3359 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3360 | MBEDTLS_MD_NONE ); |
| 3361 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3362 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3363 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3364 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3365 | md_alg, |
| 3366 | (unsigned int) hash_length, |
| 3367 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3368 | signature ); |
| 3369 | } |
| 3370 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3371 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3372 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3373 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3374 | { |
| 3375 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3376 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3377 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3378 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3379 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3380 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3381 | (unsigned int) hash_length, |
| 3382 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3383 | signature ); |
| 3384 | } |
| 3385 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3386 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3387 | { |
| 3388 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3389 | } |
| 3390 | |
| 3391 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3392 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3393 | return( mbedtls_to_psa_error( ret ) ); |
| 3394 | } |
| 3395 | |
| 3396 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3397 | psa_algorithm_t alg, |
| 3398 | const uint8_t *hash, |
| 3399 | size_t hash_length, |
| 3400 | const uint8_t *signature, |
| 3401 | size_t signature_length ) |
| 3402 | { |
| 3403 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3404 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3405 | mbedtls_md_type_t md_alg; |
| 3406 | |
| 3407 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3408 | if( status != PSA_SUCCESS ) |
| 3409 | return( status ); |
| 3410 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3411 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3412 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3413 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3414 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3415 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3416 | { |
| 3417 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3418 | MBEDTLS_MD_NONE ); |
| 3419 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3420 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3421 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3422 | MBEDTLS_RSA_PUBLIC, |
| 3423 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3424 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3425 | hash, |
| 3426 | signature ); |
| 3427 | } |
| 3428 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3429 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3430 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3431 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3432 | { |
| 3433 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3434 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3435 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3436 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3437 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3438 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3439 | (unsigned int) hash_length, |
| 3440 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3441 | signature ); |
| 3442 | } |
| 3443 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3444 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3445 | { |
| 3446 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3447 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3448 | |
| 3449 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3450 | * the rest of the signature is invalid". This has little use in |
| 3451 | * practice and PSA doesn't report this distinction. */ |
| 3452 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3453 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3454 | return( mbedtls_to_psa_error( ret ) ); |
| 3455 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3456 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3457 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3458 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3459 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3460 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3461 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3462 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3463 | * (even though these functions don't modify it). */ |
| 3464 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3465 | psa_algorithm_t alg, |
| 3466 | const uint8_t *hash, |
| 3467 | size_t hash_length, |
| 3468 | uint8_t *signature, |
| 3469 | size_t signature_size, |
| 3470 | size_t *signature_length ) |
| 3471 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3472 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3473 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3474 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3475 | mbedtls_mpi_init( &r ); |
| 3476 | mbedtls_mpi_init( &s ); |
| 3477 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3478 | if( signature_size < 2 * curve_bytes ) |
| 3479 | { |
| 3480 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3481 | goto cleanup; |
| 3482 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3483 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3484 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3485 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3486 | { |
| 3487 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3488 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3489 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3490 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3491 | &ecp->d, hash, |
| 3492 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3493 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3494 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3495 | } |
| 3496 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3497 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3498 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3499 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3500 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3501 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3502 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3503 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3504 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3505 | |
| 3506 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3507 | signature, |
| 3508 | curve_bytes ) ); |
| 3509 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3510 | signature + curve_bytes, |
| 3511 | curve_bytes ) ); |
| 3512 | |
| 3513 | cleanup: |
| 3514 | mbedtls_mpi_free( &r ); |
| 3515 | mbedtls_mpi_free( &s ); |
| 3516 | if( ret == 0 ) |
| 3517 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3518 | return( mbedtls_to_psa_error( ret ) ); |
| 3519 | } |
| 3520 | |
| 3521 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3522 | const uint8_t *hash, |
| 3523 | size_t hash_length, |
| 3524 | const uint8_t *signature, |
| 3525 | size_t signature_length ) |
| 3526 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3527 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3528 | mbedtls_mpi r, s; |
| 3529 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3530 | mbedtls_mpi_init( &r ); |
| 3531 | mbedtls_mpi_init( &s ); |
| 3532 | |
| 3533 | if( signature_length != 2 * curve_bytes ) |
| 3534 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3535 | |
| 3536 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3537 | signature, |
| 3538 | curve_bytes ) ); |
| 3539 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3540 | signature + curve_bytes, |
| 3541 | curve_bytes ) ); |
| 3542 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3543 | /* Check whether the public part is loaded. If not, load it. */ |
| 3544 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3545 | { |
| 3546 | MBEDTLS_MPI_CHK( |
| 3547 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3548 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3549 | } |
| 3550 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3551 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3552 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3553 | |
| 3554 | cleanup: |
| 3555 | mbedtls_mpi_free( &r ); |
| 3556 | mbedtls_mpi_free( &s ); |
| 3557 | return( mbedtls_to_psa_error( ret ) ); |
| 3558 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3559 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3560 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3561 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3562 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3563 | psa_algorithm_t alg, |
| 3564 | const uint8_t *hash, |
| 3565 | size_t hash_length, |
| 3566 | uint8_t *signature, |
| 3567 | size_t signature_size, |
| 3568 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3569 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3570 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3571 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3572 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3573 | |
| 3574 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3575 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3576 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3577 | * buffer can in principle be empty since it doesn't actually have |
| 3578 | * to be a hash.) */ |
| 3579 | if( signature_size == 0 ) |
| 3580 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3581 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3582 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3583 | PSA_KEY_USAGE_SIGN_HASH, |
| 3584 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3585 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3586 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3587 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3588 | { |
| 3589 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3590 | goto exit; |
| 3591 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3592 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3593 | /* Try any of the available accelerators first */ |
| 3594 | status = psa_driver_wrapper_sign_hash( slot, |
| 3595 | alg, |
| 3596 | hash, |
| 3597 | hash_length, |
| 3598 | signature, |
| 3599 | signature_size, |
| 3600 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3601 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3602 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3603 | goto exit; |
| 3604 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3605 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3606 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3607 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3608 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3609 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3610 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3611 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3612 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3613 | slot->key.data, |
| 3614 | slot->key.bytes, |
| 3615 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3616 | if( status != PSA_SUCCESS ) |
| 3617 | goto exit; |
| 3618 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3619 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3620 | alg, |
| 3621 | hash, hash_length, |
| 3622 | signature, signature_size, |
| 3623 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3624 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3625 | mbedtls_rsa_free( rsa ); |
| 3626 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3627 | } |
| 3628 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3629 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3630 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3631 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3632 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3633 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3634 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3635 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3636 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3637 | PSA_ALG_IS_ECDSA( alg ) |
| 3638 | #else |
| 3639 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3640 | #endif |
| 3641 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3642 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3643 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3644 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 3645 | slot->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3646 | slot->key.data, |
| 3647 | slot->key.bytes, |
| 3648 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3649 | if( status != PSA_SUCCESS ) |
| 3650 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3651 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3652 | alg, |
| 3653 | hash, hash_length, |
| 3654 | signature, signature_size, |
| 3655 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3656 | mbedtls_ecp_keypair_free( ecp ); |
| 3657 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3658 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3659 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3660 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3661 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3662 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3663 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3664 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3665 | } |
| 3666 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3667 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3668 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3669 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3670 | |
| 3671 | exit: |
| 3672 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3673 | * the trailing part on success) with something that isn't a valid mac |
| 3674 | * (barring an attack on the mac and deliberately-crafted input), |
| 3675 | * in case the caller doesn't check the return status properly. */ |
| 3676 | if( status == PSA_SUCCESS ) |
| 3677 | memset( signature + *signature_length, '!', |
| 3678 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3679 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3680 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3681 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3682 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3683 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3684 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3685 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3686 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3687 | } |
| 3688 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3689 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3690 | psa_algorithm_t alg, |
| 3691 | const uint8_t *hash, |
| 3692 | size_t hash_length, |
| 3693 | const uint8_t *signature, |
| 3694 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3695 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3696 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3697 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3698 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3699 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3700 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3701 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3702 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3703 | if( status != PSA_SUCCESS ) |
| 3704 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3705 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3706 | /* Try any of the available accelerators first */ |
| 3707 | status = psa_driver_wrapper_verify_hash( slot, |
| 3708 | alg, |
| 3709 | hash, |
| 3710 | hash_length, |
| 3711 | signature, |
| 3712 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3713 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3714 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3715 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3716 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3717 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3718 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3719 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3720 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3721 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3722 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3723 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3724 | slot->key.data, |
| 3725 | slot->key.bytes, |
| 3726 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3727 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3728 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3729 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3730 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3731 | alg, |
| 3732 | hash, hash_length, |
| 3733 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3734 | mbedtls_rsa_free( rsa ); |
| 3735 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3736 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3737 | } |
| 3738 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3739 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3740 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3741 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3742 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3743 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3744 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3745 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3746 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3747 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3748 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 3749 | slot->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3750 | slot->key.data, |
| 3751 | slot->key.bytes, |
| 3752 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3753 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3754 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3755 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3756 | hash, hash_length, |
| 3757 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3758 | mbedtls_ecp_keypair_free( ecp ); |
| 3759 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3760 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3761 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3762 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3763 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3764 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3765 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3766 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3767 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3768 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3769 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3770 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3771 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3772 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3773 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3774 | |
| 3775 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3776 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3777 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3778 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3781 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3782 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3783 | mbedtls_rsa_context *rsa ) |
| 3784 | { |
| 3785 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3786 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3787 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3788 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3789 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3790 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3791 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3792 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3793 | psa_algorithm_t alg, |
| 3794 | const uint8_t *input, |
| 3795 | size_t input_length, |
| 3796 | const uint8_t *salt, |
| 3797 | size_t salt_length, |
| 3798 | uint8_t *output, |
| 3799 | size_t output_size, |
| 3800 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3801 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3802 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3803 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3804 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3805 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3806 | (void) input; |
| 3807 | (void) input_length; |
| 3808 | (void) salt; |
| 3809 | (void) output; |
| 3810 | (void) output_size; |
| 3811 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3812 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3813 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3814 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3815 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3816 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3817 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3818 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3819 | if( status != PSA_SUCCESS ) |
| 3820 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3821 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3822 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3823 | { |
| 3824 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3825 | goto exit; |
| 3826 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3827 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3828 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3829 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3830 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3831 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3832 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3833 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3834 | slot->key.data, |
| 3835 | slot->key.bytes, |
| 3836 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3837 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3838 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3839 | |
| 3840 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3841 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3842 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3843 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3844 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3845 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3846 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3847 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3848 | status = mbedtls_to_psa_error( |
| 3849 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3850 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3851 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3852 | MBEDTLS_RSA_PUBLIC, |
| 3853 | input_length, |
| 3854 | input, |
| 3855 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3856 | } |
| 3857 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3858 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3859 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3860 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3861 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3862 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3863 | status = mbedtls_to_psa_error( |
| 3864 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3865 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3866 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3867 | MBEDTLS_RSA_PUBLIC, |
| 3868 | salt, salt_length, |
| 3869 | input_length, |
| 3870 | input, |
| 3871 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3872 | } |
| 3873 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3874 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3875 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3876 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3877 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3878 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3879 | rsa_exit: |
| 3880 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3881 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3882 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3883 | mbedtls_rsa_free( rsa ); |
| 3884 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3885 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3886 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3887 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3888 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3889 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3890 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3891 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3892 | |
| 3893 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3894 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3895 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3896 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3897 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3898 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3899 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3900 | psa_algorithm_t alg, |
| 3901 | const uint8_t *input, |
| 3902 | size_t input_length, |
| 3903 | const uint8_t *salt, |
| 3904 | size_t salt_length, |
| 3905 | uint8_t *output, |
| 3906 | size_t output_size, |
| 3907 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3908 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3909 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3910 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3911 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3912 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3913 | (void) input; |
| 3914 | (void) input_length; |
| 3915 | (void) salt; |
| 3916 | (void) output; |
| 3917 | (void) output_size; |
| 3918 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3919 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3920 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3921 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3922 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3923 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3924 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3925 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3926 | if( status != PSA_SUCCESS ) |
| 3927 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3928 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3929 | { |
| 3930 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3931 | goto exit; |
| 3932 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3933 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3934 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3935 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3936 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3937 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3938 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3939 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3940 | slot->key.data, |
| 3941 | slot->key.bytes, |
| 3942 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3943 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3944 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3945 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3946 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3947 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3948 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3949 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3950 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3951 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3952 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3953 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3954 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3955 | status = mbedtls_to_psa_error( |
| 3956 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3957 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3958 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3959 | MBEDTLS_RSA_PRIVATE, |
| 3960 | output_length, |
| 3961 | input, |
| 3962 | output, |
| 3963 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3964 | } |
| 3965 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3966 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 3967 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3968 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3969 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3970 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3971 | status = mbedtls_to_psa_error( |
| 3972 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3973 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3974 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3975 | MBEDTLS_RSA_PRIVATE, |
| 3976 | salt, salt_length, |
| 3977 | output_length, |
| 3978 | input, |
| 3979 | output, |
| 3980 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3981 | } |
| 3982 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3983 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3984 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3985 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3986 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3987 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3988 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3989 | mbedtls_rsa_free( rsa ); |
| 3990 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3991 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3992 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3993 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3994 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3995 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3996 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3997 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3998 | |
| 3999 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4000 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4001 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4002 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4003 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4004 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4005 | |
| 4006 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4007 | /****************************************************************/ |
| 4008 | /* Symmetric cryptography */ |
| 4009 | /****************************************************************/ |
| 4010 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4011 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4012 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4013 | psa_algorithm_t alg, |
| 4014 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4015 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4016 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4017 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4018 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4019 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4020 | size_t key_bits; |
| 4021 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4022 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4023 | PSA_KEY_USAGE_ENCRYPT : |
| 4024 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4025 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4026 | /* A context must be freshly initialized before it can be set up. */ |
| 4027 | if( operation->alg != 0 ) |
| 4028 | return( PSA_ERROR_BAD_STATE ); |
| 4029 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4030 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4031 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4032 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4033 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4034 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4035 | 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] | 4036 | if( status != PSA_SUCCESS ) |
| 4037 | goto exit; |
| 4038 | |
| 4039 | /* Initialize the operation struct members, except for alg. The alg member |
| 4040 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4041 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4042 | operation->key_set = 0; |
| 4043 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4044 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4045 | operation->iv_size = 0; |
| 4046 | operation->block_size = 0; |
| 4047 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4048 | operation->iv_required = 0; |
| 4049 | else |
| 4050 | operation->iv_required = 1; |
| 4051 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4052 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4053 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4054 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4055 | slot, |
| 4056 | alg ); |
| 4057 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4058 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4059 | slot, |
| 4060 | alg ); |
| 4061 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4062 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4063 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4064 | /* Once the driver context is initialised, it needs to be freed using |
| 4065 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4066 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4067 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4068 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4069 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4070 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4071 | goto exit; |
| 4072 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4073 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4074 | * available for the given algorithm & key. */ |
| 4075 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4076 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4077 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4078 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4079 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4080 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4081 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4082 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4083 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4084 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4085 | 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] | 4086 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4087 | { |
| 4088 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4089 | goto exit; |
| 4090 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4091 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4092 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4093 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4094 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4095 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4096 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4097 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4098 | { |
| 4099 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4100 | uint8_t keys[24]; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4101 | memcpy( keys, slot->key.data, 16 ); |
| 4102 | memcpy( keys + 16, slot->key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4103 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4104 | keys, |
| 4105 | 192, cipher_operation ); |
| 4106 | } |
| 4107 | else |
| 4108 | #endif |
| 4109 | { |
| 4110 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4111 | slot->key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4112 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4113 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4114 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4115 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4116 | |
David Brown | 63ca260 | 2021-01-26 11:51:12 -0700 | [diff] [blame] | 4117 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 4118 | defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4119 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4120 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4121 | case PSA_ALG_CBC_NO_PADDING: |
| 4122 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4123 | MBEDTLS_PADDING_NONE ); |
| 4124 | break; |
| 4125 | case PSA_ALG_CBC_PKCS7: |
| 4126 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4127 | MBEDTLS_PADDING_PKCS7 ); |
| 4128 | break; |
| 4129 | default: |
| 4130 | /* The algorithm doesn't involve padding. */ |
| 4131 | ret = 0; |
| 4132 | break; |
| 4133 | } |
| 4134 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4135 | goto exit; |
David Brown | 288a96e | 2021-02-09 16:27:55 -0700 | [diff] [blame] | 4136 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */ |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4137 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4138 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4139 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4140 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4141 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4142 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4143 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4144 | } |
David Brown | 1bfe4d7 | 2021-02-16 12:54:35 -0700 | [diff] [blame] | 4145 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4146 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4147 | 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] | 4148 | operation->iv_size = 12; |
| 4149 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4150 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4151 | status = PSA_SUCCESS; |
| 4152 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4153 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4154 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4155 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4156 | if( status == PSA_SUCCESS ) |
| 4157 | { |
| 4158 | /* Update operation flags for both driver and software implementations */ |
| 4159 | operation->key_set = 1; |
| 4160 | } |
| 4161 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4162 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4163 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4164 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4165 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4166 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4169 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4170 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4171 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4172 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4173 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4174 | } |
| 4175 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4176 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4177 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4178 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4179 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4180 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4181 | } |
| 4182 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4183 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4184 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4185 | size_t iv_size, |
| 4186 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4187 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4188 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4189 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4190 | if( operation->iv_set || ! operation->iv_required ) |
| 4191 | { |
| 4192 | return( PSA_ERROR_BAD_STATE ); |
| 4193 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4194 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4195 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4196 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4197 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4198 | iv, |
| 4199 | iv_size, |
| 4200 | iv_length ); |
| 4201 | goto exit; |
| 4202 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4203 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4204 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4205 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4206 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4207 | goto exit; |
| 4208 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4209 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4210 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4211 | if( ret != 0 ) |
| 4212 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4213 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4214 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4215 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4216 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4217 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4218 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4219 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4220 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4221 | if( status == PSA_SUCCESS ) |
| 4222 | operation->iv_set = 1; |
| 4223 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4224 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4225 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4228 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4229 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4230 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4231 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4232 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4233 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4234 | if( operation->iv_set || ! operation->iv_required ) |
| 4235 | { |
| 4236 | return( PSA_ERROR_BAD_STATE ); |
| 4237 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4238 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4239 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4240 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4241 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4242 | iv, |
| 4243 | iv_length ); |
| 4244 | goto exit; |
| 4245 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4246 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4247 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4248 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4249 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4250 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4251 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4252 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4253 | status = mbedtls_to_psa_error( ret ); |
| 4254 | exit: |
| 4255 | if( status == PSA_SUCCESS ) |
| 4256 | operation->iv_set = 1; |
| 4257 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4258 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4259 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4260 | } |
| 4261 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4262 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4263 | * manual processing, since the PSA API is defined as being able to process |
| 4264 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4265 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4266 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4267 | mbedtls_cipher_context_t *ctx, |
| 4268 | const uint8_t *input, |
| 4269 | size_t input_length, |
| 4270 | uint8_t *output, |
| 4271 | size_t output_size, |
| 4272 | size_t *output_length ) |
| 4273 | { |
| 4274 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4275 | size_t block_size = ctx->cipher_info->block_size; |
| 4276 | size_t internal_output_length = 0; |
| 4277 | *output_length = 0; |
| 4278 | |
| 4279 | if( input_length == 0 ) |
| 4280 | { |
| 4281 | status = PSA_SUCCESS; |
| 4282 | goto exit; |
| 4283 | } |
| 4284 | |
| 4285 | if( ctx->unprocessed_len > 0 ) |
| 4286 | { |
| 4287 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4288 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4289 | |
| 4290 | if( input_length < bytes_to_copy ) |
| 4291 | bytes_to_copy = input_length; |
| 4292 | |
| 4293 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4294 | input, bytes_to_copy ); |
| 4295 | input_length -= bytes_to_copy; |
| 4296 | input += bytes_to_copy; |
| 4297 | ctx->unprocessed_len += bytes_to_copy; |
| 4298 | |
| 4299 | if( ctx->unprocessed_len == block_size ) |
| 4300 | { |
| 4301 | status = mbedtls_to_psa_error( |
| 4302 | mbedtls_cipher_update( ctx, |
| 4303 | ctx->unprocessed_data, |
| 4304 | block_size, |
| 4305 | output, &internal_output_length ) ); |
| 4306 | |
| 4307 | if( status != PSA_SUCCESS ) |
| 4308 | goto exit; |
| 4309 | |
| 4310 | output += internal_output_length; |
| 4311 | output_size -= internal_output_length; |
| 4312 | *output_length += internal_output_length; |
| 4313 | ctx->unprocessed_len = 0; |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | while( input_length >= block_size ) |
| 4318 | { |
| 4319 | /* Run all full blocks we have, one by one */ |
| 4320 | status = mbedtls_to_psa_error( |
| 4321 | mbedtls_cipher_update( ctx, input, |
| 4322 | block_size, |
| 4323 | output, &internal_output_length ) ); |
| 4324 | |
| 4325 | if( status != PSA_SUCCESS ) |
| 4326 | goto exit; |
| 4327 | |
| 4328 | input_length -= block_size; |
| 4329 | input += block_size; |
| 4330 | |
| 4331 | output += internal_output_length; |
| 4332 | output_size -= internal_output_length; |
| 4333 | *output_length += internal_output_length; |
| 4334 | } |
| 4335 | |
| 4336 | if( input_length > 0 ) |
| 4337 | { |
| 4338 | /* Save unprocessed bytes for later processing */ |
| 4339 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4340 | input, input_length ); |
| 4341 | ctx->unprocessed_len += input_length; |
| 4342 | } |
| 4343 | |
| 4344 | status = PSA_SUCCESS; |
| 4345 | |
| 4346 | exit: |
| 4347 | return( status ); |
| 4348 | } |
| 4349 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4350 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4351 | const uint8_t *input, |
| 4352 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4353 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4354 | size_t output_size, |
| 4355 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4356 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4357 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4358 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4359 | if( operation->alg == 0 ) |
| 4360 | { |
| 4361 | return( PSA_ERROR_BAD_STATE ); |
| 4362 | } |
| 4363 | if( operation->iv_required && ! operation->iv_set ) |
| 4364 | { |
| 4365 | return( PSA_ERROR_BAD_STATE ); |
| 4366 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4367 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4368 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4369 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4370 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4371 | input, |
| 4372 | input_length, |
| 4373 | output, |
| 4374 | output_size, |
| 4375 | output_length ); |
| 4376 | goto exit; |
| 4377 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4378 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4379 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4380 | { |
| 4381 | /* Take the unprocessed partial block left over from previous |
| 4382 | * update calls, if any, plus the input to this call. Remove |
| 4383 | * the last partial block, if any. You get the data that will be |
| 4384 | * output in this call. */ |
| 4385 | expected_output_size = |
| 4386 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4387 | / operation->block_size * operation->block_size; |
| 4388 | } |
| 4389 | else |
| 4390 | { |
| 4391 | expected_output_size = input_length; |
| 4392 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4393 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4394 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4395 | { |
| 4396 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4397 | goto exit; |
| 4398 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4399 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4400 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4401 | { |
| 4402 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4403 | * process a single block at a time in ECB mode. Abstract away that |
| 4404 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4405 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4406 | input, |
| 4407 | input_length, |
| 4408 | output, |
| 4409 | output_size, |
| 4410 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4411 | } |
| 4412 | else |
| 4413 | { |
| 4414 | status = mbedtls_to_psa_error( |
| 4415 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4416 | input_length, output, output_length ) ); |
| 4417 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4418 | exit: |
| 4419 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4420 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4421 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4422 | } |
| 4423 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4424 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4425 | uint8_t *output, |
| 4426 | size_t output_size, |
| 4427 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4428 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4429 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4430 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4431 | if( operation->alg == 0 ) |
| 4432 | { |
| 4433 | return( PSA_ERROR_BAD_STATE ); |
| 4434 | } |
| 4435 | if( operation->iv_required && ! operation->iv_set ) |
| 4436 | { |
| 4437 | return( PSA_ERROR_BAD_STATE ); |
| 4438 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4439 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4440 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4441 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4442 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4443 | output, |
| 4444 | output_size, |
| 4445 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4446 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4447 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4448 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4449 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4450 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4451 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4452 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4453 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4454 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4455 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4456 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4457 | } |
| 4458 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4459 | status = mbedtls_to_psa_error( |
| 4460 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4461 | temp_output_buffer, |
| 4462 | output_length ) ); |
| 4463 | if( status != PSA_SUCCESS ) |
| 4464 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4465 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4466 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4467 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4468 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4469 | memcpy( output, temp_output_buffer, *output_length ); |
| 4470 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4471 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4472 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4473 | exit: |
| 4474 | if( operation->mbedtls_in_use == 1 ) |
| 4475 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4476 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4477 | if( status == PSA_SUCCESS ) |
| 4478 | return( psa_cipher_abort( operation ) ); |
| 4479 | else |
| 4480 | { |
| 4481 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4482 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4483 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4484 | return( status ); |
| 4485 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4486 | } |
| 4487 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4488 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4489 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4490 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4491 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4492 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4493 | * in use. It's ok to call abort on such an object, and there's |
| 4494 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4495 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4496 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4497 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4498 | /* Sanity check (shouldn't happen: operation->alg should |
| 4499 | * always have been initialized to a valid value). */ |
| 4500 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4501 | return( PSA_ERROR_BAD_STATE ); |
| 4502 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4503 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4504 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4505 | else |
| 4506 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4507 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4508 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4509 | operation->key_set = 0; |
| 4510 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4511 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4512 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4513 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4514 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4515 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4516 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4517 | } |
| 4518 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4519 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4520 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4521 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4522 | /****************************************************************/ |
| 4523 | /* AEAD */ |
| 4524 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4525 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4526 | typedef struct |
| 4527 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4528 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4529 | const mbedtls_cipher_info_t *cipher_info; |
| 4530 | union |
| 4531 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4532 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4533 | #if defined(MBEDTLS_CCM_C) |
| 4534 | mbedtls_ccm_context ccm; |
| 4535 | #endif /* MBEDTLS_CCM_C */ |
| 4536 | #if defined(MBEDTLS_GCM_C) |
| 4537 | mbedtls_gcm_context gcm; |
| 4538 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4539 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4540 | mbedtls_chachapoly_context chachapoly; |
| 4541 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4542 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4543 | psa_algorithm_t core_alg; |
| 4544 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4545 | uint8_t tag_length; |
| 4546 | } aead_operation_t; |
| 4547 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4548 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4549 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4550 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4551 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4552 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4553 | { |
| 4554 | #if defined(MBEDTLS_CCM_C) |
| 4555 | case PSA_ALG_CCM: |
| 4556 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4557 | break; |
| 4558 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4559 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4560 | case PSA_ALG_GCM: |
| 4561 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4562 | break; |
| 4563 | #endif /* MBEDTLS_GCM_C */ |
| 4564 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4565 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4566 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4570 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4571 | psa_key_usage_t usage, |
| 4572 | psa_algorithm_t alg ) |
| 4573 | { |
| 4574 | psa_status_t status; |
| 4575 | size_t key_bits; |
| 4576 | mbedtls_cipher_id_t cipher_id; |
| 4577 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4578 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4579 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4580 | if( status != PSA_SUCCESS ) |
| 4581 | return( status ); |
| 4582 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4583 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4584 | |
| 4585 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4586 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4587 | &cipher_id ); |
| 4588 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4589 | { |
| 4590 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4591 | goto cleanup; |
| 4592 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4593 | |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4594 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4595 | { |
| 4596 | #if defined(MBEDTLS_CCM_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4597 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4598 | operation->core_alg = PSA_ALG_CCM; |
| 4599 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4600 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4601 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4602 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4603 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4604 | { |
| 4605 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4606 | goto cleanup; |
| 4607 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4608 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4609 | status = mbedtls_to_psa_error( |
| 4610 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4611 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4612 | (unsigned int) key_bits ) ); |
| 4613 | if( status != 0 ) |
| 4614 | goto cleanup; |
| 4615 | break; |
| 4616 | #endif /* MBEDTLS_CCM_C */ |
| 4617 | |
| 4618 | #if defined(MBEDTLS_GCM_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4619 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4620 | operation->core_alg = PSA_ALG_GCM; |
| 4621 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4622 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4623 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4624 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4625 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4626 | { |
| 4627 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4628 | goto cleanup; |
| 4629 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4630 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4631 | status = mbedtls_to_psa_error( |
| 4632 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4633 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4634 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4635 | if( status != 0 ) |
| 4636 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4637 | break; |
| 4638 | #endif /* MBEDTLS_GCM_C */ |
| 4639 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4640 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 4641 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4642 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4643 | operation->full_tag_length = 16; |
| 4644 | /* We only support the default tag length. */ |
| 4645 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4646 | { |
| 4647 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4648 | goto cleanup; |
| 4649 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4650 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4651 | status = mbedtls_to_psa_error( |
| 4652 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4653 | operation->slot->key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4654 | if( status != 0 ) |
| 4655 | goto cleanup; |
| 4656 | break; |
| 4657 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4658 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4659 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4660 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4661 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4662 | } |
| 4663 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4664 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4665 | { |
| 4666 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4667 | goto cleanup; |
| 4668 | } |
| 4669 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4670 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4671 | return( PSA_SUCCESS ); |
| 4672 | |
| 4673 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4674 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4675 | return( status ); |
| 4676 | } |
| 4677 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4678 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4679 | psa_algorithm_t alg, |
| 4680 | const uint8_t *nonce, |
| 4681 | size_t nonce_length, |
| 4682 | const uint8_t *additional_data, |
| 4683 | size_t additional_data_length, |
| 4684 | const uint8_t *plaintext, |
| 4685 | size_t plaintext_length, |
| 4686 | uint8_t *ciphertext, |
| 4687 | size_t ciphertext_size, |
| 4688 | size_t *ciphertext_length ) |
| 4689 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4690 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4691 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4692 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4693 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4694 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4695 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4696 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4697 | if( status != PSA_SUCCESS ) |
| 4698 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4699 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4700 | /* For all currently supported modes, the tag is at the end of the |
| 4701 | * ciphertext. */ |
| 4702 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4703 | { |
| 4704 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4705 | goto exit; |
| 4706 | } |
| 4707 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4708 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4709 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4710 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4711 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4712 | status = mbedtls_to_psa_error( |
| 4713 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4714 | MBEDTLS_GCM_ENCRYPT, |
| 4715 | plaintext_length, |
| 4716 | nonce, nonce_length, |
| 4717 | additional_data, additional_data_length, |
| 4718 | plaintext, ciphertext, |
| 4719 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4720 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4721 | else |
| 4722 | #endif /* MBEDTLS_GCM_C */ |
| 4723 | #if defined(MBEDTLS_CCM_C) |
| 4724 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4725 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4726 | status = mbedtls_to_psa_error( |
| 4727 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4728 | plaintext_length, |
| 4729 | nonce, nonce_length, |
| 4730 | additional_data, |
| 4731 | additional_data_length, |
| 4732 | plaintext, ciphertext, |
| 4733 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4734 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4735 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4736 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4737 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4738 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4739 | { |
| 4740 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4741 | { |
| 4742 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4743 | goto exit; |
| 4744 | } |
| 4745 | status = mbedtls_to_psa_error( |
| 4746 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4747 | plaintext_length, |
| 4748 | nonce, |
| 4749 | additional_data, |
| 4750 | additional_data_length, |
| 4751 | plaintext, |
| 4752 | ciphertext, |
| 4753 | tag ) ); |
| 4754 | } |
| 4755 | else |
| 4756 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4757 | { |
Steven Cooreman | 7196fef | 2021-02-10 17:13:28 +0100 | [diff] [blame] | 4758 | (void) tag; |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4759 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4760 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4761 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4762 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4763 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4764 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4765 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4766 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4767 | if( status == PSA_SUCCESS ) |
| 4768 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4769 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4770 | } |
| 4771 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4772 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4773 | * followed by the tag. Return the length of the part preceding the tag in |
| 4774 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4775 | * the encrypted data has the same size as the plaintext, such as |
| 4776 | * CCM and GCM. */ |
| 4777 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4778 | const uint8_t *ciphertext, |
| 4779 | size_t ciphertext_length, |
| 4780 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4781 | const uint8_t **p_tag ) |
| 4782 | { |
| 4783 | size_t payload_length; |
| 4784 | if( tag_length > ciphertext_length ) |
| 4785 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4786 | payload_length = ciphertext_length - tag_length; |
| 4787 | if( payload_length > plaintext_size ) |
| 4788 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4789 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4790 | return( PSA_SUCCESS ); |
| 4791 | } |
| 4792 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4793 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4794 | psa_algorithm_t alg, |
| 4795 | const uint8_t *nonce, |
| 4796 | size_t nonce_length, |
| 4797 | const uint8_t *additional_data, |
| 4798 | size_t additional_data_length, |
| 4799 | const uint8_t *ciphertext, |
| 4800 | size_t ciphertext_length, |
| 4801 | uint8_t *plaintext, |
| 4802 | size_t plaintext_size, |
| 4803 | size_t *plaintext_length ) |
| 4804 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4805 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4806 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4807 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4808 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4809 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4810 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4811 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4812 | if( status != PSA_SUCCESS ) |
| 4813 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4814 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4815 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4816 | ciphertext, ciphertext_length, |
| 4817 | plaintext_size, &tag ); |
| 4818 | if( status != PSA_SUCCESS ) |
| 4819 | goto exit; |
| 4820 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4821 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4822 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4823 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4824 | status = mbedtls_to_psa_error( |
| 4825 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4826 | ciphertext_length - operation.tag_length, |
| 4827 | nonce, nonce_length, |
| 4828 | additional_data, |
| 4829 | additional_data_length, |
| 4830 | tag, operation.tag_length, |
| 4831 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4832 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4833 | else |
| 4834 | #endif /* MBEDTLS_GCM_C */ |
| 4835 | #if defined(MBEDTLS_CCM_C) |
| 4836 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4837 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4838 | status = mbedtls_to_psa_error( |
| 4839 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4840 | ciphertext_length - operation.tag_length, |
| 4841 | nonce, nonce_length, |
| 4842 | additional_data, |
| 4843 | additional_data_length, |
| 4844 | ciphertext, plaintext, |
| 4845 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4846 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4847 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4848 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4849 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4850 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4851 | { |
| 4852 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4853 | { |
| 4854 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4855 | goto exit; |
| 4856 | } |
| 4857 | status = mbedtls_to_psa_error( |
| 4858 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4859 | ciphertext_length - operation.tag_length, |
| 4860 | nonce, |
| 4861 | additional_data, |
| 4862 | additional_data_length, |
| 4863 | tag, |
| 4864 | ciphertext, |
| 4865 | plaintext ) ); |
| 4866 | } |
| 4867 | else |
| 4868 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4869 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4870 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4871 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4872 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4873 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4874 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4875 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4876 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4877 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4878 | if( status == PSA_SUCCESS ) |
| 4879 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4880 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4881 | } |
| 4882 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4883 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4884 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4885 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4886 | /* Generators */ |
| 4887 | /****************************************************************/ |
| 4888 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4889 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4890 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4891 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4892 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 4893 | #endif |
| 4894 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4895 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4896 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4897 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4898 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4899 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4900 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4901 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4902 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4903 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4904 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4905 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4906 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4907 | } |
| 4908 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4909 | 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] | 4910 | { |
| 4911 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4912 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4913 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4914 | { |
| 4915 | /* The object has (apparently) been initialized but it is not |
| 4916 | * in use. It's ok to call abort on such an object, and there's |
| 4917 | * nothing to do. */ |
| 4918 | } |
| 4919 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4920 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4921 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4922 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4923 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4924 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4925 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4926 | else |
| 4927 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4928 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4929 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4930 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4931 | /* 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] | 4932 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4933 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4934 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4935 | { |
| 4936 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4937 | operation->ctx.tls12_prf.seed_length ); |
| 4938 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4939 | } |
| 4940 | |
| 4941 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4942 | { |
| 4943 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4944 | operation->ctx.tls12_prf.label_length ); |
| 4945 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4946 | } |
| 4947 | |
| 4948 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4949 | |
| 4950 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4951 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4952 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4953 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4954 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4955 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4956 | { |
| 4957 | status = PSA_ERROR_BAD_STATE; |
| 4958 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4959 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4960 | return( status ); |
| 4961 | } |
| 4962 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4963 | 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] | 4964 | size_t *capacity) |
| 4965 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4966 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4967 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4968 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4969 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4972 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4973 | return( PSA_SUCCESS ); |
| 4974 | } |
| 4975 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4976 | 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] | 4977 | size_t capacity ) |
| 4978 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4979 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4980 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4981 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4982 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4983 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4984 | return( PSA_SUCCESS ); |
| 4985 | } |
| 4986 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4987 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4988 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4989 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4990 | 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] | 4991 | psa_algorithm_t hash_alg, |
| 4992 | uint8_t *output, |
| 4993 | size_t output_length ) |
| 4994 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4995 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4996 | psa_status_t status; |
| 4997 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4998 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4999 | return( PSA_ERROR_BAD_STATE ); |
| 5000 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5001 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5002 | while( output_length != 0 ) |
| 5003 | { |
| 5004 | /* Copy what remains of the current block */ |
| 5005 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5006 | if( n > output_length ) |
| 5007 | n = (uint8_t) output_length; |
| 5008 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5009 | output += n; |
| 5010 | output_length -= n; |
| 5011 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5012 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5013 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5014 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5015 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5016 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5017 | * object was corrupted or if this function is called directly |
| 5018 | * inside the library. */ |
| 5019 | if( hkdf->block_number == 0xff ) |
| 5020 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5021 | |
| 5022 | /* We need a new block */ |
| 5023 | ++hkdf->block_number; |
| 5024 | hkdf->offset_in_block = 0; |
| 5025 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5026 | hkdf->prk, hash_length, |
| 5027 | hash_alg ); |
| 5028 | if( status != PSA_SUCCESS ) |
| 5029 | return( status ); |
| 5030 | if( hkdf->block_number != 1 ) |
| 5031 | { |
| 5032 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5033 | hkdf->output_block, |
| 5034 | hash_length ); |
| 5035 | if( status != PSA_SUCCESS ) |
| 5036 | return( status ); |
| 5037 | } |
| 5038 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5039 | hkdf->info, |
| 5040 | hkdf->info_length ); |
| 5041 | if( status != PSA_SUCCESS ) |
| 5042 | return( status ); |
| 5043 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5044 | &hkdf->block_number, 1 ); |
| 5045 | if( status != PSA_SUCCESS ) |
| 5046 | return( status ); |
| 5047 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5048 | hkdf->output_block, |
| 5049 | sizeof( hkdf->output_block ) ); |
| 5050 | if( status != PSA_SUCCESS ) |
| 5051 | return( status ); |
| 5052 | } |
| 5053 | |
| 5054 | return( PSA_SUCCESS ); |
| 5055 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5056 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5057 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5058 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5059 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5060 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5061 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5062 | psa_algorithm_t alg ) |
| 5063 | { |
| 5064 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5065 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5066 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5067 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5068 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5069 | /* We can't be wanting more output after block 0xff, otherwise |
| 5070 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5071 | * prevented this call. It could happen only if the operation |
| 5072 | * object was corrupted or if this function is called directly |
| 5073 | * inside the library. */ |
| 5074 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5075 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5076 | |
| 5077 | /* We need a new block */ |
| 5078 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5079 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5080 | |
| 5081 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5082 | * |
| 5083 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5084 | * |
| 5085 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5086 | * HMAC_hash(secret, A(2) + seed) + |
| 5087 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5088 | * |
| 5089 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5090 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5091 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5092 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5093 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5094 | * is currently extracted as `output_block` and where i is |
| 5095 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5096 | */ |
| 5097 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5098 | /* Save the hash context before using it, to preserve the hash state with |
| 5099 | * only the inner padding in it. We need this, because inner padding depends |
| 5100 | * on the key (secret in the RFC's terminology). */ |
| 5101 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5102 | if( status != PSA_SUCCESS ) |
| 5103 | goto cleanup; |
| 5104 | |
| 5105 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5106 | if( tls12_prf->block_number == 1 ) |
| 5107 | { |
| 5108 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5109 | * the variable seed and in this instance means it in the context of the |
| 5110 | * P_hash function, where seed = label + seed.) */ |
| 5111 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5112 | tls12_prf->label, tls12_prf->label_length ); |
| 5113 | if( status != PSA_SUCCESS ) |
| 5114 | goto cleanup; |
| 5115 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5116 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5117 | if( status != PSA_SUCCESS ) |
| 5118 | goto cleanup; |
| 5119 | } |
| 5120 | else |
| 5121 | { |
| 5122 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5123 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5124 | tls12_prf->Ai, hash_length ); |
| 5125 | if( status != PSA_SUCCESS ) |
| 5126 | goto cleanup; |
| 5127 | } |
| 5128 | |
| 5129 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5130 | tls12_prf->Ai, hash_length ); |
| 5131 | if( status != PSA_SUCCESS ) |
| 5132 | goto cleanup; |
| 5133 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5134 | if( status != PSA_SUCCESS ) |
| 5135 | goto cleanup; |
| 5136 | |
| 5137 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5138 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5139 | tls12_prf->Ai, hash_length ); |
| 5140 | if( status != PSA_SUCCESS ) |
| 5141 | goto cleanup; |
| 5142 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5143 | tls12_prf->label, tls12_prf->label_length ); |
| 5144 | if( status != PSA_SUCCESS ) |
| 5145 | goto cleanup; |
| 5146 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5147 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5148 | if( status != PSA_SUCCESS ) |
| 5149 | goto cleanup; |
| 5150 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5151 | tls12_prf->output_block, hash_length ); |
| 5152 | if( status != PSA_SUCCESS ) |
| 5153 | goto cleanup; |
| 5154 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5155 | if( status != PSA_SUCCESS ) |
| 5156 | goto cleanup; |
| 5157 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5158 | |
| 5159 | cleanup: |
| 5160 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5161 | cleanup_status = psa_hash_abort( &backup ); |
| 5162 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5163 | status = cleanup_status; |
| 5164 | |
| 5165 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5166 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5167 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5168 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5169 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5170 | psa_algorithm_t alg, |
| 5171 | uint8_t *output, |
| 5172 | size_t output_length ) |
| 5173 | { |
| 5174 | 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] | 5175 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5176 | psa_status_t status; |
| 5177 | uint8_t offset, length; |
| 5178 | |
| 5179 | while( output_length != 0 ) |
| 5180 | { |
| 5181 | /* Check if we have fully processed the current block. */ |
| 5182 | if( tls12_prf->left_in_block == 0 ) |
| 5183 | { |
| 5184 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5185 | alg ); |
| 5186 | if( status != PSA_SUCCESS ) |
| 5187 | return( status ); |
| 5188 | |
| 5189 | continue; |
| 5190 | } |
| 5191 | |
| 5192 | if( tls12_prf->left_in_block > output_length ) |
| 5193 | length = (uint8_t) output_length; |
| 5194 | else |
| 5195 | length = tls12_prf->left_in_block; |
| 5196 | |
| 5197 | offset = hash_length - tls12_prf->left_in_block; |
| 5198 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5199 | output += length; |
| 5200 | output_length -= length; |
| 5201 | tls12_prf->left_in_block -= length; |
| 5202 | } |
| 5203 | |
| 5204 | return( PSA_SUCCESS ); |
| 5205 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5206 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5207 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5208 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5209 | psa_status_t psa_key_derivation_output_bytes( |
| 5210 | psa_key_derivation_operation_t *operation, |
| 5211 | uint8_t *output, |
| 5212 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5213 | { |
| 5214 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5215 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5216 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5217 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5218 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5219 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5220 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5223 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5224 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5225 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5226 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5227 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5228 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5229 | goto exit; |
| 5230 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5231 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5232 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5233 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5234 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5235 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5236 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5237 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5238 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5239 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5240 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5241 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5242 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5243 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5244 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5245 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5246 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5247 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5248 | output, output_length ); |
| 5249 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5250 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5251 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5252 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5253 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5254 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5255 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5256 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5257 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5258 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5259 | output_length ); |
| 5260 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5261 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5262 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5263 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5264 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5265 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5266 | return( PSA_ERROR_BAD_STATE ); |
| 5267 | } |
| 5268 | |
| 5269 | exit: |
| 5270 | if( status != PSA_SUCCESS ) |
| 5271 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5272 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5273 | * This allows us to differentiate between exhausted operations and |
| 5274 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5275 | * operations. */ |
| 5276 | psa_algorithm_t alg = operation->alg; |
| 5277 | psa_key_derivation_abort( operation ); |
| 5278 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5279 | memset( output, '!', output_length ); |
| 5280 | } |
| 5281 | return( status ); |
| 5282 | } |
| 5283 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5284 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5285 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5286 | { |
| 5287 | if( data_size >= 8 ) |
| 5288 | mbedtls_des_key_set_parity( data ); |
| 5289 | if( data_size >= 16 ) |
| 5290 | mbedtls_des_key_set_parity( data + 8 ); |
| 5291 | if( data_size >= 24 ) |
| 5292 | mbedtls_des_key_set_parity( data + 16 ); |
| 5293 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5294 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5295 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5296 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5297 | psa_key_slot_t *slot, |
| 5298 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5299 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5300 | { |
| 5301 | uint8_t *data = NULL; |
| 5302 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5303 | psa_status_t status; |
| 5304 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5305 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5307 | if( bits % 8 != 0 ) |
| 5308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5309 | data = mbedtls_calloc( 1, bytes ); |
| 5310 | if( data == NULL ) |
| 5311 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5312 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5313 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5314 | if( status != PSA_SUCCESS ) |
| 5315 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5316 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5317 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5318 | psa_des_set_key_parity( data, bytes ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5319 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5320 | |
| 5321 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5322 | if( status != PSA_SUCCESS ) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 5323 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5324 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5325 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5326 | psa_key_attributes_t attributes = { |
| 5327 | .core = slot->attr |
| 5328 | }; |
| 5329 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5330 | status = psa_driver_wrapper_import_key( &attributes, |
| 5331 | data, bytes, |
| 5332 | slot->key.data, |
| 5333 | slot->key.bytes, |
| 5334 | &slot->key.bytes, &bits ); |
| 5335 | if( bits != slot->attr.bits ) |
| 5336 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5337 | |
| 5338 | exit: |
| 5339 | mbedtls_free( data ); |
| 5340 | return( status ); |
| 5341 | } |
| 5342 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5343 | 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] | 5344 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5345 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5346 | { |
| 5347 | psa_status_t status; |
| 5348 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5349 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5350 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5351 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5352 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5353 | /* Reject any attempt to create a zero-length key so that we don't |
| 5354 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5355 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5356 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5357 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5358 | if( ! operation->can_output_key ) |
| 5359 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5360 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5361 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5362 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5363 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5364 | if( driver != NULL ) |
| 5365 | { |
| 5366 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5367 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5368 | } |
| 5369 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5370 | if( status == PSA_SUCCESS ) |
| 5371 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5372 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5373 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5374 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5375 | } |
| 5376 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5377 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5378 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5379 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5380 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5381 | return( status ); |
| 5382 | } |
| 5383 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5384 | |
| 5385 | |
| 5386 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5387 | /* Key derivation */ |
| 5388 | /****************************************************************/ |
| 5389 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5390 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5391 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5392 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5393 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5394 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5395 | int is_kdf_alg_supported; |
| 5396 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5397 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5398 | * initialisers for this union leave some bytes unspecified.) */ |
| 5399 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5400 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5401 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5402 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5403 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5404 | is_kdf_alg_supported = 1; |
| 5405 | else |
| 5406 | #endif |
| 5407 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5408 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5409 | is_kdf_alg_supported = 1; |
| 5410 | else |
| 5411 | #endif |
| 5412 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5413 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5414 | is_kdf_alg_supported = 1; |
| 5415 | else |
| 5416 | #endif |
| 5417 | is_kdf_alg_supported = 0; |
| 5418 | |
| 5419 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5420 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5421 | 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] | 5422 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5423 | if( hash_size == 0 ) |
| 5424 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5425 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5426 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5427 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5428 | { |
| 5429 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5430 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5431 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5432 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5433 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5434 | |
| 5435 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5436 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5437 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5438 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5439 | 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] | 5440 | psa_algorithm_t alg ) |
| 5441 | { |
| 5442 | psa_status_t status; |
| 5443 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5444 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5445 | return( PSA_ERROR_BAD_STATE ); |
| 5446 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5447 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5448 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5449 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5450 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5451 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5452 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5453 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5454 | #else |
| 5455 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5456 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5457 | } |
| 5458 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5459 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5460 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5461 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5462 | #else |
| 5463 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5464 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5465 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5466 | else |
| 5467 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5468 | |
| 5469 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5470 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5471 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5472 | } |
| 5473 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5474 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5475 | 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] | 5476 | psa_algorithm_t hash_alg, |
| 5477 | psa_key_derivation_step_t step, |
| 5478 | const uint8_t *data, |
| 5479 | size_t data_length ) |
| 5480 | { |
| 5481 | psa_status_t status; |
| 5482 | switch( step ) |
| 5483 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5484 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5485 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5486 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5487 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5488 | data, data_length, |
| 5489 | hash_alg ); |
| 5490 | if( status != PSA_SUCCESS ) |
| 5491 | return( status ); |
| 5492 | hkdf->state = HKDF_STATE_STARTED; |
| 5493 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5494 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5495 | /* If no salt was provided, use an empty salt. */ |
| 5496 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5497 | { |
| 5498 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5499 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5500 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5501 | if( status != PSA_SUCCESS ) |
| 5502 | return( status ); |
| 5503 | hkdf->state = HKDF_STATE_STARTED; |
| 5504 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5505 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5506 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5507 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5508 | data, data_length ); |
| 5509 | if( status != PSA_SUCCESS ) |
| 5510 | return( status ); |
| 5511 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5512 | hkdf->prk, |
| 5513 | sizeof( hkdf->prk ) ); |
| 5514 | if( status != PSA_SUCCESS ) |
| 5515 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5516 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5517 | hkdf->block_number = 0; |
| 5518 | hkdf->state = HKDF_STATE_KEYED; |
| 5519 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5520 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5521 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5522 | return( PSA_ERROR_BAD_STATE ); |
| 5523 | if( hkdf->info_set ) |
| 5524 | return( PSA_ERROR_BAD_STATE ); |
| 5525 | hkdf->info_length = data_length; |
| 5526 | if( data_length != 0 ) |
| 5527 | { |
| 5528 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5529 | if( hkdf->info == NULL ) |
| 5530 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5531 | memcpy( hkdf->info, data, data_length ); |
| 5532 | } |
| 5533 | hkdf->info_set = 1; |
| 5534 | return( PSA_SUCCESS ); |
| 5535 | default: |
| 5536 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5537 | } |
| 5538 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5539 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5540 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5541 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5542 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5543 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5544 | const uint8_t *data, |
| 5545 | size_t data_length ) |
| 5546 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5547 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5548 | return( PSA_ERROR_BAD_STATE ); |
| 5549 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5550 | if( data_length != 0 ) |
| 5551 | { |
| 5552 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5553 | if( prf->seed == NULL ) |
| 5554 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5555 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5556 | memcpy( prf->seed, data, data_length ); |
| 5557 | prf->seed_length = data_length; |
| 5558 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5559 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5560 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5561 | |
| 5562 | return( PSA_SUCCESS ); |
| 5563 | } |
| 5564 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5565 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5566 | psa_algorithm_t hash_alg, |
| 5567 | const uint8_t *data, |
| 5568 | size_t data_length ) |
| 5569 | { |
| 5570 | psa_status_t status; |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5571 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5572 | return( PSA_ERROR_BAD_STATE ); |
| 5573 | |
| 5574 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5575 | if( status != PSA_SUCCESS ) |
| 5576 | return( status ); |
| 5577 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5578 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5579 | |
| 5580 | return( PSA_SUCCESS ); |
| 5581 | } |
| 5582 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5583 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5584 | const uint8_t *data, |
| 5585 | size_t data_length ) |
| 5586 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5587 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5588 | return( PSA_ERROR_BAD_STATE ); |
| 5589 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5590 | if( data_length != 0 ) |
| 5591 | { |
| 5592 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5593 | if( prf->label == NULL ) |
| 5594 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5595 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5596 | memcpy( prf->label, data, data_length ); |
| 5597 | prf->label_length = data_length; |
| 5598 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5599 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5600 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5601 | |
| 5602 | return( PSA_SUCCESS ); |
| 5603 | } |
| 5604 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5605 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5606 | psa_algorithm_t hash_alg, |
| 5607 | psa_key_derivation_step_t step, |
| 5608 | const uint8_t *data, |
| 5609 | size_t data_length ) |
| 5610 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5611 | switch( step ) |
| 5612 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5613 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5614 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5615 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5616 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5617 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5618 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5619 | default: |
| 5620 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5621 | } |
| 5622 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5623 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5624 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5625 | |
| 5626 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5627 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5628 | psa_tls12_prf_key_derivation_t *prf, |
| 5629 | psa_algorithm_t hash_alg, |
| 5630 | const uint8_t *data, |
| 5631 | size_t data_length ) |
| 5632 | { |
| 5633 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5634 | 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] | 5635 | uint8_t *cur = pms; |
| 5636 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5637 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5638 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5639 | |
| 5640 | /* Quoting RFC 4279, Section 2: |
| 5641 | * |
| 5642 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5643 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5644 | * uint16 with the value N, and the PSK itself. |
| 5645 | */ |
| 5646 | |
| 5647 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5648 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5649 | memset( cur, 0, data_length ); |
| 5650 | cur += data_length; |
| 5651 | *cur++ = pms[0]; |
| 5652 | *cur++ = pms[1]; |
| 5653 | memcpy( cur, data, data_length ); |
| 5654 | cur += data_length; |
| 5655 | |
| 5656 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5657 | |
| 5658 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5659 | return( status ); |
| 5660 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5661 | |
| 5662 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5663 | psa_tls12_prf_key_derivation_t *prf, |
| 5664 | psa_algorithm_t hash_alg, |
| 5665 | psa_key_derivation_step_t step, |
| 5666 | const uint8_t *data, |
| 5667 | size_t data_length ) |
| 5668 | { |
| 5669 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5670 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5671 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5672 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5673 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5674 | |
| 5675 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5676 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5677 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5678 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5679 | /** Check whether the given key type is acceptable for the given |
| 5680 | * input step of a key derivation. |
| 5681 | * |
| 5682 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5683 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5684 | * Both secret and non-secret inputs can alternatively have the type |
| 5685 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5686 | * that the input was passed as a buffer rather than via a key object. |
| 5687 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5688 | static int psa_key_derivation_check_input_type( |
| 5689 | psa_key_derivation_step_t step, |
| 5690 | psa_key_type_t key_type ) |
| 5691 | { |
| 5692 | switch( step ) |
| 5693 | { |
| 5694 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5695 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5696 | return( PSA_SUCCESS ); |
| 5697 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5698 | return( PSA_SUCCESS ); |
| 5699 | break; |
| 5700 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5701 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5702 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5703 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5704 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5705 | return( PSA_SUCCESS ); |
| 5706 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5707 | return( PSA_SUCCESS ); |
| 5708 | break; |
| 5709 | } |
| 5710 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5711 | } |
| 5712 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5713 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5714 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5715 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5716 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5717 | const uint8_t *data, |
| 5718 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5719 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5720 | psa_status_t status; |
| 5721 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5722 | |
| 5723 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5724 | if( status != PSA_SUCCESS ) |
| 5725 | goto exit; |
| 5726 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5727 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5728 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5729 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5730 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5731 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5732 | step, data, data_length ); |
| 5733 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5734 | else |
| 5735 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5736 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5737 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5738 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5739 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5740 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5741 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5742 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5743 | else |
| 5744 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5745 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5746 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5747 | { |
| 5748 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5749 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5750 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5751 | } |
| 5752 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5753 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5754 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5755 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5756 | (void) data; |
| 5757 | (void) data_length; |
| 5758 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5759 | return( PSA_ERROR_BAD_STATE ); |
| 5760 | } |
| 5761 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5762 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5763 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5764 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5765 | return( status ); |
| 5766 | } |
| 5767 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5768 | psa_status_t psa_key_derivation_input_bytes( |
| 5769 | psa_key_derivation_operation_t *operation, |
| 5770 | psa_key_derivation_step_t step, |
| 5771 | const uint8_t *data, |
| 5772 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5773 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5774 | return( psa_key_derivation_input_internal( operation, step, |
| 5775 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5776 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5777 | } |
| 5778 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5779 | psa_status_t psa_key_derivation_input_key( |
| 5780 | psa_key_derivation_operation_t *operation, |
| 5781 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5782 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5783 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5784 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5785 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5786 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5787 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5788 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5789 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5790 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5791 | { |
| 5792 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5793 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5794 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5795 | |
| 5796 | /* Passing a key object as a SECRET input unlocks the permission |
| 5797 | * to output to a key object. */ |
| 5798 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5799 | operation->can_output_key = 1; |
| 5800 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5801 | status = psa_key_derivation_input_internal( operation, |
| 5802 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5803 | slot->key.data, |
| 5804 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5805 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5806 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5807 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5808 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5809 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5810 | |
| 5811 | |
| 5812 | |
| 5813 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5814 | /* Key agreement */ |
| 5815 | /****************************************************************/ |
| 5816 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5817 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5818 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5819 | size_t peer_key_length, |
| 5820 | const mbedtls_ecp_keypair *our_key, |
| 5821 | uint8_t *shared_secret, |
| 5822 | size_t shared_secret_size, |
| 5823 | size_t *shared_secret_length ) |
| 5824 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5825 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5826 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5827 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5828 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5829 | 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] | 5830 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5831 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5832 | status = mbedtls_psa_ecp_load_representation( |
| 5833 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5834 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5835 | peer_key, |
| 5836 | peer_key_length, |
| 5837 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5838 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5839 | goto exit; |
| 5840 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5841 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5842 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5843 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5844 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5845 | status = mbedtls_to_psa_error( |
| 5846 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5847 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5848 | goto exit; |
| 5849 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5850 | status = mbedtls_to_psa_error( |
| 5851 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5852 | shared_secret_length, |
| 5853 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5854 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5855 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5856 | if( status != PSA_SUCCESS ) |
| 5857 | goto exit; |
| 5858 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5859 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5860 | |
| 5861 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5862 | if( status != PSA_SUCCESS ) |
| 5863 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5864 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5865 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5866 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5867 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5868 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5869 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5870 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5871 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5872 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5873 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5874 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5875 | psa_key_slot_t *private_key, |
| 5876 | const uint8_t *peer_key, |
| 5877 | size_t peer_key_length, |
| 5878 | uint8_t *shared_secret, |
| 5879 | size_t shared_secret_size, |
| 5880 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5881 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5882 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5883 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5884 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5885 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5886 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5887 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5888 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5889 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 5890 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5891 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5892 | private_key->key.data, |
| 5893 | private_key->key.bytes, |
| 5894 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5895 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5896 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5897 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5898 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5899 | shared_secret, shared_secret_size, |
| 5900 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5901 | mbedtls_ecp_keypair_free( ecp ); |
| 5902 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5903 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5904 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5905 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5906 | (void) private_key; |
| 5907 | (void) peer_key; |
| 5908 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5909 | (void) shared_secret; |
| 5910 | (void) shared_secret_size; |
| 5911 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5912 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5913 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5914 | } |
| 5915 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5916 | /* 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] | 5917 | * to potentially free embedded data structures and wipe confidential data. |
| 5918 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5919 | 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] | 5920 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5921 | psa_key_slot_t *private_key, |
| 5922 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5923 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5924 | { |
| 5925 | psa_status_t status; |
| 5926 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5927 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5928 | 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] | 5929 | |
| 5930 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5931 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5932 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5933 | private_key, |
| 5934 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5935 | shared_secret, |
| 5936 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5937 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5938 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5939 | goto exit; |
| 5940 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5941 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5942 | * the shared secret. A shared secret is permitted wherever a key |
| 5943 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5944 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5945 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5946 | shared_secret, |
| 5947 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5948 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5949 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5950 | return( status ); |
| 5951 | } |
| 5952 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5953 | 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] | 5954 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5955 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5956 | const uint8_t *peer_key, |
| 5957 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5958 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5959 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5960 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5961 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5962 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5963 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5964 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5965 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5966 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5967 | if( status != PSA_SUCCESS ) |
| 5968 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5969 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5970 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5971 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5972 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5973 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5974 | else |
| 5975 | { |
| 5976 | /* If a private key has been added as SECRET, we allow the derived |
| 5977 | * key material to be used as a key in PSA Crypto. */ |
| 5978 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5979 | operation->can_output_key = 1; |
| 5980 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5981 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5982 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5983 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5984 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5985 | } |
| 5986 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5987 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5988 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5989 | const uint8_t *peer_key, |
| 5990 | size_t peer_key_length, |
| 5991 | uint8_t *output, |
| 5992 | size_t output_size, |
| 5993 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5994 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5995 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5996 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5997 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5998 | |
| 5999 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6000 | { |
| 6001 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6002 | goto exit; |
| 6003 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6004 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6005 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6006 | if( status != PSA_SUCCESS ) |
| 6007 | goto exit; |
| 6008 | |
| 6009 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6010 | peer_key, peer_key_length, |
| 6011 | output, output_size, |
| 6012 | output_length ); |
| 6013 | |
| 6014 | exit: |
| 6015 | if( status != PSA_SUCCESS ) |
| 6016 | { |
| 6017 | /* If an error happens and is not handled properly, the output |
| 6018 | * may be used as a key to protect sensitive data. Arrange for such |
| 6019 | * a key to be random, which is likely to result in decryption or |
| 6020 | * verification errors. This is better than filling the buffer with |
| 6021 | * some constant data such as zeros, which would result in the data |
| 6022 | * being protected with a reproducible, easily knowable key. |
| 6023 | */ |
| 6024 | psa_generate_random( output, output_size ); |
| 6025 | *output_length = output_size; |
| 6026 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6027 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6028 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6029 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6030 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6031 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6032 | |
| 6033 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6034 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6035 | /****************************************************************/ |
| 6036 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6037 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6038 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6039 | /** Initialize the PSA random generator. |
| 6040 | */ |
| 6041 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6042 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6043 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6044 | memset( rng, 0, sizeof( *rng ) ); |
| 6045 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6046 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6047 | /* Set default configuration if |
| 6048 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6049 | if( rng->entropy_init == NULL ) |
| 6050 | rng->entropy_init = mbedtls_entropy_init; |
| 6051 | if( rng->entropy_free == NULL ) |
| 6052 | rng->entropy_free = mbedtls_entropy_free; |
| 6053 | |
| 6054 | rng->entropy_init( &rng->entropy ); |
| 6055 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6056 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6057 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6058 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6059 | mbedtls_entropy_add_source( &rng->entropy, |
| 6060 | mbedtls_nv_seed_poll, NULL, |
| 6061 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6062 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6063 | #endif |
| 6064 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6065 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6066 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | /** Deinitialize the PSA random generator. |
| 6070 | */ |
| 6071 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6072 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6073 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6074 | memset( rng, 0, sizeof( *rng ) ); |
| 6075 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6076 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6077 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6078 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6079 | } |
| 6080 | |
| 6081 | /** Seed the PSA random generator. |
| 6082 | */ |
| 6083 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6084 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6085 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6086 | /* Do nothing: the external RNG seeds itself. */ |
| 6087 | (void) rng; |
| 6088 | return( PSA_SUCCESS ); |
| 6089 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6090 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6091 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6092 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6093 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6094 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6095 | } |
| 6096 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6097 | psa_status_t psa_generate_random( uint8_t *output, |
| 6098 | size_t output_size ) |
| 6099 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6100 | GUARD_MODULE_INITIALIZED; |
| 6101 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6102 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6103 | |
| 6104 | size_t output_length = 0; |
| 6105 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6106 | output, output_size, |
| 6107 | &output_length ); |
| 6108 | if( status != PSA_SUCCESS ) |
| 6109 | return( status ); |
| 6110 | /* Breaking up a request into smaller chunks is currently not supported |
| 6111 | * for the extrernal RNG interface. */ |
| 6112 | if( output_length != output_size ) |
| 6113 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6114 | return( PSA_SUCCESS ); |
| 6115 | |
| 6116 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6117 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6118 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6119 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6120 | size_t request_size = |
| 6121 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6122 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6123 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6124 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6125 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6126 | if( ret != 0 ) |
| 6127 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6128 | output_size -= request_size; |
| 6129 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6130 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6131 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6132 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6133 | } |
| 6134 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6135 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6136 | * |
| 6137 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6138 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6139 | * PSA API doesn't support passing an explicit state. |
| 6140 | * |
| 6141 | * In the non-external case, psa_generate_random() calls an |
| 6142 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6143 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6144 | * instead of doing this back-and-forth between the PSA API and the |
| 6145 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6146 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6147 | */ |
| 6148 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6149 | int mbedtls_psa_get_random( void *p_rng, |
| 6150 | unsigned char *output, |
| 6151 | size_t output_size ) |
| 6152 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6153 | /* This function takes a pointer to the RNG state because that's what |
| 6154 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6155 | * its own state internally and doesn't let the caller access that state. |
| 6156 | * So we just ignore the state parameter, and in practice we'll pass |
| 6157 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6158 | (void) p_rng; |
| 6159 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6160 | if( status == PSA_SUCCESS ) |
| 6161 | return( 0 ); |
| 6162 | else |
| 6163 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6164 | } |
| 6165 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6166 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6167 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6168 | #include "mbedtls/entropy_poll.h" |
| 6169 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6170 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6171 | size_t seed_size ) |
| 6172 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6173 | if( global_data.initialized ) |
| 6174 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6175 | |
| 6176 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6177 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6178 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6179 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6180 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6181 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6182 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6183 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6184 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6185 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6186 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6187 | * \param type The key type |
| 6188 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6189 | * |
| 6190 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6191 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6192 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6193 | * The size in bits of the key is not valid. |
| 6194 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6195 | * The type and/or the size in bits of the key or the combination of |
| 6196 | * the two is not supported. |
| 6197 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6198 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 6199 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6200 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6201 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6202 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6203 | if( key_type_is_raw_bytes( type ) ) |
| 6204 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6205 | status = validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6206 | if( status != PSA_SUCCESS ) |
| 6207 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6208 | } |
| 6209 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6210 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6211 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6212 | { |
| 6213 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6214 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6215 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6216 | /* Accept only byte-aligned keys, for the same reasons as |
| 6217 | * in psa_import_rsa_key(). */ |
| 6218 | if( bits % 8 != 0 ) |
| 6219 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6220 | } |
| 6221 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6222 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6223 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6224 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6225 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6226 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6227 | /* To avoid empty block, return successfully here. */ |
| 6228 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6229 | } |
| 6230 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6231 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6232 | { |
| 6233 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6234 | } |
| 6235 | |
| 6236 | return( PSA_SUCCESS ); |
| 6237 | } |
| 6238 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6239 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6240 | const psa_key_attributes_t *attributes, |
| 6241 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6242 | { |
| 6243 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6244 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6245 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6246 | if( ( attributes->domain_parameters == NULL ) && |
| 6247 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6248 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6249 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6250 | if( key_type_is_raw_bytes( type ) ) |
| 6251 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6252 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6253 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6254 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6255 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6256 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6257 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6258 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6259 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6260 | } |
| 6261 | else |
| 6262 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6263 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6264 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6265 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 6266 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 6267 | key_buffer, |
| 6268 | key_buffer_size, |
| 6269 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6270 | } |
| 6271 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6272 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6273 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6274 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6275 | 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] | 6276 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 6277 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 6278 | key_buffer, |
| 6279 | key_buffer_size, |
| 6280 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6281 | } |
| 6282 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6283 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6284 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6285 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6286 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6287 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6288 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6289 | return( PSA_SUCCESS ); |
| 6290 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6291 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6292 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6293 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6294 | { |
| 6295 | psa_status_t status; |
| 6296 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6297 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6298 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6299 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6300 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6301 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6302 | /* Reject any attempt to create a zero-length key so that we don't |
| 6303 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6304 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6305 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6306 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6307 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6308 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6309 | if( status != PSA_SUCCESS ) |
| 6310 | goto exit; |
| 6311 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6312 | /* In the case of a transparent key or an opaque key stored in local |
| 6313 | * storage (thus not in the case of generating a key in a secure element |
| 6314 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 6315 | * hold the generated key material. */ |
| 6316 | if( slot->key.data == NULL ) |
| 6317 | { |
| 6318 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 6319 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 6320 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6321 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6322 | attributes->core.type, attributes->core.bits ); |
| 6323 | if( status != PSA_SUCCESS ) |
| 6324 | goto exit; |
| 6325 | |
| 6326 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6327 | attributes->core.type, |
| 6328 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6329 | } |
| 6330 | else |
| 6331 | { |
| 6332 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6333 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6334 | if( status != PSA_SUCCESS ) |
| 6335 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6336 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6337 | |
| 6338 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6339 | if( status != PSA_SUCCESS ) |
| 6340 | goto exit; |
| 6341 | } |
| 6342 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6343 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6344 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6345 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6346 | if( status != PSA_SUCCESS ) |
| 6347 | psa_remove_key_data_from_memory( slot ); |
| 6348 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6349 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6350 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6351 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6352 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6353 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6354 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6355 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6356 | } |
| 6357 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6358 | /****************************************************************/ |
| 6359 | /* Module setup */ |
| 6360 | /****************************************************************/ |
| 6361 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6362 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6363 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6364 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6365 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6366 | { |
| 6367 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6368 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6369 | global_data.rng.entropy_init = entropy_init; |
| 6370 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6371 | return( PSA_SUCCESS ); |
| 6372 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6373 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6374 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6375 | void mbedtls_psa_crypto_free( void ) |
| 6376 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6377 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6378 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6379 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6380 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6381 | } |
| 6382 | /* Wipe all remaining data, including configuration. |
| 6383 | * In particular, this sets all state indicator to the value |
| 6384 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6385 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6386 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6387 | /* Unregister all secure element drivers, so that we restart from |
| 6388 | * a pristine state. */ |
| 6389 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6390 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6391 | } |
| 6392 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6393 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6394 | /** Recover a transaction that was interrupted by a power failure. |
| 6395 | * |
| 6396 | * This function is called during initialization, before psa_crypto_init() |
| 6397 | * returns. If this function returns a failure status, the initialization |
| 6398 | * fails. |
| 6399 | */ |
| 6400 | static psa_status_t psa_crypto_recover_transaction( |
| 6401 | const psa_crypto_transaction_t *transaction ) |
| 6402 | { |
| 6403 | switch( transaction->unknown.type ) |
| 6404 | { |
| 6405 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6406 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6407 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6408 | * is implemented. |
| 6409 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6410 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6411 | default: |
| 6412 | /* We found an unsupported transaction in the storage. |
| 6413 | * We don't know what state the storage is in. Give up. */ |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 6414 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6415 | } |
| 6416 | } |
| 6417 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6418 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6419 | psa_status_t psa_crypto_init( void ) |
| 6420 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6421 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6422 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6423 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6424 | if( global_data.initialized != 0 ) |
| 6425 | return( PSA_SUCCESS ); |
| 6426 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6427 | /* Initialize and seed the random generator. */ |
| 6428 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6429 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6430 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6431 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6432 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6433 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6434 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6435 | status = psa_initialize_key_slots( ); |
| 6436 | if( status != PSA_SUCCESS ) |
| 6437 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6438 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6439 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6440 | status = psa_init_all_se_drivers( ); |
| 6441 | if( status != PSA_SUCCESS ) |
| 6442 | goto exit; |
| 6443 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6444 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6445 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6446 | status = psa_crypto_load_transaction( ); |
| 6447 | if( status == PSA_SUCCESS ) |
| 6448 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6449 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6450 | if( status != PSA_SUCCESS ) |
| 6451 | goto exit; |
| 6452 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6453 | } |
| 6454 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6455 | { |
| 6456 | /* There's no transaction to complete. It's all good. */ |
| 6457 | status = PSA_SUCCESS; |
| 6458 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6459 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6460 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6461 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6462 | global_data.initialized = 1; |
| 6463 | |
| 6464 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6465 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6466 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6467 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |