Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 29 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | #include "psa/crypto.h" |
| 31 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 33 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 35 | #include "psa_crypto_ecp.h" |
| 36 | #include "psa_crypto_rsa.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 38 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 39 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 40 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 41 | /* Include internal declarations that are useful for implementing persistently |
| 42 | * stored keys. */ |
| 43 | #include "psa_crypto_storage.h" |
| 44 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 45 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 46 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 47 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 51 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 52 | #define mbedtls_calloc calloc |
| 53 | #define mbedtls_free free |
| 54 | #endif |
| 55 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 56 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 57 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 58 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 59 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 60 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 61 | #include "mbedtls/blowfish.h" |
| 62 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 63 | #include "mbedtls/chacha20.h" |
| 64 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 65 | #include "mbedtls/cipher.h" |
| 66 | #include "mbedtls/ccm.h" |
| 67 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 69 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 70 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 71 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 72 | #include "mbedtls/error.h" |
| 73 | #include "mbedtls/gcm.h" |
| 74 | #include "mbedtls/md2.h" |
| 75 | #include "mbedtls/md4.h" |
| 76 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 77 | #include "mbedtls/md.h" |
| 78 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 79 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 80 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 81 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 82 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 84 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 85 | #include "mbedtls/sha1.h" |
| 86 | #include "mbedtls/sha256.h" |
| 87 | #include "mbedtls/sha512.h" |
| 88 | #include "mbedtls/xtea.h" |
| 89 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 90 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 91 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 92 | /* constant-time buffer comparison */ |
| 93 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 94 | { |
| 95 | size_t i; |
| 96 | unsigned char diff = 0; |
| 97 | |
| 98 | for( i = 0; i < n; i++ ) |
| 99 | diff |= a[i] ^ b[i]; |
| 100 | |
| 101 | return( diff ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 106 | /****************************************************************/ |
| 107 | /* Global data, support functions and library management */ |
| 108 | /****************************************************************/ |
| 109 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 110 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 111 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 112 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 115 | /* Values for psa_global_data_t::rng_state */ |
| 116 | #define RNG_NOT_INITIALIZED 0 |
| 117 | #define RNG_INITIALIZED 1 |
| 118 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 119 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 120 | typedef struct |
| 121 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 122 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 123 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 124 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 125 | } psa_global_data_t; |
| 126 | |
| 127 | static psa_global_data_t global_data; |
| 128 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 129 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 130 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 131 | &global_data.rng.drbg; |
| 132 | #endif |
| 133 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 134 | #define GUARD_MODULE_INITIALIZED \ |
| 135 | if( global_data.initialized == 0 ) \ |
| 136 | return( PSA_ERROR_BAD_STATE ); |
| 137 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 138 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 139 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 140 | /* Mbed TLS error codes can combine a high-level error code and a |
| 141 | * low-level error code. The low-level error usually reflects the |
| 142 | * root cause better, so dispatch on that preferably. */ |
| 143 | int low_level_ret = - ( -ret & 0x007f ); |
| 144 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 145 | { |
| 146 | case 0: |
| 147 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 148 | |
| 149 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 150 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 151 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 152 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 153 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 154 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 155 | |
| 156 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 157 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 158 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 159 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 160 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 161 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 162 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 163 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 164 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 165 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 166 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 167 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 168 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 169 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 171 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 173 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 174 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 175 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 176 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 177 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 178 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 179 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 180 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 181 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 182 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 183 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 184 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 185 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 186 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 187 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 188 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 189 | |
| 190 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 191 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 192 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 193 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 194 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 195 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 196 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 197 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 198 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 199 | |
| 200 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 201 | return( PSA_ERROR_BAD_STATE ); |
| 202 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 203 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 204 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 205 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 206 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 207 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 208 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 209 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 210 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 211 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 212 | return( PSA_ERROR_INVALID_PADDING ); |
| 213 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 214 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 215 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 216 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 217 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 218 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 219 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 220 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 221 | |
| 222 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 223 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 224 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 225 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 226 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 227 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 228 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 230 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 231 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 232 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 233 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 234 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 235 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 236 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | |
| 238 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 239 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 240 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 241 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 242 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 243 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 244 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 245 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 246 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 247 | |
| 248 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 249 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 250 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 252 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 253 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 254 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 255 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 256 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 257 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 258 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 259 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 260 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 261 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 262 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 263 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 264 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 265 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 266 | #endif |
| 267 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 268 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 269 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 270 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 271 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 272 | |
| 273 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 274 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 275 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 277 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 278 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 279 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 280 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 281 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 282 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 283 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 284 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 285 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 286 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 288 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 290 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 291 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 292 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 294 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 295 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 296 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 297 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 298 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 299 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 300 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 301 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 302 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 303 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 304 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 305 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 306 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 307 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 308 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 309 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 310 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 311 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 312 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 313 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 314 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 315 | return( PSA_ERROR_NOT_PERMITTED ); |
| 316 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 317 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 318 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 319 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 320 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 321 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 322 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 323 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 324 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 325 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 326 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 327 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 328 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 329 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 330 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 331 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 332 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 333 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 334 | |
| 335 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 337 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 338 | return( PSA_ERROR_INVALID_PADDING ); |
| 339 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 340 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 341 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 342 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 343 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 344 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 345 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 346 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 347 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 348 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 349 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 350 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 351 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 352 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 353 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 354 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 355 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 356 | |
| 357 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 358 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 359 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 360 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 361 | |
| 362 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 363 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 364 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 365 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 366 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 367 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 368 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 369 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 370 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 371 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 372 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 373 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 374 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 375 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 376 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 377 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 378 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 379 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 380 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 381 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 382 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 383 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 384 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 385 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 386 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 387 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 388 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 392 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 393 | |
| 394 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 395 | /****************************************************************/ |
| 396 | /* Key management */ |
| 397 | /****************************************************************/ |
| 398 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 399 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 400 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 401 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 402 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 403 | } |
| 404 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 405 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 406 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 407 | * current test driver in key_management.c is using this function |
| 408 | * when accelerators are used for ECC key pair and public key. |
| 409 | * Once that dependency is resolved these guards can be removed. |
| 410 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 411 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 412 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 413 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 414 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 415 | 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] | 416 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 417 | { |
| 418 | switch( curve ) |
| 419 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 420 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 421 | switch( byte_length ) |
| 422 | { |
| 423 | case PSA_BITS_TO_BYTES( 192 ): |
| 424 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 425 | case PSA_BITS_TO_BYTES( 224 ): |
| 426 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 427 | case PSA_BITS_TO_BYTES( 256 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 429 | case PSA_BITS_TO_BYTES( 384 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 431 | case PSA_BITS_TO_BYTES( 521 ): |
| 432 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 433 | default: |
| 434 | return( MBEDTLS_ECP_DP_NONE ); |
| 435 | } |
| 436 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 438 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 439 | switch( byte_length ) |
| 440 | { |
| 441 | case PSA_BITS_TO_BYTES( 256 ): |
| 442 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 443 | case PSA_BITS_TO_BYTES( 384 ): |
| 444 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 445 | case PSA_BITS_TO_BYTES( 512 ): |
| 446 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 447 | default: |
| 448 | return( MBEDTLS_ECP_DP_NONE ); |
| 449 | } |
| 450 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 451 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 452 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 453 | switch( byte_length ) |
| 454 | { |
| 455 | case PSA_BITS_TO_BYTES( 255 ): |
| 456 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 457 | case PSA_BITS_TO_BYTES( 448 ): |
| 458 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 459 | default: |
| 460 | return( MBEDTLS_ECP_DP_NONE ); |
| 461 | } |
| 462 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 463 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 464 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 465 | switch( byte_length ) |
| 466 | { |
| 467 | case PSA_BITS_TO_BYTES( 192 ): |
| 468 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 469 | case PSA_BITS_TO_BYTES( 224 ): |
| 470 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 471 | case PSA_BITS_TO_BYTES( 256 ): |
| 472 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 473 | default: |
| 474 | return( MBEDTLS_ECP_DP_NONE ); |
| 475 | } |
| 476 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 477 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 478 | default: |
| 479 | return( MBEDTLS_ECP_DP_NONE ); |
| 480 | } |
| 481 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 482 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 483 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 484 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 485 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 486 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 487 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 488 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 489 | { |
| 490 | /* Check that the bit size is acceptable for the key type */ |
| 491 | switch( type ) |
| 492 | { |
| 493 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 494 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 495 | case PSA_KEY_TYPE_DERIVE: |
| 496 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 497 | #if defined(MBEDTLS_AES_C) |
| 498 | case PSA_KEY_TYPE_AES: |
| 499 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 500 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 501 | break; |
| 502 | #endif |
| 503 | #if defined(MBEDTLS_CAMELLIA_C) |
| 504 | case PSA_KEY_TYPE_CAMELLIA: |
| 505 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 506 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 507 | break; |
| 508 | #endif |
| 509 | #if defined(MBEDTLS_DES_C) |
| 510 | case PSA_KEY_TYPE_DES: |
| 511 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 512 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 513 | break; |
| 514 | #endif |
| 515 | #if defined(MBEDTLS_ARC4_C) |
| 516 | case PSA_KEY_TYPE_ARC4: |
| 517 | if( bits < 8 || bits > 2048 ) |
| 518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 519 | break; |
| 520 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 521 | #if defined(MBEDTLS_CHACHA20_C) |
| 522 | case PSA_KEY_TYPE_CHACHA20: |
| 523 | if( bits != 256 ) |
| 524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 525 | break; |
| 526 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 527 | default: |
| 528 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 529 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 530 | if( bits % 8 != 0 ) |
| 531 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 532 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 533 | return( PSA_SUCCESS ); |
| 534 | } |
| 535 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 536 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 537 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 538 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 539 | /** Export an RSA key to export representation |
| 540 | * |
| 541 | * \param[in] type The type of key (public/private) to export |
| 542 | * \param[in] rsa The internal RSA representation from which to export |
| 543 | * \param[out] data The buffer to export to |
| 544 | * \param[in] data_size The length of the buffer to export to |
| 545 | * \param[out] data_length The amount of bytes written to \p data |
| 546 | */ |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 547 | static psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type, |
| 548 | mbedtls_rsa_context *rsa, |
| 549 | uint8_t *data, |
| 550 | size_t data_size, |
| 551 | size_t *data_length ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 552 | { |
| 553 | #if defined(MBEDTLS_PK_WRITE_C) |
| 554 | int ret; |
| 555 | mbedtls_pk_context pk; |
| 556 | uint8_t *pos = data + data_size; |
| 557 | |
| 558 | mbedtls_pk_init( &pk ); |
| 559 | pk.pk_info = &mbedtls_rsa_info; |
| 560 | pk.pk_ctx = rsa; |
| 561 | |
| 562 | /* 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] | 563 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 564 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 565 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 566 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 567 | else |
| 568 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 569 | |
| 570 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 571 | { |
| 572 | /* Clean up in case pk_write failed halfway through. */ |
| 573 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 574 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 575 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 576 | |
| 577 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 578 | * Move the data to the beginning and erase remaining data |
| 579 | * at the original location. */ |
| 580 | if( 2 * (size_t) ret <= data_size ) |
| 581 | { |
| 582 | memcpy( data, data + data_size - ret, ret ); |
| 583 | memset( data + data_size - ret, 0, ret ); |
| 584 | } |
| 585 | else if( (size_t) ret < data_size ) |
| 586 | { |
| 587 | memmove( data, data + data_size - ret, ret ); |
| 588 | memset( data + ret, 0, data_size - ret ); |
| 589 | } |
| 590 | |
| 591 | *data_length = ret; |
| 592 | return( PSA_SUCCESS ); |
| 593 | #else |
| 594 | (void) type; |
| 595 | (void) rsa; |
| 596 | (void) data; |
| 597 | (void) data_size; |
| 598 | (void) data_length; |
| 599 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 600 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 601 | } |
| 602 | |
| 603 | /** Import an RSA key from import representation to a slot |
| 604 | * |
| 605 | * \param[in,out] slot The slot where to store the export representation to |
| 606 | * \param[in] data The buffer containing the import representation |
| 607 | * \param[in] data_length The amount of bytes in \p data |
| 608 | */ |
| 609 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 610 | const uint8_t *data, |
| 611 | size_t data_length ) |
| 612 | { |
| 613 | psa_status_t status; |
| 614 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 615 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 616 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 617 | /* Parse input */ |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 618 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 619 | data, |
| 620 | data_length, |
| 621 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 622 | if( status != PSA_SUCCESS ) |
| 623 | goto exit; |
| 624 | |
| 625 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 626 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 627 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 628 | /* Re-export the data to PSA export format, such that we can store export |
| 629 | * representation in the key slot. Export representation in case of RSA is |
| 630 | * the smallest representation that's allowed as input, so a straight-up |
| 631 | * 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] | 632 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 633 | if( output == NULL ) |
| 634 | { |
| 635 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 636 | goto exit; |
| 637 | } |
| 638 | |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 639 | status = mbedtls_psa_rsa_export_key( slot->attr.type, |
| 640 | rsa, |
| 641 | output, |
| 642 | data_length, |
| 643 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 644 | exit: |
| 645 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 646 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 647 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 648 | |
| 649 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 650 | if( status != PSA_SUCCESS ) |
| 651 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 652 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 653 | return( status ); |
| 654 | } |
| 655 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 656 | /* On success, store the allocated export-formatted key. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 657 | slot->key.data = output; |
| 658 | slot->key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 659 | |
| 660 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 661 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 662 | |
| 663 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 664 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 665 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 666 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 667 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 668 | /** Export an ECP key to export representation |
| 669 | * |
| 670 | * \param[in] type The type of key (public/private) to export |
| 671 | * \param[in] ecp The internal ECP representation from which to export |
| 672 | * \param[out] data The buffer to export to |
| 673 | * \param[in] data_size The length of the buffer to export to |
| 674 | * \param[out] data_length The amount of bytes written to \p data |
| 675 | */ |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 676 | static psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type, |
| 677 | mbedtls_ecp_keypair *ecp, |
| 678 | uint8_t *data, |
| 679 | size_t data_size, |
| 680 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 681 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 682 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 683 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 684 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 685 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 686 | /* Check whether the public part is loaded */ |
| 687 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 688 | { |
| 689 | /* Calculate the public key */ |
| 690 | status = mbedtls_to_psa_error( |
| 691 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 692 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 693 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 694 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 697 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 698 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 699 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 700 | data_length, |
| 701 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 702 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 703 | if( status != PSA_SUCCESS ) |
| 704 | memset( data, 0, data_size ); |
| 705 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 706 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 707 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 708 | else |
| 709 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 710 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 711 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 712 | |
| 713 | status = mbedtls_to_psa_error( |
| 714 | mbedtls_ecp_write_key( ecp, |
| 715 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 716 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 717 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 718 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 719 | else |
| 720 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 721 | |
| 722 | return( status ); |
| 723 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 724 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 725 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 726 | /** Import an ECP key from import representation to a slot |
| 727 | * |
| 728 | * \param[in,out] slot The slot where to store the export representation to |
| 729 | * \param[in] data The buffer containing the import representation |
| 730 | * \param[in] data_length The amount of bytes in \p data |
| 731 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 732 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 733 | const uint8_t *data, |
| 734 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 735 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 736 | psa_status_t status; |
| 737 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 738 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 739 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 740 | /* Parse input */ |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 741 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
| 742 | data, |
| 743 | data_length, |
| 744 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 745 | if( status != PSA_SUCCESS ) |
| 746 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 747 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 748 | 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] | 749 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 750 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 751 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 752 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 753 | /* Re-export the data to PSA export format. There is currently no support |
| 754 | * for other input formats then the export format, so this is a 1-1 |
| 755 | * copy operation. */ |
| 756 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 757 | if( output == NULL ) |
| 758 | { |
| 759 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 760 | goto exit; |
| 761 | } |
| 762 | |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 763 | status = mbedtls_psa_ecp_export_key( slot->attr.type, |
| 764 | ecp, |
| 765 | output, |
| 766 | data_length, |
| 767 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 768 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 769 | /* Always free the PK object (will also free contained ECP context) */ |
| 770 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 771 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 772 | |
| 773 | /* Free the allocated buffer only on error. */ |
| 774 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 775 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 776 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 777 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 778 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 779 | |
| 780 | /* On success, store the allocated export-formatted key. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 781 | slot->key.data = output; |
| 782 | slot->key.bytes = data_length; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 783 | |
| 784 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 785 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 786 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 787 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 788 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 789 | /** Return the size of the key in the given slot, in bits. |
| 790 | * |
| 791 | * \param[in] slot A key slot. |
| 792 | * |
| 793 | * \return The key size in bits, read from the metadata in the slot. |
| 794 | */ |
| 795 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 796 | { |
| 797 | return( slot->attr.bits ); |
| 798 | } |
| 799 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 800 | /** Try to allocate a buffer to an empty key slot. |
| 801 | * |
| 802 | * \param[in,out] slot Key slot to attach buffer to. |
| 803 | * \param[in] buffer_length Requested size of the buffer. |
| 804 | * |
| 805 | * \retval #PSA_SUCCESS |
| 806 | * The buffer has been successfully allocated. |
| 807 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 808 | * Not enough memory was available for allocation. |
| 809 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 810 | * Trying to allocate a buffer to a non-empty key slot. |
| 811 | */ |
| 812 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 813 | size_t buffer_length ) |
| 814 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 815 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 816 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 817 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 818 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 819 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 820 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 821 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 822 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 823 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 826 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 827 | const uint8_t* data, |
| 828 | size_t data_length ) |
| 829 | { |
| 830 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 831 | data_length ); |
| 832 | if( status != PSA_SUCCESS ) |
| 833 | return( status ); |
| 834 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 835 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 836 | return( PSA_SUCCESS ); |
| 837 | } |
| 838 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 839 | /** Import key data into a slot. |
| 840 | * |
| 841 | * `slot->type` must have been set previously. |
| 842 | * This function assumes that the slot does not contain any key material yet. |
| 843 | * On failure, the slot content is unchanged. |
| 844 | * |
| 845 | * Persistent storage is not affected. |
| 846 | * |
| 847 | * \param[in,out] slot The key slot to import data into. |
| 848 | * Its `type` field must have previously been set to |
| 849 | * the desired key type. |
| 850 | * It must not contain any key material yet. |
| 851 | * \param[in] data Buffer containing the key material to parse and import. |
| 852 | * \param data_length Size of \p data in bytes. |
| 853 | * |
| 854 | * \retval #PSA_SUCCESS |
| 855 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 856 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 857 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 858 | */ |
| 859 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 860 | const uint8_t *data, |
| 861 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 862 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 863 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 864 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 865 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 866 | /* zero-length keys are never supported. */ |
| 867 | if( data_length == 0 ) |
| 868 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 869 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 870 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 871 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 872 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 873 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 874 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 875 | if( data_length > SIZE_MAX / 8 ) |
| 876 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 877 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 878 | /* Enforce a size limit, and in particular ensure that the bit |
| 879 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 880 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 881 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 882 | |
| 883 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 884 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 885 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 886 | |
| 887 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 888 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 889 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 890 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 891 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 892 | /* Write the actual key size to the slot. |
| 893 | * psa_start_key_creation() wrote the size declared by the |
| 894 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 895 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 896 | |
| 897 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 898 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 899 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 900 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 901 | /* Try validation through accelerators first. */ |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 902 | psa_key_attributes_t attributes = { |
| 903 | .core = slot->attr |
| 904 | }; |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 905 | |
| 906 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
| 907 | if( status != PSA_SUCCESS ) |
| 908 | return( status ); |
| 909 | |
| 910 | bit_size = slot->attr.bits; |
| 911 | status = psa_driver_wrapper_import_key( &attributes, |
| 912 | data, data_length, |
| 913 | slot->key.data, |
| 914 | slot->key.bytes, |
| 915 | &slot->key.bytes, |
| 916 | &bit_size ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 917 | if( status == PSA_SUCCESS ) |
| 918 | { |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 919 | if( slot->attr.bits == 0 ) |
| 920 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 921 | else if( bit_size != slot->attr.bits ) |
| 922 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 923 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 924 | return( PSA_SUCCESS ); |
| 925 | } |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 926 | else |
| 927 | { |
| 928 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 929 | return( status ); |
| 930 | } |
| 931 | |
| 932 | mbedtls_platform_zeroize( slot->key.data, data_length ); |
| 933 | mbedtls_free( slot->key.data ); |
| 934 | slot->key.data = NULL; |
| 935 | slot->key.bytes = 0; |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 936 | |
| 937 | /* Key format is not supported by any accelerator, try software fallback |
| 938 | * if present. */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 939 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 940 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 941 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 942 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 943 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 944 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 945 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 946 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 947 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 948 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 949 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 950 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 951 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 952 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 953 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 954 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 955 | |
| 956 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 957 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 958 | } |
| 959 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 960 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 961 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 962 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 963 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 966 | /** Calculate the intersection of two algorithm usage policies. |
| 967 | * |
| 968 | * Return 0 (which allows no operation) on incompatibility. |
| 969 | */ |
| 970 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 971 | psa_algorithm_t alg1, |
| 972 | psa_algorithm_t alg2 ) |
| 973 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 974 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 975 | if( alg1 == alg2 ) |
| 976 | return( alg1 ); |
| 977 | /* If the policies are from the same hash-and-sign family, check |
| 978 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 979 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 980 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 981 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 982 | { |
| 983 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 984 | return( alg2 ); |
| 985 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 986 | return( alg1 ); |
| 987 | } |
| 988 | /* If the policies are incompatible, allow nothing. */ |
| 989 | return( 0 ); |
| 990 | } |
| 991 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 992 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 993 | psa_algorithm_t requested_alg ) |
| 994 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 995 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 996 | if( requested_alg == policy_alg ) |
| 997 | return( 1 ); |
| 998 | /* 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] | 999 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1000 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1001 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1002 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1003 | { |
| 1004 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1005 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1006 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1007 | /* 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] | 1008 | * a key derivation with that key agreement should also be allowed. This |
| 1009 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1010 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1011 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1012 | { |
| 1013 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1014 | policy_alg ); |
| 1015 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1016 | /* If it isn't permitted, it's forbidden. */ |
| 1017 | return( 0 ); |
| 1018 | } |
| 1019 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1020 | /** Test whether a policy permits an algorithm. |
| 1021 | * |
| 1022 | * The caller must test usage flags separately. |
| 1023 | */ |
| 1024 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1025 | psa_algorithm_t alg ) |
| 1026 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1027 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1028 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1031 | /** Restrict a key policy based on a constraint. |
| 1032 | * |
| 1033 | * \param[in,out] policy The policy to restrict. |
| 1034 | * \param[in] constraint The policy constraint to apply. |
| 1035 | * |
| 1036 | * \retval #PSA_SUCCESS |
| 1037 | * \c *policy contains the intersection of the original value of |
| 1038 | * \c *policy and \c *constraint. |
| 1039 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1040 | * \c *policy and \c *constraint are incompatible. |
| 1041 | * \c *policy is unchanged. |
| 1042 | */ |
| 1043 | static psa_status_t psa_restrict_key_policy( |
| 1044 | psa_key_policy_t *policy, |
| 1045 | const psa_key_policy_t *constraint ) |
| 1046 | { |
| 1047 | psa_algorithm_t intersection_alg = |
| 1048 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1049 | psa_algorithm_t intersection_alg2 = |
| 1050 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1051 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1052 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1053 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1054 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1055 | policy->usage &= constraint->usage; |
| 1056 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1057 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1058 | return( PSA_SUCCESS ); |
| 1059 | } |
| 1060 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1061 | /** Get the description of a key given its identifier and policy constraints |
| 1062 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1063 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1064 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1065 | * nonzero, the key must allow operations with this algorithm. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1066 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1067 | * In case of a persistent key, the function loads the description of the key |
| 1068 | * into a key slot if not already done. |
| 1069 | * |
| 1070 | * On success, the returned key slot is locked. It is the responsibility of |
| 1071 | * 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] | 1072 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1073 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1074 | mbedtls_svc_key_id_t key, |
| 1075 | psa_key_slot_t **p_slot, |
| 1076 | psa_key_usage_t usage, |
| 1077 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1078 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1079 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1080 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1081 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1082 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1083 | if( status != PSA_SUCCESS ) |
| 1084 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1085 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1086 | |
| 1087 | /* Enforce that usage policy for the key slot contains all the flags |
| 1088 | * required by the usage parameter. There is one exception: public |
| 1089 | * keys can always be exported, so we treat public key objects as |
| 1090 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1091 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1092 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1093 | |
| 1094 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1095 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1096 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1097 | |
| 1098 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1099 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1100 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1101 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1102 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1103 | |
| 1104 | error: |
| 1105 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1106 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1107 | |
| 1108 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1109 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1110 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1111 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1112 | * |
| 1113 | * A transparent key is a key for which the key material is directly |
| 1114 | * available, as opposed to a key in a secure element. |
| 1115 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1116 | * This is a temporary function to use instead of |
| 1117 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1118 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1119 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1120 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1121 | * 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] | 1122 | */ |
| 1123 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1124 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1125 | mbedtls_svc_key_id_t key, |
| 1126 | psa_key_slot_t **p_slot, |
| 1127 | psa_key_usage_t usage, |
| 1128 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1129 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1130 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1131 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1132 | if( status != PSA_SUCCESS ) |
| 1133 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1134 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1135 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1136 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1137 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1138 | *p_slot = NULL; |
| 1139 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1140 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1141 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1142 | return( PSA_SUCCESS ); |
| 1143 | } |
| 1144 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1145 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1146 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1147 | 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] | 1148 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1149 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1150 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1151 | 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] | 1152 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1153 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1154 | * initialized slot, so we can just free it. */ |
| 1155 | if( slot->key.data != NULL ) |
| 1156 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 1157 | |
| 1158 | mbedtls_free( slot->key.data ); |
| 1159 | slot->key.data = NULL; |
| 1160 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1161 | |
| 1162 | return( PSA_SUCCESS ); |
| 1163 | } |
| 1164 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1165 | /** Completely wipe a slot in memory, including its policy. |
| 1166 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1167 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1168 | { |
| 1169 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1170 | |
| 1171 | /* |
| 1172 | * 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] | 1173 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1174 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1175 | * 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] | 1176 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1177 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1178 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1179 | { |
| 1180 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1181 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1182 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1183 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1184 | } |
| 1185 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1186 | /* Multipart operations may still be using the key. This is safe |
| 1187 | * because all multipart operation objects are independent from |
| 1188 | * the key slot: if they need to access the key after the setup |
| 1189 | * phase, they have a copy of the key. Note that this means that |
| 1190 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1191 | /* At this point, key material and other type-specific content has |
| 1192 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1193 | * zeroize because the metadata is not particularly sensitive. */ |
| 1194 | memset( slot, 0, sizeof( *slot ) ); |
| 1195 | return( status ); |
| 1196 | } |
| 1197 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1198 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1199 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1200 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1201 | psa_status_t status; /* status of the last operation */ |
| 1202 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1203 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1204 | psa_se_drv_table_entry_t *driver; |
| 1205 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1206 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1207 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1208 | return( PSA_SUCCESS ); |
| 1209 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1210 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1211 | * 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] | 1212 | * key, this will load the key description from persistent memory if not |
| 1213 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1214 | * the key is operated by an SE or not and this information is needed by |
| 1215 | * the current implementation. |
| 1216 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1217 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1218 | if( status != PSA_SUCCESS ) |
| 1219 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1220 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1221 | /* |
| 1222 | * If the key slot containing the key description is under access by the |
| 1223 | * library (apart from the present access), the key cannot be destroyed |
| 1224 | * yet. For the time being, just return in error. Eventually (to be |
| 1225 | * implemented), the key should be destroyed when all accesses have |
| 1226 | * stopped. |
| 1227 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1228 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1229 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1230 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1231 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1232 | } |
| 1233 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1234 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1235 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1236 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1237 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1238 | /* For a key in a secure element, we need to do three things: |
| 1239 | * remove the key file in internal storage, destroy the |
| 1240 | * key inside the secure element, and update the driver's |
| 1241 | * persistent data. Start a transaction that will encompass these |
| 1242 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1243 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1244 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1245 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1246 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1247 | status = psa_crypto_save_transaction( ); |
| 1248 | if( status != PSA_SUCCESS ) |
| 1249 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1250 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1251 | /* We should still try to destroy the key in the secure |
| 1252 | * element and the key metadata in storage. This is especially |
| 1253 | * important if the error is that the storage is full. |
| 1254 | * But how to do it exactly without risking an inconsistent |
| 1255 | * state after a reset? |
| 1256 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1257 | */ |
| 1258 | overall_status = status; |
| 1259 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1262 | status = psa_destroy_se_key( driver, |
| 1263 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1264 | if( overall_status == PSA_SUCCESS ) |
| 1265 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1266 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1267 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1268 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1269 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1270 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1271 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1272 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1273 | if( overall_status == PSA_SUCCESS ) |
| 1274 | overall_status = status; |
| 1275 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1276 | /* TODO: other slots may have a copy of the same key. We should |
| 1277 | * invalidate them. |
| 1278 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1279 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1280 | } |
| 1281 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1282 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1283 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1284 | if( driver != NULL ) |
| 1285 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1286 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1287 | if( overall_status == PSA_SUCCESS ) |
| 1288 | overall_status = status; |
| 1289 | status = psa_crypto_stop_transaction( ); |
| 1290 | if( overall_status == PSA_SUCCESS ) |
| 1291 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1292 | } |
| 1293 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1294 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1295 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1296 | exit: |
| 1297 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1298 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1299 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1300 | if( overall_status == PSA_SUCCESS ) |
| 1301 | overall_status = status; |
| 1302 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1305 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1306 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1307 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1308 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1311 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1312 | psa_key_type_t type, |
| 1313 | const uint8_t *data, |
| 1314 | size_t data_length ) |
| 1315 | { |
| 1316 | uint8_t *copy = NULL; |
| 1317 | |
| 1318 | if( data_length != 0 ) |
| 1319 | { |
| 1320 | copy = mbedtls_calloc( 1, data_length ); |
| 1321 | if( copy == NULL ) |
| 1322 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1323 | memcpy( copy, data, data_length ); |
| 1324 | } |
| 1325 | /* After this point, this function is guaranteed to succeed, so it |
| 1326 | * can start modifying `*attributes`. */ |
| 1327 | |
| 1328 | if( attributes->domain_parameters != NULL ) |
| 1329 | { |
| 1330 | mbedtls_free( attributes->domain_parameters ); |
| 1331 | attributes->domain_parameters = NULL; |
| 1332 | attributes->domain_parameters_size = 0; |
| 1333 | } |
| 1334 | |
| 1335 | attributes->domain_parameters = copy; |
| 1336 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1337 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1338 | return( PSA_SUCCESS ); |
| 1339 | } |
| 1340 | |
| 1341 | psa_status_t psa_get_key_domain_parameters( |
| 1342 | const psa_key_attributes_t *attributes, |
| 1343 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1344 | { |
| 1345 | if( attributes->domain_parameters_size > data_size ) |
| 1346 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1347 | *data_length = attributes->domain_parameters_size; |
| 1348 | if( attributes->domain_parameters_size != 0 ) |
| 1349 | memcpy( data, attributes->domain_parameters, |
| 1350 | attributes->domain_parameters_size ); |
| 1351 | return( PSA_SUCCESS ); |
| 1352 | } |
| 1353 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1354 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1355 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1356 | static psa_status_t psa_get_rsa_public_exponent( |
| 1357 | const mbedtls_rsa_context *rsa, |
| 1358 | psa_key_attributes_t *attributes ) |
| 1359 | { |
| 1360 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1361 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1362 | uint8_t *buffer = NULL; |
| 1363 | size_t buflen; |
| 1364 | mbedtls_mpi_init( &mpi ); |
| 1365 | |
| 1366 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1367 | if( ret != 0 ) |
| 1368 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1369 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1370 | { |
| 1371 | /* It's the default value, which is reported as an empty string, |
| 1372 | * so there's nothing to do. */ |
| 1373 | goto exit; |
| 1374 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1375 | |
| 1376 | buflen = mbedtls_mpi_size( &mpi ); |
| 1377 | buffer = mbedtls_calloc( 1, buflen ); |
| 1378 | if( buffer == NULL ) |
| 1379 | { |
| 1380 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1381 | goto exit; |
| 1382 | } |
| 1383 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1384 | if( ret != 0 ) |
| 1385 | goto exit; |
| 1386 | attributes->domain_parameters = buffer; |
| 1387 | attributes->domain_parameters_size = buflen; |
| 1388 | |
| 1389 | exit: |
| 1390 | mbedtls_mpi_free( &mpi ); |
| 1391 | if( ret != 0 ) |
| 1392 | mbedtls_free( buffer ); |
| 1393 | return( mbedtls_to_psa_error( ret ) ); |
| 1394 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1395 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1396 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1397 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1398 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1399 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1400 | 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] | 1401 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1402 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1403 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1404 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1405 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1406 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1407 | psa_reset_key_attributes( attributes ); |
| 1408 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1409 | 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] | 1410 | if( status != PSA_SUCCESS ) |
| 1411 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1412 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1413 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1414 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1415 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1416 | |
| 1417 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1418 | if( psa_key_slot_is_external( slot ) ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1419 | psa_set_key_slot_number( attributes, |
| 1420 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1421 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1422 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1423 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1424 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1425 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1426 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1427 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1428 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1429 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1430 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1431 | * is not yet implemented. |
| 1432 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1433 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1434 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1435 | break; |
| 1436 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1437 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1438 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1439 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1440 | status = mbedtls_psa_rsa_load_representation( |
| 1441 | slot->attr.type, |
| 1442 | slot->key.data, |
| 1443 | slot->key.bytes, |
| 1444 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1445 | if( status != PSA_SUCCESS ) |
| 1446 | break; |
| 1447 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1448 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1449 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1450 | mbedtls_rsa_free( rsa ); |
| 1451 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1452 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1453 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1454 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1455 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1456 | default: |
| 1457 | /* Nothing else to do. */ |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | if( status != PSA_SUCCESS ) |
| 1462 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1463 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1464 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1465 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1466 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1469 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1470 | psa_status_t psa_get_key_slot_number( |
| 1471 | const psa_key_attributes_t *attributes, |
| 1472 | psa_key_slot_number_t *slot_number ) |
| 1473 | { |
| 1474 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1475 | { |
| 1476 | *slot_number = attributes->slot_number; |
| 1477 | return( PSA_SUCCESS ); |
| 1478 | } |
| 1479 | else |
| 1480 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1481 | } |
| 1482 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1483 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1484 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1485 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1486 | uint8_t *data, |
| 1487 | size_t data_size, |
| 1488 | size_t *data_length ) |
| 1489 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1490 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1491 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1492 | memcpy( data, key_buffer, key_buffer_size ); |
| 1493 | memset( data + key_buffer_size, 0, |
| 1494 | data_size - key_buffer_size ); |
| 1495 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1496 | return( PSA_SUCCESS ); |
| 1497 | } |
| 1498 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1499 | /** Export a key in binary format |
| 1500 | * |
| 1501 | * \note The signature of this function is that of a PSA driver export_key |
| 1502 | * entry point. This function behaves as an export_key entry point as |
| 1503 | * defined in the PSA driver interface specification. |
| 1504 | * |
| 1505 | * \param[in] attributes The attributes for the key to export. |
| 1506 | * \param[in] key_buffer Material or context of the key to export. |
| 1507 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 1508 | * \param[out] data Buffer where the key data is to be written. |
| 1509 | * \param[in] data_size Size of the \p data buffer in bytes. |
| 1510 | * \param[out] data_length On success, the number of bytes written in |
| 1511 | * \p data |
| 1512 | * |
| 1513 | * \retval #PSA_SUCCESS The key was exported successfully. |
| 1514 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1515 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1516 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1517 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1518 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 1519 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1520 | */ |
| 1521 | static psa_status_t psa_export_key_internal( |
| 1522 | const psa_key_attributes_t *attributes, |
| 1523 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1524 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1525 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1526 | psa_key_type_t type = attributes->core.type; |
| 1527 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1528 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1529 | const psa_drv_se_t *drv; |
| 1530 | psa_drv_se_context_t *drv_context; |
| 1531 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1532 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1533 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1534 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1535 | { |
| 1536 | if( ( drv->key_management == NULL ) || |
| 1537 | ( drv->key_management->p_export == NULL ) ) |
| 1538 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1539 | |
| 1540 | return( drv->key_management->p_export( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1541 | drv_context, |
| 1542 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1543 | data, data_size, data_length ) ); |
| 1544 | } |
| 1545 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1546 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1547 | if( key_type_is_raw_bytes( type ) || |
| 1548 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1549 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1550 | { |
| 1551 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1552 | key_buffer, key_buffer_size, |
| 1553 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1554 | } |
| 1555 | else |
| 1556 | { |
| 1557 | /* This shouldn't happen in the reference implementation, but |
| 1558 | it is valid for a special-purpose implementation to omit |
| 1559 | support for exporting certain key types. */ |
| 1560 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1565 | uint8_t *data, |
| 1566 | size_t data_size, |
| 1567 | size_t *data_length ) |
| 1568 | { |
| 1569 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1570 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1571 | psa_key_slot_t *slot; |
| 1572 | |
| 1573 | /* Set the key to empty now, so that even when there are errors, we always |
| 1574 | * set data_length to a value between 0 and data_size. On error, setting |
| 1575 | * the key to empty is a good choice because an empty key representation is |
| 1576 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1577 | *data_length = 0; |
| 1578 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1579 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1580 | * which don't require any flag, but |
| 1581 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1582 | */ |
| 1583 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1584 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1585 | if( status != PSA_SUCCESS ) |
| 1586 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1587 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1588 | /* Reject a zero-length output buffer now, since this can never be a |
| 1589 | * valid key representation. This way we know that data must be a valid |
| 1590 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1591 | if( data_size == 0 ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1592 | { |
| 1593 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 1594 | goto exit; |
| 1595 | } |
| 1596 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1597 | psa_key_attributes_t attributes = { |
| 1598 | .core = slot->attr |
| 1599 | }; |
| 1600 | status = psa_export_key_internal( &attributes, |
| 1601 | slot->key.data, slot->key.bytes, |
| 1602 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1603 | |
| 1604 | exit: |
| 1605 | unlock_status = psa_unlock_key_slot( slot ); |
| 1606 | |
| 1607 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1608 | } |
| 1609 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1610 | /** Export a public key or the public part of a key pair in binary format. |
| 1611 | * |
| 1612 | * \note The signature of this function is that of a PSA driver |
| 1613 | * export_public_key entry point. This function behaves as an |
| 1614 | * export_public_key entry point as defined in the PSA driver interface |
| 1615 | * specification. |
| 1616 | * |
| 1617 | * \param[in] attributes The attributes for the key to export. |
| 1618 | * \param[in] key_buffer Material or context of the key to export. |
| 1619 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 1620 | * \param[out] data Buffer where the key data is to be written. |
| 1621 | * \param[in] data_size Size of the \p data buffer in bytes. |
| 1622 | * \param[out] data_length On success, the number of bytes written in |
| 1623 | * \p data |
| 1624 | * |
| 1625 | * \retval #PSA_SUCCESS The public key was exported successfully. |
| 1626 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1627 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1628 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1629 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1630 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 1631 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1632 | */ |
| 1633 | static psa_status_t psa_export_public_key_internal( |
| 1634 | const psa_key_attributes_t *attributes, |
| 1635 | const uint8_t *key_buffer, |
| 1636 | size_t key_buffer_size, |
| 1637 | uint8_t *data, |
| 1638 | size_t data_size, |
| 1639 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1640 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1641 | psa_key_type_t type = attributes->core.type; |
| 1642 | psa_key_lifetime_t lifetime = attributes->core.lifetime; |
| 1643 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1644 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1645 | const psa_drv_se_t *drv; |
| 1646 | psa_drv_se_context_t *drv_context; |
| 1647 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1648 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1649 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1650 | if( psa_get_se_driver( lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1651 | { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1652 | if( ( drv->key_management == NULL ) || |
| 1653 | ( drv->key_management->p_export_public == NULL ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1654 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1655 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1656 | return( drv->key_management->p_export_public( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1657 | drv_context, |
| 1658 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1659 | data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1660 | } |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1661 | else |
| 1662 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1663 | if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1664 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1665 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1666 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1667 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1668 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1669 | key_buffer, key_buffer_size, |
| 1670 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1671 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1672 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1673 | /* Need to export the public part of a private key, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1674 | * so conversion is needed. Try the accelerators first. */ |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 1675 | psa_status_t status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1676 | attributes, key_buffer, key_buffer_size, |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 1677 | data, data_size, data_length ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1678 | |
| 1679 | if( status != PSA_ERROR_NOT_SUPPORTED || |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1680 | psa_key_lifetime_is_external( lifetime ) ) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1681 | return( status ); |
| 1682 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1683 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1684 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1685 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1686 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1687 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1688 | status = mbedtls_psa_rsa_load_representation( type, |
| 1689 | key_buffer, |
| 1690 | key_buffer_size, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1691 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1692 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1693 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1694 | |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 1695 | status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
| 1696 | rsa, |
| 1697 | data, |
| 1698 | data_size, |
| 1699 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1700 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1701 | mbedtls_rsa_free( rsa ); |
| 1702 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1703 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1704 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1705 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1706 | /* We don't know how to convert a private RSA key to public. */ |
| 1707 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1708 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1709 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1710 | } |
| 1711 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1712 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1713 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1714 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1715 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1716 | status = mbedtls_psa_ecp_load_representation( type, |
| 1717 | key_buffer, |
| 1718 | key_buffer_size, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1719 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1720 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1721 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1722 | |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 1723 | status = mbedtls_psa_ecp_export_key( |
| 1724 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1725 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) ), |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 1726 | ecp, |
| 1727 | data, |
| 1728 | data_size, |
| 1729 | data_length ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1730 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1731 | mbedtls_ecp_keypair_free( ecp ); |
| 1732 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1733 | return( status ); |
| 1734 | #else |
| 1735 | /* 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] | 1736 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1737 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1738 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1739 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1740 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1741 | else |
| 1742 | { |
| 1743 | /* This shouldn't happen in the reference implementation, but |
| 1744 | it is valid for a special-purpose implementation to omit |
| 1745 | support for exporting certain key types. */ |
| 1746 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1747 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1748 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1749 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1750 | 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] | 1751 | uint8_t *data, |
| 1752 | size_t data_size, |
| 1753 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1754 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1755 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1756 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1757 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1758 | |
| 1759 | /* Set the key to empty now, so that even when there are errors, we always |
| 1760 | * set data_length to a value between 0 and data_size. On error, setting |
| 1761 | * the key to empty is a good choice because an empty key representation is |
| 1762 | * unlikely to be accepted anywhere. */ |
| 1763 | *data_length = 0; |
| 1764 | |
| 1765 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1766 | 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] | 1767 | if( status != PSA_SUCCESS ) |
| 1768 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1769 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1770 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1771 | { |
| 1772 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1773 | goto exit; |
| 1774 | } |
| 1775 | |
| 1776 | /* Reject a zero-length output buffer now, since this can never be a |
| 1777 | * valid key representation. This way we know that data must be a valid |
| 1778 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1779 | if( data_size == 0 ) |
| 1780 | { |
| 1781 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 1782 | goto exit; |
| 1783 | } |
| 1784 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1785 | psa_key_attributes_t attributes = { |
| 1786 | .core = slot->attr |
| 1787 | }; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1788 | status = psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame^] | 1789 | &attributes, slot->key.data, slot->key.bytes, |
| 1790 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1791 | |
| 1792 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1793 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1794 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1795 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1796 | } |
| 1797 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1798 | #if defined(static_assert) |
| 1799 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1800 | "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] | 1801 | 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] | 1802 | "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] | 1803 | 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] | 1804 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1805 | #endif |
| 1806 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1807 | /** Validate that a key policy is internally well-formed. |
| 1808 | * |
| 1809 | * This function only rejects invalid policies. It does not validate the |
| 1810 | * consistency of the policy with respect to other attributes of the key |
| 1811 | * such as the key type. |
| 1812 | */ |
| 1813 | 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] | 1814 | { |
| 1815 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1816 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1817 | PSA_KEY_USAGE_ENCRYPT | |
| 1818 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1819 | PSA_KEY_USAGE_SIGN_HASH | |
| 1820 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1821 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1822 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1823 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1824 | return( PSA_SUCCESS ); |
| 1825 | } |
| 1826 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1827 | /** Validate the internal consistency of key attributes. |
| 1828 | * |
| 1829 | * This function only rejects invalid attribute values. If does not |
| 1830 | * validate the consistency of the attributes with any key data that may |
| 1831 | * be involved in the creation of the key. |
| 1832 | * |
| 1833 | * Call this function early in the key creation process. |
| 1834 | * |
| 1835 | * \param[in] attributes Key attributes for the new key. |
| 1836 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1837 | * NULL for a transparent key. |
| 1838 | * |
| 1839 | */ |
| 1840 | static psa_status_t psa_validate_key_attributes( |
| 1841 | const psa_key_attributes_t *attributes, |
| 1842 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1843 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1844 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1845 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1846 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1847 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1848 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1849 | if( status != PSA_SUCCESS ) |
| 1850 | return( status ); |
| 1851 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1852 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1853 | if( status != PSA_SUCCESS ) |
| 1854 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1855 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1856 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1857 | { |
| 1858 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1859 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1860 | } |
| 1861 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1862 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1863 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1864 | if( status != PSA_SUCCESS ) |
| 1865 | return( status ); |
| 1866 | } |
| 1867 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1868 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1869 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1870 | return( status ); |
| 1871 | |
| 1872 | /* Refuse to create overly large keys. |
| 1873 | * Note that this doesn't trigger on import if the attributes don't |
| 1874 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1875 | * psa_import_key() needs its own checks. */ |
| 1876 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1877 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1878 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1879 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1880 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1881 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1882 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1883 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1884 | return( PSA_SUCCESS ); |
| 1885 | } |
| 1886 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1887 | /** Prepare a key slot to receive key material. |
| 1888 | * |
| 1889 | * This function allocates a key slot and sets its metadata. |
| 1890 | * |
| 1891 | * If this function fails, call psa_fail_key_creation(). |
| 1892 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1893 | * This function is intended to be used as follows: |
| 1894 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1895 | * it with the specified attributes, and in case of a volatile key assign it |
| 1896 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1897 | * -# Populate the slot with the key material. |
| 1898 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1899 | * In case of failure at any step, stop the sequence and call |
| 1900 | * psa_fail_key_creation(). |
| 1901 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1902 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1903 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1904 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1905 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1906 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1907 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1908 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1909 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1910 | * |
| 1911 | * \retval #PSA_SUCCESS |
| 1912 | * The key slot is ready to receive key material. |
| 1913 | * \return If this function fails, the key slot is an invalid state. |
| 1914 | * 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] | 1915 | */ |
| 1916 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1917 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1918 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1919 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1920 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1921 | { |
| 1922 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1923 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1924 | psa_key_slot_t *slot; |
| 1925 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1926 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1927 | *p_drv = NULL; |
| 1928 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1929 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1930 | if( status != PSA_SUCCESS ) |
| 1931 | return( status ); |
| 1932 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1933 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1934 | if( status != PSA_SUCCESS ) |
| 1935 | return( status ); |
| 1936 | slot = *p_slot; |
| 1937 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1938 | /* We're storing the declared bit-size of the key. It's up to each |
| 1939 | * creation mechanism to verify that this information is correct. |
| 1940 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1941 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1942 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1943 | * volatile key identifier associated to the slot returned to contain its |
| 1944 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1945 | |
| 1946 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1947 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1948 | { |
| 1949 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1950 | slot->attr.id = volatile_key_id; |
| 1951 | #else |
| 1952 | slot->attr.id.key_id = volatile_key_id; |
| 1953 | #endif |
| 1954 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1955 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1956 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1957 | * external-only flags, query `attributes`. Thanks to the check |
| 1958 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1959 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1960 | * may have set. */ |
| 1961 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1962 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1963 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1964 | /* 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] | 1965 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1966 | * create the key file in internal storage, create the |
| 1967 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1968 | * persistent data. This is done by starting a transaction that will |
| 1969 | * encompass these three actions. |
| 1970 | * For registering a volatile key, we just need to find an appropriate |
| 1971 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1972 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1973 | /* The first thing to do is to find a slot number for the new key. |
| 1974 | * We save the slot number in persistent storage as part of the |
| 1975 | * transaction data. It will be needed to recover if the power |
| 1976 | * fails during the key creation process, to clean up on the secure |
| 1977 | * element side after restarting. Obtaining a slot number from the |
| 1978 | * secure element driver updates its persistent state, but we do not yet |
| 1979 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1980 | * 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] | 1981 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1982 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1983 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1984 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1985 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1986 | if( status != PSA_SUCCESS ) |
| 1987 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1988 | |
| 1989 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1990 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1991 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1992 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1993 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1994 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1995 | status = psa_crypto_save_transaction( ); |
| 1996 | if( status != PSA_SUCCESS ) |
| 1997 | { |
| 1998 | (void) psa_crypto_stop_transaction( ); |
| 1999 | return( status ); |
| 2000 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2001 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2002 | |
| 2003 | status = psa_copy_key_material_into_slot( |
| 2004 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2005 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2006 | |
| 2007 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2008 | { |
| 2009 | /* Key registration only makes sense with a secure element. */ |
| 2010 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2011 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2012 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2013 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2014 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2015 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2016 | |
| 2017 | /** Finalize the creation of a key once its key material has been set. |
| 2018 | * |
| 2019 | * This entails writing the key to persistent storage. |
| 2020 | * |
| 2021 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2022 | * See the documentation of psa_start_key_creation() for the intended use |
| 2023 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2024 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2025 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 2026 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 2027 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2028 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2029 | * \param[in,out] slot Pointer to the slot with key material. |
| 2030 | * \param[in] driver The secure element driver for the key, |
| 2031 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2032 | * \param[out] key On success, identifier of the key. Note that the |
| 2033 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2034 | * |
| 2035 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2036 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2037 | * \return If this function fails, the key slot is an invalid state. |
| 2038 | * 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] | 2039 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2040 | static psa_status_t psa_finish_key_creation( |
| 2041 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2042 | psa_se_drv_table_entry_t *driver, |
| 2043 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2044 | { |
| 2045 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2046 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2047 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2048 | |
| 2049 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2050 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2051 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2052 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2053 | if( driver != NULL ) |
| 2054 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2055 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2056 | psa_key_slot_number_t slot_number = |
| 2057 | psa_key_slot_get_slot_number( slot ) ; |
| 2058 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2059 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2060 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2061 | sizeof( data.slot_number ), |
| 2062 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2063 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2064 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2065 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2066 | (uint8_t*) &data, |
| 2067 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2068 | } |
| 2069 | else |
| 2070 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2071 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2072 | /* Key material is saved in export representation in the slot, so |
| 2073 | * just pass the slot buffer for storage. */ |
| 2074 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2075 | slot->key.data, |
| 2076 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2077 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2078 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2079 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2080 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2081 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2082 | /* Finish the transaction for a key creation. This does not |
| 2083 | * happen when registering an existing key. Detect this case |
| 2084 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2085 | * creation of a persistent key in a secure element requires a transaction, |
| 2086 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2087 | if( driver != NULL && |
| 2088 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2089 | { |
| 2090 | status = psa_save_se_persistent_data( driver ); |
| 2091 | if( status != PSA_SUCCESS ) |
| 2092 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2093 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2094 | return( status ); |
| 2095 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2096 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2097 | } |
| 2098 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2099 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2100 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2101 | { |
| 2102 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2103 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2104 | if( status != PSA_SUCCESS ) |
| 2105 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2106 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2107 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2108 | return( status ); |
| 2109 | } |
| 2110 | |
| 2111 | /** Abort the creation of a key. |
| 2112 | * |
| 2113 | * You may call this function after calling psa_start_key_creation(), |
| 2114 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2115 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2116 | * See the documentation of psa_start_key_creation() for the intended use |
| 2117 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2118 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2119 | * \param[in,out] slot Pointer to the slot with key material. |
| 2120 | * \param[in] driver The secure element driver for the key, |
| 2121 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2122 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2123 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2124 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2125 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2126 | (void) driver; |
| 2127 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2128 | if( slot == NULL ) |
| 2129 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2130 | |
| 2131 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2132 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2133 | * element, and the failure happened later (when saving metadata |
| 2134 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2135 | * element. |
| 2136 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2137 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2139 | /* Abort the ongoing transaction if any (there may not be one if |
| 2140 | * the creation process failed before starting one, or if the |
| 2141 | * key creation is a registration of a key in a secure element). |
| 2142 | * Earlier functions must already have done what it takes to undo any |
| 2143 | * partial creation. All that's left is to update the transaction data |
| 2144 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2145 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2146 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2147 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2148 | psa_wipe_key_slot( slot ); |
| 2149 | } |
| 2150 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2151 | /** Validate optional attributes during key creation. |
| 2152 | * |
| 2153 | * Some key attributes are optional during key creation. If they are |
| 2154 | * specified in the attributes structure, check that they are consistent |
| 2155 | * with the data in the slot. |
| 2156 | * |
| 2157 | * This function should be called near the end of key creation, after |
| 2158 | * the slot in memory is fully populated but before saving persistent data. |
| 2159 | */ |
| 2160 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2161 | const psa_key_slot_t *slot, |
| 2162 | const psa_key_attributes_t *attributes ) |
| 2163 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2164 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2165 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2166 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2167 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2168 | } |
| 2169 | |
| 2170 | if( attributes->domain_parameters_size != 0 ) |
| 2171 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2172 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2173 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2174 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2175 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2176 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2177 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2178 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2179 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2180 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 2181 | slot->attr.type, |
| 2182 | slot->key.data, |
| 2183 | slot->key.bytes, |
| 2184 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2185 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2186 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2187 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2188 | mbedtls_mpi_init( &actual ); |
| 2189 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2190 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2191 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2192 | mbedtls_rsa_free( rsa ); |
| 2193 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2194 | if( ret != 0 ) |
| 2195 | goto rsa_exit; |
| 2196 | ret = mbedtls_mpi_read_binary( &required, |
| 2197 | attributes->domain_parameters, |
| 2198 | attributes->domain_parameters_size ); |
| 2199 | if( ret != 0 ) |
| 2200 | goto rsa_exit; |
| 2201 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2202 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2203 | rsa_exit: |
| 2204 | mbedtls_mpi_free( &actual ); |
| 2205 | mbedtls_mpi_free( &required ); |
| 2206 | if( ret != 0) |
| 2207 | return( mbedtls_to_psa_error( ret ) ); |
| 2208 | } |
| 2209 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2210 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2211 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2212 | { |
| 2213 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2214 | } |
| 2215 | } |
| 2216 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2217 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2218 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2219 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2220 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2221 | } |
| 2222 | |
| 2223 | return( PSA_SUCCESS ); |
| 2224 | } |
| 2225 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2226 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2227 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2228 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2229 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2230 | { |
| 2231 | psa_status_t status; |
| 2232 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2233 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2234 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2235 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2236 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2237 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2238 | * This also rejects any key which might be encoded as an empty string, |
| 2239 | * which is never valid. */ |
| 2240 | if( data_length == 0 ) |
| 2241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2242 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2243 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2244 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2245 | if( status != PSA_SUCCESS ) |
| 2246 | goto exit; |
| 2247 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2248 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2249 | if( driver != NULL ) |
| 2250 | { |
| 2251 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2252 | /* The driver should set the number of key bits, however in |
| 2253 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2254 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2255 | if( drv->key_management == NULL || |
| 2256 | drv->key_management->p_import == NULL ) |
| 2257 | { |
| 2258 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2259 | goto exit; |
| 2260 | } |
| 2261 | status = drv->key_management->p_import( |
| 2262 | psa_get_se_driver_context( driver ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2263 | psa_key_slot_get_slot_number( slot ), |
| 2264 | attributes, data, data_length, &bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2265 | if( status != PSA_SUCCESS ) |
| 2266 | goto exit; |
| 2267 | if( bits > PSA_MAX_KEY_BITS ) |
| 2268 | { |
| 2269 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2270 | goto exit; |
| 2271 | } |
| 2272 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2273 | } |
| 2274 | else |
| 2275 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2276 | if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) ) |
| 2277 | { |
| 2278 | /* Importing a key with external lifetime through the driver wrapper |
| 2279 | * interface is not yet supported. Return as if this was an invalid |
| 2280 | * lifetime. */ |
| 2281 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2282 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2283 | } |
| 2284 | else |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2285 | { |
| 2286 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2287 | if( status != PSA_SUCCESS ) |
| 2288 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2289 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2290 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2291 | if( status != PSA_SUCCESS ) |
| 2292 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2293 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2294 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2295 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2296 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2297 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2298 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2299 | return( status ); |
| 2300 | } |
| 2301 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2302 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2303 | psa_status_t mbedtls_psa_register_se_key( |
| 2304 | const psa_key_attributes_t *attributes ) |
| 2305 | { |
| 2306 | psa_status_t status; |
| 2307 | psa_key_slot_t *slot = NULL; |
| 2308 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2309 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2310 | |
| 2311 | /* Leaving attributes unspecified is not currently supported. |
| 2312 | * It could make sense to query the key type and size from the |
| 2313 | * secure element, but not all secure elements support this |
| 2314 | * and the driver HAL doesn't currently support it. */ |
| 2315 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2316 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2317 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2318 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2319 | |
| 2320 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2321 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2322 | if( status != PSA_SUCCESS ) |
| 2323 | goto exit; |
| 2324 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2325 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2326 | |
| 2327 | exit: |
| 2328 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2329 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2330 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2331 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2332 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2333 | return( status ); |
| 2334 | } |
| 2335 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2336 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2337 | 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] | 2338 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2339 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2340 | psa_status_t status = psa_copy_key_material_into_slot( target, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2341 | source->key.data, |
| 2342 | source->key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2343 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2344 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2345 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2346 | target->attr.type = source->attr.type; |
| 2347 | target->attr.bits = source->attr.bits; |
| 2348 | |
| 2349 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2352 | 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] | 2353 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2354 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2355 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2356 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2357 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2358 | psa_key_slot_t *source_slot = NULL; |
| 2359 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2360 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2361 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2362 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2363 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2364 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2365 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2366 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2367 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2368 | goto exit; |
| 2369 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2370 | status = psa_validate_optional_attributes( source_slot, |
| 2371 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2372 | if( status != PSA_SUCCESS ) |
| 2373 | goto exit; |
| 2374 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2375 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2376 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2377 | if( status != PSA_SUCCESS ) |
| 2378 | goto exit; |
| 2379 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2380 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2381 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2382 | if( status != PSA_SUCCESS ) |
| 2383 | goto exit; |
| 2384 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2385 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2386 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2387 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2388 | /* Copying to a secure element is not implemented yet. */ |
| 2389 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2390 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2391 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2392 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2393 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2394 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2395 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2396 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2397 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2398 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2399 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2400 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2401 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2402 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2403 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2404 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2405 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2408 | |
| 2409 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2410 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2411 | /* Message digests */ |
| 2412 | /****************************************************************/ |
| 2413 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2414 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2415 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2416 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2417 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2418 | 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] | 2419 | { |
| 2420 | switch( alg ) |
| 2421 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2422 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2423 | case PSA_ALG_MD2: |
| 2424 | return( &mbedtls_md2_info ); |
| 2425 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2426 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2427 | case PSA_ALG_MD4: |
| 2428 | return( &mbedtls_md4_info ); |
| 2429 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2430 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2431 | case PSA_ALG_MD5: |
| 2432 | return( &mbedtls_md5_info ); |
| 2433 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2434 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2435 | case PSA_ALG_RIPEMD160: |
| 2436 | return( &mbedtls_ripemd160_info ); |
| 2437 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2438 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2439 | case PSA_ALG_SHA_1: |
| 2440 | return( &mbedtls_sha1_info ); |
| 2441 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2442 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2443 | case PSA_ALG_SHA_224: |
| 2444 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2445 | #endif |
| 2446 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2447 | case PSA_ALG_SHA_256: |
| 2448 | return( &mbedtls_sha256_info ); |
| 2449 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2450 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2451 | case PSA_ALG_SHA_384: |
| 2452 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2453 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2454 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2455 | case PSA_ALG_SHA_512: |
| 2456 | return( &mbedtls_sha512_info ); |
| 2457 | #endif |
| 2458 | default: |
| 2459 | return( NULL ); |
| 2460 | } |
| 2461 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2462 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2463 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2464 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2465 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2466 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2467 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2468 | { |
| 2469 | switch( operation->alg ) |
| 2470 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2471 | case 0: |
| 2472 | /* The object has (apparently) been initialized but it is not |
| 2473 | * in use. It's ok to call abort on such an object, and there's |
| 2474 | * nothing to do. */ |
| 2475 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2476 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2477 | case PSA_ALG_MD2: |
| 2478 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2479 | break; |
| 2480 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2481 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2482 | case PSA_ALG_MD4: |
| 2483 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2484 | break; |
| 2485 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2486 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2487 | case PSA_ALG_MD5: |
| 2488 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2489 | break; |
| 2490 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2491 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2492 | case PSA_ALG_RIPEMD160: |
| 2493 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2494 | break; |
| 2495 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2496 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2497 | case PSA_ALG_SHA_1: |
| 2498 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2499 | break; |
| 2500 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2501 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2502 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2503 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2504 | break; |
| 2505 | #endif |
| 2506 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2507 | case PSA_ALG_SHA_256: |
| 2508 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2509 | break; |
| 2510 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2511 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2512 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2513 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2514 | break; |
| 2515 | #endif |
| 2516 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2517 | case PSA_ALG_SHA_512: |
| 2518 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2519 | break; |
| 2520 | #endif |
| 2521 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2522 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2523 | } |
| 2524 | operation->alg = 0; |
| 2525 | return( PSA_SUCCESS ); |
| 2526 | } |
| 2527 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2528 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2529 | psa_algorithm_t alg ) |
| 2530 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2531 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2532 | |
| 2533 | /* A context must be freshly initialized before it can be set up. */ |
| 2534 | if( operation->alg != 0 ) |
| 2535 | { |
| 2536 | return( PSA_ERROR_BAD_STATE ); |
| 2537 | } |
| 2538 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2539 | switch( alg ) |
| 2540 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2541 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2542 | case PSA_ALG_MD2: |
| 2543 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2544 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2545 | break; |
| 2546 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2547 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2548 | case PSA_ALG_MD4: |
| 2549 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2550 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2551 | break; |
| 2552 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2553 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2554 | case PSA_ALG_MD5: |
| 2555 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2556 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2557 | break; |
| 2558 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2559 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2560 | case PSA_ALG_RIPEMD160: |
| 2561 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2562 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2563 | break; |
| 2564 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2565 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2566 | case PSA_ALG_SHA_1: |
| 2567 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2568 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2569 | break; |
| 2570 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2571 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2572 | case PSA_ALG_SHA_224: |
| 2573 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2574 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2575 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2576 | #endif |
| 2577 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2578 | case PSA_ALG_SHA_256: |
| 2579 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2580 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2581 | break; |
| 2582 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2583 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2584 | case PSA_ALG_SHA_384: |
| 2585 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2586 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2587 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2588 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2589 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2590 | case PSA_ALG_SHA_512: |
| 2591 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2592 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2593 | break; |
| 2594 | #endif |
| 2595 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2596 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2597 | PSA_ERROR_NOT_SUPPORTED : |
| 2598 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2599 | } |
| 2600 | if( ret == 0 ) |
| 2601 | operation->alg = alg; |
| 2602 | else |
| 2603 | psa_hash_abort( operation ); |
| 2604 | return( mbedtls_to_psa_error( ret ) ); |
| 2605 | } |
| 2606 | |
| 2607 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2608 | const uint8_t *input, |
| 2609 | size_t input_length ) |
| 2610 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2611 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2612 | |
| 2613 | /* Don't require hash implementations to behave correctly on a |
| 2614 | * zero-length input, which may have an invalid pointer. */ |
| 2615 | if( input_length == 0 ) |
| 2616 | return( PSA_SUCCESS ); |
| 2617 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2618 | switch( operation->alg ) |
| 2619 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2620 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2621 | case PSA_ALG_MD2: |
| 2622 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2623 | input, input_length ); |
| 2624 | break; |
| 2625 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2626 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2627 | case PSA_ALG_MD4: |
| 2628 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2629 | input, input_length ); |
| 2630 | break; |
| 2631 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2632 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2633 | case PSA_ALG_MD5: |
| 2634 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2635 | input, input_length ); |
| 2636 | break; |
| 2637 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2638 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2639 | case PSA_ALG_RIPEMD160: |
| 2640 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2641 | input, input_length ); |
| 2642 | break; |
| 2643 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2644 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2645 | case PSA_ALG_SHA_1: |
| 2646 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2647 | input, input_length ); |
| 2648 | break; |
| 2649 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2650 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2651 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2652 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2653 | input, input_length ); |
| 2654 | break; |
| 2655 | #endif |
| 2656 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2657 | case PSA_ALG_SHA_256: |
| 2658 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2659 | input, input_length ); |
| 2660 | break; |
| 2661 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2662 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2663 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2664 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2665 | input, input_length ); |
| 2666 | break; |
| 2667 | #endif |
| 2668 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2669 | case PSA_ALG_SHA_512: |
| 2670 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2671 | input, input_length ); |
| 2672 | break; |
| 2673 | #endif |
| 2674 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2675 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2676 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2677 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2678 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2679 | if( ret != 0 ) |
| 2680 | psa_hash_abort( operation ); |
| 2681 | return( mbedtls_to_psa_error( ret ) ); |
| 2682 | } |
| 2683 | |
| 2684 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2685 | uint8_t *hash, |
| 2686 | size_t hash_size, |
| 2687 | size_t *hash_length ) |
| 2688 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2689 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2690 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2691 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2692 | |
| 2693 | /* Fill the output buffer with something that isn't a valid hash |
| 2694 | * (barring an attack on the hash and deliberately-crafted input), |
| 2695 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2696 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2697 | /* If hash_size is 0 then hash may be NULL and then the |
| 2698 | * call to memset would have undefined behavior. */ |
| 2699 | if( hash_size != 0 ) |
| 2700 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2701 | |
| 2702 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2703 | { |
| 2704 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2705 | goto exit; |
| 2706 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2707 | |
| 2708 | switch( operation->alg ) |
| 2709 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2710 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2711 | case PSA_ALG_MD2: |
| 2712 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2713 | break; |
| 2714 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2715 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2716 | case PSA_ALG_MD4: |
| 2717 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2718 | break; |
| 2719 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2720 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2721 | case PSA_ALG_MD5: |
| 2722 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2723 | break; |
| 2724 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2725 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2726 | case PSA_ALG_RIPEMD160: |
| 2727 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2728 | break; |
| 2729 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2730 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2731 | case PSA_ALG_SHA_1: |
| 2732 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2733 | break; |
| 2734 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2735 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2736 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2737 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2738 | break; |
| 2739 | #endif |
| 2740 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2741 | case PSA_ALG_SHA_256: |
| 2742 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2743 | break; |
| 2744 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2745 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2746 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2747 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2748 | break; |
| 2749 | #endif |
| 2750 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2751 | case PSA_ALG_SHA_512: |
| 2752 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2753 | break; |
| 2754 | #endif |
| 2755 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2756 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2757 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2758 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2759 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2760 | exit: |
| 2761 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2762 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2763 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2764 | return( psa_hash_abort( operation ) ); |
| 2765 | } |
| 2766 | else |
| 2767 | { |
| 2768 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2769 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2773 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2774 | const uint8_t *hash, |
| 2775 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2776 | { |
| 2777 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2778 | size_t actual_hash_length; |
| 2779 | psa_status_t status = psa_hash_finish( operation, |
| 2780 | actual_hash, sizeof( actual_hash ), |
| 2781 | &actual_hash_length ); |
| 2782 | if( status != PSA_SUCCESS ) |
| 2783 | return( status ); |
| 2784 | if( actual_hash_length != hash_length ) |
| 2785 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2786 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2787 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2788 | return( PSA_SUCCESS ); |
| 2789 | } |
| 2790 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2791 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2792 | const uint8_t *input, size_t input_length, |
| 2793 | uint8_t *hash, size_t hash_size, |
| 2794 | size_t *hash_length ) |
| 2795 | { |
| 2796 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2797 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2798 | |
| 2799 | *hash_length = hash_size; |
| 2800 | status = psa_hash_setup( &operation, alg ); |
| 2801 | if( status != PSA_SUCCESS ) |
| 2802 | goto exit; |
| 2803 | status = psa_hash_update( &operation, input, input_length ); |
| 2804 | if( status != PSA_SUCCESS ) |
| 2805 | goto exit; |
| 2806 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2807 | if( status != PSA_SUCCESS ) |
| 2808 | goto exit; |
| 2809 | |
| 2810 | exit: |
| 2811 | if( status == PSA_SUCCESS ) |
| 2812 | status = psa_hash_abort( &operation ); |
| 2813 | else |
| 2814 | psa_hash_abort( &operation ); |
| 2815 | return( status ); |
| 2816 | } |
| 2817 | |
| 2818 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2819 | const uint8_t *input, size_t input_length, |
| 2820 | const uint8_t *hash, size_t hash_length ) |
| 2821 | { |
| 2822 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2823 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2824 | |
| 2825 | status = psa_hash_setup( &operation, alg ); |
| 2826 | if( status != PSA_SUCCESS ) |
| 2827 | goto exit; |
| 2828 | status = psa_hash_update( &operation, input, input_length ); |
| 2829 | if( status != PSA_SUCCESS ) |
| 2830 | goto exit; |
| 2831 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2832 | if( status != PSA_SUCCESS ) |
| 2833 | goto exit; |
| 2834 | |
| 2835 | exit: |
| 2836 | if( status == PSA_SUCCESS ) |
| 2837 | status = psa_hash_abort( &operation ); |
| 2838 | else |
| 2839 | psa_hash_abort( &operation ); |
| 2840 | return( status ); |
| 2841 | } |
| 2842 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2843 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2844 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2845 | { |
| 2846 | if( target_operation->alg != 0 ) |
| 2847 | return( PSA_ERROR_BAD_STATE ); |
| 2848 | |
| 2849 | switch( source_operation->alg ) |
| 2850 | { |
| 2851 | case 0: |
| 2852 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2853 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2854 | case PSA_ALG_MD2: |
| 2855 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2856 | &source_operation->ctx.md2 ); |
| 2857 | break; |
| 2858 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2859 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2860 | case PSA_ALG_MD4: |
| 2861 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2862 | &source_operation->ctx.md4 ); |
| 2863 | break; |
| 2864 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2865 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2866 | case PSA_ALG_MD5: |
| 2867 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2868 | &source_operation->ctx.md5 ); |
| 2869 | break; |
| 2870 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2871 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2872 | case PSA_ALG_RIPEMD160: |
| 2873 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2874 | &source_operation->ctx.ripemd160 ); |
| 2875 | break; |
| 2876 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2877 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2878 | case PSA_ALG_SHA_1: |
| 2879 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2880 | &source_operation->ctx.sha1 ); |
| 2881 | break; |
| 2882 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2883 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2884 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2885 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2886 | &source_operation->ctx.sha256 ); |
| 2887 | break; |
| 2888 | #endif |
| 2889 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2890 | case PSA_ALG_SHA_256: |
| 2891 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2892 | &source_operation->ctx.sha256 ); |
| 2893 | break; |
| 2894 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2895 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2896 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2897 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2898 | &source_operation->ctx.sha512 ); |
| 2899 | break; |
| 2900 | #endif |
| 2901 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2902 | case PSA_ALG_SHA_512: |
| 2903 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2904 | &source_operation->ctx.sha512 ); |
| 2905 | break; |
| 2906 | #endif |
| 2907 | default: |
| 2908 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2909 | } |
| 2910 | |
| 2911 | target_operation->alg = source_operation->alg; |
| 2912 | return( PSA_SUCCESS ); |
| 2913 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2914 | |
| 2915 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2916 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2917 | /* MAC */ |
| 2918 | /****************************************************************/ |
| 2919 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2920 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2921 | psa_algorithm_t alg, |
| 2922 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2923 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2924 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2925 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2926 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2927 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2928 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2929 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2930 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2931 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2932 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2933 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2934 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2935 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 2936 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2937 | mode = MBEDTLS_MODE_STREAM; |
| 2938 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2939 | case PSA_ALG_CTR: |
| 2940 | mode = MBEDTLS_MODE_CTR; |
| 2941 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2942 | case PSA_ALG_CFB: |
| 2943 | mode = MBEDTLS_MODE_CFB; |
| 2944 | break; |
| 2945 | case PSA_ALG_OFB: |
| 2946 | mode = MBEDTLS_MODE_OFB; |
| 2947 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2948 | case PSA_ALG_ECB_NO_PADDING: |
| 2949 | mode = MBEDTLS_MODE_ECB; |
| 2950 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2951 | case PSA_ALG_CBC_NO_PADDING: |
| 2952 | mode = MBEDTLS_MODE_CBC; |
| 2953 | break; |
| 2954 | case PSA_ALG_CBC_PKCS7: |
| 2955 | mode = MBEDTLS_MODE_CBC; |
| 2956 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2957 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2958 | mode = MBEDTLS_MODE_CCM; |
| 2959 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2960 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2961 | mode = MBEDTLS_MODE_GCM; |
| 2962 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2963 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2964 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2965 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2966 | default: |
| 2967 | return( NULL ); |
| 2968 | } |
| 2969 | } |
| 2970 | else if( alg == PSA_ALG_CMAC ) |
| 2971 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2972 | else |
| 2973 | return( NULL ); |
| 2974 | |
| 2975 | switch( key_type ) |
| 2976 | { |
| 2977 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2978 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2979 | break; |
| 2980 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2981 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2982 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2983 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2984 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2985 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2986 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2987 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2988 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2989 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2990 | if( key_bits == 128 ) |
| 2991 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2992 | break; |
| 2993 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2994 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2995 | break; |
| 2996 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2997 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2998 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2999 | case PSA_KEY_TYPE_CHACHA20: |
| 3000 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 3001 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3002 | default: |
| 3003 | return( NULL ); |
| 3004 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3005 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3006 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3007 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3008 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 3009 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3012 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3013 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3014 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3015 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3016 | { |
| 3017 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3018 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3019 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3020 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3021 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3022 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3023 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3024 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3025 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3026 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3027 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3028 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3029 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3030 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3031 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3032 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3033 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3034 | return( 128 ); |
| 3035 | default: |
| 3036 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3037 | } |
| 3038 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3039 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 3040 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3041 | /* Initialize the MAC operation structure. Once this function has been |
| 3042 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3043 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3044 | psa_algorithm_t alg ) |
| 3045 | { |
| 3046 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3047 | |
| 3048 | operation->alg = alg; |
| 3049 | operation->key_set = 0; |
| 3050 | operation->iv_set = 0; |
| 3051 | operation->iv_required = 0; |
| 3052 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3053 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3054 | |
| 3055 | #if defined(MBEDTLS_CMAC_C) |
| 3056 | if( alg == PSA_ALG_CMAC ) |
| 3057 | { |
| 3058 | operation->iv_required = 0; |
| 3059 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3060 | status = PSA_SUCCESS; |
| 3061 | } |
| 3062 | else |
| 3063 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3064 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3065 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3066 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3067 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3068 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3069 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3070 | } |
| 3071 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3072 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3073 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3074 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3075 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | if( status != PSA_SUCCESS ) |
| 3079 | memset( operation, 0, sizeof( *operation ) ); |
| 3080 | return( status ); |
| 3081 | } |
| 3082 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3083 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3084 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3085 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3086 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3087 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3088 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3089 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3090 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3091 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3092 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3093 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3094 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3095 | /* The object has (apparently) been initialized but it is not |
| 3096 | * in use. It's ok to call abort on such an object, and there's |
| 3097 | * nothing to do. */ |
| 3098 | return( PSA_SUCCESS ); |
| 3099 | } |
| 3100 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3101 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3102 | if( operation->alg == PSA_ALG_CMAC ) |
| 3103 | { |
| 3104 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3105 | } |
| 3106 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3107 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3108 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3109 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3110 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3111 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3112 | } |
| 3113 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3114 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3115 | { |
| 3116 | /* Sanity check (shouldn't happen: operation->alg should |
| 3117 | * always have been initialized to a valid value). */ |
| 3118 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3119 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3120 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3121 | operation->alg = 0; |
| 3122 | operation->key_set = 0; |
| 3123 | operation->iv_set = 0; |
| 3124 | operation->iv_required = 0; |
| 3125 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3126 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3127 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3128 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3129 | |
| 3130 | bad_state: |
| 3131 | /* If abort is called on an uninitialized object, we can't trust |
| 3132 | * anything. Wipe the object in case it contains confidential data. |
| 3133 | * This may result in a memory leak if a pointer gets overwritten, |
| 3134 | * but it's too late to do anything about this. */ |
| 3135 | memset( operation, 0, sizeof( *operation ) ); |
| 3136 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3139 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3140 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3141 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3142 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3143 | const mbedtls_cipher_info_t *cipher_info ) |
| 3144 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3145 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3146 | |
| 3147 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3148 | |
| 3149 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3150 | if( ret != 0 ) |
| 3151 | return( ret ); |
| 3152 | |
| 3153 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3154 | slot->key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3155 | key_bits ); |
| 3156 | return( ret ); |
| 3157 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3158 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3159 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3160 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3161 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3162 | const uint8_t *key, |
| 3163 | size_t key_length, |
| 3164 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3165 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3166 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3167 | size_t i; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3168 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3169 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3170 | psa_status_t status; |
| 3171 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3172 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3173 | * overflow below. This should never trigger if the hash algorithm |
| 3174 | * is implemented correctly. */ |
| 3175 | /* The size checks against the ipad and opad buffers cannot be written |
| 3176 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3177 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3178 | if( block_size > sizeof( ipad ) ) |
| 3179 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3180 | if( block_size > sizeof( hmac->opad ) ) |
| 3181 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3182 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3183 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3184 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3185 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3186 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3187 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3188 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3189 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3190 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3191 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3192 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3193 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3194 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3195 | * an invalid pointer which would make the behavior undefined. */ |
| 3196 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3197 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3198 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3199 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3200 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3201 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3202 | ipad[i] ^= 0x36; |
| 3203 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3204 | |
| 3205 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3206 | * and filling the rest of opad with the requisite constant. */ |
| 3207 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3208 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3209 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3210 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3211 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3212 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3213 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3214 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3215 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3216 | |
| 3217 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3218 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3219 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3220 | return( status ); |
| 3221 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3222 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3223 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3224 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3225 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3226 | psa_algorithm_t alg, |
| 3227 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3228 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3229 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3230 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3231 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3232 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3233 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3234 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3235 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3236 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3237 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3238 | /* A context must be freshly initialized before it can be set up. */ |
| 3239 | if( operation->alg != 0 ) |
| 3240 | { |
| 3241 | return( PSA_ERROR_BAD_STATE ); |
| 3242 | } |
| 3243 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3244 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3245 | if( status != PSA_SUCCESS ) |
| 3246 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3247 | if( is_sign ) |
| 3248 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3249 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3250 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3251 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3252 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3253 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3254 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3255 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3256 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3257 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3258 | { |
| 3259 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3260 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3261 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3262 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3263 | if( cipher_info == NULL ) |
| 3264 | { |
| 3265 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3266 | goto exit; |
| 3267 | } |
| 3268 | operation->mac_size = cipher_info->block_size; |
| 3269 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3270 | status = mbedtls_to_psa_error( ret ); |
| 3271 | } |
| 3272 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3273 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3274 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3275 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3276 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3277 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3278 | if( hash_alg == 0 ) |
| 3279 | { |
| 3280 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3281 | goto exit; |
| 3282 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3283 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3284 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3285 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3286 | if( operation->mac_size == 0 || |
| 3287 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3288 | { |
| 3289 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3290 | goto exit; |
| 3291 | } |
| 3292 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3293 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3294 | { |
| 3295 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3296 | goto exit; |
| 3297 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3298 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3299 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 3300 | slot->key.data, |
| 3301 | slot->key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3302 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3303 | } |
| 3304 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3305 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3306 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3307 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3308 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3309 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3310 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3311 | if( truncated == 0 ) |
| 3312 | { |
| 3313 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3314 | } |
| 3315 | else if( truncated < 4 ) |
| 3316 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3317 | /* A very short MAC is too short for security since it can be |
| 3318 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3319 | * so we make this our minimum, even though 32 bits is still |
| 3320 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3321 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3322 | } |
| 3323 | else if( truncated > operation->mac_size ) |
| 3324 | { |
| 3325 | /* It's impossible to "truncate" to a larger length. */ |
| 3326 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3327 | } |
| 3328 | else |
| 3329 | operation->mac_size = truncated; |
| 3330 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3331 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3332 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3333 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3334 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3335 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3336 | else |
| 3337 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3338 | operation->key_set = 1; |
| 3339 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3340 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3341 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3342 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3343 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3344 | } |
| 3345 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3346 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3347 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3348 | psa_algorithm_t alg ) |
| 3349 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3350 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3354 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3355 | psa_algorithm_t alg ) |
| 3356 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3357 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3360 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3361 | const uint8_t *input, |
| 3362 | size_t input_length ) |
| 3363 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3364 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3365 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3366 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3367 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3368 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3369 | operation->has_input = 1; |
| 3370 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3371 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3372 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3373 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3374 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3375 | input, input_length ); |
| 3376 | status = mbedtls_to_psa_error( ret ); |
| 3377 | } |
| 3378 | else |
| 3379 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3380 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3381 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3382 | { |
| 3383 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3384 | input_length ); |
| 3385 | } |
| 3386 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3387 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3388 | { |
| 3389 | /* This shouldn't happen if `operation` was initialized by |
| 3390 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3391 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3392 | } |
| 3393 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3394 | if( status != PSA_SUCCESS ) |
| 3395 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3396 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3399 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3400 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3401 | uint8_t *mac, |
| 3402 | size_t mac_size ) |
| 3403 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3404 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3405 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3406 | size_t hash_size = 0; |
| 3407 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3408 | psa_status_t status; |
| 3409 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3410 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3411 | if( status != PSA_SUCCESS ) |
| 3412 | return( status ); |
| 3413 | /* From here on, tmp needs to be wiped. */ |
| 3414 | |
| 3415 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3416 | if( status != PSA_SUCCESS ) |
| 3417 | goto exit; |
| 3418 | |
| 3419 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3420 | if( status != PSA_SUCCESS ) |
| 3421 | goto exit; |
| 3422 | |
| 3423 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3424 | if( status != PSA_SUCCESS ) |
| 3425 | goto exit; |
| 3426 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3427 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3428 | if( status != PSA_SUCCESS ) |
| 3429 | goto exit; |
| 3430 | |
| 3431 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3432 | |
| 3433 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3434 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3435 | return( status ); |
| 3436 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3437 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3438 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3439 | 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] | 3440 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3441 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3442 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3443 | if( ! operation->key_set ) |
| 3444 | return( PSA_ERROR_BAD_STATE ); |
| 3445 | if( operation->iv_required && ! operation->iv_set ) |
| 3446 | return( PSA_ERROR_BAD_STATE ); |
| 3447 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3448 | if( mac_size < operation->mac_size ) |
| 3449 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3450 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3451 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3452 | if( operation->alg == PSA_ALG_CMAC ) |
| 3453 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3454 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3455 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3456 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3457 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3458 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3459 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3460 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3461 | else |
| 3462 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3463 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3464 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3465 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3466 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3467 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3468 | } |
| 3469 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3470 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3471 | { |
| 3472 | /* This shouldn't happen if `operation` was initialized by |
| 3473 | * a setup function. */ |
| 3474 | return( PSA_ERROR_BAD_STATE ); |
| 3475 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3476 | } |
| 3477 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3478 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3479 | uint8_t *mac, |
| 3480 | size_t mac_size, |
| 3481 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3482 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3483 | psa_status_t status; |
| 3484 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3485 | if( operation->alg == 0 ) |
| 3486 | { |
| 3487 | return( PSA_ERROR_BAD_STATE ); |
| 3488 | } |
| 3489 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3490 | /* Fill the output buffer with something that isn't a valid mac |
| 3491 | * (barring an attack on the mac and deliberately-crafted input), |
| 3492 | * in case the caller doesn't check the return status properly. */ |
| 3493 | *mac_length = mac_size; |
| 3494 | /* If mac_size is 0 then mac may be NULL and then the |
| 3495 | * call to memset would have undefined behavior. */ |
| 3496 | if( mac_size != 0 ) |
| 3497 | memset( mac, '!', mac_size ); |
| 3498 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3499 | if( ! operation->is_sign ) |
| 3500 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3501 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3502 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3503 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3504 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3505 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3506 | if( status == PSA_SUCCESS ) |
| 3507 | { |
| 3508 | status = psa_mac_abort( operation ); |
| 3509 | if( status == PSA_SUCCESS ) |
| 3510 | *mac_length = operation->mac_size; |
| 3511 | else |
| 3512 | memset( mac, '!', mac_size ); |
| 3513 | } |
| 3514 | else |
| 3515 | psa_mac_abort( operation ); |
| 3516 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3519 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3520 | const uint8_t *mac, |
| 3521 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3522 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3523 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3524 | psa_status_t status; |
| 3525 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3526 | if( operation->alg == 0 ) |
| 3527 | { |
| 3528 | return( PSA_ERROR_BAD_STATE ); |
| 3529 | } |
| 3530 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3531 | if( operation->is_sign ) |
| 3532 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3533 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3534 | } |
| 3535 | if( operation->mac_size != mac_length ) |
| 3536 | { |
| 3537 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3538 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3539 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3540 | |
| 3541 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3542 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3543 | if( status != PSA_SUCCESS ) |
| 3544 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3545 | |
| 3546 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3547 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3548 | |
| 3549 | cleanup: |
| 3550 | if( status == PSA_SUCCESS ) |
| 3551 | status = psa_mac_abort( operation ); |
| 3552 | else |
| 3553 | psa_mac_abort( operation ); |
| 3554 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3555 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3556 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3557 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3561 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3562 | /****************************************************************/ |
| 3563 | /* Asymmetric cryptography */ |
| 3564 | /****************************************************************/ |
| 3565 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3566 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3567 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3568 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3569 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3570 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3571 | size_t hash_length, |
| 3572 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3573 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3574 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3575 | 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] | 3576 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3577 | |
| 3578 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3579 | * parameters. Validate that it fits so that we don't risk an |
| 3580 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3581 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3582 | if( hash_length > UINT_MAX ) |
| 3583 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3584 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3585 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3586 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3587 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3588 | * must be correct. */ |
| 3589 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3590 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3591 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3592 | if( md_info == NULL ) |
| 3593 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3594 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3595 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3596 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3597 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3598 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3599 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3600 | /* PSS requires a hash internally. */ |
| 3601 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3602 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3603 | if( md_info == NULL ) |
| 3604 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3605 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3606 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3607 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3608 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3609 | } |
| 3610 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3611 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3612 | psa_algorithm_t alg, |
| 3613 | const uint8_t *hash, |
| 3614 | size_t hash_length, |
| 3615 | uint8_t *signature, |
| 3616 | size_t signature_size, |
| 3617 | size_t *signature_length ) |
| 3618 | { |
| 3619 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3620 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3621 | mbedtls_md_type_t md_alg; |
| 3622 | |
| 3623 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3624 | if( status != PSA_SUCCESS ) |
| 3625 | return( status ); |
| 3626 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3627 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3628 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3629 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3630 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3631 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3632 | { |
| 3633 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3634 | MBEDTLS_MD_NONE ); |
| 3635 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3636 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3637 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3638 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3639 | md_alg, |
| 3640 | (unsigned int) hash_length, |
| 3641 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3642 | signature ); |
| 3643 | } |
| 3644 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3645 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3646 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3647 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3648 | { |
| 3649 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3650 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3651 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3652 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3653 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3654 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3655 | (unsigned int) hash_length, |
| 3656 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3657 | signature ); |
| 3658 | } |
| 3659 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3660 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3661 | { |
| 3662 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3663 | } |
| 3664 | |
| 3665 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3666 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3667 | return( mbedtls_to_psa_error( ret ) ); |
| 3668 | } |
| 3669 | |
| 3670 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3671 | psa_algorithm_t alg, |
| 3672 | const uint8_t *hash, |
| 3673 | size_t hash_length, |
| 3674 | const uint8_t *signature, |
| 3675 | size_t signature_length ) |
| 3676 | { |
| 3677 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3678 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3679 | mbedtls_md_type_t md_alg; |
| 3680 | |
| 3681 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3682 | if( status != PSA_SUCCESS ) |
| 3683 | return( status ); |
| 3684 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3685 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3686 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3687 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3688 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3689 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3690 | { |
| 3691 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3692 | MBEDTLS_MD_NONE ); |
| 3693 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3694 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3695 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3696 | MBEDTLS_RSA_PUBLIC, |
| 3697 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3698 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3699 | hash, |
| 3700 | signature ); |
| 3701 | } |
| 3702 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3703 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3704 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3705 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3706 | { |
| 3707 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3708 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3709 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3710 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3711 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3712 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3713 | (unsigned int) hash_length, |
| 3714 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3715 | signature ); |
| 3716 | } |
| 3717 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3718 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3719 | { |
| 3720 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3721 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3722 | |
| 3723 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3724 | * the rest of the signature is invalid". This has little use in |
| 3725 | * practice and PSA doesn't report this distinction. */ |
| 3726 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3727 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3728 | return( mbedtls_to_psa_error( ret ) ); |
| 3729 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3730 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3731 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3732 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3733 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3734 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3735 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3736 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3737 | * (even though these functions don't modify it). */ |
| 3738 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3739 | psa_algorithm_t alg, |
| 3740 | const uint8_t *hash, |
| 3741 | size_t hash_length, |
| 3742 | uint8_t *signature, |
| 3743 | size_t signature_size, |
| 3744 | size_t *signature_length ) |
| 3745 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3746 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3747 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3748 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3749 | mbedtls_mpi_init( &r ); |
| 3750 | mbedtls_mpi_init( &s ); |
| 3751 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3752 | if( signature_size < 2 * curve_bytes ) |
| 3753 | { |
| 3754 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3755 | goto cleanup; |
| 3756 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3757 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3758 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3759 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3760 | { |
| 3761 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3762 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3763 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3764 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3765 | &ecp->d, hash, |
| 3766 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3767 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3768 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3769 | } |
| 3770 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3771 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3772 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3773 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3774 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3775 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3776 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3777 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3778 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3779 | |
| 3780 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3781 | signature, |
| 3782 | curve_bytes ) ); |
| 3783 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3784 | signature + curve_bytes, |
| 3785 | curve_bytes ) ); |
| 3786 | |
| 3787 | cleanup: |
| 3788 | mbedtls_mpi_free( &r ); |
| 3789 | mbedtls_mpi_free( &s ); |
| 3790 | if( ret == 0 ) |
| 3791 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3792 | return( mbedtls_to_psa_error( ret ) ); |
| 3793 | } |
| 3794 | |
| 3795 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3796 | const uint8_t *hash, |
| 3797 | size_t hash_length, |
| 3798 | const uint8_t *signature, |
| 3799 | size_t signature_length ) |
| 3800 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3801 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3802 | mbedtls_mpi r, s; |
| 3803 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3804 | mbedtls_mpi_init( &r ); |
| 3805 | mbedtls_mpi_init( &s ); |
| 3806 | |
| 3807 | if( signature_length != 2 * curve_bytes ) |
| 3808 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3809 | |
| 3810 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3811 | signature, |
| 3812 | curve_bytes ) ); |
| 3813 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3814 | signature + curve_bytes, |
| 3815 | curve_bytes ) ); |
| 3816 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3817 | /* Check whether the public part is loaded. If not, load it. */ |
| 3818 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3819 | { |
| 3820 | MBEDTLS_MPI_CHK( |
| 3821 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3822 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3825 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3826 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3827 | |
| 3828 | cleanup: |
| 3829 | mbedtls_mpi_free( &r ); |
| 3830 | mbedtls_mpi_free( &s ); |
| 3831 | return( mbedtls_to_psa_error( ret ) ); |
| 3832 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3833 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3834 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3835 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3836 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3837 | psa_algorithm_t alg, |
| 3838 | const uint8_t *hash, |
| 3839 | size_t hash_length, |
| 3840 | uint8_t *signature, |
| 3841 | size_t signature_size, |
| 3842 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3843 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3844 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3845 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3846 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3847 | |
| 3848 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3849 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3850 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3851 | * buffer can in principle be empty since it doesn't actually have |
| 3852 | * to be a hash.) */ |
| 3853 | if( signature_size == 0 ) |
| 3854 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3855 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3856 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3857 | PSA_KEY_USAGE_SIGN_HASH, |
| 3858 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3859 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3860 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3861 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3862 | { |
| 3863 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3864 | goto exit; |
| 3865 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3866 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3867 | /* Try any of the available accelerators first */ |
| 3868 | status = psa_driver_wrapper_sign_hash( slot, |
| 3869 | alg, |
| 3870 | hash, |
| 3871 | hash_length, |
| 3872 | signature, |
| 3873 | signature_size, |
| 3874 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3875 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3876 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3877 | goto exit; |
| 3878 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3879 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3880 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3881 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3882 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3883 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3884 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3885 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3886 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3887 | slot->key.data, |
| 3888 | slot->key.bytes, |
| 3889 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3890 | if( status != PSA_SUCCESS ) |
| 3891 | goto exit; |
| 3892 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3893 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3894 | alg, |
| 3895 | hash, hash_length, |
| 3896 | signature, signature_size, |
| 3897 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3898 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3899 | mbedtls_rsa_free( rsa ); |
| 3900 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3901 | } |
| 3902 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3903 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3904 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3905 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3906 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3907 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3908 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3909 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3910 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3911 | PSA_ALG_IS_ECDSA( alg ) |
| 3912 | #else |
| 3913 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3914 | #endif |
| 3915 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3916 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3917 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3918 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
| 3919 | slot->key.data, |
| 3920 | slot->key.bytes, |
| 3921 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3922 | if( status != PSA_SUCCESS ) |
| 3923 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3924 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3925 | alg, |
| 3926 | hash, hash_length, |
| 3927 | signature, signature_size, |
| 3928 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3929 | mbedtls_ecp_keypair_free( ecp ); |
| 3930 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3931 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3932 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3933 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3934 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3935 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3936 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3937 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3938 | } |
| 3939 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3940 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3941 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3942 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3943 | |
| 3944 | exit: |
| 3945 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3946 | * the trailing part on success) with something that isn't a valid mac |
| 3947 | * (barring an attack on the mac and deliberately-crafted input), |
| 3948 | * in case the caller doesn't check the return status properly. */ |
| 3949 | if( status == PSA_SUCCESS ) |
| 3950 | memset( signature + *signature_length, '!', |
| 3951 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3952 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3953 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3954 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3955 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3956 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3957 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3958 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3959 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3960 | } |
| 3961 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3962 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3963 | psa_algorithm_t alg, |
| 3964 | const uint8_t *hash, |
| 3965 | size_t hash_length, |
| 3966 | const uint8_t *signature, |
| 3967 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3968 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3969 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3970 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3971 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3972 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3973 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3974 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3975 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3976 | if( status != PSA_SUCCESS ) |
| 3977 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3978 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3979 | /* Try any of the available accelerators first */ |
| 3980 | status = psa_driver_wrapper_verify_hash( slot, |
| 3981 | alg, |
| 3982 | hash, |
| 3983 | hash_length, |
| 3984 | signature, |
| 3985 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3986 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3987 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3988 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3989 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3990 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3991 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3992 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3993 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3994 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3995 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 3996 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 3997 | slot->key.data, |
| 3998 | slot->key.bytes, |
| 3999 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4000 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4001 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4002 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4003 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4004 | alg, |
| 4005 | hash, hash_length, |
| 4006 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4007 | mbedtls_rsa_free( rsa ); |
| 4008 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4009 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4010 | } |
| 4011 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4012 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4013 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4014 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4015 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4016 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4017 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4018 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4019 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4020 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4021 | status = mbedtls_psa_ecp_load_representation( slot->attr.type, |
| 4022 | slot->key.data, |
| 4023 | slot->key.bytes, |
| 4024 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4025 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4026 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4027 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4028 | hash, hash_length, |
| 4029 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4030 | mbedtls_ecp_keypair_free( ecp ); |
| 4031 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4032 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4033 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4034 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4035 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4036 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4037 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4038 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4039 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4040 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4041 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4042 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4043 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4044 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4045 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4046 | |
| 4047 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4048 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4049 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4050 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4051 | } |
| 4052 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4053 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4054 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4055 | mbedtls_rsa_context *rsa ) |
| 4056 | { |
| 4057 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4058 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4059 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4060 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4061 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4062 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4063 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4064 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4065 | psa_algorithm_t alg, |
| 4066 | const uint8_t *input, |
| 4067 | size_t input_length, |
| 4068 | const uint8_t *salt, |
| 4069 | size_t salt_length, |
| 4070 | uint8_t *output, |
| 4071 | size_t output_size, |
| 4072 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4073 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4074 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4075 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4076 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4077 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4078 | (void) input; |
| 4079 | (void) input_length; |
| 4080 | (void) salt; |
| 4081 | (void) output; |
| 4082 | (void) output_size; |
| 4083 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4084 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4085 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4086 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4087 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4088 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4089 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4090 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4091 | if( status != PSA_SUCCESS ) |
| 4092 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4093 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4094 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4095 | { |
| 4096 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4097 | goto exit; |
| 4098 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4099 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4100 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4101 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4102 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4103 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4104 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4105 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 4106 | slot->key.data, |
| 4107 | slot->key.bytes, |
| 4108 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4109 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4110 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4111 | |
| 4112 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4113 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4114 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4115 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4116 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4117 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4118 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4119 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4120 | status = mbedtls_to_psa_error( |
| 4121 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4122 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4123 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4124 | MBEDTLS_RSA_PUBLIC, |
| 4125 | input_length, |
| 4126 | input, |
| 4127 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4128 | } |
| 4129 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4130 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4131 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4132 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4133 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4134 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4135 | status = mbedtls_to_psa_error( |
| 4136 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4137 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4138 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4139 | MBEDTLS_RSA_PUBLIC, |
| 4140 | salt, salt_length, |
| 4141 | input_length, |
| 4142 | input, |
| 4143 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4144 | } |
| 4145 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4146 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4147 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4148 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4149 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4150 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4151 | rsa_exit: |
| 4152 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4153 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4154 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4155 | mbedtls_rsa_free( rsa ); |
| 4156 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4157 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4158 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4159 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4160 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4161 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4162 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4163 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4164 | |
| 4165 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4166 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4167 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4168 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4169 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4170 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4171 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4172 | psa_algorithm_t alg, |
| 4173 | const uint8_t *input, |
| 4174 | size_t input_length, |
| 4175 | const uint8_t *salt, |
| 4176 | size_t salt_length, |
| 4177 | uint8_t *output, |
| 4178 | size_t output_size, |
| 4179 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4180 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4181 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4182 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4183 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4184 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4185 | (void) input; |
| 4186 | (void) input_length; |
| 4187 | (void) salt; |
| 4188 | (void) output; |
| 4189 | (void) output_size; |
| 4190 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4191 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4192 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4193 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4194 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4195 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4196 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4197 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4198 | if( status != PSA_SUCCESS ) |
| 4199 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4200 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4201 | { |
| 4202 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4203 | goto exit; |
| 4204 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4205 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4206 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4207 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4208 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4209 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4210 | mbedtls_rsa_context *rsa = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4211 | status = mbedtls_psa_rsa_load_representation( slot->attr.type, |
| 4212 | slot->key.data, |
| 4213 | slot->key.bytes, |
| 4214 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4215 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4216 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4217 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4218 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4219 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4220 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4221 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4222 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4223 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4224 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4225 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4226 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4227 | status = mbedtls_to_psa_error( |
| 4228 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4229 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4230 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4231 | MBEDTLS_RSA_PRIVATE, |
| 4232 | output_length, |
| 4233 | input, |
| 4234 | output, |
| 4235 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4236 | } |
| 4237 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4238 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4239 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4240 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4241 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4242 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4243 | status = mbedtls_to_psa_error( |
| 4244 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4245 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4246 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4247 | MBEDTLS_RSA_PRIVATE, |
| 4248 | salt, salt_length, |
| 4249 | output_length, |
| 4250 | input, |
| 4251 | output, |
| 4252 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4253 | } |
| 4254 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4255 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4256 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4257 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4258 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4259 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4260 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4261 | mbedtls_rsa_free( rsa ); |
| 4262 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4263 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4264 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4265 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4266 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4267 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4268 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4269 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4270 | |
| 4271 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4272 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4273 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4274 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4275 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4276 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4277 | |
| 4278 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4279 | /****************************************************************/ |
| 4280 | /* Symmetric cryptography */ |
| 4281 | /****************************************************************/ |
| 4282 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4283 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4284 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4285 | psa_algorithm_t alg, |
| 4286 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4287 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4288 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4289 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4290 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4291 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4292 | size_t key_bits; |
| 4293 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4294 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4295 | PSA_KEY_USAGE_ENCRYPT : |
| 4296 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4297 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4298 | /* A context must be freshly initialized before it can be set up. */ |
| 4299 | if( operation->alg != 0 ) |
| 4300 | return( PSA_ERROR_BAD_STATE ); |
| 4301 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4302 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4303 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4304 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4305 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4306 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4307 | 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] | 4308 | if( status != PSA_SUCCESS ) |
| 4309 | goto exit; |
| 4310 | |
| 4311 | /* Initialize the operation struct members, except for alg. The alg member |
| 4312 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4313 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4314 | operation->key_set = 0; |
| 4315 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4316 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4317 | operation->iv_size = 0; |
| 4318 | operation->block_size = 0; |
| 4319 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4320 | operation->iv_required = 0; |
| 4321 | else |
| 4322 | operation->iv_required = 1; |
| 4323 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4324 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4325 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4326 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4327 | slot, |
| 4328 | alg ); |
| 4329 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4330 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4331 | slot, |
| 4332 | alg ); |
| 4333 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4334 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4335 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4336 | /* Once the driver context is initialised, it needs to be freed using |
| 4337 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4338 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4339 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4340 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4341 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4342 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4343 | goto exit; |
| 4344 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4345 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4346 | * available for the given algorithm & key. */ |
| 4347 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4348 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4349 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4350 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4351 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4352 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4353 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4354 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4355 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4356 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4357 | 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] | 4358 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4359 | { |
| 4360 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4361 | goto exit; |
| 4362 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4363 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4364 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4365 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4366 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4367 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4368 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4369 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4370 | { |
| 4371 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4372 | uint8_t keys[24]; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4373 | memcpy( keys, slot->key.data, 16 ); |
| 4374 | memcpy( keys + 16, slot->key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4375 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4376 | keys, |
| 4377 | 192, cipher_operation ); |
| 4378 | } |
| 4379 | else |
| 4380 | #endif |
| 4381 | { |
| 4382 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4383 | slot->key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4384 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4385 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4386 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4387 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4388 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4389 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4390 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4391 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4392 | case PSA_ALG_CBC_NO_PADDING: |
| 4393 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4394 | MBEDTLS_PADDING_NONE ); |
| 4395 | break; |
| 4396 | case PSA_ALG_CBC_PKCS7: |
| 4397 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4398 | MBEDTLS_PADDING_PKCS7 ); |
| 4399 | break; |
| 4400 | default: |
| 4401 | /* The algorithm doesn't involve padding. */ |
| 4402 | ret = 0; |
| 4403 | break; |
| 4404 | } |
| 4405 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4406 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4407 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4408 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4409 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4410 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4411 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4412 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4413 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4414 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4415 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4416 | #if defined(MBEDTLS_CHACHA20_C) |
| 4417 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4418 | 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] | 4419 | operation->iv_size = 12; |
| 4420 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4421 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4422 | status = PSA_SUCCESS; |
| 4423 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4424 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4425 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4426 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4427 | if( status == PSA_SUCCESS ) |
| 4428 | { |
| 4429 | /* Update operation flags for both driver and software implementations */ |
| 4430 | operation->key_set = 1; |
| 4431 | } |
| 4432 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4433 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4434 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4435 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4436 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4437 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4438 | } |
| 4439 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4440 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4441 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4442 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4443 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4444 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4445 | } |
| 4446 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4447 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4448 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4449 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4450 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4451 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4452 | } |
| 4453 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4454 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4455 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4456 | size_t iv_size, |
| 4457 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4458 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4459 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4460 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4461 | if( operation->iv_set || ! operation->iv_required ) |
| 4462 | { |
| 4463 | return( PSA_ERROR_BAD_STATE ); |
| 4464 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4465 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4466 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4467 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4468 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4469 | iv, |
| 4470 | iv_size, |
| 4471 | iv_length ); |
| 4472 | goto exit; |
| 4473 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4474 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4475 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4476 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4477 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4478 | goto exit; |
| 4479 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4480 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4481 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4482 | if( ret != 0 ) |
| 4483 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4484 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4485 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4486 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4487 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4488 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4489 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4490 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4491 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4492 | if( status == PSA_SUCCESS ) |
| 4493 | operation->iv_set = 1; |
| 4494 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4495 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4496 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4497 | } |
| 4498 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4499 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4500 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4501 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4502 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4503 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4504 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4505 | if( operation->iv_set || ! operation->iv_required ) |
| 4506 | { |
| 4507 | return( PSA_ERROR_BAD_STATE ); |
| 4508 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4509 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4510 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4511 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4512 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4513 | iv, |
| 4514 | iv_length ); |
| 4515 | goto exit; |
| 4516 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4517 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4518 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4519 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4520 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4521 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4522 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4523 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4524 | status = mbedtls_to_psa_error( ret ); |
| 4525 | exit: |
| 4526 | if( status == PSA_SUCCESS ) |
| 4527 | operation->iv_set = 1; |
| 4528 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4529 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4530 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4531 | } |
| 4532 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4533 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4534 | * manual processing, since the PSA API is defined as being able to process |
| 4535 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4536 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4537 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4538 | mbedtls_cipher_context_t *ctx, |
| 4539 | const uint8_t *input, |
| 4540 | size_t input_length, |
| 4541 | uint8_t *output, |
| 4542 | size_t output_size, |
| 4543 | size_t *output_length ) |
| 4544 | { |
| 4545 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4546 | size_t block_size = ctx->cipher_info->block_size; |
| 4547 | size_t internal_output_length = 0; |
| 4548 | *output_length = 0; |
| 4549 | |
| 4550 | if( input_length == 0 ) |
| 4551 | { |
| 4552 | status = PSA_SUCCESS; |
| 4553 | goto exit; |
| 4554 | } |
| 4555 | |
| 4556 | if( ctx->unprocessed_len > 0 ) |
| 4557 | { |
| 4558 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4559 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4560 | |
| 4561 | if( input_length < bytes_to_copy ) |
| 4562 | bytes_to_copy = input_length; |
| 4563 | |
| 4564 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4565 | input, bytes_to_copy ); |
| 4566 | input_length -= bytes_to_copy; |
| 4567 | input += bytes_to_copy; |
| 4568 | ctx->unprocessed_len += bytes_to_copy; |
| 4569 | |
| 4570 | if( ctx->unprocessed_len == block_size ) |
| 4571 | { |
| 4572 | status = mbedtls_to_psa_error( |
| 4573 | mbedtls_cipher_update( ctx, |
| 4574 | ctx->unprocessed_data, |
| 4575 | block_size, |
| 4576 | output, &internal_output_length ) ); |
| 4577 | |
| 4578 | if( status != PSA_SUCCESS ) |
| 4579 | goto exit; |
| 4580 | |
| 4581 | output += internal_output_length; |
| 4582 | output_size -= internal_output_length; |
| 4583 | *output_length += internal_output_length; |
| 4584 | ctx->unprocessed_len = 0; |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | while( input_length >= block_size ) |
| 4589 | { |
| 4590 | /* Run all full blocks we have, one by one */ |
| 4591 | status = mbedtls_to_psa_error( |
| 4592 | mbedtls_cipher_update( ctx, input, |
| 4593 | block_size, |
| 4594 | output, &internal_output_length ) ); |
| 4595 | |
| 4596 | if( status != PSA_SUCCESS ) |
| 4597 | goto exit; |
| 4598 | |
| 4599 | input_length -= block_size; |
| 4600 | input += block_size; |
| 4601 | |
| 4602 | output += internal_output_length; |
| 4603 | output_size -= internal_output_length; |
| 4604 | *output_length += internal_output_length; |
| 4605 | } |
| 4606 | |
| 4607 | if( input_length > 0 ) |
| 4608 | { |
| 4609 | /* Save unprocessed bytes for later processing */ |
| 4610 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4611 | input, input_length ); |
| 4612 | ctx->unprocessed_len += input_length; |
| 4613 | } |
| 4614 | |
| 4615 | status = PSA_SUCCESS; |
| 4616 | |
| 4617 | exit: |
| 4618 | return( status ); |
| 4619 | } |
| 4620 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4621 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4622 | const uint8_t *input, |
| 4623 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4624 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4625 | size_t output_size, |
| 4626 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4627 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4628 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4629 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4630 | if( operation->alg == 0 ) |
| 4631 | { |
| 4632 | return( PSA_ERROR_BAD_STATE ); |
| 4633 | } |
| 4634 | if( operation->iv_required && ! operation->iv_set ) |
| 4635 | { |
| 4636 | return( PSA_ERROR_BAD_STATE ); |
| 4637 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4638 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4639 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4640 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4641 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4642 | input, |
| 4643 | input_length, |
| 4644 | output, |
| 4645 | output_size, |
| 4646 | output_length ); |
| 4647 | goto exit; |
| 4648 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4649 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4650 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4651 | { |
| 4652 | /* Take the unprocessed partial block left over from previous |
| 4653 | * update calls, if any, plus the input to this call. Remove |
| 4654 | * the last partial block, if any. You get the data that will be |
| 4655 | * output in this call. */ |
| 4656 | expected_output_size = |
| 4657 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4658 | / operation->block_size * operation->block_size; |
| 4659 | } |
| 4660 | else |
| 4661 | { |
| 4662 | expected_output_size = input_length; |
| 4663 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4664 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4665 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4666 | { |
| 4667 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4668 | goto exit; |
| 4669 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4670 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4671 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4672 | { |
| 4673 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4674 | * process a single block at a time in ECB mode. Abstract away that |
| 4675 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4676 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4677 | input, |
| 4678 | input_length, |
| 4679 | output, |
| 4680 | output_size, |
| 4681 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4682 | } |
| 4683 | else |
| 4684 | { |
| 4685 | status = mbedtls_to_psa_error( |
| 4686 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4687 | input_length, output, output_length ) ); |
| 4688 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4689 | exit: |
| 4690 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4691 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4692 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4693 | } |
| 4694 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4695 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4696 | uint8_t *output, |
| 4697 | size_t output_size, |
| 4698 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4699 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4700 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4701 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4702 | if( operation->alg == 0 ) |
| 4703 | { |
| 4704 | return( PSA_ERROR_BAD_STATE ); |
| 4705 | } |
| 4706 | if( operation->iv_required && ! operation->iv_set ) |
| 4707 | { |
| 4708 | return( PSA_ERROR_BAD_STATE ); |
| 4709 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4710 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4711 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4712 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4713 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4714 | output, |
| 4715 | output_size, |
| 4716 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4717 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4718 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4719 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4720 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4721 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4722 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4723 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4724 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4725 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4726 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4727 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4728 | } |
| 4729 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4730 | status = mbedtls_to_psa_error( |
| 4731 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4732 | temp_output_buffer, |
| 4733 | output_length ) ); |
| 4734 | if( status != PSA_SUCCESS ) |
| 4735 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4736 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4737 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4738 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4739 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4740 | memcpy( output, temp_output_buffer, *output_length ); |
| 4741 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4742 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4743 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4744 | exit: |
| 4745 | if( operation->mbedtls_in_use == 1 ) |
| 4746 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4747 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4748 | if( status == PSA_SUCCESS ) |
| 4749 | return( psa_cipher_abort( operation ) ); |
| 4750 | else |
| 4751 | { |
| 4752 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4753 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4754 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4755 | return( status ); |
| 4756 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4757 | } |
| 4758 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4759 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4760 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4761 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4762 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4763 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4764 | * in use. It's ok to call abort on such an object, and there's |
| 4765 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4766 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4767 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4768 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4769 | /* Sanity check (shouldn't happen: operation->alg should |
| 4770 | * always have been initialized to a valid value). */ |
| 4771 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4772 | return( PSA_ERROR_BAD_STATE ); |
| 4773 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4774 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4775 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4776 | else |
| 4777 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4778 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4779 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4780 | operation->key_set = 0; |
| 4781 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4782 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4783 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4784 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4785 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4786 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4787 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4788 | } |
| 4789 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4790 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4791 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4792 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4793 | /****************************************************************/ |
| 4794 | /* AEAD */ |
| 4795 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4797 | typedef struct |
| 4798 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4799 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4800 | const mbedtls_cipher_info_t *cipher_info; |
| 4801 | union |
| 4802 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4803 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4804 | #if defined(MBEDTLS_CCM_C) |
| 4805 | mbedtls_ccm_context ccm; |
| 4806 | #endif /* MBEDTLS_CCM_C */ |
| 4807 | #if defined(MBEDTLS_GCM_C) |
| 4808 | mbedtls_gcm_context gcm; |
| 4809 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4810 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4811 | mbedtls_chachapoly_context chachapoly; |
| 4812 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4813 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4814 | psa_algorithm_t core_alg; |
| 4815 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4816 | uint8_t tag_length; |
| 4817 | } aead_operation_t; |
| 4818 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4819 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4820 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4821 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4822 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4823 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4824 | { |
| 4825 | #if defined(MBEDTLS_CCM_C) |
| 4826 | case PSA_ALG_CCM: |
| 4827 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4828 | break; |
| 4829 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4830 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4831 | case PSA_ALG_GCM: |
| 4832 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4833 | break; |
| 4834 | #endif /* MBEDTLS_GCM_C */ |
| 4835 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4836 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4837 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4841 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4842 | psa_key_usage_t usage, |
| 4843 | psa_algorithm_t alg ) |
| 4844 | { |
| 4845 | psa_status_t status; |
| 4846 | size_t key_bits; |
| 4847 | mbedtls_cipher_id_t cipher_id; |
| 4848 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4849 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4850 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4851 | if( status != PSA_SUCCESS ) |
| 4852 | return( status ); |
| 4853 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4854 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4855 | |
| 4856 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4857 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4858 | &cipher_id ); |
| 4859 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4860 | { |
| 4861 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4862 | goto cleanup; |
| 4863 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4864 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4865 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4866 | { |
| 4867 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4868 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4869 | operation->core_alg = PSA_ALG_CCM; |
| 4870 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4871 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4872 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4873 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4874 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4875 | { |
| 4876 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4877 | goto cleanup; |
| 4878 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4879 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4880 | status = mbedtls_to_psa_error( |
| 4881 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4882 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4883 | (unsigned int) key_bits ) ); |
| 4884 | if( status != 0 ) |
| 4885 | goto cleanup; |
| 4886 | break; |
| 4887 | #endif /* MBEDTLS_CCM_C */ |
| 4888 | |
| 4889 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4890 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4891 | operation->core_alg = PSA_ALG_GCM; |
| 4892 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4893 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4894 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4895 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4896 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4897 | { |
| 4898 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4899 | goto cleanup; |
| 4900 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4901 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4902 | status = mbedtls_to_psa_error( |
| 4903 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4904 | operation->slot->key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4905 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4906 | if( status != 0 ) |
| 4907 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4908 | break; |
| 4909 | #endif /* MBEDTLS_GCM_C */ |
| 4910 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4911 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4912 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4913 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4914 | operation->full_tag_length = 16; |
| 4915 | /* We only support the default tag length. */ |
| 4916 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4917 | { |
| 4918 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4919 | goto cleanup; |
| 4920 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4921 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4922 | status = mbedtls_to_psa_error( |
| 4923 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 4924 | operation->slot->key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4925 | if( status != 0 ) |
| 4926 | goto cleanup; |
| 4927 | break; |
| 4928 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4929 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4930 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4931 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4932 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4933 | } |
| 4934 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4935 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4936 | { |
| 4937 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4938 | goto cleanup; |
| 4939 | } |
| 4940 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4941 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4942 | return( PSA_SUCCESS ); |
| 4943 | |
| 4944 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4945 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4946 | return( status ); |
| 4947 | } |
| 4948 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4949 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4950 | psa_algorithm_t alg, |
| 4951 | const uint8_t *nonce, |
| 4952 | size_t nonce_length, |
| 4953 | const uint8_t *additional_data, |
| 4954 | size_t additional_data_length, |
| 4955 | const uint8_t *plaintext, |
| 4956 | size_t plaintext_length, |
| 4957 | uint8_t *ciphertext, |
| 4958 | size_t ciphertext_size, |
| 4959 | size_t *ciphertext_length ) |
| 4960 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4961 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4962 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4963 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4964 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4965 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4966 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4967 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4968 | if( status != PSA_SUCCESS ) |
| 4969 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4970 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4971 | /* For all currently supported modes, the tag is at the end of the |
| 4972 | * ciphertext. */ |
| 4973 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4974 | { |
| 4975 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4976 | goto exit; |
| 4977 | } |
| 4978 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4979 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4980 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4981 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4982 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4983 | status = mbedtls_to_psa_error( |
| 4984 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4985 | MBEDTLS_GCM_ENCRYPT, |
| 4986 | plaintext_length, |
| 4987 | nonce, nonce_length, |
| 4988 | additional_data, additional_data_length, |
| 4989 | plaintext, ciphertext, |
| 4990 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4991 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4992 | else |
| 4993 | #endif /* MBEDTLS_GCM_C */ |
| 4994 | #if defined(MBEDTLS_CCM_C) |
| 4995 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4996 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4997 | status = mbedtls_to_psa_error( |
| 4998 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4999 | plaintext_length, |
| 5000 | nonce, nonce_length, |
| 5001 | additional_data, |
| 5002 | additional_data_length, |
| 5003 | plaintext, ciphertext, |
| 5004 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5005 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5006 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5007 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5008 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5009 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5010 | { |
| 5011 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5012 | { |
| 5013 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5014 | goto exit; |
| 5015 | } |
| 5016 | status = mbedtls_to_psa_error( |
| 5017 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 5018 | plaintext_length, |
| 5019 | nonce, |
| 5020 | additional_data, |
| 5021 | additional_data_length, |
| 5022 | plaintext, |
| 5023 | ciphertext, |
| 5024 | tag ) ); |
| 5025 | } |
| 5026 | else |
| 5027 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5028 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5029 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5030 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5031 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5032 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 5033 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5034 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5035 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5036 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5037 | if( status == PSA_SUCCESS ) |
| 5038 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 5039 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5040 | } |
| 5041 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5042 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 5043 | * followed by the tag. Return the length of the part preceding the tag in |
| 5044 | * *plaintext_length. This is the size of the plaintext in modes where |
| 5045 | * the encrypted data has the same size as the plaintext, such as |
| 5046 | * CCM and GCM. */ |
| 5047 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 5048 | const uint8_t *ciphertext, |
| 5049 | size_t ciphertext_length, |
| 5050 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5051 | const uint8_t **p_tag ) |
| 5052 | { |
| 5053 | size_t payload_length; |
| 5054 | if( tag_length > ciphertext_length ) |
| 5055 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5056 | payload_length = ciphertext_length - tag_length; |
| 5057 | if( payload_length > plaintext_size ) |
| 5058 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5059 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5060 | return( PSA_SUCCESS ); |
| 5061 | } |
| 5062 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5063 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5064 | psa_algorithm_t alg, |
| 5065 | const uint8_t *nonce, |
| 5066 | size_t nonce_length, |
| 5067 | const uint8_t *additional_data, |
| 5068 | size_t additional_data_length, |
| 5069 | const uint8_t *ciphertext, |
| 5070 | size_t ciphertext_length, |
| 5071 | uint8_t *plaintext, |
| 5072 | size_t plaintext_size, |
| 5073 | size_t *plaintext_length ) |
| 5074 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5075 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5076 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5077 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5078 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5079 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5080 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5081 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5082 | if( status != PSA_SUCCESS ) |
| 5083 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5084 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5085 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5086 | ciphertext, ciphertext_length, |
| 5087 | plaintext_size, &tag ); |
| 5088 | if( status != PSA_SUCCESS ) |
| 5089 | goto exit; |
| 5090 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5091 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5092 | if( operation.core_alg == PSA_ALG_GCM ) |
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_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5096 | ciphertext_length - operation.tag_length, |
| 5097 | nonce, nonce_length, |
| 5098 | additional_data, |
| 5099 | additional_data_length, |
| 5100 | tag, operation.tag_length, |
| 5101 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5102 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5103 | else |
| 5104 | #endif /* MBEDTLS_GCM_C */ |
| 5105 | #if defined(MBEDTLS_CCM_C) |
| 5106 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5107 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5108 | status = mbedtls_to_psa_error( |
| 5109 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5110 | ciphertext_length - operation.tag_length, |
| 5111 | nonce, nonce_length, |
| 5112 | additional_data, |
| 5113 | additional_data_length, |
| 5114 | ciphertext, plaintext, |
| 5115 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5116 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5117 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5118 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5119 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5120 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5121 | { |
| 5122 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5123 | { |
| 5124 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5125 | goto exit; |
| 5126 | } |
| 5127 | status = mbedtls_to_psa_error( |
| 5128 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5129 | ciphertext_length - operation.tag_length, |
| 5130 | nonce, |
| 5131 | additional_data, |
| 5132 | additional_data_length, |
| 5133 | tag, |
| 5134 | ciphertext, |
| 5135 | plaintext ) ); |
| 5136 | } |
| 5137 | else |
| 5138 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5139 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5140 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5141 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5142 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5143 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5144 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5145 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5146 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5147 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5148 | if( status == PSA_SUCCESS ) |
| 5149 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5150 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5151 | } |
| 5152 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5153 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5154 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5155 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5156 | /* Generators */ |
| 5157 | /****************************************************************/ |
| 5158 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5159 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 5160 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5161 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5162 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 5163 | #endif |
| 5164 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5165 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5166 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5167 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5168 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5169 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5170 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5171 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5172 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5173 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5174 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5175 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5176 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5177 | } |
| 5178 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5179 | 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] | 5180 | { |
| 5181 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5182 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5183 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5184 | { |
| 5185 | /* The object has (apparently) been initialized but it is not |
| 5186 | * in use. It's ok to call abort on such an object, and there's |
| 5187 | * nothing to do. */ |
| 5188 | } |
| 5189 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5190 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5191 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5192 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5193 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5194 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5195 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5196 | else |
| 5197 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5198 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5199 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5200 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5201 | /* 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] | 5202 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5203 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5204 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5205 | { |
| 5206 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5207 | operation->ctx.tls12_prf.seed_length ); |
| 5208 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5209 | } |
| 5210 | |
| 5211 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5212 | { |
| 5213 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5214 | operation->ctx.tls12_prf.label_length ); |
| 5215 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5216 | } |
| 5217 | |
| 5218 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5219 | |
| 5220 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5221 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5222 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5223 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5224 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5225 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5226 | { |
| 5227 | status = PSA_ERROR_BAD_STATE; |
| 5228 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5229 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5230 | return( status ); |
| 5231 | } |
| 5232 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5233 | 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] | 5234 | size_t *capacity) |
| 5235 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5236 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5237 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5238 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5239 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5240 | } |
| 5241 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5242 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5243 | return( PSA_SUCCESS ); |
| 5244 | } |
| 5245 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5246 | 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] | 5247 | size_t capacity ) |
| 5248 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5249 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5250 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5251 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5252 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5253 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5254 | return( PSA_SUCCESS ); |
| 5255 | } |
| 5256 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5257 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5258 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5259 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5260 | 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] | 5261 | psa_algorithm_t hash_alg, |
| 5262 | uint8_t *output, |
| 5263 | size_t output_length ) |
| 5264 | { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5265 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5266 | psa_status_t status; |
| 5267 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5268 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5269 | return( PSA_ERROR_BAD_STATE ); |
| 5270 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5271 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5272 | while( output_length != 0 ) |
| 5273 | { |
| 5274 | /* Copy what remains of the current block */ |
| 5275 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5276 | if( n > output_length ) |
| 5277 | n = (uint8_t) output_length; |
| 5278 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5279 | output += n; |
| 5280 | output_length -= n; |
| 5281 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5282 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5283 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5284 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5285 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5286 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5287 | * object was corrupted or if this function is called directly |
| 5288 | * inside the library. */ |
| 5289 | if( hkdf->block_number == 0xff ) |
| 5290 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5291 | |
| 5292 | /* We need a new block */ |
| 5293 | ++hkdf->block_number; |
| 5294 | hkdf->offset_in_block = 0; |
| 5295 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5296 | hkdf->prk, hash_length, |
| 5297 | hash_alg ); |
| 5298 | if( status != PSA_SUCCESS ) |
| 5299 | return( status ); |
| 5300 | if( hkdf->block_number != 1 ) |
| 5301 | { |
| 5302 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5303 | hkdf->output_block, |
| 5304 | hash_length ); |
| 5305 | if( status != PSA_SUCCESS ) |
| 5306 | return( status ); |
| 5307 | } |
| 5308 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5309 | hkdf->info, |
| 5310 | hkdf->info_length ); |
| 5311 | if( status != PSA_SUCCESS ) |
| 5312 | return( status ); |
| 5313 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5314 | &hkdf->block_number, 1 ); |
| 5315 | if( status != PSA_SUCCESS ) |
| 5316 | return( status ); |
| 5317 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5318 | hkdf->output_block, |
| 5319 | sizeof( hkdf->output_block ) ); |
| 5320 | if( status != PSA_SUCCESS ) |
| 5321 | return( status ); |
| 5322 | } |
| 5323 | |
| 5324 | return( PSA_SUCCESS ); |
| 5325 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5326 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5327 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5328 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5329 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5330 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5331 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5332 | psa_algorithm_t alg ) |
| 5333 | { |
| 5334 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5335 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5336 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5337 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5338 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5339 | /* We can't be wanting more output after block 0xff, otherwise |
| 5340 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5341 | * prevented this call. It could happen only if the operation |
| 5342 | * object was corrupted or if this function is called directly |
| 5343 | * inside the library. */ |
| 5344 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5345 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5346 | |
| 5347 | /* We need a new block */ |
| 5348 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5349 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5350 | |
| 5351 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5352 | * |
| 5353 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5354 | * |
| 5355 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5356 | * HMAC_hash(secret, A(2) + seed) + |
| 5357 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5358 | * |
| 5359 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5360 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5361 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5362 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5363 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5364 | * is currently extracted as `output_block` and where i is |
| 5365 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5366 | */ |
| 5367 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5368 | /* Save the hash context before using it, to preserve the hash state with |
| 5369 | * only the inner padding in it. We need this, because inner padding depends |
| 5370 | * on the key (secret in the RFC's terminology). */ |
| 5371 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5372 | if( status != PSA_SUCCESS ) |
| 5373 | goto cleanup; |
| 5374 | |
| 5375 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5376 | if( tls12_prf->block_number == 1 ) |
| 5377 | { |
| 5378 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5379 | * the variable seed and in this instance means it in the context of the |
| 5380 | * P_hash function, where seed = label + seed.) */ |
| 5381 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5382 | tls12_prf->label, tls12_prf->label_length ); |
| 5383 | if( status != PSA_SUCCESS ) |
| 5384 | goto cleanup; |
| 5385 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5386 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5387 | if( status != PSA_SUCCESS ) |
| 5388 | goto cleanup; |
| 5389 | } |
| 5390 | else |
| 5391 | { |
| 5392 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5393 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5394 | tls12_prf->Ai, hash_length ); |
| 5395 | if( status != PSA_SUCCESS ) |
| 5396 | goto cleanup; |
| 5397 | } |
| 5398 | |
| 5399 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5400 | tls12_prf->Ai, hash_length ); |
| 5401 | if( status != PSA_SUCCESS ) |
| 5402 | goto cleanup; |
| 5403 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5404 | if( status != PSA_SUCCESS ) |
| 5405 | goto cleanup; |
| 5406 | |
| 5407 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5408 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5409 | tls12_prf->Ai, hash_length ); |
| 5410 | if( status != PSA_SUCCESS ) |
| 5411 | goto cleanup; |
| 5412 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5413 | tls12_prf->label, tls12_prf->label_length ); |
| 5414 | if( status != PSA_SUCCESS ) |
| 5415 | goto cleanup; |
| 5416 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5417 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5418 | if( status != PSA_SUCCESS ) |
| 5419 | goto cleanup; |
| 5420 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5421 | tls12_prf->output_block, hash_length ); |
| 5422 | if( status != PSA_SUCCESS ) |
| 5423 | goto cleanup; |
| 5424 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5425 | if( status != PSA_SUCCESS ) |
| 5426 | goto cleanup; |
| 5427 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5428 | |
| 5429 | cleanup: |
| 5430 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5431 | cleanup_status = psa_hash_abort( &backup ); |
| 5432 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5433 | status = cleanup_status; |
| 5434 | |
| 5435 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5436 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5437 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5438 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5439 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5440 | psa_algorithm_t alg, |
| 5441 | uint8_t *output, |
| 5442 | size_t output_length ) |
| 5443 | { |
| 5444 | 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] | 5445 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5446 | psa_status_t status; |
| 5447 | uint8_t offset, length; |
| 5448 | |
| 5449 | while( output_length != 0 ) |
| 5450 | { |
| 5451 | /* Check if we have fully processed the current block. */ |
| 5452 | if( tls12_prf->left_in_block == 0 ) |
| 5453 | { |
| 5454 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5455 | alg ); |
| 5456 | if( status != PSA_SUCCESS ) |
| 5457 | return( status ); |
| 5458 | |
| 5459 | continue; |
| 5460 | } |
| 5461 | |
| 5462 | if( tls12_prf->left_in_block > output_length ) |
| 5463 | length = (uint8_t) output_length; |
| 5464 | else |
| 5465 | length = tls12_prf->left_in_block; |
| 5466 | |
| 5467 | offset = hash_length - tls12_prf->left_in_block; |
| 5468 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5469 | output += length; |
| 5470 | output_length -= length; |
| 5471 | tls12_prf->left_in_block -= length; |
| 5472 | } |
| 5473 | |
| 5474 | return( PSA_SUCCESS ); |
| 5475 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5476 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5477 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5478 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5479 | psa_status_t psa_key_derivation_output_bytes( |
| 5480 | psa_key_derivation_operation_t *operation, |
| 5481 | uint8_t *output, |
| 5482 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5483 | { |
| 5484 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5485 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5486 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5487 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5488 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5489 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5490 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5493 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5494 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5495 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5496 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5497 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5498 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5499 | goto exit; |
| 5500 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5501 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5502 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5503 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5504 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5505 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5506 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5507 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5508 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5509 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5510 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5511 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5512 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5513 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5514 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5515 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5516 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5517 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5518 | output, output_length ); |
| 5519 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5520 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5521 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5522 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5523 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5524 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5525 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5526 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5527 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5528 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5529 | output_length ); |
| 5530 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5531 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5532 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5533 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5534 | { |
| 5535 | return( PSA_ERROR_BAD_STATE ); |
| 5536 | } |
| 5537 | |
| 5538 | exit: |
| 5539 | if( status != PSA_SUCCESS ) |
| 5540 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5541 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5542 | * This allows us to differentiate between exhausted operations and |
| 5543 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5544 | * operations. */ |
| 5545 | psa_algorithm_t alg = operation->alg; |
| 5546 | psa_key_derivation_abort( operation ); |
| 5547 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5548 | memset( output, '!', output_length ); |
| 5549 | } |
| 5550 | return( status ); |
| 5551 | } |
| 5552 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5553 | #if defined(MBEDTLS_DES_C) |
| 5554 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5555 | { |
| 5556 | if( data_size >= 8 ) |
| 5557 | mbedtls_des_key_set_parity( data ); |
| 5558 | if( data_size >= 16 ) |
| 5559 | mbedtls_des_key_set_parity( data + 8 ); |
| 5560 | if( data_size >= 24 ) |
| 5561 | mbedtls_des_key_set_parity( data + 16 ); |
| 5562 | } |
| 5563 | #endif /* MBEDTLS_DES_C */ |
| 5564 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5565 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5566 | psa_key_slot_t *slot, |
| 5567 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5568 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5569 | { |
| 5570 | uint8_t *data = NULL; |
| 5571 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5572 | psa_status_t status; |
| 5573 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5574 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5575 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5576 | if( bits % 8 != 0 ) |
| 5577 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5578 | data = mbedtls_calloc( 1, bytes ); |
| 5579 | if( data == NULL ) |
| 5580 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5581 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5582 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5583 | if( status != PSA_SUCCESS ) |
| 5584 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5585 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5586 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5587 | psa_des_set_key_parity( data, bytes ); |
| 5588 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5589 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5590 | |
| 5591 | exit: |
| 5592 | mbedtls_free( data ); |
| 5593 | return( status ); |
| 5594 | } |
| 5595 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5596 | 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] | 5597 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5598 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5599 | { |
| 5600 | psa_status_t status; |
| 5601 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5602 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5603 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5604 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5605 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5606 | /* Reject any attempt to create a zero-length key so that we don't |
| 5607 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5608 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5609 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5610 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5611 | if( ! operation->can_output_key ) |
| 5612 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5613 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5614 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5615 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5616 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5617 | if( driver != NULL ) |
| 5618 | { |
| 5619 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5620 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5621 | } |
| 5622 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5623 | if( status == PSA_SUCCESS ) |
| 5624 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5625 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5626 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5627 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5628 | } |
| 5629 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5630 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5631 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5632 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5633 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5634 | return( status ); |
| 5635 | } |
| 5636 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5637 | |
| 5638 | |
| 5639 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5640 | /* Key derivation */ |
| 5641 | /****************************************************************/ |
| 5642 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5643 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5644 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5645 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5646 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5647 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5648 | int is_kdf_alg_supported; |
| 5649 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5650 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5651 | * initialisers for this union leave some bytes unspecified.) */ |
| 5652 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5653 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5654 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5655 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5656 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5657 | is_kdf_alg_supported = 1; |
| 5658 | else |
| 5659 | #endif |
| 5660 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5661 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5662 | is_kdf_alg_supported = 1; |
| 5663 | else |
| 5664 | #endif |
| 5665 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5666 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5667 | is_kdf_alg_supported = 1; |
| 5668 | else |
| 5669 | #endif |
| 5670 | is_kdf_alg_supported = 0; |
| 5671 | |
| 5672 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5673 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5674 | 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] | 5675 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5676 | if( hash_size == 0 ) |
| 5677 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5678 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5679 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5680 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5681 | { |
| 5682 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5683 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5684 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5685 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5686 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5687 | |
| 5688 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5689 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5690 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5691 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5692 | 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] | 5693 | psa_algorithm_t alg ) |
| 5694 | { |
| 5695 | psa_status_t status; |
| 5696 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5697 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5698 | return( PSA_ERROR_BAD_STATE ); |
| 5699 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5700 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5701 | return( PSA_ERROR_INVALID_ARGUMENT ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5702 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5703 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5704 | { |
| 5705 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5706 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5707 | } |
| 5708 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5709 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5710 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5711 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5712 | #endif |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5713 | else |
| 5714 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5715 | |
| 5716 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5717 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5718 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5719 | } |
| 5720 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5721 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5722 | 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] | 5723 | psa_algorithm_t hash_alg, |
| 5724 | psa_key_derivation_step_t step, |
| 5725 | const uint8_t *data, |
| 5726 | size_t data_length ) |
| 5727 | { |
| 5728 | psa_status_t status; |
| 5729 | switch( step ) |
| 5730 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5731 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5732 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5733 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5734 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5735 | data, data_length, |
| 5736 | hash_alg ); |
| 5737 | if( status != PSA_SUCCESS ) |
| 5738 | return( status ); |
| 5739 | hkdf->state = HKDF_STATE_STARTED; |
| 5740 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5741 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5742 | /* If no salt was provided, use an empty salt. */ |
| 5743 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5744 | { |
| 5745 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5746 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5747 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5748 | if( status != PSA_SUCCESS ) |
| 5749 | return( status ); |
| 5750 | hkdf->state = HKDF_STATE_STARTED; |
| 5751 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5752 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5753 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5754 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5755 | data, data_length ); |
| 5756 | if( status != PSA_SUCCESS ) |
| 5757 | return( status ); |
| 5758 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5759 | hkdf->prk, |
| 5760 | sizeof( hkdf->prk ) ); |
| 5761 | if( status != PSA_SUCCESS ) |
| 5762 | return( status ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5763 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5764 | hkdf->block_number = 0; |
| 5765 | hkdf->state = HKDF_STATE_KEYED; |
| 5766 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5767 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5768 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5769 | return( PSA_ERROR_BAD_STATE ); |
| 5770 | if( hkdf->info_set ) |
| 5771 | return( PSA_ERROR_BAD_STATE ); |
| 5772 | hkdf->info_length = data_length; |
| 5773 | if( data_length != 0 ) |
| 5774 | { |
| 5775 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5776 | if( hkdf->info == NULL ) |
| 5777 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5778 | memcpy( hkdf->info, data, data_length ); |
| 5779 | } |
| 5780 | hkdf->info_set = 1; |
| 5781 | return( PSA_SUCCESS ); |
| 5782 | default: |
| 5783 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5784 | } |
| 5785 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5786 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5787 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5788 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5789 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5790 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5791 | const uint8_t *data, |
| 5792 | size_t data_length ) |
| 5793 | { |
| 5794 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5795 | return( PSA_ERROR_BAD_STATE ); |
| 5796 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5797 | if( data_length != 0 ) |
| 5798 | { |
| 5799 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5800 | if( prf->seed == NULL ) |
| 5801 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5802 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5803 | memcpy( prf->seed, data, data_length ); |
| 5804 | prf->seed_length = data_length; |
| 5805 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5806 | |
| 5807 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5808 | |
| 5809 | return( PSA_SUCCESS ); |
| 5810 | } |
| 5811 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5812 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5813 | psa_algorithm_t hash_alg, |
| 5814 | const uint8_t *data, |
| 5815 | size_t data_length ) |
| 5816 | { |
| 5817 | psa_status_t status; |
| 5818 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5819 | return( PSA_ERROR_BAD_STATE ); |
| 5820 | |
| 5821 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5822 | if( status != PSA_SUCCESS ) |
| 5823 | return( status ); |
| 5824 | |
| 5825 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5826 | |
| 5827 | return( PSA_SUCCESS ); |
| 5828 | } |
| 5829 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5830 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5831 | const uint8_t *data, |
| 5832 | size_t data_length ) |
| 5833 | { |
| 5834 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5835 | return( PSA_ERROR_BAD_STATE ); |
| 5836 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5837 | if( data_length != 0 ) |
| 5838 | { |
| 5839 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5840 | if( prf->label == NULL ) |
| 5841 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5842 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5843 | memcpy( prf->label, data, data_length ); |
| 5844 | prf->label_length = data_length; |
| 5845 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5846 | |
| 5847 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5848 | |
| 5849 | return( PSA_SUCCESS ); |
| 5850 | } |
| 5851 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5852 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5853 | psa_algorithm_t hash_alg, |
| 5854 | psa_key_derivation_step_t step, |
| 5855 | const uint8_t *data, |
| 5856 | size_t data_length ) |
| 5857 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5858 | switch( step ) |
| 5859 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5860 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5861 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5862 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5863 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5864 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5865 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5866 | default: |
| 5867 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5868 | } |
| 5869 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5870 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5871 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5872 | |
| 5873 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5874 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5875 | psa_tls12_prf_key_derivation_t *prf, |
| 5876 | psa_algorithm_t hash_alg, |
| 5877 | const uint8_t *data, |
| 5878 | size_t data_length ) |
| 5879 | { |
| 5880 | psa_status_t status; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5881 | 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] | 5882 | uint8_t *cur = pms; |
| 5883 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5884 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5885 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5886 | |
| 5887 | /* Quoting RFC 4279, Section 2: |
| 5888 | * |
| 5889 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5890 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5891 | * uint16 with the value N, and the PSK itself. |
| 5892 | */ |
| 5893 | |
| 5894 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5895 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5896 | memset( cur, 0, data_length ); |
| 5897 | cur += data_length; |
| 5898 | *cur++ = pms[0]; |
| 5899 | *cur++ = pms[1]; |
| 5900 | memcpy( cur, data, data_length ); |
| 5901 | cur += data_length; |
| 5902 | |
| 5903 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5904 | |
| 5905 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5906 | return( status ); |
| 5907 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5908 | |
| 5909 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5910 | psa_tls12_prf_key_derivation_t *prf, |
| 5911 | psa_algorithm_t hash_alg, |
| 5912 | psa_key_derivation_step_t step, |
| 5913 | const uint8_t *data, |
| 5914 | size_t data_length ) |
| 5915 | { |
| 5916 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5917 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5918 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5919 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5920 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5921 | |
| 5922 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5923 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5924 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5925 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5926 | /** Check whether the given key type is acceptable for the given |
| 5927 | * input step of a key derivation. |
| 5928 | * |
| 5929 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5930 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5931 | * Both secret and non-secret inputs can alternatively have the type |
| 5932 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5933 | * that the input was passed as a buffer rather than via a key object. |
| 5934 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5935 | static int psa_key_derivation_check_input_type( |
| 5936 | psa_key_derivation_step_t step, |
| 5937 | psa_key_type_t key_type ) |
| 5938 | { |
| 5939 | switch( step ) |
| 5940 | { |
| 5941 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5942 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5943 | return( PSA_SUCCESS ); |
| 5944 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5945 | return( PSA_SUCCESS ); |
| 5946 | break; |
| 5947 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5948 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5949 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5950 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5951 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5952 | return( PSA_SUCCESS ); |
| 5953 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5954 | return( PSA_SUCCESS ); |
| 5955 | break; |
| 5956 | } |
| 5957 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5958 | } |
| 5959 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5960 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5961 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5962 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5963 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5964 | const uint8_t *data, |
| 5965 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5966 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5967 | psa_status_t status; |
| 5968 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5969 | |
| 5970 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5971 | if( status != PSA_SUCCESS ) |
| 5972 | goto exit; |
| 5973 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5974 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5975 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5976 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5977 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5978 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5979 | step, data, data_length ); |
| 5980 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5981 | else |
| 5982 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5983 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5984 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5985 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5986 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5987 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5988 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5989 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5990 | else |
| 5991 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5992 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5993 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5994 | { |
| 5995 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5996 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5997 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5998 | } |
| 5999 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6000 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6001 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6002 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6003 | return( PSA_ERROR_BAD_STATE ); |
| 6004 | } |
| 6005 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6006 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6007 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6008 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6009 | return( status ); |
| 6010 | } |
| 6011 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6012 | psa_status_t psa_key_derivation_input_bytes( |
| 6013 | psa_key_derivation_operation_t *operation, |
| 6014 | psa_key_derivation_step_t step, |
| 6015 | const uint8_t *data, |
| 6016 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6017 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6018 | return( psa_key_derivation_input_internal( operation, step, |
| 6019 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 6020 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6021 | } |
| 6022 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6023 | psa_status_t psa_key_derivation_input_key( |
| 6024 | psa_key_derivation_operation_t *operation, |
| 6025 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6026 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6027 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6028 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6029 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6030 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6031 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6032 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6033 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6034 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6035 | { |
| 6036 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6037 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6038 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6039 | |
| 6040 | /* Passing a key object as a SECRET input unlocks the permission |
| 6041 | * to output to a key object. */ |
| 6042 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6043 | operation->can_output_key = 1; |
| 6044 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6045 | status = psa_key_derivation_input_internal( operation, |
| 6046 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6047 | slot->key.data, |
| 6048 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6049 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6050 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6051 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6052 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6053 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6054 | |
| 6055 | |
| 6056 | |
| 6057 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6058 | /* Key agreement */ |
| 6059 | /****************************************************************/ |
| 6060 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6061 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6062 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 6063 | size_t peer_key_length, |
| 6064 | const mbedtls_ecp_keypair *our_key, |
| 6065 | uint8_t *shared_secret, |
| 6066 | size_t shared_secret_size, |
| 6067 | size_t *shared_secret_length ) |
| 6068 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 6069 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6070 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6071 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6072 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6073 | 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] | 6074 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6075 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 6076 | status = mbedtls_psa_ecp_load_representation( |
| 6077 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 6078 | peer_key, |
| 6079 | peer_key_length, |
| 6080 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6081 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6082 | goto exit; |
| 6083 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6084 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6085 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6086 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6087 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6088 | status = mbedtls_to_psa_error( |
| 6089 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 6090 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6091 | goto exit; |
| 6092 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6093 | status = mbedtls_to_psa_error( |
| 6094 | mbedtls_ecdh_calc_secret( &ecdh, |
| 6095 | shared_secret_length, |
| 6096 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6097 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6098 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6099 | if( status != PSA_SUCCESS ) |
| 6100 | goto exit; |
| 6101 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 6102 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6103 | |
| 6104 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 6105 | if( status != PSA_SUCCESS ) |
| 6106 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6107 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6108 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6109 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6110 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6111 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6112 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6113 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6114 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6115 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6116 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6117 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6118 | psa_key_slot_t *private_key, |
| 6119 | const uint8_t *peer_key, |
| 6120 | size_t peer_key_length, |
| 6121 | uint8_t *shared_secret, |
| 6122 | size_t shared_secret_size, |
| 6123 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6124 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6125 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6126 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6127 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6128 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6129 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6130 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6131 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 6132 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 6133 | private_key->attr.type, |
| 6134 | private_key->key.data, |
| 6135 | private_key->key.bytes, |
| 6136 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6137 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6138 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6139 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6140 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6141 | shared_secret, shared_secret_size, |
| 6142 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6143 | mbedtls_ecp_keypair_free( ecp ); |
| 6144 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6145 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6146 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6147 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6148 | (void) private_key; |
| 6149 | (void) peer_key; |
| 6150 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6151 | (void) shared_secret; |
| 6152 | (void) shared_secret_size; |
| 6153 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6154 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6155 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6156 | } |
| 6157 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6158 | /* 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] | 6159 | * to potentially free embedded data structures and wipe confidential data. |
| 6160 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6161 | 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] | 6162 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6163 | psa_key_slot_t *private_key, |
| 6164 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6165 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6166 | { |
| 6167 | psa_status_t status; |
| 6168 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6169 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6170 | 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] | 6171 | |
| 6172 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6173 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6174 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6175 | private_key, |
| 6176 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6177 | shared_secret, |
| 6178 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6179 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6180 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6181 | goto exit; |
| 6182 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6183 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6184 | * the shared secret. A shared secret is permitted wherever a key |
| 6185 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6186 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6187 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6188 | shared_secret, |
| 6189 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6190 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6191 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6192 | return( status ); |
| 6193 | } |
| 6194 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6195 | 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] | 6196 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6197 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6198 | const uint8_t *peer_key, |
| 6199 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6200 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6201 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6202 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6203 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6204 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6205 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6206 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6207 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6208 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6209 | if( status != PSA_SUCCESS ) |
| 6210 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6211 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6212 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6213 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6214 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6215 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6216 | else |
| 6217 | { |
| 6218 | /* If a private key has been added as SECRET, we allow the derived |
| 6219 | * key material to be used as a key in PSA Crypto. */ |
| 6220 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6221 | operation->can_output_key = 1; |
| 6222 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6223 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6224 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6225 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6226 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6227 | } |
| 6228 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6229 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6230 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6231 | const uint8_t *peer_key, |
| 6232 | size_t peer_key_length, |
| 6233 | uint8_t *output, |
| 6234 | size_t output_size, |
| 6235 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6236 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6237 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6238 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6239 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6240 | |
| 6241 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6242 | { |
| 6243 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6244 | goto exit; |
| 6245 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6246 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6247 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6248 | if( status != PSA_SUCCESS ) |
| 6249 | goto exit; |
| 6250 | |
| 6251 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6252 | peer_key, peer_key_length, |
| 6253 | output, output_size, |
| 6254 | output_length ); |
| 6255 | |
| 6256 | exit: |
| 6257 | if( status != PSA_SUCCESS ) |
| 6258 | { |
| 6259 | /* If an error happens and is not handled properly, the output |
| 6260 | * may be used as a key to protect sensitive data. Arrange for such |
| 6261 | * a key to be random, which is likely to result in decryption or |
| 6262 | * verification errors. This is better than filling the buffer with |
| 6263 | * some constant data such as zeros, which would result in the data |
| 6264 | * being protected with a reproducible, easily knowable key. |
| 6265 | */ |
| 6266 | psa_generate_random( output, output_size ); |
| 6267 | *output_length = output_size; |
| 6268 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6269 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6270 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6271 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6272 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6273 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6274 | |
| 6275 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6276 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6277 | /****************************************************************/ |
| 6278 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6279 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6280 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6281 | /** Initialize the PSA random generator. |
| 6282 | */ |
| 6283 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6284 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6285 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6286 | memset( rng, 0, sizeof( *rng ) ); |
| 6287 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6288 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6289 | /* Set default configuration if |
| 6290 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6291 | if( rng->entropy_init == NULL ) |
| 6292 | rng->entropy_init = mbedtls_entropy_init; |
| 6293 | if( rng->entropy_free == NULL ) |
| 6294 | rng->entropy_free = mbedtls_entropy_free; |
| 6295 | |
| 6296 | rng->entropy_init( &rng->entropy ); |
| 6297 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6298 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6299 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6300 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6301 | mbedtls_entropy_add_source( &rng->entropy, |
| 6302 | mbedtls_nv_seed_poll, NULL, |
| 6303 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6304 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6305 | #endif |
| 6306 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6307 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6308 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | /** Deinitialize the PSA random generator. |
| 6312 | */ |
| 6313 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6314 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6315 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6316 | memset( rng, 0, sizeof( *rng ) ); |
| 6317 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6318 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6319 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6320 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6321 | } |
| 6322 | |
| 6323 | /** Seed the PSA random generator. |
| 6324 | */ |
| 6325 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6326 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6327 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6328 | /* Do nothing: the external RNG seeds itself. */ |
| 6329 | (void) rng; |
| 6330 | return( PSA_SUCCESS ); |
| 6331 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6332 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6333 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6334 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6335 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6336 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6337 | } |
| 6338 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6339 | psa_status_t psa_generate_random( uint8_t *output, |
| 6340 | size_t output_size ) |
| 6341 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6342 | GUARD_MODULE_INITIALIZED; |
| 6343 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6344 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6345 | |
| 6346 | size_t output_length = 0; |
| 6347 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6348 | output, output_size, |
| 6349 | &output_length ); |
| 6350 | if( status != PSA_SUCCESS ) |
| 6351 | return( status ); |
| 6352 | /* Breaking up a request into smaller chunks is currently not supported |
| 6353 | * for the extrernal RNG interface. */ |
| 6354 | if( output_length != output_size ) |
| 6355 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6356 | return( PSA_SUCCESS ); |
| 6357 | |
| 6358 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6359 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6360 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6361 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6362 | size_t request_size = |
| 6363 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6364 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6365 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6366 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6367 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6368 | if( ret != 0 ) |
| 6369 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6370 | output_size -= request_size; |
| 6371 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6372 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6373 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6374 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6375 | } |
| 6376 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6377 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6378 | * |
| 6379 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6380 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6381 | * PSA API doesn't support passing an explicit state. |
| 6382 | * |
| 6383 | * In the non-external case, psa_generate_random() calls an |
| 6384 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6385 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6386 | * instead of doing this back-and-forth between the PSA API and the |
| 6387 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6388 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6389 | */ |
| 6390 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6391 | int mbedtls_psa_get_random( void *p_rng, |
| 6392 | unsigned char *output, |
| 6393 | size_t output_size ) |
| 6394 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6395 | /* This function takes a pointer to the RNG state because that's what |
| 6396 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6397 | * its own state internally and doesn't let the caller access that state. |
| 6398 | * So we just ignore the state parameter, and in practice we'll pass |
| 6399 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6400 | (void) p_rng; |
| 6401 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6402 | if( status == PSA_SUCCESS ) |
| 6403 | return( 0 ); |
| 6404 | else |
| 6405 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6406 | } |
| 6407 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6408 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6409 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6410 | #include "mbedtls/entropy_poll.h" |
| 6411 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6412 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6413 | size_t seed_size ) |
| 6414 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6415 | if( global_data.initialized ) |
| 6416 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6417 | |
| 6418 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6419 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6420 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6421 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6422 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6423 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6424 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6425 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6426 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6427 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6428 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6429 | size_t domain_parameters_size, |
| 6430 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6431 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6432 | size_t i; |
| 6433 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6434 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6435 | if( domain_parameters_size == 0 ) |
| 6436 | { |
| 6437 | *exponent = 65537; |
| 6438 | return( PSA_SUCCESS ); |
| 6439 | } |
| 6440 | |
| 6441 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6442 | * support values that fit in a 32-bit integer, which is larger than |
| 6443 | * int on just about every platform anyway. */ |
| 6444 | if( domain_parameters_size > sizeof( acc ) ) |
| 6445 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6446 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6447 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6448 | if( acc > INT_MAX ) |
| 6449 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6450 | *exponent = acc; |
| 6451 | return( PSA_SUCCESS ); |
| 6452 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6453 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6454 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6455 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6456 | psa_key_slot_t *slot, size_t bits, |
| 6457 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6458 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6459 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6460 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6461 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6462 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6463 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6464 | if( key_type_is_raw_bytes( type ) ) |
| 6465 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6466 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6467 | |
| 6468 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6469 | if( status != PSA_SUCCESS ) |
| 6470 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6471 | |
| 6472 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6473 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6474 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6475 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6476 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6477 | status = psa_generate_random( slot->key.data, |
| 6478 | slot->key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6479 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6480 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6481 | |
| 6482 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6483 | #if defined(MBEDTLS_DES_C) |
| 6484 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6485 | psa_des_set_key_parity( slot->key.data, |
| 6486 | slot->key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6487 | #endif /* MBEDTLS_DES_C */ |
| 6488 | } |
| 6489 | else |
| 6490 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6491 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6492 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6493 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6494 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6495 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6496 | int exponent; |
| 6497 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6498 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6499 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6500 | /* Accept only byte-aligned keys, for the same reasons as |
| 6501 | * in psa_import_rsa_key(). */ |
| 6502 | if( bits % 8 != 0 ) |
| 6503 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6504 | status = psa_read_rsa_exponent( domain_parameters, |
| 6505 | domain_parameters_size, |
| 6506 | &exponent ); |
| 6507 | if( status != PSA_SUCCESS ) |
| 6508 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6509 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6510 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6511 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6512 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6513 | (unsigned int) bits, |
| 6514 | exponent ); |
| 6515 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6516 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6517 | |
| 6518 | /* Make sure to always have an export representation available */ |
| 6519 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6520 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6521 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6522 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6523 | { |
| 6524 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6525 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6526 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6527 | |
Ronald Cron | 3c8ca3a | 2020-11-26 16:45:24 +0100 | [diff] [blame] | 6528 | status = mbedtls_psa_rsa_export_key( type, |
| 6529 | &rsa, |
| 6530 | slot->key.data, |
| 6531 | bytes, |
| 6532 | &slot->key.bytes ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6533 | mbedtls_rsa_free( &rsa ); |
| 6534 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6535 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6536 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6537 | } |
| 6538 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6539 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6540 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6541 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6542 | 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] | 6543 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6544 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6545 | mbedtls_ecp_group_id grp_id = |
| 6546 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6547 | const mbedtls_ecp_curve_info *curve_info = |
| 6548 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6549 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6550 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6551 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6552 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6553 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6554 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6555 | mbedtls_ecp_keypair_init( &ecp ); |
| 6556 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6557 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6558 | MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6559 | if( ret != 0 ) |
| 6560 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6561 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6562 | return( mbedtls_to_psa_error( ret ) ); |
| 6563 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6564 | |
| 6565 | |
| 6566 | /* Make sure to always have an export representation available */ |
| 6567 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6568 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6569 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6570 | { |
| 6571 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6572 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6573 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6574 | |
| 6575 | status = mbedtls_to_psa_error( |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6576 | mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6577 | |
| 6578 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6579 | if( status != PSA_SUCCESS ) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 6580 | memset( slot->key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6581 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6582 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6583 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6584 | } |
| 6585 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6586 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6587 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6588 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6589 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6590 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6591 | return( PSA_SUCCESS ); |
| 6592 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6593 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6594 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6595 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6596 | { |
| 6597 | psa_status_t status; |
| 6598 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6599 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6600 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6601 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6602 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6603 | /* Reject any attempt to create a zero-length key so that we don't |
| 6604 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6605 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6606 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6607 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6608 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6609 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6610 | if( status != PSA_SUCCESS ) |
| 6611 | goto exit; |
| 6612 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6613 | status = psa_driver_wrapper_generate_key( attributes, |
| 6614 | slot ); |
| 6615 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6616 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6617 | goto exit; |
| 6618 | |
| 6619 | status = psa_generate_key_internal( |
| 6620 | slot, attributes->core.bits, |
| 6621 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6622 | |
| 6623 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6624 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6625 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6626 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6627 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6628 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6629 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6630 | } |
| 6631 | |
| 6632 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6633 | |
| 6634 | /****************************************************************/ |
| 6635 | /* Module setup */ |
| 6636 | /****************************************************************/ |
| 6637 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6638 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6639 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6640 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6641 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6642 | { |
| 6643 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6644 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6645 | global_data.rng.entropy_init = entropy_init; |
| 6646 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6647 | return( PSA_SUCCESS ); |
| 6648 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6649 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6650 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6651 | void mbedtls_psa_crypto_free( void ) |
| 6652 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6653 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6654 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6655 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6656 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6657 | } |
| 6658 | /* Wipe all remaining data, including configuration. |
| 6659 | * In particular, this sets all state indicator to the value |
| 6660 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6661 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6662 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6663 | /* Unregister all secure element drivers, so that we restart from |
| 6664 | * a pristine state. */ |
| 6665 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6666 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6667 | } |
| 6668 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6669 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6670 | /** Recover a transaction that was interrupted by a power failure. |
| 6671 | * |
| 6672 | * This function is called during initialization, before psa_crypto_init() |
| 6673 | * returns. If this function returns a failure status, the initialization |
| 6674 | * fails. |
| 6675 | */ |
| 6676 | static psa_status_t psa_crypto_recover_transaction( |
| 6677 | const psa_crypto_transaction_t *transaction ) |
| 6678 | { |
| 6679 | switch( transaction->unknown.type ) |
| 6680 | { |
| 6681 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6682 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6683 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6684 | * is implemented. |
| 6685 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6686 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6687 | default: |
| 6688 | /* We found an unsupported transaction in the storage. |
| 6689 | * We don't know what state the storage is in. Give up. */ |
| 6690 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6691 | } |
| 6692 | } |
| 6693 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6694 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6695 | psa_status_t psa_crypto_init( void ) |
| 6696 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6697 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6698 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6699 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6700 | if( global_data.initialized != 0 ) |
| 6701 | return( PSA_SUCCESS ); |
| 6702 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6703 | /* Initialize and seed the random generator. */ |
| 6704 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6705 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6706 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6707 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6708 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6709 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6710 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6711 | status = psa_initialize_key_slots( ); |
| 6712 | if( status != PSA_SUCCESS ) |
| 6713 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6714 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6715 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6716 | status = psa_init_all_se_drivers( ); |
| 6717 | if( status != PSA_SUCCESS ) |
| 6718 | goto exit; |
| 6719 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6720 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6721 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6722 | status = psa_crypto_load_transaction( ); |
| 6723 | if( status == PSA_SUCCESS ) |
| 6724 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6725 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6726 | if( status != PSA_SUCCESS ) |
| 6727 | goto exit; |
| 6728 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6729 | } |
| 6730 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6731 | { |
| 6732 | /* There's no transaction to complete. It's all good. */ |
| 6733 | status = PSA_SUCCESS; |
| 6734 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6735 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6736 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6737 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6738 | global_data.initialized = 1; |
| 6739 | |
| 6740 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6741 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6742 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6743 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6744 | } |
| 6745 | |
| 6746 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |