Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 29 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | #include "psa/crypto.h" |
| 31 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 33 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | #include "psa_crypto_driver_wrappers.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 43 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 44 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 45 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | #define mbedtls_calloc calloc |
| 51 | #define mbedtls_free free |
| 52 | #endif |
| 53 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 54 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 56 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 57 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 58 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/blowfish.h" |
| 60 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 61 | #include "mbedtls/chacha20.h" |
| 62 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/cipher.h" |
| 64 | #include "mbedtls/ccm.h" |
| 65 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 67 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 68 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 69 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/error.h" |
| 71 | #include "mbedtls/gcm.h" |
| 72 | #include "mbedtls/md2.h" |
| 73 | #include "mbedtls/md4.h" |
| 74 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 75 | #include "mbedtls/md.h" |
| 76 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 78 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 79 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 80 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 82 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/sha1.h" |
| 84 | #include "mbedtls/sha256.h" |
| 85 | #include "mbedtls/sha512.h" |
| 86 | #include "mbedtls/xtea.h" |
| 87 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 88 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 89 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 90 | /* constant-time buffer comparison */ |
| 91 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 92 | { |
| 93 | size_t i; |
| 94 | unsigned char diff = 0; |
| 95 | |
| 96 | for( i = 0; i < n; i++ ) |
| 97 | diff |= a[i] ^ b[i]; |
| 98 | |
| 99 | return( diff ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 104 | /****************************************************************/ |
| 105 | /* Global data, support functions and library management */ |
| 106 | /****************************************************************/ |
| 107 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 109 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 110 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 113 | /* Values for psa_global_data_t::rng_state */ |
| 114 | #define RNG_NOT_INITIALIZED 0 |
| 115 | #define RNG_INITIALIZED 1 |
| 116 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 118 | typedef struct |
| 119 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 120 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; |
| 124 | |
| 125 | static psa_global_data_t global_data; |
| 126 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 127 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 128 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 129 | &global_data.rng.drbg; |
| 130 | #endif |
| 131 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 132 | #define GUARD_MODULE_INITIALIZED \ |
| 133 | if( global_data.initialized == 0 ) \ |
| 134 | return( PSA_ERROR_BAD_STATE ); |
| 135 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 136 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 137 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 138 | /* If there's both a high-level code and low-level code, dispatch on |
| 139 | * the high-level code. */ |
| 140 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 141 | { |
| 142 | case 0: |
| 143 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 144 | |
| 145 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 146 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 147 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 148 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 149 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 150 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 151 | |
| 152 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 153 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 154 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 155 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 156 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 157 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 158 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 159 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 160 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 161 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 162 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 163 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 164 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 165 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 166 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 167 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 168 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 169 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 170 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 171 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 172 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 173 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 174 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 175 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 176 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 177 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 178 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 179 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 180 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 181 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 182 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 183 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 184 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 185 | |
| 186 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 187 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 188 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 189 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 190 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 191 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 192 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 193 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 194 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 195 | |
| 196 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 197 | return( PSA_ERROR_BAD_STATE ); |
| 198 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 199 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 200 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 201 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 202 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 203 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 204 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 205 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 206 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 207 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 208 | return( PSA_ERROR_INVALID_PADDING ); |
| 209 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 210 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 212 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 213 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 214 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 215 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 216 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 217 | |
| 218 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 219 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 220 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 221 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 222 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 223 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 224 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 226 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 227 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 228 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 229 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 230 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 231 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 232 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 | |
| 234 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 235 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 236 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 237 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 238 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 239 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 240 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 241 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 242 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 243 | |
| 244 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 245 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 246 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 248 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 249 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 250 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 251 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 252 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 253 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 254 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 256 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 257 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 258 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 259 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 260 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 261 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 262 | #endif |
| 263 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 264 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 265 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 266 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 267 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 268 | |
| 269 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 270 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 271 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 272 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 273 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 274 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 275 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 276 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 277 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 278 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 279 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 280 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 281 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 282 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 283 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 284 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 286 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 287 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 288 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 290 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 292 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 294 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 295 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 296 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 298 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 299 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 300 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 301 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 302 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 303 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 304 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 305 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 307 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 308 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 309 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 310 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 311 | return( PSA_ERROR_NOT_PERMITTED ); |
| 312 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 313 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 314 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 315 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 316 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 317 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 318 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 319 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 321 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 322 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 323 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 324 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 325 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 326 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 327 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 328 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | |
| 331 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 333 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 334 | return( PSA_ERROR_INVALID_PADDING ); |
| 335 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 336 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 337 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 338 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 339 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 340 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 341 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 342 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 343 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 344 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 345 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 346 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 347 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 348 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 349 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 350 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 351 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 352 | |
| 353 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 354 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 355 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 356 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 357 | |
| 358 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 359 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 360 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 361 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 362 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 363 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 364 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 365 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 366 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 367 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 368 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 369 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 370 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 371 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 372 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 373 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 374 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 375 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 376 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 377 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 378 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 379 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 380 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 381 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 385 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 386 | |
| 387 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 388 | /****************************************************************/ |
| 389 | /* Key management */ |
| 390 | /****************************************************************/ |
| 391 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 392 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 393 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 394 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 395 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 396 | } |
| 397 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 398 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 399 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 400 | * current test driver in key_management.c is using this function |
| 401 | * when accelerators are used for ECC key pair and public key. |
| 402 | * Once that dependency is resolved these guards can be removed. |
| 403 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 404 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 405 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 406 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 407 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 408 | 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] | 409 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 410 | { |
| 411 | switch( curve ) |
| 412 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 413 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 414 | switch( byte_length ) |
| 415 | { |
| 416 | case PSA_BITS_TO_BYTES( 192 ): |
| 417 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 418 | case PSA_BITS_TO_BYTES( 224 ): |
| 419 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 420 | case PSA_BITS_TO_BYTES( 256 ): |
| 421 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 422 | case PSA_BITS_TO_BYTES( 384 ): |
| 423 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 424 | case PSA_BITS_TO_BYTES( 521 ): |
| 425 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 426 | default: |
| 427 | return( MBEDTLS_ECP_DP_NONE ); |
| 428 | } |
| 429 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 430 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 431 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 432 | switch( byte_length ) |
| 433 | { |
| 434 | case PSA_BITS_TO_BYTES( 256 ): |
| 435 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 436 | case PSA_BITS_TO_BYTES( 384 ): |
| 437 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 438 | case PSA_BITS_TO_BYTES( 512 ): |
| 439 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 440 | default: |
| 441 | return( MBEDTLS_ECP_DP_NONE ); |
| 442 | } |
| 443 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 444 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 445 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 446 | switch( byte_length ) |
| 447 | { |
| 448 | case PSA_BITS_TO_BYTES( 255 ): |
| 449 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 450 | case PSA_BITS_TO_BYTES( 448 ): |
| 451 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 452 | default: |
| 453 | return( MBEDTLS_ECP_DP_NONE ); |
| 454 | } |
| 455 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 456 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 457 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 458 | switch( byte_length ) |
| 459 | { |
| 460 | case PSA_BITS_TO_BYTES( 192 ): |
| 461 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 462 | case PSA_BITS_TO_BYTES( 224 ): |
| 463 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 464 | case PSA_BITS_TO_BYTES( 256 ): |
| 465 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 466 | default: |
| 467 | return( MBEDTLS_ECP_DP_NONE ); |
| 468 | } |
| 469 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 470 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 471 | default: |
| 472 | return( MBEDTLS_ECP_DP_NONE ); |
| 473 | } |
| 474 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 475 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 476 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 477 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 478 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 479 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 480 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 481 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 482 | { |
| 483 | /* Check that the bit size is acceptable for the key type */ |
| 484 | switch( type ) |
| 485 | { |
| 486 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 488 | case PSA_KEY_TYPE_DERIVE: |
| 489 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_AES_C) |
| 491 | case PSA_KEY_TYPE_AES: |
| 492 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 493 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 494 | break; |
| 495 | #endif |
| 496 | #if defined(MBEDTLS_CAMELLIA_C) |
| 497 | case PSA_KEY_TYPE_CAMELLIA: |
| 498 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 499 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 500 | break; |
| 501 | #endif |
| 502 | #if defined(MBEDTLS_DES_C) |
| 503 | case PSA_KEY_TYPE_DES: |
| 504 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 505 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 506 | break; |
| 507 | #endif |
| 508 | #if defined(MBEDTLS_ARC4_C) |
| 509 | case PSA_KEY_TYPE_ARC4: |
| 510 | if( bits < 8 || bits > 2048 ) |
| 511 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 512 | break; |
| 513 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 514 | #if defined(MBEDTLS_CHACHA20_C) |
| 515 | case PSA_KEY_TYPE_CHACHA20: |
| 516 | if( bits != 256 ) |
| 517 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 518 | break; |
| 519 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 520 | default: |
| 521 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 522 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 523 | if( bits % 8 != 0 ) |
| 524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 525 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 526 | return( PSA_SUCCESS ); |
| 527 | } |
| 528 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 529 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 530 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 531 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 532 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 533 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 534 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 535 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 536 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 537 | * that are not a multiple of 8) well. For example, there is only |
| 538 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 539 | * way to return the exact bit size of a key. |
| 540 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 541 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 542 | const mbedtls_rsa_context *rsa ) |
| 543 | { |
| 544 | mbedtls_mpi n; |
| 545 | psa_status_t status; |
| 546 | mbedtls_mpi_init( &n ); |
| 547 | status = mbedtls_to_psa_error( |
| 548 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 549 | if( status == PSA_SUCCESS ) |
| 550 | { |
| 551 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 552 | status = PSA_ERROR_NOT_SUPPORTED; |
| 553 | } |
| 554 | mbedtls_mpi_free( &n ); |
| 555 | return( status ); |
| 556 | } |
| 557 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 558 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 559 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 560 | * \param[in] type The type of key contained in \p data. |
| 561 | * \param[in] data The buffer from which to load the representation. |
| 562 | * \param[in] data_length The size in bytes of \p data. |
| 563 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 564 | * The caller is responsible for freeing both the |
| 565 | * contents of the context and the context itself |
| 566 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 567 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 568 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 569 | const uint8_t *data, |
| 570 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 571 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 572 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 573 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 574 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 575 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 576 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 577 | |
| 578 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 579 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 580 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 581 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 582 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 583 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 584 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 585 | if( status != PSA_SUCCESS ) |
| 586 | goto exit; |
| 587 | |
| 588 | /* We have something that the pkparse module recognizes. If it is a |
| 589 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 590 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 591 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 592 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 593 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 594 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 595 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 596 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 597 | * supports non-byte-aligned key sizes, but not well. For example, |
| 598 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 599 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 600 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 601 | { |
| 602 | status = PSA_ERROR_NOT_SUPPORTED; |
| 603 | goto exit; |
| 604 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 605 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 606 | if( status != PSA_SUCCESS ) |
| 607 | goto exit; |
| 608 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 609 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 610 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 611 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 612 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 613 | |
| 614 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 615 | mbedtls_pk_free( &ctx ); |
| 616 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 617 | } |
| 618 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 619 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 620 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 621 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 622 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 623 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 624 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 625 | |
| 626 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 627 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 628 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 629 | /** Export an RSA key to export representation |
| 630 | * |
| 631 | * \param[in] type The type of key (public/private) to export |
| 632 | * \param[in] rsa The internal RSA representation from which to export |
| 633 | * \param[out] data The buffer to export to |
| 634 | * \param[in] data_size The length of the buffer to export to |
| 635 | * \param[out] data_length The amount of bytes written to \p data |
| 636 | */ |
| 637 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 638 | mbedtls_rsa_context *rsa, |
| 639 | uint8_t *data, |
| 640 | size_t data_size, |
| 641 | size_t *data_length ) |
| 642 | { |
| 643 | #if defined(MBEDTLS_PK_WRITE_C) |
| 644 | int ret; |
| 645 | mbedtls_pk_context pk; |
| 646 | uint8_t *pos = data + data_size; |
| 647 | |
| 648 | mbedtls_pk_init( &pk ); |
| 649 | pk.pk_info = &mbedtls_rsa_info; |
| 650 | pk.pk_ctx = rsa; |
| 651 | |
| 652 | /* 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] | 653 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 654 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 655 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 656 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 657 | else |
| 658 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 659 | |
| 660 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 661 | { |
| 662 | /* Clean up in case pk_write failed halfway through. */ |
| 663 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 664 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 665 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 666 | |
| 667 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 668 | * Move the data to the beginning and erase remaining data |
| 669 | * at the original location. */ |
| 670 | if( 2 * (size_t) ret <= data_size ) |
| 671 | { |
| 672 | memcpy( data, data + data_size - ret, ret ); |
| 673 | memset( data + data_size - ret, 0, ret ); |
| 674 | } |
| 675 | else if( (size_t) ret < data_size ) |
| 676 | { |
| 677 | memmove( data, data + data_size - ret, ret ); |
| 678 | memset( data + ret, 0, data_size - ret ); |
| 679 | } |
| 680 | |
| 681 | *data_length = ret; |
| 682 | return( PSA_SUCCESS ); |
| 683 | #else |
| 684 | (void) type; |
| 685 | (void) rsa; |
| 686 | (void) data; |
| 687 | (void) data_size; |
| 688 | (void) data_length; |
| 689 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 690 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 691 | } |
| 692 | |
| 693 | /** Import an RSA key from import representation to a slot |
| 694 | * |
| 695 | * \param[in,out] slot The slot where to store the export representation to |
| 696 | * \param[in] data The buffer containing the import representation |
| 697 | * \param[in] data_length The amount of bytes in \p data |
| 698 | */ |
| 699 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 700 | const uint8_t *data, |
| 701 | size_t data_length ) |
| 702 | { |
| 703 | psa_status_t status; |
| 704 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 705 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 706 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 707 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 708 | status = psa_load_rsa_representation( slot->attr.type, |
| 709 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 710 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 711 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 712 | if( status != PSA_SUCCESS ) |
| 713 | goto exit; |
| 714 | |
| 715 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 716 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 717 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 718 | /* Re-export the data to PSA export format, such that we can store export |
| 719 | * representation in the key slot. Export representation in case of RSA is |
| 720 | * the smallest representation that's allowed as input, so a straight-up |
| 721 | * 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] | 722 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 723 | if( output == NULL ) |
| 724 | { |
| 725 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 726 | goto exit; |
| 727 | } |
| 728 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 729 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 730 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 731 | output, |
| 732 | data_length, |
| 733 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 734 | exit: |
| 735 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 736 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 737 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 738 | |
| 739 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 740 | if( status != PSA_SUCCESS ) |
| 741 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 742 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 743 | return( status ); |
| 744 | } |
| 745 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 746 | /* On success, store the allocated export-formatted key. */ |
| 747 | slot->data.key.data = output; |
| 748 | slot->data.key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 749 | |
| 750 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 751 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 752 | |
| 753 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 754 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 755 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 756 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 757 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 758 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 759 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \ |
| 760 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 761 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 762 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 763 | * \param[in] type The type of key contained in \p data. |
| 764 | * \param[in] data The buffer from which to load the representation. |
| 765 | * \param[in] data_length The size in bytes of \p data. |
| 766 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 767 | * The caller is responsible for freeing both the |
| 768 | * contents of the context and the context itself |
| 769 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 770 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 771 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 772 | const uint8_t *data, |
| 773 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 774 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 775 | { |
| 776 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 777 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 778 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 779 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 780 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 781 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 782 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 783 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 784 | /* A Weierstrass public key is represented as: |
| 785 | * - The byte 0x04; |
| 786 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 787 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 788 | * So its data length is 2m+1 where m is the curve size in bits. |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 789 | */ |
| 790 | if( ( data_length & 1 ) == 0 ) |
| 791 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 792 | curve_size = data_length / 2; |
| 793 | |
| 794 | /* Montgomery public keys are represented in compressed format, meaning |
| 795 | * their curve_size is equal to the amount of input. */ |
| 796 | |
| 797 | /* Private keys are represented in uncompressed private random integer |
| 798 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 801 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 802 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 803 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 804 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 805 | mbedtls_ecp_keypair_init( ecp ); |
| 806 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 807 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 808 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 809 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 810 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 811 | { |
| 812 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 813 | goto exit; |
| 814 | } |
| 815 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 816 | status = mbedtls_to_psa_error( |
| 817 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 818 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 819 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 820 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 821 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 822 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 823 | { |
| 824 | /* Load the public value. */ |
| 825 | status = mbedtls_to_psa_error( |
| 826 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 827 | data, |
| 828 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 829 | if( status != PSA_SUCCESS ) |
| 830 | goto exit; |
| 831 | |
| 832 | /* Check that the point is on the curve. */ |
| 833 | status = mbedtls_to_psa_error( |
| 834 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 835 | if( status != PSA_SUCCESS ) |
| 836 | goto exit; |
| 837 | } |
| 838 | else |
| 839 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 840 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 841 | status = mbedtls_to_psa_error( |
| 842 | mbedtls_ecp_read_key( ecp->grp.id, |
| 843 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 844 | data, |
| 845 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 846 | if( status != PSA_SUCCESS ) |
| 847 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 848 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 849 | |
| 850 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 851 | exit: |
| 852 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 853 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 854 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 855 | mbedtls_free( ecp ); |
| 856 | } |
| 857 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 858 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 859 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 860 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 861 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 862 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 863 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || |
| 864 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 865 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 866 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 867 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 868 | /** Export an ECP key to export representation |
| 869 | * |
| 870 | * \param[in] type The type of key (public/private) to export |
| 871 | * \param[in] ecp The internal ECP representation from which to export |
| 872 | * \param[out] data The buffer to export to |
| 873 | * \param[in] data_size The length of the buffer to export to |
| 874 | * \param[out] data_length The amount of bytes written to \p data |
| 875 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 876 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 877 | mbedtls_ecp_keypair *ecp, |
| 878 | uint8_t *data, |
| 879 | size_t data_size, |
| 880 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 881 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 882 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 883 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 884 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 885 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 886 | /* Check whether the public part is loaded */ |
| 887 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 888 | { |
| 889 | /* Calculate the public key */ |
| 890 | status = mbedtls_to_psa_error( |
| 891 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 892 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 893 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 894 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 897 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 898 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 899 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 900 | data_length, |
| 901 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 902 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 903 | if( status != PSA_SUCCESS ) |
| 904 | memset( data, 0, data_size ); |
| 905 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 906 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 907 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 908 | else |
| 909 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 910 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 911 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 912 | |
| 913 | status = mbedtls_to_psa_error( |
| 914 | mbedtls_ecp_write_key( ecp, |
| 915 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 916 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 917 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 918 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 919 | else |
| 920 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 921 | |
| 922 | return( status ); |
| 923 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 924 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 925 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 926 | /** Import an ECP key from import representation to a slot |
| 927 | * |
| 928 | * \param[in,out] slot The slot where to store the export representation to |
| 929 | * \param[in] data The buffer containing the import representation |
| 930 | * \param[in] data_length The amount of bytes in \p data |
| 931 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 932 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 933 | const uint8_t *data, |
| 934 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 935 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 936 | psa_status_t status; |
| 937 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 938 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 939 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 940 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 941 | status = psa_load_ecp_representation( slot->attr.type, |
| 942 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 943 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 944 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 945 | if( status != PSA_SUCCESS ) |
| 946 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 947 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 948 | 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] | 949 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 950 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 951 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 952 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 953 | /* Re-export the data to PSA export format. There is currently no support |
| 954 | * for other input formats then the export format, so this is a 1-1 |
| 955 | * copy operation. */ |
| 956 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 957 | if( output == NULL ) |
| 958 | { |
| 959 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 960 | goto exit; |
| 961 | } |
| 962 | |
| 963 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 964 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 965 | output, |
| 966 | data_length, |
| 967 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 968 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 969 | /* Always free the PK object (will also free contained ECP context) */ |
| 970 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 971 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 972 | |
| 973 | /* Free the allocated buffer only on error. */ |
| 974 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 975 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 976 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 977 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 978 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 979 | |
| 980 | /* On success, store the allocated export-formatted key. */ |
| 981 | slot->data.key.data = output; |
| 982 | slot->data.key.bytes = data_length; |
| 983 | |
| 984 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 985 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 986 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 987 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 988 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 989 | /** Return the size of the key in the given slot, in bits. |
| 990 | * |
| 991 | * \param[in] slot A key slot. |
| 992 | * |
| 993 | * \return The key size in bits, read from the metadata in the slot. |
| 994 | */ |
| 995 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 996 | { |
| 997 | return( slot->attr.bits ); |
| 998 | } |
| 999 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1000 | /** Try to allocate a buffer to an empty key slot. |
| 1001 | * |
| 1002 | * \param[in,out] slot Key slot to attach buffer to. |
| 1003 | * \param[in] buffer_length Requested size of the buffer. |
| 1004 | * |
| 1005 | * \retval #PSA_SUCCESS |
| 1006 | * The buffer has been successfully allocated. |
| 1007 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1008 | * Not enough memory was available for allocation. |
| 1009 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1010 | * Trying to allocate a buffer to a non-empty key slot. |
| 1011 | */ |
| 1012 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 1013 | size_t buffer_length ) |
| 1014 | { |
| 1015 | if( slot->data.key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1016 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1017 | |
| 1018 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 1019 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1020 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1021 | |
| 1022 | slot->data.key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1023 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1026 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 1027 | const uint8_t* data, |
| 1028 | size_t data_length ) |
| 1029 | { |
| 1030 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 1031 | data_length ); |
| 1032 | if( status != PSA_SUCCESS ) |
| 1033 | return( status ); |
| 1034 | |
| 1035 | memcpy( slot->data.key.data, data, data_length ); |
| 1036 | return( PSA_SUCCESS ); |
| 1037 | } |
| 1038 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1039 | /** Import key data into a slot. |
| 1040 | * |
| 1041 | * `slot->type` must have been set previously. |
| 1042 | * This function assumes that the slot does not contain any key material yet. |
| 1043 | * On failure, the slot content is unchanged. |
| 1044 | * |
| 1045 | * Persistent storage is not affected. |
| 1046 | * |
| 1047 | * \param[in,out] slot The key slot to import data into. |
| 1048 | * Its `type` field must have previously been set to |
| 1049 | * the desired key type. |
| 1050 | * It must not contain any key material yet. |
| 1051 | * \param[in] data Buffer containing the key material to parse and import. |
| 1052 | * \param data_length Size of \p data in bytes. |
| 1053 | * |
| 1054 | * \retval #PSA_SUCCESS |
| 1055 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1056 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1057 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1058 | */ |
| 1059 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 1060 | const uint8_t *data, |
| 1061 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1062 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1063 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1064 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1065 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1066 | /* zero-length keys are never supported. */ |
| 1067 | if( data_length == 0 ) |
| 1068 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1069 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1070 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1071 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1072 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1073 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1074 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 1075 | if( data_length > SIZE_MAX / 8 ) |
| 1076 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1077 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 1078 | /* Enforce a size limit, and in particular ensure that the bit |
| 1079 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1080 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 1081 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1082 | |
| 1083 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1084 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1085 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1086 | |
| 1087 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1088 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1089 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1090 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1091 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1092 | /* Write the actual key size to the slot. |
| 1093 | * psa_start_key_creation() wrote the size declared by the |
| 1094 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1095 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1096 | |
| 1097 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1098 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1099 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1100 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1101 | /* Try validation through accelerators first. */ |
| 1102 | bit_size = slot->attr.bits; |
| 1103 | psa_key_attributes_t attributes = { |
| 1104 | .core = slot->attr |
| 1105 | }; |
| 1106 | status = psa_driver_wrapper_validate_key( &attributes, |
| 1107 | data, |
| 1108 | data_length, |
| 1109 | &bit_size ); |
| 1110 | if( status == PSA_SUCCESS ) |
| 1111 | { |
| 1112 | /* Key has been validated successfully by an accelerator. |
| 1113 | * Copy key material into slot. */ |
| 1114 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
| 1115 | if( status != PSA_SUCCESS ) |
| 1116 | return( status ); |
| 1117 | |
| 1118 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 1119 | return( PSA_SUCCESS ); |
| 1120 | } |
| 1121 | else if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1122 | return( status ); |
| 1123 | |
| 1124 | /* Key format is not supported by any accelerator, try software fallback |
| 1125 | * if present. */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1126 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1127 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1128 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1129 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1130 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 1131 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1132 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1133 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1134 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1135 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1136 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1137 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1138 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1139 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1140 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1141 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1142 | |
| 1143 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 1144 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1145 | } |
| 1146 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1147 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1148 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1149 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1150 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1153 | /** Calculate the intersection of two algorithm usage policies. |
| 1154 | * |
| 1155 | * Return 0 (which allows no operation) on incompatibility. |
| 1156 | */ |
| 1157 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1158 | psa_algorithm_t alg1, |
| 1159 | psa_algorithm_t alg2 ) |
| 1160 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1161 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1162 | if( alg1 == alg2 ) |
| 1163 | return( alg1 ); |
| 1164 | /* If the policies are from the same hash-and-sign family, check |
| 1165 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1166 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1167 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1168 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1169 | { |
| 1170 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1171 | return( alg2 ); |
| 1172 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1173 | return( alg1 ); |
| 1174 | } |
| 1175 | /* If the policies are incompatible, allow nothing. */ |
| 1176 | return( 0 ); |
| 1177 | } |
| 1178 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1179 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1180 | psa_algorithm_t requested_alg ) |
| 1181 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1182 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1183 | if( requested_alg == policy_alg ) |
| 1184 | return( 1 ); |
| 1185 | /* 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] | 1186 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1187 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1188 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1189 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1190 | { |
| 1191 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1192 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1193 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1194 | /* 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] | 1195 | * a key derivation with that key agreement should also be allowed. This |
| 1196 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1197 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1198 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1199 | { |
| 1200 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1201 | policy_alg ); |
| 1202 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1203 | /* If it isn't permitted, it's forbidden. */ |
| 1204 | return( 0 ); |
| 1205 | } |
| 1206 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1207 | /** Test whether a policy permits an algorithm. |
| 1208 | * |
| 1209 | * The caller must test usage flags separately. |
| 1210 | */ |
| 1211 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1212 | psa_algorithm_t alg ) |
| 1213 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1214 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1215 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1218 | /** Restrict a key policy based on a constraint. |
| 1219 | * |
| 1220 | * \param[in,out] policy The policy to restrict. |
| 1221 | * \param[in] constraint The policy constraint to apply. |
| 1222 | * |
| 1223 | * \retval #PSA_SUCCESS |
| 1224 | * \c *policy contains the intersection of the original value of |
| 1225 | * \c *policy and \c *constraint. |
| 1226 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1227 | * \c *policy and \c *constraint are incompatible. |
| 1228 | * \c *policy is unchanged. |
| 1229 | */ |
| 1230 | static psa_status_t psa_restrict_key_policy( |
| 1231 | psa_key_policy_t *policy, |
| 1232 | const psa_key_policy_t *constraint ) |
| 1233 | { |
| 1234 | psa_algorithm_t intersection_alg = |
| 1235 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1236 | psa_algorithm_t intersection_alg2 = |
| 1237 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1238 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1240 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1242 | policy->usage &= constraint->usage; |
| 1243 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1244 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1245 | return( PSA_SUCCESS ); |
| 1246 | } |
| 1247 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1248 | /** Get the description of a key given its identifier and policy constraints |
| 1249 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1250 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1251 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1252 | * nonzero, the key must allow operations with this algorithm. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1253 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1254 | * In case of a persistent key, the function loads the description of the key |
| 1255 | * into a key slot if not already done. |
| 1256 | * |
| 1257 | * On success, the returned key slot is locked. It is the responsibility of |
| 1258 | * 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] | 1259 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1260 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1261 | mbedtls_svc_key_id_t key, |
| 1262 | psa_key_slot_t **p_slot, |
| 1263 | psa_key_usage_t usage, |
| 1264 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1265 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1266 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1267 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1268 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1269 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1270 | if( status != PSA_SUCCESS ) |
| 1271 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1272 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1273 | |
| 1274 | /* Enforce that usage policy for the key slot contains all the flags |
| 1275 | * required by the usage parameter. There is one exception: public |
| 1276 | * keys can always be exported, so we treat public key objects as |
| 1277 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1278 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1279 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1280 | |
| 1281 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1282 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1283 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1284 | |
| 1285 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1286 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1287 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1288 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1289 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1290 | |
| 1291 | error: |
| 1292 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1293 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1294 | |
| 1295 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1296 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1297 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1298 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1299 | * |
| 1300 | * A transparent key is a key for which the key material is directly |
| 1301 | * available, as opposed to a key in a secure element. |
| 1302 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1303 | * This is a temporary function to use instead of |
| 1304 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1305 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1306 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1307 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1308 | * 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] | 1309 | */ |
| 1310 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1311 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1312 | mbedtls_svc_key_id_t key, |
| 1313 | psa_key_slot_t **p_slot, |
| 1314 | psa_key_usage_t usage, |
| 1315 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1316 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1317 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1318 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1319 | if( status != PSA_SUCCESS ) |
| 1320 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1321 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1322 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1323 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1324 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1325 | *p_slot = NULL; |
| 1326 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1327 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1328 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1329 | return( PSA_SUCCESS ); |
| 1330 | } |
| 1331 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1332 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1333 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1334 | 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] | 1335 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1336 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1337 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1338 | 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] | 1339 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1340 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Fredrik Strupe | 462aa57 | 2020-12-17 10:44:38 +0100 | [diff] [blame] | 1341 | if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) && |
| 1342 | psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1343 | { |
| 1344 | /* No key material to clean. */ |
| 1345 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1346 | else |
| 1347 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1348 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame] | 1349 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1350 | * initialized slot, so we can just free it. */ |
| 1351 | if( slot->data.key.data != NULL ) |
| 1352 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1353 | mbedtls_free( slot->data.key.data ); |
| 1354 | slot->data.key.data = NULL; |
| 1355 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1356 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1357 | |
| 1358 | return( PSA_SUCCESS ); |
| 1359 | } |
| 1360 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1361 | /** Completely wipe a slot in memory, including its policy. |
| 1362 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1363 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1364 | { |
| 1365 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1366 | |
| 1367 | /* |
| 1368 | * 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] | 1369 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1370 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1371 | * 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] | 1372 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1373 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1374 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1375 | { |
| 1376 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1377 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1378 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1379 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1380 | } |
| 1381 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1382 | /* Multipart operations may still be using the key. This is safe |
| 1383 | * because all multipart operation objects are independent from |
| 1384 | * the key slot: if they need to access the key after the setup |
| 1385 | * phase, they have a copy of the key. Note that this means that |
| 1386 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1387 | /* At this point, key material and other type-specific content has |
| 1388 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1389 | * zeroize because the metadata is not particularly sensitive. */ |
| 1390 | memset( slot, 0, sizeof( *slot ) ); |
| 1391 | return( status ); |
| 1392 | } |
| 1393 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1394 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1395 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1396 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1397 | psa_status_t status; /* status of the last operation */ |
| 1398 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1399 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1400 | psa_se_drv_table_entry_t *driver; |
| 1401 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1402 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1403 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1404 | return( PSA_SUCCESS ); |
| 1405 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1406 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1407 | * 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] | 1408 | * key, this will load the key description from persistent memory if not |
| 1409 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1410 | * the key is operated by an SE or not and this information is needed by |
| 1411 | * the current implementation. |
| 1412 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1413 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1414 | if( status != PSA_SUCCESS ) |
| 1415 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1416 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1417 | /* |
| 1418 | * If the key slot containing the key description is under access by the |
| 1419 | * library (apart from the present access), the key cannot be destroyed |
| 1420 | * yet. For the time being, just return in error. Eventually (to be |
| 1421 | * implemented), the key should be destroyed when all accesses have |
| 1422 | * stopped. |
| 1423 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1424 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1425 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1426 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1427 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1428 | } |
| 1429 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1430 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1431 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1432 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1433 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1434 | /* For a key in a secure element, we need to do three things: |
| 1435 | * remove the key file in internal storage, destroy the |
| 1436 | * key inside the secure element, and update the driver's |
| 1437 | * persistent data. Start a transaction that will encompass these |
| 1438 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1439 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1440 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1441 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1442 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1443 | status = psa_crypto_save_transaction( ); |
| 1444 | if( status != PSA_SUCCESS ) |
| 1445 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1446 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1447 | /* We should still try to destroy the key in the secure |
| 1448 | * element and the key metadata in storage. This is especially |
| 1449 | * important if the error is that the storage is full. |
| 1450 | * But how to do it exactly without risking an inconsistent |
| 1451 | * state after a reset? |
| 1452 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1453 | */ |
| 1454 | overall_status = status; |
| 1455 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1458 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1459 | if( overall_status == PSA_SUCCESS ) |
| 1460 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1461 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1462 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1463 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1464 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1465 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1466 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1467 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1468 | if( overall_status == PSA_SUCCESS ) |
| 1469 | overall_status = status; |
| 1470 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1471 | /* TODO: other slots may have a copy of the same key. We should |
| 1472 | * invalidate them. |
| 1473 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1474 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1475 | } |
| 1476 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1477 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1478 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1479 | if( driver != NULL ) |
| 1480 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1481 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1482 | if( overall_status == PSA_SUCCESS ) |
| 1483 | overall_status = status; |
| 1484 | status = psa_crypto_stop_transaction( ); |
| 1485 | if( overall_status == PSA_SUCCESS ) |
| 1486 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1487 | } |
| 1488 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1489 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1490 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1491 | exit: |
| 1492 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1493 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1494 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1495 | if( overall_status == PSA_SUCCESS ) |
| 1496 | overall_status = status; |
| 1497 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1500 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1501 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1502 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1503 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1506 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1507 | psa_key_type_t type, |
| 1508 | const uint8_t *data, |
| 1509 | size_t data_length ) |
| 1510 | { |
| 1511 | uint8_t *copy = NULL; |
| 1512 | |
| 1513 | if( data_length != 0 ) |
| 1514 | { |
| 1515 | copy = mbedtls_calloc( 1, data_length ); |
| 1516 | if( copy == NULL ) |
| 1517 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1518 | memcpy( copy, data, data_length ); |
| 1519 | } |
| 1520 | /* After this point, this function is guaranteed to succeed, so it |
| 1521 | * can start modifying `*attributes`. */ |
| 1522 | |
| 1523 | if( attributes->domain_parameters != NULL ) |
| 1524 | { |
| 1525 | mbedtls_free( attributes->domain_parameters ); |
| 1526 | attributes->domain_parameters = NULL; |
| 1527 | attributes->domain_parameters_size = 0; |
| 1528 | } |
| 1529 | |
| 1530 | attributes->domain_parameters = copy; |
| 1531 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1532 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1533 | return( PSA_SUCCESS ); |
| 1534 | } |
| 1535 | |
| 1536 | psa_status_t psa_get_key_domain_parameters( |
| 1537 | const psa_key_attributes_t *attributes, |
| 1538 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1539 | { |
| 1540 | if( attributes->domain_parameters_size > data_size ) |
| 1541 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1542 | *data_length = attributes->domain_parameters_size; |
| 1543 | if( attributes->domain_parameters_size != 0 ) |
| 1544 | memcpy( data, attributes->domain_parameters, |
| 1545 | attributes->domain_parameters_size ); |
| 1546 | return( PSA_SUCCESS ); |
| 1547 | } |
| 1548 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1549 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1550 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1551 | static psa_status_t psa_get_rsa_public_exponent( |
| 1552 | const mbedtls_rsa_context *rsa, |
| 1553 | psa_key_attributes_t *attributes ) |
| 1554 | { |
| 1555 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1556 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1557 | uint8_t *buffer = NULL; |
| 1558 | size_t buflen; |
| 1559 | mbedtls_mpi_init( &mpi ); |
| 1560 | |
| 1561 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1562 | if( ret != 0 ) |
| 1563 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1564 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1565 | { |
| 1566 | /* It's the default value, which is reported as an empty string, |
| 1567 | * so there's nothing to do. */ |
| 1568 | goto exit; |
| 1569 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1570 | |
| 1571 | buflen = mbedtls_mpi_size( &mpi ); |
| 1572 | buffer = mbedtls_calloc( 1, buflen ); |
| 1573 | if( buffer == NULL ) |
| 1574 | { |
| 1575 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1576 | goto exit; |
| 1577 | } |
| 1578 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1579 | if( ret != 0 ) |
| 1580 | goto exit; |
| 1581 | attributes->domain_parameters = buffer; |
| 1582 | attributes->domain_parameters_size = buflen; |
| 1583 | |
| 1584 | exit: |
| 1585 | mbedtls_mpi_free( &mpi ); |
| 1586 | if( ret != 0 ) |
| 1587 | mbedtls_free( buffer ); |
| 1588 | return( mbedtls_to_psa_error( ret ) ); |
| 1589 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1590 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1591 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1592 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1593 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1594 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1595 | 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] | 1596 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1597 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1598 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1599 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1600 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1601 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1602 | psa_reset_key_attributes( attributes ); |
| 1603 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1604 | 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] | 1605 | if( status != PSA_SUCCESS ) |
| 1606 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1607 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1608 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1609 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1610 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1611 | |
| 1612 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1613 | if( psa_key_slot_is_external( slot ) ) |
| 1614 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1615 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1616 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1617 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1618 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1619 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1620 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1621 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1622 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1623 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1624 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1625 | * is not yet implemented. |
| 1626 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1627 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1628 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1629 | break; |
| 1630 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1631 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1632 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1633 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1634 | status = psa_load_rsa_representation( slot->attr.type, |
| 1635 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1636 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1637 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1638 | if( status != PSA_SUCCESS ) |
| 1639 | break; |
| 1640 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1641 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1642 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1643 | mbedtls_rsa_free( rsa ); |
| 1644 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1645 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1646 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1647 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1648 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1649 | default: |
| 1650 | /* Nothing else to do. */ |
| 1651 | break; |
| 1652 | } |
| 1653 | |
| 1654 | if( status != PSA_SUCCESS ) |
| 1655 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1656 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1657 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1658 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1659 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1662 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1663 | psa_status_t psa_get_key_slot_number( |
| 1664 | const psa_key_attributes_t *attributes, |
| 1665 | psa_key_slot_number_t *slot_number ) |
| 1666 | { |
| 1667 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1668 | { |
| 1669 | *slot_number = attributes->slot_number; |
| 1670 | return( PSA_SUCCESS ); |
| 1671 | } |
| 1672 | else |
| 1673 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1674 | } |
| 1675 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1676 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1677 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1678 | uint8_t *data, |
| 1679 | size_t data_size, |
| 1680 | size_t *data_length ) |
| 1681 | { |
| 1682 | if( slot->data.key.bytes > data_size ) |
| 1683 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1684 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1685 | memset( data + slot->data.key.bytes, 0, |
| 1686 | data_size - slot->data.key.bytes ); |
| 1687 | *data_length = slot->data.key.bytes; |
| 1688 | return( PSA_SUCCESS ); |
| 1689 | } |
| 1690 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1691 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1692 | uint8_t *data, |
| 1693 | size_t data_size, |
| 1694 | size_t *data_length, |
| 1695 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1696 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1697 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1698 | const psa_drv_se_t *drv; |
| 1699 | psa_drv_se_context_t *drv_context; |
| 1700 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1701 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1702 | *data_length = 0; |
| 1703 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1704 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1705 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1706 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1707 | /* Reject a zero-length output buffer now, since this can never be a |
| 1708 | * valid key representation. This way we know that data must be a valid |
| 1709 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1710 | if( data_size == 0 ) |
| 1711 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1712 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1713 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1714 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1715 | { |
| 1716 | psa_drv_se_export_key_t method; |
| 1717 | if( drv->key_management == NULL ) |
| 1718 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1719 | method = ( export_public_key ? |
| 1720 | drv->key_management->p_export_public : |
| 1721 | drv->key_management->p_export ); |
| 1722 | if( method == NULL ) |
| 1723 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1724 | return( method( drv_context, |
| 1725 | slot->data.se.slot_number, |
| 1726 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1727 | } |
| 1728 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1729 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1730 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1731 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1732 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1733 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1734 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1735 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1736 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1737 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1738 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1739 | /* Exporting public -> public */ |
| 1740 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1741 | } |
| 1742 | else if( !export_public_key ) |
| 1743 | { |
| 1744 | /* Exporting private -> private */ |
| 1745 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1746 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1747 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1748 | /* Need to export the public part of a private key, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1749 | * so conversion is needed. Try the accelerators first. */ |
| 1750 | psa_status_t status = psa_driver_wrapper_export_public_key( slot, |
| 1751 | data, |
| 1752 | data_size, |
| 1753 | data_length ); |
| 1754 | |
| 1755 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 1756 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 1757 | return( status ); |
| 1758 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1759 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1760 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1761 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1762 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1763 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1764 | status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1765 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1766 | slot->data.key.data, |
| 1767 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1768 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1769 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1770 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1771 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1772 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1773 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1774 | data, |
| 1775 | data_size, |
| 1776 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1777 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1778 | mbedtls_rsa_free( rsa ); |
| 1779 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1780 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1781 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1782 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1783 | /* We don't know how to convert a private RSA key to public. */ |
| 1784 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1785 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1786 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1787 | } |
| 1788 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1789 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1790 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1791 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1792 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1793 | status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1794 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1795 | slot->data.key.data, |
| 1796 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1797 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1798 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1799 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1800 | |
| 1801 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1802 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1803 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1804 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1805 | data, |
| 1806 | data_size, |
| 1807 | data_length ); |
| 1808 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1809 | mbedtls_ecp_keypair_free( ecp ); |
| 1810 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1811 | return( status ); |
| 1812 | #else |
| 1813 | /* 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] | 1814 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1815 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1816 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1817 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1818 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1819 | else |
| 1820 | { |
| 1821 | /* This shouldn't happen in the reference implementation, but |
| 1822 | it is valid for a special-purpose implementation to omit |
| 1823 | support for exporting certain key types. */ |
| 1824 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1825 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1826 | } |
| 1827 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1828 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1829 | uint8_t *data, |
| 1830 | size_t data_size, |
| 1831 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1832 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1833 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1834 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1835 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1836 | |
| 1837 | /* Set the key to empty now, so that even when there are errors, we always |
| 1838 | * set data_length to a value between 0 and data_size. On error, setting |
| 1839 | * the key to empty is a good choice because an empty key representation is |
| 1840 | * unlikely to be accepted anywhere. */ |
| 1841 | *data_length = 0; |
| 1842 | |
| 1843 | /* Export requires the EXPORT flag. There is an exception for public keys, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1844 | * which don't require any flag, but |
| 1845 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1846 | */ |
| 1847 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1848 | PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1849 | if( status != PSA_SUCCESS ) |
| 1850 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1851 | |
| 1852 | status = psa_internal_export_key( slot, data, data_size, data_length, 0 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1853 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1854 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1855 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1856 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1857 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1858 | 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] | 1859 | uint8_t *data, |
| 1860 | size_t data_size, |
| 1861 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1862 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1863 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1864 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1865 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1866 | |
| 1867 | /* Set the key to empty now, so that even when there are errors, we always |
| 1868 | * set data_length to a value between 0 and data_size. On error, setting |
| 1869 | * the key to empty is a good choice because an empty key representation is |
| 1870 | * unlikely to be accepted anywhere. */ |
| 1871 | *data_length = 0; |
| 1872 | |
| 1873 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1874 | 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] | 1875 | if( status != PSA_SUCCESS ) |
| 1876 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1877 | |
| 1878 | status = psa_internal_export_key( slot, data, data_size, data_length, 1 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1879 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1880 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1881 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1882 | } |
| 1883 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1884 | #if defined(static_assert) |
| 1885 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1886 | "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] | 1887 | 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] | 1888 | "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] | 1889 | 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] | 1890 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1891 | #endif |
| 1892 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1893 | /** Validate that a key policy is internally well-formed. |
| 1894 | * |
| 1895 | * This function only rejects invalid policies. It does not validate the |
| 1896 | * consistency of the policy with respect to other attributes of the key |
| 1897 | * such as the key type. |
| 1898 | */ |
| 1899 | 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] | 1900 | { |
| 1901 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1902 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1903 | PSA_KEY_USAGE_ENCRYPT | |
| 1904 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1905 | PSA_KEY_USAGE_SIGN_HASH | |
| 1906 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1907 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1908 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1909 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1910 | return( PSA_SUCCESS ); |
| 1911 | } |
| 1912 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1913 | /** Validate the internal consistency of key attributes. |
| 1914 | * |
| 1915 | * This function only rejects invalid attribute values. If does not |
| 1916 | * validate the consistency of the attributes with any key data that may |
| 1917 | * be involved in the creation of the key. |
| 1918 | * |
| 1919 | * Call this function early in the key creation process. |
| 1920 | * |
| 1921 | * \param[in] attributes Key attributes for the new key. |
| 1922 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1923 | * NULL for a transparent key. |
| 1924 | * |
| 1925 | */ |
| 1926 | static psa_status_t psa_validate_key_attributes( |
| 1927 | const psa_key_attributes_t *attributes, |
| 1928 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1929 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1930 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1931 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1932 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1933 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1934 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1935 | if( status != PSA_SUCCESS ) |
| 1936 | return( status ); |
| 1937 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1938 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1939 | if( status != PSA_SUCCESS ) |
| 1940 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1941 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1942 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1943 | { |
| 1944 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1945 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1946 | } |
| 1947 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1948 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1949 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1950 | if( status != PSA_SUCCESS ) |
| 1951 | return( status ); |
| 1952 | } |
| 1953 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1954 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1955 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1956 | return( status ); |
| 1957 | |
| 1958 | /* Refuse to create overly large keys. |
| 1959 | * Note that this doesn't trigger on import if the attributes don't |
| 1960 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1961 | * psa_import_key() needs its own checks. */ |
| 1962 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1963 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1964 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1965 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1966 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1967 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1968 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1969 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1970 | return( PSA_SUCCESS ); |
| 1971 | } |
| 1972 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1973 | /** Prepare a key slot to receive key material. |
| 1974 | * |
| 1975 | * This function allocates a key slot and sets its metadata. |
| 1976 | * |
| 1977 | * If this function fails, call psa_fail_key_creation(). |
| 1978 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1979 | * This function is intended to be used as follows: |
| 1980 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1981 | * it with the specified attributes, and in case of a volatile key assign it |
| 1982 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1983 | * -# Populate the slot with the key material. |
| 1984 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1985 | * In case of failure at any step, stop the sequence and call |
| 1986 | * psa_fail_key_creation(). |
| 1987 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1988 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1989 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1990 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1991 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1992 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1993 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1994 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1995 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1996 | * |
| 1997 | * \retval #PSA_SUCCESS |
| 1998 | * The key slot is ready to receive key material. |
| 1999 | * \return If this function fails, the key slot is an invalid state. |
| 2000 | * 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] | 2001 | */ |
| 2002 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2003 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2004 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2005 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2006 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2007 | { |
| 2008 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 2009 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2010 | psa_key_slot_t *slot; |
| 2011 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2012 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2013 | *p_drv = NULL; |
| 2014 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2015 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 2016 | if( status != PSA_SUCCESS ) |
| 2017 | return( status ); |
| 2018 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2019 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2020 | if( status != PSA_SUCCESS ) |
| 2021 | return( status ); |
| 2022 | slot = *p_slot; |
| 2023 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2024 | /* We're storing the declared bit-size of the key. It's up to each |
| 2025 | * creation mechanism to verify that this information is correct. |
| 2026 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2027 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2028 | * is optional (import, copy). In case of a volatile key, assign it the |
| 2029 | * volatile key identifier associated to the slot returned to contain its |
| 2030 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2031 | |
| 2032 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2033 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 2034 | { |
| 2035 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 2036 | slot->attr.id = volatile_key_id; |
| 2037 | #else |
| 2038 | slot->attr.id.key_id = volatile_key_id; |
| 2039 | #endif |
| 2040 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 2041 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2042 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2043 | * external-only flags, query `attributes`. Thanks to the check |
| 2044 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 2045 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2046 | * may have set. */ |
| 2047 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2048 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2049 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2050 | /* 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] | 2051 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2052 | * create the key file in internal storage, create the |
| 2053 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2054 | * persistent data. This is done by starting a transaction that will |
| 2055 | * encompass these three actions. |
| 2056 | * For registering a volatile key, we just need to find an appropriate |
| 2057 | * slot number inside the SE. Since the key is designated volatile, creating |
| 2058 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2059 | /* The first thing to do is to find a slot number for the new key. |
| 2060 | * We save the slot number in persistent storage as part of the |
| 2061 | * transaction data. It will be needed to recover if the power |
| 2062 | * fails during the key creation process, to clean up on the secure |
| 2063 | * element side after restarting. Obtaining a slot number from the |
| 2064 | * secure element driver updates its persistent state, but we do not yet |
| 2065 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2066 | * 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] | 2067 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2068 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 2069 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2070 | &slot->data.se.slot_number ); |
| 2071 | if( status != PSA_SUCCESS ) |
| 2072 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2073 | |
| 2074 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2075 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2076 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 2077 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 2078 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 2079 | psa_crypto_transaction.key.id = slot->attr.id; |
| 2080 | status = psa_crypto_save_transaction( ); |
| 2081 | if( status != PSA_SUCCESS ) |
| 2082 | { |
| 2083 | (void) psa_crypto_stop_transaction( ); |
| 2084 | return( status ); |
| 2085 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2086 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2087 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2088 | |
| 2089 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2090 | { |
| 2091 | /* Key registration only makes sense with a secure element. */ |
| 2092 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2093 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2094 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2095 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2096 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2097 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2098 | |
| 2099 | /** Finalize the creation of a key once its key material has been set. |
| 2100 | * |
| 2101 | * This entails writing the key to persistent storage. |
| 2102 | * |
| 2103 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2104 | * See the documentation of psa_start_key_creation() for the intended use |
| 2105 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2106 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2107 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 2108 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 2109 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2110 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2111 | * \param[in,out] slot Pointer to the slot with key material. |
| 2112 | * \param[in] driver The secure element driver for the key, |
| 2113 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2114 | * \param[out] key On success, identifier of the key. Note that the |
| 2115 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2116 | * |
| 2117 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2118 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2119 | * \return If this function fails, the key slot is an invalid state. |
| 2120 | * 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] | 2121 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2122 | static psa_status_t psa_finish_key_creation( |
| 2123 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2124 | psa_se_drv_table_entry_t *driver, |
| 2125 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2126 | { |
| 2127 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2128 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2129 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2130 | |
| 2131 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2132 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2133 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2134 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2135 | if( driver != NULL ) |
| 2136 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2137 | psa_se_key_data_storage_t data; |
| 2138 | #if defined(static_assert) |
| 2139 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 2140 | sizeof( data.slot_number ), |
| 2141 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2142 | #endif |
| 2143 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 2144 | sizeof( slot->data.se.slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2145 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2146 | (uint8_t*) &data, |
| 2147 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2148 | } |
| 2149 | else |
| 2150 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2151 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2152 | /* Key material is saved in export representation in the slot, so |
| 2153 | * just pass the slot buffer for storage. */ |
| 2154 | status = psa_save_persistent_key( &slot->attr, |
| 2155 | slot->data.key.data, |
| 2156 | slot->data.key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2157 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2158 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2159 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2160 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2161 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2162 | /* Finish the transaction for a key creation. This does not |
| 2163 | * happen when registering an existing key. Detect this case |
| 2164 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2165 | * creation of a persistent key in a secure element requires a transaction, |
| 2166 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2167 | if( driver != NULL && |
| 2168 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2169 | { |
| 2170 | status = psa_save_se_persistent_data( driver ); |
| 2171 | if( status != PSA_SUCCESS ) |
| 2172 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2173 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2174 | return( status ); |
| 2175 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2176 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2177 | } |
| 2178 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2179 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2180 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2181 | { |
| 2182 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2183 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2184 | if( status != PSA_SUCCESS ) |
| 2185 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2186 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2187 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2188 | return( status ); |
| 2189 | } |
| 2190 | |
| 2191 | /** Abort the creation of a key. |
| 2192 | * |
| 2193 | * You may call this function after calling psa_start_key_creation(), |
| 2194 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2195 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2196 | * See the documentation of psa_start_key_creation() for the intended use |
| 2197 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2198 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2199 | * \param[in,out] slot Pointer to the slot with key material. |
| 2200 | * \param[in] driver The secure element driver for the key, |
| 2201 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2202 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2203 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2204 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2205 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2206 | (void) driver; |
| 2207 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2208 | if( slot == NULL ) |
| 2209 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2210 | |
| 2211 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2212 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2213 | * element, and the failure happened later (when saving metadata |
| 2214 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2215 | * element. |
| 2216 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2217 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2219 | /* Abort the ongoing transaction if any (there may not be one if |
| 2220 | * the creation process failed before starting one, or if the |
| 2221 | * key creation is a registration of a key in a secure element). |
| 2222 | * Earlier functions must already have done what it takes to undo any |
| 2223 | * partial creation. All that's left is to update the transaction data |
| 2224 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2225 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2226 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2227 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2228 | psa_wipe_key_slot( slot ); |
| 2229 | } |
| 2230 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2231 | /** Validate optional attributes during key creation. |
| 2232 | * |
| 2233 | * Some key attributes are optional during key creation. If they are |
| 2234 | * specified in the attributes structure, check that they are consistent |
| 2235 | * with the data in the slot. |
| 2236 | * |
| 2237 | * This function should be called near the end of key creation, after |
| 2238 | * the slot in memory is fully populated but before saving persistent data. |
| 2239 | */ |
| 2240 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2241 | const psa_key_slot_t *slot, |
| 2242 | const psa_key_attributes_t *attributes ) |
| 2243 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2244 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2245 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2246 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2248 | } |
| 2249 | |
| 2250 | if( attributes->domain_parameters_size != 0 ) |
| 2251 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2252 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2253 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2254 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2255 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2256 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2257 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2258 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2259 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2260 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2261 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2262 | slot->data.key.data, |
| 2263 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2264 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2265 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2266 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2267 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2268 | mbedtls_mpi_init( &actual ); |
| 2269 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2270 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2271 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2272 | mbedtls_rsa_free( rsa ); |
| 2273 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2274 | if( ret != 0 ) |
| 2275 | goto rsa_exit; |
| 2276 | ret = mbedtls_mpi_read_binary( &required, |
| 2277 | attributes->domain_parameters, |
| 2278 | attributes->domain_parameters_size ); |
| 2279 | if( ret != 0 ) |
| 2280 | goto rsa_exit; |
| 2281 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2282 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2283 | rsa_exit: |
| 2284 | mbedtls_mpi_free( &actual ); |
| 2285 | mbedtls_mpi_free( &required ); |
| 2286 | if( ret != 0) |
| 2287 | return( mbedtls_to_psa_error( ret ) ); |
| 2288 | } |
| 2289 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2290 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2291 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2292 | { |
| 2293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2294 | } |
| 2295 | } |
| 2296 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2297 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2298 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2299 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2300 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2301 | } |
| 2302 | |
| 2303 | return( PSA_SUCCESS ); |
| 2304 | } |
| 2305 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2306 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2307 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2308 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2309 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2310 | { |
| 2311 | psa_status_t status; |
| 2312 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2313 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2314 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2315 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2316 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2317 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2318 | * This also rejects any key which might be encoded as an empty string, |
| 2319 | * which is never valid. */ |
| 2320 | if( data_length == 0 ) |
| 2321 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2322 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2323 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2324 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2325 | if( status != PSA_SUCCESS ) |
| 2326 | goto exit; |
| 2327 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2328 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2329 | if( driver != NULL ) |
| 2330 | { |
| 2331 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2332 | /* The driver should set the number of key bits, however in |
| 2333 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2334 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2335 | if( drv->key_management == NULL || |
| 2336 | drv->key_management->p_import == NULL ) |
| 2337 | { |
| 2338 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2339 | goto exit; |
| 2340 | } |
| 2341 | status = drv->key_management->p_import( |
| 2342 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2343 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2344 | &bits ); |
| 2345 | if( status != PSA_SUCCESS ) |
| 2346 | goto exit; |
| 2347 | if( bits > PSA_MAX_KEY_BITS ) |
| 2348 | { |
| 2349 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2350 | goto exit; |
| 2351 | } |
| 2352 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2353 | } |
| 2354 | else |
| 2355 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2356 | { |
| 2357 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2358 | if( status != PSA_SUCCESS ) |
| 2359 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2360 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2361 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2362 | if( status != PSA_SUCCESS ) |
| 2363 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2364 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2365 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2366 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2367 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2368 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2369 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2370 | return( status ); |
| 2371 | } |
| 2372 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2373 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2374 | psa_status_t mbedtls_psa_register_se_key( |
| 2375 | const psa_key_attributes_t *attributes ) |
| 2376 | { |
| 2377 | psa_status_t status; |
| 2378 | psa_key_slot_t *slot = NULL; |
| 2379 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2380 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2381 | |
| 2382 | /* Leaving attributes unspecified is not currently supported. |
| 2383 | * It could make sense to query the key type and size from the |
| 2384 | * secure element, but not all secure elements support this |
| 2385 | * and the driver HAL doesn't currently support it. */ |
| 2386 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2387 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2388 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2389 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2390 | |
| 2391 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2392 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2393 | if( status != PSA_SUCCESS ) |
| 2394 | goto exit; |
| 2395 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2396 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2397 | |
| 2398 | exit: |
| 2399 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2400 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2401 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2402 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2403 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2404 | return( status ); |
| 2405 | } |
| 2406 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2407 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2408 | 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] | 2409 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2410 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2411 | psa_status_t status = psa_copy_key_material_into_slot( target, |
| 2412 | source->data.key.data, |
| 2413 | source->data.key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2414 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2415 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2416 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2417 | target->attr.type = source->attr.type; |
| 2418 | target->attr.bits = source->attr.bits; |
| 2419 | |
| 2420 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2421 | } |
| 2422 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2423 | 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] | 2424 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2425 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2426 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2427 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2428 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2429 | psa_key_slot_t *source_slot = NULL; |
| 2430 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2431 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2432 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2433 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2434 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2435 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2436 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2437 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2438 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2439 | goto exit; |
| 2440 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2441 | status = psa_validate_optional_attributes( source_slot, |
| 2442 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2443 | if( status != PSA_SUCCESS ) |
| 2444 | goto exit; |
| 2445 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2446 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2447 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2448 | if( status != PSA_SUCCESS ) |
| 2449 | goto exit; |
| 2450 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2451 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2452 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2453 | if( status != PSA_SUCCESS ) |
| 2454 | goto exit; |
| 2455 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2456 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2457 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2458 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2459 | /* Copying to a secure element is not implemented yet. */ |
| 2460 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2461 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2462 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2463 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2464 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2465 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2466 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2467 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2468 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2469 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2470 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2471 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2472 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2473 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2474 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2475 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2476 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2477 | } |
| 2478 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2479 | |
| 2480 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2481 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2482 | /* Message digests */ |
| 2483 | /****************************************************************/ |
| 2484 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2485 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2486 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2487 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2488 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2489 | 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] | 2490 | { |
| 2491 | switch( alg ) |
| 2492 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2493 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2494 | case PSA_ALG_MD2: |
| 2495 | return( &mbedtls_md2_info ); |
| 2496 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2497 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2498 | case PSA_ALG_MD4: |
| 2499 | return( &mbedtls_md4_info ); |
| 2500 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2501 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2502 | case PSA_ALG_MD5: |
| 2503 | return( &mbedtls_md5_info ); |
| 2504 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2505 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2506 | case PSA_ALG_RIPEMD160: |
| 2507 | return( &mbedtls_ripemd160_info ); |
| 2508 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2509 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2510 | case PSA_ALG_SHA_1: |
| 2511 | return( &mbedtls_sha1_info ); |
| 2512 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2513 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2514 | case PSA_ALG_SHA_224: |
| 2515 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2516 | #endif |
| 2517 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2518 | case PSA_ALG_SHA_256: |
| 2519 | return( &mbedtls_sha256_info ); |
| 2520 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2521 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2522 | case PSA_ALG_SHA_384: |
| 2523 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2524 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2525 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2526 | case PSA_ALG_SHA_512: |
| 2527 | return( &mbedtls_sha512_info ); |
| 2528 | #endif |
| 2529 | default: |
| 2530 | return( NULL ); |
| 2531 | } |
| 2532 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2533 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2534 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2535 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2536 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2537 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2538 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2539 | { |
| 2540 | switch( operation->alg ) |
| 2541 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2542 | case 0: |
| 2543 | /* The object has (apparently) been initialized but it is not |
| 2544 | * in use. It's ok to call abort on such an object, and there's |
| 2545 | * nothing to do. */ |
| 2546 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2547 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2548 | case PSA_ALG_MD2: |
| 2549 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2550 | break; |
| 2551 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2552 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2553 | case PSA_ALG_MD4: |
| 2554 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2555 | break; |
| 2556 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2557 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2558 | case PSA_ALG_MD5: |
| 2559 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2560 | break; |
| 2561 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2562 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2563 | case PSA_ALG_RIPEMD160: |
| 2564 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2565 | break; |
| 2566 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2567 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2568 | case PSA_ALG_SHA_1: |
| 2569 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2570 | break; |
| 2571 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2572 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2573 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2574 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2575 | break; |
| 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_free( &operation->ctx.sha256 ); |
| 2580 | break; |
| 2581 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2582 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2583 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2584 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2585 | break; |
| 2586 | #endif |
| 2587 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2588 | case PSA_ALG_SHA_512: |
| 2589 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2590 | break; |
| 2591 | #endif |
| 2592 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2593 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2594 | } |
| 2595 | operation->alg = 0; |
| 2596 | return( PSA_SUCCESS ); |
| 2597 | } |
| 2598 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2599 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2600 | psa_algorithm_t alg ) |
| 2601 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2602 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2603 | |
| 2604 | /* A context must be freshly initialized before it can be set up. */ |
| 2605 | if( operation->alg != 0 ) |
| 2606 | { |
| 2607 | return( PSA_ERROR_BAD_STATE ); |
| 2608 | } |
| 2609 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2610 | switch( alg ) |
| 2611 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2612 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2613 | case PSA_ALG_MD2: |
| 2614 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2615 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2616 | break; |
| 2617 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2618 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2619 | case PSA_ALG_MD4: |
| 2620 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2621 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2622 | break; |
| 2623 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2624 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2625 | case PSA_ALG_MD5: |
| 2626 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2627 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2628 | break; |
| 2629 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2630 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2631 | case PSA_ALG_RIPEMD160: |
| 2632 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2633 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2634 | break; |
| 2635 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2636 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2637 | case PSA_ALG_SHA_1: |
| 2638 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2639 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2640 | break; |
| 2641 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2642 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2643 | case PSA_ALG_SHA_224: |
| 2644 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2645 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2646 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2647 | #endif |
| 2648 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2649 | case PSA_ALG_SHA_256: |
| 2650 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2651 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2652 | break; |
| 2653 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2654 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2655 | case PSA_ALG_SHA_384: |
| 2656 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2657 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2658 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2659 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2660 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2661 | case PSA_ALG_SHA_512: |
| 2662 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2663 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2664 | break; |
| 2665 | #endif |
| 2666 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2667 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2668 | PSA_ERROR_NOT_SUPPORTED : |
| 2669 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2670 | } |
| 2671 | if( ret == 0 ) |
| 2672 | operation->alg = alg; |
| 2673 | else |
| 2674 | psa_hash_abort( operation ); |
| 2675 | return( mbedtls_to_psa_error( ret ) ); |
| 2676 | } |
| 2677 | |
| 2678 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2679 | const uint8_t *input, |
| 2680 | size_t input_length ) |
| 2681 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2682 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2683 | |
| 2684 | /* Don't require hash implementations to behave correctly on a |
| 2685 | * zero-length input, which may have an invalid pointer. */ |
| 2686 | if( input_length == 0 ) |
| 2687 | return( PSA_SUCCESS ); |
| 2688 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2689 | switch( operation->alg ) |
| 2690 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2691 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2692 | case PSA_ALG_MD2: |
| 2693 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2694 | input, input_length ); |
| 2695 | break; |
| 2696 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2697 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2698 | case PSA_ALG_MD4: |
| 2699 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2700 | input, input_length ); |
| 2701 | break; |
| 2702 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2703 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2704 | case PSA_ALG_MD5: |
| 2705 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2706 | input, input_length ); |
| 2707 | break; |
| 2708 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2709 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2710 | case PSA_ALG_RIPEMD160: |
| 2711 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2712 | input, input_length ); |
| 2713 | break; |
| 2714 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2715 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2716 | case PSA_ALG_SHA_1: |
| 2717 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2718 | input, input_length ); |
| 2719 | break; |
| 2720 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2721 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2722 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2723 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2724 | input, input_length ); |
| 2725 | break; |
| 2726 | #endif |
| 2727 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2728 | case PSA_ALG_SHA_256: |
| 2729 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2730 | input, input_length ); |
| 2731 | break; |
| 2732 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2733 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2734 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2735 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2736 | input, input_length ); |
| 2737 | break; |
| 2738 | #endif |
| 2739 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2740 | case PSA_ALG_SHA_512: |
| 2741 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2742 | input, input_length ); |
| 2743 | break; |
| 2744 | #endif |
| 2745 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2746 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2747 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2748 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2749 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2750 | if( ret != 0 ) |
| 2751 | psa_hash_abort( operation ); |
| 2752 | return( mbedtls_to_psa_error( ret ) ); |
| 2753 | } |
| 2754 | |
| 2755 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2756 | uint8_t *hash, |
| 2757 | size_t hash_size, |
| 2758 | size_t *hash_length ) |
| 2759 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2760 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2761 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2762 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2763 | |
| 2764 | /* Fill the output buffer with something that isn't a valid hash |
| 2765 | * (barring an attack on the hash and deliberately-crafted input), |
| 2766 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2767 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2768 | /* If hash_size is 0 then hash may be NULL and then the |
| 2769 | * call to memset would have undefined behavior. */ |
| 2770 | if( hash_size != 0 ) |
| 2771 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2772 | |
| 2773 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2774 | { |
| 2775 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2776 | goto exit; |
| 2777 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2778 | |
| 2779 | switch( operation->alg ) |
| 2780 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2781 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2782 | case PSA_ALG_MD2: |
| 2783 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2784 | break; |
| 2785 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2786 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2787 | case PSA_ALG_MD4: |
| 2788 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2789 | break; |
| 2790 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2791 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2792 | case PSA_ALG_MD5: |
| 2793 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2794 | break; |
| 2795 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2796 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2797 | case PSA_ALG_RIPEMD160: |
| 2798 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2799 | break; |
| 2800 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2801 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2802 | case PSA_ALG_SHA_1: |
| 2803 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2804 | break; |
| 2805 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2806 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2807 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2808 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2809 | break; |
| 2810 | #endif |
| 2811 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2812 | case PSA_ALG_SHA_256: |
| 2813 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2814 | break; |
| 2815 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2816 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2817 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2818 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2819 | break; |
| 2820 | #endif |
| 2821 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2822 | case PSA_ALG_SHA_512: |
| 2823 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2824 | break; |
| 2825 | #endif |
| 2826 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2827 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2828 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2829 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2830 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2831 | exit: |
| 2832 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2833 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2834 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2835 | return( psa_hash_abort( operation ) ); |
| 2836 | } |
| 2837 | else |
| 2838 | { |
| 2839 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2840 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2841 | } |
| 2842 | } |
| 2843 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2844 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2845 | const uint8_t *hash, |
| 2846 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2847 | { |
| 2848 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2849 | size_t actual_hash_length; |
| 2850 | psa_status_t status = psa_hash_finish( operation, |
| 2851 | actual_hash, sizeof( actual_hash ), |
| 2852 | &actual_hash_length ); |
| 2853 | if( status != PSA_SUCCESS ) |
| 2854 | return( status ); |
| 2855 | if( actual_hash_length != hash_length ) |
| 2856 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2857 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2858 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2859 | return( PSA_SUCCESS ); |
| 2860 | } |
| 2861 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2862 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2863 | const uint8_t *input, size_t input_length, |
| 2864 | uint8_t *hash, size_t hash_size, |
| 2865 | size_t *hash_length ) |
| 2866 | { |
| 2867 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2868 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2869 | |
| 2870 | *hash_length = hash_size; |
| 2871 | status = psa_hash_setup( &operation, alg ); |
| 2872 | if( status != PSA_SUCCESS ) |
| 2873 | goto exit; |
| 2874 | status = psa_hash_update( &operation, input, input_length ); |
| 2875 | if( status != PSA_SUCCESS ) |
| 2876 | goto exit; |
| 2877 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2878 | if( status != PSA_SUCCESS ) |
| 2879 | goto exit; |
| 2880 | |
| 2881 | exit: |
| 2882 | if( status == PSA_SUCCESS ) |
| 2883 | status = psa_hash_abort( &operation ); |
| 2884 | else |
| 2885 | psa_hash_abort( &operation ); |
| 2886 | return( status ); |
| 2887 | } |
| 2888 | |
| 2889 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2890 | const uint8_t *input, size_t input_length, |
| 2891 | const uint8_t *hash, size_t hash_length ) |
| 2892 | { |
| 2893 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2894 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2895 | |
| 2896 | status = psa_hash_setup( &operation, alg ); |
| 2897 | if( status != PSA_SUCCESS ) |
| 2898 | goto exit; |
| 2899 | status = psa_hash_update( &operation, input, input_length ); |
| 2900 | if( status != PSA_SUCCESS ) |
| 2901 | goto exit; |
| 2902 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2903 | if( status != PSA_SUCCESS ) |
| 2904 | goto exit; |
| 2905 | |
| 2906 | exit: |
| 2907 | if( status == PSA_SUCCESS ) |
| 2908 | status = psa_hash_abort( &operation ); |
| 2909 | else |
| 2910 | psa_hash_abort( &operation ); |
| 2911 | return( status ); |
| 2912 | } |
| 2913 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2914 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2915 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2916 | { |
| 2917 | if( target_operation->alg != 0 ) |
| 2918 | return( PSA_ERROR_BAD_STATE ); |
| 2919 | |
| 2920 | switch( source_operation->alg ) |
| 2921 | { |
| 2922 | case 0: |
| 2923 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2924 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2925 | case PSA_ALG_MD2: |
| 2926 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2927 | &source_operation->ctx.md2 ); |
| 2928 | break; |
| 2929 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2930 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2931 | case PSA_ALG_MD4: |
| 2932 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2933 | &source_operation->ctx.md4 ); |
| 2934 | break; |
| 2935 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2936 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2937 | case PSA_ALG_MD5: |
| 2938 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2939 | &source_operation->ctx.md5 ); |
| 2940 | break; |
| 2941 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2942 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2943 | case PSA_ALG_RIPEMD160: |
| 2944 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2945 | &source_operation->ctx.ripemd160 ); |
| 2946 | break; |
| 2947 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2948 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2949 | case PSA_ALG_SHA_1: |
| 2950 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2951 | &source_operation->ctx.sha1 ); |
| 2952 | break; |
| 2953 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2954 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2955 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2956 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2957 | &source_operation->ctx.sha256 ); |
| 2958 | break; |
| 2959 | #endif |
| 2960 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2961 | case PSA_ALG_SHA_256: |
| 2962 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2963 | &source_operation->ctx.sha256 ); |
| 2964 | break; |
| 2965 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2966 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2967 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2968 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2969 | &source_operation->ctx.sha512 ); |
| 2970 | break; |
| 2971 | #endif |
| 2972 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2973 | case PSA_ALG_SHA_512: |
| 2974 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2975 | &source_operation->ctx.sha512 ); |
| 2976 | break; |
| 2977 | #endif |
| 2978 | default: |
| 2979 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2980 | } |
| 2981 | |
| 2982 | target_operation->alg = source_operation->alg; |
| 2983 | return( PSA_SUCCESS ); |
| 2984 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2985 | |
| 2986 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2987 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2988 | /* MAC */ |
| 2989 | /****************************************************************/ |
| 2990 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2991 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2992 | psa_algorithm_t alg, |
| 2993 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2994 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2995 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2996 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2997 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2998 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2999 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3000 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3001 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3002 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3003 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 3004 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 3005 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3006 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 3007 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3008 | mode = MBEDTLS_MODE_STREAM; |
| 3009 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3010 | case PSA_ALG_CTR: |
| 3011 | mode = MBEDTLS_MODE_CTR; |
| 3012 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3013 | case PSA_ALG_CFB: |
| 3014 | mode = MBEDTLS_MODE_CFB; |
| 3015 | break; |
| 3016 | case PSA_ALG_OFB: |
| 3017 | mode = MBEDTLS_MODE_OFB; |
| 3018 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3019 | case PSA_ALG_ECB_NO_PADDING: |
| 3020 | mode = MBEDTLS_MODE_ECB; |
| 3021 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3022 | case PSA_ALG_CBC_NO_PADDING: |
| 3023 | mode = MBEDTLS_MODE_CBC; |
| 3024 | break; |
| 3025 | case PSA_ALG_CBC_PKCS7: |
| 3026 | mode = MBEDTLS_MODE_CBC; |
| 3027 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3028 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3029 | mode = MBEDTLS_MODE_CCM; |
| 3030 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3031 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3032 | mode = MBEDTLS_MODE_GCM; |
| 3033 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3034 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3035 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 3036 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3037 | default: |
| 3038 | return( NULL ); |
| 3039 | } |
| 3040 | } |
| 3041 | else if( alg == PSA_ALG_CMAC ) |
| 3042 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3043 | else |
| 3044 | return( NULL ); |
| 3045 | |
| 3046 | switch( key_type ) |
| 3047 | { |
| 3048 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3049 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3050 | break; |
| 3051 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3052 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 3053 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3054 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3055 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3056 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3057 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3058 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 3059 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 3060 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 3061 | if( key_bits == 128 ) |
| 3062 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3063 | break; |
| 3064 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3065 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3066 | break; |
| 3067 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3068 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3069 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3070 | case PSA_KEY_TYPE_CHACHA20: |
| 3071 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 3072 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3073 | default: |
| 3074 | return( NULL ); |
| 3075 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3076 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3077 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3078 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3079 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 3080 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3081 | } |
| 3082 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3083 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3084 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3085 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3086 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3087 | { |
| 3088 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3089 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3090 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3091 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3092 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3093 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3094 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3095 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3096 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3097 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3098 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3099 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3100 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3101 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3102 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3103 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3104 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3105 | return( 128 ); |
| 3106 | default: |
| 3107 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3108 | } |
| 3109 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3110 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 3111 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3112 | /* Initialize the MAC operation structure. Once this function has been |
| 3113 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3114 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3115 | psa_algorithm_t alg ) |
| 3116 | { |
| 3117 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3118 | |
| 3119 | operation->alg = alg; |
| 3120 | operation->key_set = 0; |
| 3121 | operation->iv_set = 0; |
| 3122 | operation->iv_required = 0; |
| 3123 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3124 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3125 | |
| 3126 | #if defined(MBEDTLS_CMAC_C) |
| 3127 | if( alg == PSA_ALG_CMAC ) |
| 3128 | { |
| 3129 | operation->iv_required = 0; |
| 3130 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3131 | status = PSA_SUCCESS; |
| 3132 | } |
| 3133 | else |
| 3134 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3135 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3136 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3137 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3138 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3139 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3140 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3141 | } |
| 3142 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3143 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3145 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3146 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | if( status != PSA_SUCCESS ) |
| 3150 | memset( operation, 0, sizeof( *operation ) ); |
| 3151 | return( status ); |
| 3152 | } |
| 3153 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3154 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3155 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3156 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3157 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3158 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3159 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3160 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3161 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3162 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3163 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3164 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3165 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3166 | /* The object has (apparently) been initialized but it is not |
| 3167 | * in use. It's ok to call abort on such an object, and there's |
| 3168 | * nothing to do. */ |
| 3169 | return( PSA_SUCCESS ); |
| 3170 | } |
| 3171 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3172 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3173 | if( operation->alg == PSA_ALG_CMAC ) |
| 3174 | { |
| 3175 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3176 | } |
| 3177 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3178 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3179 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3180 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3181 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3182 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3183 | } |
| 3184 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3185 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3186 | { |
| 3187 | /* Sanity check (shouldn't happen: operation->alg should |
| 3188 | * always have been initialized to a valid value). */ |
| 3189 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3190 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3191 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3192 | operation->alg = 0; |
| 3193 | operation->key_set = 0; |
| 3194 | operation->iv_set = 0; |
| 3195 | operation->iv_required = 0; |
| 3196 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3197 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3198 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3199 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3200 | |
| 3201 | bad_state: |
| 3202 | /* If abort is called on an uninitialized object, we can't trust |
| 3203 | * anything. Wipe the object in case it contains confidential data. |
| 3204 | * This may result in a memory leak if a pointer gets overwritten, |
| 3205 | * but it's too late to do anything about this. */ |
| 3206 | memset( operation, 0, sizeof( *operation ) ); |
| 3207 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3208 | } |
| 3209 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3210 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3211 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3212 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3213 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3214 | const mbedtls_cipher_info_t *cipher_info ) |
| 3215 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3216 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3217 | |
| 3218 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3219 | |
| 3220 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3221 | if( ret != 0 ) |
| 3222 | return( ret ); |
| 3223 | |
| 3224 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3225 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3226 | key_bits ); |
| 3227 | return( ret ); |
| 3228 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3229 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3230 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3231 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3232 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3233 | const uint8_t *key, |
| 3234 | size_t key_length, |
| 3235 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3236 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3237 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3238 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3239 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3240 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3241 | psa_status_t status; |
| 3242 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3243 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3244 | * overflow below. This should never trigger if the hash algorithm |
| 3245 | * is implemented correctly. */ |
| 3246 | /* The size checks against the ipad and opad buffers cannot be written |
| 3247 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3248 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3249 | if( block_size > sizeof( ipad ) ) |
| 3250 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3251 | if( block_size > sizeof( hmac->opad ) ) |
| 3252 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3253 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3254 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3255 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3256 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3257 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3258 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3259 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3260 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3261 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3262 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3263 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3264 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3265 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3266 | * an invalid pointer which would make the behavior undefined. */ |
| 3267 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3268 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3269 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3270 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3271 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3272 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3273 | ipad[i] ^= 0x36; |
| 3274 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3275 | |
| 3276 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3277 | * and filling the rest of opad with the requisite constant. */ |
| 3278 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3279 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3280 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3282 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3283 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3284 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3285 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3286 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3287 | |
| 3288 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3289 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3290 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3291 | return( status ); |
| 3292 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3293 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3294 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3295 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3296 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3297 | psa_algorithm_t alg, |
| 3298 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3299 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3300 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3301 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3302 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3303 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3304 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3305 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3306 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3307 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3308 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3309 | /* A context must be freshly initialized before it can be set up. */ |
| 3310 | if( operation->alg != 0 ) |
| 3311 | { |
| 3312 | return( PSA_ERROR_BAD_STATE ); |
| 3313 | } |
| 3314 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3315 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3316 | if( status != PSA_SUCCESS ) |
| 3317 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3318 | if( is_sign ) |
| 3319 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3320 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3321 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3322 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3323 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3324 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3325 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3326 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3327 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3328 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3329 | { |
| 3330 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3331 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3332 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3333 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3334 | if( cipher_info == NULL ) |
| 3335 | { |
| 3336 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3337 | goto exit; |
| 3338 | } |
| 3339 | operation->mac_size = cipher_info->block_size; |
| 3340 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3341 | status = mbedtls_to_psa_error( ret ); |
| 3342 | } |
| 3343 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3344 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3345 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3346 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3347 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3348 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3349 | if( hash_alg == 0 ) |
| 3350 | { |
| 3351 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3352 | goto exit; |
| 3353 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3354 | |
| 3355 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3356 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3357 | if( operation->mac_size == 0 || |
| 3358 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3359 | { |
| 3360 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3361 | goto exit; |
| 3362 | } |
| 3363 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3364 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3365 | { |
| 3366 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3367 | goto exit; |
| 3368 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3370 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3371 | slot->data.key.data, |
| 3372 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3373 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3374 | } |
| 3375 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3376 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3377 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3378 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3379 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3380 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3381 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3382 | if( truncated == 0 ) |
| 3383 | { |
| 3384 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3385 | } |
| 3386 | else if( truncated < 4 ) |
| 3387 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3388 | /* A very short MAC is too short for security since it can be |
| 3389 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3390 | * so we make this our minimum, even though 32 bits is still |
| 3391 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3392 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3393 | } |
| 3394 | else if( truncated > operation->mac_size ) |
| 3395 | { |
| 3396 | /* It's impossible to "truncate" to a larger length. */ |
| 3397 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3398 | } |
| 3399 | else |
| 3400 | operation->mac_size = truncated; |
| 3401 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3402 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3403 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3404 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3405 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3406 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3407 | else |
| 3408 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3409 | operation->key_set = 1; |
| 3410 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3411 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3412 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3413 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3414 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3415 | } |
| 3416 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3417 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3418 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3419 | psa_algorithm_t alg ) |
| 3420 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3421 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3425 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3426 | psa_algorithm_t alg ) |
| 3427 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3428 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3429 | } |
| 3430 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3431 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3432 | const uint8_t *input, |
| 3433 | size_t input_length ) |
| 3434 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3435 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3436 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3437 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3438 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3439 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3440 | operation->has_input = 1; |
| 3441 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3442 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3443 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3444 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3445 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3446 | input, input_length ); |
| 3447 | status = mbedtls_to_psa_error( ret ); |
| 3448 | } |
| 3449 | else |
| 3450 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3451 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3452 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3453 | { |
| 3454 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3455 | input_length ); |
| 3456 | } |
| 3457 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3458 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3459 | { |
| 3460 | /* This shouldn't happen if `operation` was initialized by |
| 3461 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3462 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3463 | } |
| 3464 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3465 | if( status != PSA_SUCCESS ) |
| 3466 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3467 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3468 | } |
| 3469 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3470 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3471 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3472 | uint8_t *mac, |
| 3473 | size_t mac_size ) |
| 3474 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3475 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3476 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3477 | size_t hash_size = 0; |
| 3478 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3479 | psa_status_t status; |
| 3480 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3481 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3482 | if( status != PSA_SUCCESS ) |
| 3483 | return( status ); |
| 3484 | /* From here on, tmp needs to be wiped. */ |
| 3485 | |
| 3486 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3487 | if( status != PSA_SUCCESS ) |
| 3488 | goto exit; |
| 3489 | |
| 3490 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3491 | if( status != PSA_SUCCESS ) |
| 3492 | goto exit; |
| 3493 | |
| 3494 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3495 | if( status != PSA_SUCCESS ) |
| 3496 | goto exit; |
| 3497 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3498 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3499 | if( status != PSA_SUCCESS ) |
| 3500 | goto exit; |
| 3501 | |
| 3502 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3503 | |
| 3504 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3505 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3506 | return( status ); |
| 3507 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3508 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3509 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3510 | 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] | 3511 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3512 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3513 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3514 | if( ! operation->key_set ) |
| 3515 | return( PSA_ERROR_BAD_STATE ); |
| 3516 | if( operation->iv_required && ! operation->iv_set ) |
| 3517 | return( PSA_ERROR_BAD_STATE ); |
| 3518 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3519 | if( mac_size < operation->mac_size ) |
| 3520 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3521 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3522 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3523 | if( operation->alg == PSA_ALG_CMAC ) |
| 3524 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3525 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3526 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3527 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3528 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3529 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3530 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3531 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3532 | else |
| 3533 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3534 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3535 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3536 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3537 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3538 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3539 | } |
| 3540 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3541 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3542 | { |
| 3543 | /* This shouldn't happen if `operation` was initialized by |
| 3544 | * a setup function. */ |
| 3545 | return( PSA_ERROR_BAD_STATE ); |
| 3546 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3549 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3550 | uint8_t *mac, |
| 3551 | size_t mac_size, |
| 3552 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3553 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3554 | psa_status_t status; |
| 3555 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3556 | if( operation->alg == 0 ) |
| 3557 | { |
| 3558 | return( PSA_ERROR_BAD_STATE ); |
| 3559 | } |
| 3560 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3561 | /* Fill the output buffer with something that isn't a valid mac |
| 3562 | * (barring an attack on the mac and deliberately-crafted input), |
| 3563 | * in case the caller doesn't check the return status properly. */ |
| 3564 | *mac_length = mac_size; |
| 3565 | /* If mac_size is 0 then mac may be NULL and then the |
| 3566 | * call to memset would have undefined behavior. */ |
| 3567 | if( mac_size != 0 ) |
| 3568 | memset( mac, '!', mac_size ); |
| 3569 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3570 | if( ! operation->is_sign ) |
| 3571 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3572 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3573 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3574 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3575 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3576 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3577 | if( status == PSA_SUCCESS ) |
| 3578 | { |
| 3579 | status = psa_mac_abort( operation ); |
| 3580 | if( status == PSA_SUCCESS ) |
| 3581 | *mac_length = operation->mac_size; |
| 3582 | else |
| 3583 | memset( mac, '!', mac_size ); |
| 3584 | } |
| 3585 | else |
| 3586 | psa_mac_abort( operation ); |
| 3587 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3590 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3591 | const uint8_t *mac, |
| 3592 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3593 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3594 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3595 | psa_status_t status; |
| 3596 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3597 | if( operation->alg == 0 ) |
| 3598 | { |
| 3599 | return( PSA_ERROR_BAD_STATE ); |
| 3600 | } |
| 3601 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3602 | if( operation->is_sign ) |
| 3603 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3604 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3605 | } |
| 3606 | if( operation->mac_size != mac_length ) |
| 3607 | { |
| 3608 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3609 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3610 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3611 | |
| 3612 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3613 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3614 | if( status != PSA_SUCCESS ) |
| 3615 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3616 | |
| 3617 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3618 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3619 | |
| 3620 | cleanup: |
| 3621 | if( status == PSA_SUCCESS ) |
| 3622 | status = psa_mac_abort( operation ); |
| 3623 | else |
| 3624 | psa_mac_abort( operation ); |
| 3625 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3626 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3627 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3628 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3632 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3633 | /****************************************************************/ |
| 3634 | /* Asymmetric cryptography */ |
| 3635 | /****************************************************************/ |
| 3636 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3637 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3638 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3639 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3640 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3641 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3642 | size_t hash_length, |
| 3643 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3644 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3645 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3646 | 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] | 3647 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3648 | |
| 3649 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3650 | * parameters. Validate that it fits so that we don't risk an |
| 3651 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3652 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3653 | if( hash_length > UINT_MAX ) |
| 3654 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3655 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3656 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3657 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3658 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3659 | * must be correct. */ |
| 3660 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3661 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3662 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3663 | if( md_info == NULL ) |
| 3664 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3665 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3666 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3667 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3668 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3669 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3670 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3671 | /* PSS requires a hash internally. */ |
| 3672 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3673 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3674 | if( md_info == NULL ) |
| 3675 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3676 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3677 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3678 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3679 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3680 | } |
| 3681 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3682 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3683 | psa_algorithm_t alg, |
| 3684 | const uint8_t *hash, |
| 3685 | size_t hash_length, |
| 3686 | uint8_t *signature, |
| 3687 | size_t signature_size, |
| 3688 | size_t *signature_length ) |
| 3689 | { |
| 3690 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3691 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3692 | mbedtls_md_type_t md_alg; |
| 3693 | |
| 3694 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3695 | if( status != PSA_SUCCESS ) |
| 3696 | return( status ); |
| 3697 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3698 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3699 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3700 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3701 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3702 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3703 | { |
| 3704 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3705 | MBEDTLS_MD_NONE ); |
| 3706 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3707 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3708 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3709 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3710 | md_alg, |
| 3711 | (unsigned int) hash_length, |
| 3712 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3713 | signature ); |
| 3714 | } |
| 3715 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3716 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3717 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3718 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3719 | { |
| 3720 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3721 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3722 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3723 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3724 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3725 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3726 | (unsigned int) hash_length, |
| 3727 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3728 | signature ); |
| 3729 | } |
| 3730 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3731 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3732 | { |
| 3733 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3734 | } |
| 3735 | |
| 3736 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3737 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3738 | return( mbedtls_to_psa_error( ret ) ); |
| 3739 | } |
| 3740 | |
| 3741 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3742 | psa_algorithm_t alg, |
| 3743 | const uint8_t *hash, |
| 3744 | size_t hash_length, |
| 3745 | const uint8_t *signature, |
| 3746 | size_t signature_length ) |
| 3747 | { |
| 3748 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3749 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3750 | mbedtls_md_type_t md_alg; |
| 3751 | |
| 3752 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3753 | if( status != PSA_SUCCESS ) |
| 3754 | return( status ); |
| 3755 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3756 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3757 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3758 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3759 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3760 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3761 | { |
| 3762 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3763 | MBEDTLS_MD_NONE ); |
| 3764 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3765 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3766 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3767 | MBEDTLS_RSA_PUBLIC, |
| 3768 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3769 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3770 | hash, |
| 3771 | signature ); |
| 3772 | } |
| 3773 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3774 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3775 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3776 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3777 | { |
| 3778 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3779 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3780 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3781 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3782 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3783 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3784 | (unsigned int) hash_length, |
| 3785 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3786 | signature ); |
| 3787 | } |
| 3788 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3789 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3790 | { |
| 3791 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3792 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3793 | |
| 3794 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3795 | * the rest of the signature is invalid". This has little use in |
| 3796 | * practice and PSA doesn't report this distinction. */ |
| 3797 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3798 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3799 | return( mbedtls_to_psa_error( ret ) ); |
| 3800 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3801 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3802 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3803 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3804 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3805 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3806 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3807 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3808 | * (even though these functions don't modify it). */ |
| 3809 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3810 | psa_algorithm_t alg, |
| 3811 | const uint8_t *hash, |
| 3812 | size_t hash_length, |
| 3813 | uint8_t *signature, |
| 3814 | size_t signature_size, |
| 3815 | size_t *signature_length ) |
| 3816 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3817 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3818 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3819 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3820 | mbedtls_mpi_init( &r ); |
| 3821 | mbedtls_mpi_init( &s ); |
| 3822 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3823 | if( signature_size < 2 * curve_bytes ) |
| 3824 | { |
| 3825 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3826 | goto cleanup; |
| 3827 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3828 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3829 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3830 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3831 | { |
| 3832 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3833 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3834 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3835 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3836 | &ecp->d, hash, |
| 3837 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3838 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3839 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3840 | } |
| 3841 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3842 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3843 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3844 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3845 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3846 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3847 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3848 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3849 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3850 | |
| 3851 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3852 | signature, |
| 3853 | curve_bytes ) ); |
| 3854 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3855 | signature + curve_bytes, |
| 3856 | curve_bytes ) ); |
| 3857 | |
| 3858 | cleanup: |
| 3859 | mbedtls_mpi_free( &r ); |
| 3860 | mbedtls_mpi_free( &s ); |
| 3861 | if( ret == 0 ) |
| 3862 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3863 | return( mbedtls_to_psa_error( ret ) ); |
| 3864 | } |
| 3865 | |
| 3866 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3867 | const uint8_t *hash, |
| 3868 | size_t hash_length, |
| 3869 | const uint8_t *signature, |
| 3870 | size_t signature_length ) |
| 3871 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3872 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3873 | mbedtls_mpi r, s; |
| 3874 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3875 | mbedtls_mpi_init( &r ); |
| 3876 | mbedtls_mpi_init( &s ); |
| 3877 | |
| 3878 | if( signature_length != 2 * curve_bytes ) |
| 3879 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3880 | |
| 3881 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3882 | signature, |
| 3883 | curve_bytes ) ); |
| 3884 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3885 | signature + curve_bytes, |
| 3886 | curve_bytes ) ); |
| 3887 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3888 | /* Check whether the public part is loaded. If not, load it. */ |
| 3889 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3890 | { |
| 3891 | MBEDTLS_MPI_CHK( |
| 3892 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3893 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3896 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3897 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3898 | |
| 3899 | cleanup: |
| 3900 | mbedtls_mpi_free( &r ); |
| 3901 | mbedtls_mpi_free( &s ); |
| 3902 | return( mbedtls_to_psa_error( ret ) ); |
| 3903 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3904 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3905 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3906 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3907 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3908 | psa_algorithm_t alg, |
| 3909 | const uint8_t *hash, |
| 3910 | size_t hash_length, |
| 3911 | uint8_t *signature, |
| 3912 | size_t signature_size, |
| 3913 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3914 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3915 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3916 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3917 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3918 | |
| 3919 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3920 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3921 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3922 | * buffer can in principle be empty since it doesn't actually have |
| 3923 | * to be a hash.) */ |
| 3924 | if( signature_size == 0 ) |
| 3925 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3926 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3927 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3928 | PSA_KEY_USAGE_SIGN_HASH, |
| 3929 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3930 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3931 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3932 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3933 | { |
| 3934 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3935 | goto exit; |
| 3936 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3937 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3938 | /* Try any of the available accelerators first */ |
| 3939 | status = psa_driver_wrapper_sign_hash( slot, |
| 3940 | alg, |
| 3941 | hash, |
| 3942 | hash_length, |
| 3943 | signature, |
| 3944 | signature_size, |
| 3945 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3946 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3947 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3948 | goto exit; |
| 3949 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3950 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3951 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3952 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3953 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3954 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3955 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3956 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3957 | status = psa_load_rsa_representation( slot->attr.type, |
| 3958 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3959 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3960 | &rsa ); |
| 3961 | if( status != PSA_SUCCESS ) |
| 3962 | goto exit; |
| 3963 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3964 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3965 | alg, |
| 3966 | hash, hash_length, |
| 3967 | signature, signature_size, |
| 3968 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3969 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3970 | mbedtls_rsa_free( rsa ); |
| 3971 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3972 | } |
| 3973 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3974 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3975 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3976 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3977 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3978 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3979 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3980 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3981 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3982 | PSA_ALG_IS_ECDSA( alg ) |
| 3983 | #else |
| 3984 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3985 | #endif |
| 3986 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3987 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3988 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3989 | status = psa_load_ecp_representation( slot->attr.type, |
| 3990 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3991 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3992 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3993 | if( status != PSA_SUCCESS ) |
| 3994 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3995 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3996 | alg, |
| 3997 | hash, hash_length, |
| 3998 | signature, signature_size, |
| 3999 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4000 | mbedtls_ecp_keypair_free( ecp ); |
| 4001 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4002 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4003 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4004 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4005 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4006 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4007 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4008 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4009 | } |
| 4010 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4011 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4012 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4013 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4014 | |
| 4015 | exit: |
| 4016 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 4017 | * the trailing part on success) with something that isn't a valid mac |
| 4018 | * (barring an attack on the mac and deliberately-crafted input), |
| 4019 | * in case the caller doesn't check the return status properly. */ |
| 4020 | if( status == PSA_SUCCESS ) |
| 4021 | memset( signature + *signature_length, '!', |
| 4022 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 4023 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4024 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4025 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 4026 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4027 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4028 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4029 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4030 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4031 | } |
| 4032 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4033 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4034 | psa_algorithm_t alg, |
| 4035 | const uint8_t *hash, |
| 4036 | size_t hash_length, |
| 4037 | const uint8_t *signature, |
| 4038 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4039 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4040 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4041 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4042 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 4043 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4044 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 4045 | PSA_KEY_USAGE_VERIFY_HASH, |
| 4046 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4047 | if( status != PSA_SUCCESS ) |
| 4048 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4049 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4050 | /* Try any of the available accelerators first */ |
| 4051 | status = psa_driver_wrapper_verify_hash( slot, |
| 4052 | alg, |
| 4053 | hash, |
| 4054 | hash_length, |
| 4055 | signature, |
| 4056 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 4057 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4058 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4059 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4060 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4061 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 4062 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4063 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4064 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4065 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4066 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4067 | status = psa_load_rsa_representation( slot->attr.type, |
| 4068 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4069 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4070 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4071 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4072 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4073 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4074 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4075 | alg, |
| 4076 | hash, hash_length, |
| 4077 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4078 | mbedtls_rsa_free( rsa ); |
| 4079 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4080 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4081 | } |
| 4082 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4083 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4084 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4085 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4086 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4087 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4088 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4089 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4090 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4091 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4092 | status = psa_load_ecp_representation( slot->attr.type, |
| 4093 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4094 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4095 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4096 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4097 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4098 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4099 | hash, hash_length, |
| 4100 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4101 | mbedtls_ecp_keypair_free( ecp ); |
| 4102 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4103 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4104 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4105 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4106 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4107 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4108 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4109 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4110 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4111 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4112 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4113 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4114 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4115 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4116 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4117 | |
| 4118 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4119 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4120 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4121 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4122 | } |
| 4123 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4124 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4125 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4126 | mbedtls_rsa_context *rsa ) |
| 4127 | { |
| 4128 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4129 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4130 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4131 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4132 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4133 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4134 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4135 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4136 | psa_algorithm_t alg, |
| 4137 | const uint8_t *input, |
| 4138 | size_t input_length, |
| 4139 | const uint8_t *salt, |
| 4140 | size_t salt_length, |
| 4141 | uint8_t *output, |
| 4142 | size_t output_size, |
| 4143 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4144 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4145 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4146 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4147 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4148 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4149 | (void) input; |
| 4150 | (void) input_length; |
| 4151 | (void) salt; |
| 4152 | (void) output; |
| 4153 | (void) output_size; |
| 4154 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4155 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4156 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4157 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4158 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4159 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4160 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4161 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4162 | if( status != PSA_SUCCESS ) |
| 4163 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4164 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4165 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4166 | { |
| 4167 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4168 | goto exit; |
| 4169 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4170 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4171 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4172 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4173 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4174 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4175 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4176 | status = psa_load_rsa_representation( slot->attr.type, |
| 4177 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4178 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4179 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4180 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4181 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4182 | |
| 4183 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4184 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4185 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4186 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4187 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4188 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4189 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4190 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4191 | status = mbedtls_to_psa_error( |
| 4192 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4193 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4194 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4195 | MBEDTLS_RSA_PUBLIC, |
| 4196 | input_length, |
| 4197 | input, |
| 4198 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4199 | } |
| 4200 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4201 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4202 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4203 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4204 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4205 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4206 | status = mbedtls_to_psa_error( |
| 4207 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4208 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4209 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4210 | MBEDTLS_RSA_PUBLIC, |
| 4211 | salt, salt_length, |
| 4212 | input_length, |
| 4213 | input, |
| 4214 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4215 | } |
| 4216 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4217 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4218 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4219 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4220 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4221 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4222 | rsa_exit: |
| 4223 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4224 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4225 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4226 | mbedtls_rsa_free( rsa ); |
| 4227 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4228 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4229 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4230 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4231 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4232 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4233 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4234 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4235 | |
| 4236 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4237 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4238 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4239 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4240 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4241 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4242 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4243 | psa_algorithm_t alg, |
| 4244 | const uint8_t *input, |
| 4245 | size_t input_length, |
| 4246 | const uint8_t *salt, |
| 4247 | size_t salt_length, |
| 4248 | uint8_t *output, |
| 4249 | size_t output_size, |
| 4250 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4251 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4252 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4253 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4254 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4255 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4256 | (void) input; |
| 4257 | (void) input_length; |
| 4258 | (void) salt; |
| 4259 | (void) output; |
| 4260 | (void) output_size; |
| 4261 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4262 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4263 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4264 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4266 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4267 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4268 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4269 | if( status != PSA_SUCCESS ) |
| 4270 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4271 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4272 | { |
| 4273 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4274 | goto exit; |
| 4275 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4276 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4277 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4278 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4279 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4280 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4281 | mbedtls_rsa_context *rsa = NULL; |
| 4282 | status = psa_load_rsa_representation( slot->attr.type, |
| 4283 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4284 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4285 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4286 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4287 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4288 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4289 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4290 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4291 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4292 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4293 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4294 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4296 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4297 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4298 | status = mbedtls_to_psa_error( |
| 4299 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4300 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4301 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4302 | MBEDTLS_RSA_PRIVATE, |
| 4303 | output_length, |
| 4304 | input, |
| 4305 | output, |
| 4306 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4307 | } |
| 4308 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4309 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4310 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4311 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4312 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4313 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4314 | status = mbedtls_to_psa_error( |
| 4315 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4316 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4317 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4318 | MBEDTLS_RSA_PRIVATE, |
| 4319 | salt, salt_length, |
| 4320 | output_length, |
| 4321 | input, |
| 4322 | output, |
| 4323 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4324 | } |
| 4325 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4326 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4327 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4328 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4329 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4330 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4331 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4332 | mbedtls_rsa_free( rsa ); |
| 4333 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4334 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4335 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4336 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4337 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4338 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4339 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4340 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4341 | |
| 4342 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4343 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4344 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4345 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4346 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4347 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4348 | |
| 4349 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4350 | /****************************************************************/ |
| 4351 | /* Symmetric cryptography */ |
| 4352 | /****************************************************************/ |
| 4353 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4354 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4355 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4356 | psa_algorithm_t alg, |
| 4357 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4358 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4359 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4360 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4361 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4362 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4363 | size_t key_bits; |
| 4364 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4365 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4366 | PSA_KEY_USAGE_ENCRYPT : |
| 4367 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4368 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4369 | /* A context must be freshly initialized before it can be set up. */ |
| 4370 | if( operation->alg != 0 ) |
| 4371 | return( PSA_ERROR_BAD_STATE ); |
| 4372 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4373 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4374 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4375 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4376 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4377 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4378 | 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] | 4379 | if( status != PSA_SUCCESS ) |
| 4380 | goto exit; |
| 4381 | |
| 4382 | /* Initialize the operation struct members, except for alg. The alg member |
| 4383 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4384 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4385 | operation->key_set = 0; |
| 4386 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4387 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4388 | operation->iv_size = 0; |
| 4389 | operation->block_size = 0; |
| 4390 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4391 | operation->iv_required = 0; |
| 4392 | else |
| 4393 | operation->iv_required = 1; |
| 4394 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4395 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4396 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4397 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4398 | slot, |
| 4399 | alg ); |
| 4400 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4401 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4402 | slot, |
| 4403 | alg ); |
| 4404 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4405 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4406 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4407 | /* Once the driver context is initialised, it needs to be freed using |
| 4408 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4409 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4410 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4411 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4412 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4413 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4414 | goto exit; |
| 4415 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4416 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4417 | * available for the given algorithm & key. */ |
| 4418 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4419 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4420 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4421 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4422 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4423 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4424 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4425 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4426 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4427 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4428 | 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] | 4429 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4430 | { |
| 4431 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4432 | goto exit; |
| 4433 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4434 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4435 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4436 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4437 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4438 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4439 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4440 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4441 | { |
| 4442 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4443 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4444 | memcpy( keys, slot->data.key.data, 16 ); |
| 4445 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4446 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4447 | keys, |
| 4448 | 192, cipher_operation ); |
| 4449 | } |
| 4450 | else |
| 4451 | #endif |
| 4452 | { |
| 4453 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4454 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4455 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4456 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4457 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4458 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4459 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4460 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4461 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4462 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4463 | case PSA_ALG_CBC_NO_PADDING: |
| 4464 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4465 | MBEDTLS_PADDING_NONE ); |
| 4466 | break; |
| 4467 | case PSA_ALG_CBC_PKCS7: |
| 4468 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4469 | MBEDTLS_PADDING_PKCS7 ); |
| 4470 | break; |
| 4471 | default: |
| 4472 | /* The algorithm doesn't involve padding. */ |
| 4473 | ret = 0; |
| 4474 | break; |
| 4475 | } |
| 4476 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4477 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4478 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4479 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4480 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4481 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4482 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4483 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4484 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4485 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4486 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4487 | #if defined(MBEDTLS_CHACHA20_C) |
| 4488 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4489 | 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] | 4490 | operation->iv_size = 12; |
| 4491 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4492 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4493 | status = PSA_SUCCESS; |
| 4494 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4495 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4496 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4497 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4498 | if( status == PSA_SUCCESS ) |
| 4499 | { |
| 4500 | /* Update operation flags for both driver and software implementations */ |
| 4501 | operation->key_set = 1; |
| 4502 | } |
| 4503 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4504 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4505 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4506 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4507 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4508 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4509 | } |
| 4510 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4511 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4512 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4513 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4514 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4515 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4518 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4519 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4520 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4521 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4522 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4523 | } |
| 4524 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4525 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4526 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4527 | size_t iv_size, |
| 4528 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4529 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4530 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4531 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4532 | if( operation->iv_set || ! operation->iv_required ) |
| 4533 | { |
| 4534 | return( PSA_ERROR_BAD_STATE ); |
| 4535 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4536 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4537 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4538 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4539 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4540 | iv, |
| 4541 | iv_size, |
| 4542 | iv_length ); |
| 4543 | goto exit; |
| 4544 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4545 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4546 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4547 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4548 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4549 | goto exit; |
| 4550 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4551 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4552 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4553 | if( ret != 0 ) |
| 4554 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4555 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4556 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4557 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4558 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4559 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4560 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4561 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4562 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4563 | if( status == PSA_SUCCESS ) |
| 4564 | operation->iv_set = 1; |
| 4565 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4566 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4567 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4570 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4571 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4572 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4573 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4574 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4575 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4576 | if( operation->iv_set || ! operation->iv_required ) |
| 4577 | { |
| 4578 | return( PSA_ERROR_BAD_STATE ); |
| 4579 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4580 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4581 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4582 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4583 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4584 | iv, |
| 4585 | iv_length ); |
| 4586 | goto exit; |
| 4587 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4588 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4589 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4590 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4591 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4592 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4593 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4594 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4595 | status = mbedtls_to_psa_error( ret ); |
| 4596 | exit: |
| 4597 | if( status == PSA_SUCCESS ) |
| 4598 | operation->iv_set = 1; |
| 4599 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4600 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4601 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4602 | } |
| 4603 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4604 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4605 | * manual processing, since the PSA API is defined as being able to process |
| 4606 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4607 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4608 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4609 | mbedtls_cipher_context_t *ctx, |
| 4610 | const uint8_t *input, |
| 4611 | size_t input_length, |
| 4612 | uint8_t *output, |
| 4613 | size_t output_size, |
| 4614 | size_t *output_length ) |
| 4615 | { |
| 4616 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4617 | size_t block_size = ctx->cipher_info->block_size; |
| 4618 | size_t internal_output_length = 0; |
| 4619 | *output_length = 0; |
| 4620 | |
| 4621 | if( input_length == 0 ) |
| 4622 | { |
| 4623 | status = PSA_SUCCESS; |
| 4624 | goto exit; |
| 4625 | } |
| 4626 | |
| 4627 | if( ctx->unprocessed_len > 0 ) |
| 4628 | { |
| 4629 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4630 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4631 | |
| 4632 | if( input_length < bytes_to_copy ) |
| 4633 | bytes_to_copy = input_length; |
| 4634 | |
| 4635 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4636 | input, bytes_to_copy ); |
| 4637 | input_length -= bytes_to_copy; |
| 4638 | input += bytes_to_copy; |
| 4639 | ctx->unprocessed_len += bytes_to_copy; |
| 4640 | |
| 4641 | if( ctx->unprocessed_len == block_size ) |
| 4642 | { |
| 4643 | status = mbedtls_to_psa_error( |
| 4644 | mbedtls_cipher_update( ctx, |
| 4645 | ctx->unprocessed_data, |
| 4646 | block_size, |
| 4647 | output, &internal_output_length ) ); |
| 4648 | |
| 4649 | if( status != PSA_SUCCESS ) |
| 4650 | goto exit; |
| 4651 | |
| 4652 | output += internal_output_length; |
| 4653 | output_size -= internal_output_length; |
| 4654 | *output_length += internal_output_length; |
| 4655 | ctx->unprocessed_len = 0; |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | while( input_length >= block_size ) |
| 4660 | { |
| 4661 | /* Run all full blocks we have, one by one */ |
| 4662 | status = mbedtls_to_psa_error( |
| 4663 | mbedtls_cipher_update( ctx, input, |
| 4664 | block_size, |
| 4665 | output, &internal_output_length ) ); |
| 4666 | |
| 4667 | if( status != PSA_SUCCESS ) |
| 4668 | goto exit; |
| 4669 | |
| 4670 | input_length -= block_size; |
| 4671 | input += block_size; |
| 4672 | |
| 4673 | output += internal_output_length; |
| 4674 | output_size -= internal_output_length; |
| 4675 | *output_length += internal_output_length; |
| 4676 | } |
| 4677 | |
| 4678 | if( input_length > 0 ) |
| 4679 | { |
| 4680 | /* Save unprocessed bytes for later processing */ |
| 4681 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4682 | input, input_length ); |
| 4683 | ctx->unprocessed_len += input_length; |
| 4684 | } |
| 4685 | |
| 4686 | status = PSA_SUCCESS; |
| 4687 | |
| 4688 | exit: |
| 4689 | return( status ); |
| 4690 | } |
| 4691 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4692 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4693 | const uint8_t *input, |
| 4694 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4695 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4696 | size_t output_size, |
| 4697 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4698 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4699 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4700 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4701 | if( operation->alg == 0 ) |
| 4702 | { |
| 4703 | return( PSA_ERROR_BAD_STATE ); |
| 4704 | } |
| 4705 | if( operation->iv_required && ! operation->iv_set ) |
| 4706 | { |
| 4707 | return( PSA_ERROR_BAD_STATE ); |
| 4708 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4709 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4710 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4711 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4712 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4713 | input, |
| 4714 | input_length, |
| 4715 | output, |
| 4716 | output_size, |
| 4717 | output_length ); |
| 4718 | goto exit; |
| 4719 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4720 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4721 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4722 | { |
| 4723 | /* Take the unprocessed partial block left over from previous |
| 4724 | * update calls, if any, plus the input to this call. Remove |
| 4725 | * the last partial block, if any. You get the data that will be |
| 4726 | * output in this call. */ |
| 4727 | expected_output_size = |
| 4728 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4729 | / operation->block_size * operation->block_size; |
| 4730 | } |
| 4731 | else |
| 4732 | { |
| 4733 | expected_output_size = input_length; |
| 4734 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4735 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4736 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4737 | { |
| 4738 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4739 | goto exit; |
| 4740 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4741 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4742 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4743 | { |
| 4744 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4745 | * process a single block at a time in ECB mode. Abstract away that |
| 4746 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4747 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4748 | input, |
| 4749 | input_length, |
| 4750 | output, |
| 4751 | output_size, |
| 4752 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4753 | } |
| 4754 | else |
| 4755 | { |
| 4756 | status = mbedtls_to_psa_error( |
| 4757 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4758 | input_length, output, output_length ) ); |
| 4759 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4760 | exit: |
| 4761 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4762 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4763 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4764 | } |
| 4765 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4766 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4767 | uint8_t *output, |
| 4768 | size_t output_size, |
| 4769 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4770 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4771 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4772 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4773 | if( operation->alg == 0 ) |
| 4774 | { |
| 4775 | return( PSA_ERROR_BAD_STATE ); |
| 4776 | } |
| 4777 | if( operation->iv_required && ! operation->iv_set ) |
| 4778 | { |
| 4779 | return( PSA_ERROR_BAD_STATE ); |
| 4780 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4781 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4782 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4783 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4784 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4785 | output, |
| 4786 | output_size, |
| 4787 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4788 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4789 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4790 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4791 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4792 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4793 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4794 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4795 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4796 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4797 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4798 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4799 | } |
| 4800 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4801 | status = mbedtls_to_psa_error( |
| 4802 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4803 | temp_output_buffer, |
| 4804 | output_length ) ); |
| 4805 | if( status != PSA_SUCCESS ) |
| 4806 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4807 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4808 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4809 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4810 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4811 | memcpy( output, temp_output_buffer, *output_length ); |
| 4812 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4813 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4814 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4815 | exit: |
| 4816 | if( operation->mbedtls_in_use == 1 ) |
| 4817 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4818 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4819 | if( status == PSA_SUCCESS ) |
| 4820 | return( psa_cipher_abort( operation ) ); |
| 4821 | else |
| 4822 | { |
| 4823 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4824 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4825 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4826 | return( status ); |
| 4827 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4828 | } |
| 4829 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4830 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4831 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4832 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4833 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4834 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4835 | * in use. It's ok to call abort on such an object, and there's |
| 4836 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4837 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4838 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4839 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4840 | /* Sanity check (shouldn't happen: operation->alg should |
| 4841 | * always have been initialized to a valid value). */ |
| 4842 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4843 | return( PSA_ERROR_BAD_STATE ); |
| 4844 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4845 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4846 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4847 | else |
| 4848 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4849 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4850 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4851 | operation->key_set = 0; |
| 4852 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4853 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4854 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4855 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4856 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4857 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4858 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4859 | } |
| 4860 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4861 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4862 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4863 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4864 | /****************************************************************/ |
| 4865 | /* AEAD */ |
| 4866 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4868 | typedef struct |
| 4869 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4870 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4871 | const mbedtls_cipher_info_t *cipher_info; |
| 4872 | union |
| 4873 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4874 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4875 | #if defined(MBEDTLS_CCM_C) |
| 4876 | mbedtls_ccm_context ccm; |
| 4877 | #endif /* MBEDTLS_CCM_C */ |
| 4878 | #if defined(MBEDTLS_GCM_C) |
| 4879 | mbedtls_gcm_context gcm; |
| 4880 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4881 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4882 | mbedtls_chachapoly_context chachapoly; |
| 4883 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4884 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4885 | psa_algorithm_t core_alg; |
| 4886 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4887 | uint8_t tag_length; |
| 4888 | } aead_operation_t; |
| 4889 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4890 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4891 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4892 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4893 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4894 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4895 | { |
| 4896 | #if defined(MBEDTLS_CCM_C) |
| 4897 | case PSA_ALG_CCM: |
| 4898 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4899 | break; |
| 4900 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4901 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4902 | case PSA_ALG_GCM: |
| 4903 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4904 | break; |
| 4905 | #endif /* MBEDTLS_GCM_C */ |
| 4906 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4907 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4908 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4912 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4913 | psa_key_usage_t usage, |
| 4914 | psa_algorithm_t alg ) |
| 4915 | { |
| 4916 | psa_status_t status; |
| 4917 | size_t key_bits; |
| 4918 | mbedtls_cipher_id_t cipher_id; |
| 4919 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4920 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4921 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4922 | if( status != PSA_SUCCESS ) |
| 4923 | return( status ); |
| 4924 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4925 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4926 | |
| 4927 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4928 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4929 | &cipher_id ); |
| 4930 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4931 | { |
| 4932 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4933 | goto cleanup; |
| 4934 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4935 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4936 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4937 | { |
| 4938 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4939 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4940 | operation->core_alg = PSA_ALG_CCM; |
| 4941 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4942 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4943 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4944 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4945 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4946 | { |
| 4947 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4948 | goto cleanup; |
| 4949 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4950 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4951 | status = mbedtls_to_psa_error( |
| 4952 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4953 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4954 | (unsigned int) key_bits ) ); |
| 4955 | if( status != 0 ) |
| 4956 | goto cleanup; |
| 4957 | break; |
| 4958 | #endif /* MBEDTLS_CCM_C */ |
| 4959 | |
| 4960 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4961 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4962 | operation->core_alg = PSA_ALG_GCM; |
| 4963 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4964 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4965 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4966 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4967 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4968 | { |
| 4969 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4970 | goto cleanup; |
| 4971 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4972 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4973 | status = mbedtls_to_psa_error( |
| 4974 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4975 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4976 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4977 | if( status != 0 ) |
| 4978 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4979 | break; |
| 4980 | #endif /* MBEDTLS_GCM_C */ |
| 4981 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4982 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4983 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4984 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4985 | operation->full_tag_length = 16; |
| 4986 | /* We only support the default tag length. */ |
| 4987 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4988 | { |
| 4989 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4990 | goto cleanup; |
| 4991 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4992 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4993 | status = mbedtls_to_psa_error( |
| 4994 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4995 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4996 | if( status != 0 ) |
| 4997 | goto cleanup; |
| 4998 | break; |
| 4999 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 5000 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5001 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5002 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5003 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5004 | } |
| 5005 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5006 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 5007 | { |
| 5008 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5009 | goto cleanup; |
| 5010 | } |
| 5011 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5012 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5013 | return( PSA_SUCCESS ); |
| 5014 | |
| 5015 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5016 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5017 | return( status ); |
| 5018 | } |
| 5019 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5020 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5021 | psa_algorithm_t alg, |
| 5022 | const uint8_t *nonce, |
| 5023 | size_t nonce_length, |
| 5024 | const uint8_t *additional_data, |
| 5025 | size_t additional_data_length, |
| 5026 | const uint8_t *plaintext, |
| 5027 | size_t plaintext_length, |
| 5028 | uint8_t *ciphertext, |
| 5029 | size_t ciphertext_size, |
| 5030 | size_t *ciphertext_length ) |
| 5031 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5032 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5033 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 5034 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5035 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 5036 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 5037 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5038 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5039 | if( status != PSA_SUCCESS ) |
| 5040 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5041 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5042 | /* For all currently supported modes, the tag is at the end of the |
| 5043 | * ciphertext. */ |
| 5044 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 5045 | { |
| 5046 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5047 | goto exit; |
| 5048 | } |
| 5049 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5050 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5051 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5052 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5053 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5054 | status = mbedtls_to_psa_error( |
| 5055 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 5056 | MBEDTLS_GCM_ENCRYPT, |
| 5057 | plaintext_length, |
| 5058 | nonce, nonce_length, |
| 5059 | additional_data, additional_data_length, |
| 5060 | plaintext, ciphertext, |
| 5061 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5062 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5063 | else |
| 5064 | #endif /* MBEDTLS_GCM_C */ |
| 5065 | #if defined(MBEDTLS_CCM_C) |
| 5066 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5067 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5068 | status = mbedtls_to_psa_error( |
| 5069 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 5070 | plaintext_length, |
| 5071 | nonce, nonce_length, |
| 5072 | additional_data, |
| 5073 | additional_data_length, |
| 5074 | plaintext, ciphertext, |
| 5075 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5076 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5077 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5078 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5079 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5080 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5081 | { |
| 5082 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5083 | { |
| 5084 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5085 | goto exit; |
| 5086 | } |
| 5087 | status = mbedtls_to_psa_error( |
| 5088 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 5089 | plaintext_length, |
| 5090 | nonce, |
| 5091 | additional_data, |
| 5092 | additional_data_length, |
| 5093 | plaintext, |
| 5094 | ciphertext, |
| 5095 | tag ) ); |
| 5096 | } |
| 5097 | else |
| 5098 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5099 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5100 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5101 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5102 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5103 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 5104 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5105 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5106 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5107 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5108 | if( status == PSA_SUCCESS ) |
| 5109 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 5110 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5111 | } |
| 5112 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5113 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 5114 | * followed by the tag. Return the length of the part preceding the tag in |
| 5115 | * *plaintext_length. This is the size of the plaintext in modes where |
| 5116 | * the encrypted data has the same size as the plaintext, such as |
| 5117 | * CCM and GCM. */ |
| 5118 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 5119 | const uint8_t *ciphertext, |
| 5120 | size_t ciphertext_length, |
| 5121 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5122 | const uint8_t **p_tag ) |
| 5123 | { |
| 5124 | size_t payload_length; |
| 5125 | if( tag_length > ciphertext_length ) |
| 5126 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5127 | payload_length = ciphertext_length - tag_length; |
| 5128 | if( payload_length > plaintext_size ) |
| 5129 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5130 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5131 | return( PSA_SUCCESS ); |
| 5132 | } |
| 5133 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5134 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5135 | psa_algorithm_t alg, |
| 5136 | const uint8_t *nonce, |
| 5137 | size_t nonce_length, |
| 5138 | const uint8_t *additional_data, |
| 5139 | size_t additional_data_length, |
| 5140 | const uint8_t *ciphertext, |
| 5141 | size_t ciphertext_length, |
| 5142 | uint8_t *plaintext, |
| 5143 | size_t plaintext_size, |
| 5144 | size_t *plaintext_length ) |
| 5145 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5146 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5147 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5148 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5149 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5150 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5151 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5152 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5153 | if( status != PSA_SUCCESS ) |
| 5154 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5155 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5156 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5157 | ciphertext, ciphertext_length, |
| 5158 | plaintext_size, &tag ); |
| 5159 | if( status != PSA_SUCCESS ) |
| 5160 | goto exit; |
| 5161 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5162 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5163 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5164 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5165 | status = mbedtls_to_psa_error( |
| 5166 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5167 | ciphertext_length - operation.tag_length, |
| 5168 | nonce, nonce_length, |
| 5169 | additional_data, |
| 5170 | additional_data_length, |
| 5171 | tag, operation.tag_length, |
| 5172 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5173 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5174 | else |
| 5175 | #endif /* MBEDTLS_GCM_C */ |
| 5176 | #if defined(MBEDTLS_CCM_C) |
| 5177 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5178 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5179 | status = mbedtls_to_psa_error( |
| 5180 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5181 | ciphertext_length - operation.tag_length, |
| 5182 | nonce, nonce_length, |
| 5183 | additional_data, |
| 5184 | additional_data_length, |
| 5185 | ciphertext, plaintext, |
| 5186 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5187 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5188 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5189 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5190 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5191 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5192 | { |
| 5193 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5194 | { |
| 5195 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5196 | goto exit; |
| 5197 | } |
| 5198 | status = mbedtls_to_psa_error( |
| 5199 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5200 | ciphertext_length - operation.tag_length, |
| 5201 | nonce, |
| 5202 | additional_data, |
| 5203 | additional_data_length, |
| 5204 | tag, |
| 5205 | ciphertext, |
| 5206 | plaintext ) ); |
| 5207 | } |
| 5208 | else |
| 5209 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5210 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5211 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5212 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5213 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5214 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5215 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5216 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5217 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5218 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5219 | if( status == PSA_SUCCESS ) |
| 5220 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5221 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5222 | } |
| 5223 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5224 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5225 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5226 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5227 | /* Generators */ |
| 5228 | /****************************************************************/ |
| 5229 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5230 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 5231 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5232 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5233 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 5234 | #endif |
| 5235 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5236 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5237 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5238 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5239 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5240 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5241 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5242 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5243 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5244 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5245 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5246 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5247 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5248 | } |
| 5249 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5250 | 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] | 5251 | { |
| 5252 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5253 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5254 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5255 | { |
| 5256 | /* The object has (apparently) been initialized but it is not |
| 5257 | * in use. It's ok to call abort on such an object, and there's |
| 5258 | * nothing to do. */ |
| 5259 | } |
| 5260 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5261 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5262 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5263 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5264 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5265 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5266 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5267 | else |
| 5268 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5269 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5270 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5271 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5272 | /* 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] | 5273 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5274 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5275 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5276 | { |
| 5277 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5278 | operation->ctx.tls12_prf.seed_length ); |
| 5279 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5280 | } |
| 5281 | |
| 5282 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5283 | { |
| 5284 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5285 | operation->ctx.tls12_prf.label_length ); |
| 5286 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5287 | } |
| 5288 | |
| 5289 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5290 | |
| 5291 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5292 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5293 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5294 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5295 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5296 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5297 | { |
| 5298 | status = PSA_ERROR_BAD_STATE; |
| 5299 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5300 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5301 | return( status ); |
| 5302 | } |
| 5303 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5304 | 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] | 5305 | size_t *capacity) |
| 5306 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5307 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5308 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5309 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5310 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5313 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5314 | return( PSA_SUCCESS ); |
| 5315 | } |
| 5316 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5317 | 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] | 5318 | size_t capacity ) |
| 5319 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5320 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5321 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5322 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5323 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5324 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5325 | return( PSA_SUCCESS ); |
| 5326 | } |
| 5327 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5328 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5329 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5330 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5331 | 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] | 5332 | psa_algorithm_t hash_alg, |
| 5333 | uint8_t *output, |
| 5334 | size_t output_length ) |
| 5335 | { |
| 5336 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5337 | psa_status_t status; |
| 5338 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5339 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5340 | return( PSA_ERROR_BAD_STATE ); |
| 5341 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5342 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5343 | while( output_length != 0 ) |
| 5344 | { |
| 5345 | /* Copy what remains of the current block */ |
| 5346 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5347 | if( n > output_length ) |
| 5348 | n = (uint8_t) output_length; |
| 5349 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5350 | output += n; |
| 5351 | output_length -= n; |
| 5352 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5353 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5354 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5355 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5356 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5357 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5358 | * object was corrupted or if this function is called directly |
| 5359 | * inside the library. */ |
| 5360 | if( hkdf->block_number == 0xff ) |
| 5361 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5362 | |
| 5363 | /* We need a new block */ |
| 5364 | ++hkdf->block_number; |
| 5365 | hkdf->offset_in_block = 0; |
| 5366 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5367 | hkdf->prk, hash_length, |
| 5368 | hash_alg ); |
| 5369 | if( status != PSA_SUCCESS ) |
| 5370 | return( status ); |
| 5371 | if( hkdf->block_number != 1 ) |
| 5372 | { |
| 5373 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5374 | hkdf->output_block, |
| 5375 | hash_length ); |
| 5376 | if( status != PSA_SUCCESS ) |
| 5377 | return( status ); |
| 5378 | } |
| 5379 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5380 | hkdf->info, |
| 5381 | hkdf->info_length ); |
| 5382 | if( status != PSA_SUCCESS ) |
| 5383 | return( status ); |
| 5384 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5385 | &hkdf->block_number, 1 ); |
| 5386 | if( status != PSA_SUCCESS ) |
| 5387 | return( status ); |
| 5388 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5389 | hkdf->output_block, |
| 5390 | sizeof( hkdf->output_block ) ); |
| 5391 | if( status != PSA_SUCCESS ) |
| 5392 | return( status ); |
| 5393 | } |
| 5394 | |
| 5395 | return( PSA_SUCCESS ); |
| 5396 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5397 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5398 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5399 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5400 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5401 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5402 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5403 | psa_algorithm_t alg ) |
| 5404 | { |
| 5405 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 5406 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5407 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5408 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5409 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5410 | /* We can't be wanting more output after block 0xff, otherwise |
| 5411 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5412 | * prevented this call. It could happen only if the operation |
| 5413 | * object was corrupted or if this function is called directly |
| 5414 | * inside the library. */ |
| 5415 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5416 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5417 | |
| 5418 | /* We need a new block */ |
| 5419 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5420 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5421 | |
| 5422 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5423 | * |
| 5424 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5425 | * |
| 5426 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5427 | * HMAC_hash(secret, A(2) + seed) + |
| 5428 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5429 | * |
| 5430 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5431 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5432 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5433 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5434 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5435 | * is currently extracted as `output_block` and where i is |
| 5436 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5437 | */ |
| 5438 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5439 | /* Save the hash context before using it, to preserve the hash state with |
| 5440 | * only the inner padding in it. We need this, because inner padding depends |
| 5441 | * on the key (secret in the RFC's terminology). */ |
| 5442 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5443 | if( status != PSA_SUCCESS ) |
| 5444 | goto cleanup; |
| 5445 | |
| 5446 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5447 | if( tls12_prf->block_number == 1 ) |
| 5448 | { |
| 5449 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5450 | * the variable seed and in this instance means it in the context of the |
| 5451 | * P_hash function, where seed = label + seed.) */ |
| 5452 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5453 | tls12_prf->label, tls12_prf->label_length ); |
| 5454 | if( status != PSA_SUCCESS ) |
| 5455 | goto cleanup; |
| 5456 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5457 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5458 | if( status != PSA_SUCCESS ) |
| 5459 | goto cleanup; |
| 5460 | } |
| 5461 | else |
| 5462 | { |
| 5463 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5464 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5465 | tls12_prf->Ai, hash_length ); |
| 5466 | if( status != PSA_SUCCESS ) |
| 5467 | goto cleanup; |
| 5468 | } |
| 5469 | |
| 5470 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5471 | tls12_prf->Ai, hash_length ); |
| 5472 | if( status != PSA_SUCCESS ) |
| 5473 | goto cleanup; |
| 5474 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5475 | if( status != PSA_SUCCESS ) |
| 5476 | goto cleanup; |
| 5477 | |
| 5478 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5479 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5480 | tls12_prf->Ai, hash_length ); |
| 5481 | if( status != PSA_SUCCESS ) |
| 5482 | goto cleanup; |
| 5483 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5484 | tls12_prf->label, tls12_prf->label_length ); |
| 5485 | if( status != PSA_SUCCESS ) |
| 5486 | goto cleanup; |
| 5487 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5488 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5489 | if( status != PSA_SUCCESS ) |
| 5490 | goto cleanup; |
| 5491 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5492 | tls12_prf->output_block, hash_length ); |
| 5493 | if( status != PSA_SUCCESS ) |
| 5494 | goto cleanup; |
| 5495 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5496 | if( status != PSA_SUCCESS ) |
| 5497 | goto cleanup; |
| 5498 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5499 | |
| 5500 | cleanup: |
| 5501 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5502 | cleanup_status = psa_hash_abort( &backup ); |
| 5503 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5504 | status = cleanup_status; |
| 5505 | |
| 5506 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5507 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5508 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5509 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5510 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5511 | psa_algorithm_t alg, |
| 5512 | uint8_t *output, |
| 5513 | size_t output_length ) |
| 5514 | { |
| 5515 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5516 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5517 | psa_status_t status; |
| 5518 | uint8_t offset, length; |
| 5519 | |
| 5520 | while( output_length != 0 ) |
| 5521 | { |
| 5522 | /* Check if we have fully processed the current block. */ |
| 5523 | if( tls12_prf->left_in_block == 0 ) |
| 5524 | { |
| 5525 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5526 | alg ); |
| 5527 | if( status != PSA_SUCCESS ) |
| 5528 | return( status ); |
| 5529 | |
| 5530 | continue; |
| 5531 | } |
| 5532 | |
| 5533 | if( tls12_prf->left_in_block > output_length ) |
| 5534 | length = (uint8_t) output_length; |
| 5535 | else |
| 5536 | length = tls12_prf->left_in_block; |
| 5537 | |
| 5538 | offset = hash_length - tls12_prf->left_in_block; |
| 5539 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5540 | output += length; |
| 5541 | output_length -= length; |
| 5542 | tls12_prf->left_in_block -= length; |
| 5543 | } |
| 5544 | |
| 5545 | return( PSA_SUCCESS ); |
| 5546 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5547 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5548 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5549 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5550 | psa_status_t psa_key_derivation_output_bytes( |
| 5551 | psa_key_derivation_operation_t *operation, |
| 5552 | uint8_t *output, |
| 5553 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5554 | { |
| 5555 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5556 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5557 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5558 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5559 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5560 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5561 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5564 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5565 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5566 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5567 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5568 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5569 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5570 | goto exit; |
| 5571 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5572 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5573 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5574 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5575 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5576 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5577 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5578 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5579 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5580 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5581 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5582 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5583 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5584 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5585 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5586 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5587 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5588 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5589 | output, output_length ); |
| 5590 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5591 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5592 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5593 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5594 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5595 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5596 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5597 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5598 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5599 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5600 | output_length ); |
| 5601 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5602 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5603 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5604 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5605 | { |
| 5606 | return( PSA_ERROR_BAD_STATE ); |
| 5607 | } |
| 5608 | |
| 5609 | exit: |
| 5610 | if( status != PSA_SUCCESS ) |
| 5611 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5612 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5613 | * This allows us to differentiate between exhausted operations and |
| 5614 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5615 | * operations. */ |
| 5616 | psa_algorithm_t alg = operation->alg; |
| 5617 | psa_key_derivation_abort( operation ); |
| 5618 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5619 | memset( output, '!', output_length ); |
| 5620 | } |
| 5621 | return( status ); |
| 5622 | } |
| 5623 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5624 | #if defined(MBEDTLS_DES_C) |
| 5625 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5626 | { |
| 5627 | if( data_size >= 8 ) |
| 5628 | mbedtls_des_key_set_parity( data ); |
| 5629 | if( data_size >= 16 ) |
| 5630 | mbedtls_des_key_set_parity( data + 8 ); |
| 5631 | if( data_size >= 24 ) |
| 5632 | mbedtls_des_key_set_parity( data + 16 ); |
| 5633 | } |
| 5634 | #endif /* MBEDTLS_DES_C */ |
| 5635 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5636 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5637 | psa_key_slot_t *slot, |
| 5638 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5639 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5640 | { |
| 5641 | uint8_t *data = NULL; |
| 5642 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5643 | psa_status_t status; |
| 5644 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5645 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5646 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5647 | if( bits % 8 != 0 ) |
| 5648 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5649 | data = mbedtls_calloc( 1, bytes ); |
| 5650 | if( data == NULL ) |
| 5651 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5652 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5653 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5654 | if( status != PSA_SUCCESS ) |
| 5655 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5656 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5657 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5658 | psa_des_set_key_parity( data, bytes ); |
| 5659 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5660 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5661 | |
| 5662 | exit: |
| 5663 | mbedtls_free( data ); |
| 5664 | return( status ); |
| 5665 | } |
| 5666 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5667 | 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] | 5668 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5669 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5670 | { |
| 5671 | psa_status_t status; |
| 5672 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5673 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5674 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5675 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5676 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5677 | /* Reject any attempt to create a zero-length key so that we don't |
| 5678 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5679 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5680 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5681 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5682 | if( ! operation->can_output_key ) |
| 5683 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5684 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5685 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5686 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5687 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5688 | if( driver != NULL ) |
| 5689 | { |
| 5690 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5691 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5692 | } |
| 5693 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5694 | if( status == PSA_SUCCESS ) |
| 5695 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5696 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5697 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5698 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5699 | } |
| 5700 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5701 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5702 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5703 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5704 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5705 | return( status ); |
| 5706 | } |
| 5707 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5708 | |
| 5709 | |
| 5710 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5711 | /* Key derivation */ |
| 5712 | /****************************************************************/ |
| 5713 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5714 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5715 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5716 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5717 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5718 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5719 | int is_kdf_alg_supported; |
| 5720 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5721 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5722 | * initialisers for this union leave some bytes unspecified.) */ |
| 5723 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5724 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5725 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5726 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5727 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5728 | is_kdf_alg_supported = 1; |
| 5729 | else |
| 5730 | #endif |
| 5731 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5732 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5733 | is_kdf_alg_supported = 1; |
| 5734 | else |
| 5735 | #endif |
| 5736 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5737 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5738 | is_kdf_alg_supported = 1; |
| 5739 | else |
| 5740 | #endif |
| 5741 | is_kdf_alg_supported = 0; |
| 5742 | |
| 5743 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5744 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5745 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5746 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5747 | if( hash_size == 0 ) |
| 5748 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5749 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5750 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5751 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5752 | { |
| 5753 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5754 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5755 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5756 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5757 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5758 | |
| 5759 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5760 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5761 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5762 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5763 | 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] | 5764 | psa_algorithm_t alg ) |
| 5765 | { |
| 5766 | psa_status_t status; |
| 5767 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5768 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5769 | return( PSA_ERROR_BAD_STATE ); |
| 5770 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5771 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5772 | return( PSA_ERROR_INVALID_ARGUMENT ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5773 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5774 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5775 | { |
| 5776 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5777 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5778 | } |
| 5779 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5780 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5781 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5782 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5783 | #endif |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5784 | else |
| 5785 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5786 | |
| 5787 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5788 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5789 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5790 | } |
| 5791 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5792 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5793 | 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] | 5794 | psa_algorithm_t hash_alg, |
| 5795 | psa_key_derivation_step_t step, |
| 5796 | const uint8_t *data, |
| 5797 | size_t data_length ) |
| 5798 | { |
| 5799 | psa_status_t status; |
| 5800 | switch( step ) |
| 5801 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5802 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5803 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5804 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5805 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5806 | data, data_length, |
| 5807 | hash_alg ); |
| 5808 | if( status != PSA_SUCCESS ) |
| 5809 | return( status ); |
| 5810 | hkdf->state = HKDF_STATE_STARTED; |
| 5811 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5812 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5813 | /* If no salt was provided, use an empty salt. */ |
| 5814 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5815 | { |
| 5816 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5817 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5818 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5819 | if( status != PSA_SUCCESS ) |
| 5820 | return( status ); |
| 5821 | hkdf->state = HKDF_STATE_STARTED; |
| 5822 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5823 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5824 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5825 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5826 | data, data_length ); |
| 5827 | if( status != PSA_SUCCESS ) |
| 5828 | return( status ); |
| 5829 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5830 | hkdf->prk, |
| 5831 | sizeof( hkdf->prk ) ); |
| 5832 | if( status != PSA_SUCCESS ) |
| 5833 | return( status ); |
| 5834 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5835 | hkdf->block_number = 0; |
| 5836 | hkdf->state = HKDF_STATE_KEYED; |
| 5837 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5838 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5839 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5840 | return( PSA_ERROR_BAD_STATE ); |
| 5841 | if( hkdf->info_set ) |
| 5842 | return( PSA_ERROR_BAD_STATE ); |
| 5843 | hkdf->info_length = data_length; |
| 5844 | if( data_length != 0 ) |
| 5845 | { |
| 5846 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5847 | if( hkdf->info == NULL ) |
| 5848 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5849 | memcpy( hkdf->info, data, data_length ); |
| 5850 | } |
| 5851 | hkdf->info_set = 1; |
| 5852 | return( PSA_SUCCESS ); |
| 5853 | default: |
| 5854 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5855 | } |
| 5856 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5857 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5858 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5859 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5860 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5861 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5862 | const uint8_t *data, |
| 5863 | size_t data_length ) |
| 5864 | { |
| 5865 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5866 | return( PSA_ERROR_BAD_STATE ); |
| 5867 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5868 | if( data_length != 0 ) |
| 5869 | { |
| 5870 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5871 | if( prf->seed == NULL ) |
| 5872 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5873 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5874 | memcpy( prf->seed, data, data_length ); |
| 5875 | prf->seed_length = data_length; |
| 5876 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5877 | |
| 5878 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5879 | |
| 5880 | return( PSA_SUCCESS ); |
| 5881 | } |
| 5882 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5883 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5884 | psa_algorithm_t hash_alg, |
| 5885 | const uint8_t *data, |
| 5886 | size_t data_length ) |
| 5887 | { |
| 5888 | psa_status_t status; |
| 5889 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5890 | return( PSA_ERROR_BAD_STATE ); |
| 5891 | |
| 5892 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5893 | if( status != PSA_SUCCESS ) |
| 5894 | return( status ); |
| 5895 | |
| 5896 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5897 | |
| 5898 | return( PSA_SUCCESS ); |
| 5899 | } |
| 5900 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5901 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5902 | const uint8_t *data, |
| 5903 | size_t data_length ) |
| 5904 | { |
| 5905 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5906 | return( PSA_ERROR_BAD_STATE ); |
| 5907 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5908 | if( data_length != 0 ) |
| 5909 | { |
| 5910 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5911 | if( prf->label == NULL ) |
| 5912 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5913 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5914 | memcpy( prf->label, data, data_length ); |
| 5915 | prf->label_length = data_length; |
| 5916 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5917 | |
| 5918 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5919 | |
| 5920 | return( PSA_SUCCESS ); |
| 5921 | } |
| 5922 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5923 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5924 | psa_algorithm_t hash_alg, |
| 5925 | psa_key_derivation_step_t step, |
| 5926 | const uint8_t *data, |
| 5927 | size_t data_length ) |
| 5928 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5929 | switch( step ) |
| 5930 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5931 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5932 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5933 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5934 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5935 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5936 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5937 | default: |
| 5938 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5939 | } |
| 5940 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5941 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5942 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5943 | |
| 5944 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5945 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5946 | psa_tls12_prf_key_derivation_t *prf, |
| 5947 | psa_algorithm_t hash_alg, |
| 5948 | const uint8_t *data, |
| 5949 | size_t data_length ) |
| 5950 | { |
| 5951 | psa_status_t status; |
| 5952 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5953 | uint8_t *cur = pms; |
| 5954 | |
| 5955 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5956 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5957 | |
| 5958 | /* Quoting RFC 4279, Section 2: |
| 5959 | * |
| 5960 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5961 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5962 | * uint16 with the value N, and the PSK itself. |
| 5963 | */ |
| 5964 | |
| 5965 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5966 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5967 | memset( cur, 0, data_length ); |
| 5968 | cur += data_length; |
| 5969 | *cur++ = pms[0]; |
| 5970 | *cur++ = pms[1]; |
| 5971 | memcpy( cur, data, data_length ); |
| 5972 | cur += data_length; |
| 5973 | |
| 5974 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5975 | |
| 5976 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5977 | return( status ); |
| 5978 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5979 | |
| 5980 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5981 | psa_tls12_prf_key_derivation_t *prf, |
| 5982 | psa_algorithm_t hash_alg, |
| 5983 | psa_key_derivation_step_t step, |
| 5984 | const uint8_t *data, |
| 5985 | size_t data_length ) |
| 5986 | { |
| 5987 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5988 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5989 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5990 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5991 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5992 | |
| 5993 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5994 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5995 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5996 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5997 | /** Check whether the given key type is acceptable for the given |
| 5998 | * input step of a key derivation. |
| 5999 | * |
| 6000 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6001 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6002 | * Both secret and non-secret inputs can alternatively have the type |
| 6003 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6004 | * that the input was passed as a buffer rather than via a key object. |
| 6005 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6006 | static int psa_key_derivation_check_input_type( |
| 6007 | psa_key_derivation_step_t step, |
| 6008 | psa_key_type_t key_type ) |
| 6009 | { |
| 6010 | switch( step ) |
| 6011 | { |
| 6012 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6013 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 6014 | return( PSA_SUCCESS ); |
| 6015 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6016 | return( PSA_SUCCESS ); |
| 6017 | break; |
| 6018 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6019 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6020 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6021 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6022 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 6023 | return( PSA_SUCCESS ); |
| 6024 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6025 | return( PSA_SUCCESS ); |
| 6026 | break; |
| 6027 | } |
| 6028 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6029 | } |
| 6030 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6031 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6032 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6033 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6034 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6035 | const uint8_t *data, |
| 6036 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6037 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6038 | psa_status_t status; |
| 6039 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 6040 | |
| 6041 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6042 | if( status != PSA_SUCCESS ) |
| 6043 | goto exit; |
| 6044 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 6045 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6046 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6047 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6048 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6049 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6050 | step, data, data_length ); |
| 6051 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6052 | else |
| 6053 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 6054 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 6055 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6056 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6057 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 6058 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6059 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6060 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6061 | else |
| 6062 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6063 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6064 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6065 | { |
| 6066 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 6067 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6068 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6069 | } |
| 6070 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6071 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6072 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6073 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6074 | return( PSA_ERROR_BAD_STATE ); |
| 6075 | } |
| 6076 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6077 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6078 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6079 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6080 | return( status ); |
| 6081 | } |
| 6082 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6083 | psa_status_t psa_key_derivation_input_bytes( |
| 6084 | psa_key_derivation_operation_t *operation, |
| 6085 | psa_key_derivation_step_t step, |
| 6086 | const uint8_t *data, |
| 6087 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6088 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6089 | return( psa_key_derivation_input_internal( operation, step, |
| 6090 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 6091 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6092 | } |
| 6093 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6094 | psa_status_t psa_key_derivation_input_key( |
| 6095 | psa_key_derivation_operation_t *operation, |
| 6096 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6097 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6098 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6099 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6100 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6101 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6102 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6103 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6104 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6105 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6106 | { |
| 6107 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6108 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6109 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6110 | |
| 6111 | /* Passing a key object as a SECRET input unlocks the permission |
| 6112 | * to output to a key object. */ |
| 6113 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6114 | operation->can_output_key = 1; |
| 6115 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6116 | status = psa_key_derivation_input_internal( operation, |
| 6117 | step, slot->attr.type, |
| 6118 | slot->data.key.data, |
| 6119 | slot->data.key.bytes ); |
| 6120 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6121 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6122 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6123 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6124 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6125 | |
| 6126 | |
| 6127 | |
| 6128 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6129 | /* Key agreement */ |
| 6130 | /****************************************************************/ |
| 6131 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6132 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6133 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 6134 | size_t peer_key_length, |
| 6135 | const mbedtls_ecp_keypair *our_key, |
| 6136 | uint8_t *shared_secret, |
| 6137 | size_t shared_secret_size, |
| 6138 | size_t *shared_secret_length ) |
| 6139 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 6140 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6141 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6142 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6143 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6144 | 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] | 6145 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6146 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6147 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 6148 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6149 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6150 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6151 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6152 | goto exit; |
| 6153 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6154 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6155 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6156 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6157 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6158 | status = mbedtls_to_psa_error( |
| 6159 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 6160 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6161 | goto exit; |
| 6162 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6163 | status = mbedtls_to_psa_error( |
| 6164 | mbedtls_ecdh_calc_secret( &ecdh, |
| 6165 | shared_secret_length, |
| 6166 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6167 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6168 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6169 | if( status != PSA_SUCCESS ) |
| 6170 | goto exit; |
| 6171 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 6172 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6173 | |
| 6174 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 6175 | if( status != PSA_SUCCESS ) |
| 6176 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6177 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6178 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6179 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6180 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6181 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6182 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6183 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6184 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6185 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6186 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6187 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6188 | psa_key_slot_t *private_key, |
| 6189 | const uint8_t *peer_key, |
| 6190 | size_t peer_key_length, |
| 6191 | uint8_t *shared_secret, |
| 6192 | size_t shared_secret_size, |
| 6193 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6194 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6195 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6196 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6197 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6198 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6199 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6200 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6201 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6202 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6203 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6204 | private_key->data.key.data, |
| 6205 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6206 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6207 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6208 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6209 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6210 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6211 | shared_secret, shared_secret_size, |
| 6212 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6213 | mbedtls_ecp_keypair_free( ecp ); |
| 6214 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6215 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6216 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6217 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6218 | (void) private_key; |
| 6219 | (void) peer_key; |
| 6220 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6221 | (void) shared_secret; |
| 6222 | (void) shared_secret_size; |
| 6223 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6225 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6226 | } |
| 6227 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6228 | /* 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] | 6229 | * to potentially free embedded data structures and wipe confidential data. |
| 6230 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6231 | 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] | 6232 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6233 | psa_key_slot_t *private_key, |
| 6234 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6235 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6236 | { |
| 6237 | psa_status_t status; |
| 6238 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6239 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6240 | 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] | 6241 | |
| 6242 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6243 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6244 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6245 | private_key, |
| 6246 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6247 | shared_secret, |
| 6248 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6249 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6250 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6251 | goto exit; |
| 6252 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6253 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6254 | * the shared secret. A shared secret is permitted wherever a key |
| 6255 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6256 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6257 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6258 | shared_secret, |
| 6259 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6260 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6261 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6262 | return( status ); |
| 6263 | } |
| 6264 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6265 | 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] | 6266 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6267 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6268 | const uint8_t *peer_key, |
| 6269 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6270 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6271 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6272 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6273 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6274 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6275 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6277 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6278 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6279 | if( status != PSA_SUCCESS ) |
| 6280 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6281 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6282 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6283 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6284 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6285 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6286 | else |
| 6287 | { |
| 6288 | /* If a private key has been added as SECRET, we allow the derived |
| 6289 | * key material to be used as a key in PSA Crypto. */ |
| 6290 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6291 | operation->can_output_key = 1; |
| 6292 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6293 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6294 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6295 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6296 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6297 | } |
| 6298 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6299 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6300 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6301 | const uint8_t *peer_key, |
| 6302 | size_t peer_key_length, |
| 6303 | uint8_t *output, |
| 6304 | size_t output_size, |
| 6305 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6306 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6307 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6308 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6309 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6310 | |
| 6311 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6312 | { |
| 6313 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6314 | goto exit; |
| 6315 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6316 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6317 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6318 | if( status != PSA_SUCCESS ) |
| 6319 | goto exit; |
| 6320 | |
| 6321 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6322 | peer_key, peer_key_length, |
| 6323 | output, output_size, |
| 6324 | output_length ); |
| 6325 | |
| 6326 | exit: |
| 6327 | if( status != PSA_SUCCESS ) |
| 6328 | { |
| 6329 | /* If an error happens and is not handled properly, the output |
| 6330 | * may be used as a key to protect sensitive data. Arrange for such |
| 6331 | * a key to be random, which is likely to result in decryption or |
| 6332 | * verification errors. This is better than filling the buffer with |
| 6333 | * some constant data such as zeros, which would result in the data |
| 6334 | * being protected with a reproducible, easily knowable key. |
| 6335 | */ |
| 6336 | psa_generate_random( output, output_size ); |
| 6337 | *output_length = output_size; |
| 6338 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6339 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6340 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6341 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6342 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6343 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6344 | |
| 6345 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6347 | /****************************************************************/ |
| 6348 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6349 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6350 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6351 | /** Initialize the PSA random generator. |
| 6352 | */ |
| 6353 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6354 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6355 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6356 | memset( rng, 0, sizeof( *rng ) ); |
| 6357 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6358 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6359 | /* Set default configuration if |
| 6360 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6361 | if( rng->entropy_init == NULL ) |
| 6362 | rng->entropy_init = mbedtls_entropy_init; |
| 6363 | if( rng->entropy_free == NULL ) |
| 6364 | rng->entropy_free = mbedtls_entropy_free; |
| 6365 | |
| 6366 | rng->entropy_init( &rng->entropy ); |
| 6367 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6368 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6369 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6370 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6371 | mbedtls_entropy_add_source( &rng->entropy, |
| 6372 | mbedtls_nv_seed_poll, NULL, |
| 6373 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6374 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6375 | #endif |
| 6376 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6377 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6378 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6379 | } |
| 6380 | |
| 6381 | /** Deinitialize the PSA random generator. |
| 6382 | */ |
| 6383 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6384 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6385 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6386 | memset( rng, 0, sizeof( *rng ) ); |
| 6387 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6388 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6389 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6390 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6391 | } |
| 6392 | |
| 6393 | /** Seed the PSA random generator. |
| 6394 | */ |
| 6395 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6396 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6397 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6398 | /* Do nothing: the external RNG seeds itself. */ |
| 6399 | (void) rng; |
| 6400 | return( PSA_SUCCESS ); |
| 6401 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6402 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6403 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6404 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6405 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6406 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6407 | } |
| 6408 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6409 | psa_status_t psa_generate_random( uint8_t *output, |
| 6410 | size_t output_size ) |
| 6411 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6412 | GUARD_MODULE_INITIALIZED; |
| 6413 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6414 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6415 | |
| 6416 | size_t output_length = 0; |
| 6417 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6418 | output, output_size, |
| 6419 | &output_length ); |
| 6420 | if( status != PSA_SUCCESS ) |
| 6421 | return( status ); |
| 6422 | /* Breaking up a request into smaller chunks is currently not supported |
| 6423 | * for the extrernal RNG interface. */ |
| 6424 | if( output_length != output_size ) |
| 6425 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6426 | return( PSA_SUCCESS ); |
| 6427 | |
| 6428 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6429 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6430 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6431 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6432 | size_t request_size = |
| 6433 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6434 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6435 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6436 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6437 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6438 | if( ret != 0 ) |
| 6439 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6440 | output_size -= request_size; |
| 6441 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6442 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6443 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6444 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6445 | } |
| 6446 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6447 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6448 | * |
| 6449 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6450 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6451 | * PSA API doesn't support passing an explicit state. |
| 6452 | * |
| 6453 | * In the non-external case, psa_generate_random() calls an |
| 6454 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6455 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6456 | * instead of doing this back-and-forth between the PSA API and the |
| 6457 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6458 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6459 | */ |
| 6460 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6461 | int mbedtls_psa_get_random( void *p_rng, |
| 6462 | unsigned char *output, |
| 6463 | size_t output_size ) |
| 6464 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6465 | /* This function takes a pointer to the RNG state because that's what |
| 6466 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6467 | * its own state internally and doesn't let the caller access that state. |
| 6468 | * So we just ignore the state parameter, and in practice we'll pass |
| 6469 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6470 | (void) p_rng; |
| 6471 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6472 | if( status == PSA_SUCCESS ) |
| 6473 | return( 0 ); |
| 6474 | else |
| 6475 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6476 | } |
| 6477 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6478 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6479 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6480 | #include "mbedtls/entropy_poll.h" |
| 6481 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6482 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6483 | size_t seed_size ) |
| 6484 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6485 | if( global_data.initialized ) |
| 6486 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6487 | |
| 6488 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6489 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6490 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6491 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6492 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6493 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6494 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6495 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6496 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6497 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6498 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6499 | size_t domain_parameters_size, |
| 6500 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6501 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6502 | size_t i; |
| 6503 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6504 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6505 | if( domain_parameters_size == 0 ) |
| 6506 | { |
| 6507 | *exponent = 65537; |
| 6508 | return( PSA_SUCCESS ); |
| 6509 | } |
| 6510 | |
| 6511 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6512 | * support values that fit in a 32-bit integer, which is larger than |
| 6513 | * int on just about every platform anyway. */ |
| 6514 | if( domain_parameters_size > sizeof( acc ) ) |
| 6515 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6516 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6517 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6518 | if( acc > INT_MAX ) |
| 6519 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6520 | *exponent = acc; |
| 6521 | return( PSA_SUCCESS ); |
| 6522 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6523 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6524 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6525 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6526 | psa_key_slot_t *slot, size_t bits, |
| 6527 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6528 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6529 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6530 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6531 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6532 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6533 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6534 | if( key_type_is_raw_bytes( type ) ) |
| 6535 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6536 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6537 | |
| 6538 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6539 | if( status != PSA_SUCCESS ) |
| 6540 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6541 | |
| 6542 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6543 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6544 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6545 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6546 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6547 | status = psa_generate_random( slot->data.key.data, |
| 6548 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6549 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6550 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6551 | |
| 6552 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6553 | #if defined(MBEDTLS_DES_C) |
| 6554 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6555 | psa_des_set_key_parity( slot->data.key.data, |
| 6556 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6557 | #endif /* MBEDTLS_DES_C */ |
| 6558 | } |
| 6559 | else |
| 6560 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6561 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6562 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6563 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6564 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6565 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6566 | int exponent; |
| 6567 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6568 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6569 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6570 | /* Accept only byte-aligned keys, for the same reasons as |
| 6571 | * in psa_import_rsa_key(). */ |
| 6572 | if( bits % 8 != 0 ) |
| 6573 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6574 | status = psa_read_rsa_exponent( domain_parameters, |
| 6575 | domain_parameters_size, |
| 6576 | &exponent ); |
| 6577 | if( status != PSA_SUCCESS ) |
| 6578 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6579 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6580 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6581 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6582 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6583 | (unsigned int) bits, |
| 6584 | exponent ); |
| 6585 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6586 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6587 | |
| 6588 | /* Make sure to always have an export representation available */ |
| 6589 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6590 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6591 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6592 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6593 | { |
| 6594 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6595 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6596 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6597 | |
| 6598 | status = psa_export_rsa_key( type, |
| 6599 | &rsa, |
| 6600 | slot->data.key.data, |
| 6601 | bytes, |
| 6602 | &slot->data.key.bytes ); |
| 6603 | mbedtls_rsa_free( &rsa ); |
| 6604 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6605 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6606 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6607 | } |
| 6608 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6609 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6610 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6611 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6612 | 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] | 6613 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6614 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6615 | mbedtls_ecp_group_id grp_id = |
| 6616 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6617 | const mbedtls_ecp_curve_info *curve_info = |
| 6618 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6619 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6620 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6621 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6622 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6623 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6624 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6625 | mbedtls_ecp_keypair_init( &ecp ); |
| 6626 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6627 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6628 | MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6629 | if( ret != 0 ) |
| 6630 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6631 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6632 | return( mbedtls_to_psa_error( ret ) ); |
| 6633 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6634 | |
| 6635 | |
| 6636 | /* Make sure to always have an export representation available */ |
| 6637 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6638 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6639 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6640 | { |
| 6641 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6642 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6643 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6644 | |
| 6645 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6646 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 6647 | |
| 6648 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6649 | if( status != PSA_SUCCESS ) { |
| 6650 | memset( slot->data.key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6651 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6652 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6653 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6654 | } |
| 6655 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6656 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6657 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6658 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6659 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6660 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6661 | return( PSA_SUCCESS ); |
| 6662 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6663 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6664 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6665 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6666 | { |
| 6667 | psa_status_t status; |
| 6668 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6669 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6670 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6671 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6672 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6673 | /* Reject any attempt to create a zero-length key so that we don't |
| 6674 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6675 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6676 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6677 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6678 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6679 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6680 | if( status != PSA_SUCCESS ) |
| 6681 | goto exit; |
| 6682 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6683 | status = psa_driver_wrapper_generate_key( attributes, |
| 6684 | slot ); |
| 6685 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6686 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6687 | goto exit; |
| 6688 | |
| 6689 | status = psa_generate_key_internal( |
| 6690 | slot, attributes->core.bits, |
| 6691 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6692 | |
| 6693 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6694 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6695 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6696 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6697 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6698 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6699 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6700 | } |
| 6701 | |
| 6702 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6703 | |
| 6704 | /****************************************************************/ |
| 6705 | /* Module setup */ |
| 6706 | /****************************************************************/ |
| 6707 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6708 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6709 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6710 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6711 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6712 | { |
| 6713 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6714 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6715 | global_data.rng.entropy_init = entropy_init; |
| 6716 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6717 | return( PSA_SUCCESS ); |
| 6718 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6719 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6720 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6721 | void mbedtls_psa_crypto_free( void ) |
| 6722 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6723 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6724 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6725 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6726 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6727 | } |
| 6728 | /* Wipe all remaining data, including configuration. |
| 6729 | * In particular, this sets all state indicator to the value |
| 6730 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6731 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6732 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6733 | /* Unregister all secure element drivers, so that we restart from |
| 6734 | * a pristine state. */ |
| 6735 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6736 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6737 | } |
| 6738 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6739 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6740 | /** Recover a transaction that was interrupted by a power failure. |
| 6741 | * |
| 6742 | * This function is called during initialization, before psa_crypto_init() |
| 6743 | * returns. If this function returns a failure status, the initialization |
| 6744 | * fails. |
| 6745 | */ |
| 6746 | static psa_status_t psa_crypto_recover_transaction( |
| 6747 | const psa_crypto_transaction_t *transaction ) |
| 6748 | { |
| 6749 | switch( transaction->unknown.type ) |
| 6750 | { |
| 6751 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6752 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6753 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6754 | * is implemented. |
| 6755 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6756 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6757 | default: |
| 6758 | /* We found an unsupported transaction in the storage. |
| 6759 | * We don't know what state the storage is in. Give up. */ |
| 6760 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6761 | } |
| 6762 | } |
| 6763 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6764 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6765 | psa_status_t psa_crypto_init( void ) |
| 6766 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6767 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6768 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6769 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6770 | if( global_data.initialized != 0 ) |
| 6771 | return( PSA_SUCCESS ); |
| 6772 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6773 | /* Initialize and seed the random generator. */ |
| 6774 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6775 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6776 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6777 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6778 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6779 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6780 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6781 | status = psa_initialize_key_slots( ); |
| 6782 | if( status != PSA_SUCCESS ) |
| 6783 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6784 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6785 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6786 | status = psa_init_all_se_drivers( ); |
| 6787 | if( status != PSA_SUCCESS ) |
| 6788 | goto exit; |
| 6789 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6790 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6791 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6792 | status = psa_crypto_load_transaction( ); |
| 6793 | if( status == PSA_SUCCESS ) |
| 6794 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6795 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6796 | if( status != PSA_SUCCESS ) |
| 6797 | goto exit; |
| 6798 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6799 | } |
| 6800 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6801 | { |
| 6802 | /* There's no transaction to complete. It's all good. */ |
| 6803 | status = PSA_SUCCESS; |
| 6804 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6805 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6806 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6807 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6808 | global_data.initialized = 1; |
| 6809 | |
| 6810 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6811 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6812 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6813 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6814 | } |
| 6815 | |
| 6816 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |