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) |
| 1341 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1342 | { |
| 1343 | /* No key material to clean. */ |
| 1344 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1345 | else |
| 1346 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1347 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame] | 1348 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1349 | * initialized slot, so we can just free it. */ |
| 1350 | if( slot->data.key.data != NULL ) |
| 1351 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1352 | mbedtls_free( slot->data.key.data ); |
| 1353 | slot->data.key.data = NULL; |
| 1354 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1355 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1356 | |
| 1357 | return( PSA_SUCCESS ); |
| 1358 | } |
| 1359 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1360 | /** Completely wipe a slot in memory, including its policy. |
| 1361 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1362 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1363 | { |
| 1364 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * 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] | 1368 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1369 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1370 | * 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] | 1371 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1372 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1373 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1374 | { |
| 1375 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1376 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1377 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1378 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1379 | } |
| 1380 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1381 | /* Multipart operations may still be using the key. This is safe |
| 1382 | * because all multipart operation objects are independent from |
| 1383 | * the key slot: if they need to access the key after the setup |
| 1384 | * phase, they have a copy of the key. Note that this means that |
| 1385 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1386 | /* At this point, key material and other type-specific content has |
| 1387 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1388 | * zeroize because the metadata is not particularly sensitive. */ |
| 1389 | memset( slot, 0, sizeof( *slot ) ); |
| 1390 | return( status ); |
| 1391 | } |
| 1392 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1393 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1394 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1395 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1396 | psa_status_t status; /* status of the last operation */ |
| 1397 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1398 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1399 | psa_se_drv_table_entry_t *driver; |
| 1400 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1401 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1402 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1403 | return( PSA_SUCCESS ); |
| 1404 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1405 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1406 | * 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] | 1407 | * key, this will load the key description from persistent memory if not |
| 1408 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1409 | * the key is operated by an SE or not and this information is needed by |
| 1410 | * the current implementation. |
| 1411 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1412 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1413 | if( status != PSA_SUCCESS ) |
| 1414 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1415 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1416 | /* |
| 1417 | * If the key slot containing the key description is under access by the |
| 1418 | * library (apart from the present access), the key cannot be destroyed |
| 1419 | * yet. For the time being, just return in error. Eventually (to be |
| 1420 | * implemented), the key should be destroyed when all accesses have |
| 1421 | * stopped. |
| 1422 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1423 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1424 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1425 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1426 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1427 | } |
| 1428 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1429 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1430 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1431 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1432 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1433 | /* For a key in a secure element, we need to do three things: |
| 1434 | * remove the key file in internal storage, destroy the |
| 1435 | * key inside the secure element, and update the driver's |
| 1436 | * persistent data. Start a transaction that will encompass these |
| 1437 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1438 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1439 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1440 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1441 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1442 | status = psa_crypto_save_transaction( ); |
| 1443 | if( status != PSA_SUCCESS ) |
| 1444 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1445 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1446 | /* We should still try to destroy the key in the secure |
| 1447 | * element and the key metadata in storage. This is especially |
| 1448 | * important if the error is that the storage is full. |
| 1449 | * But how to do it exactly without risking an inconsistent |
| 1450 | * state after a reset? |
| 1451 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1452 | */ |
| 1453 | overall_status = status; |
| 1454 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1457 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1458 | if( overall_status == PSA_SUCCESS ) |
| 1459 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1460 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1461 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1462 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1463 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1464 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1465 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1466 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1467 | if( overall_status == PSA_SUCCESS ) |
| 1468 | overall_status = status; |
| 1469 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1470 | /* TODO: other slots may have a copy of the same key. We should |
| 1471 | * invalidate them. |
| 1472 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1473 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1474 | } |
| 1475 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1476 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1477 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1478 | if( driver != NULL ) |
| 1479 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1480 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1481 | if( overall_status == PSA_SUCCESS ) |
| 1482 | overall_status = status; |
| 1483 | status = psa_crypto_stop_transaction( ); |
| 1484 | if( overall_status == PSA_SUCCESS ) |
| 1485 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1486 | } |
| 1487 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1488 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1489 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1490 | exit: |
| 1491 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1492 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1493 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1494 | if( overall_status == PSA_SUCCESS ) |
| 1495 | overall_status = status; |
| 1496 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1499 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1500 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1501 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1502 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1505 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1506 | psa_key_type_t type, |
| 1507 | const uint8_t *data, |
| 1508 | size_t data_length ) |
| 1509 | { |
| 1510 | uint8_t *copy = NULL; |
| 1511 | |
| 1512 | if( data_length != 0 ) |
| 1513 | { |
| 1514 | copy = mbedtls_calloc( 1, data_length ); |
| 1515 | if( copy == NULL ) |
| 1516 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1517 | memcpy( copy, data, data_length ); |
| 1518 | } |
| 1519 | /* After this point, this function is guaranteed to succeed, so it |
| 1520 | * can start modifying `*attributes`. */ |
| 1521 | |
| 1522 | if( attributes->domain_parameters != NULL ) |
| 1523 | { |
| 1524 | mbedtls_free( attributes->domain_parameters ); |
| 1525 | attributes->domain_parameters = NULL; |
| 1526 | attributes->domain_parameters_size = 0; |
| 1527 | } |
| 1528 | |
| 1529 | attributes->domain_parameters = copy; |
| 1530 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1531 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1532 | return( PSA_SUCCESS ); |
| 1533 | } |
| 1534 | |
| 1535 | psa_status_t psa_get_key_domain_parameters( |
| 1536 | const psa_key_attributes_t *attributes, |
| 1537 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1538 | { |
| 1539 | if( attributes->domain_parameters_size > data_size ) |
| 1540 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1541 | *data_length = attributes->domain_parameters_size; |
| 1542 | if( attributes->domain_parameters_size != 0 ) |
| 1543 | memcpy( data, attributes->domain_parameters, |
| 1544 | attributes->domain_parameters_size ); |
| 1545 | return( PSA_SUCCESS ); |
| 1546 | } |
| 1547 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1548 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1549 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1550 | static psa_status_t psa_get_rsa_public_exponent( |
| 1551 | const mbedtls_rsa_context *rsa, |
| 1552 | psa_key_attributes_t *attributes ) |
| 1553 | { |
| 1554 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1555 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1556 | uint8_t *buffer = NULL; |
| 1557 | size_t buflen; |
| 1558 | mbedtls_mpi_init( &mpi ); |
| 1559 | |
| 1560 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1561 | if( ret != 0 ) |
| 1562 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1563 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1564 | { |
| 1565 | /* It's the default value, which is reported as an empty string, |
| 1566 | * so there's nothing to do. */ |
| 1567 | goto exit; |
| 1568 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1569 | |
| 1570 | buflen = mbedtls_mpi_size( &mpi ); |
| 1571 | buffer = mbedtls_calloc( 1, buflen ); |
| 1572 | if( buffer == NULL ) |
| 1573 | { |
| 1574 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1575 | goto exit; |
| 1576 | } |
| 1577 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1578 | if( ret != 0 ) |
| 1579 | goto exit; |
| 1580 | attributes->domain_parameters = buffer; |
| 1581 | attributes->domain_parameters_size = buflen; |
| 1582 | |
| 1583 | exit: |
| 1584 | mbedtls_mpi_free( &mpi ); |
| 1585 | if( ret != 0 ) |
| 1586 | mbedtls_free( buffer ); |
| 1587 | return( mbedtls_to_psa_error( ret ) ); |
| 1588 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1589 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1590 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1591 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1592 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1593 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1594 | 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] | 1595 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1597 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1598 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1599 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1601 | psa_reset_key_attributes( attributes ); |
| 1602 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1603 | 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] | 1604 | if( status != PSA_SUCCESS ) |
| 1605 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1606 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1607 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1608 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1609 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1610 | |
| 1611 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1612 | if( psa_key_slot_is_external( slot ) ) |
| 1613 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1614 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1615 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1616 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1617 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1618 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1619 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1620 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1621 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1622 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1623 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1624 | * is not yet implemented. |
| 1625 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1626 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1627 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1628 | break; |
| 1629 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1630 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1631 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1632 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1633 | status = psa_load_rsa_representation( slot->attr.type, |
| 1634 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1635 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1636 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1637 | if( status != PSA_SUCCESS ) |
| 1638 | break; |
| 1639 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1640 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1641 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1642 | mbedtls_rsa_free( rsa ); |
| 1643 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1644 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1645 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1646 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1647 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1648 | default: |
| 1649 | /* Nothing else to do. */ |
| 1650 | break; |
| 1651 | } |
| 1652 | |
| 1653 | if( status != PSA_SUCCESS ) |
| 1654 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1655 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1656 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1657 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1658 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1659 | } |
| 1660 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1661 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1662 | psa_status_t psa_get_key_slot_number( |
| 1663 | const psa_key_attributes_t *attributes, |
| 1664 | psa_key_slot_number_t *slot_number ) |
| 1665 | { |
| 1666 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1667 | { |
| 1668 | *slot_number = attributes->slot_number; |
| 1669 | return( PSA_SUCCESS ); |
| 1670 | } |
| 1671 | else |
| 1672 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1673 | } |
| 1674 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1675 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1676 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1677 | uint8_t *data, |
| 1678 | size_t data_size, |
| 1679 | size_t *data_length ) |
| 1680 | { |
| 1681 | if( slot->data.key.bytes > data_size ) |
| 1682 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1683 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1684 | memset( data + slot->data.key.bytes, 0, |
| 1685 | data_size - slot->data.key.bytes ); |
| 1686 | *data_length = slot->data.key.bytes; |
| 1687 | return( PSA_SUCCESS ); |
| 1688 | } |
| 1689 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1690 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1691 | uint8_t *data, |
| 1692 | size_t data_size, |
| 1693 | size_t *data_length, |
| 1694 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1695 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1696 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1697 | const psa_drv_se_t *drv; |
| 1698 | psa_drv_se_context_t *drv_context; |
| 1699 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1700 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1701 | *data_length = 0; |
| 1702 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1703 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1704 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1705 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1706 | /* Reject a zero-length output buffer now, since this can never be a |
| 1707 | * valid key representation. This way we know that data must be a valid |
| 1708 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1709 | if( data_size == 0 ) |
| 1710 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1711 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1712 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1713 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1714 | { |
| 1715 | psa_drv_se_export_key_t method; |
| 1716 | if( drv->key_management == NULL ) |
| 1717 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1718 | method = ( export_public_key ? |
| 1719 | drv->key_management->p_export_public : |
| 1720 | drv->key_management->p_export ); |
| 1721 | if( method == NULL ) |
| 1722 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1723 | return( method( drv_context, |
| 1724 | slot->data.se.slot_number, |
| 1725 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1726 | } |
| 1727 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1728 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1729 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1730 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1731 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1732 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1733 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1734 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1735 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1736 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1737 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1738 | /* Exporting public -> public */ |
| 1739 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1740 | } |
| 1741 | else if( !export_public_key ) |
| 1742 | { |
| 1743 | /* Exporting private -> private */ |
| 1744 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1745 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1746 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1747 | /* Need to export the public part of a private key, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1748 | * so conversion is needed. Try the accelerators first. */ |
| 1749 | psa_status_t status = psa_driver_wrapper_export_public_key( slot, |
| 1750 | data, |
| 1751 | data_size, |
| 1752 | data_length ); |
| 1753 | |
| 1754 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 1755 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 1756 | return( status ); |
| 1757 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1758 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1759 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1760 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1761 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1762 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1763 | status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1764 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1765 | slot->data.key.data, |
| 1766 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1767 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1768 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1769 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1770 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1771 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1772 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1773 | data, |
| 1774 | data_size, |
| 1775 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1776 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1777 | mbedtls_rsa_free( rsa ); |
| 1778 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1779 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1780 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1781 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1782 | /* We don't know how to convert a private RSA key to public. */ |
| 1783 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1784 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1785 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1786 | } |
| 1787 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1788 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1789 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1790 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1791 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1792 | status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1793 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1794 | slot->data.key.data, |
| 1795 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1796 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1797 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1798 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1799 | |
| 1800 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1801 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1802 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1803 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1804 | data, |
| 1805 | data_size, |
| 1806 | data_length ); |
| 1807 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1808 | mbedtls_ecp_keypair_free( ecp ); |
| 1809 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1810 | return( status ); |
| 1811 | #else |
| 1812 | /* 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] | 1813 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1814 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1815 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1816 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1817 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1818 | else |
| 1819 | { |
| 1820 | /* This shouldn't happen in the reference implementation, but |
| 1821 | it is valid for a special-purpose implementation to omit |
| 1822 | support for exporting certain key types. */ |
| 1823 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1824 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1825 | } |
| 1826 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1827 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1828 | uint8_t *data, |
| 1829 | size_t data_size, |
| 1830 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1831 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1832 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1833 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1834 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1835 | |
| 1836 | /* Set the key to empty now, so that even when there are errors, we always |
| 1837 | * set data_length to a value between 0 and data_size. On error, setting |
| 1838 | * the key to empty is a good choice because an empty key representation is |
| 1839 | * unlikely to be accepted anywhere. */ |
| 1840 | *data_length = 0; |
| 1841 | |
| 1842 | /* Export requires the EXPORT flag. There is an exception for public keys, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1843 | * which don't require any flag, but |
| 1844 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1845 | */ |
| 1846 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1847 | PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1848 | if( status != PSA_SUCCESS ) |
| 1849 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1850 | |
| 1851 | status = psa_internal_export_key( slot, data, data_size, data_length, 0 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1852 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1853 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1854 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1855 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1856 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1857 | 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] | 1858 | uint8_t *data, |
| 1859 | size_t data_size, |
| 1860 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1861 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1862 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1863 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1864 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1865 | |
| 1866 | /* Set the key to empty now, so that even when there are errors, we always |
| 1867 | * set data_length to a value between 0 and data_size. On error, setting |
| 1868 | * the key to empty is a good choice because an empty key representation is |
| 1869 | * unlikely to be accepted anywhere. */ |
| 1870 | *data_length = 0; |
| 1871 | |
| 1872 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1873 | 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] | 1874 | if( status != PSA_SUCCESS ) |
| 1875 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1876 | |
| 1877 | status = psa_internal_export_key( slot, data, data_size, data_length, 1 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1878 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1879 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1880 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1881 | } |
| 1882 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1883 | #if defined(static_assert) |
| 1884 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1885 | "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] | 1886 | 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] | 1887 | "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] | 1888 | 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] | 1889 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1890 | #endif |
| 1891 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1892 | /** Validate that a key policy is internally well-formed. |
| 1893 | * |
| 1894 | * This function only rejects invalid policies. It does not validate the |
| 1895 | * consistency of the policy with respect to other attributes of the key |
| 1896 | * such as the key type. |
| 1897 | */ |
| 1898 | 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] | 1899 | { |
| 1900 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1901 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1902 | PSA_KEY_USAGE_ENCRYPT | |
| 1903 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1904 | PSA_KEY_USAGE_SIGN_HASH | |
| 1905 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1906 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1907 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1908 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1909 | return( PSA_SUCCESS ); |
| 1910 | } |
| 1911 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1912 | /** Validate the internal consistency of key attributes. |
| 1913 | * |
| 1914 | * This function only rejects invalid attribute values. If does not |
| 1915 | * validate the consistency of the attributes with any key data that may |
| 1916 | * be involved in the creation of the key. |
| 1917 | * |
| 1918 | * Call this function early in the key creation process. |
| 1919 | * |
| 1920 | * \param[in] attributes Key attributes for the new key. |
| 1921 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1922 | * NULL for a transparent key. |
| 1923 | * |
| 1924 | */ |
| 1925 | static psa_status_t psa_validate_key_attributes( |
| 1926 | const psa_key_attributes_t *attributes, |
| 1927 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1928 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1929 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1930 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1931 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1932 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1933 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1934 | if( status != PSA_SUCCESS ) |
| 1935 | return( status ); |
| 1936 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1937 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1938 | if( status != PSA_SUCCESS ) |
| 1939 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1940 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1941 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1942 | { |
| 1943 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1944 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1945 | } |
| 1946 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1947 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1948 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1949 | if( status != PSA_SUCCESS ) |
| 1950 | return( status ); |
| 1951 | } |
| 1952 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1953 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1954 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1955 | return( status ); |
| 1956 | |
| 1957 | /* Refuse to create overly large keys. |
| 1958 | * Note that this doesn't trigger on import if the attributes don't |
| 1959 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1960 | * psa_import_key() needs its own checks. */ |
| 1961 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1962 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1963 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1964 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1965 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1966 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1967 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1968 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1969 | return( PSA_SUCCESS ); |
| 1970 | } |
| 1971 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1972 | /** Prepare a key slot to receive key material. |
| 1973 | * |
| 1974 | * This function allocates a key slot and sets its metadata. |
| 1975 | * |
| 1976 | * If this function fails, call psa_fail_key_creation(). |
| 1977 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1978 | * This function is intended to be used as follows: |
| 1979 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1980 | * it with the specified attributes, and in case of a volatile key assign it |
| 1981 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1982 | * -# Populate the slot with the key material. |
| 1983 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1984 | * In case of failure at any step, stop the sequence and call |
| 1985 | * psa_fail_key_creation(). |
| 1986 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1987 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1988 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1989 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1990 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1991 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1992 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1993 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1994 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1995 | * |
| 1996 | * \retval #PSA_SUCCESS |
| 1997 | * The key slot is ready to receive key material. |
| 1998 | * \return If this function fails, the key slot is an invalid state. |
| 1999 | * 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] | 2000 | */ |
| 2001 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2002 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2003 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2004 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2005 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2006 | { |
| 2007 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 2008 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2009 | psa_key_slot_t *slot; |
| 2010 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2011 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2012 | *p_drv = NULL; |
| 2013 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2014 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 2015 | if( status != PSA_SUCCESS ) |
| 2016 | return( status ); |
| 2017 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2018 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2019 | if( status != PSA_SUCCESS ) |
| 2020 | return( status ); |
| 2021 | slot = *p_slot; |
| 2022 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2023 | /* We're storing the declared bit-size of the key. It's up to each |
| 2024 | * creation mechanism to verify that this information is correct. |
| 2025 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2026 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2027 | * is optional (import, copy). In case of a volatile key, assign it the |
| 2028 | * volatile key identifier associated to the slot returned to contain its |
| 2029 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2030 | |
| 2031 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2032 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 2033 | { |
| 2034 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 2035 | slot->attr.id = volatile_key_id; |
| 2036 | #else |
| 2037 | slot->attr.id.key_id = volatile_key_id; |
| 2038 | #endif |
| 2039 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 2040 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2041 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2042 | * external-only flags, query `attributes`. Thanks to the check |
| 2043 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 2044 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2045 | * may have set. */ |
| 2046 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2047 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2048 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2049 | /* 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] | 2050 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2051 | * create the key file in internal storage, create the |
| 2052 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2053 | * persistent data. This is done by starting a transaction that will |
| 2054 | * encompass these three actions. |
| 2055 | * For registering a volatile key, we just need to find an appropriate |
| 2056 | * slot number inside the SE. Since the key is designated volatile, creating |
| 2057 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2058 | /* The first thing to do is to find a slot number for the new key. |
| 2059 | * We save the slot number in persistent storage as part of the |
| 2060 | * transaction data. It will be needed to recover if the power |
| 2061 | * fails during the key creation process, to clean up on the secure |
| 2062 | * element side after restarting. Obtaining a slot number from the |
| 2063 | * secure element driver updates its persistent state, but we do not yet |
| 2064 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2065 | * 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] | 2066 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2067 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 2068 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2069 | &slot->data.se.slot_number ); |
| 2070 | if( status != PSA_SUCCESS ) |
| 2071 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2072 | |
| 2073 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2074 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2075 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 2076 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 2077 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 2078 | psa_crypto_transaction.key.id = slot->attr.id; |
| 2079 | status = psa_crypto_save_transaction( ); |
| 2080 | if( status != PSA_SUCCESS ) |
| 2081 | { |
| 2082 | (void) psa_crypto_stop_transaction( ); |
| 2083 | return( status ); |
| 2084 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2085 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2086 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2087 | |
| 2088 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2089 | { |
| 2090 | /* Key registration only makes sense with a secure element. */ |
| 2091 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2092 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2093 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2094 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2095 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2096 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2097 | |
| 2098 | /** Finalize the creation of a key once its key material has been set. |
| 2099 | * |
| 2100 | * This entails writing the key to persistent storage. |
| 2101 | * |
| 2102 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2103 | * See the documentation of psa_start_key_creation() for the intended use |
| 2104 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2105 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2106 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 2107 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 2108 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2109 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2110 | * \param[in,out] slot Pointer to the slot with key material. |
| 2111 | * \param[in] driver The secure element driver for the key, |
| 2112 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2113 | * \param[out] key On success, identifier of the key. Note that the |
| 2114 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2115 | * |
| 2116 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2117 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2118 | * \return If this function fails, the key slot is an invalid state. |
| 2119 | * 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] | 2120 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2121 | static psa_status_t psa_finish_key_creation( |
| 2122 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2123 | psa_se_drv_table_entry_t *driver, |
| 2124 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2125 | { |
| 2126 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2127 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2128 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2129 | |
| 2130 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2131 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2132 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2133 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2134 | if( driver != NULL ) |
| 2135 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2136 | psa_se_key_data_storage_t data; |
| 2137 | #if defined(static_assert) |
| 2138 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 2139 | sizeof( data.slot_number ), |
| 2140 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2141 | #endif |
| 2142 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 2143 | sizeof( slot->data.se.slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2144 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2145 | (uint8_t*) &data, |
| 2146 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2147 | } |
| 2148 | else |
| 2149 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2150 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2151 | /* Key material is saved in export representation in the slot, so |
| 2152 | * just pass the slot buffer for storage. */ |
| 2153 | status = psa_save_persistent_key( &slot->attr, |
| 2154 | slot->data.key.data, |
| 2155 | slot->data.key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2156 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2157 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2158 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2159 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2160 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2161 | /* Finish the transaction for a key creation. This does not |
| 2162 | * happen when registering an existing key. Detect this case |
| 2163 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2164 | * creation of a persistent key in a secure element requires a transaction, |
| 2165 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2166 | if( driver != NULL && |
| 2167 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2168 | { |
| 2169 | status = psa_save_se_persistent_data( driver ); |
| 2170 | if( status != PSA_SUCCESS ) |
| 2171 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2172 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2173 | return( status ); |
| 2174 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2175 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2176 | } |
| 2177 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2178 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2179 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2180 | { |
| 2181 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2182 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2183 | if( status != PSA_SUCCESS ) |
| 2184 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2185 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2186 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2187 | return( status ); |
| 2188 | } |
| 2189 | |
| 2190 | /** Abort the creation of a key. |
| 2191 | * |
| 2192 | * You may call this function after calling psa_start_key_creation(), |
| 2193 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2194 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2195 | * See the documentation of psa_start_key_creation() for the intended use |
| 2196 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2197 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2198 | * \param[in,out] slot Pointer to the slot with key material. |
| 2199 | * \param[in] driver The secure element driver for the key, |
| 2200 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2201 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2202 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2203 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2204 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2205 | (void) driver; |
| 2206 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2207 | if( slot == NULL ) |
| 2208 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2209 | |
| 2210 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2211 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2212 | * element, and the failure happened later (when saving metadata |
| 2213 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2214 | * element. |
| 2215 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2216 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2217 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2218 | /* Abort the ongoing transaction if any (there may not be one if |
| 2219 | * the creation process failed before starting one, or if the |
| 2220 | * key creation is a registration of a key in a secure element). |
| 2221 | * Earlier functions must already have done what it takes to undo any |
| 2222 | * partial creation. All that's left is to update the transaction data |
| 2223 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2224 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2225 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2226 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2227 | psa_wipe_key_slot( slot ); |
| 2228 | } |
| 2229 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2230 | /** Validate optional attributes during key creation. |
| 2231 | * |
| 2232 | * Some key attributes are optional during key creation. If they are |
| 2233 | * specified in the attributes structure, check that they are consistent |
| 2234 | * with the data in the slot. |
| 2235 | * |
| 2236 | * This function should be called near the end of key creation, after |
| 2237 | * the slot in memory is fully populated but before saving persistent data. |
| 2238 | */ |
| 2239 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2240 | const psa_key_slot_t *slot, |
| 2241 | const psa_key_attributes_t *attributes ) |
| 2242 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2243 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2244 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2245 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2246 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2247 | } |
| 2248 | |
| 2249 | if( attributes->domain_parameters_size != 0 ) |
| 2250 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2251 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2252 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2253 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2254 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2255 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2256 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2257 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2258 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2259 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2260 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2261 | slot->data.key.data, |
| 2262 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2263 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2264 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2265 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2266 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2267 | mbedtls_mpi_init( &actual ); |
| 2268 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2269 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2270 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2271 | mbedtls_rsa_free( rsa ); |
| 2272 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2273 | if( ret != 0 ) |
| 2274 | goto rsa_exit; |
| 2275 | ret = mbedtls_mpi_read_binary( &required, |
| 2276 | attributes->domain_parameters, |
| 2277 | attributes->domain_parameters_size ); |
| 2278 | if( ret != 0 ) |
| 2279 | goto rsa_exit; |
| 2280 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2281 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2282 | rsa_exit: |
| 2283 | mbedtls_mpi_free( &actual ); |
| 2284 | mbedtls_mpi_free( &required ); |
| 2285 | if( ret != 0) |
| 2286 | return( mbedtls_to_psa_error( ret ) ); |
| 2287 | } |
| 2288 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2289 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2290 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2291 | { |
| 2292 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2293 | } |
| 2294 | } |
| 2295 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2296 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2297 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2298 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2299 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2300 | } |
| 2301 | |
| 2302 | return( PSA_SUCCESS ); |
| 2303 | } |
| 2304 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2305 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2306 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2307 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2308 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2309 | { |
| 2310 | psa_status_t status; |
| 2311 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2312 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2313 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2314 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2315 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2316 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2317 | * This also rejects any key which might be encoded as an empty string, |
| 2318 | * which is never valid. */ |
| 2319 | if( data_length == 0 ) |
| 2320 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2321 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2322 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2323 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2324 | if( status != PSA_SUCCESS ) |
| 2325 | goto exit; |
| 2326 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2327 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2328 | if( driver != NULL ) |
| 2329 | { |
| 2330 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2331 | /* The driver should set the number of key bits, however in |
| 2332 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2333 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2334 | if( drv->key_management == NULL || |
| 2335 | drv->key_management->p_import == NULL ) |
| 2336 | { |
| 2337 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2338 | goto exit; |
| 2339 | } |
| 2340 | status = drv->key_management->p_import( |
| 2341 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2342 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2343 | &bits ); |
| 2344 | if( status != PSA_SUCCESS ) |
| 2345 | goto exit; |
| 2346 | if( bits > PSA_MAX_KEY_BITS ) |
| 2347 | { |
| 2348 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2349 | goto exit; |
| 2350 | } |
| 2351 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2352 | } |
| 2353 | else |
| 2354 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2355 | { |
| 2356 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2357 | if( status != PSA_SUCCESS ) |
| 2358 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2359 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2360 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2361 | if( status != PSA_SUCCESS ) |
| 2362 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2363 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2364 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2365 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2366 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2367 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2368 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2369 | return( status ); |
| 2370 | } |
| 2371 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2372 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2373 | psa_status_t mbedtls_psa_register_se_key( |
| 2374 | const psa_key_attributes_t *attributes ) |
| 2375 | { |
| 2376 | psa_status_t status; |
| 2377 | psa_key_slot_t *slot = NULL; |
| 2378 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2379 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2380 | |
| 2381 | /* Leaving attributes unspecified is not currently supported. |
| 2382 | * It could make sense to query the key type and size from the |
| 2383 | * secure element, but not all secure elements support this |
| 2384 | * and the driver HAL doesn't currently support it. */ |
| 2385 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2386 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2387 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2388 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2389 | |
| 2390 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2391 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2392 | if( status != PSA_SUCCESS ) |
| 2393 | goto exit; |
| 2394 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2395 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2396 | |
| 2397 | exit: |
| 2398 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2399 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2400 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2401 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2402 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2403 | return( status ); |
| 2404 | } |
| 2405 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2406 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2407 | 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] | 2408 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2409 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2410 | psa_status_t status = psa_copy_key_material_into_slot( target, |
| 2411 | source->data.key.data, |
| 2412 | source->data.key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2413 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2414 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2415 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2416 | target->attr.type = source->attr.type; |
| 2417 | target->attr.bits = source->attr.bits; |
| 2418 | |
| 2419 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2422 | 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] | 2423 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2424 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2425 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2426 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2427 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2428 | psa_key_slot_t *source_slot = NULL; |
| 2429 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2430 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2431 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2432 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2433 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2434 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2435 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2436 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2437 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2438 | goto exit; |
| 2439 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2440 | status = psa_validate_optional_attributes( source_slot, |
| 2441 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2442 | if( status != PSA_SUCCESS ) |
| 2443 | goto exit; |
| 2444 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2445 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2446 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2447 | if( status != PSA_SUCCESS ) |
| 2448 | goto exit; |
| 2449 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2450 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2451 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2452 | if( status != PSA_SUCCESS ) |
| 2453 | goto exit; |
| 2454 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2455 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2456 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2457 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2458 | /* Copying to a secure element is not implemented yet. */ |
| 2459 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2460 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2461 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2462 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2463 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2464 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2465 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2466 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2467 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2468 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2469 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2470 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2471 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2472 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2473 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2474 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2475 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2478 | |
| 2479 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2480 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2481 | /* Message digests */ |
| 2482 | /****************************************************************/ |
| 2483 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2484 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2485 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2486 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2487 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2488 | 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] | 2489 | { |
| 2490 | switch( alg ) |
| 2491 | { |
| 2492 | #if defined(MBEDTLS_MD2_C) |
| 2493 | case PSA_ALG_MD2: |
| 2494 | return( &mbedtls_md2_info ); |
| 2495 | #endif |
| 2496 | #if defined(MBEDTLS_MD4_C) |
| 2497 | case PSA_ALG_MD4: |
| 2498 | return( &mbedtls_md4_info ); |
| 2499 | #endif |
| 2500 | #if defined(MBEDTLS_MD5_C) |
| 2501 | case PSA_ALG_MD5: |
| 2502 | return( &mbedtls_md5_info ); |
| 2503 | #endif |
| 2504 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2505 | case PSA_ALG_RIPEMD160: |
| 2506 | return( &mbedtls_ripemd160_info ); |
| 2507 | #endif |
| 2508 | #if defined(MBEDTLS_SHA1_C) |
| 2509 | case PSA_ALG_SHA_1: |
| 2510 | return( &mbedtls_sha1_info ); |
| 2511 | #endif |
| 2512 | #if defined(MBEDTLS_SHA256_C) |
| 2513 | case PSA_ALG_SHA_224: |
| 2514 | return( &mbedtls_sha224_info ); |
| 2515 | case PSA_ALG_SHA_256: |
| 2516 | return( &mbedtls_sha256_info ); |
| 2517 | #endif |
| 2518 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2519 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2520 | case PSA_ALG_SHA_384: |
| 2521 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2522 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2523 | case PSA_ALG_SHA_512: |
| 2524 | return( &mbedtls_sha512_info ); |
| 2525 | #endif |
| 2526 | default: |
| 2527 | return( NULL ); |
| 2528 | } |
| 2529 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2530 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2531 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2532 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2533 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2534 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2535 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2536 | { |
| 2537 | switch( operation->alg ) |
| 2538 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2539 | case 0: |
| 2540 | /* The object has (apparently) been initialized but it is not |
| 2541 | * in use. It's ok to call abort on such an object, and there's |
| 2542 | * nothing to do. */ |
| 2543 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2544 | #if defined(MBEDTLS_MD2_C) |
| 2545 | case PSA_ALG_MD2: |
| 2546 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2547 | break; |
| 2548 | #endif |
| 2549 | #if defined(MBEDTLS_MD4_C) |
| 2550 | case PSA_ALG_MD4: |
| 2551 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2552 | break; |
| 2553 | #endif |
| 2554 | #if defined(MBEDTLS_MD5_C) |
| 2555 | case PSA_ALG_MD5: |
| 2556 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2557 | break; |
| 2558 | #endif |
| 2559 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2560 | case PSA_ALG_RIPEMD160: |
| 2561 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2562 | break; |
| 2563 | #endif |
| 2564 | #if defined(MBEDTLS_SHA1_C) |
| 2565 | case PSA_ALG_SHA_1: |
| 2566 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2567 | break; |
| 2568 | #endif |
| 2569 | #if defined(MBEDTLS_SHA256_C) |
| 2570 | case PSA_ALG_SHA_224: |
| 2571 | case PSA_ALG_SHA_256: |
| 2572 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2573 | break; |
| 2574 | #endif |
| 2575 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2576 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2577 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2578 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2579 | case PSA_ALG_SHA_512: |
| 2580 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2581 | break; |
| 2582 | #endif |
| 2583 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2584 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2585 | } |
| 2586 | operation->alg = 0; |
| 2587 | return( PSA_SUCCESS ); |
| 2588 | } |
| 2589 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2590 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2591 | psa_algorithm_t alg ) |
| 2592 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2593 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2594 | |
| 2595 | /* A context must be freshly initialized before it can be set up. */ |
| 2596 | if( operation->alg != 0 ) |
| 2597 | { |
| 2598 | return( PSA_ERROR_BAD_STATE ); |
| 2599 | } |
| 2600 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2601 | switch( alg ) |
| 2602 | { |
| 2603 | #if defined(MBEDTLS_MD2_C) |
| 2604 | case PSA_ALG_MD2: |
| 2605 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2606 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2607 | break; |
| 2608 | #endif |
| 2609 | #if defined(MBEDTLS_MD4_C) |
| 2610 | case PSA_ALG_MD4: |
| 2611 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2612 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2613 | break; |
| 2614 | #endif |
| 2615 | #if defined(MBEDTLS_MD5_C) |
| 2616 | case PSA_ALG_MD5: |
| 2617 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2618 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2619 | break; |
| 2620 | #endif |
| 2621 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2622 | case PSA_ALG_RIPEMD160: |
| 2623 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2624 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2625 | break; |
| 2626 | #endif |
| 2627 | #if defined(MBEDTLS_SHA1_C) |
| 2628 | case PSA_ALG_SHA_1: |
| 2629 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2630 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2631 | break; |
| 2632 | #endif |
| 2633 | #if defined(MBEDTLS_SHA256_C) |
| 2634 | case PSA_ALG_SHA_224: |
| 2635 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2636 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2637 | break; |
| 2638 | case PSA_ALG_SHA_256: |
| 2639 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2640 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2641 | break; |
| 2642 | #endif |
| 2643 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2644 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2645 | case PSA_ALG_SHA_384: |
| 2646 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2647 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2648 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2649 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2650 | case PSA_ALG_SHA_512: |
| 2651 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2652 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2653 | break; |
| 2654 | #endif |
| 2655 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2656 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2657 | PSA_ERROR_NOT_SUPPORTED : |
| 2658 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2659 | } |
| 2660 | if( ret == 0 ) |
| 2661 | operation->alg = alg; |
| 2662 | else |
| 2663 | psa_hash_abort( operation ); |
| 2664 | return( mbedtls_to_psa_error( ret ) ); |
| 2665 | } |
| 2666 | |
| 2667 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2668 | const uint8_t *input, |
| 2669 | size_t input_length ) |
| 2670 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2671 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2672 | |
| 2673 | /* Don't require hash implementations to behave correctly on a |
| 2674 | * zero-length input, which may have an invalid pointer. */ |
| 2675 | if( input_length == 0 ) |
| 2676 | return( PSA_SUCCESS ); |
| 2677 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2678 | switch( operation->alg ) |
| 2679 | { |
| 2680 | #if defined(MBEDTLS_MD2_C) |
| 2681 | case PSA_ALG_MD2: |
| 2682 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2683 | input, input_length ); |
| 2684 | break; |
| 2685 | #endif |
| 2686 | #if defined(MBEDTLS_MD4_C) |
| 2687 | case PSA_ALG_MD4: |
| 2688 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2689 | input, input_length ); |
| 2690 | break; |
| 2691 | #endif |
| 2692 | #if defined(MBEDTLS_MD5_C) |
| 2693 | case PSA_ALG_MD5: |
| 2694 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2695 | input, input_length ); |
| 2696 | break; |
| 2697 | #endif |
| 2698 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2699 | case PSA_ALG_RIPEMD160: |
| 2700 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2701 | input, input_length ); |
| 2702 | break; |
| 2703 | #endif |
| 2704 | #if defined(MBEDTLS_SHA1_C) |
| 2705 | case PSA_ALG_SHA_1: |
| 2706 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2707 | input, input_length ); |
| 2708 | break; |
| 2709 | #endif |
| 2710 | #if defined(MBEDTLS_SHA256_C) |
| 2711 | case PSA_ALG_SHA_224: |
| 2712 | case PSA_ALG_SHA_256: |
| 2713 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2714 | input, input_length ); |
| 2715 | break; |
| 2716 | #endif |
| 2717 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2718 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2719 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2720 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2721 | case PSA_ALG_SHA_512: |
| 2722 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2723 | input, input_length ); |
| 2724 | break; |
| 2725 | #endif |
| 2726 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2727 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2728 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2729 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2730 | if( ret != 0 ) |
| 2731 | psa_hash_abort( operation ); |
| 2732 | return( mbedtls_to_psa_error( ret ) ); |
| 2733 | } |
| 2734 | |
| 2735 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2736 | uint8_t *hash, |
| 2737 | size_t hash_size, |
| 2738 | size_t *hash_length ) |
| 2739 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2740 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2741 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2742 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2743 | |
| 2744 | /* Fill the output buffer with something that isn't a valid hash |
| 2745 | * (barring an attack on the hash and deliberately-crafted input), |
| 2746 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2747 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2748 | /* If hash_size is 0 then hash may be NULL and then the |
| 2749 | * call to memset would have undefined behavior. */ |
| 2750 | if( hash_size != 0 ) |
| 2751 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2752 | |
| 2753 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2754 | { |
| 2755 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2756 | goto exit; |
| 2757 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2758 | |
| 2759 | switch( operation->alg ) |
| 2760 | { |
| 2761 | #if defined(MBEDTLS_MD2_C) |
| 2762 | case PSA_ALG_MD2: |
| 2763 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2764 | break; |
| 2765 | #endif |
| 2766 | #if defined(MBEDTLS_MD4_C) |
| 2767 | case PSA_ALG_MD4: |
| 2768 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2769 | break; |
| 2770 | #endif |
| 2771 | #if defined(MBEDTLS_MD5_C) |
| 2772 | case PSA_ALG_MD5: |
| 2773 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2774 | break; |
| 2775 | #endif |
| 2776 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2777 | case PSA_ALG_RIPEMD160: |
| 2778 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2779 | break; |
| 2780 | #endif |
| 2781 | #if defined(MBEDTLS_SHA1_C) |
| 2782 | case PSA_ALG_SHA_1: |
| 2783 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2784 | break; |
| 2785 | #endif |
| 2786 | #if defined(MBEDTLS_SHA256_C) |
| 2787 | case PSA_ALG_SHA_224: |
| 2788 | case PSA_ALG_SHA_256: |
| 2789 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2790 | break; |
| 2791 | #endif |
| 2792 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2793 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2794 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2795 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2796 | case PSA_ALG_SHA_512: |
| 2797 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2798 | break; |
| 2799 | #endif |
| 2800 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2801 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2802 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2803 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2804 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2805 | exit: |
| 2806 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2807 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2808 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2809 | return( psa_hash_abort( operation ) ); |
| 2810 | } |
| 2811 | else |
| 2812 | { |
| 2813 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2814 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2818 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2819 | const uint8_t *hash, |
| 2820 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2821 | { |
| 2822 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2823 | size_t actual_hash_length; |
| 2824 | psa_status_t status = psa_hash_finish( operation, |
| 2825 | actual_hash, sizeof( actual_hash ), |
| 2826 | &actual_hash_length ); |
| 2827 | if( status != PSA_SUCCESS ) |
| 2828 | return( status ); |
| 2829 | if( actual_hash_length != hash_length ) |
| 2830 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2831 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2832 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2833 | return( PSA_SUCCESS ); |
| 2834 | } |
| 2835 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2836 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2837 | const uint8_t *input, size_t input_length, |
| 2838 | uint8_t *hash, size_t hash_size, |
| 2839 | size_t *hash_length ) |
| 2840 | { |
| 2841 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2842 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2843 | |
| 2844 | *hash_length = hash_size; |
| 2845 | status = psa_hash_setup( &operation, alg ); |
| 2846 | if( status != PSA_SUCCESS ) |
| 2847 | goto exit; |
| 2848 | status = psa_hash_update( &operation, input, input_length ); |
| 2849 | if( status != PSA_SUCCESS ) |
| 2850 | goto exit; |
| 2851 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2852 | if( status != PSA_SUCCESS ) |
| 2853 | goto exit; |
| 2854 | |
| 2855 | exit: |
| 2856 | if( status == PSA_SUCCESS ) |
| 2857 | status = psa_hash_abort( &operation ); |
| 2858 | else |
| 2859 | psa_hash_abort( &operation ); |
| 2860 | return( status ); |
| 2861 | } |
| 2862 | |
| 2863 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2864 | const uint8_t *input, size_t input_length, |
| 2865 | const uint8_t *hash, 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 | status = psa_hash_setup( &operation, alg ); |
| 2871 | if( status != PSA_SUCCESS ) |
| 2872 | goto exit; |
| 2873 | status = psa_hash_update( &operation, input, input_length ); |
| 2874 | if( status != PSA_SUCCESS ) |
| 2875 | goto exit; |
| 2876 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2877 | if( status != PSA_SUCCESS ) |
| 2878 | goto exit; |
| 2879 | |
| 2880 | exit: |
| 2881 | if( status == PSA_SUCCESS ) |
| 2882 | status = psa_hash_abort( &operation ); |
| 2883 | else |
| 2884 | psa_hash_abort( &operation ); |
| 2885 | return( status ); |
| 2886 | } |
| 2887 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2888 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2889 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2890 | { |
| 2891 | if( target_operation->alg != 0 ) |
| 2892 | return( PSA_ERROR_BAD_STATE ); |
| 2893 | |
| 2894 | switch( source_operation->alg ) |
| 2895 | { |
| 2896 | case 0: |
| 2897 | return( PSA_ERROR_BAD_STATE ); |
| 2898 | #if defined(MBEDTLS_MD2_C) |
| 2899 | case PSA_ALG_MD2: |
| 2900 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2901 | &source_operation->ctx.md2 ); |
| 2902 | break; |
| 2903 | #endif |
| 2904 | #if defined(MBEDTLS_MD4_C) |
| 2905 | case PSA_ALG_MD4: |
| 2906 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2907 | &source_operation->ctx.md4 ); |
| 2908 | break; |
| 2909 | #endif |
| 2910 | #if defined(MBEDTLS_MD5_C) |
| 2911 | case PSA_ALG_MD5: |
| 2912 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2913 | &source_operation->ctx.md5 ); |
| 2914 | break; |
| 2915 | #endif |
| 2916 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2917 | case PSA_ALG_RIPEMD160: |
| 2918 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2919 | &source_operation->ctx.ripemd160 ); |
| 2920 | break; |
| 2921 | #endif |
| 2922 | #if defined(MBEDTLS_SHA1_C) |
| 2923 | case PSA_ALG_SHA_1: |
| 2924 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2925 | &source_operation->ctx.sha1 ); |
| 2926 | break; |
| 2927 | #endif |
| 2928 | #if defined(MBEDTLS_SHA256_C) |
| 2929 | case PSA_ALG_SHA_224: |
| 2930 | case PSA_ALG_SHA_256: |
| 2931 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2932 | &source_operation->ctx.sha256 ); |
| 2933 | break; |
| 2934 | #endif |
| 2935 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2936 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2937 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2938 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2939 | case PSA_ALG_SHA_512: |
| 2940 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2941 | &source_operation->ctx.sha512 ); |
| 2942 | break; |
| 2943 | #endif |
| 2944 | default: |
| 2945 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2946 | } |
| 2947 | |
| 2948 | target_operation->alg = source_operation->alg; |
| 2949 | return( PSA_SUCCESS ); |
| 2950 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2951 | |
| 2952 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2953 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2954 | /* MAC */ |
| 2955 | /****************************************************************/ |
| 2956 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2957 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2958 | psa_algorithm_t alg, |
| 2959 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2960 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2961 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2962 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2963 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2964 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2965 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2966 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2967 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2968 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2969 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2970 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2971 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2972 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2973 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2974 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2975 | mode = MBEDTLS_MODE_STREAM; |
| 2976 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2977 | case PSA_ALG_CTR: |
| 2978 | mode = MBEDTLS_MODE_CTR; |
| 2979 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2980 | case PSA_ALG_CFB: |
| 2981 | mode = MBEDTLS_MODE_CFB; |
| 2982 | break; |
| 2983 | case PSA_ALG_OFB: |
| 2984 | mode = MBEDTLS_MODE_OFB; |
| 2985 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2986 | case PSA_ALG_ECB_NO_PADDING: |
| 2987 | mode = MBEDTLS_MODE_ECB; |
| 2988 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2989 | case PSA_ALG_CBC_NO_PADDING: |
| 2990 | mode = MBEDTLS_MODE_CBC; |
| 2991 | break; |
| 2992 | case PSA_ALG_CBC_PKCS7: |
| 2993 | mode = MBEDTLS_MODE_CBC; |
| 2994 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2995 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2996 | mode = MBEDTLS_MODE_CCM; |
| 2997 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2998 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2999 | mode = MBEDTLS_MODE_GCM; |
| 3000 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3001 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3002 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 3003 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3004 | default: |
| 3005 | return( NULL ); |
| 3006 | } |
| 3007 | } |
| 3008 | else if( alg == PSA_ALG_CMAC ) |
| 3009 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3010 | else |
| 3011 | return( NULL ); |
| 3012 | |
| 3013 | switch( key_type ) |
| 3014 | { |
| 3015 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3016 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3017 | break; |
| 3018 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3019 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 3020 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3021 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3022 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3023 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3024 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3025 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 3026 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 3027 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 3028 | if( key_bits == 128 ) |
| 3029 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3030 | break; |
| 3031 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3032 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3033 | break; |
| 3034 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3035 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3036 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3037 | case PSA_KEY_TYPE_CHACHA20: |
| 3038 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 3039 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3040 | default: |
| 3041 | return( NULL ); |
| 3042 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3043 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3044 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3045 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3046 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 3047 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3048 | } |
| 3049 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3050 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3051 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3052 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3053 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3054 | { |
| 3055 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3056 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3057 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3058 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3059 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3060 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3061 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3062 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3063 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3064 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3065 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3066 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3067 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3068 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3069 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3070 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3071 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3072 | return( 128 ); |
| 3073 | default: |
| 3074 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3075 | } |
| 3076 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3077 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 3078 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3079 | /* Initialize the MAC operation structure. Once this function has been |
| 3080 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3081 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3082 | psa_algorithm_t alg ) |
| 3083 | { |
| 3084 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3085 | |
| 3086 | operation->alg = alg; |
| 3087 | operation->key_set = 0; |
| 3088 | operation->iv_set = 0; |
| 3089 | operation->iv_required = 0; |
| 3090 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3091 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3092 | |
| 3093 | #if defined(MBEDTLS_CMAC_C) |
| 3094 | if( alg == PSA_ALG_CMAC ) |
| 3095 | { |
| 3096 | operation->iv_required = 0; |
| 3097 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3098 | status = PSA_SUCCESS; |
| 3099 | } |
| 3100 | else |
| 3101 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3102 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3103 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3104 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3105 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3106 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3107 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3108 | } |
| 3109 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3110 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3111 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3112 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3113 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | if( status != PSA_SUCCESS ) |
| 3117 | memset( operation, 0, sizeof( *operation ) ); |
| 3118 | return( status ); |
| 3119 | } |
| 3120 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3121 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3122 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3123 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3124 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3125 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3126 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3127 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3128 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3129 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3130 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3131 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3132 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3133 | /* The object has (apparently) been initialized but it is not |
| 3134 | * in use. It's ok to call abort on such an object, and there's |
| 3135 | * nothing to do. */ |
| 3136 | return( PSA_SUCCESS ); |
| 3137 | } |
| 3138 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3139 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3140 | if( operation->alg == PSA_ALG_CMAC ) |
| 3141 | { |
| 3142 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3143 | } |
| 3144 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3145 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3146 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3147 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3148 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3149 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3150 | } |
| 3151 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3152 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3153 | { |
| 3154 | /* Sanity check (shouldn't happen: operation->alg should |
| 3155 | * always have been initialized to a valid value). */ |
| 3156 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3157 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3158 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3159 | operation->alg = 0; |
| 3160 | operation->key_set = 0; |
| 3161 | operation->iv_set = 0; |
| 3162 | operation->iv_required = 0; |
| 3163 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3164 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3165 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3166 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3167 | |
| 3168 | bad_state: |
| 3169 | /* If abort is called on an uninitialized object, we can't trust |
| 3170 | * anything. Wipe the object in case it contains confidential data. |
| 3171 | * This may result in a memory leak if a pointer gets overwritten, |
| 3172 | * but it's too late to do anything about this. */ |
| 3173 | memset( operation, 0, sizeof( *operation ) ); |
| 3174 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3177 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3178 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3179 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3180 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3181 | const mbedtls_cipher_info_t *cipher_info ) |
| 3182 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3183 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3184 | |
| 3185 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3186 | |
| 3187 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3188 | if( ret != 0 ) |
| 3189 | return( ret ); |
| 3190 | |
| 3191 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3192 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3193 | key_bits ); |
| 3194 | return( ret ); |
| 3195 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3196 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3197 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3198 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3199 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3200 | const uint8_t *key, |
| 3201 | size_t key_length, |
| 3202 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3203 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3204 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3205 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3206 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3207 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3208 | psa_status_t status; |
| 3209 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3210 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3211 | * overflow below. This should never trigger if the hash algorithm |
| 3212 | * is implemented correctly. */ |
| 3213 | /* The size checks against the ipad and opad buffers cannot be written |
| 3214 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3215 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3216 | if( block_size > sizeof( ipad ) ) |
| 3217 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3218 | if( block_size > sizeof( hmac->opad ) ) |
| 3219 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3220 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3221 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3222 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3223 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3224 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3225 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3226 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3227 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3228 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3229 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3230 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3231 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3232 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3233 | * an invalid pointer which would make the behavior undefined. */ |
| 3234 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3235 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3236 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3237 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3238 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3239 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3240 | ipad[i] ^= 0x36; |
| 3241 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3242 | |
| 3243 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3244 | * and filling the rest of opad with the requisite constant. */ |
| 3245 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3246 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3247 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3248 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3249 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3250 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3251 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3252 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3253 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3254 | |
| 3255 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3256 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3257 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3258 | return( status ); |
| 3259 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3260 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3261 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3262 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3263 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3264 | psa_algorithm_t alg, |
| 3265 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3266 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3267 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3268 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3269 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3270 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3271 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3272 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3273 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3274 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3275 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3276 | /* A context must be freshly initialized before it can be set up. */ |
| 3277 | if( operation->alg != 0 ) |
| 3278 | { |
| 3279 | return( PSA_ERROR_BAD_STATE ); |
| 3280 | } |
| 3281 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3282 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3283 | if( status != PSA_SUCCESS ) |
| 3284 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3285 | if( is_sign ) |
| 3286 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3287 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3288 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3289 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3290 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3291 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3292 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3294 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3295 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3296 | { |
| 3297 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3298 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3299 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3300 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3301 | if( cipher_info == NULL ) |
| 3302 | { |
| 3303 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3304 | goto exit; |
| 3305 | } |
| 3306 | operation->mac_size = cipher_info->block_size; |
| 3307 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3308 | status = mbedtls_to_psa_error( ret ); |
| 3309 | } |
| 3310 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3311 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3312 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3313 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3314 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3315 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3316 | if( hash_alg == 0 ) |
| 3317 | { |
| 3318 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3319 | goto exit; |
| 3320 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3321 | |
| 3322 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3323 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3324 | if( operation->mac_size == 0 || |
| 3325 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3326 | { |
| 3327 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3328 | goto exit; |
| 3329 | } |
| 3330 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3331 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3332 | { |
| 3333 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3334 | goto exit; |
| 3335 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3336 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3337 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3338 | slot->data.key.data, |
| 3339 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3340 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3341 | } |
| 3342 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3343 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3344 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3345 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3346 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3347 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3348 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3349 | if( truncated == 0 ) |
| 3350 | { |
| 3351 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3352 | } |
| 3353 | else if( truncated < 4 ) |
| 3354 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3355 | /* A very short MAC is too short for security since it can be |
| 3356 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3357 | * so we make this our minimum, even though 32 bits is still |
| 3358 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3359 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3360 | } |
| 3361 | else if( truncated > operation->mac_size ) |
| 3362 | { |
| 3363 | /* It's impossible to "truncate" to a larger length. */ |
| 3364 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3365 | } |
| 3366 | else |
| 3367 | operation->mac_size = truncated; |
| 3368 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3369 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3370 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3371 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3372 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3373 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3374 | else |
| 3375 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3376 | operation->key_set = 1; |
| 3377 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3378 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3379 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3380 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3381 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3382 | } |
| 3383 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3384 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3385 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3386 | psa_algorithm_t alg ) |
| 3387 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3388 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3392 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3393 | psa_algorithm_t alg ) |
| 3394 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3395 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3398 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3399 | const uint8_t *input, |
| 3400 | size_t input_length ) |
| 3401 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3402 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3403 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3404 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3405 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3406 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3407 | operation->has_input = 1; |
| 3408 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3409 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3410 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3411 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3412 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3413 | input, input_length ); |
| 3414 | status = mbedtls_to_psa_error( ret ); |
| 3415 | } |
| 3416 | else |
| 3417 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3418 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3419 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3420 | { |
| 3421 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3422 | input_length ); |
| 3423 | } |
| 3424 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3425 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3426 | { |
| 3427 | /* This shouldn't happen if `operation` was initialized by |
| 3428 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3429 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3430 | } |
| 3431 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3432 | if( status != PSA_SUCCESS ) |
| 3433 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3434 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3435 | } |
| 3436 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3437 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3438 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3439 | uint8_t *mac, |
| 3440 | size_t mac_size ) |
| 3441 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3442 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3443 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3444 | size_t hash_size = 0; |
| 3445 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3446 | psa_status_t status; |
| 3447 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3448 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3449 | if( status != PSA_SUCCESS ) |
| 3450 | return( status ); |
| 3451 | /* From here on, tmp needs to be wiped. */ |
| 3452 | |
| 3453 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3454 | if( status != PSA_SUCCESS ) |
| 3455 | goto exit; |
| 3456 | |
| 3457 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3458 | if( status != PSA_SUCCESS ) |
| 3459 | goto exit; |
| 3460 | |
| 3461 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3462 | if( status != PSA_SUCCESS ) |
| 3463 | goto exit; |
| 3464 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3465 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3466 | if( status != PSA_SUCCESS ) |
| 3467 | goto exit; |
| 3468 | |
| 3469 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3470 | |
| 3471 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3472 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3473 | return( status ); |
| 3474 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3475 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3476 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3477 | 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] | 3478 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3479 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3480 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3481 | if( ! operation->key_set ) |
| 3482 | return( PSA_ERROR_BAD_STATE ); |
| 3483 | if( operation->iv_required && ! operation->iv_set ) |
| 3484 | return( PSA_ERROR_BAD_STATE ); |
| 3485 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3486 | if( mac_size < operation->mac_size ) |
| 3487 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3488 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3489 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3490 | if( operation->alg == PSA_ALG_CMAC ) |
| 3491 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3492 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3493 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3494 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3495 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3496 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3497 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3498 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3499 | else |
| 3500 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3501 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3502 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3503 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3504 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3505 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3506 | } |
| 3507 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3508 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3509 | { |
| 3510 | /* This shouldn't happen if `operation` was initialized by |
| 3511 | * a setup function. */ |
| 3512 | return( PSA_ERROR_BAD_STATE ); |
| 3513 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3514 | } |
| 3515 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3516 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3517 | uint8_t *mac, |
| 3518 | size_t mac_size, |
| 3519 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3520 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3521 | psa_status_t status; |
| 3522 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3523 | if( operation->alg == 0 ) |
| 3524 | { |
| 3525 | return( PSA_ERROR_BAD_STATE ); |
| 3526 | } |
| 3527 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3528 | /* Fill the output buffer with something that isn't a valid mac |
| 3529 | * (barring an attack on the mac and deliberately-crafted input), |
| 3530 | * in case the caller doesn't check the return status properly. */ |
| 3531 | *mac_length = mac_size; |
| 3532 | /* If mac_size is 0 then mac may be NULL and then the |
| 3533 | * call to memset would have undefined behavior. */ |
| 3534 | if( mac_size != 0 ) |
| 3535 | memset( mac, '!', mac_size ); |
| 3536 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3537 | if( ! operation->is_sign ) |
| 3538 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3539 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3540 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3541 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3542 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3543 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3544 | if( status == PSA_SUCCESS ) |
| 3545 | { |
| 3546 | status = psa_mac_abort( operation ); |
| 3547 | if( status == PSA_SUCCESS ) |
| 3548 | *mac_length = operation->mac_size; |
| 3549 | else |
| 3550 | memset( mac, '!', mac_size ); |
| 3551 | } |
| 3552 | else |
| 3553 | psa_mac_abort( operation ); |
| 3554 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3557 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3558 | const uint8_t *mac, |
| 3559 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3560 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3561 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3562 | psa_status_t status; |
| 3563 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3564 | if( operation->alg == 0 ) |
| 3565 | { |
| 3566 | return( PSA_ERROR_BAD_STATE ); |
| 3567 | } |
| 3568 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3569 | if( operation->is_sign ) |
| 3570 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3571 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3572 | } |
| 3573 | if( operation->mac_size != mac_length ) |
| 3574 | { |
| 3575 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3576 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3577 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3578 | |
| 3579 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3580 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3581 | if( status != PSA_SUCCESS ) |
| 3582 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3583 | |
| 3584 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3585 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3586 | |
| 3587 | cleanup: |
| 3588 | if( status == PSA_SUCCESS ) |
| 3589 | status = psa_mac_abort( operation ); |
| 3590 | else |
| 3591 | psa_mac_abort( operation ); |
| 3592 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3593 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3594 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3595 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3599 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3600 | /****************************************************************/ |
| 3601 | /* Asymmetric cryptography */ |
| 3602 | /****************************************************************/ |
| 3603 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3604 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3605 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3606 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3607 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3608 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3609 | size_t hash_length, |
| 3610 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3611 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3612 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3613 | 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] | 3614 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3615 | |
| 3616 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3617 | * parameters. Validate that it fits so that we don't risk an |
| 3618 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3619 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3620 | if( hash_length > UINT_MAX ) |
| 3621 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3622 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3623 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3624 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3625 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3626 | * must be correct. */ |
| 3627 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3628 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3629 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3630 | if( md_info == NULL ) |
| 3631 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3632 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3633 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3634 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3635 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3636 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3637 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3638 | /* PSS requires a hash internally. */ |
| 3639 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3640 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3641 | if( md_info == NULL ) |
| 3642 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3643 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3644 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3645 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3646 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3647 | } |
| 3648 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3649 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3650 | psa_algorithm_t alg, |
| 3651 | const uint8_t *hash, |
| 3652 | size_t hash_length, |
| 3653 | uint8_t *signature, |
| 3654 | size_t signature_size, |
| 3655 | size_t *signature_length ) |
| 3656 | { |
| 3657 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3658 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3659 | mbedtls_md_type_t md_alg; |
| 3660 | |
| 3661 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3662 | if( status != PSA_SUCCESS ) |
| 3663 | return( status ); |
| 3664 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3665 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3666 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3667 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3668 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3669 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3670 | { |
| 3671 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3672 | MBEDTLS_MD_NONE ); |
| 3673 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3674 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3675 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3676 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3677 | md_alg, |
| 3678 | (unsigned int) hash_length, |
| 3679 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3680 | signature ); |
| 3681 | } |
| 3682 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3683 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3684 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3685 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3686 | { |
| 3687 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3688 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3689 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3690 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3691 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3692 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3693 | (unsigned int) hash_length, |
| 3694 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3695 | signature ); |
| 3696 | } |
| 3697 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3698 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3699 | { |
| 3700 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3701 | } |
| 3702 | |
| 3703 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3704 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3705 | return( mbedtls_to_psa_error( ret ) ); |
| 3706 | } |
| 3707 | |
| 3708 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3709 | psa_algorithm_t alg, |
| 3710 | const uint8_t *hash, |
| 3711 | size_t hash_length, |
| 3712 | const uint8_t *signature, |
| 3713 | size_t signature_length ) |
| 3714 | { |
| 3715 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3716 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3717 | mbedtls_md_type_t md_alg; |
| 3718 | |
| 3719 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3720 | if( status != PSA_SUCCESS ) |
| 3721 | return( status ); |
| 3722 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3723 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3724 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3725 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3726 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3727 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3728 | { |
| 3729 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3730 | MBEDTLS_MD_NONE ); |
| 3731 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3732 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3733 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3734 | MBEDTLS_RSA_PUBLIC, |
| 3735 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3736 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3737 | hash, |
| 3738 | signature ); |
| 3739 | } |
| 3740 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3741 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3742 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3743 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3744 | { |
| 3745 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3746 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3747 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3748 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3749 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3750 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3751 | (unsigned int) hash_length, |
| 3752 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3753 | signature ); |
| 3754 | } |
| 3755 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3756 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3757 | { |
| 3758 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3759 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3760 | |
| 3761 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3762 | * the rest of the signature is invalid". This has little use in |
| 3763 | * practice and PSA doesn't report this distinction. */ |
| 3764 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3765 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3766 | return( mbedtls_to_psa_error( ret ) ); |
| 3767 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3768 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3769 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3770 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3771 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3772 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3773 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3774 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3775 | * (even though these functions don't modify it). */ |
| 3776 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3777 | psa_algorithm_t alg, |
| 3778 | const uint8_t *hash, |
| 3779 | size_t hash_length, |
| 3780 | uint8_t *signature, |
| 3781 | size_t signature_size, |
| 3782 | size_t *signature_length ) |
| 3783 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3784 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3785 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3786 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3787 | mbedtls_mpi_init( &r ); |
| 3788 | mbedtls_mpi_init( &s ); |
| 3789 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3790 | if( signature_size < 2 * curve_bytes ) |
| 3791 | { |
| 3792 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3793 | goto cleanup; |
| 3794 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3795 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3796 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3797 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3798 | { |
| 3799 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3800 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3801 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3802 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3803 | &ecp->d, hash, |
| 3804 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3805 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3806 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3807 | } |
| 3808 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3809 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3810 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3811 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3812 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3813 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3814 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3815 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3816 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3817 | |
| 3818 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3819 | signature, |
| 3820 | curve_bytes ) ); |
| 3821 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3822 | signature + curve_bytes, |
| 3823 | curve_bytes ) ); |
| 3824 | |
| 3825 | cleanup: |
| 3826 | mbedtls_mpi_free( &r ); |
| 3827 | mbedtls_mpi_free( &s ); |
| 3828 | if( ret == 0 ) |
| 3829 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3830 | return( mbedtls_to_psa_error( ret ) ); |
| 3831 | } |
| 3832 | |
| 3833 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3834 | const uint8_t *hash, |
| 3835 | size_t hash_length, |
| 3836 | const uint8_t *signature, |
| 3837 | size_t signature_length ) |
| 3838 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3839 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3840 | mbedtls_mpi r, s; |
| 3841 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3842 | mbedtls_mpi_init( &r ); |
| 3843 | mbedtls_mpi_init( &s ); |
| 3844 | |
| 3845 | if( signature_length != 2 * curve_bytes ) |
| 3846 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3847 | |
| 3848 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3849 | signature, |
| 3850 | curve_bytes ) ); |
| 3851 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3852 | signature + curve_bytes, |
| 3853 | curve_bytes ) ); |
| 3854 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3855 | /* Check whether the public part is loaded. If not, load it. */ |
| 3856 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3857 | { |
| 3858 | MBEDTLS_MPI_CHK( |
| 3859 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3860 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3861 | } |
| 3862 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3863 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3864 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3865 | |
| 3866 | cleanup: |
| 3867 | mbedtls_mpi_free( &r ); |
| 3868 | mbedtls_mpi_free( &s ); |
| 3869 | return( mbedtls_to_psa_error( ret ) ); |
| 3870 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3871 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3872 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3873 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3874 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3875 | psa_algorithm_t alg, |
| 3876 | const uint8_t *hash, |
| 3877 | size_t hash_length, |
| 3878 | uint8_t *signature, |
| 3879 | size_t signature_size, |
| 3880 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3881 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3882 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3883 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3884 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3885 | |
| 3886 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3887 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3888 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3889 | * buffer can in principle be empty since it doesn't actually have |
| 3890 | * to be a hash.) */ |
| 3891 | if( signature_size == 0 ) |
| 3892 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3893 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3894 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3895 | PSA_KEY_USAGE_SIGN_HASH, |
| 3896 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3897 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3898 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3899 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3900 | { |
| 3901 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3902 | goto exit; |
| 3903 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3904 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3905 | /* Try any of the available accelerators first */ |
| 3906 | status = psa_driver_wrapper_sign_hash( slot, |
| 3907 | alg, |
| 3908 | hash, |
| 3909 | hash_length, |
| 3910 | signature, |
| 3911 | signature_size, |
| 3912 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3913 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3914 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3915 | goto exit; |
| 3916 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3917 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3918 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3919 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3920 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3921 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3922 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3923 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3924 | status = psa_load_rsa_representation( slot->attr.type, |
| 3925 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3926 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3927 | &rsa ); |
| 3928 | if( status != PSA_SUCCESS ) |
| 3929 | goto exit; |
| 3930 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3931 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3932 | alg, |
| 3933 | hash, hash_length, |
| 3934 | signature, signature_size, |
| 3935 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3936 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3937 | mbedtls_rsa_free( rsa ); |
| 3938 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3939 | } |
| 3940 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3941 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3942 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3943 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3944 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3945 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3946 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3947 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3948 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3949 | PSA_ALG_IS_ECDSA( alg ) |
| 3950 | #else |
| 3951 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3952 | #endif |
| 3953 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3954 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3955 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3956 | status = psa_load_ecp_representation( slot->attr.type, |
| 3957 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3958 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3959 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3960 | if( status != PSA_SUCCESS ) |
| 3961 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3962 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3963 | alg, |
| 3964 | hash, hash_length, |
| 3965 | signature, signature_size, |
| 3966 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3967 | mbedtls_ecp_keypair_free( ecp ); |
| 3968 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3969 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3970 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3971 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3972 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3973 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3974 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3975 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3976 | } |
| 3977 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3978 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3979 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3980 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3981 | |
| 3982 | exit: |
| 3983 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3984 | * the trailing part on success) with something that isn't a valid mac |
| 3985 | * (barring an attack on the mac and deliberately-crafted input), |
| 3986 | * in case the caller doesn't check the return status properly. */ |
| 3987 | if( status == PSA_SUCCESS ) |
| 3988 | memset( signature + *signature_length, '!', |
| 3989 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3990 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3991 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3992 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3993 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3994 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3995 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3996 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3997 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3998 | } |
| 3999 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4000 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4001 | psa_algorithm_t alg, |
| 4002 | const uint8_t *hash, |
| 4003 | size_t hash_length, |
| 4004 | const uint8_t *signature, |
| 4005 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4006 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4007 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4008 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4009 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 4010 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4011 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 4012 | PSA_KEY_USAGE_VERIFY_HASH, |
| 4013 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4014 | if( status != PSA_SUCCESS ) |
| 4015 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4016 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4017 | /* Try any of the available accelerators first */ |
| 4018 | status = psa_driver_wrapper_verify_hash( slot, |
| 4019 | alg, |
| 4020 | hash, |
| 4021 | hash_length, |
| 4022 | signature, |
| 4023 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 4024 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4025 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4026 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4027 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4028 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 4029 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4030 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4031 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4032 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4033 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4034 | status = psa_load_rsa_representation( slot->attr.type, |
| 4035 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4036 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4037 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4038 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4039 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4040 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4041 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4042 | alg, |
| 4043 | hash, hash_length, |
| 4044 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4045 | mbedtls_rsa_free( rsa ); |
| 4046 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4047 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4048 | } |
| 4049 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4050 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4051 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4052 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4053 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4054 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4055 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4056 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4057 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4058 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4059 | status = psa_load_ecp_representation( slot->attr.type, |
| 4060 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4061 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4062 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4063 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4064 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4065 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4066 | hash, hash_length, |
| 4067 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4068 | mbedtls_ecp_keypair_free( ecp ); |
| 4069 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4070 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4071 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4072 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4073 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4074 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4075 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4076 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4077 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4078 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4079 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4080 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4081 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4082 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4083 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4084 | |
| 4085 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4086 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4087 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4088 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4089 | } |
| 4090 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4091 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4092 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4093 | mbedtls_rsa_context *rsa ) |
| 4094 | { |
| 4095 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4096 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4097 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4098 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4099 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4100 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4101 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4102 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4103 | psa_algorithm_t alg, |
| 4104 | const uint8_t *input, |
| 4105 | size_t input_length, |
| 4106 | const uint8_t *salt, |
| 4107 | size_t salt_length, |
| 4108 | uint8_t *output, |
| 4109 | size_t output_size, |
| 4110 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4111 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4112 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4113 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4114 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4115 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4116 | (void) input; |
| 4117 | (void) input_length; |
| 4118 | (void) salt; |
| 4119 | (void) output; |
| 4120 | (void) output_size; |
| 4121 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4122 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4123 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4124 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4125 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4126 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4127 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4128 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4129 | if( status != PSA_SUCCESS ) |
| 4130 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4131 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4132 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4133 | { |
| 4134 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4135 | goto exit; |
| 4136 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4137 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4138 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4139 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4140 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4141 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4142 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4143 | status = psa_load_rsa_representation( slot->attr.type, |
| 4144 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4145 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4146 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4147 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4148 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4149 | |
| 4150 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4151 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4152 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4153 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4154 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4155 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4156 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4157 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4158 | status = mbedtls_to_psa_error( |
| 4159 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4160 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4161 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4162 | MBEDTLS_RSA_PUBLIC, |
| 4163 | input_length, |
| 4164 | input, |
| 4165 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4166 | } |
| 4167 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4168 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4169 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4170 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4171 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4172 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4173 | status = mbedtls_to_psa_error( |
| 4174 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4175 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4176 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4177 | MBEDTLS_RSA_PUBLIC, |
| 4178 | salt, salt_length, |
| 4179 | input_length, |
| 4180 | input, |
| 4181 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4182 | } |
| 4183 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4184 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4185 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4186 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4187 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4188 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4189 | rsa_exit: |
| 4190 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4191 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4192 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4193 | mbedtls_rsa_free( rsa ); |
| 4194 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4195 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4196 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4197 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4198 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4199 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4200 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4201 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4202 | |
| 4203 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4204 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4205 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4206 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4207 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4208 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4209 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4210 | psa_algorithm_t alg, |
| 4211 | const uint8_t *input, |
| 4212 | size_t input_length, |
| 4213 | const uint8_t *salt, |
| 4214 | size_t salt_length, |
| 4215 | uint8_t *output, |
| 4216 | size_t output_size, |
| 4217 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4218 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4219 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4220 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4221 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4222 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4223 | (void) input; |
| 4224 | (void) input_length; |
| 4225 | (void) salt; |
| 4226 | (void) output; |
| 4227 | (void) output_size; |
| 4228 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4229 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4230 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4231 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4233 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4234 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4235 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4236 | if( status != PSA_SUCCESS ) |
| 4237 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4238 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4239 | { |
| 4240 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4241 | goto exit; |
| 4242 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4243 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4244 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4245 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4246 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4247 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4248 | mbedtls_rsa_context *rsa = NULL; |
| 4249 | status = psa_load_rsa_representation( slot->attr.type, |
| 4250 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4251 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4252 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4253 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4254 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4255 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4256 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4257 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4258 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4259 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4260 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4261 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4262 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4263 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4264 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4265 | status = mbedtls_to_psa_error( |
| 4266 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4267 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4268 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4269 | MBEDTLS_RSA_PRIVATE, |
| 4270 | output_length, |
| 4271 | input, |
| 4272 | output, |
| 4273 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4274 | } |
| 4275 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4276 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4277 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4278 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4279 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4280 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4281 | status = mbedtls_to_psa_error( |
| 4282 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4283 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4284 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4285 | MBEDTLS_RSA_PRIVATE, |
| 4286 | salt, salt_length, |
| 4287 | output_length, |
| 4288 | input, |
| 4289 | output, |
| 4290 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4291 | } |
| 4292 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4293 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4294 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4295 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4296 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4297 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4298 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4299 | mbedtls_rsa_free( rsa ); |
| 4300 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4301 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4302 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4303 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4304 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4305 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4306 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4307 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4308 | |
| 4309 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4310 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4311 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4312 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4313 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4314 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4315 | |
| 4316 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4317 | /****************************************************************/ |
| 4318 | /* Symmetric cryptography */ |
| 4319 | /****************************************************************/ |
| 4320 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4321 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4322 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4323 | psa_algorithm_t alg, |
| 4324 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4325 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4326 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4327 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4328 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4329 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4330 | size_t key_bits; |
| 4331 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4332 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4333 | PSA_KEY_USAGE_ENCRYPT : |
| 4334 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4335 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4336 | /* A context must be freshly initialized before it can be set up. */ |
| 4337 | if( operation->alg != 0 ) |
| 4338 | return( PSA_ERROR_BAD_STATE ); |
| 4339 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4340 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4341 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4342 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4343 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4344 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4345 | 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] | 4346 | if( status != PSA_SUCCESS ) |
| 4347 | goto exit; |
| 4348 | |
| 4349 | /* Initialize the operation struct members, except for alg. The alg member |
| 4350 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4351 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4352 | operation->key_set = 0; |
| 4353 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4354 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4355 | operation->iv_size = 0; |
| 4356 | operation->block_size = 0; |
| 4357 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4358 | operation->iv_required = 0; |
| 4359 | else |
| 4360 | operation->iv_required = 1; |
| 4361 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4362 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4363 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4364 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4365 | slot, |
| 4366 | alg ); |
| 4367 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4368 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4369 | slot, |
| 4370 | alg ); |
| 4371 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4372 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4373 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4374 | /* Once the driver context is initialised, it needs to be freed using |
| 4375 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4376 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4377 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4378 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4379 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4380 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4381 | goto exit; |
| 4382 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4383 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4384 | * available for the given algorithm & key. */ |
| 4385 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4386 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4387 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4388 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4389 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4390 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4391 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4392 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4393 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4394 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4395 | 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] | 4396 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4397 | { |
| 4398 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4399 | goto exit; |
| 4400 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4401 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4402 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4403 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4404 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4405 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4406 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4407 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4408 | { |
| 4409 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4410 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4411 | memcpy( keys, slot->data.key.data, 16 ); |
| 4412 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4413 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4414 | keys, |
| 4415 | 192, cipher_operation ); |
| 4416 | } |
| 4417 | else |
| 4418 | #endif |
| 4419 | { |
| 4420 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4421 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4422 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4423 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4424 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4425 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4426 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4427 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4428 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4429 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4430 | case PSA_ALG_CBC_NO_PADDING: |
| 4431 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4432 | MBEDTLS_PADDING_NONE ); |
| 4433 | break; |
| 4434 | case PSA_ALG_CBC_PKCS7: |
| 4435 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4436 | MBEDTLS_PADDING_PKCS7 ); |
| 4437 | break; |
| 4438 | default: |
| 4439 | /* The algorithm doesn't involve padding. */ |
| 4440 | ret = 0; |
| 4441 | break; |
| 4442 | } |
| 4443 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4444 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4445 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4446 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4447 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4448 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4449 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4450 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4451 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4452 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4453 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4454 | #if defined(MBEDTLS_CHACHA20_C) |
| 4455 | else |
| 4456 | if( alg == PSA_ALG_CHACHA20 ) |
| 4457 | operation->iv_size = 12; |
| 4458 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4459 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4460 | status = PSA_SUCCESS; |
| 4461 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4462 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4463 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4464 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4465 | if( status == PSA_SUCCESS ) |
| 4466 | { |
| 4467 | /* Update operation flags for both driver and software implementations */ |
| 4468 | operation->key_set = 1; |
| 4469 | } |
| 4470 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4471 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4472 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4473 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4474 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4475 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4476 | } |
| 4477 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4478 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4479 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4480 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4481 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4482 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4483 | } |
| 4484 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4485 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4486 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4487 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4488 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4489 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4490 | } |
| 4491 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4492 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4493 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4494 | size_t iv_size, |
| 4495 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4496 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4497 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4498 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4499 | if( operation->iv_set || ! operation->iv_required ) |
| 4500 | { |
| 4501 | return( PSA_ERROR_BAD_STATE ); |
| 4502 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4503 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4504 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4505 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4506 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4507 | iv, |
| 4508 | iv_size, |
| 4509 | iv_length ); |
| 4510 | goto exit; |
| 4511 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4512 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4513 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4514 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4515 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4516 | goto exit; |
| 4517 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4518 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4519 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4520 | if( ret != 0 ) |
| 4521 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4522 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4523 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4524 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4525 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4526 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4527 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4528 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4529 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4530 | if( status == PSA_SUCCESS ) |
| 4531 | operation->iv_set = 1; |
| 4532 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4533 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4534 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4535 | } |
| 4536 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4537 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4538 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4539 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4540 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4541 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4542 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4543 | if( operation->iv_set || ! operation->iv_required ) |
| 4544 | { |
| 4545 | return( PSA_ERROR_BAD_STATE ); |
| 4546 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4547 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4548 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4549 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4550 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4551 | iv, |
| 4552 | iv_length ); |
| 4553 | goto exit; |
| 4554 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4555 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4556 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4557 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4558 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4559 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4560 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4561 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4562 | status = mbedtls_to_psa_error( ret ); |
| 4563 | exit: |
| 4564 | if( status == PSA_SUCCESS ) |
| 4565 | operation->iv_set = 1; |
| 4566 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4567 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4568 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4569 | } |
| 4570 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4571 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4572 | * manual processing, since the PSA API is defined as being able to process |
| 4573 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4574 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4575 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4576 | mbedtls_cipher_context_t *ctx, |
| 4577 | const uint8_t *input, |
| 4578 | size_t input_length, |
| 4579 | uint8_t *output, |
| 4580 | size_t output_size, |
| 4581 | size_t *output_length ) |
| 4582 | { |
| 4583 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4584 | size_t block_size = ctx->cipher_info->block_size; |
| 4585 | size_t internal_output_length = 0; |
| 4586 | *output_length = 0; |
| 4587 | |
| 4588 | if( input_length == 0 ) |
| 4589 | { |
| 4590 | status = PSA_SUCCESS; |
| 4591 | goto exit; |
| 4592 | } |
| 4593 | |
| 4594 | if( ctx->unprocessed_len > 0 ) |
| 4595 | { |
| 4596 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4597 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4598 | |
| 4599 | if( input_length < bytes_to_copy ) |
| 4600 | bytes_to_copy = input_length; |
| 4601 | |
| 4602 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4603 | input, bytes_to_copy ); |
| 4604 | input_length -= bytes_to_copy; |
| 4605 | input += bytes_to_copy; |
| 4606 | ctx->unprocessed_len += bytes_to_copy; |
| 4607 | |
| 4608 | if( ctx->unprocessed_len == block_size ) |
| 4609 | { |
| 4610 | status = mbedtls_to_psa_error( |
| 4611 | mbedtls_cipher_update( ctx, |
| 4612 | ctx->unprocessed_data, |
| 4613 | block_size, |
| 4614 | output, &internal_output_length ) ); |
| 4615 | |
| 4616 | if( status != PSA_SUCCESS ) |
| 4617 | goto exit; |
| 4618 | |
| 4619 | output += internal_output_length; |
| 4620 | output_size -= internal_output_length; |
| 4621 | *output_length += internal_output_length; |
| 4622 | ctx->unprocessed_len = 0; |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | while( input_length >= block_size ) |
| 4627 | { |
| 4628 | /* Run all full blocks we have, one by one */ |
| 4629 | status = mbedtls_to_psa_error( |
| 4630 | mbedtls_cipher_update( ctx, input, |
| 4631 | block_size, |
| 4632 | output, &internal_output_length ) ); |
| 4633 | |
| 4634 | if( status != PSA_SUCCESS ) |
| 4635 | goto exit; |
| 4636 | |
| 4637 | input_length -= block_size; |
| 4638 | input += block_size; |
| 4639 | |
| 4640 | output += internal_output_length; |
| 4641 | output_size -= internal_output_length; |
| 4642 | *output_length += internal_output_length; |
| 4643 | } |
| 4644 | |
| 4645 | if( input_length > 0 ) |
| 4646 | { |
| 4647 | /* Save unprocessed bytes for later processing */ |
| 4648 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4649 | input, input_length ); |
| 4650 | ctx->unprocessed_len += input_length; |
| 4651 | } |
| 4652 | |
| 4653 | status = PSA_SUCCESS; |
| 4654 | |
| 4655 | exit: |
| 4656 | return( status ); |
| 4657 | } |
| 4658 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4659 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4660 | const uint8_t *input, |
| 4661 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4662 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4663 | size_t output_size, |
| 4664 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4665 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4666 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4667 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4668 | if( operation->alg == 0 ) |
| 4669 | { |
| 4670 | return( PSA_ERROR_BAD_STATE ); |
| 4671 | } |
| 4672 | if( operation->iv_required && ! operation->iv_set ) |
| 4673 | { |
| 4674 | return( PSA_ERROR_BAD_STATE ); |
| 4675 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4676 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4677 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4678 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4679 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4680 | input, |
| 4681 | input_length, |
| 4682 | output, |
| 4683 | output_size, |
| 4684 | output_length ); |
| 4685 | goto exit; |
| 4686 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4687 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4688 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4689 | { |
| 4690 | /* Take the unprocessed partial block left over from previous |
| 4691 | * update calls, if any, plus the input to this call. Remove |
| 4692 | * the last partial block, if any. You get the data that will be |
| 4693 | * output in this call. */ |
| 4694 | expected_output_size = |
| 4695 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4696 | / operation->block_size * operation->block_size; |
| 4697 | } |
| 4698 | else |
| 4699 | { |
| 4700 | expected_output_size = input_length; |
| 4701 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4702 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4703 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4704 | { |
| 4705 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4706 | goto exit; |
| 4707 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4708 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4709 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4710 | { |
| 4711 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4712 | * process a single block at a time in ECB mode. Abstract away that |
| 4713 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4714 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4715 | input, |
| 4716 | input_length, |
| 4717 | output, |
| 4718 | output_size, |
| 4719 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4720 | } |
| 4721 | else |
| 4722 | { |
| 4723 | status = mbedtls_to_psa_error( |
| 4724 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4725 | input_length, output, output_length ) ); |
| 4726 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4727 | exit: |
| 4728 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4729 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4730 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4731 | } |
| 4732 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4733 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4734 | uint8_t *output, |
| 4735 | size_t output_size, |
| 4736 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4737 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4738 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4739 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4740 | if( operation->alg == 0 ) |
| 4741 | { |
| 4742 | return( PSA_ERROR_BAD_STATE ); |
| 4743 | } |
| 4744 | if( operation->iv_required && ! operation->iv_set ) |
| 4745 | { |
| 4746 | return( PSA_ERROR_BAD_STATE ); |
| 4747 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4748 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4749 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4750 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4751 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4752 | output, |
| 4753 | output_size, |
| 4754 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4755 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4756 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4757 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4758 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4759 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4760 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4761 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4762 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4763 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4764 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4765 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4766 | } |
| 4767 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4768 | status = mbedtls_to_psa_error( |
| 4769 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4770 | temp_output_buffer, |
| 4771 | output_length ) ); |
| 4772 | if( status != PSA_SUCCESS ) |
| 4773 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4774 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4775 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4776 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4777 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4778 | memcpy( output, temp_output_buffer, *output_length ); |
| 4779 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4780 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4781 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4782 | exit: |
| 4783 | if( operation->mbedtls_in_use == 1 ) |
| 4784 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4785 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4786 | if( status == PSA_SUCCESS ) |
| 4787 | return( psa_cipher_abort( operation ) ); |
| 4788 | else |
| 4789 | { |
| 4790 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4791 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4792 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4793 | return( status ); |
| 4794 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4795 | } |
| 4796 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4797 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4798 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4799 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4800 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4801 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4802 | * in use. It's ok to call abort on such an object, and there's |
| 4803 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4804 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4805 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4806 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4807 | /* Sanity check (shouldn't happen: operation->alg should |
| 4808 | * always have been initialized to a valid value). */ |
| 4809 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4810 | return( PSA_ERROR_BAD_STATE ); |
| 4811 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4812 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4813 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4814 | else |
| 4815 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4816 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4817 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4818 | operation->key_set = 0; |
| 4819 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4820 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4821 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4822 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4823 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4824 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4825 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4828 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4829 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4830 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4831 | /****************************************************************/ |
| 4832 | /* AEAD */ |
| 4833 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4835 | typedef struct |
| 4836 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4837 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4838 | const mbedtls_cipher_info_t *cipher_info; |
| 4839 | union |
| 4840 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4841 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4842 | #if defined(MBEDTLS_CCM_C) |
| 4843 | mbedtls_ccm_context ccm; |
| 4844 | #endif /* MBEDTLS_CCM_C */ |
| 4845 | #if defined(MBEDTLS_GCM_C) |
| 4846 | mbedtls_gcm_context gcm; |
| 4847 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4848 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4849 | mbedtls_chachapoly_context chachapoly; |
| 4850 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4851 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4852 | psa_algorithm_t core_alg; |
| 4853 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4854 | uint8_t tag_length; |
| 4855 | } aead_operation_t; |
| 4856 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4857 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4858 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4859 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4860 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4861 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4862 | { |
| 4863 | #if defined(MBEDTLS_CCM_C) |
| 4864 | case PSA_ALG_CCM: |
| 4865 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4866 | break; |
| 4867 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4868 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4869 | case PSA_ALG_GCM: |
| 4870 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4871 | break; |
| 4872 | #endif /* MBEDTLS_GCM_C */ |
| 4873 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4874 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4875 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4879 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4880 | psa_key_usage_t usage, |
| 4881 | psa_algorithm_t alg ) |
| 4882 | { |
| 4883 | psa_status_t status; |
| 4884 | size_t key_bits; |
| 4885 | mbedtls_cipher_id_t cipher_id; |
| 4886 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4887 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4888 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4889 | if( status != PSA_SUCCESS ) |
| 4890 | return( status ); |
| 4891 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4892 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4893 | |
| 4894 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4895 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4896 | &cipher_id ); |
| 4897 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4898 | { |
| 4899 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4900 | goto cleanup; |
| 4901 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4902 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4903 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4904 | { |
| 4905 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4906 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4907 | operation->core_alg = PSA_ALG_CCM; |
| 4908 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4909 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4910 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4911 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4912 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4913 | { |
| 4914 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4915 | goto cleanup; |
| 4916 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4917 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4918 | status = mbedtls_to_psa_error( |
| 4919 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4920 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4921 | (unsigned int) key_bits ) ); |
| 4922 | if( status != 0 ) |
| 4923 | goto cleanup; |
| 4924 | break; |
| 4925 | #endif /* MBEDTLS_CCM_C */ |
| 4926 | |
| 4927 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4928 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4929 | operation->core_alg = PSA_ALG_GCM; |
| 4930 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4931 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4932 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4933 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4934 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4935 | { |
| 4936 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4937 | goto cleanup; |
| 4938 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4939 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4940 | status = mbedtls_to_psa_error( |
| 4941 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4942 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4943 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4944 | if( status != 0 ) |
| 4945 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4946 | break; |
| 4947 | #endif /* MBEDTLS_GCM_C */ |
| 4948 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4949 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4950 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4951 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4952 | operation->full_tag_length = 16; |
| 4953 | /* We only support the default tag length. */ |
| 4954 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4955 | { |
| 4956 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4957 | goto cleanup; |
| 4958 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4959 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4960 | status = mbedtls_to_psa_error( |
| 4961 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4962 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4963 | if( status != 0 ) |
| 4964 | goto cleanup; |
| 4965 | break; |
| 4966 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4967 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4968 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4969 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4970 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4971 | } |
| 4972 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4973 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4974 | { |
| 4975 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4976 | goto cleanup; |
| 4977 | } |
| 4978 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4979 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4980 | return( PSA_SUCCESS ); |
| 4981 | |
| 4982 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4983 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4984 | return( status ); |
| 4985 | } |
| 4986 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4987 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4988 | psa_algorithm_t alg, |
| 4989 | const uint8_t *nonce, |
| 4990 | size_t nonce_length, |
| 4991 | const uint8_t *additional_data, |
| 4992 | size_t additional_data_length, |
| 4993 | const uint8_t *plaintext, |
| 4994 | size_t plaintext_length, |
| 4995 | uint8_t *ciphertext, |
| 4996 | size_t ciphertext_size, |
| 4997 | size_t *ciphertext_length ) |
| 4998 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4999 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5000 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 5001 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5002 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 5003 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 5004 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5005 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5006 | if( status != PSA_SUCCESS ) |
| 5007 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5008 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5009 | /* For all currently supported modes, the tag is at the end of the |
| 5010 | * ciphertext. */ |
| 5011 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 5012 | { |
| 5013 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5014 | goto exit; |
| 5015 | } |
| 5016 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5017 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5018 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5019 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5020 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5021 | status = mbedtls_to_psa_error( |
| 5022 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 5023 | MBEDTLS_GCM_ENCRYPT, |
| 5024 | plaintext_length, |
| 5025 | nonce, nonce_length, |
| 5026 | additional_data, additional_data_length, |
| 5027 | plaintext, ciphertext, |
| 5028 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5029 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5030 | else |
| 5031 | #endif /* MBEDTLS_GCM_C */ |
| 5032 | #if defined(MBEDTLS_CCM_C) |
| 5033 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5034 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5035 | status = mbedtls_to_psa_error( |
| 5036 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 5037 | plaintext_length, |
| 5038 | nonce, nonce_length, |
| 5039 | additional_data, |
| 5040 | additional_data_length, |
| 5041 | plaintext, ciphertext, |
| 5042 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5043 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5044 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5045 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5046 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5047 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5048 | { |
| 5049 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5050 | { |
| 5051 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5052 | goto exit; |
| 5053 | } |
| 5054 | status = mbedtls_to_psa_error( |
| 5055 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 5056 | plaintext_length, |
| 5057 | nonce, |
| 5058 | additional_data, |
| 5059 | additional_data_length, |
| 5060 | plaintext, |
| 5061 | ciphertext, |
| 5062 | tag ) ); |
| 5063 | } |
| 5064 | else |
| 5065 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5066 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5067 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5068 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5069 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5070 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 5071 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5072 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5073 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5074 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5075 | if( status == PSA_SUCCESS ) |
| 5076 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 5077 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5078 | } |
| 5079 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5080 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 5081 | * followed by the tag. Return the length of the part preceding the tag in |
| 5082 | * *plaintext_length. This is the size of the plaintext in modes where |
| 5083 | * the encrypted data has the same size as the plaintext, such as |
| 5084 | * CCM and GCM. */ |
| 5085 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 5086 | const uint8_t *ciphertext, |
| 5087 | size_t ciphertext_length, |
| 5088 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5089 | const uint8_t **p_tag ) |
| 5090 | { |
| 5091 | size_t payload_length; |
| 5092 | if( tag_length > ciphertext_length ) |
| 5093 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5094 | payload_length = ciphertext_length - tag_length; |
| 5095 | if( payload_length > plaintext_size ) |
| 5096 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5097 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5098 | return( PSA_SUCCESS ); |
| 5099 | } |
| 5100 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5101 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5102 | psa_algorithm_t alg, |
| 5103 | const uint8_t *nonce, |
| 5104 | size_t nonce_length, |
| 5105 | const uint8_t *additional_data, |
| 5106 | size_t additional_data_length, |
| 5107 | const uint8_t *ciphertext, |
| 5108 | size_t ciphertext_length, |
| 5109 | uint8_t *plaintext, |
| 5110 | size_t plaintext_size, |
| 5111 | size_t *plaintext_length ) |
| 5112 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5113 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5114 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5115 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5116 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5117 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5118 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5119 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5120 | if( status != PSA_SUCCESS ) |
| 5121 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5122 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5123 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5124 | ciphertext, ciphertext_length, |
| 5125 | plaintext_size, &tag ); |
| 5126 | if( status != PSA_SUCCESS ) |
| 5127 | goto exit; |
| 5128 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5129 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5130 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5131 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5132 | status = mbedtls_to_psa_error( |
| 5133 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5134 | ciphertext_length - operation.tag_length, |
| 5135 | nonce, nonce_length, |
| 5136 | additional_data, |
| 5137 | additional_data_length, |
| 5138 | tag, operation.tag_length, |
| 5139 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5140 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5141 | else |
| 5142 | #endif /* MBEDTLS_GCM_C */ |
| 5143 | #if defined(MBEDTLS_CCM_C) |
| 5144 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5145 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5146 | status = mbedtls_to_psa_error( |
| 5147 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5148 | ciphertext_length - operation.tag_length, |
| 5149 | nonce, nonce_length, |
| 5150 | additional_data, |
| 5151 | additional_data_length, |
| 5152 | ciphertext, plaintext, |
| 5153 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5154 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5155 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5156 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5157 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5158 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5159 | { |
| 5160 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5161 | { |
| 5162 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5163 | goto exit; |
| 5164 | } |
| 5165 | status = mbedtls_to_psa_error( |
| 5166 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5167 | ciphertext_length - operation.tag_length, |
| 5168 | nonce, |
| 5169 | additional_data, |
| 5170 | additional_data_length, |
| 5171 | tag, |
| 5172 | ciphertext, |
| 5173 | plaintext ) ); |
| 5174 | } |
| 5175 | else |
| 5176 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5177 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5178 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5179 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5180 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5181 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5182 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5183 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5184 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5185 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5186 | if( status == PSA_SUCCESS ) |
| 5187 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5188 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5189 | } |
| 5190 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5191 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5192 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5193 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5194 | /* Generators */ |
| 5195 | /****************************************************************/ |
| 5196 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5197 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 5198 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5199 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5200 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 5201 | #endif |
| 5202 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5203 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5204 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5205 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5206 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5207 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5208 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5209 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5210 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5211 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5212 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5213 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5214 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5215 | } |
| 5216 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5217 | 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] | 5218 | { |
| 5219 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5220 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5221 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5222 | { |
| 5223 | /* The object has (apparently) been initialized but it is not |
| 5224 | * in use. It's ok to call abort on such an object, and there's |
| 5225 | * nothing to do. */ |
| 5226 | } |
| 5227 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5228 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5229 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5230 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5231 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5232 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5233 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5234 | else |
| 5235 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5236 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5237 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5238 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5239 | /* 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] | 5240 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5241 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5242 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5243 | { |
| 5244 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5245 | operation->ctx.tls12_prf.seed_length ); |
| 5246 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5247 | } |
| 5248 | |
| 5249 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5250 | { |
| 5251 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5252 | operation->ctx.tls12_prf.label_length ); |
| 5253 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5254 | } |
| 5255 | |
| 5256 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5257 | |
| 5258 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5259 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5260 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5261 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5262 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5263 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5264 | { |
| 5265 | status = PSA_ERROR_BAD_STATE; |
| 5266 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5267 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5268 | return( status ); |
| 5269 | } |
| 5270 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5271 | 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] | 5272 | size_t *capacity) |
| 5273 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5274 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5275 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5276 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5277 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5280 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5281 | return( PSA_SUCCESS ); |
| 5282 | } |
| 5283 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5284 | 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] | 5285 | size_t capacity ) |
| 5286 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5287 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5288 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5289 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5291 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5292 | return( PSA_SUCCESS ); |
| 5293 | } |
| 5294 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5296 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5297 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5298 | 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] | 5299 | psa_algorithm_t hash_alg, |
| 5300 | uint8_t *output, |
| 5301 | size_t output_length ) |
| 5302 | { |
| 5303 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5304 | psa_status_t status; |
| 5305 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5306 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5307 | return( PSA_ERROR_BAD_STATE ); |
| 5308 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5309 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5310 | while( output_length != 0 ) |
| 5311 | { |
| 5312 | /* Copy what remains of the current block */ |
| 5313 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5314 | if( n > output_length ) |
| 5315 | n = (uint8_t) output_length; |
| 5316 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5317 | output += n; |
| 5318 | output_length -= n; |
| 5319 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5320 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5321 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5322 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5323 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5324 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5325 | * object was corrupted or if this function is called directly |
| 5326 | * inside the library. */ |
| 5327 | if( hkdf->block_number == 0xff ) |
| 5328 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5329 | |
| 5330 | /* We need a new block */ |
| 5331 | ++hkdf->block_number; |
| 5332 | hkdf->offset_in_block = 0; |
| 5333 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5334 | hkdf->prk, hash_length, |
| 5335 | hash_alg ); |
| 5336 | if( status != PSA_SUCCESS ) |
| 5337 | return( status ); |
| 5338 | if( hkdf->block_number != 1 ) |
| 5339 | { |
| 5340 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5341 | hkdf->output_block, |
| 5342 | hash_length ); |
| 5343 | if( status != PSA_SUCCESS ) |
| 5344 | return( status ); |
| 5345 | } |
| 5346 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5347 | hkdf->info, |
| 5348 | hkdf->info_length ); |
| 5349 | if( status != PSA_SUCCESS ) |
| 5350 | return( status ); |
| 5351 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5352 | &hkdf->block_number, 1 ); |
| 5353 | if( status != PSA_SUCCESS ) |
| 5354 | return( status ); |
| 5355 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5356 | hkdf->output_block, |
| 5357 | sizeof( hkdf->output_block ) ); |
| 5358 | if( status != PSA_SUCCESS ) |
| 5359 | return( status ); |
| 5360 | } |
| 5361 | |
| 5362 | return( PSA_SUCCESS ); |
| 5363 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5364 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5365 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5366 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5367 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5368 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5369 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5370 | psa_algorithm_t alg ) |
| 5371 | { |
| 5372 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 5373 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5374 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5375 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5376 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5377 | /* We can't be wanting more output after block 0xff, otherwise |
| 5378 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5379 | * prevented this call. It could happen only if the operation |
| 5380 | * object was corrupted or if this function is called directly |
| 5381 | * inside the library. */ |
| 5382 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5383 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5384 | |
| 5385 | /* We need a new block */ |
| 5386 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5387 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5388 | |
| 5389 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5390 | * |
| 5391 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5392 | * |
| 5393 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5394 | * HMAC_hash(secret, A(2) + seed) + |
| 5395 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5396 | * |
| 5397 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5398 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5399 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5400 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5401 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5402 | * is currently extracted as `output_block` and where i is |
| 5403 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5404 | */ |
| 5405 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5406 | /* Save the hash context before using it, to preserve the hash state with |
| 5407 | * only the inner padding in it. We need this, because inner padding depends |
| 5408 | * on the key (secret in the RFC's terminology). */ |
| 5409 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5410 | if( status != PSA_SUCCESS ) |
| 5411 | goto cleanup; |
| 5412 | |
| 5413 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5414 | if( tls12_prf->block_number == 1 ) |
| 5415 | { |
| 5416 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5417 | * the variable seed and in this instance means it in the context of the |
| 5418 | * P_hash function, where seed = label + seed.) */ |
| 5419 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5420 | tls12_prf->label, tls12_prf->label_length ); |
| 5421 | if( status != PSA_SUCCESS ) |
| 5422 | goto cleanup; |
| 5423 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5424 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5425 | if( status != PSA_SUCCESS ) |
| 5426 | goto cleanup; |
| 5427 | } |
| 5428 | else |
| 5429 | { |
| 5430 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5431 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5432 | tls12_prf->Ai, hash_length ); |
| 5433 | if( status != PSA_SUCCESS ) |
| 5434 | goto cleanup; |
| 5435 | } |
| 5436 | |
| 5437 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5438 | tls12_prf->Ai, hash_length ); |
| 5439 | if( status != PSA_SUCCESS ) |
| 5440 | goto cleanup; |
| 5441 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5442 | if( status != PSA_SUCCESS ) |
| 5443 | goto cleanup; |
| 5444 | |
| 5445 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5446 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5447 | tls12_prf->Ai, hash_length ); |
| 5448 | if( status != PSA_SUCCESS ) |
| 5449 | goto cleanup; |
| 5450 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5451 | tls12_prf->label, tls12_prf->label_length ); |
| 5452 | if( status != PSA_SUCCESS ) |
| 5453 | goto cleanup; |
| 5454 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5455 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5456 | if( status != PSA_SUCCESS ) |
| 5457 | goto cleanup; |
| 5458 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5459 | tls12_prf->output_block, hash_length ); |
| 5460 | if( status != PSA_SUCCESS ) |
| 5461 | goto cleanup; |
| 5462 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5463 | if( status != PSA_SUCCESS ) |
| 5464 | goto cleanup; |
| 5465 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5466 | |
| 5467 | cleanup: |
| 5468 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5469 | cleanup_status = psa_hash_abort( &backup ); |
| 5470 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5471 | status = cleanup_status; |
| 5472 | |
| 5473 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5474 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5475 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5476 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5477 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5478 | psa_algorithm_t alg, |
| 5479 | uint8_t *output, |
| 5480 | size_t output_length ) |
| 5481 | { |
| 5482 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5483 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5484 | psa_status_t status; |
| 5485 | uint8_t offset, length; |
| 5486 | |
| 5487 | while( output_length != 0 ) |
| 5488 | { |
| 5489 | /* Check if we have fully processed the current block. */ |
| 5490 | if( tls12_prf->left_in_block == 0 ) |
| 5491 | { |
| 5492 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5493 | alg ); |
| 5494 | if( status != PSA_SUCCESS ) |
| 5495 | return( status ); |
| 5496 | |
| 5497 | continue; |
| 5498 | } |
| 5499 | |
| 5500 | if( tls12_prf->left_in_block > output_length ) |
| 5501 | length = (uint8_t) output_length; |
| 5502 | else |
| 5503 | length = tls12_prf->left_in_block; |
| 5504 | |
| 5505 | offset = hash_length - tls12_prf->left_in_block; |
| 5506 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5507 | output += length; |
| 5508 | output_length -= length; |
| 5509 | tls12_prf->left_in_block -= length; |
| 5510 | } |
| 5511 | |
| 5512 | return( PSA_SUCCESS ); |
| 5513 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5514 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5515 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5516 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5517 | psa_status_t psa_key_derivation_output_bytes( |
| 5518 | psa_key_derivation_operation_t *operation, |
| 5519 | uint8_t *output, |
| 5520 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5521 | { |
| 5522 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5523 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5524 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5525 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5526 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5527 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5528 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5531 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5532 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5533 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5534 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5535 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5536 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5537 | goto exit; |
| 5538 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5539 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5540 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5541 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5542 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5543 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5544 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5545 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5546 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5547 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5548 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5549 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5550 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5551 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5552 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5553 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5554 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5555 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5556 | output, output_length ); |
| 5557 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5558 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5559 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5560 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5561 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5562 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5563 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5564 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5565 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5566 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5567 | output_length ); |
| 5568 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5569 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5570 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5571 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5572 | { |
| 5573 | return( PSA_ERROR_BAD_STATE ); |
| 5574 | } |
| 5575 | |
| 5576 | exit: |
| 5577 | if( status != PSA_SUCCESS ) |
| 5578 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5579 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5580 | * This allows us to differentiate between exhausted operations and |
| 5581 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5582 | * operations. */ |
| 5583 | psa_algorithm_t alg = operation->alg; |
| 5584 | psa_key_derivation_abort( operation ); |
| 5585 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5586 | memset( output, '!', output_length ); |
| 5587 | } |
| 5588 | return( status ); |
| 5589 | } |
| 5590 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5591 | #if defined(MBEDTLS_DES_C) |
| 5592 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5593 | { |
| 5594 | if( data_size >= 8 ) |
| 5595 | mbedtls_des_key_set_parity( data ); |
| 5596 | if( data_size >= 16 ) |
| 5597 | mbedtls_des_key_set_parity( data + 8 ); |
| 5598 | if( data_size >= 24 ) |
| 5599 | mbedtls_des_key_set_parity( data + 16 ); |
| 5600 | } |
| 5601 | #endif /* MBEDTLS_DES_C */ |
| 5602 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5603 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5604 | psa_key_slot_t *slot, |
| 5605 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5606 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5607 | { |
| 5608 | uint8_t *data = NULL; |
| 5609 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5610 | psa_status_t status; |
| 5611 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5612 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5613 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5614 | if( bits % 8 != 0 ) |
| 5615 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5616 | data = mbedtls_calloc( 1, bytes ); |
| 5617 | if( data == NULL ) |
| 5618 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5619 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5620 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5621 | if( status != PSA_SUCCESS ) |
| 5622 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5623 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5624 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5625 | psa_des_set_key_parity( data, bytes ); |
| 5626 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5627 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5628 | |
| 5629 | exit: |
| 5630 | mbedtls_free( data ); |
| 5631 | return( status ); |
| 5632 | } |
| 5633 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5634 | 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] | 5635 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5636 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5637 | { |
| 5638 | psa_status_t status; |
| 5639 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5640 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5641 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5642 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5643 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5644 | /* Reject any attempt to create a zero-length key so that we don't |
| 5645 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5646 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5647 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5648 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5649 | if( ! operation->can_output_key ) |
| 5650 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5651 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5652 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5653 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5654 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5655 | if( driver != NULL ) |
| 5656 | { |
| 5657 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5658 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5659 | } |
| 5660 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5661 | if( status == PSA_SUCCESS ) |
| 5662 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5663 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5664 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5665 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5666 | } |
| 5667 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5668 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5669 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5670 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5671 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5672 | return( status ); |
| 5673 | } |
| 5674 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5675 | |
| 5676 | |
| 5677 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5678 | /* Key derivation */ |
| 5679 | /****************************************************************/ |
| 5680 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5681 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5682 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5683 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5684 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5685 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5686 | int is_kdf_alg_supported; |
| 5687 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5688 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5689 | * initialisers for this union leave some bytes unspecified.) */ |
| 5690 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5691 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5692 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5693 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5694 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5695 | is_kdf_alg_supported = 1; |
| 5696 | else |
| 5697 | #endif |
| 5698 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5699 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5700 | is_kdf_alg_supported = 1; |
| 5701 | else |
| 5702 | #endif |
| 5703 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5704 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5705 | is_kdf_alg_supported = 1; |
| 5706 | else |
| 5707 | #endif |
| 5708 | is_kdf_alg_supported = 0; |
| 5709 | |
| 5710 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5711 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5712 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5713 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5714 | if( hash_size == 0 ) |
| 5715 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5716 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5717 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5718 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5719 | { |
| 5720 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5721 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5722 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5723 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5724 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5725 | |
| 5726 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5727 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5728 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5729 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5730 | 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] | 5731 | psa_algorithm_t alg ) |
| 5732 | { |
| 5733 | psa_status_t status; |
| 5734 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5735 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5736 | return( PSA_ERROR_BAD_STATE ); |
| 5737 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5738 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5739 | return( PSA_ERROR_INVALID_ARGUMENT ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5740 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5741 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5742 | { |
| 5743 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5744 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5745 | } |
| 5746 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5747 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5748 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5749 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5750 | #endif |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5751 | else |
| 5752 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5753 | |
| 5754 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5755 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5756 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5757 | } |
| 5758 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5759 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5760 | 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] | 5761 | psa_algorithm_t hash_alg, |
| 5762 | psa_key_derivation_step_t step, |
| 5763 | const uint8_t *data, |
| 5764 | size_t data_length ) |
| 5765 | { |
| 5766 | psa_status_t status; |
| 5767 | switch( step ) |
| 5768 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5769 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5770 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5771 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5772 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5773 | data, data_length, |
| 5774 | hash_alg ); |
| 5775 | if( status != PSA_SUCCESS ) |
| 5776 | return( status ); |
| 5777 | hkdf->state = HKDF_STATE_STARTED; |
| 5778 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5779 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5780 | /* If no salt was provided, use an empty salt. */ |
| 5781 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5782 | { |
| 5783 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5784 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5785 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5786 | if( status != PSA_SUCCESS ) |
| 5787 | return( status ); |
| 5788 | hkdf->state = HKDF_STATE_STARTED; |
| 5789 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5790 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5791 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5792 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5793 | data, data_length ); |
| 5794 | if( status != PSA_SUCCESS ) |
| 5795 | return( status ); |
| 5796 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5797 | hkdf->prk, |
| 5798 | sizeof( hkdf->prk ) ); |
| 5799 | if( status != PSA_SUCCESS ) |
| 5800 | return( status ); |
| 5801 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5802 | hkdf->block_number = 0; |
| 5803 | hkdf->state = HKDF_STATE_KEYED; |
| 5804 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5805 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5806 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5807 | return( PSA_ERROR_BAD_STATE ); |
| 5808 | if( hkdf->info_set ) |
| 5809 | return( PSA_ERROR_BAD_STATE ); |
| 5810 | hkdf->info_length = data_length; |
| 5811 | if( data_length != 0 ) |
| 5812 | { |
| 5813 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5814 | if( hkdf->info == NULL ) |
| 5815 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5816 | memcpy( hkdf->info, data, data_length ); |
| 5817 | } |
| 5818 | hkdf->info_set = 1; |
| 5819 | return( PSA_SUCCESS ); |
| 5820 | default: |
| 5821 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5822 | } |
| 5823 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5824 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5825 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5826 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5827 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5828 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5829 | const uint8_t *data, |
| 5830 | size_t data_length ) |
| 5831 | { |
| 5832 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5833 | return( PSA_ERROR_BAD_STATE ); |
| 5834 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5835 | if( data_length != 0 ) |
| 5836 | { |
| 5837 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5838 | if( prf->seed == NULL ) |
| 5839 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5840 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5841 | memcpy( prf->seed, data, data_length ); |
| 5842 | prf->seed_length = data_length; |
| 5843 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5844 | |
| 5845 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5846 | |
| 5847 | return( PSA_SUCCESS ); |
| 5848 | } |
| 5849 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5850 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5851 | psa_algorithm_t hash_alg, |
| 5852 | const uint8_t *data, |
| 5853 | size_t data_length ) |
| 5854 | { |
| 5855 | psa_status_t status; |
| 5856 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5857 | return( PSA_ERROR_BAD_STATE ); |
| 5858 | |
| 5859 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5860 | if( status != PSA_SUCCESS ) |
| 5861 | return( status ); |
| 5862 | |
| 5863 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5864 | |
| 5865 | return( PSA_SUCCESS ); |
| 5866 | } |
| 5867 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5868 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5869 | const uint8_t *data, |
| 5870 | size_t data_length ) |
| 5871 | { |
| 5872 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5873 | return( PSA_ERROR_BAD_STATE ); |
| 5874 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5875 | if( data_length != 0 ) |
| 5876 | { |
| 5877 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5878 | if( prf->label == NULL ) |
| 5879 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5880 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5881 | memcpy( prf->label, data, data_length ); |
| 5882 | prf->label_length = data_length; |
| 5883 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5884 | |
| 5885 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5886 | |
| 5887 | return( PSA_SUCCESS ); |
| 5888 | } |
| 5889 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5890 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5891 | psa_algorithm_t hash_alg, |
| 5892 | psa_key_derivation_step_t step, |
| 5893 | const uint8_t *data, |
| 5894 | size_t data_length ) |
| 5895 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5896 | switch( step ) |
| 5897 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5898 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5899 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5900 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5901 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5902 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5903 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5904 | default: |
| 5905 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5906 | } |
| 5907 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5908 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5909 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5910 | |
| 5911 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5912 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5913 | psa_tls12_prf_key_derivation_t *prf, |
| 5914 | psa_algorithm_t hash_alg, |
| 5915 | const uint8_t *data, |
| 5916 | size_t data_length ) |
| 5917 | { |
| 5918 | psa_status_t status; |
| 5919 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5920 | uint8_t *cur = pms; |
| 5921 | |
| 5922 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5923 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5924 | |
| 5925 | /* Quoting RFC 4279, Section 2: |
| 5926 | * |
| 5927 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5928 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5929 | * uint16 with the value N, and the PSK itself. |
| 5930 | */ |
| 5931 | |
| 5932 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5933 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5934 | memset( cur, 0, data_length ); |
| 5935 | cur += data_length; |
| 5936 | *cur++ = pms[0]; |
| 5937 | *cur++ = pms[1]; |
| 5938 | memcpy( cur, data, data_length ); |
| 5939 | cur += data_length; |
| 5940 | |
| 5941 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5942 | |
| 5943 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5944 | return( status ); |
| 5945 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5946 | |
| 5947 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5948 | psa_tls12_prf_key_derivation_t *prf, |
| 5949 | psa_algorithm_t hash_alg, |
| 5950 | psa_key_derivation_step_t step, |
| 5951 | const uint8_t *data, |
| 5952 | size_t data_length ) |
| 5953 | { |
| 5954 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5955 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5956 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5957 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5958 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5959 | |
| 5960 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5961 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5962 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5963 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5964 | /** Check whether the given key type is acceptable for the given |
| 5965 | * input step of a key derivation. |
| 5966 | * |
| 5967 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5968 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5969 | * Both secret and non-secret inputs can alternatively have the type |
| 5970 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5971 | * that the input was passed as a buffer rather than via a key object. |
| 5972 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5973 | static int psa_key_derivation_check_input_type( |
| 5974 | psa_key_derivation_step_t step, |
| 5975 | psa_key_type_t key_type ) |
| 5976 | { |
| 5977 | switch( step ) |
| 5978 | { |
| 5979 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5980 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5981 | return( PSA_SUCCESS ); |
| 5982 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5983 | return( PSA_SUCCESS ); |
| 5984 | break; |
| 5985 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5986 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5987 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5988 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5989 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5990 | return( PSA_SUCCESS ); |
| 5991 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5992 | return( PSA_SUCCESS ); |
| 5993 | break; |
| 5994 | } |
| 5995 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5996 | } |
| 5997 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5998 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5999 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6000 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6001 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6002 | const uint8_t *data, |
| 6003 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6004 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6005 | psa_status_t status; |
| 6006 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 6007 | |
| 6008 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6009 | if( status != PSA_SUCCESS ) |
| 6010 | goto exit; |
| 6011 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 6012 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6013 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6014 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6015 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6016 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6017 | step, data, data_length ); |
| 6018 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6019 | else |
| 6020 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 6021 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 6022 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6023 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6024 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 6025 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6026 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6027 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6028 | else |
| 6029 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6030 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6031 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6032 | { |
| 6033 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 6034 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6035 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6036 | } |
| 6037 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6038 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6039 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6040 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6041 | return( PSA_ERROR_BAD_STATE ); |
| 6042 | } |
| 6043 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6044 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6045 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6046 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6047 | return( status ); |
| 6048 | } |
| 6049 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6050 | psa_status_t psa_key_derivation_input_bytes( |
| 6051 | psa_key_derivation_operation_t *operation, |
| 6052 | psa_key_derivation_step_t step, |
| 6053 | const uint8_t *data, |
| 6054 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6055 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6056 | return( psa_key_derivation_input_internal( operation, step, |
| 6057 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 6058 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6059 | } |
| 6060 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6061 | psa_status_t psa_key_derivation_input_key( |
| 6062 | psa_key_derivation_operation_t *operation, |
| 6063 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6064 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6065 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6066 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6067 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6068 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6069 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6070 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6071 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6072 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6073 | { |
| 6074 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6075 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6076 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6077 | |
| 6078 | /* Passing a key object as a SECRET input unlocks the permission |
| 6079 | * to output to a key object. */ |
| 6080 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6081 | operation->can_output_key = 1; |
| 6082 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6083 | status = psa_key_derivation_input_internal( operation, |
| 6084 | step, slot->attr.type, |
| 6085 | slot->data.key.data, |
| 6086 | slot->data.key.bytes ); |
| 6087 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6088 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6089 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6090 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6091 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6092 | |
| 6093 | |
| 6094 | |
| 6095 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6096 | /* Key agreement */ |
| 6097 | /****************************************************************/ |
| 6098 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6099 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6100 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 6101 | size_t peer_key_length, |
| 6102 | const mbedtls_ecp_keypair *our_key, |
| 6103 | uint8_t *shared_secret, |
| 6104 | size_t shared_secret_size, |
| 6105 | size_t *shared_secret_length ) |
| 6106 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 6107 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6108 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6109 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6110 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6111 | 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] | 6112 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6113 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6114 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 6115 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6116 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6117 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6118 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6119 | goto exit; |
| 6120 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6121 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6122 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6123 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6124 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6125 | status = mbedtls_to_psa_error( |
| 6126 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 6127 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6128 | goto exit; |
| 6129 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6130 | status = mbedtls_to_psa_error( |
| 6131 | mbedtls_ecdh_calc_secret( &ecdh, |
| 6132 | shared_secret_length, |
| 6133 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6134 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6135 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6136 | if( status != PSA_SUCCESS ) |
| 6137 | goto exit; |
| 6138 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 6139 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6140 | |
| 6141 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 6142 | if( status != PSA_SUCCESS ) |
| 6143 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6144 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6145 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6146 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6147 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6148 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6149 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6150 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6151 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6152 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6153 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6154 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6155 | psa_key_slot_t *private_key, |
| 6156 | const uint8_t *peer_key, |
| 6157 | size_t peer_key_length, |
| 6158 | uint8_t *shared_secret, |
| 6159 | size_t shared_secret_size, |
| 6160 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6161 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6162 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6163 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6164 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6165 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6166 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6167 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6168 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6169 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6170 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6171 | private_key->data.key.data, |
| 6172 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6173 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6174 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6175 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6176 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6177 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6178 | shared_secret, shared_secret_size, |
| 6179 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6180 | mbedtls_ecp_keypair_free( ecp ); |
| 6181 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6182 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6183 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6184 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6185 | (void) private_key; |
| 6186 | (void) peer_key; |
| 6187 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6188 | (void) shared_secret; |
| 6189 | (void) shared_secret_size; |
| 6190 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6191 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6192 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6193 | } |
| 6194 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6195 | /* 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] | 6196 | * to potentially free embedded data structures and wipe confidential data. |
| 6197 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6198 | 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] | 6199 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6200 | psa_key_slot_t *private_key, |
| 6201 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6202 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6203 | { |
| 6204 | psa_status_t status; |
| 6205 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6206 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6207 | 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] | 6208 | |
| 6209 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6210 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6211 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6212 | private_key, |
| 6213 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6214 | shared_secret, |
| 6215 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6216 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6217 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6218 | goto exit; |
| 6219 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6220 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6221 | * the shared secret. A shared secret is permitted wherever a key |
| 6222 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6223 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6224 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6225 | shared_secret, |
| 6226 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6227 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6228 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6229 | return( status ); |
| 6230 | } |
| 6231 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6232 | 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] | 6233 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6234 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6235 | const uint8_t *peer_key, |
| 6236 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6237 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6238 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6239 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6240 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6241 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6242 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6244 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6245 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6246 | if( status != PSA_SUCCESS ) |
| 6247 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6248 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6249 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6250 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6251 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6252 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6253 | else |
| 6254 | { |
| 6255 | /* If a private key has been added as SECRET, we allow the derived |
| 6256 | * key material to be used as a key in PSA Crypto. */ |
| 6257 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6258 | operation->can_output_key = 1; |
| 6259 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6260 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6261 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6262 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6263 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6264 | } |
| 6265 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6266 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6267 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6268 | const uint8_t *peer_key, |
| 6269 | size_t peer_key_length, |
| 6270 | uint8_t *output, |
| 6271 | size_t output_size, |
| 6272 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6273 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6274 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6275 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6276 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6277 | |
| 6278 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6279 | { |
| 6280 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6281 | goto exit; |
| 6282 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6283 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6284 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6285 | if( status != PSA_SUCCESS ) |
| 6286 | goto exit; |
| 6287 | |
| 6288 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6289 | peer_key, peer_key_length, |
| 6290 | output, output_size, |
| 6291 | output_length ); |
| 6292 | |
| 6293 | exit: |
| 6294 | if( status != PSA_SUCCESS ) |
| 6295 | { |
| 6296 | /* If an error happens and is not handled properly, the output |
| 6297 | * may be used as a key to protect sensitive data. Arrange for such |
| 6298 | * a key to be random, which is likely to result in decryption or |
| 6299 | * verification errors. This is better than filling the buffer with |
| 6300 | * some constant data such as zeros, which would result in the data |
| 6301 | * being protected with a reproducible, easily knowable key. |
| 6302 | */ |
| 6303 | psa_generate_random( output, output_size ); |
| 6304 | *output_length = output_size; |
| 6305 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6306 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6307 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6308 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6309 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6310 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6311 | |
| 6312 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6313 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6314 | /****************************************************************/ |
| 6315 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6316 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6317 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6318 | /** Initialize the PSA random generator. |
| 6319 | */ |
| 6320 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6321 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6322 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6323 | memset( rng, 0, sizeof( *rng ) ); |
| 6324 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6325 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6326 | /* Set default configuration if |
| 6327 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6328 | if( rng->entropy_init == NULL ) |
| 6329 | rng->entropy_init = mbedtls_entropy_init; |
| 6330 | if( rng->entropy_free == NULL ) |
| 6331 | rng->entropy_free = mbedtls_entropy_free; |
| 6332 | |
| 6333 | rng->entropy_init( &rng->entropy ); |
| 6334 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6335 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6336 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6337 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6338 | mbedtls_entropy_add_source( &rng->entropy, |
| 6339 | mbedtls_nv_seed_poll, NULL, |
| 6340 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6341 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6342 | #endif |
| 6343 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6344 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6345 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6346 | } |
| 6347 | |
| 6348 | /** Deinitialize the PSA random generator. |
| 6349 | */ |
| 6350 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6351 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6352 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6353 | memset( rng, 0, sizeof( *rng ) ); |
| 6354 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6355 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6356 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6357 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6358 | } |
| 6359 | |
| 6360 | /** Seed the PSA random generator. |
| 6361 | */ |
| 6362 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6363 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6364 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6365 | /* Do nothing: the external RNG seeds itself. */ |
| 6366 | (void) rng; |
| 6367 | return( PSA_SUCCESS ); |
| 6368 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6369 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6370 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6371 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6372 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6373 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6374 | } |
| 6375 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6376 | psa_status_t psa_generate_random( uint8_t *output, |
| 6377 | size_t output_size ) |
| 6378 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6379 | GUARD_MODULE_INITIALIZED; |
| 6380 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6381 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6382 | |
| 6383 | size_t output_length = 0; |
| 6384 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6385 | output, output_size, |
| 6386 | &output_length ); |
| 6387 | if( status != PSA_SUCCESS ) |
| 6388 | return( status ); |
| 6389 | /* Breaking up a request into smaller chunks is currently not supported |
| 6390 | * for the extrernal RNG interface. */ |
| 6391 | if( output_length != output_size ) |
| 6392 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6393 | return( PSA_SUCCESS ); |
| 6394 | |
| 6395 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6396 | |
| 6397 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6398 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6399 | while( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6400 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6401 | ret = mbedtls_psa_get_random( |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6402 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6403 | output, MBEDTLS_PSA_RANDOM_MAX_REQUEST ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6404 | if( ret != 0 ) |
| 6405 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6406 | output += MBEDTLS_PSA_RANDOM_MAX_REQUEST; |
| 6407 | output_size -= MBEDTLS_PSA_RANDOM_MAX_REQUEST; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6408 | } |
| 6409 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6410 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6411 | output, output_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6412 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6413 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6414 | } |
| 6415 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6416 | /* Wrapper function allowing the classic API to use the PSA RNG. |
| 6417 | * In the non-external case, mbedtls_psa_get_random is defined |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame^] | 6418 | * as a constant function pointer in psa_crypto_random_impl.h. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6419 | */ |
| 6420 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6421 | int mbedtls_psa_get_random( void *p_rng, |
| 6422 | unsigned char *output, |
| 6423 | size_t output_size ) |
| 6424 | { |
| 6425 | (void) p_rng; |
| 6426 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6427 | if( status == PSA_SUCCESS ) |
| 6428 | return( 0 ); |
| 6429 | else |
| 6430 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6431 | } |
| 6432 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6433 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6434 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6435 | #include "mbedtls/entropy_poll.h" |
| 6436 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6437 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6438 | size_t seed_size ) |
| 6439 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6440 | if( global_data.initialized ) |
| 6441 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6442 | |
| 6443 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6444 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6445 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6446 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6447 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6448 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6449 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6450 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6451 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6452 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6453 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6454 | size_t domain_parameters_size, |
| 6455 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6456 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6457 | size_t i; |
| 6458 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6459 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6460 | if( domain_parameters_size == 0 ) |
| 6461 | { |
| 6462 | *exponent = 65537; |
| 6463 | return( PSA_SUCCESS ); |
| 6464 | } |
| 6465 | |
| 6466 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6467 | * support values that fit in a 32-bit integer, which is larger than |
| 6468 | * int on just about every platform anyway. */ |
| 6469 | if( domain_parameters_size > sizeof( acc ) ) |
| 6470 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6471 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6472 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6473 | if( acc > INT_MAX ) |
| 6474 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6475 | *exponent = acc; |
| 6476 | return( PSA_SUCCESS ); |
| 6477 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6478 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6479 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6480 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6481 | psa_key_slot_t *slot, size_t bits, |
| 6482 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6483 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6484 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6485 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6486 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6487 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6488 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6489 | if( key_type_is_raw_bytes( type ) ) |
| 6490 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6491 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6492 | |
| 6493 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6494 | if( status != PSA_SUCCESS ) |
| 6495 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6496 | |
| 6497 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6498 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6499 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6500 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6501 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6502 | status = psa_generate_random( slot->data.key.data, |
| 6503 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6504 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6505 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6506 | |
| 6507 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6508 | #if defined(MBEDTLS_DES_C) |
| 6509 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6510 | psa_des_set_key_parity( slot->data.key.data, |
| 6511 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6512 | #endif /* MBEDTLS_DES_C */ |
| 6513 | } |
| 6514 | else |
| 6515 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6516 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6517 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6518 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6519 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6520 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6521 | int exponent; |
| 6522 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6523 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6524 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6525 | /* Accept only byte-aligned keys, for the same reasons as |
| 6526 | * in psa_import_rsa_key(). */ |
| 6527 | if( bits % 8 != 0 ) |
| 6528 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6529 | status = psa_read_rsa_exponent( domain_parameters, |
| 6530 | domain_parameters_size, |
| 6531 | &exponent ); |
| 6532 | if( status != PSA_SUCCESS ) |
| 6533 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6534 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6535 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6536 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6537 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6538 | (unsigned int) bits, |
| 6539 | exponent ); |
| 6540 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6541 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6542 | |
| 6543 | /* Make sure to always have an export representation available */ |
| 6544 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6545 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6546 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6547 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6548 | { |
| 6549 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6550 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6551 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6552 | |
| 6553 | status = psa_export_rsa_key( type, |
| 6554 | &rsa, |
| 6555 | slot->data.key.data, |
| 6556 | bytes, |
| 6557 | &slot->data.key.bytes ); |
| 6558 | mbedtls_rsa_free( &rsa ); |
| 6559 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6560 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6561 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6562 | } |
| 6563 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6564 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6565 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6566 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6567 | 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] | 6568 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6569 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6570 | mbedtls_ecp_group_id grp_id = |
| 6571 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6572 | const mbedtls_ecp_curve_info *curve_info = |
| 6573 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6574 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6575 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6576 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6577 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6578 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6579 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6580 | mbedtls_ecp_keypair_init( &ecp ); |
| 6581 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6582 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6583 | MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6584 | if( ret != 0 ) |
| 6585 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6586 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6587 | return( mbedtls_to_psa_error( ret ) ); |
| 6588 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6589 | |
| 6590 | |
| 6591 | /* Make sure to always have an export representation available */ |
| 6592 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6593 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6594 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6595 | { |
| 6596 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6597 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6598 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6599 | |
| 6600 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6601 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 6602 | |
| 6603 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6604 | if( status != PSA_SUCCESS ) { |
| 6605 | memset( slot->data.key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6606 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6607 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6608 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6609 | } |
| 6610 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6611 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6612 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6613 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6614 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6615 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6616 | return( PSA_SUCCESS ); |
| 6617 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6618 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6619 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6620 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6621 | { |
| 6622 | psa_status_t status; |
| 6623 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6624 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6625 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6626 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6627 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6628 | /* Reject any attempt to create a zero-length key so that we don't |
| 6629 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6630 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6631 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6632 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6633 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6634 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6635 | if( status != PSA_SUCCESS ) |
| 6636 | goto exit; |
| 6637 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6638 | status = psa_driver_wrapper_generate_key( attributes, |
| 6639 | slot ); |
| 6640 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6641 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6642 | goto exit; |
| 6643 | |
| 6644 | status = psa_generate_key_internal( |
| 6645 | slot, attributes->core.bits, |
| 6646 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6647 | |
| 6648 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6649 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6650 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6651 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6652 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6653 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6654 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6658 | |
| 6659 | /****************************************************************/ |
| 6660 | /* Module setup */ |
| 6661 | /****************************************************************/ |
| 6662 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6663 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6664 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6665 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6666 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6667 | { |
| 6668 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6669 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6670 | global_data.rng.entropy_init = entropy_init; |
| 6671 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6672 | return( PSA_SUCCESS ); |
| 6673 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6674 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6675 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6676 | void mbedtls_psa_crypto_free( void ) |
| 6677 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6678 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6679 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6680 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6681 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6682 | } |
| 6683 | /* Wipe all remaining data, including configuration. |
| 6684 | * In particular, this sets all state indicator to the value |
| 6685 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6686 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6687 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6688 | /* Unregister all secure element drivers, so that we restart from |
| 6689 | * a pristine state. */ |
| 6690 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6691 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6692 | } |
| 6693 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6694 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6695 | /** Recover a transaction that was interrupted by a power failure. |
| 6696 | * |
| 6697 | * This function is called during initialization, before psa_crypto_init() |
| 6698 | * returns. If this function returns a failure status, the initialization |
| 6699 | * fails. |
| 6700 | */ |
| 6701 | static psa_status_t psa_crypto_recover_transaction( |
| 6702 | const psa_crypto_transaction_t *transaction ) |
| 6703 | { |
| 6704 | switch( transaction->unknown.type ) |
| 6705 | { |
| 6706 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6707 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6708 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6709 | * is implemented. |
| 6710 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6711 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6712 | default: |
| 6713 | /* We found an unsupported transaction in the storage. |
| 6714 | * We don't know what state the storage is in. Give up. */ |
| 6715 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6716 | } |
| 6717 | } |
| 6718 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6719 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6720 | psa_status_t psa_crypto_init( void ) |
| 6721 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6722 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6723 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6724 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6725 | if( global_data.initialized != 0 ) |
| 6726 | return( PSA_SUCCESS ); |
| 6727 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6728 | /* Initialize and seed the random generator. */ |
| 6729 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6730 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6731 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6732 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6733 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6734 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6735 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6736 | status = psa_initialize_key_slots( ); |
| 6737 | if( status != PSA_SUCCESS ) |
| 6738 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6739 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6740 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6741 | status = psa_init_all_se_drivers( ); |
| 6742 | if( status != PSA_SUCCESS ) |
| 6743 | goto exit; |
| 6744 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6745 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6746 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6747 | status = psa_crypto_load_transaction( ); |
| 6748 | if( status == PSA_SUCCESS ) |
| 6749 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6750 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6751 | if( status != PSA_SUCCESS ) |
| 6752 | goto exit; |
| 6753 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6754 | } |
| 6755 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6756 | { |
| 6757 | /* There's no transaction to complete. It's all good. */ |
| 6758 | status = PSA_SUCCESS; |
| 6759 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6760 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6761 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6762 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6763 | global_data.initialized = 1; |
| 6764 | |
| 6765 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6766 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6767 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6768 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |