Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 29 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | #include "psa/crypto.h" |
| 31 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 33 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | #include "psa_crypto_driver_wrappers.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 43 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 44 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 45 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | #define mbedtls_calloc calloc |
| 51 | #define mbedtls_free free |
| 52 | #endif |
| 53 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 54 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 56 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 57 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 58 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/blowfish.h" |
| 60 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 61 | #include "mbedtls/chacha20.h" |
| 62 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/cipher.h" |
| 64 | #include "mbedtls/ccm.h" |
| 65 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 67 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 68 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 69 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/error.h" |
| 71 | #include "mbedtls/gcm.h" |
| 72 | #include "mbedtls/md2.h" |
| 73 | #include "mbedtls/md4.h" |
| 74 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 75 | #include "mbedtls/md.h" |
| 76 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 78 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 79 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 80 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 82 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/sha1.h" |
| 84 | #include "mbedtls/sha256.h" |
| 85 | #include "mbedtls/sha512.h" |
| 86 | #include "mbedtls/xtea.h" |
| 87 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 88 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 89 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 90 | /* constant-time buffer comparison */ |
| 91 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 92 | { |
| 93 | size_t i; |
| 94 | unsigned char diff = 0; |
| 95 | |
| 96 | for( i = 0; i < n; i++ ) |
| 97 | diff |= a[i] ^ b[i]; |
| 98 | |
| 99 | return( diff ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 104 | /****************************************************************/ |
| 105 | /* Global data, support functions and library management */ |
| 106 | /****************************************************************/ |
| 107 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 109 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 110 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 113 | /* Values for psa_global_data_t::rng_state */ |
| 114 | #define RNG_NOT_INITIALIZED 0 |
| 115 | #define RNG_INITIALIZED 1 |
| 116 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 118 | typedef struct |
| 119 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 120 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; |
| 124 | |
| 125 | static psa_global_data_t global_data; |
| 126 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 127 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 128 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 129 | &global_data.rng.drbg; |
| 130 | #endif |
| 131 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 132 | #define GUARD_MODULE_INITIALIZED \ |
| 133 | if( global_data.initialized == 0 ) \ |
| 134 | return( PSA_ERROR_BAD_STATE ); |
| 135 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 136 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 137 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 138 | /* Mbed TLS error codes can combine a high-level error code and a |
| 139 | * low-level error code. The low-level error usually reflects the |
| 140 | * root cause better, so dispatch on that preferably. */ |
| 141 | int low_level_ret = - ( -ret & 0x007f ); |
| 142 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 143 | { |
| 144 | case 0: |
| 145 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 146 | |
| 147 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 148 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 149 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 150 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 151 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 152 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 153 | |
| 154 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 155 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 156 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 157 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 158 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 159 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 160 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 161 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 163 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 164 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 165 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 166 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 167 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 168 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 169 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 171 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 172 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 174 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 175 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 176 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 177 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 178 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 179 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 180 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 181 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 182 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 184 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 185 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 186 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 187 | |
| 188 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 189 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 190 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 191 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 192 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 193 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 194 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 195 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 196 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 197 | |
| 198 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 199 | return( PSA_ERROR_BAD_STATE ); |
| 200 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 201 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 202 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 204 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 205 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 206 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 207 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 208 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 209 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 210 | return( PSA_ERROR_INVALID_PADDING ); |
| 211 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 212 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 213 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 214 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 215 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 216 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 217 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 218 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 219 | |
| 220 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 221 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 222 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 223 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 224 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 225 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 226 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 227 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 228 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 229 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 230 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 231 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 232 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 233 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 234 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 235 | |
| 236 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 237 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 238 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 239 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 240 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 241 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 242 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 243 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 244 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 245 | |
| 246 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 247 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 248 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 250 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 251 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 252 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 253 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 254 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 255 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 256 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 258 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 259 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 260 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 261 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 262 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 263 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 264 | #endif |
| 265 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 266 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 267 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 268 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 269 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 270 | |
| 271 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 272 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 273 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 275 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 276 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 277 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 278 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 279 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 280 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 281 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 283 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 284 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 286 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 288 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 289 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 290 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 292 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 294 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 295 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 296 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 297 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 298 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 299 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 300 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 301 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 302 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 303 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 304 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 306 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 307 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 309 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 311 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 312 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 313 | return( PSA_ERROR_NOT_PERMITTED ); |
| 314 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 315 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 316 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 317 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 318 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 319 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 320 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 321 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 322 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 323 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 324 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 325 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 326 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 327 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 328 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 329 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 330 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 331 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 332 | |
| 333 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 335 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 336 | return( PSA_ERROR_INVALID_PADDING ); |
| 337 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 338 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 339 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 341 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 342 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 343 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 344 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 345 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 346 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 347 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 348 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 349 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 350 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 352 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 353 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 354 | |
| 355 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 356 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 357 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 358 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 359 | |
| 360 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 361 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 362 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 363 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 364 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 365 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 366 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 367 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 368 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 369 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 370 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 371 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 372 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 373 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 374 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 375 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 376 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 377 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 378 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 379 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 380 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 381 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 382 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 383 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 384 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 385 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 386 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 390 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 391 | |
| 392 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 393 | /****************************************************************/ |
| 394 | /* Key management */ |
| 395 | /****************************************************************/ |
| 396 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 397 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 398 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 399 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 400 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 401 | } |
| 402 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 403 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 404 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 405 | * current test driver in key_management.c is using this function |
| 406 | * when accelerators are used for ECC key pair and public key. |
| 407 | * Once that dependency is resolved these guards can be removed. |
| 408 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 409 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 410 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 411 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 412 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 413 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 414 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 415 | { |
| 416 | switch( curve ) |
| 417 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 418 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 419 | switch( byte_length ) |
| 420 | { |
| 421 | case PSA_BITS_TO_BYTES( 192 ): |
| 422 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 423 | case PSA_BITS_TO_BYTES( 224 ): |
| 424 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 425 | case PSA_BITS_TO_BYTES( 256 ): |
| 426 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 427 | case PSA_BITS_TO_BYTES( 384 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 429 | case PSA_BITS_TO_BYTES( 521 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 431 | default: |
| 432 | return( MBEDTLS_ECP_DP_NONE ); |
| 433 | } |
| 434 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 435 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 436 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | switch( byte_length ) |
| 438 | { |
| 439 | case PSA_BITS_TO_BYTES( 256 ): |
| 440 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 441 | case PSA_BITS_TO_BYTES( 384 ): |
| 442 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 443 | case PSA_BITS_TO_BYTES( 512 ): |
| 444 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 445 | default: |
| 446 | return( MBEDTLS_ECP_DP_NONE ); |
| 447 | } |
| 448 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 450 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 451 | switch( byte_length ) |
| 452 | { |
| 453 | case PSA_BITS_TO_BYTES( 255 ): |
| 454 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 455 | case PSA_BITS_TO_BYTES( 448 ): |
| 456 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 457 | default: |
| 458 | return( MBEDTLS_ECP_DP_NONE ); |
| 459 | } |
| 460 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 461 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 462 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 463 | switch( byte_length ) |
| 464 | { |
| 465 | case PSA_BITS_TO_BYTES( 192 ): |
| 466 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 467 | case PSA_BITS_TO_BYTES( 224 ): |
| 468 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 469 | case PSA_BITS_TO_BYTES( 256 ): |
| 470 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 471 | default: |
| 472 | return( MBEDTLS_ECP_DP_NONE ); |
| 473 | } |
| 474 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 475 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 476 | default: |
| 477 | return( MBEDTLS_ECP_DP_NONE ); |
| 478 | } |
| 479 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 480 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 481 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 482 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 483 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 484 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 485 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 486 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | { |
| 488 | /* Check that the bit size is acceptable for the key type */ |
| 489 | switch( type ) |
| 490 | { |
| 491 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 493 | case PSA_KEY_TYPE_DERIVE: |
| 494 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | #if defined(MBEDTLS_AES_C) |
| 496 | case PSA_KEY_TYPE_AES: |
| 497 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 498 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 499 | break; |
| 500 | #endif |
| 501 | #if defined(MBEDTLS_CAMELLIA_C) |
| 502 | case PSA_KEY_TYPE_CAMELLIA: |
| 503 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 504 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 505 | break; |
| 506 | #endif |
| 507 | #if defined(MBEDTLS_DES_C) |
| 508 | case PSA_KEY_TYPE_DES: |
| 509 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 510 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 511 | break; |
| 512 | #endif |
| 513 | #if defined(MBEDTLS_ARC4_C) |
| 514 | case PSA_KEY_TYPE_ARC4: |
| 515 | if( bits < 8 || bits > 2048 ) |
| 516 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 517 | break; |
| 518 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 519 | #if defined(MBEDTLS_CHACHA20_C) |
| 520 | case PSA_KEY_TYPE_CHACHA20: |
| 521 | if( bits != 256 ) |
| 522 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 523 | break; |
| 524 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 525 | default: |
| 526 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 527 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 528 | if( bits % 8 != 0 ) |
| 529 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 530 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 531 | return( PSA_SUCCESS ); |
| 532 | } |
| 533 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 534 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 535 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 536 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 537 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 538 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 539 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 540 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 541 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 542 | * that are not a multiple of 8) well. For example, there is only |
| 543 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 544 | * way to return the exact bit size of a key. |
| 545 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 546 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 547 | const mbedtls_rsa_context *rsa ) |
| 548 | { |
| 549 | mbedtls_mpi n; |
| 550 | psa_status_t status; |
| 551 | mbedtls_mpi_init( &n ); |
| 552 | status = mbedtls_to_psa_error( |
| 553 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 554 | if( status == PSA_SUCCESS ) |
| 555 | { |
| 556 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 557 | status = PSA_ERROR_NOT_SUPPORTED; |
| 558 | } |
| 559 | mbedtls_mpi_free( &n ); |
| 560 | return( status ); |
| 561 | } |
| 562 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 563 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 564 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 565 | * \param[in] type The type of key contained in \p data. |
| 566 | * \param[in] data The buffer from which to load the representation. |
| 567 | * \param[in] data_length The size in bytes of \p data. |
| 568 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 569 | * The caller is responsible for freeing both the |
| 570 | * contents of the context and the context itself |
| 571 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 572 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 573 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 574 | const uint8_t *data, |
| 575 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 576 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 577 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 578 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 579 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 580 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 581 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 582 | |
| 583 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 584 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 585 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 586 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 587 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 588 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 589 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 590 | if( status != PSA_SUCCESS ) |
| 591 | goto exit; |
| 592 | |
| 593 | /* We have something that the pkparse module recognizes. If it is a |
| 594 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 595 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 596 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 597 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 598 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 599 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 600 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 601 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 602 | * supports non-byte-aligned key sizes, but not well. For example, |
| 603 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 604 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 605 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 606 | { |
| 607 | status = PSA_ERROR_NOT_SUPPORTED; |
| 608 | goto exit; |
| 609 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 610 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 611 | if( status != PSA_SUCCESS ) |
| 612 | goto exit; |
| 613 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 614 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 615 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 616 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 617 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 618 | |
| 619 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 620 | mbedtls_pk_free( &ctx ); |
| 621 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 622 | } |
| 623 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 624 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 625 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 626 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 627 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 628 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 629 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 630 | |
| 631 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 632 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 633 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 634 | /** Export an RSA key to export representation |
| 635 | * |
| 636 | * \param[in] type The type of key (public/private) to export |
| 637 | * \param[in] rsa The internal RSA representation from which to export |
| 638 | * \param[out] data The buffer to export to |
| 639 | * \param[in] data_size The length of the buffer to export to |
| 640 | * \param[out] data_length The amount of bytes written to \p data |
| 641 | */ |
| 642 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 643 | mbedtls_rsa_context *rsa, |
| 644 | uint8_t *data, |
| 645 | size_t data_size, |
| 646 | size_t *data_length ) |
| 647 | { |
| 648 | #if defined(MBEDTLS_PK_WRITE_C) |
| 649 | int ret; |
| 650 | mbedtls_pk_context pk; |
| 651 | uint8_t *pos = data + data_size; |
| 652 | |
| 653 | mbedtls_pk_init( &pk ); |
| 654 | pk.pk_info = &mbedtls_rsa_info; |
| 655 | pk.pk_ctx = rsa; |
| 656 | |
| 657 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 658 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 659 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 660 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 661 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 662 | else |
| 663 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 664 | |
| 665 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 666 | { |
| 667 | /* Clean up in case pk_write failed halfway through. */ |
| 668 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 669 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 670 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 671 | |
| 672 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 673 | * Move the data to the beginning and erase remaining data |
| 674 | * at the original location. */ |
| 675 | if( 2 * (size_t) ret <= data_size ) |
| 676 | { |
| 677 | memcpy( data, data + data_size - ret, ret ); |
| 678 | memset( data + data_size - ret, 0, ret ); |
| 679 | } |
| 680 | else if( (size_t) ret < data_size ) |
| 681 | { |
| 682 | memmove( data, data + data_size - ret, ret ); |
| 683 | memset( data + ret, 0, data_size - ret ); |
| 684 | } |
| 685 | |
| 686 | *data_length = ret; |
| 687 | return( PSA_SUCCESS ); |
| 688 | #else |
| 689 | (void) type; |
| 690 | (void) rsa; |
| 691 | (void) data; |
| 692 | (void) data_size; |
| 693 | (void) data_length; |
| 694 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 695 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 696 | } |
| 697 | |
| 698 | /** Import an RSA key from import representation to a slot |
| 699 | * |
| 700 | * \param[in,out] slot The slot where to store the export representation to |
| 701 | * \param[in] data The buffer containing the import representation |
| 702 | * \param[in] data_length The amount of bytes in \p data |
| 703 | */ |
| 704 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 705 | const uint8_t *data, |
| 706 | size_t data_length ) |
| 707 | { |
| 708 | psa_status_t status; |
| 709 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 710 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 711 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 712 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 713 | status = psa_load_rsa_representation( slot->attr.type, |
| 714 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 715 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 716 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 717 | if( status != PSA_SUCCESS ) |
| 718 | goto exit; |
| 719 | |
| 720 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 721 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 722 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 723 | /* Re-export the data to PSA export format, such that we can store export |
| 724 | * representation in the key slot. Export representation in case of RSA is |
| 725 | * the smallest representation that's allowed as input, so a straight-up |
| 726 | * allocation of the same size as the input buffer will be large enough. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 727 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 728 | if( output == NULL ) |
| 729 | { |
| 730 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 731 | goto exit; |
| 732 | } |
| 733 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 734 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 735 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 736 | output, |
| 737 | data_length, |
| 738 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 739 | exit: |
| 740 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 741 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 742 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 743 | |
| 744 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 745 | if( status != PSA_SUCCESS ) |
| 746 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 747 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 748 | return( status ); |
| 749 | } |
| 750 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 751 | /* On success, store the allocated export-formatted key. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 752 | slot->key.data = output; |
| 753 | slot->key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 754 | |
| 755 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 756 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 757 | |
| 758 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 759 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 760 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 761 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 762 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 763 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 764 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \ |
| 765 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 766 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 767 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 768 | * \param[in] type The type of key contained in \p data. |
| 769 | * \param[in] data The buffer from which to load the representation. |
| 770 | * \param[in] data_length The size in bytes of \p data. |
| 771 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 772 | * The caller is responsible for freeing both the |
| 773 | * contents of the context and the context itself |
| 774 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 775 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 776 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 777 | const uint8_t *data, |
| 778 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 779 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 780 | { |
| 781 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 782 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 783 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 784 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 785 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 786 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 787 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 788 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 789 | /* A Weierstrass public key is represented as: |
| 790 | * - The byte 0x04; |
| 791 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 792 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 793 | * So its data length is 2m+1 where m is the curve size in bits. |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 794 | */ |
| 795 | if( ( data_length & 1 ) == 0 ) |
| 796 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 797 | curve_size = data_length / 2; |
| 798 | |
| 799 | /* Montgomery public keys are represented in compressed format, meaning |
| 800 | * their curve_size is equal to the amount of input. */ |
| 801 | |
| 802 | /* Private keys are represented in uncompressed private random integer |
| 803 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 806 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 807 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 808 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 809 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 810 | mbedtls_ecp_keypair_init( ecp ); |
| 811 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 812 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 813 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 814 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 815 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 816 | { |
| 817 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 818 | goto exit; |
| 819 | } |
| 820 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 821 | status = mbedtls_to_psa_error( |
| 822 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 823 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 824 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 825 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 826 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 827 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 828 | { |
| 829 | /* Load the public value. */ |
| 830 | status = mbedtls_to_psa_error( |
| 831 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 832 | data, |
| 833 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 834 | if( status != PSA_SUCCESS ) |
| 835 | goto exit; |
| 836 | |
| 837 | /* Check that the point is on the curve. */ |
| 838 | status = mbedtls_to_psa_error( |
| 839 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 840 | if( status != PSA_SUCCESS ) |
| 841 | goto exit; |
| 842 | } |
| 843 | else |
| 844 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 845 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 846 | status = mbedtls_to_psa_error( |
| 847 | mbedtls_ecp_read_key( ecp->grp.id, |
| 848 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 849 | data, |
| 850 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 851 | if( status != PSA_SUCCESS ) |
| 852 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 853 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 854 | |
| 855 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 856 | exit: |
| 857 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 858 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 859 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 860 | mbedtls_free( ecp ); |
| 861 | } |
| 862 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 863 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 864 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 865 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 866 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 867 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 868 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || |
| 869 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 870 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 871 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 872 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 873 | /** Export an ECP key to export representation |
| 874 | * |
| 875 | * \param[in] type The type of key (public/private) to export |
| 876 | * \param[in] ecp The internal ECP representation from which to export |
| 877 | * \param[out] data The buffer to export to |
| 878 | * \param[in] data_size The length of the buffer to export to |
| 879 | * \param[out] data_length The amount of bytes written to \p data |
| 880 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 881 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 882 | mbedtls_ecp_keypair *ecp, |
| 883 | uint8_t *data, |
| 884 | size_t data_size, |
| 885 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 886 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 887 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 888 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 889 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 890 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 891 | /* Check whether the public part is loaded */ |
| 892 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 893 | { |
| 894 | /* Calculate the public key */ |
| 895 | status = mbedtls_to_psa_error( |
| 896 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 897 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 898 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 899 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 902 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 903 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 904 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 905 | data_length, |
| 906 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 907 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 908 | if( status != PSA_SUCCESS ) |
| 909 | memset( data, 0, data_size ); |
| 910 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 911 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 912 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 913 | else |
| 914 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 915 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 916 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 917 | |
| 918 | status = mbedtls_to_psa_error( |
| 919 | mbedtls_ecp_write_key( ecp, |
| 920 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 921 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 922 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 923 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 924 | else |
| 925 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 926 | |
| 927 | return( status ); |
| 928 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 929 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 930 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 931 | /** Import an ECP key from import representation to a slot |
| 932 | * |
| 933 | * \param[in,out] slot The slot where to store the export representation to |
| 934 | * \param[in] data The buffer containing the import representation |
| 935 | * \param[in] data_length The amount of bytes in \p data |
| 936 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 937 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 938 | const uint8_t *data, |
| 939 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 940 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 941 | psa_status_t status; |
| 942 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 943 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 944 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 945 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 946 | status = psa_load_ecp_representation( slot->attr.type, |
| 947 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 948 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 949 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 950 | if( status != PSA_SUCCESS ) |
| 951 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 952 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 953 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 954 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 955 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 956 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 957 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 958 | /* Re-export the data to PSA export format. There is currently no support |
| 959 | * for other input formats then the export format, so this is a 1-1 |
| 960 | * copy operation. */ |
| 961 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 962 | if( output == NULL ) |
| 963 | { |
| 964 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 965 | goto exit; |
| 966 | } |
| 967 | |
| 968 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 969 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 970 | output, |
| 971 | data_length, |
| 972 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 973 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 974 | /* Always free the PK object (will also free contained ECP context) */ |
| 975 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 976 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 977 | |
| 978 | /* Free the allocated buffer only on error. */ |
| 979 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 980 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 981 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 982 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 983 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 984 | |
| 985 | /* On success, store the allocated export-formatted key. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 986 | slot->key.data = output; |
| 987 | slot->key.bytes = data_length; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 988 | |
| 989 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 990 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 991 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 992 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 993 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 994 | /** Return the size of the key in the given slot, in bits. |
| 995 | * |
| 996 | * \param[in] slot A key slot. |
| 997 | * |
| 998 | * \return The key size in bits, read from the metadata in the slot. |
| 999 | */ |
| 1000 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 1001 | { |
| 1002 | return( slot->attr.bits ); |
| 1003 | } |
| 1004 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1005 | /** Try to allocate a buffer to an empty key slot. |
| 1006 | * |
| 1007 | * \param[in,out] slot Key slot to attach buffer to. |
| 1008 | * \param[in] buffer_length Requested size of the buffer. |
| 1009 | * |
| 1010 | * \retval #PSA_SUCCESS |
| 1011 | * The buffer has been successfully allocated. |
| 1012 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1013 | * Not enough memory was available for allocation. |
| 1014 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1015 | * Trying to allocate a buffer to a non-empty key slot. |
| 1016 | */ |
| 1017 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 1018 | size_t buffer_length ) |
| 1019 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1020 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1021 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1022 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1023 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 1024 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1025 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1026 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1027 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1028 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1031 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 1032 | const uint8_t* data, |
| 1033 | size_t data_length ) |
| 1034 | { |
| 1035 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 1036 | data_length ); |
| 1037 | if( status != PSA_SUCCESS ) |
| 1038 | return( status ); |
| 1039 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1040 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1041 | return( PSA_SUCCESS ); |
| 1042 | } |
| 1043 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1044 | /** Import key data into a slot. |
| 1045 | * |
| 1046 | * `slot->type` must have been set previously. |
| 1047 | * This function assumes that the slot does not contain any key material yet. |
| 1048 | * On failure, the slot content is unchanged. |
| 1049 | * |
| 1050 | * Persistent storage is not affected. |
| 1051 | * |
| 1052 | * \param[in,out] slot The key slot to import data into. |
| 1053 | * Its `type` field must have previously been set to |
| 1054 | * the desired key type. |
| 1055 | * It must not contain any key material yet. |
| 1056 | * \param[in] data Buffer containing the key material to parse and import. |
| 1057 | * \param data_length Size of \p data in bytes. |
| 1058 | * |
| 1059 | * \retval #PSA_SUCCESS |
| 1060 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1061 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1062 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1063 | */ |
| 1064 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 1065 | const uint8_t *data, |
| 1066 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1067 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1068 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1069 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1070 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1071 | /* zero-length keys are never supported. */ |
| 1072 | if( data_length == 0 ) |
| 1073 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1074 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1075 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1076 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1077 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1078 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1079 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 1080 | if( data_length > SIZE_MAX / 8 ) |
| 1081 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1082 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 1083 | /* Enforce a size limit, and in particular ensure that the bit |
| 1084 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1085 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 1086 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1087 | |
| 1088 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1089 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1090 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1093 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1094 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1095 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1096 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1097 | /* Write the actual key size to the slot. |
| 1098 | * psa_start_key_creation() wrote the size declared by the |
| 1099 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1100 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1101 | |
| 1102 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1103 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1104 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1105 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1106 | /* Try validation through accelerators first. */ |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1107 | psa_key_attributes_t attributes = { |
| 1108 | .core = slot->attr |
| 1109 | }; |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame^] | 1110 | |
| 1111 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
| 1112 | if( status != PSA_SUCCESS ) |
| 1113 | return( status ); |
| 1114 | |
| 1115 | bit_size = slot->attr.bits; |
| 1116 | status = psa_driver_wrapper_import_key( &attributes, |
| 1117 | data, data_length, |
| 1118 | slot->key.data, |
| 1119 | slot->key.bytes, |
| 1120 | &slot->key.bytes, |
| 1121 | &bit_size ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1122 | if( status == PSA_SUCCESS ) |
| 1123 | { |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame^] | 1124 | if( slot->attr.bits == 0 ) |
| 1125 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 1126 | else if( bit_size != slot->attr.bits ) |
| 1127 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1128 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1129 | return( PSA_SUCCESS ); |
| 1130 | } |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame^] | 1131 | else |
| 1132 | { |
| 1133 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1134 | return( status ); |
| 1135 | } |
| 1136 | |
| 1137 | mbedtls_platform_zeroize( slot->key.data, data_length ); |
| 1138 | mbedtls_free( slot->key.data ); |
| 1139 | slot->key.data = NULL; |
| 1140 | slot->key.bytes = 0; |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1141 | |
| 1142 | /* Key format is not supported by any accelerator, try software fallback |
| 1143 | * if present. */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1144 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1145 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1146 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1147 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1148 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 1149 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1150 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1151 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1152 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1153 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1154 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1155 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1156 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1157 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1158 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1159 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1160 | |
| 1161 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 1162 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1163 | } |
| 1164 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1165 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1166 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1167 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1168 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1171 | /** Calculate the intersection of two algorithm usage policies. |
| 1172 | * |
| 1173 | * Return 0 (which allows no operation) on incompatibility. |
| 1174 | */ |
| 1175 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1176 | psa_algorithm_t alg1, |
| 1177 | psa_algorithm_t alg2 ) |
| 1178 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1179 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1180 | if( alg1 == alg2 ) |
| 1181 | return( alg1 ); |
| 1182 | /* If the policies are from the same hash-and-sign family, check |
| 1183 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1184 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1185 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1186 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1187 | { |
| 1188 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1189 | return( alg2 ); |
| 1190 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1191 | return( alg1 ); |
| 1192 | } |
| 1193 | /* If the policies are incompatible, allow nothing. */ |
| 1194 | return( 0 ); |
| 1195 | } |
| 1196 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1197 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1198 | psa_algorithm_t requested_alg ) |
| 1199 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1200 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1201 | if( requested_alg == policy_alg ) |
| 1202 | return( 1 ); |
| 1203 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1204 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1205 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1206 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1207 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1208 | { |
| 1209 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1210 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1211 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1212 | /* If policy_alg is a generic key agreement operation, then using it for |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 1213 | * a key derivation with that key agreement should also be allowed. This |
| 1214 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1215 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1216 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1217 | { |
| 1218 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1219 | policy_alg ); |
| 1220 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1221 | /* If it isn't permitted, it's forbidden. */ |
| 1222 | return( 0 ); |
| 1223 | } |
| 1224 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1225 | /** Test whether a policy permits an algorithm. |
| 1226 | * |
| 1227 | * The caller must test usage flags separately. |
| 1228 | */ |
| 1229 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1230 | psa_algorithm_t alg ) |
| 1231 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1232 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1233 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1236 | /** Restrict a key policy based on a constraint. |
| 1237 | * |
| 1238 | * \param[in,out] policy The policy to restrict. |
| 1239 | * \param[in] constraint The policy constraint to apply. |
| 1240 | * |
| 1241 | * \retval #PSA_SUCCESS |
| 1242 | * \c *policy contains the intersection of the original value of |
| 1243 | * \c *policy and \c *constraint. |
| 1244 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1245 | * \c *policy and \c *constraint are incompatible. |
| 1246 | * \c *policy is unchanged. |
| 1247 | */ |
| 1248 | static psa_status_t psa_restrict_key_policy( |
| 1249 | psa_key_policy_t *policy, |
| 1250 | const psa_key_policy_t *constraint ) |
| 1251 | { |
| 1252 | psa_algorithm_t intersection_alg = |
| 1253 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1254 | psa_algorithm_t intersection_alg2 = |
| 1255 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1256 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1257 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1258 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1260 | policy->usage &= constraint->usage; |
| 1261 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1262 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1263 | return( PSA_SUCCESS ); |
| 1264 | } |
| 1265 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1266 | /** Get the description of a key given its identifier and policy constraints |
| 1267 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1268 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1269 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1270 | * nonzero, the key must allow operations with this algorithm. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1271 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1272 | * In case of a persistent key, the function loads the description of the key |
| 1273 | * into a key slot if not already done. |
| 1274 | * |
| 1275 | * On success, the returned key slot is locked. It is the responsibility of |
| 1276 | * the caller to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1277 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1278 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1279 | mbedtls_svc_key_id_t key, |
| 1280 | psa_key_slot_t **p_slot, |
| 1281 | psa_key_usage_t usage, |
| 1282 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1283 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1284 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1285 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1286 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1287 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1288 | if( status != PSA_SUCCESS ) |
| 1289 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1290 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1291 | |
| 1292 | /* Enforce that usage policy for the key slot contains all the flags |
| 1293 | * required by the usage parameter. There is one exception: public |
| 1294 | * keys can always be exported, so we treat public key objects as |
| 1295 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1296 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1297 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1298 | |
| 1299 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1300 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1301 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1302 | |
| 1303 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1304 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1305 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1306 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1307 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1308 | |
| 1309 | error: |
| 1310 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1311 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1312 | |
| 1313 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1314 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1315 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1316 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1317 | * |
| 1318 | * A transparent key is a key for which the key material is directly |
| 1319 | * available, as opposed to a key in a secure element. |
| 1320 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1321 | * This is a temporary function to use instead of |
| 1322 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1323 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1324 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1325 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1326 | * caller to unlock the key slot when it does not access it anymore. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1327 | */ |
| 1328 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1329 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1330 | mbedtls_svc_key_id_t key, |
| 1331 | psa_key_slot_t **p_slot, |
| 1332 | psa_key_usage_t usage, |
| 1333 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1334 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1335 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1336 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1337 | if( status != PSA_SUCCESS ) |
| 1338 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1340 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1341 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1342 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1343 | *p_slot = NULL; |
| 1344 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1345 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1346 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1347 | return( PSA_SUCCESS ); |
| 1348 | } |
| 1349 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1350 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1351 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1352 | psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1353 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1354 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1355 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1356 | static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1357 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1358 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1359 | * initialized slot, so we can just free it. */ |
| 1360 | if( slot->key.data != NULL ) |
| 1361 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1362 | |
| 1363 | mbedtls_free( slot->key.data ); |
| 1364 | slot->key.data = NULL; |
| 1365 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1366 | |
| 1367 | return( PSA_SUCCESS ); |
| 1368 | } |
| 1369 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1370 | /** Completely wipe a slot in memory, including its policy. |
| 1371 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1372 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1373 | { |
| 1374 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * As the return error code may not be handled in case of multiple errors, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1378 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1379 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1380 | * part of the execution of a test suite this will stop the test suite |
Ronald Cron | 4640c15 | 2020-11-13 10:11:01 +0100 | [diff] [blame] | 1381 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1382 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1383 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1384 | { |
| 1385 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1386 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1387 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1388 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1389 | } |
| 1390 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1391 | /* Multipart operations may still be using the key. This is safe |
| 1392 | * because all multipart operation objects are independent from |
| 1393 | * the key slot: if they need to access the key after the setup |
| 1394 | * phase, they have a copy of the key. Note that this means that |
| 1395 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1396 | /* At this point, key material and other type-specific content has |
| 1397 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1398 | * zeroize because the metadata is not particularly sensitive. */ |
| 1399 | memset( slot, 0, sizeof( *slot ) ); |
| 1400 | return( status ); |
| 1401 | } |
| 1402 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1403 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1404 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1405 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1406 | psa_status_t status; /* status of the last operation */ |
| 1407 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1408 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1409 | psa_se_drv_table_entry_t *driver; |
| 1410 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1411 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1412 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1413 | return( PSA_SUCCESS ); |
| 1414 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1415 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1416 | * Get the description of the key in a key slot. In case of a persistent |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1417 | * key, this will load the key description from persistent memory if not |
| 1418 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1419 | * the key is operated by an SE or not and this information is needed by |
| 1420 | * the current implementation. |
| 1421 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1422 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1423 | if( status != PSA_SUCCESS ) |
| 1424 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1425 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1426 | /* |
| 1427 | * If the key slot containing the key description is under access by the |
| 1428 | * library (apart from the present access), the key cannot be destroyed |
| 1429 | * yet. For the time being, just return in error. Eventually (to be |
| 1430 | * implemented), the key should be destroyed when all accesses have |
| 1431 | * stopped. |
| 1432 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1433 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1434 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1435 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1436 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1437 | } |
| 1438 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1439 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1440 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1441 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1442 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1443 | /* For a key in a secure element, we need to do three things: |
| 1444 | * remove the key file in internal storage, destroy the |
| 1445 | * key inside the secure element, and update the driver's |
| 1446 | * persistent data. Start a transaction that will encompass these |
| 1447 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1448 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1449 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1450 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1451 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1452 | status = psa_crypto_save_transaction( ); |
| 1453 | if( status != PSA_SUCCESS ) |
| 1454 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1455 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1456 | /* We should still try to destroy the key in the secure |
| 1457 | * element and the key metadata in storage. This is especially |
| 1458 | * important if the error is that the storage is full. |
| 1459 | * But how to do it exactly without risking an inconsistent |
| 1460 | * state after a reset? |
| 1461 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1462 | */ |
| 1463 | overall_status = status; |
| 1464 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1465 | } |
| 1466 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1467 | status = psa_destroy_se_key( driver, |
| 1468 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1469 | if( overall_status == PSA_SUCCESS ) |
| 1470 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1471 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1472 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1473 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1474 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1475 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1476 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1477 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1478 | if( overall_status == PSA_SUCCESS ) |
| 1479 | overall_status = status; |
| 1480 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1481 | /* TODO: other slots may have a copy of the same key. We should |
| 1482 | * invalidate them. |
| 1483 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1484 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1485 | } |
| 1486 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1487 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1488 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1489 | if( driver != NULL ) |
| 1490 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1491 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1492 | if( overall_status == PSA_SUCCESS ) |
| 1493 | overall_status = status; |
| 1494 | status = psa_crypto_stop_transaction( ); |
| 1495 | if( overall_status == PSA_SUCCESS ) |
| 1496 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1497 | } |
| 1498 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1499 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1500 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1501 | exit: |
| 1502 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1503 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1504 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1505 | if( overall_status == PSA_SUCCESS ) |
| 1506 | overall_status = status; |
| 1507 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1508 | } |
| 1509 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1510 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1511 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1512 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1513 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1516 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1517 | psa_key_type_t type, |
| 1518 | const uint8_t *data, |
| 1519 | size_t data_length ) |
| 1520 | { |
| 1521 | uint8_t *copy = NULL; |
| 1522 | |
| 1523 | if( data_length != 0 ) |
| 1524 | { |
| 1525 | copy = mbedtls_calloc( 1, data_length ); |
| 1526 | if( copy == NULL ) |
| 1527 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1528 | memcpy( copy, data, data_length ); |
| 1529 | } |
| 1530 | /* After this point, this function is guaranteed to succeed, so it |
| 1531 | * can start modifying `*attributes`. */ |
| 1532 | |
| 1533 | if( attributes->domain_parameters != NULL ) |
| 1534 | { |
| 1535 | mbedtls_free( attributes->domain_parameters ); |
| 1536 | attributes->domain_parameters = NULL; |
| 1537 | attributes->domain_parameters_size = 0; |
| 1538 | } |
| 1539 | |
| 1540 | attributes->domain_parameters = copy; |
| 1541 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1542 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1543 | return( PSA_SUCCESS ); |
| 1544 | } |
| 1545 | |
| 1546 | psa_status_t psa_get_key_domain_parameters( |
| 1547 | const psa_key_attributes_t *attributes, |
| 1548 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1549 | { |
| 1550 | if( attributes->domain_parameters_size > data_size ) |
| 1551 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1552 | *data_length = attributes->domain_parameters_size; |
| 1553 | if( attributes->domain_parameters_size != 0 ) |
| 1554 | memcpy( data, attributes->domain_parameters, |
| 1555 | attributes->domain_parameters_size ); |
| 1556 | return( PSA_SUCCESS ); |
| 1557 | } |
| 1558 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1559 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1560 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1561 | static psa_status_t psa_get_rsa_public_exponent( |
| 1562 | const mbedtls_rsa_context *rsa, |
| 1563 | psa_key_attributes_t *attributes ) |
| 1564 | { |
| 1565 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1566 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1567 | uint8_t *buffer = NULL; |
| 1568 | size_t buflen; |
| 1569 | mbedtls_mpi_init( &mpi ); |
| 1570 | |
| 1571 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1572 | if( ret != 0 ) |
| 1573 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1574 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1575 | { |
| 1576 | /* It's the default value, which is reported as an empty string, |
| 1577 | * so there's nothing to do. */ |
| 1578 | goto exit; |
| 1579 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1580 | |
| 1581 | buflen = mbedtls_mpi_size( &mpi ); |
| 1582 | buffer = mbedtls_calloc( 1, buflen ); |
| 1583 | if( buffer == NULL ) |
| 1584 | { |
| 1585 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1586 | goto exit; |
| 1587 | } |
| 1588 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1589 | if( ret != 0 ) |
| 1590 | goto exit; |
| 1591 | attributes->domain_parameters = buffer; |
| 1592 | attributes->domain_parameters_size = buflen; |
| 1593 | |
| 1594 | exit: |
| 1595 | mbedtls_mpi_free( &mpi ); |
| 1596 | if( ret != 0 ) |
| 1597 | mbedtls_free( buffer ); |
| 1598 | return( mbedtls_to_psa_error( ret ) ); |
| 1599 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1600 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1601 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1602 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1603 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1604 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1605 | psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1606 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1607 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1608 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1609 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1610 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1611 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1612 | psa_reset_key_attributes( attributes ); |
| 1613 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1614 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1615 | if( status != PSA_SUCCESS ) |
| 1616 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1617 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1618 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1619 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1620 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1621 | |
| 1622 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1623 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1624 | psa_set_key_slot_number( attributes, |
| 1625 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1626 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1627 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1628 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1629 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1630 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1631 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1632 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1633 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1634 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1635 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1636 | * is not yet implemented. |
| 1637 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1638 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1639 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1640 | break; |
| 1641 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1642 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1643 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1644 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1645 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1646 | slot->key.data, |
| 1647 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1648 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1649 | if( status != PSA_SUCCESS ) |
| 1650 | break; |
| 1651 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1652 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1653 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1654 | mbedtls_rsa_free( rsa ); |
| 1655 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1656 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1657 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1658 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1659 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1660 | default: |
| 1661 | /* Nothing else to do. */ |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | if( status != PSA_SUCCESS ) |
| 1666 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1667 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1668 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1669 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1670 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1673 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1674 | psa_status_t psa_get_key_slot_number( |
| 1675 | const psa_key_attributes_t *attributes, |
| 1676 | psa_key_slot_number_t *slot_number ) |
| 1677 | { |
| 1678 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1679 | { |
| 1680 | *slot_number = attributes->slot_number; |
| 1681 | return( PSA_SUCCESS ); |
| 1682 | } |
| 1683 | else |
| 1684 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1685 | } |
| 1686 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1687 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1688 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1689 | uint8_t *data, |
| 1690 | size_t data_size, |
| 1691 | size_t *data_length ) |
| 1692 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1693 | if( slot->key.bytes > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1694 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1695 | memcpy( data, slot->key.data, slot->key.bytes ); |
| 1696 | memset( data + slot->key.bytes, 0, |
| 1697 | data_size - slot->key.bytes ); |
| 1698 | *data_length = slot->key.bytes; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1699 | return( PSA_SUCCESS ); |
| 1700 | } |
| 1701 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1702 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1703 | uint8_t *data, |
| 1704 | size_t data_size, |
| 1705 | size_t *data_length, |
| 1706 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1707 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1708 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1709 | const psa_drv_se_t *drv; |
| 1710 | psa_drv_se_context_t *drv_context; |
| 1711 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1712 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1713 | *data_length = 0; |
| 1714 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1715 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1716 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1717 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1718 | /* Reject a zero-length output buffer now, since this can never be a |
| 1719 | * valid key representation. This way we know that data must be a valid |
| 1720 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1721 | if( data_size == 0 ) |
| 1722 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1723 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1724 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1725 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1726 | { |
| 1727 | psa_drv_se_export_key_t method; |
| 1728 | if( drv->key_management == NULL ) |
| 1729 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1730 | method = ( export_public_key ? |
| 1731 | drv->key_management->p_export_public : |
| 1732 | drv->key_management->p_export ); |
| 1733 | if( method == NULL ) |
| 1734 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1735 | return( method( drv_context, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1736 | psa_key_slot_get_slot_number( slot ), |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1737 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1738 | } |
| 1739 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1740 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1741 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1742 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1743 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1744 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1745 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1746 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1747 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1748 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1749 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1750 | /* Exporting public -> public */ |
| 1751 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1752 | } |
| 1753 | else if( !export_public_key ) |
| 1754 | { |
| 1755 | /* Exporting private -> private */ |
| 1756 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1757 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1758 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1759 | /* Need to export the public part of a private key, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1760 | * so conversion is needed. Try the accelerators first. */ |
| 1761 | psa_status_t status = psa_driver_wrapper_export_public_key( slot, |
| 1762 | data, |
| 1763 | data_size, |
| 1764 | data_length ); |
| 1765 | |
| 1766 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 1767 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 1768 | return( status ); |
| 1769 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1770 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1771 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1772 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1773 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1774 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1775 | status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1776 | slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1777 | slot->key.data, |
| 1778 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1779 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1780 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1781 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1782 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1783 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1784 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1785 | data, |
| 1786 | data_size, |
| 1787 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1788 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1789 | mbedtls_rsa_free( rsa ); |
| 1790 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1791 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1792 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1793 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1794 | /* We don't know how to convert a private RSA key to public. */ |
| 1795 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1796 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1797 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1798 | } |
| 1799 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1800 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1801 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1802 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1803 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1804 | status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1805 | slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1806 | slot->key.data, |
| 1807 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1808 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1809 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1810 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1811 | |
| 1812 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1813 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1814 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1815 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1816 | data, |
| 1817 | data_size, |
| 1818 | data_length ); |
| 1819 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1820 | mbedtls_ecp_keypair_free( ecp ); |
| 1821 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1822 | return( status ); |
| 1823 | #else |
| 1824 | /* 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] | 1825 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1826 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1827 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1828 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1829 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1830 | else |
| 1831 | { |
| 1832 | /* This shouldn't happen in the reference implementation, but |
| 1833 | it is valid for a special-purpose implementation to omit |
| 1834 | support for exporting certain key types. */ |
| 1835 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1836 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1839 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1840 | uint8_t *data, |
| 1841 | size_t data_size, |
| 1842 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1843 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1844 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1845 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1846 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* Set the key to empty now, so that even when there are errors, we always |
| 1849 | * set data_length to a value between 0 and data_size. On error, setting |
| 1850 | * the key to empty is a good choice because an empty key representation is |
| 1851 | * unlikely to be accepted anywhere. */ |
| 1852 | *data_length = 0; |
| 1853 | |
| 1854 | /* Export requires the EXPORT flag. There is an exception for public keys, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1855 | * which don't require any flag, but |
| 1856 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1857 | */ |
| 1858 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1859 | PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1860 | if( status != PSA_SUCCESS ) |
| 1861 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1862 | |
| 1863 | status = psa_internal_export_key( slot, data, data_size, data_length, 0 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1864 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1865 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1866 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1867 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1868 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1869 | 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] | 1870 | uint8_t *data, |
| 1871 | size_t data_size, |
| 1872 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1873 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1874 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1875 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1876 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1877 | |
| 1878 | /* Set the key to empty now, so that even when there are errors, we always |
| 1879 | * set data_length to a value between 0 and data_size. On error, setting |
| 1880 | * the key to empty is a good choice because an empty key representation is |
| 1881 | * unlikely to be accepted anywhere. */ |
| 1882 | *data_length = 0; |
| 1883 | |
| 1884 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1885 | 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] | 1886 | if( status != PSA_SUCCESS ) |
| 1887 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1888 | |
| 1889 | status = psa_internal_export_key( slot, data, data_size, data_length, 1 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1890 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1891 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1892 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1893 | } |
| 1894 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1895 | #if defined(static_assert) |
| 1896 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1897 | "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] | 1898 | 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] | 1899 | "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] | 1900 | 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] | 1901 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1902 | #endif |
| 1903 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1904 | /** Validate that a key policy is internally well-formed. |
| 1905 | * |
| 1906 | * This function only rejects invalid policies. It does not validate the |
| 1907 | * consistency of the policy with respect to other attributes of the key |
| 1908 | * such as the key type. |
| 1909 | */ |
| 1910 | 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] | 1911 | { |
| 1912 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1913 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1914 | PSA_KEY_USAGE_ENCRYPT | |
| 1915 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1916 | PSA_KEY_USAGE_SIGN_HASH | |
| 1917 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1918 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1919 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1920 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1921 | return( PSA_SUCCESS ); |
| 1922 | } |
| 1923 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1924 | /** Validate the internal consistency of key attributes. |
| 1925 | * |
| 1926 | * This function only rejects invalid attribute values. If does not |
| 1927 | * validate the consistency of the attributes with any key data that may |
| 1928 | * be involved in the creation of the key. |
| 1929 | * |
| 1930 | * Call this function early in the key creation process. |
| 1931 | * |
| 1932 | * \param[in] attributes Key attributes for the new key. |
| 1933 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1934 | * NULL for a transparent key. |
| 1935 | * |
| 1936 | */ |
| 1937 | static psa_status_t psa_validate_key_attributes( |
| 1938 | const psa_key_attributes_t *attributes, |
| 1939 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1940 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1941 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1942 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1943 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1944 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1945 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1946 | if( status != PSA_SUCCESS ) |
| 1947 | return( status ); |
| 1948 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1949 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1950 | if( status != PSA_SUCCESS ) |
| 1951 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1952 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1953 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1954 | { |
| 1955 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1956 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1957 | } |
| 1958 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1959 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1960 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1961 | if( status != PSA_SUCCESS ) |
| 1962 | return( status ); |
| 1963 | } |
| 1964 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1965 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1966 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1967 | return( status ); |
| 1968 | |
| 1969 | /* Refuse to create overly large keys. |
| 1970 | * Note that this doesn't trigger on import if the attributes don't |
| 1971 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1972 | * psa_import_key() needs its own checks. */ |
| 1973 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1974 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1975 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1976 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1977 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1978 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1979 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1980 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1981 | return( PSA_SUCCESS ); |
| 1982 | } |
| 1983 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1984 | /** Prepare a key slot to receive key material. |
| 1985 | * |
| 1986 | * This function allocates a key slot and sets its metadata. |
| 1987 | * |
| 1988 | * If this function fails, call psa_fail_key_creation(). |
| 1989 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1990 | * This function is intended to be used as follows: |
| 1991 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1992 | * it with the specified attributes, and in case of a volatile key assign it |
| 1993 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1994 | * -# Populate the slot with the key material. |
| 1995 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1996 | * In case of failure at any step, stop the sequence and call |
| 1997 | * psa_fail_key_creation(). |
| 1998 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1999 | * On success, the key slot is locked. It is the responsibility of the caller |
| 2000 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2001 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2002 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2003 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2004 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 2005 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 2006 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2007 | * |
| 2008 | * \retval #PSA_SUCCESS |
| 2009 | * The key slot is ready to receive key material. |
| 2010 | * \return If this function fails, the key slot is an invalid state. |
| 2011 | * 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] | 2012 | */ |
| 2013 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2014 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2015 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2016 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2017 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2018 | { |
| 2019 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 2020 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2021 | psa_key_slot_t *slot; |
| 2022 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2023 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2024 | *p_drv = NULL; |
| 2025 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2026 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 2027 | if( status != PSA_SUCCESS ) |
| 2028 | return( status ); |
| 2029 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2030 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2031 | if( status != PSA_SUCCESS ) |
| 2032 | return( status ); |
| 2033 | slot = *p_slot; |
| 2034 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2035 | /* We're storing the declared bit-size of the key. It's up to each |
| 2036 | * creation mechanism to verify that this information is correct. |
| 2037 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2038 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2039 | * is optional (import, copy). In case of a volatile key, assign it the |
| 2040 | * volatile key identifier associated to the slot returned to contain its |
| 2041 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2042 | |
| 2043 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2044 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 2045 | { |
| 2046 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 2047 | slot->attr.id = volatile_key_id; |
| 2048 | #else |
| 2049 | slot->attr.id.key_id = volatile_key_id; |
| 2050 | #endif |
| 2051 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 2052 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2053 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2054 | * external-only flags, query `attributes`. Thanks to the check |
| 2055 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 2056 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2057 | * may have set. */ |
| 2058 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2059 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2060 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2061 | /* 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] | 2062 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2063 | * create the key file in internal storage, create the |
| 2064 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2065 | * persistent data. This is done by starting a transaction that will |
| 2066 | * encompass these three actions. |
| 2067 | * For registering a volatile key, we just need to find an appropriate |
| 2068 | * slot number inside the SE. Since the key is designated volatile, creating |
| 2069 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2070 | /* The first thing to do is to find a slot number for the new key. |
| 2071 | * We save the slot number in persistent storage as part of the |
| 2072 | * transaction data. It will be needed to recover if the power |
| 2073 | * fails during the key creation process, to clean up on the secure |
| 2074 | * element side after restarting. Obtaining a slot number from the |
| 2075 | * secure element driver updates its persistent state, but we do not yet |
| 2076 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2077 | * 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] | 2078 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2079 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2080 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 2081 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2082 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2083 | if( status != PSA_SUCCESS ) |
| 2084 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2085 | |
| 2086 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2087 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2088 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 2089 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2090 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2091 | psa_crypto_transaction.key.id = slot->attr.id; |
| 2092 | status = psa_crypto_save_transaction( ); |
| 2093 | if( status != PSA_SUCCESS ) |
| 2094 | { |
| 2095 | (void) psa_crypto_stop_transaction( ); |
| 2096 | return( status ); |
| 2097 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2098 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2099 | |
| 2100 | status = psa_copy_key_material_into_slot( |
| 2101 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2102 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2103 | |
| 2104 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2105 | { |
| 2106 | /* Key registration only makes sense with a secure element. */ |
| 2107 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2108 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2109 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2110 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2111 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2112 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2113 | |
| 2114 | /** Finalize the creation of a key once its key material has been set. |
| 2115 | * |
| 2116 | * This entails writing the key to persistent storage. |
| 2117 | * |
| 2118 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2119 | * See the documentation of psa_start_key_creation() for the intended use |
| 2120 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2121 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2122 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 2123 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 2124 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2125 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2126 | * \param[in,out] slot Pointer to the slot with key material. |
| 2127 | * \param[in] driver The secure element driver for the key, |
| 2128 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2129 | * \param[out] key On success, identifier of the key. Note that the |
| 2130 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2131 | * |
| 2132 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2133 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2134 | * \return If this function fails, the key slot is an invalid state. |
| 2135 | * 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] | 2136 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2137 | static psa_status_t psa_finish_key_creation( |
| 2138 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2139 | psa_se_drv_table_entry_t *driver, |
| 2140 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2141 | { |
| 2142 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2143 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2144 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2145 | |
| 2146 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2147 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2148 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2149 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2150 | if( driver != NULL ) |
| 2151 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2152 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2153 | psa_key_slot_number_t slot_number = |
| 2154 | psa_key_slot_get_slot_number( slot ) ; |
| 2155 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2156 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2157 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2158 | sizeof( data.slot_number ), |
| 2159 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2160 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2161 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2162 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2163 | (uint8_t*) &data, |
| 2164 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2165 | } |
| 2166 | else |
| 2167 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2168 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2169 | /* Key material is saved in export representation in the slot, so |
| 2170 | * just pass the slot buffer for storage. */ |
| 2171 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2172 | slot->key.data, |
| 2173 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2174 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2175 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2176 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2177 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2178 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2179 | /* Finish the transaction for a key creation. This does not |
| 2180 | * happen when registering an existing key. Detect this case |
| 2181 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2182 | * creation of a persistent key in a secure element requires a transaction, |
| 2183 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2184 | if( driver != NULL && |
| 2185 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2186 | { |
| 2187 | status = psa_save_se_persistent_data( driver ); |
| 2188 | if( status != PSA_SUCCESS ) |
| 2189 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2190 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2191 | return( status ); |
| 2192 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2193 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2194 | } |
| 2195 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2196 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2197 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2198 | { |
| 2199 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2200 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2201 | if( status != PSA_SUCCESS ) |
| 2202 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2203 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2204 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2205 | return( status ); |
| 2206 | } |
| 2207 | |
| 2208 | /** Abort the creation of a key. |
| 2209 | * |
| 2210 | * You may call this function after calling psa_start_key_creation(), |
| 2211 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2212 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2213 | * See the documentation of psa_start_key_creation() for the intended use |
| 2214 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2215 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2216 | * \param[in,out] slot Pointer to the slot with key material. |
| 2217 | * \param[in] driver The secure element driver for the key, |
| 2218 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2219 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2220 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2221 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2222 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2223 | (void) driver; |
| 2224 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2225 | if( slot == NULL ) |
| 2226 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2227 | |
| 2228 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2229 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2230 | * element, and the failure happened later (when saving metadata |
| 2231 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2232 | * element. |
| 2233 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2234 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2235 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2236 | /* Abort the ongoing transaction if any (there may not be one if |
| 2237 | * the creation process failed before starting one, or if the |
| 2238 | * key creation is a registration of a key in a secure element). |
| 2239 | * Earlier functions must already have done what it takes to undo any |
| 2240 | * partial creation. All that's left is to update the transaction data |
| 2241 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2242 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2243 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2244 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2245 | psa_wipe_key_slot( slot ); |
| 2246 | } |
| 2247 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2248 | /** Validate optional attributes during key creation. |
| 2249 | * |
| 2250 | * Some key attributes are optional during key creation. If they are |
| 2251 | * specified in the attributes structure, check that they are consistent |
| 2252 | * with the data in the slot. |
| 2253 | * |
| 2254 | * This function should be called near the end of key creation, after |
| 2255 | * the slot in memory is fully populated but before saving persistent data. |
| 2256 | */ |
| 2257 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2258 | const psa_key_slot_t *slot, |
| 2259 | const psa_key_attributes_t *attributes ) |
| 2260 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2261 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2262 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2263 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2265 | } |
| 2266 | |
| 2267 | if( attributes->domain_parameters_size != 0 ) |
| 2268 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2269 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2270 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2271 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2272 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2273 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2274 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2275 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2276 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2277 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2278 | slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2279 | slot->key.data, |
| 2280 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2281 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2282 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2283 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2284 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2285 | mbedtls_mpi_init( &actual ); |
| 2286 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2287 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2288 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2289 | mbedtls_rsa_free( rsa ); |
| 2290 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2291 | if( ret != 0 ) |
| 2292 | goto rsa_exit; |
| 2293 | ret = mbedtls_mpi_read_binary( &required, |
| 2294 | attributes->domain_parameters, |
| 2295 | attributes->domain_parameters_size ); |
| 2296 | if( ret != 0 ) |
| 2297 | goto rsa_exit; |
| 2298 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2299 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2300 | rsa_exit: |
| 2301 | mbedtls_mpi_free( &actual ); |
| 2302 | mbedtls_mpi_free( &required ); |
| 2303 | if( ret != 0) |
| 2304 | return( mbedtls_to_psa_error( ret ) ); |
| 2305 | } |
| 2306 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2307 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2308 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2309 | { |
| 2310 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2311 | } |
| 2312 | } |
| 2313 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2314 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2315 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2316 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2317 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2318 | } |
| 2319 | |
| 2320 | return( PSA_SUCCESS ); |
| 2321 | } |
| 2322 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2323 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2324 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2325 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2326 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2327 | { |
| 2328 | psa_status_t status; |
| 2329 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2330 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2331 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2332 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2333 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2334 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2335 | * This also rejects any key which might be encoded as an empty string, |
| 2336 | * which is never valid. */ |
| 2337 | if( data_length == 0 ) |
| 2338 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2339 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2340 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2341 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2342 | if( status != PSA_SUCCESS ) |
| 2343 | goto exit; |
| 2344 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2345 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2346 | if( driver != NULL ) |
| 2347 | { |
| 2348 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2349 | /* The driver should set the number of key bits, however in |
| 2350 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2351 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2352 | if( drv->key_management == NULL || |
| 2353 | drv->key_management->p_import == NULL ) |
| 2354 | { |
| 2355 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2356 | goto exit; |
| 2357 | } |
| 2358 | status = drv->key_management->p_import( |
| 2359 | psa_get_se_driver_context( driver ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2360 | psa_key_slot_get_slot_number( slot ), |
| 2361 | attributes, data, data_length, &bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2362 | if( status != PSA_SUCCESS ) |
| 2363 | goto exit; |
| 2364 | if( bits > PSA_MAX_KEY_BITS ) |
| 2365 | { |
| 2366 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2367 | goto exit; |
| 2368 | } |
| 2369 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2370 | } |
| 2371 | else |
| 2372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2373 | if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) ) |
| 2374 | { |
| 2375 | /* Importing a key with external lifetime through the driver wrapper |
| 2376 | * interface is not yet supported. Return as if this was an invalid |
| 2377 | * lifetime. */ |
| 2378 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2379 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2380 | } |
| 2381 | else |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2382 | { |
| 2383 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2384 | if( status != PSA_SUCCESS ) |
| 2385 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2386 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2387 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2388 | if( status != PSA_SUCCESS ) |
| 2389 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2390 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2391 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2392 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2393 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2394 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2395 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2396 | return( status ); |
| 2397 | } |
| 2398 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2399 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2400 | psa_status_t mbedtls_psa_register_se_key( |
| 2401 | const psa_key_attributes_t *attributes ) |
| 2402 | { |
| 2403 | psa_status_t status; |
| 2404 | psa_key_slot_t *slot = NULL; |
| 2405 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2406 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2407 | |
| 2408 | /* Leaving attributes unspecified is not currently supported. |
| 2409 | * It could make sense to query the key type and size from the |
| 2410 | * secure element, but not all secure elements support this |
| 2411 | * and the driver HAL doesn't currently support it. */ |
| 2412 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2413 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2414 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2415 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2416 | |
| 2417 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2418 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2419 | if( status != PSA_SUCCESS ) |
| 2420 | goto exit; |
| 2421 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2422 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2423 | |
| 2424 | exit: |
| 2425 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2426 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2427 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2428 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2429 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2430 | return( status ); |
| 2431 | } |
| 2432 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2433 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2434 | 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] | 2435 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2436 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2437 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2438 | source->key.data, |
| 2439 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2440 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2441 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2442 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2443 | target->attr.type = source->attr.type; |
| 2444 | target->attr.bits = source->attr.bits; |
| 2445 | |
| 2446 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2447 | } |
| 2448 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2449 | 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] | 2450 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2451 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2452 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2453 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2454 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2455 | psa_key_slot_t *source_slot = NULL; |
| 2456 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2457 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2458 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2459 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2460 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2461 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2462 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2463 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2464 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2465 | goto exit; |
| 2466 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2467 | status = psa_validate_optional_attributes( source_slot, |
| 2468 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2469 | if( status != PSA_SUCCESS ) |
| 2470 | goto exit; |
| 2471 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2472 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2473 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2474 | if( status != PSA_SUCCESS ) |
| 2475 | goto exit; |
| 2476 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2477 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2478 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2479 | if( status != PSA_SUCCESS ) |
| 2480 | goto exit; |
| 2481 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2482 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2483 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2484 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2485 | /* Copying to a secure element is not implemented yet. */ |
| 2486 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2487 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2488 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2489 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2490 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2491 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2492 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2493 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2494 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2495 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2496 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2497 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2498 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2499 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2500 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2501 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2502 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2503 | } |
| 2504 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2505 | |
| 2506 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2507 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2508 | /* Message digests */ |
| 2509 | /****************************************************************/ |
| 2510 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2511 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2512 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2513 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2514 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2515 | 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] | 2516 | { |
| 2517 | switch( alg ) |
| 2518 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2519 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2520 | case PSA_ALG_MD2: |
| 2521 | return( &mbedtls_md2_info ); |
| 2522 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2523 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2524 | case PSA_ALG_MD4: |
| 2525 | return( &mbedtls_md4_info ); |
| 2526 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2527 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2528 | case PSA_ALG_MD5: |
| 2529 | return( &mbedtls_md5_info ); |
| 2530 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2531 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2532 | case PSA_ALG_RIPEMD160: |
| 2533 | return( &mbedtls_ripemd160_info ); |
| 2534 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2535 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2536 | case PSA_ALG_SHA_1: |
| 2537 | return( &mbedtls_sha1_info ); |
| 2538 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2539 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2540 | case PSA_ALG_SHA_224: |
| 2541 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2542 | #endif |
| 2543 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2544 | case PSA_ALG_SHA_256: |
| 2545 | return( &mbedtls_sha256_info ); |
| 2546 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2547 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2548 | case PSA_ALG_SHA_384: |
| 2549 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2550 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2551 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2552 | case PSA_ALG_SHA_512: |
| 2553 | return( &mbedtls_sha512_info ); |
| 2554 | #endif |
| 2555 | default: |
| 2556 | return( NULL ); |
| 2557 | } |
| 2558 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2559 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2560 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2561 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2562 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2563 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2564 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2565 | { |
| 2566 | switch( operation->alg ) |
| 2567 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2568 | case 0: |
| 2569 | /* The object has (apparently) been initialized but it is not |
| 2570 | * in use. It's ok to call abort on such an object, and there's |
| 2571 | * nothing to do. */ |
| 2572 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2573 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2574 | case PSA_ALG_MD2: |
| 2575 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2576 | break; |
| 2577 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2578 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2579 | case PSA_ALG_MD4: |
| 2580 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2581 | break; |
| 2582 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2583 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2584 | case PSA_ALG_MD5: |
| 2585 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2586 | break; |
| 2587 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2588 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2589 | case PSA_ALG_RIPEMD160: |
| 2590 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2591 | break; |
| 2592 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2593 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2594 | case PSA_ALG_SHA_1: |
| 2595 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2596 | break; |
| 2597 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2598 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2599 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2600 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2601 | break; |
| 2602 | #endif |
| 2603 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2604 | case PSA_ALG_SHA_256: |
| 2605 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2606 | break; |
| 2607 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2608 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2609 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2610 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2611 | break; |
| 2612 | #endif |
| 2613 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2614 | case PSA_ALG_SHA_512: |
| 2615 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2616 | break; |
| 2617 | #endif |
| 2618 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2619 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2620 | } |
| 2621 | operation->alg = 0; |
| 2622 | return( PSA_SUCCESS ); |
| 2623 | } |
| 2624 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2625 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2626 | psa_algorithm_t alg ) |
| 2627 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2628 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2629 | |
| 2630 | /* A context must be freshly initialized before it can be set up. */ |
| 2631 | if( operation->alg != 0 ) |
| 2632 | { |
| 2633 | return( PSA_ERROR_BAD_STATE ); |
| 2634 | } |
| 2635 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2636 | switch( alg ) |
| 2637 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2638 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2639 | case PSA_ALG_MD2: |
| 2640 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2641 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2642 | break; |
| 2643 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2644 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2645 | case PSA_ALG_MD4: |
| 2646 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2647 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2648 | break; |
| 2649 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2650 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2651 | case PSA_ALG_MD5: |
| 2652 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2653 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2654 | break; |
| 2655 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2656 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2657 | case PSA_ALG_RIPEMD160: |
| 2658 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2659 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2660 | break; |
| 2661 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2662 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2663 | case PSA_ALG_SHA_1: |
| 2664 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2665 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2666 | break; |
| 2667 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2668 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2669 | case PSA_ALG_SHA_224: |
| 2670 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2671 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2672 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2673 | #endif |
| 2674 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2675 | case PSA_ALG_SHA_256: |
| 2676 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2677 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2678 | break; |
| 2679 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2680 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2681 | case PSA_ALG_SHA_384: |
| 2682 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2683 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2684 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2685 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2686 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2687 | case PSA_ALG_SHA_512: |
| 2688 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2689 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2690 | break; |
| 2691 | #endif |
| 2692 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2693 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2694 | PSA_ERROR_NOT_SUPPORTED : |
| 2695 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2696 | } |
| 2697 | if( ret == 0 ) |
| 2698 | operation->alg = alg; |
| 2699 | else |
| 2700 | psa_hash_abort( operation ); |
| 2701 | return( mbedtls_to_psa_error( ret ) ); |
| 2702 | } |
| 2703 | |
| 2704 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2705 | const uint8_t *input, |
| 2706 | size_t input_length ) |
| 2707 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2708 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2709 | |
| 2710 | /* Don't require hash implementations to behave correctly on a |
| 2711 | * zero-length input, which may have an invalid pointer. */ |
| 2712 | if( input_length == 0 ) |
| 2713 | return( PSA_SUCCESS ); |
| 2714 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2715 | switch( operation->alg ) |
| 2716 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2717 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2718 | case PSA_ALG_MD2: |
| 2719 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2720 | input, input_length ); |
| 2721 | break; |
| 2722 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2723 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2724 | case PSA_ALG_MD4: |
| 2725 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2726 | input, input_length ); |
| 2727 | break; |
| 2728 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2729 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2730 | case PSA_ALG_MD5: |
| 2731 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2732 | input, input_length ); |
| 2733 | break; |
| 2734 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2735 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2736 | case PSA_ALG_RIPEMD160: |
| 2737 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2738 | input, input_length ); |
| 2739 | break; |
| 2740 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2741 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2742 | case PSA_ALG_SHA_1: |
| 2743 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2744 | input, input_length ); |
| 2745 | break; |
| 2746 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2747 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2748 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2749 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2750 | input, input_length ); |
| 2751 | break; |
| 2752 | #endif |
| 2753 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2754 | case PSA_ALG_SHA_256: |
| 2755 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2756 | input, input_length ); |
| 2757 | break; |
| 2758 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2759 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2760 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2761 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2762 | input, input_length ); |
| 2763 | break; |
| 2764 | #endif |
| 2765 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2766 | case PSA_ALG_SHA_512: |
| 2767 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2768 | input, input_length ); |
| 2769 | break; |
| 2770 | #endif |
| 2771 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2772 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2773 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2774 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2775 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2776 | if( ret != 0 ) |
| 2777 | psa_hash_abort( operation ); |
| 2778 | return( mbedtls_to_psa_error( ret ) ); |
| 2779 | } |
| 2780 | |
| 2781 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2782 | uint8_t *hash, |
| 2783 | size_t hash_size, |
| 2784 | size_t *hash_length ) |
| 2785 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2786 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2787 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2788 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* Fill the output buffer with something that isn't a valid hash |
| 2791 | * (barring an attack on the hash and deliberately-crafted input), |
| 2792 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2793 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2794 | /* If hash_size is 0 then hash may be NULL and then the |
| 2795 | * call to memset would have undefined behavior. */ |
| 2796 | if( hash_size != 0 ) |
| 2797 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2798 | |
| 2799 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2800 | { |
| 2801 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2802 | goto exit; |
| 2803 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2804 | |
| 2805 | switch( operation->alg ) |
| 2806 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2807 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2808 | case PSA_ALG_MD2: |
| 2809 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2810 | break; |
| 2811 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2812 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2813 | case PSA_ALG_MD4: |
| 2814 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2815 | break; |
| 2816 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2817 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2818 | case PSA_ALG_MD5: |
| 2819 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2820 | break; |
| 2821 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2822 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2823 | case PSA_ALG_RIPEMD160: |
| 2824 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2825 | break; |
| 2826 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2827 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2828 | case PSA_ALG_SHA_1: |
| 2829 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2830 | break; |
| 2831 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2832 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2833 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2834 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2835 | break; |
| 2836 | #endif |
| 2837 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2838 | case PSA_ALG_SHA_256: |
| 2839 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2840 | break; |
| 2841 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2842 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2843 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2844 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2845 | break; |
| 2846 | #endif |
| 2847 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2848 | case PSA_ALG_SHA_512: |
| 2849 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2850 | break; |
| 2851 | #endif |
| 2852 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2853 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2854 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2855 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2856 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2857 | exit: |
| 2858 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2859 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2860 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2861 | return( psa_hash_abort( operation ) ); |
| 2862 | } |
| 2863 | else |
| 2864 | { |
| 2865 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2866 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2870 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2871 | const uint8_t *hash, |
| 2872 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2873 | { |
| 2874 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2875 | size_t actual_hash_length; |
| 2876 | psa_status_t status = psa_hash_finish( operation, |
| 2877 | actual_hash, sizeof( actual_hash ), |
| 2878 | &actual_hash_length ); |
| 2879 | if( status != PSA_SUCCESS ) |
| 2880 | return( status ); |
| 2881 | if( actual_hash_length != hash_length ) |
| 2882 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2883 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2884 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2885 | return( PSA_SUCCESS ); |
| 2886 | } |
| 2887 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2888 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2889 | const uint8_t *input, size_t input_length, |
| 2890 | uint8_t *hash, size_t hash_size, |
| 2891 | size_t *hash_length ) |
| 2892 | { |
| 2893 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2894 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2895 | |
| 2896 | *hash_length = hash_size; |
| 2897 | status = psa_hash_setup( &operation, alg ); |
| 2898 | if( status != PSA_SUCCESS ) |
| 2899 | goto exit; |
| 2900 | status = psa_hash_update( &operation, input, input_length ); |
| 2901 | if( status != PSA_SUCCESS ) |
| 2902 | goto exit; |
| 2903 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2904 | if( status != PSA_SUCCESS ) |
| 2905 | goto exit; |
| 2906 | |
| 2907 | exit: |
| 2908 | if( status == PSA_SUCCESS ) |
| 2909 | status = psa_hash_abort( &operation ); |
| 2910 | else |
| 2911 | psa_hash_abort( &operation ); |
| 2912 | return( status ); |
| 2913 | } |
| 2914 | |
| 2915 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2916 | const uint8_t *input, size_t input_length, |
| 2917 | const uint8_t *hash, size_t hash_length ) |
| 2918 | { |
| 2919 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2920 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2921 | |
| 2922 | status = psa_hash_setup( &operation, alg ); |
| 2923 | if( status != PSA_SUCCESS ) |
| 2924 | goto exit; |
| 2925 | status = psa_hash_update( &operation, input, input_length ); |
| 2926 | if( status != PSA_SUCCESS ) |
| 2927 | goto exit; |
| 2928 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2929 | if( status != PSA_SUCCESS ) |
| 2930 | goto exit; |
| 2931 | |
| 2932 | exit: |
| 2933 | if( status == PSA_SUCCESS ) |
| 2934 | status = psa_hash_abort( &operation ); |
| 2935 | else |
| 2936 | psa_hash_abort( &operation ); |
| 2937 | return( status ); |
| 2938 | } |
| 2939 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2940 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2941 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2942 | { |
| 2943 | if( target_operation->alg != 0 ) |
| 2944 | return( PSA_ERROR_BAD_STATE ); |
| 2945 | |
| 2946 | switch( source_operation->alg ) |
| 2947 | { |
| 2948 | case 0: |
| 2949 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2950 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2951 | case PSA_ALG_MD2: |
| 2952 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2953 | &source_operation->ctx.md2 ); |
| 2954 | break; |
| 2955 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2956 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2957 | case PSA_ALG_MD4: |
| 2958 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2959 | &source_operation->ctx.md4 ); |
| 2960 | break; |
| 2961 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2962 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2963 | case PSA_ALG_MD5: |
| 2964 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2965 | &source_operation->ctx.md5 ); |
| 2966 | break; |
| 2967 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2968 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2969 | case PSA_ALG_RIPEMD160: |
| 2970 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2971 | &source_operation->ctx.ripemd160 ); |
| 2972 | break; |
| 2973 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2974 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2975 | case PSA_ALG_SHA_1: |
| 2976 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2977 | &source_operation->ctx.sha1 ); |
| 2978 | break; |
| 2979 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2980 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2981 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2982 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2983 | &source_operation->ctx.sha256 ); |
| 2984 | break; |
| 2985 | #endif |
| 2986 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2987 | case PSA_ALG_SHA_256: |
| 2988 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2989 | &source_operation->ctx.sha256 ); |
| 2990 | break; |
| 2991 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2992 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2993 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2994 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2995 | &source_operation->ctx.sha512 ); |
| 2996 | break; |
| 2997 | #endif |
| 2998 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2999 | case PSA_ALG_SHA_512: |
| 3000 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 3001 | &source_operation->ctx.sha512 ); |
| 3002 | break; |
| 3003 | #endif |
| 3004 | default: |
| 3005 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3006 | } |
| 3007 | |
| 3008 | target_operation->alg = source_operation->alg; |
| 3009 | return( PSA_SUCCESS ); |
| 3010 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3011 | |
| 3012 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3013 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3014 | /* MAC */ |
| 3015 | /****************************************************************/ |
| 3016 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 3017 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3018 | psa_algorithm_t alg, |
| 3019 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3020 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3021 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3022 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3023 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3024 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3025 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3026 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3027 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3028 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3029 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 3030 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 3031 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3032 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 3033 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3034 | mode = MBEDTLS_MODE_STREAM; |
| 3035 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3036 | case PSA_ALG_CTR: |
| 3037 | mode = MBEDTLS_MODE_CTR; |
| 3038 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3039 | case PSA_ALG_CFB: |
| 3040 | mode = MBEDTLS_MODE_CFB; |
| 3041 | break; |
| 3042 | case PSA_ALG_OFB: |
| 3043 | mode = MBEDTLS_MODE_OFB; |
| 3044 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3045 | case PSA_ALG_ECB_NO_PADDING: |
| 3046 | mode = MBEDTLS_MODE_ECB; |
| 3047 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3048 | case PSA_ALG_CBC_NO_PADDING: |
| 3049 | mode = MBEDTLS_MODE_CBC; |
| 3050 | break; |
| 3051 | case PSA_ALG_CBC_PKCS7: |
| 3052 | mode = MBEDTLS_MODE_CBC; |
| 3053 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3054 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3055 | mode = MBEDTLS_MODE_CCM; |
| 3056 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3057 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3058 | mode = MBEDTLS_MODE_GCM; |
| 3059 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3060 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3061 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 3062 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3063 | default: |
| 3064 | return( NULL ); |
| 3065 | } |
| 3066 | } |
| 3067 | else if( alg == PSA_ALG_CMAC ) |
| 3068 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3069 | else |
| 3070 | return( NULL ); |
| 3071 | |
| 3072 | switch( key_type ) |
| 3073 | { |
| 3074 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3075 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3076 | break; |
| 3077 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3078 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 3079 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3080 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3081 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3082 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3083 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3084 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 3085 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 3086 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 3087 | if( key_bits == 128 ) |
| 3088 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3089 | break; |
| 3090 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3091 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3092 | break; |
| 3093 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3094 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3095 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3096 | case PSA_KEY_TYPE_CHACHA20: |
| 3097 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 3098 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3099 | default: |
| 3100 | return( NULL ); |
| 3101 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3102 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3103 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3104 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3105 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 3106 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3109 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3110 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3111 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3112 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3113 | { |
| 3114 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3115 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3116 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3117 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3118 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3119 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3120 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3121 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3122 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3123 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3124 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3125 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3126 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3127 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3128 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3129 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3130 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3131 | return( 128 ); |
| 3132 | default: |
| 3133 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3134 | } |
| 3135 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3136 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 3137 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3138 | /* Initialize the MAC operation structure. Once this function has been |
| 3139 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3140 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3141 | psa_algorithm_t alg ) |
| 3142 | { |
| 3143 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3144 | |
| 3145 | operation->alg = alg; |
| 3146 | operation->key_set = 0; |
| 3147 | operation->iv_set = 0; |
| 3148 | operation->iv_required = 0; |
| 3149 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3150 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3151 | |
| 3152 | #if defined(MBEDTLS_CMAC_C) |
| 3153 | if( alg == PSA_ALG_CMAC ) |
| 3154 | { |
| 3155 | operation->iv_required = 0; |
| 3156 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3157 | status = PSA_SUCCESS; |
| 3158 | } |
| 3159 | else |
| 3160 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3161 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3162 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3163 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3164 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3165 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3166 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3167 | } |
| 3168 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3169 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3170 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3171 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3172 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | if( status != PSA_SUCCESS ) |
| 3176 | memset( operation, 0, sizeof( *operation ) ); |
| 3177 | return( status ); |
| 3178 | } |
| 3179 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3180 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3181 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3182 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3183 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3184 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3185 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3186 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3187 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3188 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3189 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3190 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3191 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3192 | /* The object has (apparently) been initialized but it is not |
| 3193 | * in use. It's ok to call abort on such an object, and there's |
| 3194 | * nothing to do. */ |
| 3195 | return( PSA_SUCCESS ); |
| 3196 | } |
| 3197 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3198 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3199 | if( operation->alg == PSA_ALG_CMAC ) |
| 3200 | { |
| 3201 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3202 | } |
| 3203 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3204 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3205 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3206 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3207 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3208 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3209 | } |
| 3210 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3211 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3212 | { |
| 3213 | /* Sanity check (shouldn't happen: operation->alg should |
| 3214 | * always have been initialized to a valid value). */ |
| 3215 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3216 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3217 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3218 | operation->alg = 0; |
| 3219 | operation->key_set = 0; |
| 3220 | operation->iv_set = 0; |
| 3221 | operation->iv_required = 0; |
| 3222 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3223 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3224 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3225 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3226 | |
| 3227 | bad_state: |
| 3228 | /* If abort is called on an uninitialized object, we can't trust |
| 3229 | * anything. Wipe the object in case it contains confidential data. |
| 3230 | * This may result in a memory leak if a pointer gets overwritten, |
| 3231 | * but it's too late to do anything about this. */ |
| 3232 | memset( operation, 0, sizeof( *operation ) ); |
| 3233 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3236 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3237 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3238 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3239 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3240 | const mbedtls_cipher_info_t *cipher_info ) |
| 3241 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3242 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3243 | |
| 3244 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3245 | |
| 3246 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3247 | if( ret != 0 ) |
| 3248 | return( ret ); |
| 3249 | |
| 3250 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3251 | slot->key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3252 | key_bits ); |
| 3253 | return( ret ); |
| 3254 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3255 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3256 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3257 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3258 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3259 | const uint8_t *key, |
| 3260 | size_t key_length, |
| 3261 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3262 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3263 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3264 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3265 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3266 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3267 | psa_status_t status; |
| 3268 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3269 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3270 | * overflow below. This should never trigger if the hash algorithm |
| 3271 | * is implemented correctly. */ |
| 3272 | /* The size checks against the ipad and opad buffers cannot be written |
| 3273 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3274 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3275 | if( block_size > sizeof( ipad ) ) |
| 3276 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3277 | if( block_size > sizeof( hmac->opad ) ) |
| 3278 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3279 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3280 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3281 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3282 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3283 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3284 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3285 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3286 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3287 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3288 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3289 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3290 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3291 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3292 | * an invalid pointer which would make the behavior undefined. */ |
| 3293 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3294 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3296 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3297 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3298 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3299 | ipad[i] ^= 0x36; |
| 3300 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3301 | |
| 3302 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3303 | * and filling the rest of opad with the requisite constant. */ |
| 3304 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3305 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3306 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3307 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3308 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3309 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3310 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3311 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3312 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3313 | |
| 3314 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3315 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3317 | return( status ); |
| 3318 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3319 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3320 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3321 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3322 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3323 | psa_algorithm_t alg, |
| 3324 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3325 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3326 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3327 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3328 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3329 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3330 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3331 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3332 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3333 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3334 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3335 | /* A context must be freshly initialized before it can be set up. */ |
| 3336 | if( operation->alg != 0 ) |
| 3337 | { |
| 3338 | return( PSA_ERROR_BAD_STATE ); |
| 3339 | } |
| 3340 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3341 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3342 | if( status != PSA_SUCCESS ) |
| 3343 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3344 | if( is_sign ) |
| 3345 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3346 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3347 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3348 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3349 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3350 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3351 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3352 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3353 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3354 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3355 | { |
| 3356 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3357 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3358 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3359 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3360 | if( cipher_info == NULL ) |
| 3361 | { |
| 3362 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3363 | goto exit; |
| 3364 | } |
| 3365 | operation->mac_size = cipher_info->block_size; |
| 3366 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3367 | status = mbedtls_to_psa_error( ret ); |
| 3368 | } |
| 3369 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3370 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3371 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3372 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3373 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3374 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3375 | if( hash_alg == 0 ) |
| 3376 | { |
| 3377 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3378 | goto exit; |
| 3379 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3380 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3381 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3382 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3383 | if( operation->mac_size == 0 || |
| 3384 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3385 | { |
| 3386 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3387 | goto exit; |
| 3388 | } |
| 3389 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3390 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3391 | { |
| 3392 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3393 | goto exit; |
| 3394 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3395 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3396 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3397 | slot->key.data, |
| 3398 | slot->key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3399 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3400 | } |
| 3401 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3402 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3403 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3404 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3405 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3406 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3407 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3408 | if( truncated == 0 ) |
| 3409 | { |
| 3410 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3411 | } |
| 3412 | else if( truncated < 4 ) |
| 3413 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3414 | /* A very short MAC is too short for security since it can be |
| 3415 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3416 | * so we make this our minimum, even though 32 bits is still |
| 3417 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3418 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3419 | } |
| 3420 | else if( truncated > operation->mac_size ) |
| 3421 | { |
| 3422 | /* It's impossible to "truncate" to a larger length. */ |
| 3423 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3424 | } |
| 3425 | else |
| 3426 | operation->mac_size = truncated; |
| 3427 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3428 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3429 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3430 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3431 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3432 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3433 | else |
| 3434 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3435 | operation->key_set = 1; |
| 3436 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3437 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3438 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3439 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3440 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3441 | } |
| 3442 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3443 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3444 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3445 | psa_algorithm_t alg ) |
| 3446 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3447 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3451 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3452 | psa_algorithm_t alg ) |
| 3453 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3454 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3455 | } |
| 3456 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3457 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3458 | const uint8_t *input, |
| 3459 | size_t input_length ) |
| 3460 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3461 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3462 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3463 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3464 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3465 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3466 | operation->has_input = 1; |
| 3467 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3468 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3469 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3470 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3471 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3472 | input, input_length ); |
| 3473 | status = mbedtls_to_psa_error( ret ); |
| 3474 | } |
| 3475 | else |
| 3476 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3477 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3478 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3479 | { |
| 3480 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3481 | input_length ); |
| 3482 | } |
| 3483 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3484 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3485 | { |
| 3486 | /* This shouldn't happen if `operation` was initialized by |
| 3487 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3488 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3489 | } |
| 3490 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3491 | if( status != PSA_SUCCESS ) |
| 3492 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3493 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3494 | } |
| 3495 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3496 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3497 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3498 | uint8_t *mac, |
| 3499 | size_t mac_size ) |
| 3500 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3501 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3502 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3503 | size_t hash_size = 0; |
| 3504 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3505 | psa_status_t status; |
| 3506 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3507 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3508 | if( status != PSA_SUCCESS ) |
| 3509 | return( status ); |
| 3510 | /* From here on, tmp needs to be wiped. */ |
| 3511 | |
| 3512 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3513 | if( status != PSA_SUCCESS ) |
| 3514 | goto exit; |
| 3515 | |
| 3516 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3517 | if( status != PSA_SUCCESS ) |
| 3518 | goto exit; |
| 3519 | |
| 3520 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3521 | if( status != PSA_SUCCESS ) |
| 3522 | goto exit; |
| 3523 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3524 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3525 | if( status != PSA_SUCCESS ) |
| 3526 | goto exit; |
| 3527 | |
| 3528 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3529 | |
| 3530 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3531 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3532 | return( status ); |
| 3533 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3534 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3535 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3536 | 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] | 3537 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3538 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3539 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3540 | if( ! operation->key_set ) |
| 3541 | return( PSA_ERROR_BAD_STATE ); |
| 3542 | if( operation->iv_required && ! operation->iv_set ) |
| 3543 | return( PSA_ERROR_BAD_STATE ); |
| 3544 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3545 | if( mac_size < operation->mac_size ) |
| 3546 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3547 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3548 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3549 | if( operation->alg == PSA_ALG_CMAC ) |
| 3550 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3551 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3552 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3553 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3554 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3555 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3556 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3557 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3558 | else |
| 3559 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3560 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3561 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3562 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3563 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3564 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3565 | } |
| 3566 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3567 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3568 | { |
| 3569 | /* This shouldn't happen if `operation` was initialized by |
| 3570 | * a setup function. */ |
| 3571 | return( PSA_ERROR_BAD_STATE ); |
| 3572 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3573 | } |
| 3574 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3575 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3576 | uint8_t *mac, |
| 3577 | size_t mac_size, |
| 3578 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3579 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3580 | psa_status_t status; |
| 3581 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3582 | if( operation->alg == 0 ) |
| 3583 | { |
| 3584 | return( PSA_ERROR_BAD_STATE ); |
| 3585 | } |
| 3586 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3587 | /* Fill the output buffer with something that isn't a valid mac |
| 3588 | * (barring an attack on the mac and deliberately-crafted input), |
| 3589 | * in case the caller doesn't check the return status properly. */ |
| 3590 | *mac_length = mac_size; |
| 3591 | /* If mac_size is 0 then mac may be NULL and then the |
| 3592 | * call to memset would have undefined behavior. */ |
| 3593 | if( mac_size != 0 ) |
| 3594 | memset( mac, '!', mac_size ); |
| 3595 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3596 | if( ! operation->is_sign ) |
| 3597 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3598 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3599 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3600 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3601 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3602 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3603 | if( status == PSA_SUCCESS ) |
| 3604 | { |
| 3605 | status = psa_mac_abort( operation ); |
| 3606 | if( status == PSA_SUCCESS ) |
| 3607 | *mac_length = operation->mac_size; |
| 3608 | else |
| 3609 | memset( mac, '!', mac_size ); |
| 3610 | } |
| 3611 | else |
| 3612 | psa_mac_abort( operation ); |
| 3613 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3614 | } |
| 3615 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3616 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3617 | const uint8_t *mac, |
| 3618 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3619 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3620 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3621 | psa_status_t status; |
| 3622 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3623 | if( operation->alg == 0 ) |
| 3624 | { |
| 3625 | return( PSA_ERROR_BAD_STATE ); |
| 3626 | } |
| 3627 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3628 | if( operation->is_sign ) |
| 3629 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3630 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3631 | } |
| 3632 | if( operation->mac_size != mac_length ) |
| 3633 | { |
| 3634 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3635 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3636 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3637 | |
| 3638 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3639 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3640 | if( status != PSA_SUCCESS ) |
| 3641 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3642 | |
| 3643 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3644 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3645 | |
| 3646 | cleanup: |
| 3647 | if( status == PSA_SUCCESS ) |
| 3648 | status = psa_mac_abort( operation ); |
| 3649 | else |
| 3650 | psa_mac_abort( operation ); |
| 3651 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3652 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3653 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3654 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3658 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3659 | /****************************************************************/ |
| 3660 | /* Asymmetric cryptography */ |
| 3661 | /****************************************************************/ |
| 3662 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3663 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3664 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3665 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3666 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3667 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3668 | size_t hash_length, |
| 3669 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3670 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3671 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3672 | 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] | 3673 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3674 | |
| 3675 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3676 | * parameters. Validate that it fits so that we don't risk an |
| 3677 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3678 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3679 | if( hash_length > UINT_MAX ) |
| 3680 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3681 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3682 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3683 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3684 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3685 | * must be correct. */ |
| 3686 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3687 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3688 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3689 | if( md_info == NULL ) |
| 3690 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3691 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3692 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3693 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3694 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3695 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3696 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3697 | /* PSS requires a hash internally. */ |
| 3698 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3699 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3700 | if( md_info == NULL ) |
| 3701 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3702 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3703 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3704 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3705 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3706 | } |
| 3707 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3708 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3709 | psa_algorithm_t alg, |
| 3710 | const uint8_t *hash, |
| 3711 | size_t hash_length, |
| 3712 | uint8_t *signature, |
| 3713 | size_t signature_size, |
| 3714 | size_t *signature_length ) |
| 3715 | { |
| 3716 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3717 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3718 | mbedtls_md_type_t md_alg; |
| 3719 | |
| 3720 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3721 | if( status != PSA_SUCCESS ) |
| 3722 | return( status ); |
| 3723 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3724 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3725 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3726 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3727 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3728 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3729 | { |
| 3730 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3731 | MBEDTLS_MD_NONE ); |
| 3732 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3733 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3734 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3735 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3736 | md_alg, |
| 3737 | (unsigned int) hash_length, |
| 3738 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3739 | signature ); |
| 3740 | } |
| 3741 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3742 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3743 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3744 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3745 | { |
| 3746 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3747 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3748 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3749 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3750 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3751 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3752 | (unsigned int) hash_length, |
| 3753 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3754 | signature ); |
| 3755 | } |
| 3756 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3757 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3758 | { |
| 3759 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3760 | } |
| 3761 | |
| 3762 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3763 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3764 | return( mbedtls_to_psa_error( ret ) ); |
| 3765 | } |
| 3766 | |
| 3767 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3768 | psa_algorithm_t alg, |
| 3769 | const uint8_t *hash, |
| 3770 | size_t hash_length, |
| 3771 | const uint8_t *signature, |
| 3772 | size_t signature_length ) |
| 3773 | { |
| 3774 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3775 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3776 | mbedtls_md_type_t md_alg; |
| 3777 | |
| 3778 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3779 | if( status != PSA_SUCCESS ) |
| 3780 | return( status ); |
| 3781 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3782 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3783 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3784 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3785 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3786 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3787 | { |
| 3788 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3789 | MBEDTLS_MD_NONE ); |
| 3790 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3791 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3792 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3793 | MBEDTLS_RSA_PUBLIC, |
| 3794 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3795 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3796 | hash, |
| 3797 | signature ); |
| 3798 | } |
| 3799 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3800 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3801 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3802 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3803 | { |
| 3804 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3805 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3806 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3807 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3808 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3809 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3810 | (unsigned int) hash_length, |
| 3811 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3812 | signature ); |
| 3813 | } |
| 3814 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3815 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3816 | { |
| 3817 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3818 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3819 | |
| 3820 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3821 | * the rest of the signature is invalid". This has little use in |
| 3822 | * practice and PSA doesn't report this distinction. */ |
| 3823 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3824 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3825 | return( mbedtls_to_psa_error( ret ) ); |
| 3826 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3827 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3828 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3829 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3830 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3831 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3832 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3833 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3834 | * (even though these functions don't modify it). */ |
| 3835 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3836 | psa_algorithm_t alg, |
| 3837 | const uint8_t *hash, |
| 3838 | size_t hash_length, |
| 3839 | uint8_t *signature, |
| 3840 | size_t signature_size, |
| 3841 | size_t *signature_length ) |
| 3842 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3843 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3844 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3845 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3846 | mbedtls_mpi_init( &r ); |
| 3847 | mbedtls_mpi_init( &s ); |
| 3848 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3849 | if( signature_size < 2 * curve_bytes ) |
| 3850 | { |
| 3851 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3852 | goto cleanup; |
| 3853 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3854 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3855 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3856 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3857 | { |
| 3858 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3859 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3860 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3861 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3862 | &ecp->d, hash, |
| 3863 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3864 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3865 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3866 | } |
| 3867 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3868 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3869 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3870 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3871 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3872 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3873 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3874 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3875 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3876 | |
| 3877 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3878 | signature, |
| 3879 | curve_bytes ) ); |
| 3880 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3881 | signature + curve_bytes, |
| 3882 | curve_bytes ) ); |
| 3883 | |
| 3884 | cleanup: |
| 3885 | mbedtls_mpi_free( &r ); |
| 3886 | mbedtls_mpi_free( &s ); |
| 3887 | if( ret == 0 ) |
| 3888 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3889 | return( mbedtls_to_psa_error( ret ) ); |
| 3890 | } |
| 3891 | |
| 3892 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3893 | const uint8_t *hash, |
| 3894 | size_t hash_length, |
| 3895 | const uint8_t *signature, |
| 3896 | size_t signature_length ) |
| 3897 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3898 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3899 | mbedtls_mpi r, s; |
| 3900 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3901 | mbedtls_mpi_init( &r ); |
| 3902 | mbedtls_mpi_init( &s ); |
| 3903 | |
| 3904 | if( signature_length != 2 * curve_bytes ) |
| 3905 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3906 | |
| 3907 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3908 | signature, |
| 3909 | curve_bytes ) ); |
| 3910 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3911 | signature + curve_bytes, |
| 3912 | curve_bytes ) ); |
| 3913 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3914 | /* Check whether the public part is loaded. If not, load it. */ |
| 3915 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3916 | { |
| 3917 | MBEDTLS_MPI_CHK( |
| 3918 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3919 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3920 | } |
| 3921 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3922 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3923 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3924 | |
| 3925 | cleanup: |
| 3926 | mbedtls_mpi_free( &r ); |
| 3927 | mbedtls_mpi_free( &s ); |
| 3928 | return( mbedtls_to_psa_error( ret ) ); |
| 3929 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3930 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3931 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3932 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3933 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3934 | psa_algorithm_t alg, |
| 3935 | const uint8_t *hash, |
| 3936 | size_t hash_length, |
| 3937 | uint8_t *signature, |
| 3938 | size_t signature_size, |
| 3939 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3940 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3941 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3942 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3943 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3944 | |
| 3945 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3946 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3947 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3948 | * buffer can in principle be empty since it doesn't actually have |
| 3949 | * to be a hash.) */ |
| 3950 | if( signature_size == 0 ) |
| 3951 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3952 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3953 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3954 | PSA_KEY_USAGE_SIGN_HASH, |
| 3955 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3956 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3957 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3958 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3959 | { |
| 3960 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3961 | goto exit; |
| 3962 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3963 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3964 | /* Try any of the available accelerators first */ |
| 3965 | status = psa_driver_wrapper_sign_hash( slot, |
| 3966 | alg, |
| 3967 | hash, |
| 3968 | hash_length, |
| 3969 | signature, |
| 3970 | signature_size, |
| 3971 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3972 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3973 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3974 | goto exit; |
| 3975 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3976 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3977 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3978 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3979 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3980 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3981 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3982 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3983 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3984 | slot->key.data, |
| 3985 | slot->key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3986 | &rsa ); |
| 3987 | if( status != PSA_SUCCESS ) |
| 3988 | goto exit; |
| 3989 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3990 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3991 | alg, |
| 3992 | hash, hash_length, |
| 3993 | signature, signature_size, |
| 3994 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3995 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3996 | mbedtls_rsa_free( rsa ); |
| 3997 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3998 | } |
| 3999 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4000 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4001 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4002 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4003 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4004 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4005 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4006 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 4007 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4008 | PSA_ALG_IS_ECDSA( alg ) |
| 4009 | #else |
| 4010 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 4011 | #endif |
| 4012 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4013 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4014 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4015 | status = psa_load_ecp_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4016 | slot->key.data, |
| 4017 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4018 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4019 | if( status != PSA_SUCCESS ) |
| 4020 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4021 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4022 | alg, |
| 4023 | hash, hash_length, |
| 4024 | signature, signature_size, |
| 4025 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4026 | mbedtls_ecp_keypair_free( ecp ); |
| 4027 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4028 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4029 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4030 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4031 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4032 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4033 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4034 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4035 | } |
| 4036 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4037 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4038 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4039 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4040 | |
| 4041 | exit: |
| 4042 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 4043 | * the trailing part on success) with something that isn't a valid mac |
| 4044 | * (barring an attack on the mac and deliberately-crafted input), |
| 4045 | * in case the caller doesn't check the return status properly. */ |
| 4046 | if( status == PSA_SUCCESS ) |
| 4047 | memset( signature + *signature_length, '!', |
| 4048 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 4049 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4050 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4051 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 4052 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4053 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4054 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4055 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4056 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4057 | } |
| 4058 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4059 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4060 | psa_algorithm_t alg, |
| 4061 | const uint8_t *hash, |
| 4062 | size_t hash_length, |
| 4063 | const uint8_t *signature, |
| 4064 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4065 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4066 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4067 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4068 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 4069 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4070 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 4071 | PSA_KEY_USAGE_VERIFY_HASH, |
| 4072 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4073 | if( status != PSA_SUCCESS ) |
| 4074 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4075 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4076 | /* Try any of the available accelerators first */ |
| 4077 | status = psa_driver_wrapper_verify_hash( slot, |
| 4078 | alg, |
| 4079 | hash, |
| 4080 | hash_length, |
| 4081 | signature, |
| 4082 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 4083 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4084 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4085 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4086 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4087 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 4088 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4089 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4090 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4091 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4092 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4093 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4094 | slot->key.data, |
| 4095 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4096 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4097 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4098 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4099 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4100 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4101 | alg, |
| 4102 | hash, hash_length, |
| 4103 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4104 | mbedtls_rsa_free( rsa ); |
| 4105 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4106 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4107 | } |
| 4108 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4109 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4110 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4111 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4112 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4113 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4114 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4115 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4116 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4117 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4118 | status = psa_load_ecp_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4119 | slot->key.data, |
| 4120 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4121 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4122 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4123 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4124 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4125 | hash, hash_length, |
| 4126 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4127 | mbedtls_ecp_keypair_free( ecp ); |
| 4128 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4129 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4130 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4131 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4132 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4133 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4134 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4135 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4136 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4137 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4138 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4139 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4140 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4141 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4142 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4143 | |
| 4144 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4145 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4146 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4147 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4148 | } |
| 4149 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4150 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4151 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4152 | mbedtls_rsa_context *rsa ) |
| 4153 | { |
| 4154 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4155 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4156 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4157 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4158 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4159 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4160 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4161 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4162 | psa_algorithm_t alg, |
| 4163 | const uint8_t *input, |
| 4164 | size_t input_length, |
| 4165 | const uint8_t *salt, |
| 4166 | size_t salt_length, |
| 4167 | uint8_t *output, |
| 4168 | size_t output_size, |
| 4169 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4170 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4171 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4172 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4173 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4174 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4175 | (void) input; |
| 4176 | (void) input_length; |
| 4177 | (void) salt; |
| 4178 | (void) output; |
| 4179 | (void) output_size; |
| 4180 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4181 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4182 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4183 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4184 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4185 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4186 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4187 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4188 | if( status != PSA_SUCCESS ) |
| 4189 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4190 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4191 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4192 | { |
| 4193 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4194 | goto exit; |
| 4195 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4196 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4197 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4198 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4199 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4200 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4201 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4202 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4203 | slot->key.data, |
| 4204 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4205 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4206 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4207 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4208 | |
| 4209 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4210 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4211 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4212 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4213 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4214 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4215 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4216 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4217 | status = mbedtls_to_psa_error( |
| 4218 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4219 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4220 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4221 | MBEDTLS_RSA_PUBLIC, |
| 4222 | input_length, |
| 4223 | input, |
| 4224 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4225 | } |
| 4226 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4227 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4228 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4229 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4230 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4231 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4232 | status = mbedtls_to_psa_error( |
| 4233 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4234 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4235 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4236 | MBEDTLS_RSA_PUBLIC, |
| 4237 | salt, salt_length, |
| 4238 | input_length, |
| 4239 | input, |
| 4240 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4241 | } |
| 4242 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4243 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4244 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4245 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4246 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4247 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4248 | rsa_exit: |
| 4249 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4250 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4251 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4252 | mbedtls_rsa_free( rsa ); |
| 4253 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4254 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4255 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4256 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4257 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4258 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4259 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4260 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4261 | |
| 4262 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4263 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4264 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4265 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4266 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4267 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4268 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4269 | psa_algorithm_t alg, |
| 4270 | const uint8_t *input, |
| 4271 | size_t input_length, |
| 4272 | const uint8_t *salt, |
| 4273 | size_t salt_length, |
| 4274 | uint8_t *output, |
| 4275 | size_t output_size, |
| 4276 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4277 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4278 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4279 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4280 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4281 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4282 | (void) input; |
| 4283 | (void) input_length; |
| 4284 | (void) salt; |
| 4285 | (void) output; |
| 4286 | (void) output_size; |
| 4287 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4288 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4289 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4290 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4292 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4293 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4294 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4295 | if( status != PSA_SUCCESS ) |
| 4296 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4297 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4298 | { |
| 4299 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4300 | goto exit; |
| 4301 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4302 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4303 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4304 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4305 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4306 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4307 | mbedtls_rsa_context *rsa = NULL; |
| 4308 | status = psa_load_rsa_representation( slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4309 | slot->key.data, |
| 4310 | slot->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4311 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4312 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4313 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4314 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4315 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4316 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4317 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4318 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4319 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4320 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4321 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4322 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4323 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4324 | status = mbedtls_to_psa_error( |
| 4325 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4326 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4327 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4328 | MBEDTLS_RSA_PRIVATE, |
| 4329 | output_length, |
| 4330 | input, |
| 4331 | output, |
| 4332 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4333 | } |
| 4334 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4335 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4336 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4337 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4338 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4339 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4340 | status = mbedtls_to_psa_error( |
| 4341 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4342 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4343 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4344 | MBEDTLS_RSA_PRIVATE, |
| 4345 | salt, salt_length, |
| 4346 | output_length, |
| 4347 | input, |
| 4348 | output, |
| 4349 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4350 | } |
| 4351 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4352 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4353 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4354 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4355 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4356 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4357 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4358 | mbedtls_rsa_free( rsa ); |
| 4359 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4360 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4361 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4362 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4363 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4364 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4365 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4366 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4367 | |
| 4368 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4369 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4370 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4371 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4372 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4373 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4374 | |
| 4375 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4376 | /****************************************************************/ |
| 4377 | /* Symmetric cryptography */ |
| 4378 | /****************************************************************/ |
| 4379 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4380 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4381 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4382 | psa_algorithm_t alg, |
| 4383 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4384 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4385 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4386 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4387 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4388 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4389 | size_t key_bits; |
| 4390 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4391 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4392 | PSA_KEY_USAGE_ENCRYPT : |
| 4393 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4394 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4395 | /* A context must be freshly initialized before it can be set up. */ |
| 4396 | if( operation->alg != 0 ) |
| 4397 | return( PSA_ERROR_BAD_STATE ); |
| 4398 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4399 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4400 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4401 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4402 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4403 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4404 | 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] | 4405 | if( status != PSA_SUCCESS ) |
| 4406 | goto exit; |
| 4407 | |
| 4408 | /* Initialize the operation struct members, except for alg. The alg member |
| 4409 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4410 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4411 | operation->key_set = 0; |
| 4412 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4413 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4414 | operation->iv_size = 0; |
| 4415 | operation->block_size = 0; |
| 4416 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4417 | operation->iv_required = 0; |
| 4418 | else |
| 4419 | operation->iv_required = 1; |
| 4420 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4421 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4422 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4423 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4424 | slot, |
| 4425 | alg ); |
| 4426 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4427 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4428 | slot, |
| 4429 | alg ); |
| 4430 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4431 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4432 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4433 | /* Once the driver context is initialised, it needs to be freed using |
| 4434 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4435 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4436 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4437 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4438 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4439 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4440 | goto exit; |
| 4441 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4442 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4443 | * available for the given algorithm & key. */ |
| 4444 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4445 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4446 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4447 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4448 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4449 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4450 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4451 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4452 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4453 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4454 | 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] | 4455 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4456 | { |
| 4457 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4458 | goto exit; |
| 4459 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4460 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4461 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4462 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4463 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4464 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4465 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4466 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4467 | { |
| 4468 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4469 | uint8_t keys[24]; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4470 | memcpy( keys, slot->key.data, 16 ); |
| 4471 | memcpy( keys + 16, slot->key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4472 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4473 | keys, |
| 4474 | 192, cipher_operation ); |
| 4475 | } |
| 4476 | else |
| 4477 | #endif |
| 4478 | { |
| 4479 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4480 | slot->key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4481 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4482 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4483 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4484 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4485 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4486 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4487 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4488 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4489 | case PSA_ALG_CBC_NO_PADDING: |
| 4490 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4491 | MBEDTLS_PADDING_NONE ); |
| 4492 | break; |
| 4493 | case PSA_ALG_CBC_PKCS7: |
| 4494 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4495 | MBEDTLS_PADDING_PKCS7 ); |
| 4496 | break; |
| 4497 | default: |
| 4498 | /* The algorithm doesn't involve padding. */ |
| 4499 | ret = 0; |
| 4500 | break; |
| 4501 | } |
| 4502 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4503 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4504 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4505 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4506 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4507 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4508 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4509 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4510 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4511 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4512 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4513 | #if defined(MBEDTLS_CHACHA20_C) |
| 4514 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4515 | 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] | 4516 | operation->iv_size = 12; |
| 4517 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4518 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4519 | status = PSA_SUCCESS; |
| 4520 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4521 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4522 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4523 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4524 | if( status == PSA_SUCCESS ) |
| 4525 | { |
| 4526 | /* Update operation flags for both driver and software implementations */ |
| 4527 | operation->key_set = 1; |
| 4528 | } |
| 4529 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4530 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4531 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4532 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4533 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4534 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4535 | } |
| 4536 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4537 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4538 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4539 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4540 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4541 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4542 | } |
| 4543 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4544 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4545 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4546 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4547 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4548 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4549 | } |
| 4550 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4551 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4552 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4553 | size_t iv_size, |
| 4554 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4555 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4556 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4557 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4558 | if( operation->iv_set || ! operation->iv_required ) |
| 4559 | { |
| 4560 | return( PSA_ERROR_BAD_STATE ); |
| 4561 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4562 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4563 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4564 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4565 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4566 | iv, |
| 4567 | iv_size, |
| 4568 | iv_length ); |
| 4569 | goto exit; |
| 4570 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4571 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4572 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4573 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4574 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4575 | goto exit; |
| 4576 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4577 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4578 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4579 | if( ret != 0 ) |
| 4580 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4581 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4582 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4583 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4584 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4585 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4586 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4587 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4588 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4589 | if( status == PSA_SUCCESS ) |
| 4590 | operation->iv_set = 1; |
| 4591 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4592 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4593 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4594 | } |
| 4595 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4596 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4597 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4598 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4599 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4600 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4602 | if( operation->iv_set || ! operation->iv_required ) |
| 4603 | { |
| 4604 | return( PSA_ERROR_BAD_STATE ); |
| 4605 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4606 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4607 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4608 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4609 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4610 | iv, |
| 4611 | iv_length ); |
| 4612 | goto exit; |
| 4613 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4614 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4615 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4616 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4617 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4618 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4619 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4620 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4621 | status = mbedtls_to_psa_error( ret ); |
| 4622 | exit: |
| 4623 | if( status == PSA_SUCCESS ) |
| 4624 | operation->iv_set = 1; |
| 4625 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4626 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4627 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4630 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4631 | * manual processing, since the PSA API is defined as being able to process |
| 4632 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4633 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4634 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4635 | mbedtls_cipher_context_t *ctx, |
| 4636 | const uint8_t *input, |
| 4637 | size_t input_length, |
| 4638 | uint8_t *output, |
| 4639 | size_t output_size, |
| 4640 | size_t *output_length ) |
| 4641 | { |
| 4642 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4643 | size_t block_size = ctx->cipher_info->block_size; |
| 4644 | size_t internal_output_length = 0; |
| 4645 | *output_length = 0; |
| 4646 | |
| 4647 | if( input_length == 0 ) |
| 4648 | { |
| 4649 | status = PSA_SUCCESS; |
| 4650 | goto exit; |
| 4651 | } |
| 4652 | |
| 4653 | if( ctx->unprocessed_len > 0 ) |
| 4654 | { |
| 4655 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4656 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4657 | |
| 4658 | if( input_length < bytes_to_copy ) |
| 4659 | bytes_to_copy = input_length; |
| 4660 | |
| 4661 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4662 | input, bytes_to_copy ); |
| 4663 | input_length -= bytes_to_copy; |
| 4664 | input += bytes_to_copy; |
| 4665 | ctx->unprocessed_len += bytes_to_copy; |
| 4666 | |
| 4667 | if( ctx->unprocessed_len == block_size ) |
| 4668 | { |
| 4669 | status = mbedtls_to_psa_error( |
| 4670 | mbedtls_cipher_update( ctx, |
| 4671 | ctx->unprocessed_data, |
| 4672 | block_size, |
| 4673 | output, &internal_output_length ) ); |
| 4674 | |
| 4675 | if( status != PSA_SUCCESS ) |
| 4676 | goto exit; |
| 4677 | |
| 4678 | output += internal_output_length; |
| 4679 | output_size -= internal_output_length; |
| 4680 | *output_length += internal_output_length; |
| 4681 | ctx->unprocessed_len = 0; |
| 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | while( input_length >= block_size ) |
| 4686 | { |
| 4687 | /* Run all full blocks we have, one by one */ |
| 4688 | status = mbedtls_to_psa_error( |
| 4689 | mbedtls_cipher_update( ctx, input, |
| 4690 | block_size, |
| 4691 | output, &internal_output_length ) ); |
| 4692 | |
| 4693 | if( status != PSA_SUCCESS ) |
| 4694 | goto exit; |
| 4695 | |
| 4696 | input_length -= block_size; |
| 4697 | input += block_size; |
| 4698 | |
| 4699 | output += internal_output_length; |
| 4700 | output_size -= internal_output_length; |
| 4701 | *output_length += internal_output_length; |
| 4702 | } |
| 4703 | |
| 4704 | if( input_length > 0 ) |
| 4705 | { |
| 4706 | /* Save unprocessed bytes for later processing */ |
| 4707 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4708 | input, input_length ); |
| 4709 | ctx->unprocessed_len += input_length; |
| 4710 | } |
| 4711 | |
| 4712 | status = PSA_SUCCESS; |
| 4713 | |
| 4714 | exit: |
| 4715 | return( status ); |
| 4716 | } |
| 4717 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4718 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4719 | const uint8_t *input, |
| 4720 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4721 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4722 | size_t output_size, |
| 4723 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4724 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4725 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4726 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4727 | if( operation->alg == 0 ) |
| 4728 | { |
| 4729 | return( PSA_ERROR_BAD_STATE ); |
| 4730 | } |
| 4731 | if( operation->iv_required && ! operation->iv_set ) |
| 4732 | { |
| 4733 | return( PSA_ERROR_BAD_STATE ); |
| 4734 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4735 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4736 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4737 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4738 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4739 | input, |
| 4740 | input_length, |
| 4741 | output, |
| 4742 | output_size, |
| 4743 | output_length ); |
| 4744 | goto exit; |
| 4745 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4746 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4747 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4748 | { |
| 4749 | /* Take the unprocessed partial block left over from previous |
| 4750 | * update calls, if any, plus the input to this call. Remove |
| 4751 | * the last partial block, if any. You get the data that will be |
| 4752 | * output in this call. */ |
| 4753 | expected_output_size = |
| 4754 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4755 | / operation->block_size * operation->block_size; |
| 4756 | } |
| 4757 | else |
| 4758 | { |
| 4759 | expected_output_size = input_length; |
| 4760 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4761 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4762 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4763 | { |
| 4764 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4765 | goto exit; |
| 4766 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4767 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4768 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4769 | { |
| 4770 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4771 | * process a single block at a time in ECB mode. Abstract away that |
| 4772 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4773 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4774 | input, |
| 4775 | input_length, |
| 4776 | output, |
| 4777 | output_size, |
| 4778 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4779 | } |
| 4780 | else |
| 4781 | { |
| 4782 | status = mbedtls_to_psa_error( |
| 4783 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4784 | input_length, output, output_length ) ); |
| 4785 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4786 | exit: |
| 4787 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4788 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4789 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4790 | } |
| 4791 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4792 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4793 | uint8_t *output, |
| 4794 | size_t output_size, |
| 4795 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4796 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4797 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4798 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4799 | if( operation->alg == 0 ) |
| 4800 | { |
| 4801 | return( PSA_ERROR_BAD_STATE ); |
| 4802 | } |
| 4803 | if( operation->iv_required && ! operation->iv_set ) |
| 4804 | { |
| 4805 | return( PSA_ERROR_BAD_STATE ); |
| 4806 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4807 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4808 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4809 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4810 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4811 | output, |
| 4812 | output_size, |
| 4813 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4814 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4815 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4816 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4817 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4818 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4819 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4820 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4821 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4822 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4823 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4824 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4825 | } |
| 4826 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4827 | status = mbedtls_to_psa_error( |
| 4828 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4829 | temp_output_buffer, |
| 4830 | output_length ) ); |
| 4831 | if( status != PSA_SUCCESS ) |
| 4832 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4833 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4834 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4835 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4836 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4837 | memcpy( output, temp_output_buffer, *output_length ); |
| 4838 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4839 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4840 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4841 | exit: |
| 4842 | if( operation->mbedtls_in_use == 1 ) |
| 4843 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4844 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4845 | if( status == PSA_SUCCESS ) |
| 4846 | return( psa_cipher_abort( operation ) ); |
| 4847 | else |
| 4848 | { |
| 4849 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4850 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4851 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4852 | return( status ); |
| 4853 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4854 | } |
| 4855 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4856 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4857 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4858 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4859 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4860 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4861 | * in use. It's ok to call abort on such an object, and there's |
| 4862 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4863 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4864 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4866 | /* Sanity check (shouldn't happen: operation->alg should |
| 4867 | * always have been initialized to a valid value). */ |
| 4868 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4869 | return( PSA_ERROR_BAD_STATE ); |
| 4870 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4871 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4872 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4873 | else |
| 4874 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4875 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4876 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4877 | operation->key_set = 0; |
| 4878 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4879 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4880 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4881 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4882 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4883 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4884 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4885 | } |
| 4886 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4887 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4888 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4889 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4890 | /****************************************************************/ |
| 4891 | /* AEAD */ |
| 4892 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4893 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4894 | typedef struct |
| 4895 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4896 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4897 | const mbedtls_cipher_info_t *cipher_info; |
| 4898 | union |
| 4899 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4900 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4901 | #if defined(MBEDTLS_CCM_C) |
| 4902 | mbedtls_ccm_context ccm; |
| 4903 | #endif /* MBEDTLS_CCM_C */ |
| 4904 | #if defined(MBEDTLS_GCM_C) |
| 4905 | mbedtls_gcm_context gcm; |
| 4906 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4907 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4908 | mbedtls_chachapoly_context chachapoly; |
| 4909 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4910 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4911 | psa_algorithm_t core_alg; |
| 4912 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4913 | uint8_t tag_length; |
| 4914 | } aead_operation_t; |
| 4915 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4916 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4917 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4918 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4919 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4920 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4921 | { |
| 4922 | #if defined(MBEDTLS_CCM_C) |
| 4923 | case PSA_ALG_CCM: |
| 4924 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4925 | break; |
| 4926 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4927 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4928 | case PSA_ALG_GCM: |
| 4929 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4930 | break; |
| 4931 | #endif /* MBEDTLS_GCM_C */ |
| 4932 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4933 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4934 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4938 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4939 | psa_key_usage_t usage, |
| 4940 | psa_algorithm_t alg ) |
| 4941 | { |
| 4942 | psa_status_t status; |
| 4943 | size_t key_bits; |
| 4944 | mbedtls_cipher_id_t cipher_id; |
| 4945 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4946 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4947 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4948 | if( status != PSA_SUCCESS ) |
| 4949 | return( status ); |
| 4950 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4951 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4952 | |
| 4953 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4954 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4955 | &cipher_id ); |
| 4956 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4957 | { |
| 4958 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4959 | goto cleanup; |
| 4960 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4961 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4962 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4963 | { |
| 4964 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4965 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4966 | operation->core_alg = PSA_ALG_CCM; |
| 4967 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4968 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4969 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4970 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4971 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4972 | { |
| 4973 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4974 | goto cleanup; |
| 4975 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4976 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4977 | status = mbedtls_to_psa_error( |
| 4978 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4979 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4980 | (unsigned int) key_bits ) ); |
| 4981 | if( status != 0 ) |
| 4982 | goto cleanup; |
| 4983 | break; |
| 4984 | #endif /* MBEDTLS_CCM_C */ |
| 4985 | |
| 4986 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4987 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4988 | operation->core_alg = PSA_ALG_GCM; |
| 4989 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4990 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4991 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4992 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4993 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4994 | { |
| 4995 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4996 | goto cleanup; |
| 4997 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4998 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4999 | status = mbedtls_to_psa_error( |
| 5000 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5001 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5002 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5003 | if( status != 0 ) |
| 5004 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5005 | break; |
| 5006 | #endif /* MBEDTLS_GCM_C */ |
| 5007 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5008 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5009 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 5010 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 5011 | operation->full_tag_length = 16; |
| 5012 | /* We only support the default tag length. */ |
| 5013 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5014 | { |
| 5015 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5016 | goto cleanup; |
| 5017 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5018 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 5019 | status = mbedtls_to_psa_error( |
| 5020 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5021 | operation->slot->key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5022 | if( status != 0 ) |
| 5023 | goto cleanup; |
| 5024 | break; |
| 5025 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 5026 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5027 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5028 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5029 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5030 | } |
| 5031 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5032 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 5033 | { |
| 5034 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5035 | goto cleanup; |
| 5036 | } |
| 5037 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5038 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5039 | return( PSA_SUCCESS ); |
| 5040 | |
| 5041 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5042 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5043 | return( status ); |
| 5044 | } |
| 5045 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5046 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5047 | psa_algorithm_t alg, |
| 5048 | const uint8_t *nonce, |
| 5049 | size_t nonce_length, |
| 5050 | const uint8_t *additional_data, |
| 5051 | size_t additional_data_length, |
| 5052 | const uint8_t *plaintext, |
| 5053 | size_t plaintext_length, |
| 5054 | uint8_t *ciphertext, |
| 5055 | size_t ciphertext_size, |
| 5056 | size_t *ciphertext_length ) |
| 5057 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5058 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5059 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 5060 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5061 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 5062 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 5063 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5064 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5065 | if( status != PSA_SUCCESS ) |
| 5066 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5067 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5068 | /* For all currently supported modes, the tag is at the end of the |
| 5069 | * ciphertext. */ |
| 5070 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 5071 | { |
| 5072 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5073 | goto exit; |
| 5074 | } |
| 5075 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5076 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5077 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5078 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5079 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5080 | status = mbedtls_to_psa_error( |
| 5081 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 5082 | MBEDTLS_GCM_ENCRYPT, |
| 5083 | plaintext_length, |
| 5084 | nonce, nonce_length, |
| 5085 | additional_data, additional_data_length, |
| 5086 | plaintext, ciphertext, |
| 5087 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5088 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5089 | else |
| 5090 | #endif /* MBEDTLS_GCM_C */ |
| 5091 | #if defined(MBEDTLS_CCM_C) |
| 5092 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5093 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5094 | status = mbedtls_to_psa_error( |
| 5095 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 5096 | plaintext_length, |
| 5097 | nonce, nonce_length, |
| 5098 | additional_data, |
| 5099 | additional_data_length, |
| 5100 | plaintext, ciphertext, |
| 5101 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5102 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5103 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5104 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5105 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5106 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5107 | { |
| 5108 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5109 | { |
| 5110 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5111 | goto exit; |
| 5112 | } |
| 5113 | status = mbedtls_to_psa_error( |
| 5114 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 5115 | plaintext_length, |
| 5116 | nonce, |
| 5117 | additional_data, |
| 5118 | additional_data_length, |
| 5119 | plaintext, |
| 5120 | ciphertext, |
| 5121 | tag ) ); |
| 5122 | } |
| 5123 | else |
| 5124 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5125 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5126 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5127 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5128 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5129 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 5130 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5131 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5132 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5133 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5134 | if( status == PSA_SUCCESS ) |
| 5135 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 5136 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5137 | } |
| 5138 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5139 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 5140 | * followed by the tag. Return the length of the part preceding the tag in |
| 5141 | * *plaintext_length. This is the size of the plaintext in modes where |
| 5142 | * the encrypted data has the same size as the plaintext, such as |
| 5143 | * CCM and GCM. */ |
| 5144 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 5145 | const uint8_t *ciphertext, |
| 5146 | size_t ciphertext_length, |
| 5147 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5148 | const uint8_t **p_tag ) |
| 5149 | { |
| 5150 | size_t payload_length; |
| 5151 | if( tag_length > ciphertext_length ) |
| 5152 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5153 | payload_length = ciphertext_length - tag_length; |
| 5154 | if( payload_length > plaintext_size ) |
| 5155 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5156 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5157 | return( PSA_SUCCESS ); |
| 5158 | } |
| 5159 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5160 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5161 | psa_algorithm_t alg, |
| 5162 | const uint8_t *nonce, |
| 5163 | size_t nonce_length, |
| 5164 | const uint8_t *additional_data, |
| 5165 | size_t additional_data_length, |
| 5166 | const uint8_t *ciphertext, |
| 5167 | size_t ciphertext_length, |
| 5168 | uint8_t *plaintext, |
| 5169 | size_t plaintext_size, |
| 5170 | size_t *plaintext_length ) |
| 5171 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5172 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5173 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5174 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5175 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5176 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5177 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5178 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5179 | if( status != PSA_SUCCESS ) |
| 5180 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5181 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5182 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5183 | ciphertext, ciphertext_length, |
| 5184 | plaintext_size, &tag ); |
| 5185 | if( status != PSA_SUCCESS ) |
| 5186 | goto exit; |
| 5187 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5188 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5189 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5190 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5191 | status = mbedtls_to_psa_error( |
| 5192 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5193 | ciphertext_length - operation.tag_length, |
| 5194 | nonce, nonce_length, |
| 5195 | additional_data, |
| 5196 | additional_data_length, |
| 5197 | tag, operation.tag_length, |
| 5198 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5199 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5200 | else |
| 5201 | #endif /* MBEDTLS_GCM_C */ |
| 5202 | #if defined(MBEDTLS_CCM_C) |
| 5203 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5204 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5205 | status = mbedtls_to_psa_error( |
| 5206 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5207 | ciphertext_length - operation.tag_length, |
| 5208 | nonce, nonce_length, |
| 5209 | additional_data, |
| 5210 | additional_data_length, |
| 5211 | ciphertext, plaintext, |
| 5212 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5213 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5214 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5215 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5216 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5217 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5218 | { |
| 5219 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5220 | { |
| 5221 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5222 | goto exit; |
| 5223 | } |
| 5224 | status = mbedtls_to_psa_error( |
| 5225 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5226 | ciphertext_length - operation.tag_length, |
| 5227 | nonce, |
| 5228 | additional_data, |
| 5229 | additional_data_length, |
| 5230 | tag, |
| 5231 | ciphertext, |
| 5232 | plaintext ) ); |
| 5233 | } |
| 5234 | else |
| 5235 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5236 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5237 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5238 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5239 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5240 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5241 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5242 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5243 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5244 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5245 | if( status == PSA_SUCCESS ) |
| 5246 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5247 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5248 | } |
| 5249 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5250 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5251 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5252 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5253 | /* Generators */ |
| 5254 | /****************************************************************/ |
| 5255 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5256 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 5257 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5258 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5259 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 5260 | #endif |
| 5261 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5262 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5263 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5264 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5265 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5266 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5267 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5268 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5269 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5270 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5271 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5272 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5273 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5274 | } |
| 5275 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5276 | 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] | 5277 | { |
| 5278 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5279 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5280 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5281 | { |
| 5282 | /* The object has (apparently) been initialized but it is not |
| 5283 | * in use. It's ok to call abort on such an object, and there's |
| 5284 | * nothing to do. */ |
| 5285 | } |
| 5286 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5287 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5288 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5289 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5290 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5291 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5292 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5293 | else |
| 5294 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5296 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5297 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5298 | /* 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] | 5299 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5300 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5301 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5302 | { |
| 5303 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5304 | operation->ctx.tls12_prf.seed_length ); |
| 5305 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5306 | } |
| 5307 | |
| 5308 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5309 | { |
| 5310 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5311 | operation->ctx.tls12_prf.label_length ); |
| 5312 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5313 | } |
| 5314 | |
| 5315 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5316 | |
| 5317 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5318 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5319 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5320 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5321 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5322 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5323 | { |
| 5324 | status = PSA_ERROR_BAD_STATE; |
| 5325 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5326 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5327 | return( status ); |
| 5328 | } |
| 5329 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5330 | 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] | 5331 | size_t *capacity) |
| 5332 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5333 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5334 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5335 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5336 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5339 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5340 | return( PSA_SUCCESS ); |
| 5341 | } |
| 5342 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5343 | 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] | 5344 | size_t capacity ) |
| 5345 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5346 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5347 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5348 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5349 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5350 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5351 | return( PSA_SUCCESS ); |
| 5352 | } |
| 5353 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5354 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5355 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5356 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5357 | 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] | 5358 | psa_algorithm_t hash_alg, |
| 5359 | uint8_t *output, |
| 5360 | size_t output_length ) |
| 5361 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5362 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5363 | psa_status_t status; |
| 5364 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5365 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5366 | return( PSA_ERROR_BAD_STATE ); |
| 5367 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5368 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5369 | while( output_length != 0 ) |
| 5370 | { |
| 5371 | /* Copy what remains of the current block */ |
| 5372 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5373 | if( n > output_length ) |
| 5374 | n = (uint8_t) output_length; |
| 5375 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5376 | output += n; |
| 5377 | output_length -= n; |
| 5378 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5379 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5380 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5381 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5382 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5383 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5384 | * object was corrupted or if this function is called directly |
| 5385 | * inside the library. */ |
| 5386 | if( hkdf->block_number == 0xff ) |
| 5387 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5388 | |
| 5389 | /* We need a new block */ |
| 5390 | ++hkdf->block_number; |
| 5391 | hkdf->offset_in_block = 0; |
| 5392 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5393 | hkdf->prk, hash_length, |
| 5394 | hash_alg ); |
| 5395 | if( status != PSA_SUCCESS ) |
| 5396 | return( status ); |
| 5397 | if( hkdf->block_number != 1 ) |
| 5398 | { |
| 5399 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5400 | hkdf->output_block, |
| 5401 | hash_length ); |
| 5402 | if( status != PSA_SUCCESS ) |
| 5403 | return( status ); |
| 5404 | } |
| 5405 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5406 | hkdf->info, |
| 5407 | hkdf->info_length ); |
| 5408 | if( status != PSA_SUCCESS ) |
| 5409 | return( status ); |
| 5410 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5411 | &hkdf->block_number, 1 ); |
| 5412 | if( status != PSA_SUCCESS ) |
| 5413 | return( status ); |
| 5414 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5415 | hkdf->output_block, |
| 5416 | sizeof( hkdf->output_block ) ); |
| 5417 | if( status != PSA_SUCCESS ) |
| 5418 | return( status ); |
| 5419 | } |
| 5420 | |
| 5421 | return( PSA_SUCCESS ); |
| 5422 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5423 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5424 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5425 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5426 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5427 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5428 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5429 | psa_algorithm_t alg ) |
| 5430 | { |
| 5431 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5432 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5433 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5434 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5435 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5436 | /* We can't be wanting more output after block 0xff, otherwise |
| 5437 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5438 | * prevented this call. It could happen only if the operation |
| 5439 | * object was corrupted or if this function is called directly |
| 5440 | * inside the library. */ |
| 5441 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5442 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5443 | |
| 5444 | /* We need a new block */ |
| 5445 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5446 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5447 | |
| 5448 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5449 | * |
| 5450 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5451 | * |
| 5452 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5453 | * HMAC_hash(secret, A(2) + seed) + |
| 5454 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5455 | * |
| 5456 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5457 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5458 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5459 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5460 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5461 | * is currently extracted as `output_block` and where i is |
| 5462 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5463 | */ |
| 5464 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5465 | /* Save the hash context before using it, to preserve the hash state with |
| 5466 | * only the inner padding in it. We need this, because inner padding depends |
| 5467 | * on the key (secret in the RFC's terminology). */ |
| 5468 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5469 | if( status != PSA_SUCCESS ) |
| 5470 | goto cleanup; |
| 5471 | |
| 5472 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5473 | if( tls12_prf->block_number == 1 ) |
| 5474 | { |
| 5475 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5476 | * the variable seed and in this instance means it in the context of the |
| 5477 | * P_hash function, where seed = label + seed.) */ |
| 5478 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5479 | tls12_prf->label, tls12_prf->label_length ); |
| 5480 | if( status != PSA_SUCCESS ) |
| 5481 | goto cleanup; |
| 5482 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5483 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5484 | if( status != PSA_SUCCESS ) |
| 5485 | goto cleanup; |
| 5486 | } |
| 5487 | else |
| 5488 | { |
| 5489 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5490 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5491 | tls12_prf->Ai, hash_length ); |
| 5492 | if( status != PSA_SUCCESS ) |
| 5493 | goto cleanup; |
| 5494 | } |
| 5495 | |
| 5496 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5497 | tls12_prf->Ai, hash_length ); |
| 5498 | if( status != PSA_SUCCESS ) |
| 5499 | goto cleanup; |
| 5500 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5501 | if( status != PSA_SUCCESS ) |
| 5502 | goto cleanup; |
| 5503 | |
| 5504 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5505 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5506 | tls12_prf->Ai, hash_length ); |
| 5507 | if( status != PSA_SUCCESS ) |
| 5508 | goto cleanup; |
| 5509 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5510 | tls12_prf->label, tls12_prf->label_length ); |
| 5511 | if( status != PSA_SUCCESS ) |
| 5512 | goto cleanup; |
| 5513 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5514 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5515 | if( status != PSA_SUCCESS ) |
| 5516 | goto cleanup; |
| 5517 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5518 | tls12_prf->output_block, hash_length ); |
| 5519 | if( status != PSA_SUCCESS ) |
| 5520 | goto cleanup; |
| 5521 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5522 | if( status != PSA_SUCCESS ) |
| 5523 | goto cleanup; |
| 5524 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5525 | |
| 5526 | cleanup: |
| 5527 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5528 | cleanup_status = psa_hash_abort( &backup ); |
| 5529 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5530 | status = cleanup_status; |
| 5531 | |
| 5532 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5533 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5534 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5535 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5536 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5537 | psa_algorithm_t alg, |
| 5538 | uint8_t *output, |
| 5539 | size_t output_length ) |
| 5540 | { |
| 5541 | 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] | 5542 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5543 | psa_status_t status; |
| 5544 | uint8_t offset, length; |
| 5545 | |
| 5546 | while( output_length != 0 ) |
| 5547 | { |
| 5548 | /* Check if we have fully processed the current block. */ |
| 5549 | if( tls12_prf->left_in_block == 0 ) |
| 5550 | { |
| 5551 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5552 | alg ); |
| 5553 | if( status != PSA_SUCCESS ) |
| 5554 | return( status ); |
| 5555 | |
| 5556 | continue; |
| 5557 | } |
| 5558 | |
| 5559 | if( tls12_prf->left_in_block > output_length ) |
| 5560 | length = (uint8_t) output_length; |
| 5561 | else |
| 5562 | length = tls12_prf->left_in_block; |
| 5563 | |
| 5564 | offset = hash_length - tls12_prf->left_in_block; |
| 5565 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5566 | output += length; |
| 5567 | output_length -= length; |
| 5568 | tls12_prf->left_in_block -= length; |
| 5569 | } |
| 5570 | |
| 5571 | return( PSA_SUCCESS ); |
| 5572 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5573 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5574 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5575 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5576 | psa_status_t psa_key_derivation_output_bytes( |
| 5577 | psa_key_derivation_operation_t *operation, |
| 5578 | uint8_t *output, |
| 5579 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5580 | { |
| 5581 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5582 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5583 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5584 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5585 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5586 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5587 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5590 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5591 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5592 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5593 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5594 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5595 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5596 | goto exit; |
| 5597 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5598 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5599 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5600 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5601 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5602 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5603 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5604 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5605 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5606 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5607 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5608 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5609 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5610 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5611 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5612 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5613 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5614 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5615 | output, output_length ); |
| 5616 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5617 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5618 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5619 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5620 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5621 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5622 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5623 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5624 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5625 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5626 | output_length ); |
| 5627 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5628 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5629 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5630 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5631 | { |
| 5632 | return( PSA_ERROR_BAD_STATE ); |
| 5633 | } |
| 5634 | |
| 5635 | exit: |
| 5636 | if( status != PSA_SUCCESS ) |
| 5637 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5638 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5639 | * This allows us to differentiate between exhausted operations and |
| 5640 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5641 | * operations. */ |
| 5642 | psa_algorithm_t alg = operation->alg; |
| 5643 | psa_key_derivation_abort( operation ); |
| 5644 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5645 | memset( output, '!', output_length ); |
| 5646 | } |
| 5647 | return( status ); |
| 5648 | } |
| 5649 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5650 | #if defined(MBEDTLS_DES_C) |
| 5651 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5652 | { |
| 5653 | if( data_size >= 8 ) |
| 5654 | mbedtls_des_key_set_parity( data ); |
| 5655 | if( data_size >= 16 ) |
| 5656 | mbedtls_des_key_set_parity( data + 8 ); |
| 5657 | if( data_size >= 24 ) |
| 5658 | mbedtls_des_key_set_parity( data + 16 ); |
| 5659 | } |
| 5660 | #endif /* MBEDTLS_DES_C */ |
| 5661 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5662 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5663 | psa_key_slot_t *slot, |
| 5664 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5665 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5666 | { |
| 5667 | uint8_t *data = NULL; |
| 5668 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5669 | psa_status_t status; |
| 5670 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5671 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5672 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5673 | if( bits % 8 != 0 ) |
| 5674 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5675 | data = mbedtls_calloc( 1, bytes ); |
| 5676 | if( data == NULL ) |
| 5677 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5678 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5679 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5680 | if( status != PSA_SUCCESS ) |
| 5681 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5682 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5683 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5684 | psa_des_set_key_parity( data, bytes ); |
| 5685 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5686 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5687 | |
| 5688 | exit: |
| 5689 | mbedtls_free( data ); |
| 5690 | return( status ); |
| 5691 | } |
| 5692 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5693 | 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] | 5694 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5695 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5696 | { |
| 5697 | psa_status_t status; |
| 5698 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5699 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5700 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5701 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5702 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5703 | /* Reject any attempt to create a zero-length key so that we don't |
| 5704 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5705 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5706 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5707 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5708 | if( ! operation->can_output_key ) |
| 5709 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5710 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5711 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5712 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5713 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5714 | if( driver != NULL ) |
| 5715 | { |
| 5716 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5717 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5718 | } |
| 5719 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5720 | if( status == PSA_SUCCESS ) |
| 5721 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5722 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5723 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5724 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5725 | } |
| 5726 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5727 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5728 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5729 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5730 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5731 | return( status ); |
| 5732 | } |
| 5733 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5734 | |
| 5735 | |
| 5736 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5737 | /* Key derivation */ |
| 5738 | /****************************************************************/ |
| 5739 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5740 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5741 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5742 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5743 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5744 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5745 | int is_kdf_alg_supported; |
| 5746 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5747 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5748 | * initialisers for this union leave some bytes unspecified.) */ |
| 5749 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5750 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5751 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5752 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5753 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5754 | is_kdf_alg_supported = 1; |
| 5755 | else |
| 5756 | #endif |
| 5757 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5758 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5759 | is_kdf_alg_supported = 1; |
| 5760 | else |
| 5761 | #endif |
| 5762 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5763 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5764 | is_kdf_alg_supported = 1; |
| 5765 | else |
| 5766 | #endif |
| 5767 | is_kdf_alg_supported = 0; |
| 5768 | |
| 5769 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5770 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5771 | 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] | 5772 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5773 | if( hash_size == 0 ) |
| 5774 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5775 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5776 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5777 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5778 | { |
| 5779 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5780 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5781 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5782 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5783 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5784 | |
| 5785 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5786 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5787 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5788 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5789 | 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] | 5790 | psa_algorithm_t alg ) |
| 5791 | { |
| 5792 | psa_status_t status; |
| 5793 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5794 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5795 | return( PSA_ERROR_BAD_STATE ); |
| 5796 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5797 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5798 | return( PSA_ERROR_INVALID_ARGUMENT ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5799 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5800 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5801 | { |
| 5802 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5803 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5804 | } |
| 5805 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5806 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5807 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5808 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5809 | #endif |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5810 | else |
| 5811 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5812 | |
| 5813 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5814 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5815 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5816 | } |
| 5817 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5818 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5819 | 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] | 5820 | psa_algorithm_t hash_alg, |
| 5821 | psa_key_derivation_step_t step, |
| 5822 | const uint8_t *data, |
| 5823 | size_t data_length ) |
| 5824 | { |
| 5825 | psa_status_t status; |
| 5826 | switch( step ) |
| 5827 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5828 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5829 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5830 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5831 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5832 | data, data_length, |
| 5833 | hash_alg ); |
| 5834 | if( status != PSA_SUCCESS ) |
| 5835 | return( status ); |
| 5836 | hkdf->state = HKDF_STATE_STARTED; |
| 5837 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5838 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5839 | /* If no salt was provided, use an empty salt. */ |
| 5840 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5841 | { |
| 5842 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5843 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5844 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5845 | if( status != PSA_SUCCESS ) |
| 5846 | return( status ); |
| 5847 | hkdf->state = HKDF_STATE_STARTED; |
| 5848 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5849 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5850 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5851 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5852 | data, data_length ); |
| 5853 | if( status != PSA_SUCCESS ) |
| 5854 | return( status ); |
| 5855 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5856 | hkdf->prk, |
| 5857 | sizeof( hkdf->prk ) ); |
| 5858 | if( status != PSA_SUCCESS ) |
| 5859 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5860 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5861 | hkdf->block_number = 0; |
| 5862 | hkdf->state = HKDF_STATE_KEYED; |
| 5863 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5864 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5865 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5866 | return( PSA_ERROR_BAD_STATE ); |
| 5867 | if( hkdf->info_set ) |
| 5868 | return( PSA_ERROR_BAD_STATE ); |
| 5869 | hkdf->info_length = data_length; |
| 5870 | if( data_length != 0 ) |
| 5871 | { |
| 5872 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5873 | if( hkdf->info == NULL ) |
| 5874 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5875 | memcpy( hkdf->info, data, data_length ); |
| 5876 | } |
| 5877 | hkdf->info_set = 1; |
| 5878 | return( PSA_SUCCESS ); |
| 5879 | default: |
| 5880 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5881 | } |
| 5882 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5883 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5884 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5885 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5886 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5887 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5888 | const uint8_t *data, |
| 5889 | size_t data_length ) |
| 5890 | { |
| 5891 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5892 | return( PSA_ERROR_BAD_STATE ); |
| 5893 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5894 | if( data_length != 0 ) |
| 5895 | { |
| 5896 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5897 | if( prf->seed == NULL ) |
| 5898 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5899 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5900 | memcpy( prf->seed, data, data_length ); |
| 5901 | prf->seed_length = data_length; |
| 5902 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5903 | |
| 5904 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5905 | |
| 5906 | return( PSA_SUCCESS ); |
| 5907 | } |
| 5908 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5909 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5910 | psa_algorithm_t hash_alg, |
| 5911 | const uint8_t *data, |
| 5912 | size_t data_length ) |
| 5913 | { |
| 5914 | psa_status_t status; |
| 5915 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5916 | return( PSA_ERROR_BAD_STATE ); |
| 5917 | |
| 5918 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5919 | if( status != PSA_SUCCESS ) |
| 5920 | return( status ); |
| 5921 | |
| 5922 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5923 | |
| 5924 | return( PSA_SUCCESS ); |
| 5925 | } |
| 5926 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5927 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5928 | const uint8_t *data, |
| 5929 | size_t data_length ) |
| 5930 | { |
| 5931 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5932 | return( PSA_ERROR_BAD_STATE ); |
| 5933 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5934 | if( data_length != 0 ) |
| 5935 | { |
| 5936 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5937 | if( prf->label == NULL ) |
| 5938 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5939 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5940 | memcpy( prf->label, data, data_length ); |
| 5941 | prf->label_length = data_length; |
| 5942 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5943 | |
| 5944 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5945 | |
| 5946 | return( PSA_SUCCESS ); |
| 5947 | } |
| 5948 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5949 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5950 | psa_algorithm_t hash_alg, |
| 5951 | psa_key_derivation_step_t step, |
| 5952 | const uint8_t *data, |
| 5953 | size_t data_length ) |
| 5954 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5955 | switch( step ) |
| 5956 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5957 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5958 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5959 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5960 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5961 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5962 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5963 | default: |
| 5964 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5965 | } |
| 5966 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5967 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5968 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5969 | |
| 5970 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5971 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5972 | psa_tls12_prf_key_derivation_t *prf, |
| 5973 | psa_algorithm_t hash_alg, |
| 5974 | const uint8_t *data, |
| 5975 | size_t data_length ) |
| 5976 | { |
| 5977 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5978 | 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] | 5979 | uint8_t *cur = pms; |
| 5980 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5981 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5982 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5983 | |
| 5984 | /* Quoting RFC 4279, Section 2: |
| 5985 | * |
| 5986 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5987 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5988 | * uint16 with the value N, and the PSK itself. |
| 5989 | */ |
| 5990 | |
| 5991 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5992 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5993 | memset( cur, 0, data_length ); |
| 5994 | cur += data_length; |
| 5995 | *cur++ = pms[0]; |
| 5996 | *cur++ = pms[1]; |
| 5997 | memcpy( cur, data, data_length ); |
| 5998 | cur += data_length; |
| 5999 | |
| 6000 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 6001 | |
| 6002 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 6003 | return( status ); |
| 6004 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6005 | |
| 6006 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6007 | psa_tls12_prf_key_derivation_t *prf, |
| 6008 | psa_algorithm_t hash_alg, |
| 6009 | psa_key_derivation_step_t step, |
| 6010 | const uint8_t *data, |
| 6011 | size_t data_length ) |
| 6012 | { |
| 6013 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 6014 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6015 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 6016 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 6017 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6018 | |
| 6019 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 6020 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6021 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6022 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6023 | /** Check whether the given key type is acceptable for the given |
| 6024 | * input step of a key derivation. |
| 6025 | * |
| 6026 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6027 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6028 | * Both secret and non-secret inputs can alternatively have the type |
| 6029 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6030 | * that the input was passed as a buffer rather than via a key object. |
| 6031 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6032 | static int psa_key_derivation_check_input_type( |
| 6033 | psa_key_derivation_step_t step, |
| 6034 | psa_key_type_t key_type ) |
| 6035 | { |
| 6036 | switch( step ) |
| 6037 | { |
| 6038 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6039 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 6040 | return( PSA_SUCCESS ); |
| 6041 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6042 | return( PSA_SUCCESS ); |
| 6043 | break; |
| 6044 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6045 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6046 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6047 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6048 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 6049 | return( PSA_SUCCESS ); |
| 6050 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6051 | return( PSA_SUCCESS ); |
| 6052 | break; |
| 6053 | } |
| 6054 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6055 | } |
| 6056 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6057 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6058 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6059 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6060 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6061 | const uint8_t *data, |
| 6062 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6063 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6064 | psa_status_t status; |
| 6065 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 6066 | |
| 6067 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6068 | if( status != PSA_SUCCESS ) |
| 6069 | goto exit; |
| 6070 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 6071 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6072 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6073 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6074 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6075 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6076 | step, data, data_length ); |
| 6077 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6078 | else |
| 6079 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 6080 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 6081 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6082 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6083 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 6084 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6085 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6086 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6087 | else |
| 6088 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6089 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6090 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6091 | { |
| 6092 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 6093 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6094 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6095 | } |
| 6096 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6097 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6098 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6099 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6100 | return( PSA_ERROR_BAD_STATE ); |
| 6101 | } |
| 6102 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6103 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6104 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6105 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6106 | return( status ); |
| 6107 | } |
| 6108 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6109 | psa_status_t psa_key_derivation_input_bytes( |
| 6110 | psa_key_derivation_operation_t *operation, |
| 6111 | psa_key_derivation_step_t step, |
| 6112 | const uint8_t *data, |
| 6113 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6114 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6115 | return( psa_key_derivation_input_internal( operation, step, |
| 6116 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 6117 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6118 | } |
| 6119 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6120 | psa_status_t psa_key_derivation_input_key( |
| 6121 | psa_key_derivation_operation_t *operation, |
| 6122 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6123 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6124 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6125 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6126 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6127 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6128 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6129 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6130 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6131 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6132 | { |
| 6133 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6134 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6135 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6136 | |
| 6137 | /* Passing a key object as a SECRET input unlocks the permission |
| 6138 | * to output to a key object. */ |
| 6139 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6140 | operation->can_output_key = 1; |
| 6141 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6142 | status = psa_key_derivation_input_internal( operation, |
| 6143 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6144 | slot->key.data, |
| 6145 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6146 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6147 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6148 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6149 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6150 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6151 | |
| 6152 | |
| 6153 | |
| 6154 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6155 | /* Key agreement */ |
| 6156 | /****************************************************************/ |
| 6157 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6158 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6159 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 6160 | size_t peer_key_length, |
| 6161 | const mbedtls_ecp_keypair *our_key, |
| 6162 | uint8_t *shared_secret, |
| 6163 | size_t shared_secret_size, |
| 6164 | size_t *shared_secret_length ) |
| 6165 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 6166 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6167 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6168 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6169 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6170 | 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] | 6171 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6172 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6173 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 6174 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6175 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6176 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6177 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6178 | goto exit; |
| 6179 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6180 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6181 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6182 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6183 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6184 | status = mbedtls_to_psa_error( |
| 6185 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 6186 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6187 | goto exit; |
| 6188 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6189 | status = mbedtls_to_psa_error( |
| 6190 | mbedtls_ecdh_calc_secret( &ecdh, |
| 6191 | shared_secret_length, |
| 6192 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6193 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6194 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6195 | if( status != PSA_SUCCESS ) |
| 6196 | goto exit; |
| 6197 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 6198 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6199 | |
| 6200 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 6201 | if( status != PSA_SUCCESS ) |
| 6202 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6203 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6204 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6205 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6206 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6207 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6208 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6209 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6210 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6211 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6212 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6213 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6214 | psa_key_slot_t *private_key, |
| 6215 | const uint8_t *peer_key, |
| 6216 | size_t peer_key_length, |
| 6217 | uint8_t *shared_secret, |
| 6218 | size_t shared_secret_size, |
| 6219 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6220 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6221 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6222 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6224 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6225 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6226 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6227 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6228 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6229 | private_key->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6230 | private_key->key.data, |
| 6231 | private_key->key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6232 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6233 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6234 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6235 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6236 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6237 | shared_secret, shared_secret_size, |
| 6238 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6239 | mbedtls_ecp_keypair_free( ecp ); |
| 6240 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6241 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6242 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6243 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6244 | (void) private_key; |
| 6245 | (void) peer_key; |
| 6246 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6247 | (void) shared_secret; |
| 6248 | (void) shared_secret_size; |
| 6249 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6250 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6251 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6252 | } |
| 6253 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6254 | /* 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] | 6255 | * to potentially free embedded data structures and wipe confidential data. |
| 6256 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6257 | 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] | 6258 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6259 | psa_key_slot_t *private_key, |
| 6260 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6261 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6262 | { |
| 6263 | psa_status_t status; |
| 6264 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6265 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6266 | 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] | 6267 | |
| 6268 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6269 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6270 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6271 | private_key, |
| 6272 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6273 | shared_secret, |
| 6274 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6275 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6276 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6277 | goto exit; |
| 6278 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6279 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6280 | * the shared secret. A shared secret is permitted wherever a key |
| 6281 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6282 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6283 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6284 | shared_secret, |
| 6285 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6286 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6287 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6288 | return( status ); |
| 6289 | } |
| 6290 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6291 | 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] | 6292 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6293 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6294 | const uint8_t *peer_key, |
| 6295 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6296 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6297 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6298 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6299 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6300 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6301 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6302 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6303 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6304 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6305 | if( status != PSA_SUCCESS ) |
| 6306 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6307 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6308 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6309 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6310 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6311 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6312 | else |
| 6313 | { |
| 6314 | /* If a private key has been added as SECRET, we allow the derived |
| 6315 | * key material to be used as a key in PSA Crypto. */ |
| 6316 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6317 | operation->can_output_key = 1; |
| 6318 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6319 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6320 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6321 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6322 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6323 | } |
| 6324 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6325 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6326 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6327 | const uint8_t *peer_key, |
| 6328 | size_t peer_key_length, |
| 6329 | uint8_t *output, |
| 6330 | size_t output_size, |
| 6331 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6332 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6333 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6334 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6335 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6336 | |
| 6337 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6338 | { |
| 6339 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6340 | goto exit; |
| 6341 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6342 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6343 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6344 | if( status != PSA_SUCCESS ) |
| 6345 | goto exit; |
| 6346 | |
| 6347 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6348 | peer_key, peer_key_length, |
| 6349 | output, output_size, |
| 6350 | output_length ); |
| 6351 | |
| 6352 | exit: |
| 6353 | if( status != PSA_SUCCESS ) |
| 6354 | { |
| 6355 | /* If an error happens and is not handled properly, the output |
| 6356 | * may be used as a key to protect sensitive data. Arrange for such |
| 6357 | * a key to be random, which is likely to result in decryption or |
| 6358 | * verification errors. This is better than filling the buffer with |
| 6359 | * some constant data such as zeros, which would result in the data |
| 6360 | * being protected with a reproducible, easily knowable key. |
| 6361 | */ |
| 6362 | psa_generate_random( output, output_size ); |
| 6363 | *output_length = output_size; |
| 6364 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6365 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6366 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6367 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6368 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6369 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6370 | |
| 6371 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6372 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6373 | /****************************************************************/ |
| 6374 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6375 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6376 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6377 | /** Initialize the PSA random generator. |
| 6378 | */ |
| 6379 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6380 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6381 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6382 | memset( rng, 0, sizeof( *rng ) ); |
| 6383 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6384 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6385 | /* Set default configuration if |
| 6386 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6387 | if( rng->entropy_init == NULL ) |
| 6388 | rng->entropy_init = mbedtls_entropy_init; |
| 6389 | if( rng->entropy_free == NULL ) |
| 6390 | rng->entropy_free = mbedtls_entropy_free; |
| 6391 | |
| 6392 | rng->entropy_init( &rng->entropy ); |
| 6393 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6394 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6395 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6396 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6397 | mbedtls_entropy_add_source( &rng->entropy, |
| 6398 | mbedtls_nv_seed_poll, NULL, |
| 6399 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6400 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6401 | #endif |
| 6402 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6403 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6404 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | /** Deinitialize the PSA random generator. |
| 6408 | */ |
| 6409 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6410 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6411 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6412 | memset( rng, 0, sizeof( *rng ) ); |
| 6413 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6414 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6415 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6416 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | /** Seed the PSA random generator. |
| 6420 | */ |
| 6421 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6422 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6423 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6424 | /* Do nothing: the external RNG seeds itself. */ |
| 6425 | (void) rng; |
| 6426 | return( PSA_SUCCESS ); |
| 6427 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6428 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6429 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6430 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6431 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6432 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6433 | } |
| 6434 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6435 | psa_status_t psa_generate_random( uint8_t *output, |
| 6436 | size_t output_size ) |
| 6437 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6438 | GUARD_MODULE_INITIALIZED; |
| 6439 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6440 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6441 | |
| 6442 | size_t output_length = 0; |
| 6443 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6444 | output, output_size, |
| 6445 | &output_length ); |
| 6446 | if( status != PSA_SUCCESS ) |
| 6447 | return( status ); |
| 6448 | /* Breaking up a request into smaller chunks is currently not supported |
| 6449 | * for the extrernal RNG interface. */ |
| 6450 | if( output_length != output_size ) |
| 6451 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6452 | return( PSA_SUCCESS ); |
| 6453 | |
| 6454 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6455 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6456 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6457 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6458 | size_t request_size = |
| 6459 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6460 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6461 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6462 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6463 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6464 | if( ret != 0 ) |
| 6465 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6466 | output_size -= request_size; |
| 6467 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6468 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6469 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6470 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6471 | } |
| 6472 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6473 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6474 | * |
| 6475 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6476 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6477 | * PSA API doesn't support passing an explicit state. |
| 6478 | * |
| 6479 | * In the non-external case, psa_generate_random() calls an |
| 6480 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6481 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6482 | * instead of doing this back-and-forth between the PSA API and the |
| 6483 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6484 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6485 | */ |
| 6486 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6487 | int mbedtls_psa_get_random( void *p_rng, |
| 6488 | unsigned char *output, |
| 6489 | size_t output_size ) |
| 6490 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6491 | /* This function takes a pointer to the RNG state because that's what |
| 6492 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6493 | * its own state internally and doesn't let the caller access that state. |
| 6494 | * So we just ignore the state parameter, and in practice we'll pass |
| 6495 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6496 | (void) p_rng; |
| 6497 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6498 | if( status == PSA_SUCCESS ) |
| 6499 | return( 0 ); |
| 6500 | else |
| 6501 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6502 | } |
| 6503 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6504 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6505 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6506 | #include "mbedtls/entropy_poll.h" |
| 6507 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6508 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6509 | size_t seed_size ) |
| 6510 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6511 | if( global_data.initialized ) |
| 6512 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6513 | |
| 6514 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6515 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6516 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6517 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6518 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6519 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6520 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6521 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6522 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6523 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6524 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6525 | size_t domain_parameters_size, |
| 6526 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6527 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6528 | size_t i; |
| 6529 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6530 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6531 | if( domain_parameters_size == 0 ) |
| 6532 | { |
| 6533 | *exponent = 65537; |
| 6534 | return( PSA_SUCCESS ); |
| 6535 | } |
| 6536 | |
| 6537 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6538 | * support values that fit in a 32-bit integer, which is larger than |
| 6539 | * int on just about every platform anyway. */ |
| 6540 | if( domain_parameters_size > sizeof( acc ) ) |
| 6541 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6542 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6543 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6544 | if( acc > INT_MAX ) |
| 6545 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6546 | *exponent = acc; |
| 6547 | return( PSA_SUCCESS ); |
| 6548 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6549 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6550 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6551 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6552 | psa_key_slot_t *slot, size_t bits, |
| 6553 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6554 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6555 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6556 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6557 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6558 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6559 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6560 | if( key_type_is_raw_bytes( type ) ) |
| 6561 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6562 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6563 | |
| 6564 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6565 | if( status != PSA_SUCCESS ) |
| 6566 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6567 | |
| 6568 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6569 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6570 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6571 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6572 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6573 | status = psa_generate_random( slot->key.data, |
| 6574 | slot->key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6575 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6576 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6577 | |
| 6578 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6579 | #if defined(MBEDTLS_DES_C) |
| 6580 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6581 | psa_des_set_key_parity( slot->key.data, |
| 6582 | slot->key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6583 | #endif /* MBEDTLS_DES_C */ |
| 6584 | } |
| 6585 | else |
| 6586 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6587 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6588 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6589 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6590 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6592 | int exponent; |
| 6593 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6594 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6595 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6596 | /* Accept only byte-aligned keys, for the same reasons as |
| 6597 | * in psa_import_rsa_key(). */ |
| 6598 | if( bits % 8 != 0 ) |
| 6599 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6600 | status = psa_read_rsa_exponent( domain_parameters, |
| 6601 | domain_parameters_size, |
| 6602 | &exponent ); |
| 6603 | if( status != PSA_SUCCESS ) |
| 6604 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6605 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6606 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6607 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6608 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6609 | (unsigned int) bits, |
| 6610 | exponent ); |
| 6611 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6612 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6613 | |
| 6614 | /* Make sure to always have an export representation available */ |
| 6615 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6616 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6617 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6618 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6619 | { |
| 6620 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6621 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6622 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6623 | |
| 6624 | status = psa_export_rsa_key( type, |
| 6625 | &rsa, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6626 | slot->key.data, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6627 | bytes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6628 | &slot->key.bytes ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6629 | mbedtls_rsa_free( &rsa ); |
| 6630 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6631 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6632 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6633 | } |
| 6634 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6635 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6636 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6637 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6638 | 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] | 6639 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6640 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6641 | mbedtls_ecp_group_id grp_id = |
| 6642 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6643 | const mbedtls_ecp_curve_info *curve_info = |
| 6644 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6645 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6646 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6647 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6648 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6649 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6650 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6651 | mbedtls_ecp_keypair_init( &ecp ); |
| 6652 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6653 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6654 | MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6655 | if( ret != 0 ) |
| 6656 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6657 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6658 | return( mbedtls_to_psa_error( ret ) ); |
| 6659 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6660 | |
| 6661 | |
| 6662 | /* Make sure to always have an export representation available */ |
| 6663 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6664 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6665 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6666 | { |
| 6667 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6668 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6669 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6670 | |
| 6671 | status = mbedtls_to_psa_error( |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6672 | mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6673 | |
| 6674 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6675 | if( status != PSA_SUCCESS ) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6676 | memset( slot->key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6677 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6678 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6679 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6680 | } |
| 6681 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6682 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6683 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6684 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6685 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6686 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6687 | return( PSA_SUCCESS ); |
| 6688 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6689 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6690 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6691 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6692 | { |
| 6693 | psa_status_t status; |
| 6694 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6695 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6696 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6697 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6698 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6699 | /* Reject any attempt to create a zero-length key so that we don't |
| 6700 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6701 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6702 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6703 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6704 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6705 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6706 | if( status != PSA_SUCCESS ) |
| 6707 | goto exit; |
| 6708 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6709 | status = psa_driver_wrapper_generate_key( attributes, |
| 6710 | slot ); |
| 6711 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6712 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6713 | goto exit; |
| 6714 | |
| 6715 | status = psa_generate_key_internal( |
| 6716 | slot, attributes->core.bits, |
| 6717 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6718 | |
| 6719 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6720 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6721 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6722 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6723 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6724 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6725 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6726 | } |
| 6727 | |
| 6728 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6729 | |
| 6730 | /****************************************************************/ |
| 6731 | /* Module setup */ |
| 6732 | /****************************************************************/ |
| 6733 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6734 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6735 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6736 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6737 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6738 | { |
| 6739 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6740 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6741 | global_data.rng.entropy_init = entropy_init; |
| 6742 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6743 | return( PSA_SUCCESS ); |
| 6744 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6745 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6746 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6747 | void mbedtls_psa_crypto_free( void ) |
| 6748 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6749 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6750 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6751 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6752 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6753 | } |
| 6754 | /* Wipe all remaining data, including configuration. |
| 6755 | * In particular, this sets all state indicator to the value |
| 6756 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6757 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6758 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6759 | /* Unregister all secure element drivers, so that we restart from |
| 6760 | * a pristine state. */ |
| 6761 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6762 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6763 | } |
| 6764 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6765 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6766 | /** Recover a transaction that was interrupted by a power failure. |
| 6767 | * |
| 6768 | * This function is called during initialization, before psa_crypto_init() |
| 6769 | * returns. If this function returns a failure status, the initialization |
| 6770 | * fails. |
| 6771 | */ |
| 6772 | static psa_status_t psa_crypto_recover_transaction( |
| 6773 | const psa_crypto_transaction_t *transaction ) |
| 6774 | { |
| 6775 | switch( transaction->unknown.type ) |
| 6776 | { |
| 6777 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6778 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6779 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6780 | * is implemented. |
| 6781 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6782 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6783 | default: |
| 6784 | /* We found an unsupported transaction in the storage. |
| 6785 | * We don't know what state the storage is in. Give up. */ |
| 6786 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6787 | } |
| 6788 | } |
| 6789 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6790 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6791 | psa_status_t psa_crypto_init( void ) |
| 6792 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6793 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6794 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6795 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6796 | if( global_data.initialized != 0 ) |
| 6797 | return( PSA_SUCCESS ); |
| 6798 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6799 | /* Initialize and seed the random generator. */ |
| 6800 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6801 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6802 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6803 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6804 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6805 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6806 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6807 | status = psa_initialize_key_slots( ); |
| 6808 | if( status != PSA_SUCCESS ) |
| 6809 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6810 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6811 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6812 | status = psa_init_all_se_drivers( ); |
| 6813 | if( status != PSA_SUCCESS ) |
| 6814 | goto exit; |
| 6815 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6816 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6817 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6818 | status = psa_crypto_load_transaction( ); |
| 6819 | if( status == PSA_SUCCESS ) |
| 6820 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6821 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6822 | if( status != PSA_SUCCESS ) |
| 6823 | goto exit; |
| 6824 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6825 | } |
| 6826 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6827 | { |
| 6828 | /* There's no transaction to complete. It's all good. */ |
| 6829 | status = PSA_SUCCESS; |
| 6830 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6831 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6832 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6833 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6834 | global_data.initialized = 1; |
| 6835 | |
| 6836 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6837 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6838 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6839 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |